Browse Source

Change notification settings and behaviour.

* Replace global 'sendmailtoteacher' setting (checkbox) by 'notifycoursebased' (checkbox):
  default for new instances
* Replace global 'sendmailtomanager' setting (checkbox) by 'notifyglobal' (multi-select):
  user based selection (users with 'manageapplications' capability in system context)
* Replace instance notification setting (checkbox) by user based selection (multi-select):
  enrolled users with 'manageapplications' capability in course context
* Upgrade process:
    * if 'sendmailtoteacher' was true, set 'notifycoursebased' true
    * if 'sendmailtomanager' was true, set 'notifyglobal' to
      "Everyone who can 'Manage apply enrolment'", "Nobody" otherwise
    * if instance based notification setting was set to true,
      set new user based setting to "Everyone who can 'Manage apply enrolment'", "Nobody" otherwise
SABERES_37_STABLE
Johannes Burk 9 years ago
parent
commit
3d278703ab
  1. 25
      db/upgrade.php
  2. 26
      edit.php
  3. 10
      edit_form.php
  4. 6
      lang/en/enrol_apply.php
  5. 78
      lib.php
  6. 22
      settings.php
  7. 2
      version.php

25
db/upgrade.php

@ -67,6 +67,31 @@ function xmldb_enrol_apply_upgrade($oldversion) {
} }
} }
if ($oldversion < 2016060803) {
// Convert old notification settings.
$enrolapply = enrol_get_plugin('apply');
$sendmailtoteacher = $enrolapply->get_config('sendmailtoteacher');
$notifycoursebased = $sendmailtoteacher;
$enrolapply->set_config('notifycoursebased', $notifycoursebased);
$enrolapply->set_config('sendmailtoteacher', null);
$sendmailtomanager = $enrolapply->get_config('sendmailtomanager');
$notifyglobal = $sendmailtomanager ? '$@ALL@$' : '';
$enrolapply->set_config('notifyglobal', $notifyglobal);
$enrolapply->set_config('sendmailtomanager', null);
$instances = $DB->get_records('enrol', array('enrol' => 'apply'));
foreach ($instances as $instance) {
$sendmailtoteacher = $instance->customint3;
$notify = $sendmailtoteacher ? '$@ALL@$' : '';
$instance->customtext2 = $notify;
$instance->customint3 = null;
$instance->customint4 = null;
$DB->update_record('enrol', $instance, true);
}
}
return true; return true;
} }

26
edit.php

