Commit f7589515 authored by Damyon Wiese's avatar Damyon Wiese
Browse files

MDL-48362 enrol: Convert guest to standard enrolment ui

Also - provide automatic course navigation links when using the standard ui.
parent 51c736f0
<?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/>.
/**
* Guest access plugin.
*
* Adds new instance of enrol_guest to specified course
* or edits current instance.
*
* @package enrol_guest
* @copyright 2015 Andrew Hancox <andrewdchancox@googlemail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_guest;
use moodleform;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* Class enrol_guest_edit_form
* @copyright 2015 Andrew Hancox <andrewdchancox@googlemail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_guest_edit_form extends moodleform {
/**
* Form definition
*/
public function definition() {
$mform = $this->_form;
list($instance, $plugin) = $this->_customdata;
$mform->addElement('header', 'header', get_string('pluginname', 'enrol_guest'));
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$mform->addElement('select', 'status', get_string('status', 'enrol_guest'), $options);
$mform->addHelpButton('status', 'status', 'enrol_guest');
$mform->setDefault('status', $plugin->get_config('status'));
$mform->setAdvanced('status', $plugin->get_config('status_adv'));
$mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_guest'));
$mform->addHelpButton('password', 'password', 'enrol_guest');
// If we have a new instance and the password is required - make sure it is set. For existing
// instances we do not force the password to be required as it may have been set to empty before
// the password was required. We check in the validation function whether this check is required
// for existing instances.
if (empty($instance->id) && $plugin->get_config('requirepassword')) {
$mform->addRule('password', get_string('required'), 'required', null);
}
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
$this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
}
/**
* Form validation
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
list($instance, $plugin) = $this->_customdata;
$checkpassword = false;
if ($data['id']) {
// Check the password if we are enabling the plugin again.
if (($instance->status == ENROL_INSTANCE_DISABLED) && ($data['status'] == ENROL_INSTANCE_ENABLED)) {
$checkpassword = true;
}
// Check the password if the instance is enabled and the password has changed.
if (($data['status'] == ENROL_INSTANCE_ENABLED) && ($instance->password !== $data['password'])) {
$checkpassword = true;
}
} else {
$checkpassword = true;
}
if ($checkpassword) {
$require = $plugin->get_config('requirepassword');
$policy = $plugin->get_config('usepasswordpolicy');
if ($require && trim($data['password']) === '') {
$errors['password'] = get_string('required');
} else if (!empty($data['password']) && $policy) {
$errmsg = '';
if (!check_password_policy($data['password'], $errmsg)) {
$errors['password'] = $errmsg;
}
}
}
return $errors;
}
}
<?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/>.
/**
* Edit instance of enrol_guest.
*
* Adds new instance of enrol_guest to specified course
* or edits current instance.
*
* @package enrol_guest
* @copyright 2015 Andrew Hancox <andrewdchancox@googlemail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.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/guest:config', $context);
$PAGE->set_url('/enrol/guest/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('guest')) {
redirect($return);
}
$plugin = enrol_get_plugin('guest');
if ($instanceid) {
$conditions = array('courseid' => $course->id, 'enrol' => 'guest', 'id' => $instanceid);
$instance = $DB->get_record('enrol', $conditions, '*', 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;
}
$mform = new \enrol_guest\enrol_guest_edit_form(null, array($instance, $plugin));
$mform->set_data($instance);
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->password = $data->password;
$instance->timemodified = time();
$DB->update_record('enrol', $instance);
if ($reset) {
$context->mark_dirty();
}
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
} else {
$fields = array(
'status' => $data->status,
'password' => $data->password);
$plugin->add_instance($course, $fields);
}
redirect($return);
}
$PAGE->set_heading($course->fullname);
$PAGE->set_title(get_string('pluginname', 'enrol_guest'));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'enrol_guest'));
$mform->display();
echo $OUTPUT->footer();
......@@ -84,51 +84,6 @@ class enrol_guest_plugin extends enrol_plugin {
return;
}
/**
* Sets up navigation entries.
*
* @param stdClass $instancesnode
* @param stdClass $instance
* @return void
* @throws coding_exception
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
if ($instance->enrol !== 'guest') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = context_course::instance($instance->courseid);
if (has_capability('enrol/guest:config', $context)) {
$managelink = new moodle_url('/enrol/guest/edit.php', array('courseid' => $instance->courseid, 'id' => $instance->id));
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
}
/**
* Returns edit icons for the page with list of instances
* @param stdClass $instance
* @return array
* @throws coding_exception
*/
public function get_action_icons(stdClass $instance) {
global $OUTPUT;
if ($instance->enrol !== 'guest') {
throw new coding_exception('invalid enrol instance!');
}
$context = context_course::instance($instance->courseid);
$icons = array();
if (has_capability('enrol/guest:config', $context)) {
$editlink = new moodle_url("/enrol/guest/edit.php", array('courseid' => $instance->courseid, 'id' => $instance->id));
$icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
array('class' => 'iconsmall')));
}
return $icons;
}
/**
* Attempt to automatically gain temporary guest access to course,
* calling code has to make sure the plugin and instance are active.
......@@ -161,24 +116,24 @@ class enrol_guest_plugin extends enrol_plugin {
}
/**
* Returns link to page which may be used to add new instance of enrolment plugin in course.
* Returns true if the current user can add a new instance of enrolment plugin in course.
* @param int $courseid
* @return moodle_url page url
* @return boolean
*/
public function get_newinstance_link($courseid) {
public function can_add_instance($courseid) {
global $DB;
$context = context_course::instance($courseid, MUST_EXIST);
if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) {
return NULL;
return false;
}
if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) {
return NULL;
return false;
}
return new moodle_url('/enrol/guest/edit.php', array('courseid' => $courseid));
return true;
}
/**
......@@ -420,4 +375,108 @@ class enrol_guest_plugin extends enrol_plugin {
}
return $instanceinfo;
}
/**
* 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) {
global $CFG;
$options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_guest'), $options);
$mform->addHelpButton('status', 'status', 'enrol_guest');
$mform->setDefault('status', $this->get_config('status'));
$mform->setAdvanced('status', $this->get_config('status_adv'));
$mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_guest'));
$mform->addHelpButton('password', 'password', 'enrol_guest');
// If we have a new instance and the password is required - make sure it is set. For existing
// instances we do not force the password to be required as it may have been set to empty before
// the password was required. We check in the validation function whether this check is required
// for existing instances.
if (empty($instance->id) && $this->get_config('requirepassword')) {
$mform->addRule('password', get_string('required'), 'required', null);
}
}
/**
* 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;
}
/**
* 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();
$checkpassword = false;
if ($data['id']) {
// Check the password if we are enabling the plugin again.
if (($instance->status == ENROL_INSTANCE_DISABLED) && ($data['status'] == ENROL_INSTANCE_ENABLED)) {
$checkpassword = true;
}
// Check the password if the instance is enabled and the password has changed.
if (($data['status'] == ENROL_INSTANCE_ENABLED) && ($instance->password !== $data['password'])) {
$checkpassword = true;
}
} else {
$checkpassword = true;
}
if ($checkpassword) {
$require = $this->get_config('requirepassword');
$policy = $this->get_config('usepasswordpolicy');
if ($require && trim($data['password']) === '') {
$errors['password'] = get_string('required');
} else if (!empty($data['password']) && $policy) {
$errmsg = '';
if (!check_password_policy($data['password'], $errmsg)) {
$errors['password'] = $errmsg;
}
}
}
$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;
}
}
......@@ -71,4 +71,4 @@ class enrol_guest_enrol_form extends moodleform {
return $errors;
}
}
\ No newline at end of file
}
......@@ -77,27 +77,6 @@ class enrol_manual_plugin extends enrol_plugin {
return new moodle_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id, 'id'=>$instance->courseid));
}
/**
* Returns enrolment instance manage link.
*
* By defaults looks for manage.php file and tests for manage capability.
*
* @param navigation_node $instancesnode
* @param stdClass $instance
* @return moodle_url;
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
if ($instance->enrol !== 'manual') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = context_course::instance($instance->courseid);
if (has_capability('enrol/manual:config', $context)) {
$managelink = new moodle_url('/enrol/editinstance.php', array('courseid' => $instance->courseid, 'type' => 'manual'));
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
}
/**
* Return true if we can add a new instance to this course.
*
......
......@@ -96,25 +96,6 @@ class enrol_paypal_plugin extends enrol_plugin {
return ($instance->status == ENROL_INSTANCE_ENABLED);
}
/**
* Sets up navigation entries.
*
* @param object $instance
* @return void
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
if ($instance->enrol !== 'paypal') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = context_course::instance($instance->courseid);
if (has_capability('enrol/paypal:config', $context)) {
$params = array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => 'paypal');
$managelink = new moodle_url('/enrol/editinstance.php', $params);
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
}
/**
* Returns true if the user can add a new instance in this course.
* @param int $courseid
......
......@@ -117,26 +117,6 @@ class enrol_self_plugin extends enrol_plugin {
return true;
}
/**
* Sets up navigation entries.
*
* @param stdClass $instancesnode
* @param stdClass $instance
* @return void
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
if ($instance->enrol !== 'self') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = context_course::instance($instance->courseid);
if (has_capability('enrol/self:config', $context)) {
$linkparams = array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => 'self');
$managelink = new moodle_url('/enrol/editinstance.php', $linkparams);
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
}
/**
* Return true if we can add a new instance to this course.
*
......
......@@ -2,7 +2,8 @@ This files describes API changes in /enrol/* - plugins,
information provided here is intended especially for developers.
=== 3.0 ===
Enrolment plugins UI have been consolidated. Plugins can implement use_standard_editing_ui() function
and add edit_instance_form() and edit_instance_validation() methods instead of providing their own edit.php and form. In future
and add edit_instance_form() and edit_instance_validation() methods instead of providing their own edit.php and form. They can
then rely on the default implementation of get_action_icons and get_course_navigation. In future
this will mean they can be called by webservices/user upload tools because they can validate their data.
=== 3.1 ===
......
......@@ -1924,7 +1924,15 @@ abstract class enrol_plugin {
* @return void
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
// usually adds manage users
if ($this->use_standard_editing_ui()) {
$context = context_course::instance($instance->courseid);
$cap = 'enrol/' . $instance->enrol . ':config';
if (has_capability($cap, $context)) {
$linkparams = array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => $instance->enrol);
$managelink = new moodle_url('/enrol/editinstance.php', $linkparams);
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
}
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment