diff --git a/classes/observer.php b/classes/observer.php index 86c2f64..2c73f27 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -15,175 +15,149 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola */ defined('MOODLE_INTERNAL') || die(); -require_once($CFG->dirroot . '/enrol/notificationeabc/lib.php'); +require_once($CFG->dirroot . '/enrol/notification/lib.php'); /** * Observer definition * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola */ -class enrol_notificationeabc_observer +class enrol_notification_observer { /** - * hook enrol event - * @param \core\event\user_enrolment_deleted $event + * hook enrolment event + * @param \core\event\user_enrolment_created $event */ - public static function user_unenroled(\core\event\user_enrolment_deleted $event) { + public static function user_enrolled(\core\event\user_enrolment_created $event) { global $DB; - // Validate status plugin. + // Validate plugin status in system context. $enableplugins = get_config(null, 'enrol_plugins_enabled'); $enableplugins = explode(',', $enableplugins); - $enabled = false; - foreach ($enableplugins as $enableplugin) { - if ($enableplugin === 'notificationeabc') { - $enabled = true; - } - } + $enabled = in_array('notification', $enableplugins); + if ($enabled) { + $notification = new enrol_notification_plugin(); + $user = $DB->get_record('user', array('id' => $event->relateduserid)); $course = $DB->get_record('course', array('id' => $event->courseid)); - $notificationeabc = new enrol_notificationeabc_plugin(); + $globalenrolalert = $notification->get_config('globalenrolalert'); - $activeglobal = $notificationeabc->get_config('activarglobalunenrolalert'); - $activeunenrolalert = $notificationeabc->get_config('activeunenrolalert'); + $enrol = $DB->get_record('enrol', array('enrol' => 'notification', 'courseid' => $event->courseid)); - $enrol = $DB->get_record('enrol', array('enrol' => 'notificationeabc', 'courseid' => $event->courseid)); - - /* - * check the instance status - * status = 0 enabled and status = 1 disabled - */ - $instanceenabled = false; - if (!empty($enrol)) { - if (!$enrol->status) { - $instanceenabled = true; + if (empty($enrol)) { + // This course does not use this enrolment plugin. + if (!empty($globalenrolalert)) { + // But global use of enrolalert was requested at site level for each course. + $notification->send_email($user, $course, 1); + } + } else { + // This course uses this enrolment plugin. + // Check the instance status. Take care: status = 0 enabled; status = 1 disabled. + if (empty($enrol->status)) { + $localenrolalert = $enrol->customint1; + if (!empty($localenrolalert)) { + $notification->send_email($user, $course, 1); + } } - } - if (!empty($enrol) && $instanceenabled) { - $activeunenrolalert = $enrol->customint4; - } - - if ($activeglobal == 1 && $activeunenrolalert == 1) { - $notificationeabc->enviarmail($user, $course, 2); - } else if (!empty($enrol) && !empty($activeunenrolalert) && $instanceenabled) { - $notificationeabc->enviarmail($user, $course, 2); } } } /** - * hook user update event - * @param \core\event\user_enrolment_updated $event + * hook unenrolment event + * @param \core\event\user_enrolment_deleted $event */ - public static function user_updated(\core\event\user_enrolment_updated $event) { + public static function user_unenrolled(\core\event\user_enrolment_deleted $event) { global $DB; - // Validate plugin status in system context. + // Validate status plugin. $enableplugins = get_config(null, 'enrol_plugins_enabled'); $enableplugins = explode(',', $enableplugins); - $enabled = false; - foreach ($enableplugins as $enableplugin) { - if ($enableplugin === 'notificationeabc') { - $enabled = true; - } - } + $enabled = in_array('notification', $enableplugins); + if ($enabled) { + $notification = new enrol_notification_plugin(); + $user = $DB->get_record('user', array('id' => $event->relateduserid)); $course = $DB->get_record('course', array('id' => $event->courseid)); - $notificationeabc = new enrol_notificationeabc_plugin(); - - $activeglobal = $notificationeabc->get_config('activarglobalenrolupdated'); - $activeenrolupdatedalert = $notificationeabc->get_config('activeenrolupdatedalert'); + $globalunenrolalert = $notification->get_config('globalunenrolalert'); - // Plugin instance in course. - $enrol = $DB->get_record('enrol', array('enrol' => 'notificationeabc', 'courseid' => $event->courseid)); + $enrol = $DB->get_record('enrol', array('enrol' => 'notification', 'courseid' => $event->courseid)); - /* - * check the instance status - * status = 0 enabled and status = 1 disabled - */ - $instanceenabled = false; - if (!empty($enrol)) { - if (!$enrol->status) { - $instanceenabled = true; + if (empty($enrol)) { + // This course does not use this enrolment plugin. + if (!empty($globalunenrolalert)) { + // But global use of unenrolalert was requested at site level for each course. + $notification->send_email($user, $course, 2); + } + } else { + // This course uses this enrolment plugin. + // Check the instance status. Take care: status = 0 enabled; status = 1 disabled. + if (empty($enrol->status)) { + $localunenrolalert = $enrol->customint2; + if (!empty($localunenrolalert)) { + $notification->send_email($user, $course, 2); + } } - } - if (!empty($enrol) && $instanceenabled) { - $activeenrolupdatedalert = $enrol->customint5; - } - - if ($activeglobal == 1 && $activeenrolupdatedalert == 1) { - $notificationeabc->enviarmail($user, $course, 3); - } else if (!empty($enrol) && !empty($activeenrolupdatedalert) && $instanceenabled) { - $notificationeabc->enviarmail($user, $course, 3); } } } /** - * hook enrolment event - * @param \core\event\user_enrolment_created $event + * hook user enrolment update event + * @param \core\event\user_enrolment_updated $event */ - public static function user_enroled(\core\event\user_enrolment_created $event) { + public static function user_updated(\core\event\user_enrolment_updated $event) { global $DB; // Validate plugin status in system context. $enableplugins = get_config(null, 'enrol_plugins_enabled'); $enableplugins = explode(',', $enableplugins); - $enabled = false; - foreach ($enableplugins as $enableplugin) { - if ($enableplugin === 'notificationeabc') { - $enabled = true; - } - } + $enabled = in_array('notification', $enableplugins); + if ($enabled) { + $notification = new enrol_notification_plugin(); + $user = $DB->get_record('user', array('id' => $event->relateduserid)); $course = $DB->get_record('course', array('id' => $event->courseid)); - $notificationeabc = new enrol_notificationeabc_plugin(); - - $activeglobal = $notificationeabc->get_config('activarglobal'); - $activeenrolalert = $notificationeabc->get_config('activeenrolalert'); + $globalenrolmentupdatealert = $notification->get_config('globalenrolmentupdatealert'); - $enrol = $DB->get_record('enrol', array('enrol' => 'notificationeabc', 'courseid' => $event->courseid)); + $enrol = $DB->get_record('enrol', array('enrol' => 'notification', 'courseid' => $event->courseid)); - /* - * check the instance status - * status = 0 enabled and status = 1 disabled - */ - $instanceenabled = false; - if (!empty($enrol)) { - if (!$enrol->status) { - $instanceenabled = true; + if (empty($enrol)) { + // This course does not use this enrolment plugin. + if (!empty($globalenrolmentupdatealert)) { + // But global use of enrolmentupdatealert was requested at site level for each course. + $notification->send_email($user, $course, 3); + } + } else { + // This course uses this enrolment plugin. + // Check the instance status. Take care: status = 0 enabled; status = 1 disabled. + if (empty($enrol->status)) { + $localenrolmentupdatealert = $enrol->customint3; + if (!empty($localenrolmentupdatealert)) { + $notification->send_email($user, $course, 3); + } } - } - - if (!empty($enrol) && $instanceenabled) { - $activeenrolalert = $enrol->customint3; - } - - if ($activeglobal == 1 && $activeenrolalert == 1) { - $notificationeabc->enviarmail($user, $course, 1); - } else if (!empty($enrol) && !empty($activeenrolalert) && $instanceenabled) { - $notificationeabc->enviarmail($user, $course, 1); } } } diff --git a/db/access.php b/db/access.php index f0fd586..cd4f77b 100644 --- a/db/access.php +++ b/db/access.php @@ -15,11 +15,11 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola @@ -28,8 +28,8 @@ defined('MOODLE_INTERNAL') || die(); $capabilities = array( - /* Manage user notificationeabc-enrolments. */ - 'enrol/notificationeabc:manage' => array( + /* Manage user notification-enrolments. */ + 'enrol/notification:manage' => array( 'captype' => 'write', 'contextlevel' => CONTEXT_COURSE, @@ -38,4 +38,13 @@ 'manager' => CAP_ALLOW, ) ), + 'enrol/notification:config' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'manager' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + ) + ), ); diff --git a/db/events.php b/db/events.php index 36d006e..fc132d3 100644 --- a/db/events.php +++ b/db/events.php @@ -15,11 +15,11 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola @@ -31,17 +31,17 @@ array( 'eventname' => '\core\event\user_enrolment_deleted', - 'callback' => 'enrol_notificationeabc_observer::user_unenroled', + 'callback' => 'enrol_notification_observer::user_unenrolled', ), array( 'eventname' => '\core\event\user_enrolment_created', - 'callback' => 'enrol_notificationeabc_observer::user_enroled', + 'callback' => 'enrol_notification_observer::user_enrolled', ), array( 'eventname' => '\core\event\user_enrolment_updated', - 'callback' => 'enrol_notificationeabc_observer::user_updated', + 'callback' => 'enrol_notification_observer::user_updated', ) ); \ No newline at end of file diff --git a/db/messages.php b/db/messages.php index 3c869e1..bfeae6d 100644 --- a/db/messages.php +++ b/db/messages.php @@ -15,11 +15,11 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola @@ -29,7 +29,7 @@ $messageproviders = array ( - 'notificationeabc_enrolment' => array(), + 'notification_enrolment' => array(), ); diff --git a/db/upgrade.php b/db/upgrade.php index 52d8f51..e0b7df7 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -15,11 +15,11 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola @@ -28,12 +28,12 @@ defined('MOODLE_INTERNAL') || die(); /** - * Upgrade script + * Upgrade script * @param int $oldversion * @return bool */ -function xmldb_enrol_notificationeabc_upgrade($oldversion) { - global $CFG, $DB; - $dbman = $DB->get_manager(); +function xmldb_enrol_notification_upgrade($oldversion) { + global $DB; + // $dbman = $DB->get_manager(); return true; } diff --git a/edit.php b/edit.php deleted file mode 100644 index aab3b4c..0000000 --- a/edit.php +++ /dev/null @@ -1,146 +0,0 @@ -. - -/** - * Notificationeabc enrolment plugin. - * - * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) - * - * @package enrol_notificationeabc - * @copyright 2017 e-ABC Learning - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @author Osvaldo Arriola - */ - - -require('../../config.php'); -require_once('edit_form.php'); - -$courseid = required_param('courseid', PARAM_INT); -$instanceid = optional_param('id', 0, PARAM_INT); - -$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); -$context = context_course::instance($course->id, MUST_EXIST); - -require_login($course); -require_capability('enrol/notificationeabc:manage', $context); - -$PAGE->set_url('/enrol/notificationeabc/edit.php', array('courseid' => $course->id, 'id' => $instanceid)); -$PAGE->set_pagelayout('admin'); - -$return = new moodle_url('/enrol/instances.php', array('id' => $course->id)); -if (!enrol_is_enabled('notificationeabc')) { - redirect($return); -} - -/** @var enrol_notificationeabc_plugin $plugin */ -$plugin = enrol_get_plugin('notificationeabc'); - -if ($instanceid) { - $instance = $DB->get_record('enrol', - array( - 'courseid' => $course->id, - 'enrol' => 'notificationeabc', - 'id' => $instanceid - ), '*', MUST_EXIST); - -} else { - require_capability('moodle/course:enrolconfig', $context); - // No instance yet, we have to add new instance. - navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id' => $course->id))); - - $instance = (object)$plugin->get_instance_defaults(); - $instance->id = null; - $instance->courseid = $course->id; - $instance->status = ENROL_INSTANCE_ENABLED; // Do not use default for automatically created instances here. -} - - -$mform = new enrol_notificationeabc_edit_form(null, array($instance, $plugin, $context)); - -if ($mform->is_cancelled()) { - redirect($return); - -} else if ($data = $mform->get_data()) { - if ($instance->id) { - $reset = ($instance->status != $data->status); - - $instance->status = $data->status; - $instance->name = $data->name; - $instance->customint1 = $data->customint1; - if (isset($data->customint2)) { - $instance->customint2 = $data->customint2; - } else { - $instance->customint2 = 0; - } - - if (isset($data->customint3)) { - $instance->customint3 = $data->customint3; - } else { - $instance->customint3 = 0; - } - - if (isset($data->customint4)) { - $instance->customint4 = $data->customint4; - } else { - $instance->customint4 = 0; - } - - if (isset($data->customint5)) { - $instance->customint5 = $data->customint5; - } else { - $instance->customint5 = 0; - } - - $instance->customtext1 = $data->customtext1['text']; - $instance->customtext2 = $data->customtext2['text']; - $instance->customtext3 = $data->customtext3['text']; - $instance->customchar1 = $data->customchar1; - $instance->customchar2 = $data->customchar2; - $instance->timemodified = time(); - $DB->update_record('enrol', $instance); - - if ($reset) { - $context->mark_dirty(); - } - - } else { - $fields = array( - 'status' => $data->status, - 'name' => $data->name, - 'customint1' => $data->customint1, - 'customint2' => $data->customint2, - 'customint3' => $data->customint3, - 'customint4' => $data->customint4, - 'customint5' => $data->customint5, - 'customtext1' => $data->customtext1['text'], - 'customtext2' => $data->customtext2['text'], - 'customtext3' => $data->customtext3['text'], - 'customchar1' => $data->customchar1, - 'customchar2' => $data->customchar2); - $plugin->add_instance($course, $fields); - } - - redirect($return); -} - -$PAGE->set_heading($course->fullname); -$PAGE->set_title(get_string('pluginname', 'enrol_notificationeabc')); - -echo $OUTPUT->header(); -echo $OUTPUT->heading(get_string('pluginname', 'enrol_notificationeabc')); -$mform->display(); -echo $OUTPUT->footer(); \ No newline at end of file diff --git a/edit_form.php b/edit_form.php deleted file mode 100644 index 3f75793..0000000 --- a/edit_form.php +++ /dev/null @@ -1,120 +0,0 @@ -. - -/** - * Notificationeabc enrolment plugin. - * - * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) - * - * @package enrol_notificationeabc - * @copyright 2017 e-ABC Learning - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @author Osvaldo Arriola - */ - -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->libdir . '/formslib.php'); - -/** - * Edit form class - * - * @package enrol_notificationeabc - * @copyright 2016 e-ABC Learning - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class enrol_notificationeabc_edit_form extends moodleform -{ - /** - * Main definition - */ - public function definition() { - - $mform = $this->_form; - - list($instance, $plugin, $context) = $this->_customdata; - - $mform->addElement('header', 'header', get_string('pluginname', 'enrol_notificationeabc')); - $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); - - $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), - ENROL_INSTANCE_DISABLED => get_string('no')); - $mform->addElement('select', 'status', get_string('status', 'enrol_notificationeabc'), $options); - - // Enrol notifications. - $mform->addElement('advcheckbox', 'customint3', get_string('activeenrolalert', 'enrol_notificationeabc')); - $mform->addHelpButton('customint3', 'activeenrolalert', 'enrol_notificationeabc'); - - $mform->addElement('editor', 'customtext1', get_string('location', 'enrol_notificationeabc'), null); - $mform->setType('customtext1', PARAM_RAW); - $mform->addHelpButton('customtext1', 'location', 'enrol_notificationeabc'); - - // Unenrol notifications. - $mform->addElement('advcheckbox', 'customint4', get_string('activeunenrolalert', 'enrol_notificationeabc')); - $mform->addHelpButton('customint4', 'activeunenrolalert', 'enrol_notificationeabc'); - - $mform->addElement('editor', 'customtext2', get_string('unenrolmessage', 'enrol_notificationeabc'), null); - $mform->setType('customtext2', PARAM_RAW); - $mform->addHelpButton('customtext2', 'unenrolmessage', 'enrol_notificationeabc'); - - // Update enrolment notifications. - $mform->addElement('advcheckbox', 'customint5', get_string('activeenrolupdatedalert', 'enrol_notificationeabc')); - $mform->addHelpButton('customint5', 'activeenrolupdatedalert', 'enrol_notificationeabc'); - - $mform->addElement('editor', 'customtext3', get_string('updatedenrolmessage', 'enrol_notificationeabc'), null); - $mform->setType('customtext3', PARAM_RAW); - $mform->addHelpButton('customtext3', 'updatedenrolmessage', 'enrol_notificationeabc'); - - // Email y nombre del remitente. - $mform->addElement('text', 'customchar1', get_string('emailsender', 'enrol_notificationeabc')); - $mform->addHelpButton('customchar1', 'emailsender', 'enrol_notificationeabc'); - - $mform->addElement('text', 'customchar2', get_string('namesender', 'enrol_notificationeabc')); - $mform->addHelpButton('customchar2', 'namesender', 'enrol_notificationeabc'); - - $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol'))); - - if (!empty($instance->courseid)) { - $mform->addElement('hidden', 'courseid', $instance->courseid); - $mform->setType('courseid', PARAM_INT); - } - - if (!empty($instance->id)) { - $mform->addElement('hidden', 'id', $instance->id); - $mform->setType('id', PARAM_INT); - } - - if (!empty($instance)) { - $mform->setDefault('customtext1', array('text' => $instance->customtext1)); - $mform->setDefault('customtext2', array('text' => $instance->customtext2)); - $mform->setDefault('customtext3', array('text' => $instance->customtext3)); - $mform->setDefault('customchar1', $instance->customchar1); - $mform->setDefault('customchar2', $instance->customchar2); - $mform->setDefault('customint3', $instance->customint3); - $mform->setDefault('customint4', $instance->customint4); - $mform->setDefault('customint5', $instance->customint5); - if (!empty($instance->name)) { - $mform->setDefault('name', $instance->name); - } else { - $mform->setDefault('name', 'notificationeabc'); - } - } - $this->set_data($instance); - - } - - -} diff --git a/lang/en/enrol_notification.php b/lang/en/enrol_notification.php new file mode 100644 index 0000000..15c47d5 --- /dev/null +++ b/lang/en/enrol_notification.php @@ -0,0 +1,86 @@ +. + +/** + * Notification enrolment plugin. + * + * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) + * + * @package enrol_notification + * @copyright 2017 e-ABC Learning + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Osvaldo Arriola + */ + +$string['failsend'] = 'WARNING: it has no been able to notify the {$a->username} user about his enrollment in the {$a->coursename} course'."\n"; +$string['messageprovider:notification_enrolment'] = 'Enrol email notification messages'; +$string['notification:config'] = 'Configure email notification instances'; +$string['notification:manage'] = 'Manage email notification'; +$string['pluginname'] = 'Enrol notification'; +$string['status'] = 'Active email notification'; +$string['subject'] = 'Enrolment status notification'; +$string['succefullsend'] = 'The user {$a->username} has been notified about his enrollment in the {$a->coursename} course'."\n"; + +// Enrol notifications. +$string['globalenrolalert'] = 'Enable global enrol message'; +$string['globalenrolalert_help'] = 'Enable enrol message for each course in this site'; +$string['enrolalert'] = 'Enable enrol message'; +$string['enrolalert_help'] = 'Enable enrol message'; +$string['enrolmessage'] = 'Custom enrol message'; +$string['enrolmessage_help'] = 'Personalize the message that users will come to be enrolled. This field accepts the following markers which then will be replaced by the corresponding values ​​dynamically +
+{COURSENAME} = course fullname
+{USERNAME} = username
+{FIRSTNAME} = firstname
+{LASTNAME} = lastname
+{URL} = course url
+
+Leave this field empty (or untouched to its default) to use the site level default settings for this plugin.'; +$string['enrolmessagedefault'] = 'You have been enrolled to course "{COURSENAME}" at {URL}'; + +// Unenrol notifications. +$string['globalunenrolalert'] = 'Enable global unenrol message'; +$string['globalunenrolalert_help'] = 'Enable unenrol message for each course in this site'; +$string['unenrolalert'] = 'Enable unenrol message'; +$string['unenrolalert_help'] = 'Enable unenrol message'; +$string['unenrolmessage'] = 'Custom unenrol message'; +$string['unenrolmessage_help'] = 'Personalize the message that users will come to be unenrolled. This field accepts the following markers which then will be replaced by the corresponding values ​​dynamically +
+{COURSENAME} = course fullname
+{USERNAME} = username
+{FIRSTNAME} = firstname
+{LASTNAME} = lastname
+{URL} = course url
+
+Leave this field empty (or untouched to its default) to use the site level default settings for this plugin.'; +$string['unenrolmessagedefault'] = 'You have been unenrolled from course "{COURSENAME}" at {URL}'; + +// Update enrol notifications. +$string['globalenrolmentupdatealert'] = 'Enable global enrol update message'; +$string['globalenrolmentupdatealert_help'] = 'Enable enrol update message for each course in this site'; +$string['enrolmentupdatealert'] = 'Enable enrol update message'; +$string['enrolmentupdatealert_help'] = 'Enable enrol update message'; +$string['enrolmentupdatemessage'] = 'Custom enrol update message'; +$string['enrolmentupdatemessage_help'] = 'Personalize the message that users will come to be updated. This field accepts the following markers which then will be replaced by the corresponding values ​​dynamically +
+{COURSENAME} = course fullname
+{USERNAME} = username
+{FIRSTNAME} = firstname
+{LASTNAME} = lastname
+{URL} = course url
+
+Leave this field empty (or untouched to its default) to use the site level default settings for this plugin.'; +$string['enrolmentupdatemessagedefault'] = 'Your enrolment to course "{COURSENAME}" at {URL} has been updated'; diff --git a/lang/en/enrol_notificationeabc.php b/lang/en/enrol_notificationeabc.php deleted file mode 100644 index e8e0d2c..0000000 --- a/lang/en/enrol_notificationeabc.php +++ /dev/null @@ -1,87 +0,0 @@ -. - -/** - * Notificationeabc enrolment plugin. - * - * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) - * - * @package enrol_notificationeabc - * @copyright 2017 e-ABC Learning - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @author Osvaldo Arriola - */ - -$string['filelockedmail'] = 'You has been enroled in {$a->fullname} ({$a->url})'; -$string['location'] = 'Message'; -$string['messageprovider:notificationeabc_enrolment'] = 'Enrol notification messages'; -$string['notificationeabc:manage'] = 'Manage notificationeabc'; -$string['pluginname'] = 'Enrol Notification'; -$string['pluginname_desc'] = 'Enrol notifications via mail'; -$string['location_help'] = 'Personalize the message that users will come to be enrolled. This field accepts the following markers which then will be replaced by the corresponding values ​​dynamically -
-{COURSENAME} = course fullname
-{USERNAME} = username
-{NOMBRE} = firstname
-{APELLIDO} = lastname
-{URL} = course url
-
'; -$string['fecha_help'] = 'Place the period for which you want to perform the first virificación'; -$string['fecha'] = 'Period for verification of users enrolled courses'; -$string['activar'] = 'Enable initial verification'; -$string['activar_help'] = 'When activated will be verified by the immediate execution of cron later, users who were enrolled for the period specified above'; -$string['activarglobal'] = 'Active global'; -$string['activarglobal_help'] = 'Active enrol notification for all site'; -$string['emailsender'] = 'Email sender '; -$string['emailsender_help'] = 'By default set to take the email user support '; -$string['namesender'] = 'Name sender '; -$string['namesender_help'] = 'By default it takes the name set to the user support'; -$string['status'] = 'Active enrol notification'; -$string['subject'] = 'Enrolment notification'; -$string['activeenrolalert'] = 'Active enrol alert'; -$string['activeenrolalert_help'] = 'Active enrol alert'; -// Unenrol notifications. -$string['activeunenrolalert'] = 'Active unenrol notifications'; -$string['activeunenrolalert_help'] = 'Active unenrol alert'; -$string['activarglobalunenrolalert'] = 'Active global'; -$string['activarglobalunenrolalert_help'] = 'Active enrol notifications for all site'; -$string['unenrolmessage'] = 'Custom Message'; -$string['unenrolmessage_help'] = 'Personalize the message that users will come to be unenrolled. This field accepts the following markers which then will be replaced by the corresponding values ​​dynamically -
-{COURSENAME} = course fullname
-{USERNAME} = username
-{NOMBRE} = firstname
-{APELLIDO} = lastname
-{URL} = course url
-
'; -$string['unenrolmessagedefault'] = 'You has been unenrolled from {$a->fullname} ({$a->url})'; -// Update enrol notifications. -$string['activeenrolupdatedalert'] = 'Active update enrol notifications'; -$string['activeenrolupdatedalert_help'] = 'Active update enrol notifications'; -$string['activarglobalenrolupdated'] = 'Active global'; -$string['activarglobalenrolupdated_help'] = 'Active enrol updated notifications for all site'; -$string['updatedenrolmessage'] = 'Custom message'; -$string['updatedenrolmessage_help'] = 'Personalize the message that users will come to be updated. This field accepts the following markers which then will be replaced by the corresponding values ​​dynamically -
-{COURSENAME} = course fullname
-{USERNAME} = username
-{NOMBRE} = firstname
-{APELLIDO} = lastname
-{URL} = course url
-
'; -$string['updatedenrolmessagedefault'] = 'Your enrolment from {$a->fullname} has been updated ({$a->url})'; -$string['succefullsend'] = 'The user {$a->username} has been notified about his enrollment in the {$a->coursename} course'."\n"; -$string['failsend'] = 'WARNING: it has no been able to notify the {$a->username} user about his enrollment in the {$a->coursename} course'."\n"; \ No newline at end of file diff --git a/lang/es/enrol_notification.php b/lang/es/enrol_notification.php new file mode 100644 index 0000000..dfe7c92 --- /dev/null +++ b/lang/es/enrol_notification.php @@ -0,0 +1,83 @@ +. + +/** + * Notification enrolment plugin. + * + * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) + * + * @package enrol_notification + * @copyright 2017 e-ABC Learning + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Osvaldo Arriola + */ + +$string['failsend'] = 'ATENCION: No se pudo notificar al usuario {$a->username} sobre su matriculación en el curso {$a->coursename}'."\n"; +$string['messageprovider:notification_enrolment'] = 'Enrol notification messages'; +$string['notification:manage'] = 'Gestionar notificaciones de matriculación'; + +$string['pluginname'] = 'Notificación de Matriculación'; +$string['status'] = 'Activar notification de matriculación'; +$string['subject'] = 'Notificación de Matriculación'; +$string['succefullsend'] = 'Se notifico al usuario {$a->username} sobre su matriculación en el curso {$a->coursename}'. "\n"; + +// Aviso de matriculacion. +$string['globalenrolalert'] = 'Activar para todo el sitio'; +$string['globalenrolalert_help'] = 'Activa la notificacion de matriculacion para todo los cursos'; +$string['enrolalert'] = 'Activar aviso de matriculación'; +$string['enrolalert_help'] = 'Activar aviso de matriculación'; +$string['enrolmessage'] = 'Mensaje personalizado'; +$string['enrolmessage_help'] = 'Personalice el mensaje que le llegará a los usuarios al ser matriculados. Este campo acepta los siguientes marcadores que luego seran reemplazados dinámicamente por los valores correspondientes +
+{COURSENAME} = Nombre completo del curso
+{USERNAME} = Nombre de usuario
+{FIRSTNAME} = Nombre
+{LASTNAME} = Apellido
+{URL} = Url del curso
+
'; +$string['enrolmessagedefault'] = 'Ud ha sido matriculado en el curso {$a->fullname} ({$a->url})'; + +// Aviso de desmatriculacion. +$string['globalunenrolalert'] = 'Activar para todo el sitio'; +$string['globalunenrolalert_help'] = 'Activar la notificacion de desmatriculacion para todo el sitio'; +$string['unenrolalert'] = 'Activar aviso de desmatriculacion'; +$string['unenrolalert_help'] = 'Activar aviso de desmatriculacion'; +$string['unenrolmessage'] = 'Mensaje personalizado'; +$string['unenrolmessage_help'] = 'Personalice el mensaje que le llegará a los usuarios al ser desmatriculados. Este campo acepta los siguientes marcadores que luego seran reemplazados dinámicamente por los valores correspondientes +
+{COURSENAME} = Nombre completo del curso
+{USERNAME} = Nombre de usuario
+{FIRSTNAME} = Nombre
+{LASTNAME} = Apellido
+{URL} = Url del curso
+
'; +$string['unenrolmessagedefault'] = 'Ud ha sido desmatriculado del curso {$a->fullname} ({$a->url})'; + +// Aviso de actualizacion de matriculacion. +$string['globalenrolmentupdatealert'] = 'Activar para todo el sitio'; +$string['globalenrolmentupdatealert_help'] = 'Activar la notificacion de actualizacion de matriculacion para todo el sitio'; +$string['enrolmentupdatealert'] = 'Activar aviso de actualizacion de matriculacion'; +$string['enrolmentupdatealert_help'] = 'Activar aviso de actualizacion de matriculacion'; +$string['enrolmentupdatemessage'] = 'Mensaje personalizado'; +$string['enrolmentupdatemessage_help'] = 'Personalice el mensaje que le llegará a los usuarios al realizar alguna actualizacion en su matriculacion. Este campo acepta los siguientes marcadores que luego seran reemplazados dinámicamente por los valores correspondientes +
+{COURSENAME} = Nombre completo del curso
+{USERNAME} = Nombre de usuario
+{FIRSTNAME} = Nombre
+{LASTNAME} = Apellido
+{URL} = Url del curso
+
'; +$string['enrolmentupdatemessagedefault'] = 'Su matriculacion en el curso {$a->fullname} ha sido actualizada ({$a->url})'; diff --git a/lang/es/enrol_notificationeabc.php b/lang/es/enrol_notificationeabc.php deleted file mode 100644 index a185d74..0000000 --- a/lang/es/enrol_notificationeabc.php +++ /dev/null @@ -1,87 +0,0 @@ -. - -/** - * Notificationeabc enrolment plugin. - * - * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) - * - * @package enrol_notificationeabc - * @copyright 2017 e-ABC Learning - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @author Osvaldo Arriola - */ - -$string['filelockedmail'] = 'Ud ha sido matriculado en el curso {$a->fullname} ({$a->url})'; -$string['location'] = 'Mensaje personalizado'; -$string['messageprovider:notificationeabc_enrolment'] = 'Enrol notification messages'; -$string['notificationeabc:manage'] = 'Gestionar notificaciones de matriculación'; -$string['pluginname'] = 'Notificación de Matriculación'; -$string['pluginname_desc'] = 'Notificación de matriculaciones a cursos via mail'; -$string['location_help'] = 'Personalice el mensaje que le llegará a los usuarios al ser matriculados. Este campo acepta los siguientes marcadores que luego seran reemplazados dinámicamente por los valores correspondientes -
-{COURSENAME} = Nombre completo del curso
-{USERNAME} = Nombre de usuario
-{NOMBRE} = Nombre
-{APELLIDO} = Apellido
-{URL} = Url del curso
-
'; -$string['fecha_help'] = 'Coloque el periodo por el cual desea que se realice la virificación inicial de usuarios matriculados'; -$string['fecha'] = 'Período para realizar la verificación de usuarios que se matricularon a cursos'; -$string['activar'] = 'Activar verificación inicial'; -$string['activar_help'] = 'Al activarse se verificará, mediante la ejecucion del cron inmediata posterior, los usuarios que fueron matriculados en el periodo establecido arriba'; -$string['activarglobal'] = 'Activar para todo el sitio'; -$string['activarglobal_help'] = 'Activa la notificacion de matriculacion para todo los cursos'; -$string['emailsender'] = 'Email del remitente '; -$string['emailsender_help'] = 'Por defecto toma el email configurado como el usuario de soporte '; -$string['namesender'] = 'Nombre del remitente '; -$string['namesender_help'] = 'Por defecto toma el nombre configurado como el usuario de soporte '; -$string['status'] = 'Activar notification de matriculación'; -$string['subject'] = 'Notificación de Matriculación'; -$string['activeenrolalert'] = 'Activar aviso de matriculación'; -$string['activeenrolalert_help'] = 'Activar aviso de matriculación'; -// Aviso de desmatriculacion. -$string['activeunenrolalert'] = 'Activar aviso de desmatriculacion'; -$string['activeunenrolalert_help'] = 'Activar aviso de desmatriculacion'; -$string['activarglobalunenrolalert'] = 'Activar para todo el sitio'; -$string['activarglobalunenrolalert_help'] = 'Activar la notificacion de desmatriculacion para todo el sitio'; -$string['unenrolmessage'] = 'Mensaje personalizado'; -$string['unenrolmessage_help'] = 'Personalice el mensaje que le llegará a los usuarios al ser desmatriculados. Este campo acepta los siguientes marcadores que luego seran reemplazados dinámicamente por los valores correspondientes -
-{COURSENAME} = Nombre completo del curso
-{USERNAME} = Nombre de usuario
-{NOMBRE} = Nombre
-{APELLIDO} = Apellido
-{URL} = Url del curso
-
'; -$string['unenrolmessagedefault'] = 'Ud ha sido desmatriculado del curso {$a->fullname} ({$a->url})'; -// Aviso de actualizacion de matriculacion. -$string['activeenrolupdatedalert'] = 'Activar aviso de actualizacion de matriculacion'; -$string['activeenrolupdatedalert_help'] = 'Activar aviso de actualizacion de matriculacion'; -$string['activarglobalenrolupdated'] = 'Activar para todo el sitio'; -$string['activarglobalenrolupdated_help'] = 'Activar la notificacion de actualizacion de matriculacion para todo el sitio'; -$string['updatedenrolmessage'] = 'Mensaje personalizado'; -$string['updatedenrolmessage_help'] = 'Personalice el mensaje que le llegará a los usuarios al realizar alguna actualizacion en su matriculacion. Este campo acepta los siguientes marcadores que luego seran reemplazados dinámicamente por los valores correspondientes -
-{COURSENAME} = Nombre completo del curso
-{USERNAME} = Nombre de usuario
-{NOMBRE} = Nombre
-{APELLIDO} = Apellido
-{URL} = Url del curso
-
'; -$string['updatedenrolmessagedefault'] = 'Su matriculacion en el curso {$a->fullname} ha sido actualizada ({$a->url})'; -$string['succefullsend'] = 'Se notifico al usuario {$a->username} sobre su matriculación en el curso {$a->coursename}'. "\n"; -$string['failsend'] = 'ATENCION: No se pudo notificar al usuario {$a->username} sobre su matriculación en el curso {$a->coursename}'."\n"; \ No newline at end of file diff --git a/lang/it/enrol_notification.php b/lang/it/enrol_notification.php new file mode 100644 index 0000000..5e92a5f --- /dev/null +++ b/lang/it/enrol_notification.php @@ -0,0 +1,31 @@ +. + +/** + * Notification enrolment plugin. + * + * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) + * + * @package enrol_notification + * @copyright 2017 e-ABC Learning + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Osvaldo Arriola + */ + +$string['subject'] = 'Notifica sullo stato di iscrizione'; +$string['enrolmessagedefault'] = 'Sei stato iscritto al corso "{COURSENAME}" all\'indirizzo: {URL}'; +$string['unenrolmessagedefault'] = 'Sei stato disiscritto dal corso "{COURSENAME}" all\'indirizzo: {URL}'; +$string['enrolmentupdatemessagedefault'] = 'La tua iscrizione al corso {COURSENAME} all\'indirizzo: {URL} è stata modificata'; diff --git a/lib.php b/lib.php index 0e68872..9f33ab4 100644 --- a/lib.php +++ b/lib.php @@ -15,11 +15,11 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola @@ -32,133 +32,88 @@ /** * Lib class * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola */ -class enrol_notificationeabc_plugin extends enrol_plugin -{ +class enrol_notification_plugin extends enrol_plugin { /** @var log.*/ private $log = ''; // Funcion que envia las notificaciones a los usuarios. /** - * Send mail method - * @param stdClass $user instancia usuario - * @param stdClass $course instancia curso - * @param int $type aviso matriculacion, actualizacion o desmatriculacion + * Send mail method + * @param stdClass $user user instance + * @param stdClass $course course instance + * @param int $type notice enrollment, update or unenrollment * @return bool */ - public function enviarmail(stdClass $user, stdClass $course, $type) { - global $CFG, $DB, $COURSE; - - $course->url = $CFG->wwwroot . '/course/view.php?id=' . $course->id; - - $enrol = $DB->get_record('enrol', array('enrol' => 'notificationeabc', 'courseid' => $course->id, 'status' => 0)); - $activeglobalenrol = $this->get_config('activarglobal'); - $activarglobalunenrolalert = $this->get_config('activarglobalunenrolalert'); - $activarglobalenrolupdated = $this->get_config('activarglobalenrolupdated'); - $mensajeenrol = $this->get_config('location'); - $plainmensajeenrol = strip_tags($mensajeenrol); - $mensajeunenrol = $this->get_config('unenrolmessage'); - $plainmensajeunenrol = strip_tags($mensajeunenrol); - $mensajeupdateenrol = $this->get_config('updatedenrolmessage'); - $plainmensajeupdateenrol = strip_tags($mensajeupdateenrol); - - switch ((int)$type) { - case 1: - // Si no se configuro un mensaje personalizado se envia uno por defecto basico. - if (!empty($enrol) && !empty($enrol->customtext1)) { - $texto = strip_tags($enrol->customtext1); - if (!empty($texto)) { - $mensaje = $this->process_mensaje($enrol->customtext1, $user, $course); - } else { - $mensaje = get_string("filelockedmail", "enrol_notificationeabc", $course); - } - - } else if (!empty($activeglobalenrol) && !empty($plainmensajeenrol)) { - $mensaje = $this->process_mensaje($mensajeenrol, $user, $course); - } else { - $mensaje = get_string("filelockedmail", "enrol_notificationeabc", $course); - } - break; - case 2: - - if (!empty($enrol) && !empty($enrol->customtext2)) { - $texto = strip_tags($enrol->customtext2); - if (!empty($texto)) { - $mensaje = $this->process_mensaje($enrol->customtext2, $user, $course); - } else { - $mensaje = get_string("unenrolmessagedefault", "enrol_notificationeabc", $course); - } - - } else if (!empty($activarglobalunenrolalert) && !empty($plainmensajeunenrol)) { - $mensaje = $this->process_mensaje($mensajeunenrol, $user, $course); - } else { - $mensaje = get_string("unenrolmessagedefault", "enrol_notificationeabc", $course); - } - break; - case 3: - - if (!empty($enrol) && !empty($enrol->customtext3)) { - $texto = strip_tags($enrol->customtext3); - if (!empty($texto)) { - $mensaje = $this->process_mensaje($enrol->customtext3, $user, $course); - } else { - $mensaje = get_string("updatedenrolmessagedefault", "enrol_notificationeabc", $course); - } - - } else if (!empty($activarglobalenrolupdated) && !empty($plainmensajeupdateenrol)) { - $mensaje = $this->process_mensaje($mensajeupdateenrol, $user, $course); - } else { - $mensaje = get_string("updatedenrolmessagedefault", "enrol_notificationeabc", $course); - } - break; + public function send_email(stdClass $user, stdClass $course, $type) { + global $CFG, $DB; - default: - break; - } + $enrol = $DB->get_record('enrol', array('enrol' => 'notification', 'courseid' => $course->id, 'status' => 0)); - $soporte = core_user::get_support_user(); + $pluginconfig = get_config('enrol_notification'); - $sender = get_admin(); + if ($type == 1) { + $enrolalert = $pluginconfig->enrolalert; + $globalenrolalert = $pluginconfig->globalenrolalert; - if (!empty($enrol) && !empty($enrol->customchar1)) { - $sender->email = $enrol->customchar1; - } else { - $sender->email = $soporte->email; + if (!$message = $enrol->customtext1) { // Course level. + if (!$message = $pluginconfig->enrolmessage) { // Plugin level. + $message = get_string('enrolmessagedefault', 'enrol_notification'); + } + } } - if (!empty($enrol) && !empty($enrol->customchar2)) { - $sender->firstname = $enrol->customchar2; - } else { - $sender->firstname = $soporte->firstname; + if ($type == 2) { + $unenrolalert = $pluginconfig->unenrolalert; + $globalunenrolalert = $pluginconfig->globalunenrolalert; + + if (!$message = $enrol->customtext2) { // Course level. + if (!$message = $pluginconfig->unenrolmessage) { // Plugin level. + $message = get_string('unenrolmessagedefault', 'enrol_notification'); + } + } } - $sender->lastname = $soporte->lastname; + if ($type == 3) { + $enrolmentupdatealert = $pluginconfig->enrolmentupdatealert; + $globalenrolmentupdatealert = $pluginconfig->globalenrolmentupdatealert; + + if (!$message = $enrol->customtext3) { // Course level. + if (!$message = $pluginconfig->enrolmentupdatemessage) { // Plugin level. + $message = get_string('enrolmentupdatemessagedefault', 'enrol_notification'); + } + } + } + + $message = $this->get_message($message, $user, $course); + + $supportuser = \core_user::get_support_user(); $eventdata = new \core\message\message(); $eventdata->courseid = $course->id; $eventdata->modulename = 'moodle'; - $eventdata->component = 'enrol_notificationeabc'; - $eventdata->name = 'notificationeabc_enrolment'; - $eventdata->userfrom = $sender; + $eventdata->component = 'enrol_notification'; + $eventdata->name = 'notification_enrolment'; + $eventdata->userfrom = $supportuser; $eventdata->userto = $user->id; - $eventdata->subject = get_string('subject', 'enrol_notificationeabc'); + $eventdata->subject = get_string('subject', 'enrol_notification'); $eventdata->fullmessage = ''; $eventdata->fullmessageformat = FORMAT_HTML; - $eventdata->fullmessagehtml = $mensaje; + $eventdata->fullmessagehtml = $message; $eventdata->smallmessage = ''; $strdata = new stdClass(); $strdata->username = $user->username; $strdata->coursename = $course->fullname; if (message_send($eventdata)) { - $this->log .= get_string('succefullsend', 'enrol_notificationeabc', $strdata); + $this->log .= get_string('succefullsend', 'enrol_notification', $strdata); return true; } else { - $this->log .= get_string('failsend', 'enrol_notificationeabc', $strdata); + $this->log .= get_string('failsend', 'enrol_notification', $strdata); return false; } } // End of function. @@ -166,24 +121,25 @@ public function enviarmail(stdClass $user, stdClass $course, $type) { // Procesa el mensaje para aceptar marcadores. /** * Proccess message method - * @param String $mensaje el mensaje en bruto - * @param stdClass $user instancia usuario - * @param stdClass $course instancia curso - * @return String el mensaje procesado + * @param String $message the raw message + * @param stdClass $user user instance + * @param stdClass $course course instance + * @return String the processed message */ - public function process_mensaje($mensaje, stdClass $user, stdClass $course) { + public function get_message($message, stdClass $user, stdClass $course) { global $CFG; - $m = $mensaje; + + $m = $message; $url = new moodle_url($CFG->wwwroot . '/course/view.php', array('id' => $course->id)); $m = str_replace('{COURSENAME}', $course->fullname, $m); $m = str_replace('{USERNAME}', $user->username, $m); - $m = str_replace('{NOMBRE}', $user->firstname, $m); - $m = str_replace('{APELLIDO}', $user->lastname, $m); + $m = str_replace('{FIRSTNAME}', $user->firstname, $m); + $m = str_replace('{LASTNAME}', $user->lastname, $m); $m = str_replace('{URL}', $url, $m); + return $m; } - /** * Returns link to page which may be used to add new instance of enrolment plugin in course. * @param int $courseid @@ -191,13 +147,15 @@ public function process_mensaje($mensaje, stdClass $user, stdClass $course) { */ public function get_newinstance_link($courseid) { global $DB; - $numenrol = $DB->count_records('enrol', array('courseid' => $courseid, 'enrol' => 'notificationeabc')); + + $numenrol = $DB->count_records('enrol', array('courseid' => $courseid, 'enrol' => 'notification')); $context = context_course::instance($courseid, MUST_EXIST); - if (!has_capability('enrol/notificationeabc:manage', $context) or $numenrol >= 1) { + if (!has_capability('enrol/notification:manage', $context) or $numenrol >= 1) { return null; } - return new moodle_url('/enrol/notificationeabc/edit.php', array('courseid' => $courseid)); + + return new moodle_url('/enrol/notification/edit.php', array('courseid' => $courseid)); } @@ -206,19 +164,26 @@ public function get_newinstance_link($courseid) { * @return array */ public function get_instance_defaults() { - $fields = array(); - $fields['customtext1'] = $this->get_config('location'); - $fields['customtext2'] = $this->get_config('unenrolmessage'); - $fields['customtext3'] = $this->get_config('updatedenrolmessage'); + + $pluginconfig = get_config('enrol_notification'); + $fields['status'] = 1; - $fields['customint1'] = 0; - $fields['customint2'] = 0; - $fields['customint3'] = $this->get_config('activeenrolalert'); - $fields['customint4'] = $this->get_config('activeunenrolalert'); - $fields['customint5'] = $this->get_config('activeenrolupdatedalert'); - $fields['customchar1'] = $this->get_config('emailsender'); - $fields['customchar2'] = $this->get_config('namesender'); + $fields['customint1'] = $this->get_config('enrolalert'); + $fields['customint2'] = $this->get_config('unenrolalert'); + $fields['customint3'] = $this->get_config('enrolmentupdatealert'); + + $fields['customtext1'] = array(); + $fields['customtext1']['text'] = $pluginconfig->enrolmessage; + $fields['customtext1']['format'] = '1'; + + $fields['customtext2'] = array(); + $fields['customtext2']['text'] = $pluginconfig->unenrolmessage; + $fields['customtext2']['format'] = '1'; + + $fields['customtext3'] = array(); + $fields['customtext3']['text'] = $pluginconfig->enrolmentupdatemessage; + $fields['customtext3']['format'] = '1'; return $fields; } @@ -232,32 +197,49 @@ public function get_instance_defaults() { public function get_action_icons(stdClass $instance) { global $OUTPUT; - if ($instance->enrol !== 'notificationeabc') { - throw new coding_exception('invalid enrol instance!'); - } $context = context_course::instance($instance->courseid); $icons = array(); - if (has_capability('enrol/notificationeabc:manage', $context)) { + if (has_capability('enrol/notification:manage', $context)) { $editlink = new moodle_url( - "/enrol/notificationeabc/edit.php", - array('courseid' => $instance->courseid, 'id' => $instance->id) + '/enrol/editinstance.php', + array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => 'notification') ); $icons[] = $OUTPUT->action_icon( $editlink, - new pix_icon( - 'i/edit', - get_string('edit'), - 'core', - array('class' => 'icon') - ) + new pix_icon( 't/edit', get_string('edit'), 'core', array('class' => 'icon iconsmall')) ); } return $icons; } + /** + * Is it possible to delete enrol instance via standard UI? + * + * @param object $instance + * @return bool + */ + public function can_delete_instance($instance) { + $context = context_course::instance($instance->courseid); + if (!has_capability('enrol/notification:manage', $context)) { + return false; + } + + return true; + } + + /** + * Is it possible to hide/show enrol instance via standard UI? + * + * @param stdClass $instance + * @return bool + */ + public function can_hide_show_instance($instance) { + $context = context_course::instance($instance->courseid); + return has_capability('enrol/notification:config', $context); + } /** * get info icons method @@ -266,9 +248,193 @@ public function get_action_icons(stdClass $instance) { */ public function get_info_icons(array $instances) { $icons = array(); - $icons[] = new pix_icon('email', get_string('pluginname', 'enrol_notificationeabc'), 'enrol_notificationeabc'); + $icons[] = new pix_icon('email', get_string('pluginname', 'enrol_notification'), 'enrol_notification'); return $icons; } + /** + * We are a good plugin and don't invent our own UI/validation code path. + * + * @return boolean + */ + public function use_standard_editing_ui() { + return true; + } + + /** + * Return an array of valid options for the status. + * + * @return array + */ + protected function get_status_options() { + $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), + ENROL_INSTANCE_DISABLED => get_string('no')); + return $options; + } + + /** + * Add elements to the edit instance form. + * + * @param stdClass $instance + * @param MoodleQuickForm $mform + * @param context $context + * @return bool + */ + public function edit_instance_form($instance, MoodleQuickForm $mform, $context) { + + // Bloody hack! + if (!is_array($instance->customtext1)) { + $temp = $instance->customtext1; + $instance->customtext1 = array(); + $instance->customtext1['text'] = $temp; + $instance->customtext1['format'] = '1'; + + $temp = $instance->customtext2; + $instance->customtext2 = array(); + $instance->customtext2['text'] = $temp; + $instance->customtext2['format'] = '1'; + + $temp = $instance->customtext3; + $instance->customtext3 = array(); + $instance->customtext3['text'] = $temp; + $instance->customtext3['format'] = '1'; + } + // End of bloody hack! + + $textareaparams = array('rows' => 8, 'cols' => 60); + + $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); + $mform->setType('name', PARAM_RAW); + $mform->setDefault('name', get_string('pluginname', 'enrol_notification')); + + $options = $this->get_status_options(); + $mform->addElement('select', 'status', get_string('status', 'enrol_notification'), $options); + $mform->setDefault('status', $this->get_config('status')); + + // Enrol notifications -> 'enrolalert'. + $mform->addElement('advcheckbox', 'customint1', get_string('enrolalert', 'enrol_notification')); + $mform->addHelpButton('customint1', 'enrolalert', 'enrol_notification'); + $mform->setDefault('customint1', $this->get_config('enrolalert')); + + // Enrol notifications -> 'enrolmessage' + $mform->addElement('editor', 'customtext1', get_string('enrolmessage', 'enrol_notification'), $textareaparams); + $mform->addHelpButton('customtext1', 'enrolmessage', 'enrol_notification'); + $mform->setType('customtext1', PARAM_RAW); + $mform->setDefault('customtext1', $this->get_config('enrolmessage')); + + // Unenrol notifications -> 'unenrolalert'. + $mform->addElement('advcheckbox', 'customint2', get_string('unenrolalert', 'enrol_notification')); + $mform->addHelpButton('customint2', 'unenrolalert', 'enrol_notification'); + $mform->setDefault('customint2', $this->get_config('unenrolalert')); + + // Unenrol notifications -> 'unenrolmessage'. + $mform->addElement('editor', 'customtext2', get_string('unenrolmessage', 'enrol_notification'), $textareaparams); + $mform->addHelpButton('customtext2', 'unenrolmessage', 'enrol_notification'); + $mform->setType('customtext2', PARAM_RAW); + $mform->setDefault('customtext2', $this->get_config('unenrolmessage')); + + // Update enrolment notifications -> 'enrolmentupdatealert'. + $mform->addElement('advcheckbox', 'customint3', get_string('enrolmentupdatealert', 'enrol_notification')); + $mform->addHelpButton('customint3', 'enrolmentupdatealert', 'enrol_notification'); + $mform->setDefault('customint3', $this->get_config('enrolmentupdatealert')); + + // Update enrolment notifications -> 'enrolmentupdatemessage'. + $mform->addElement('editor', 'customtext3', get_string('enrolmentupdatemessage', 'enrol_notification'), $textareaparams); + $mform->addHelpButton('customtext3', 'enrolmentupdatemessage', 'enrol_notification'); + $mform->setType('customtext3', PARAM_RAW); + $mform->setDefault('customtext3', $this->get_config('enrolmentupdatemessage')); + + if (enrol_accessing_via_instance($instance)) { + $warntext = get_string('instanceeditselfwarningtext', 'core_enrol'); + $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warntext); + } + } + + public function add_instance($course, array $fields = NULL) { + global $DB; + + if ($course->id == SITEID) { + throw new coding_exception('Invalid request to add enrol instance to frontpage.'); + } + + $instance = new stdClass(); + $instance->enrol = $this->get_name(); + $instance->status = ENROL_INSTANCE_ENABLED; + $instance->courseid = $course->id; + $instance->enrolstartdate = 0; + $instance->enrolenddate = 0; + $instance->timemodified = time(); + $instance->timecreated = $instance->timemodified; + $instance->sortorder = $DB->get_field('enrol', 'COALESCE(MAX(sortorder), -1) + 1', array('courseid'=>$course->id)); + + $fields = (array)$fields; + unset($fields['enrol']); + unset($fields['courseid']); + unset($fields['sortorder']); + foreach($fields as $field=>$value) { + $instance->$field = $value; + } + + $instance->customtext1 = $fields['customtext1']['text']; + $instance->customtext2 = $fields['customtext2']['text']; + $instance->customtext3 = $fields['customtext3']['text']; + + $instance->id = $DB->insert_record('enrol', $instance); + + \core\event\enrol_instance_created::create_from_record($instance)->trigger(); + + return $instance->id; + } + + public function update_instance($instance, $data) { + global $DB; + + $properties = array('status', 'name', 'password', 'customint1', 'customint2', 'customint3', + 'customint4', 'customint5', 'customint6', 'customint7', 'customint8', + 'customchar1', 'customchar2', 'customchar3', 'customdec1', 'customdec2', + 'customtext4', 'roleid', + 'enrolperiod', 'expirynotify', 'notifyall', 'expirythreshold', + 'enrolstartdate', 'enrolenddate', 'cost', 'currency'); + + foreach ($properties as $key) { + if (isset($data->$key)) { + $instance->$key = $data->$key; + } + } + $instance->timemodified = time(); + + $instance->customtext1 = $data->customtext1['text']; + $instance->customtext2 = $data->customtext2['text']; + $instance->customtext3 = $data->customtext3['text']; + + $update = $DB->update_record('enrol', $instance); + if ($update) { + \core\event\enrol_instance_updated::create_from_record($instance)->trigger(); + } + + return $update; + } + + /** + * Perform custom validation of the data used to edit the instance. + * + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @param object $instance The instance loaded from the DB + * @param context $context The context of the instance we are editing + * @return array of "element_name"=>"error_description" if there are errors, + * or an empty array if everything is OK. + * @return void + */ + public function edit_instance_validation($data, $files, $instance, $context) { + $errors = array(); + $validstatus = array_keys($this->get_status_options()); + $tovalidate = array('status' => $validstatus); + + $typeerrors = $this->validate_param_types($data, $tovalidate); + $errors = array_merge($errors, $typeerrors); + + return $errors; + } } // End of class. diff --git a/readme.md b/readme.md index 5379f66..fc4bd62 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# Notificationeabc plugin +# Notification plugin Send notifications to users when any enrolment event is executed diff --git a/settings.php b/settings.php index 4d2c738..4fe4dff 100644 --- a/settings.php +++ b/settings.php @@ -16,11 +16,11 @@ /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola @@ -34,82 +34,43 @@ // Enrol notification. $settings->add(new admin_setting_configcheckbox( - 'enrol_notificationeabc/activeenrolalert', - get_string('activeenrolalert', 'enrol_notificationeabc'), - get_string('activeenrolalert_help', 'enrol_notificationeabc'), - '', - '1') - ); - $settings->add(new admin_setting_configcheckbox( - 'enrol_notificationeabc/activarglobal', - get_string('activarglobal', 'enrol_notificationeabc'), - get_string('activarglobal_help', 'enrol_notificationeabc'), - '') - ); - $settings->add(new admin_setting_heading( - 'enrol_notificationeabc_settings', - '', - get_string('pluginname_desc', 'enrol_notificationeabc'), + 'enrol_notification/globalenrolalert', + get_string('globalenrolalert', 'enrol_notification'), + get_string('globalenrolalert_help', 'enrol_notification'), '') ); $settings->add(new admin_setting_confightmleditor( - 'enrol_notificationeabc/location', - get_string('location', 'enrol_notificationeabc'), - get_string('location_help', 'enrol_notificationeabc'), - '') + 'enrol_notification/enrolmessage', + get_string('enrolmessage', 'enrol_notification'), + get_string('enrolmessage_help', 'enrol_notification'), + new lang_string('enrolmessagedefault', 'enrol_notification')) ); // Unenrol notification. $settings->add(new admin_setting_configcheckbox( - 'enrol_notificationeabc/activeunenrolalert', - get_string('activeunenrolalert', 'enrol_notificationeabc'), - get_string('activeunenrolalert_help', 'enrol_notificationeabc'), - '', - '1') - ); - $settings->add(new admin_setting_configcheckbox( - 'enrol_notificationeabc/activarglobalunenrolalert', - get_string('activarglobalunenrolalert', 'enrol_notificationeabc'), - get_string('activarglobalunenrolalert_help', 'enrol_notificationeabc'), + 'enrol_notification/globalunenrolalert', + get_string('globalunenrolalert', 'enrol_notification'), + get_string('globalunenrolalert_help', 'enrol_notification'), '') ); $settings->add(new admin_setting_confightmleditor( - 'enrol_notificationeabc/unenrolmessage', - get_string('unenrolmessage', 'enrol_notificationeabc'), - get_string('unenrolmessage_help', 'enrol_notificationeabc'), - '') + 'enrol_notification/unenrolmessage', + get_string('unenrolmessage', 'enrol_notification'), + get_string('unenrolmessage_help', 'enrol_notification'), + new lang_string('unenrolmessagedefault', 'enrol_notification')) ); // Update enrol notification. $settings->add(new admin_setting_configcheckbox( - 'enrol_notificationeabc/activeenrolupdatedalert', - get_string('activeenrolupdatedalert', 'enrol_notificationeabc'), - get_string('activeenrolupdatedalert_help', 'enrol_notificationeabc'), - '', - '1') - ); - $settings->add(new admin_setting_configcheckbox( - 'enrol_notificationeabc/activarglobalenrolupdated', - get_string('activarglobalenrolupdated', 'enrol_notificationeabc'), - get_string('activarglobalenrolupdated_help', 'enrol_notificationeabc'), + 'enrol_notification/globalenrolmentupdatealert', + get_string('globalenrolmentupdatealert', 'enrol_notification'), + get_string('globalenrolmentupdatealert_help', 'enrol_notification'), '') ); $settings->add(new admin_setting_confightmleditor( - 'enrol_notificationeabc/updatedenrolmessage', - get_string('updatedenrolmessage', 'enrol_notificationeabc'), - get_string('updatedenrolmessage_help', 'enrol_notificationeabc'), - '') - ); - $settings->add(new admin_setting_configtext( - 'enrol_notificationeabc/emailsender', - get_string('emailsender', 'enrol_notificationeabc'), - get_string('emailsender_help', 'enrol_notificationeabc'), - '') - ); - $settings->add(new admin_setting_configtext( - 'enrol_notificationeabc/namesender', - get_string('namesender', 'enrol_notificationeabc'), - get_string('namesender_help', 'enrol_notificationeabc'), - '') + 'enrol_notification/enrolmentupdatemessage', + get_string('enrolmentupdatemessage', 'enrol_notification'), + get_string('enrolmentupdatemessage_help', 'enrol_notification'), + new lang_string('enrolmentupdatemessagedefault', 'enrol_notification')) ); } diff --git a/version.php b/version.php index c67dd02..79e1b6d 100644 --- a/version.php +++ b/version.php @@ -15,20 +15,20 @@ // along with Moodle. If not, see . /** - * Notificationeabc enrolment plugin. + * Notification enrolment plugin. * * This plugin notifies users when an event occurs on their enrolments (enrol, unenrol, update enrolment) * - * @package enrol_notificationeabc + * @package enrol_notification * @copyright 2017 e-ABC Learning * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Osvaldo Arriola */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017111700; +$plugin->version = 2018022200; $plugin->requires = 2016120503; -$plugin->component = 'enrol_notificationeabc'; +$plugin->component = 'enrol_notification'; $plugin->maturity = MATURITY_STABLE; $plugin->release = '3.2.0.1'; $plugin->cron = 30;