@ -61,19 +61,39 @@ if ($instanceid) {
$instance->courseid = $course->id; $instance->courseid = $course->id;
} }
// Process notify setting for editing...
// Convert to array for use with multi-select element.
$notify = array('$@NONE@$');
if ($instance->customtext2 != '') {
$notify = explode(',', $instance->customtext2);
}
$instance->notify = $notify;
$mform = new enrol_apply_edit_form(null, array($instance, $plugin, $context)); $mform = new enrol_apply_edit_form(null, array($instance, $plugin, $context));
if ($mform->is_cancelled()) { if ($mform->is_cancelled()) {
redirect($return); redirect($return);
} else if ($data = $mform->get_data()) { } else if ($data = $mform->get_data()) {
// Process notify setting for storing...
// Note: Mostly copied from admin_setting_users_with_capability::write_setting().
$notify = $data->notify;
// If all is selected, remove any explicit options.
if (in_array('$@ALL@$', $notify)) {
$notify = array('$@ALL@$');
}
// None never needs to be written to the DB.
if (in_array('$@NONE@$', $notify)) {
unset($notify[array_search('$@NONE@$', $notify)]);
}
// Convert back to string for storing in enrol table.
$data->customtext2 = implode(',', $notify);
if ($instance->id) { if ($instance->id) {
$instance->status = $data->status; $instance->status = $data->status;
$instance->name = $data->name; $instance->name = $data->name;
$instance->customtext1 = $data->customtext1; $instance->customtext1 = $data->customtext1;
$instance->customtext2 = $data->customtext2;
$instance->customint1 = $data->customint1; $instance->customint1 = $data->customint1;
$instance->customint2 = $data->customint2; $instance->customint2 = $data->customint2;
$instance->customint3 = $data->customint3;
$instance->roleid = $data->roleid; $instance->roleid = $data->roleid;
$instance->timemodified = time(); $instance->timemodified = time();
$DB->update_record('enrol', $instance); $DB->update_record('enrol', $instance);
@ -85,8 +105,8 @@ if ($mform->is_cancelled()) {
'roleid' => $data->roleid, 'roleid' => $data->roleid,
'customint1' => $data->customint1, 'customint1' => $data->customint1,
'customint2' => $data->customint2, 'customint2' => $data->customint2,
'customint3' => $data->customint3, 'customtext1' => $data->customtext1,
'customtext1' => $data->customtext1); 'customtext2' => $data->customtext2);
$plugin->add_instance($course, $fields); $plugin->add_instance($course, $fields);
} }

10
edit_form.php

@ -60,7 +60,15 @@ class enrol_apply_edit_form extends moodleform {
$mform->addElement('select', 'customint2', get_string('show_extra_user_profile', 'enrol_apply'), $options); $mform->addElement('select', 'customint2', get_string('show_extra_user_profile', 'enrol_apply'), $options);
$mform->setDefault('customint2', $plugin->get_config('customint2')); $mform->setDefault('customint2', $plugin->get_config('customint2'));
$mform->addElement('advcheckbox', 'customint3', get_string('sendmailtoteacher', 'enrol_apply')); $choices = array(
'$@NONE@$' => get_string('nobody'),
'$@ALL@$' => get_string('everyonewhocan', 'admin', get_capability_string('enrol/apply:manageapplications')));
$users = get_enrolled_users($context, 'enrol/apply:manageapplications');
foreach ($users as $userid => $user) {
$choices[$userid] = fullname($user);
}
$select = $mform->addElement('select', 'notify', get_string('notify_desc', 'enrol_apply'), $choices);
$select->setMultiple(true);
$mform->addElement('hidden', 'id'); $mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT); $mform->setType('id', PARAM_INT);

6
lang/en/enrol_apply.php

@ -50,8 +50,10 @@ $string['cancelmailcontent_desc'] = 'Please use the following special marks to r
$string['notify_heading'] = 'Notification settings'; $string['notify_heading'] = 'Notification settings';
$string['notify_desc'] = 'Define who gets notified about new enrolment applications.'; $string['notify_desc'] = 'Define who gets notified about new enrolment applications.';
$string['sendmailtoteacher'] = 'Send email notification to teachers'; $string['notifycoursebased'] = "New enrolment application notification (instance based, eg. course teachers)";
$string['sendmailtomanager'] = 'Send email notification to managers'; $string['notifycoursebased_desc'] = "Default for new instances: Notify everyone who have the 'Manage apply enrolment' capability for the corresponding course (eg. teachers and managers)";
$string['notifyglobal'] = "New enrolment application notification (global, eg. global managers and admins)";
$string['notifyglobal_desc'] = "Define who gets notified about new enrolment applications for any course.";
$string['messageprovider:application'] = 'Course enrolment application notifications'; $string['messageprovider:application'] = 'Course enrolment application notifications';
$string['messageprovider:confirmation'] = 'Course enrolment application confirmation notifications'; $string['messageprovider:confirmation'] = 'Course enrolment application confirmation notifications';

78
lib.php

@ -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 $teacher) { foreach ($courseuserstonotify as $user) {
$message = new enrol_apply_notification( $message = new enrol_apply_notification(
$teacher, $user,
$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 $manager) { foreach ($globaluserstonotify as $user) {
$message = new enrol_apply_notification( $message = new enrol_apply_notification(
$manager, $user,
$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,

22
settings.php

@ -90,16 +90,12 @@ if ($ADMIN->fulltree) {
'enrol_apply_notify', 'enrol_apply_notify',
get_string('notify_heading', 'enrol_apply'), get_string('notify_heading', 'enrol_apply'),
get_string('notify_desc', 'enrol_apply'))); get_string('notify_desc', 'enrol_apply')));
$settings->add(new admin_setting_configcheckbox( $settings->add(new admin_setting_users_with_capability(
'enrol_apply/sendmailtoteacher', 'enrol_apply/notifyglobal',
get_string('sendmailtoteacher', 'enrol_apply'), get_string('notifyglobal', 'enrol_apply'),
'', get_string('notifyglobal_desc', 'enrol_apply'),
0)); array(),
$settings->add(new admin_setting_configcheckbox( 'enrol/apply:manageapplications'));
'enrol_apply/sendmailtomanager',
get_string('sendmailtomanager', 'enrol_apply'),
'',
0));
// Enrol instance defaults... // Enrol instance defaults...
$settings->add(new admin_setting_heading('enrol_manual_defaults', $settings->add(new admin_setting_heading('enrol_manual_defaults',
@ -130,6 +126,12 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configselect('enrol_apply/roleid', $settings->add(new admin_setting_configselect('enrol_apply/roleid',
get_string('defaultrole', 'role'), '', $student->id, $options)); get_string('defaultrole', 'role'), '', $student->id, $options));
} }
$settings->add(new admin_setting_configcheckbox(
'enrol_apply/notifycoursebased',
get_string('notifycoursebased', 'enrol_apply'),
get_string('notifycoursebased_desc', 'enrol_apply'),
0));
} }
if ($hassiteconfig) { // Needs this condition or there is error on login page. if ($hassiteconfig) { // Needs this condition or there is error on login page.

2
version.php

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016060801; $plugin->version = 2016060803;
$plugin->requires = 2011080100; $plugin->requires = 2011080100;
$plugin->maturity = MATURITY_STABLE; $plugin->maturity = MATURITY_STABLE;
$plugin->release = 'Enrolment upon approval plugin Version 3.1-a'; $plugin->release = 'Enrolment upon approval plugin Version 3.1-a';

Loading…
Cancel
Save