@ -200,8 +200,7 @@ class enrol_apply_plugin extends enrol_plugin {
$fields['roleid'] = $this->get_config('roleid', 0);
$fields['roleid'] = $this->get_config('roleid', 0);
$fields['customint1'] = $this->get_config('show_standard_user_profile');
$fields['customint1'] = $this->get_config('show_standard_user_profile');
$fields['customint2'] = $this->get_config('show_extra_user_profile');
$fields['customint2'] = $this->get_config('show_extra_user_profile');
$fields['customint3'] = $this->get_config('sendmailtoteacher');
$fields['customtext2'] = $this->get_config('notifycoursebased') ? '$@ALL@$' : '';
$fields['customint4'] = $this->get_config('sendmailtomanager');
return $fields;
return $fields;
}
}
@ -349,13 +348,9 @@ class enrol_apply_plugin extends enrol_plugin {
$extrauserfields = $user->profile;
$extrauserfields = $user->profile;
}
}
// Send notification to Teachers? Instance depending.
// Send notification to users with manageapplications in course context (instance depending)?
if ($instance->customint3 == 1) {
$courseuserstonotify = $this->get_notifycoursebased_users($instance);
$context = context_course::instance($instance->courseid);
if (!empty($courseuserstonotify)) {
$editingteacherroles = get_archetype_roles('editingteacher');
$editingteacherrole = reset($editingteacherroles);
$teachers = get_role_users($editingteacherrole->id, $context);
$manageurl = new moodle_url("/enrol/apply/manage.php", array('id' => $instance->id));
$manageurl = new moodle_url("/enrol/apply/manage.php", array('id' => $instance->id));
$content = $renderer->application_notification_mail_body(
$content = $renderer->application_notification_mail_body(
$course,
$course,
@ -364,9 +359,9 @@ class enrol_apply_plugin extends enrol_plugin {
$data->applydescription,
$data->applydescription,
$standarduserfields,
$standarduserfields,
$extrauserfields);
$extrauserfields);
foreach ($teachers as $teach er) {
foreach ($courseuserstonotify as $us er) {
$message = new enrol_apply_notification(
$message = new enrol_apply_notification(
$teach er,
$us er,
$contact,
$contact,
'application',
'application',
get_string('mailtoteacher_suject', 'enrol_apply'),
get_string('mailtoteacher_suject', 'enrol_apply'),
@ -376,13 +371,12 @@ class enrol_apply_plugin extends enrol_plugin {
}
}
}
}
// Send notification to managers in system context?
// Send notification to users with manageapplications in system context?
if (get_config('enrol_apply', 'sendmailtomanager') == 1) {
$globaluserstonotify = $this->get_notifyglobal_users();
$context = context_system::instance();
$globaluserstonotify = array_udiff($globaluserstonotify, $courseuserstonotify, function($usera, $userb) {
$managerroles = get_archetype_roles('manager');
return $usera->id == $userb->id ? 0 : -1;
$managerrole = reset($editingteacherroles);
});
$managers = get_role_users($managerrole->id, $context);
if (!empty($globaluserstonotify)) {
$manageurl = new moodle_url('/enrol/apply/manage.php');
$manageurl = new moodle_url('/enrol/apply/manage.php');
$content = $renderer->application_notification_mail_body(
$content = $renderer->application_notification_mail_body(
$course,
$course,
@ -391,9 +385,9 @@ class enrol_apply_plugin extends enrol_plugin {
$data->applydescription,
$data->applydescription,
$standarduserfields,
$standarduserfields,
$extrauserfields);
$extrauserfields);
foreach ($managers as $manag er) {
foreach ($globaluserstonotify as $us er) {
$message = new enrol_apply_notification(
$message = new enrol_apply_notification(
$manag er,
$us er,
$contact,
$contact,
'application',
'application',
get_string('mailtoteacher_suject', 'enrol_apply'),
get_string('mailtoteacher_suject', 'enrol_apply'),
@ -404,6 +398,50 @@ class enrol_apply_plugin extends enrol_plugin {
}
}
}
}
/**
* Returns enrolled users of a course who should be notified about new course enrolment applications.
*
* Note: mostly copied from get_users_from_config() function in moodlelib.php.
* @param array $instance Enrol apply instance record.
* @return array Array of user IDs.
*/
public function get_notifycoursebased_users($instance) {
$value = $instance->customtext2;
if (empty($value) or $value === '$@NONE@$') {
return array();
}
$context = context_course::instance($instance->courseid);
// We have to make sure that users still have the necessary capability,
// it should be faster to fetch them all first and then test if they are present
// instead of validating them one-by-one.
$users = get_enrolled_users($context, 'enrol/apply:manageapplications');
if ($value === '$@ALL@$') {
return $users;
}
$result = array(); // Result in correct order.
$allowed = explode(',', $value);
foreach ($allowed as $uid) {
if (isset($users[$uid])) {
$user = $users[$uid];
$result[$user->id] = $user;
}
}
return $result;
}
/**
* Returns users who should be notified about new course enrolment applications.
* @return array Array of user IDs.
*/
public function get_notifyglobal_users() {
return get_users_from_config($this->get_config('notifyglobal'), 'enrol/apply:manageapplications');
}
private function update_mail_content($content, $course, $user) {
private function update_mail_content($content, $course, $user) {
$replace = array(
$replace = array(
'firstname' => $user->firstname,
'firstname' => $user->firstname,