Browse Source

Use Messaging API instead of email_to_user() function

SABERES_37_STABLE
Johannes Burk 9 years ago
committed by test
parent
commit
efbd37dd7f
  1. 40
      db/messages.php
  2. 10
      lang/en/enrol_apply.php
  3. 71
      lib.php
  4. 64
      notification.php
  5. 2
      version.php

40
db/messages.php

@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @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 <johannes.burk@sudile.com>
*/
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 (),
);

10
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.';

71
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);
}
}
}

64
notification.php

@ -0,0 +1,64 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @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 <johannes.burk@sudile.com>
*/
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');
}
}

2
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';

Loading…
Cancel
Save