From efbd37dd7f493316f3632ec393fa873b7257a123 Mon Sep 17 00:00:00 2001 From: Johannes Burk Date: Sat, 11 Jun 2016 01:18:32 +0200 Subject: [PATCH] Use Messaging API instead of email_to_user() function --- db/messages.php | 40 +++++++++++++++++++++++ lang/en/enrol_apply.php | 10 ++++++ lib.php | 71 ++++++++++++++++++++++++++++++----------- notification.php | 64 +++++++++++++++++++++++++++++++++++++ version.php | 2 +- 5 files changed, 168 insertions(+), 19 deletions(-) create mode 100644 db/messages.php create mode 100644 notification.php diff --git a/db/messages.php b/db/messages.php new file mode 100644 index 0000000..a86fbbb --- /dev/null +++ b/db/messages.php @@ -0,0 +1,40 @@ +. + +/** + * @package enrol_apply + * @copyright 2016 sudile GbR (http://www.sudile.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Johannes Burk + */ + +defined('MOODLE_INTERNAL') || die(); + +$messageproviders = array ( + // Notify teacher/manager that a student has applied for a course enrolment. + 'application' => array ( + 'capability' => 'enrol/apply:manageapplications' + ), + + // Notify student that his application was confirmed. + 'confirmation' => array (), + + // Notify student that his application was canceled. + 'cancelation' => array (), + + // Notify student that his application was deferred (put on a waiting list). + 'waitinglist' => array (), +); diff --git a/lang/en/enrol_apply.php b/lang/en/enrol_apply.php index 9c8e99d..4f6a3aa 100644 --- a/lang/en/enrol_apply.php +++ b/lang/en/enrol_apply.php @@ -53,6 +53,16 @@ $string['notify_desc'] = 'Define who gets notified about new enrolment applicati $string['sendmailtoteacher'] = 'Send email notification to teachers'; $string['sendmailtomanager'] = 'Send email notification to managers'; +$string['messageprovider:application'] = 'Course enrolment application notifications'; +$string['messageprovider:confirmation'] = 'Course enrolment application confirmation notifications'; +$string['messageprovider:cancelation'] = 'Course enrolment application cancelation notifications'; +$string['messageprovider:waitinglist'] = 'Course enrolment application defer notifications'; + +$string['newapplicationnotification'] = 'There is a new course enrolment application awaiting review.'; +$string['applicationconfirmednotification'] = 'Your course enrolment application was confirmed.'; +$string['applicationcancelednotification'] = 'Your course enrolment application was canceled.'; +$string['applicationdeferrednotification'] = 'Your course enrolment application was deferred (you are currently on the waiting list).'; + $string['confirmusers'] = 'Enrol Confirm'; $string['confirmusers_desc'] = 'Users in gray colored rows are on the waiting list.'; diff --git a/lib.php b/lib.php index 5cf85ab..194759b 100644 --- a/lib.php +++ b/lib.php @@ -230,9 +230,12 @@ class enrol_apply_plugin extends enrol_plugin { $this->update_user_enrol($instance, $userenrolment->userid, ENROL_USER_ACTIVE); $DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol)); - $subject = get_config('enrol_apply', 'confirmmailsubject'); - $body = get_config('enrol_apply', 'confirmmailcontent'); - $this->send_mail_to_applicant($instance, $userenrolment->userid, $subject, $body); + $this->notify_applicant( + $instance, + $userenrolment->userid, + 'confirmation', + get_config('enrol_apply', 'confirmmailsubject'), + get_config('enrol_apply', 'confirmmailcontent')); } } @@ -255,9 +258,12 @@ class enrol_apply_plugin extends enrol_plugin { $this->update_user_enrol($instance, $userenrolment->userid, ENROL_APPLY_USER_WAIT); - $subject = get_config('enrol_apply', 'waitmailsubject'); - $body = get_config('enrol_apply', 'waitmailcontent'); - $this->send_mail_to_applicant($instance, $userenrolment->userid, $subject, $body); + $this->notify_applicant( + $instance, + $userenrolment->userid, + 'waitinglist', + get_config('enrol_apply', 'waitmailsubject'), + get_config('enrol_apply', 'waitmailcontent')); } } } @@ -286,26 +292,41 @@ class enrol_apply_plugin extends enrol_plugin { $this->unenrol_user($instance, $userenrolment->userid); $DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol)); - $subject = get_config('enrol_apply', 'cancelmailsubject'); - $body = get_config('enrol_apply', 'cancelmailcontent'); - $this->send_mail_to_applicant($instance, $userenrolment->userid, $subject, $body); + $this->notify_applicant( + $instance, + $userenrolment->userid, + 'cancelation', + get_config('enrol_apply', 'cancelmailsubject'), + get_config('enrol_apply', 'cancelmailcontent')); } } - private function send_mail_to_applicant($instance, $userid, $subject, $body) { - global $DB; + private function notify_applicant($instance, $userid, $type, $subject, $content) { global $CFG; + require_once($CFG->dirroot.'/enrol/apply/notification.php'); + // Required for course_get_url() function. + require_once($CFG->dirroot.'/course/lib.php'); $course = get_course($instance->courseid); $user = core_user::get_user($userid); - $body = $this->update_mail_content($body, $course, $user); - $contact = core_user::get_support_user(); - email_to_user($user, $contact, $subject, html_to_text($body), $body); + $content = $this->update_mail_content($content, $course, $user); + + $message = new enrol_apply_notification( + $user, + core_user::get_support_user(), + $type, + $subject, + $content, + course_get_url($course)); + message_send($message); } private function send_application_notification($instance, $userid, $data) { global $CFG, $PAGE; + require_once($CFG->dirroot.'/enrol/apply/notification.php'); + // Required for course_get_url() function. + require_once($CFG->dirroot.'/course/lib.php'); $renderer = $PAGE->get_renderer('enrol_apply'); @@ -336,7 +357,7 @@ class enrol_apply_plugin extends enrol_plugin { $teachers = get_role_users($editingteacherrole->id, $context); $manageurl = new moodle_url("/enrol/apply/manage.php", array('id' => $instance->id)); - $body = $renderer->application_notification_mail_body( + $content = $renderer->application_notification_mail_body( $course, $user, $manageurl, @@ -344,7 +365,14 @@ class enrol_apply_plugin extends enrol_plugin { $standarduserfields, $extrauserfields); foreach ($teachers as $teacher) { - email_to_user($teacher, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); + $message = new enrol_apply_notification( + $teacher, + $contact, + 'application', + get_string('mailtoteacher_suject', 'enrol_apply'), + $content, + $manageurl); + message_send($message); } } @@ -356,7 +384,7 @@ class enrol_apply_plugin extends enrol_plugin { $managers = get_role_users($managerrole->id, $context); $manageurl = new moodle_url('/enrol/apply/manage.php'); - $body = $renderer->application_notification_mail_body( + $content = $renderer->application_notification_mail_body( $course, $user, $manageurl, @@ -364,7 +392,14 @@ class enrol_apply_plugin extends enrol_plugin { $standarduserfields, $extrauserfields); foreach ($managers as $manager) { - email_to_user($manager, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); + $message = new enrol_apply_notification( + $manager, + $contact, + 'application', + get_string('mailtoteacher_suject', 'enrol_apply'), + $content, + $manageurl); + message_send($message); } } } diff --git a/notification.php b/notification.php new file mode 100644 index 0000000..fce3894 --- /dev/null +++ b/notification.php @@ -0,0 +1,64 @@ +. + +/** + * @package enrol_apply + * @copyright 2016 sudile GbR (http://www.sudile.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Johannes Burk + */ + +defined('MOODLE_INTERNAL') || die(); + +class enrol_apply_notification extends \core\message\message { + public function __construct($to, $from, $type, $subject, $content, $url) { + $this->component = 'enrol_apply'; + + switch ($type) { + case 'application': + $this->name = 'application'; + $this->smallmessage = get_string('newapplicationnotification', 'enrol_apply'); + break; + case 'confirmation': + $this->name = 'confirmation'; + $this->smallmessage = get_string('applicationconfirmednotification', 'enrol_apply'); + break; + case 'cancelation': + $this->name = 'cancelation'; + $this->smallmessage = get_string('applicationcancelednotification', 'enrol_apply'); + break; + case 'waitinglist': + $this->name = 'waitinglist'; + $this->smallmessage = get_string('applicationdeferrednotification', 'enrol_apply'); + break; + default: + throw new invalid_parameter_exception('Invalid enrol_apply notification type.'); + break; + } + + $this->userfrom = $from; + $this->userto = $to; + + $this->subject = $subject; + $this->fullmessage = html_to_text($content); + $this->fullmessageformat = FORMAT_PLAIN; + $this->fullmessagehtml = $content; + + $this->notification = true; + $this->contexturl = $url; + $this->contexturlname = get_string('course'); + } +} diff --git a/version.php b/version.php index f78b896..52ed472 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016060800; +$plugin->version = 2016060801; $plugin->requires = 2011080100; $plugin->maturity = MATURITY_STABLE; $plugin->release = 'Enrolment upon approval plugin Version 3.1-a';