Commit 60010fd6 authored by Damyon Wiese's avatar Damyon Wiese
Browse files

MDL-48362 enrol: Use a standard UI and validation for enrolment plugins

Convert core enrolment plugins to use standard editing ui.
parent 03b8b55f
<?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/>.
/**
* Adds new instance of enrol_cohort to specified course.
*
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require_once("$CFG->dirroot/enrol/cohort/edit_form.php");
require_once("$CFG->dirroot/enrol/cohort/locallib.php");
require_once("$CFG->dirroot/group/lib.php");
$courseid = required_param('courseid', PARAM_INT);
$instanceid = optional_param('id', 0, PARAM_INT);
$message = optional_param('message', null, PARAM_TEXT);
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id, MUST_EXIST);
require_login($course);
require_capability('moodle/course:enrolconfig', $context);
require_capability('enrol/cohort:config', $context);
$PAGE->set_url('/enrol/cohort/edit.php', array('courseid'=>$course->id, 'id'=>$instanceid));
$PAGE->set_pagelayout('admin');
$returnurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id));
if (!enrol_is_enabled('cohort')) {
redirect($returnurl);
}
$enrol = enrol_get_plugin('cohort');
if ($instanceid) {
$instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'cohort', 'id'=>$instanceid), '*', MUST_EXIST);
} else {
// No instance yet, we have to add new instance.
if (!$enrol->get_newinstance_link($course->id)) {
redirect($returnurl);
}
navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
$instance = new stdClass();
$instance->id = null;
$instance->courseid = $course->id;
$instance->enrol = 'cohort';
$instance->customint1 = ''; // Cohort id.
$instance->customint2 = 0; // Optional group id.
}
// Try and make the manage instances node on the navigation active.
$courseadmin = $PAGE->settingsnav->get('courseadmin');
if ($courseadmin && $courseadmin->get('users') && $courseadmin->get('users')->get('manageinstances')) {
$courseadmin->get('users')->get('manageinstances')->make_active();
}
$mform = new enrol_cohort_edit_form(null, array($instance, $enrol, $course));
if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
if ($data->id) {
// NOTE: no cohort changes here!!!
if ($data->roleid != $instance->roleid) {
// The sync script can only add roles, for perf reasons it does not modify them.
role_unassign_all(array('contextid'=>$context->id, 'roleid'=>$instance->roleid, 'component'=>'enrol_cohort', 'itemid'=>$instance->id));
}
$instance->name = $data->name;
$instance->status = $data->status;
$instance->roleid = $data->roleid;
$instance->customint2 = $data->customint2;
$instance->timemodified = time();
// Create a new group for the cohort if requested.
if ($data->customint2 == COHORT_CREATE_GROUP) {
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_cohort_create_new_group($course->id, $data->customint1);
$instance->customint2 = $groupid;
}
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
} else {
// Create a new group for the cohort if requested.
if ($data->customint2 == COHORT_CREATE_GROUP) {
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_cohort_create_new_group($course->id, $data->customint1);
$enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status,
'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $groupid));
} else {
$enrol->add_instance($course, array('name' => $data->name, 'status' => $data->status,
'customint1' => $data->customint1, 'roleid' => $data->roleid, 'customint2' => $data->customint2));
}
if (!empty($data->submitbuttonnext)) {
$returnurl = new moodle_url($PAGE->url);
$returnurl->param('message', 'added');
}
}
$trace = new null_progress_trace();
enrol_cohort_sync($trace, $course->id);
$trace->finished();
redirect($returnurl);
}
$PAGE->set_heading($course->fullname);
$PAGE->set_title(get_string('pluginname', 'enrol_cohort'));
echo $OUTPUT->header();
if ($message === 'added') {
echo $OUTPUT->notification(get_string('instanceadded', 'enrol'), 'notifysuccess');
}
$mform->display();
echo $OUTPUT->footer();
<?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/>.
/**
* Adds instance form
*
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/formslib.php");
class enrol_cohort_edit_form extends moodleform {
function definition() {
global $CFG, $DB;
$mform = $this->_form;
list($instance, $plugin, $course) = $this->_customdata;
$coursecontext = context_course::instance($course->id);
$enrol = enrol_get_plugin('cohort');
$groups = array(0 => get_string('none'));
if (has_capability('moodle/course:managegroups', $coursecontext)) {
$groups[COHORT_CREATE_GROUP] = get_string('creategroup', 'enrol_cohort');
}
foreach (groups_get_all_groups($course->id) as $group) {
$groups[$group->id] = format_string($group->name, true, array('context'=>$coursecontext));
}
$mform->addElement('header','general', get_string('pluginname', 'enrol_cohort'));
$mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
$mform->setType('name', PARAM_TEXT);
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options);
if ($instance->id) {
if ($cohort = $DB->get_record('cohort', array('id'=>$instance->customint1))) {
$cohorts = array($instance->customint1=>format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid))));
} else {
$cohorts = array($instance->customint1=>get_string('error'));
}
$mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
$mform->setConstant('customint1', $instance->customint1);
$mform->hardFreeze('customint1', $instance->customint1);
} else {
$cohorts = array('' => get_string('choosedots'));
$allcohorts = cohort_get_available_cohorts($coursecontext, 0, 0, 0);
foreach ($allcohorts as $c) {
$cohorts[$c->id] = format_string($c->name);
}
$mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
$mform->addRule('customint1', get_string('required'), 'required', null, 'client');
}
$roles = get_assignable_roles($coursecontext);
$roles[0] = get_string('none');
$roles = array_reverse($roles, true); // Descending default sortorder.
$mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles);
$mform->setDefault('roleid', $enrol->get_config('roleid'));
if ($instance->id and !isset($roles[$instance->roleid])) {
if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
$roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS, true);
$roles[$instance->roleid] = role_get_name($role, $coursecontext);
} else {
$roles[$instance->roleid] = get_string('error');
}
}
$mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups);
$mform->addElement('hidden', 'courseid', null);
$mform->setType('courseid', PARAM_INT);
$mform->addElement('hidden', 'id', null);
$mform->setType('id', PARAM_INT);
if ($instance->id) {
$this->add_action_buttons(true);
} else {
$this->add_add_buttons();
}
$this->set_data($instance);
}
/**
* Adds buttons on create new method form
*/
protected function add_add_buttons() {
$mform = $this->_form;
$buttonarray = array();
$buttonarray[0] = $mform->createElement('submit', 'submitbutton', get_string('addinstance', 'enrol'));
$buttonarray[1] = $mform->createElement('submit', 'submitbuttonnext', get_string('addinstanceanother', 'enrol'));
$buttonarray[2] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');
}
function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
$params = array('roleid'=>$data['roleid'], 'customint1'=>$data['customint1'], 'courseid'=>$data['courseid'], 'id'=>$data['id']);
if ($DB->record_exists_select('enrol', "roleid = :roleid AND customint1 = :customint1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id", $params)) {
$errors['roleid'] = get_string('instanceexists', 'enrol_cohort');
}
return $errors;
}
}
......@@ -79,19 +79,6 @@ class enrol_cohort_plugin extends enrol_plugin {
}
}
/**
* Returns link to page which may be used to add new instance of enrolment plugin in course.
* @param int $courseid
* @return moodle_url page url
*/
public function get_newinstance_link($courseid) {
if (!$this->can_add_new_instances($courseid)) {
return NULL;
}
// Multiple instances supported - multiple parent courses linked.
return new moodle_url('/enrol/cohort/edit.php', array('courseid'=>$courseid));
}
/**
* Given a courseid this function returns true if the user is able to enrol or configure cohorts.
* AND there are cohorts that the user can view.
......@@ -99,7 +86,7 @@ class enrol_cohort_plugin extends enrol_plugin {
* @param int $courseid
* @return bool
*/
protected function can_add_new_instances($courseid) {
public function can_add_instance($courseid) {
global $CFG;
require_once($CFG->dirroot . '/cohort/lib.php');
$coursecontext = context_course::instance($courseid);
......@@ -109,6 +96,54 @@ class enrol_cohort_plugin extends enrol_plugin {
return cohort_get_available_cohorts($coursecontext, 0, 0, 1) ? true : false;
}
/**
* Add new instance of enrol plugin.
* @param object $course
* @param array $fields instance fields
* @return int id of new instance, null if can not be created
*/
public function add_instance($course, array $fields = null) {
if (!empty($fields['customint2']) && $fields['customint2'] == COHORT_CREATE_GROUP) {
// Create a new group for the cohort if requested.
$context = context_course::instance($course->id);
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_cohort_create_new_group($course->id, $fields['customint1']);
$fields['customint2'] = $groupid;
}
return parent::add_instance($course, $fields);
}
/**
* Update instance of enrol plugin.
* @param stdClass $instance
* @param stdClass $data modified instance fields
* @return boolean
*/
public function update_instance($instance, $data) {
// NOTE: no cohort changes here!!!
$context = context_course::instance($instance->courseid);
if ($data->roleid != $instance->roleid) {
// The sync script can only add roles, for perf reasons it does not modify them.
$params = array(
'contextid' => $context->id,
'roleid' => $instance->roleid,
'component' => 'enrol_cohort',
'itemid' => $instance->id
);
role_unassign_all($params);
}
// Create a new group for the cohort if requested.
if ($data->customint2 == COHORT_CREATE_GROUP) {
require_capability('moodle/course:managegroups', $context);
$groupid = enrol_cohort_create_new_group($instance->courseid, $data->customint1);
$data->customint2 = $groupid;
}
return parent::update_instance($instance, $data);
}
/**
* Returns edit icons for the page with list of instances.
* @param stdClass $instance
......@@ -125,7 +160,8 @@ class enrol_cohort_plugin extends enrol_plugin {
$icons = array();
if (has_capability('enrol/cohort:config', $context)) {
$editlink = new moodle_url("/enrol/cohort/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
$linkparams = array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => 'cohort');
$editlink = new moodle_url("/enrol/editinstance.php", $linkparams);
$icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
array('class' => 'iconsmall')));
}
......@@ -316,6 +352,176 @@ class enrol_cohort_plugin extends enrol_plugin {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/cohort:config', $context);
}
/**
* 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;
}
/**
* Return an array of valid options for the cohorts.
*
* @param stdClass $instance
* @param context $context
* @return array
*/
protected function get_cohort_options($instance, $context) {
global $DB, $CFG;
require_once($CFG->dirroot . '/cohort/lib.php');
$cohorts = array();
if ($instance->id) {
if ($cohort = $DB->get_record('cohort', array('id' => $instance->customint1))) {
$name = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)));
$cohorts = array($instance->customint1 => $name);
} else {
$cohorts = array($instance->customint1 => get_string('error'));
}
} else {
$cohorts = array('' => get_string('choosedots'));
$allcohorts = cohort_get_available_cohorts($context, 0, 0, 0);
foreach ($allcohorts as $c) {
$cohorts[$c->id] = format_string($c->name);
}
}
return $cohorts;
}
/**
* Return an array of valid options for the roles.
*
* @param stdClass $instance
* @param context $coursecontext
* @return array
*/
protected function get_role_options($instance, $coursecontext) {
global $DB;
$roles = get_assignable_roles($coursecontext);
$roles[0] = get_string('none');
$roles = array_reverse($roles, true); // Descending default sortorder.
if ($instance->id and !isset($roles[$instance->roleid])) {
if ($role = $DB->get_record('role', array('id' => $instance->roleid))) {
$roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS, true);
$roles[$instance->roleid] = role_get_name($role, $coursecontext);
} else {
$roles[$instance->roleid] = get_string('error');
}
}
return $roles;
}
/**
* Return an array of valid options for the groups.
*
* @param context $coursecontext
* @return array
*/
protected function get_group_options($coursecontext) {
$groups = array(0 => get_string('none'));
if (has_capability('moodle/course:managegroups', $coursecontext)) {
$groups[COHORT_CREATE_GROUP] = get_string('creategroup', 'enrol_cohort');
}
foreach (groups_get_all_groups($coursecontext->instanceid) as $group) {
$groups[$group->id] = format_string($group->name, true, array('context' => $coursecontext));
}
return $groups;
}
/**
* 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;
}
/**
* Add elements to the edit instance form.
*
* @param stdClass $instance
* @param MoodleQuickForm $mform
* @param context $coursecontext
* @return bool
*/
public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecontext) {
global $DB;
$mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
$mform->setType('name', PARAM_TEXT);
$options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options);
$options = $this->get_cohort_options($instance, $coursecontext);
$mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $options);
if ($instance->id) {
$mform->setConstant('customint1', $instance->customint1);
$mform->hardFreeze('customint1', $instance->customint1);
} else {
$mform->addRule('customint1', get_string('required'), 'required', null, 'client');
}
$roles = $this->get_role_options($instance, $coursecontext);
$mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles);
$mform->setDefault('roleid', $this->get_config('roleid'));
$groups = $this->get_group_options($coursecontext);
$mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups);
}
/**
* 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) {
global $DB;
$errors = array();
$params = array(
'roleid' => $data['roleid'],
'customint1' => $data['customint1'],
'courseid' => $data['courseid'],
'id' => $data['id']
);
$sql = "roleid = :roleid AND customint1 = :customint1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id";
if ($DB->record_exists_select('enrol', $sql, $params)) {
$errors['roleid'] = get_string('instanceexists', 'enrol_cohort');
}
$validstatus = array_keys($this->get_status_options());
$validcohorts = array_keys($this->get_cohort_options($instance, $context));
$validroles = array_keys($this->get_role_options($instance, $context));
$validgroups = array_keys($this->get_group_options($context));
$tovalidate = array(
'name' => PARAM_TEXT,
'status' => $validstatus,
'customint1' => $validcohorts,
'roleid' => $validroles,
'customint2' => $validgroups
);
$typeerrors = $this->validate_param_types($data, $tovalidate);
$errors = array_merge($errors, $typeerrors);
return $errors;
}
}
/**
......@@ -337,7 +543,9 @@ function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
* @return int $groupid Group ID for this cohort.
*/
function enrol_cohort_create_new_group($courseid, $cohortid) {
global $DB;
global $DB, $CFG;
require_once($CFG->dirroot . '/group/lib.php');
$groupname = $DB->get_field('cohort', 'name', array('id' => $cohortid), MUST_EXIST);
$a = new stdClass();
......
......@@ -15,76 +15,80 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Adds new instance of enrol_paypal to specified course
* or edits current instance.
* Adds new instance of an enrolment plugin to specified course or edits current instance.
*
* @package enrol_paypal
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @package core_enrol
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require_once('edit_form.php');
require('../config.php');
require_once('editinstance_form.php');
$courseid = required_param('courseid', PARAM_INT);
$instanceid = optional_param('id', 0, PARAM_INT); // instanceid
$type = required_param('type', PARAM_COMPONENT);
$instanceid = optional_param('id', 0, PARAM_INT);
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id, MUST_EXIST);
$plugin = enrol_get_plugin($type);
if (!$plugin) {
throw new moodle_exception('invaliddata', 'error');
}
require_login($course);
require_capability('enrol/paypal:config', $context);
require_capability('enrol/' . $type . ':config', $context);
$PAGE->set_url('/enrol/paypal/edit.php', array('courseid'=>$course->id, 'id'=>$instanceid));
$PAGE->set_url('/enrol/editinstance.php', array('courseid' => $course->id, 'id' => $instanceid, 'type' => $type));
$PAGE->set_pagelayout('admin');
$return = new moodle_url('/enrol/instances.php', array('id'=>$course->id));
if (!enrol_is_enabled('paypal')) {
$return = new moodle_url('/enrol/instances.php', array('id' => $course->id));
if (!enrol_is_enabled($type)) {
redirect($return);
}
$plugin = enrol_get_plugin('paypal');
if ($instanceid) {
$instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'paypal', 'id'=>$instanceid), '*', MUST_EXIST);
$instance->cost = format_float($instance->cost, 2, true);
$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => $type, '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 = new stdClass();
// No instance yet, we have to add new instance.