Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 95 additions & 6 deletions block_enrolkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,119 @@
require_once($CFG->libdir . '/authlib.php');

/**
* Block for allowing enrolment via an enrol key.
*
* @package block_enrolkey
* @copyright Gleimer Mora <gleimermora@catalyst-au.net>
* @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() {
$this->title = get_string('pluginname', 'block_enrolkey');
}

/**
* @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();
Expand All @@ -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.
Expand Down
22 changes: 16 additions & 6 deletions classes/form/enrolkey_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gleimermora@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrolkey_form extends \moodleform {

/**
* @var \auth_plugin_enrolkey
*/
Expand All @@ -41,6 +44,7 @@ class enrolkey_form extends \moodleform {

/**
* Add elements to form
*
* @throws coding_exception
*/
public function definition() {
Expand All @@ -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')];
}
Expand Down
4 changes: 2 additions & 2 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'user' => CAP_ALLOW,
],

'clonepermissionsfrom' => 'moodle/my:manageblocks'
'clonepermissionsfrom' => 'moodle/my:manageblocks',
],

'block/enrolkey:addinstance' => [
Expand All @@ -42,7 +42,7 @@
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
'manager' => CAP_ALLOW,
],
],
];
39 changes: 39 additions & 0 deletions edit_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Block edit form class for the block_enrolkey plugin.
*
* @package block_enrolkey
* @copyright Gleimer Mora <gleimermora@catalyst-au.net>
* @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);
}
}
9 changes: 9 additions & 0 deletions lang/en/block_enrolkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 href="{$a->href}">{$a->course}</a>\'';
$string['enrolnotification_dates'] = 'You have enrolled into {$a->course} as a {$a->role}. <a href={$a->href}>Click here to view the course.</a><br />Course starts: {$a->startdate}<br />Course ends: {$a->enddate}';
$string['enrolnotification_dates_startonly'] = 'You have enrolled into {$a->course} as a {$a->role}. <a href={$a->href}>Click here to view the course.</a><br />Course starts: {$a->startdate}';
$string['enrolnotification_dates_endonly'] = 'You have enrolled into {$a->course} as a {$a->role}. <a href={$a->href}>Click here to view the course.</a><br />Course ends: {$a->enddate}';
$string['formsuccess'] = 'Enrolment successful';
4 changes: 2 additions & 2 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.block_enrolkey .form-group > .col-md-9 .form-control {
width: 100%
width: 100%;
}

@media (min-width: 768px) {
Expand All @@ -18,6 +18,6 @@

.block_enrolkey .mform > .form-group {
margin-left: 0;
flex: 0 0 100%
flex: 0 0 100%;
}
}