diff --git a/block_enrolkey.php b/block_enrolkey.php index bc0517d..1f0d647 100644 --- a/block_enrolkey.php +++ b/block_enrolkey.php @@ -19,11 +19,16 @@ require_once($CFG->libdir . '/authlib.php'); /** + * Block for allowing enrolment via an enrol key. + * * @package block_enrolkey + * @copyright Gleimer Mora + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class block_enrolkey extends block_base { - /** + * Initialise the block. + * * @throws coding_exception */ public function init() { @@ -31,20 +36,102 @@ public function init() { } /** - * @return stdClass|stdObject + * Customise title based on config if set. + * + * @return void + */ + public function specialization() { + if (isset($this->config->title)) { + $this->title = format_string($this->config->title, true); + } + } + + /** + * Get the content of the block. + * + * @return stdClass|null * @throws moodle_exception */ public function get_content() { - if ($this->content !== null || $this->is_auth_plugin_enable() === false) { + if ($this->content !== null) { return $this->content; } - $this->content = new stdClass; + if ($this->is_auth_plugin_enable() === false) { + $syscontext = context_system::instance(); + if (has_capability('moodle/site:config', $syscontext) || has_capability('enrol/self:config', $syscontext)) { + // User should be notified that this won't display as auth_enrol is not enabled. + $this->content = new stdClass(); + $this->content->text = html_writer::start_div('block_' . $this->name()); + $this->content->text .= html_writer::tag('p', get_string('enrolkeynotenabled', 'block_enrolkey')); + $this->content->text .= html_writer::end_div(); + return $this->content; + } else { + // Regular user so return null. + return null; + } + } + + $this->content = new stdClass(); $authplugin = get_auth_plugin('enrolkey'); $form = (new \block_enrolkey\form\enrolkey_form())->set_plugin($authplugin); if ($form->get_data()) { + global $DB, $SESSION; + $enrolids = $form->get_enrol_ids(); - redirect(new moodle_url("/auth/enrolkey/view.php", ['ids' => implode(',', $enrolids)])); + if (count($enrolids) > 0) { + // There were successful enrolments, first clear existing notifications. + if (isset($SESSION->notifications) && is_array($SESSION->notifications)) { + $SESSION->notifications = array_filter($SESSION->notifications, function ($n) { + if ($n->message === get_string('youenrolledincourse', 'enrol')) { + return false; + } + return true; + }); + } + + // Now add new notifications. + foreach ($enrolids as $enrolid) { + $plugin = $DB->get_record('enrol', ['enrol' => 'self', 'id' => $enrolid]); + $course = $DB->get_record('course', ['id' => $plugin->courseid]); + + $coursecontext = context_course::instance($plugin->courseid); + $rolenames = role_get_names($coursecontext, ROLENAME_ALIAS, true); + + $data = new stdClass(); + $data->course = $course->fullname; + $data->enrolinstance = $plugin->name; + $data->role = $rolenames[$plugin->roleid]; + $data->startdate = date('Y-m-d H:i', $plugin->enrolstartdate); + $data->enddate = date('Y-m-d H:i', $plugin->enrolenddate); + $data->href = (new moodle_url('/course/view.php', ['id' => $plugin->courseid]))->out(); + + if ($plugin->enrolstartdate > 0 && $plugin->enrolenddate > 0) { + // The course had both a start and end date. + $successoutput = get_string('enrolnotification_dates', 'block_enrolkey', $data); + } else if ($plugin->enrolstartdate > 0 && $plugin->enrolenddate == 0) { + // The course only has a start date set. + $successoutput = get_string('enrolnotification_dates_startonly', 'block_enrolkey', $data); + } else if ($plugin->enrolstartdate == 0 && $plugin->enrolenddate > 0) { + // The course only has a start date set. + $successoutput = get_string('enrolnotification_dates_endonly', 'block_enrolkey', $data); + } else { + // The course has no date restrictions. + $successoutput = get_string('enrolnotification', 'block_enrolkey', $data); + } + + \core\notification::add($successoutput, \core\notification::SUCCESS); + + $this->content = new stdClass(); + $this->content->text = html_writer::start_div('block_' . $this->name()); + $this->content->text .= html_writer::tag('p', get_string('formsuccess', 'block_enrolkey')); + $this->content->text .= html_writer::end_div(); + return $this->content; + } + } else { + // There was an error. + \core\notification::add(get_string('enrolerror', 'block_enrolkey'), \core\notification::ERROR); + } } $this->content->text = html_writer::start_div('block_' . $this->name()); $this->content->text .= $form->render(); @@ -54,7 +141,9 @@ public function get_content() { } /** - * @return bool|mixed + * Checks whether the auth plugin is enabled. + * + * @return bool */ private function is_auth_plugin_enable() { // Enrolkey does not need to be the signup method. diff --git a/classes/form/enrolkey_form.php b/classes/form/enrolkey_form.php index cb96ce9..135482d 100644 --- a/classes/form/enrolkey_form.php +++ b/classes/form/enrolkey_form.php @@ -22,13 +22,16 @@ defined('MOODLE_INTERNAL') || die; -require_once($CFG->libdir.'/formslib.php'); +require_once($CFG->libdir . '/formslib.php'); /** * Class enrolkey_form + * + * @package block_enrolkey + * @copyright Gleimer Mora + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class enrolkey_form extends \moodleform { - /** * @var \auth_plugin_enrolkey */ @@ -41,6 +44,7 @@ class enrolkey_form extends \moodleform { /** * Add elements to form + * * @throws coding_exception */ public function definition() { @@ -52,33 +56,39 @@ public function definition() { } /** + * Gets the enrolids. + * * @return array */ - public function get_enrol_ids() : array { + public function get_enrol_ids(): array { return $this->enrolids; } /** + * Sets the auth plugin. + * * @param auth_plugin_enrolkey $authplugin * @return enrolkey_form */ - public function set_plugin(auth_plugin_enrolkey $authplugin) : self { + public function set_plugin(auth_plugin_enrolkey $authplugin): self { $this->authplugin = $authplugin; return $this; } /** + * Validate the form. + * * @param array $data * @param array $files * @return array * @throws coding_exception * @throws moodle_exception */ - public function validation($data, $files) : array { + public function validation($data, $files): array { if (!$this->authplugin) { return ['enrolkey' => get_string('pluginerror', 'block_enrolkey')]; } - list($enrolids, $errors) = $this->authplugin->enrol_user($data['enrolkey']); + [$enrolids, $errors] = $this->authplugin->enrol_user($data['enrolkey']); if (empty($enrolids)) { return ['enrolkey' => get_string('invalidkey', 'block_enrolkey')]; } diff --git a/db/access.php b/db/access.php index 9ac432f..cab1470 100644 --- a/db/access.php +++ b/db/access.php @@ -32,7 +32,7 @@ 'user' => CAP_ALLOW, ], - 'clonepermissionsfrom' => 'moodle/my:manageblocks' + 'clonepermissionsfrom' => 'moodle/my:manageblocks', ], 'block/enrolkey:addinstance' => [ @@ -42,7 +42,7 @@ 'contextlevel' => CONTEXT_BLOCK, 'archetypes' => [ 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW + 'manager' => CAP_ALLOW, ], ], ]; diff --git a/edit_form.php b/edit_form.php new file mode 100644 index 0000000..d9f366d --- /dev/null +++ b/edit_form.php @@ -0,0 +1,39 @@ +. + +/** + * Block edit form class for the block_enrolkey plugin. + * + * @package block_enrolkey + * @copyright Gleimer Mora + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_enrolkey_edit_form extends block_edit_form { + /** + * Setup block settings edit form + * + * @param MoodleQuickForm $mform + * @return void + */ + protected function specific_definition($mform) { + // Section header title according to language file. + $mform->addElement('header', 'config_header', get_string('blocksettings', 'block')); + // A sample string variable with a default value. + $mform->addElement('text', 'config_title', get_string('configtitle', 'block_enrolkey')); + $mform->setDefault('config_title', get_string('configtitle_default', 'block_enrolkey')); + $mform->setType('config_title', PARAM_TEXT); + } +} diff --git a/lang/en/block_enrolkey.php b/lang/en/block_enrolkey.php index 70f3933..3615bb5 100644 --- a/lang/en/block_enrolkey.php +++ b/lang/en/block_enrolkey.php @@ -32,3 +32,12 @@ $string['disabled'] = 'Sorry, you may not use this page.'; $string['pluginerror'] = 'something went wrong, try it again later'; $string['privacy:metadata'] = 'The block enrolkey plugin does not store any personal data.'; +$string['enrolkeynotenabled'] = 'The auth_enrolkey plugin is not enabled. This block will not be visible to students.'; +$string['configtitle'] = 'Block title'; +$string['configtitle_default'] = 'Enrol with key'; +$string['enrolerror'] = 'Something went wrong enrolling with the supplied enrol key. Please try again later or contact support.'; +$string['enrolnotification'] = 'You have been enrolled as a {$a->role} into the course \'{$a->course}\''; +$string['enrolnotification_dates'] = 'You have enrolled into {$a->course} as a {$a->role}. href}>Click here to view the course.
Course starts: {$a->startdate}
Course ends: {$a->enddate}'; +$string['enrolnotification_dates_startonly'] = 'You have enrolled into {$a->course} as a {$a->role}. href}>Click here to view the course.
Course starts: {$a->startdate}'; +$string['enrolnotification_dates_endonly'] = 'You have enrolled into {$a->course} as a {$a->role}. href}>Click here to view the course.
Course ends: {$a->enddate}'; +$string['formsuccess'] = 'Enrolment successful'; diff --git a/styles.css b/styles.css index e97ab9d..9053b31 100644 --- a/styles.css +++ b/styles.css @@ -1,5 +1,5 @@ .block_enrolkey .form-group > .col-md-9 .form-control { - width: 100% + width: 100%; } @media (min-width: 768px) { @@ -18,6 +18,6 @@ .block_enrolkey .mform > .form-group { margin-left: 0; - flex: 0 0 100% + flex: 0 0 100%; } }