MDL-23502 self enrol not part of course edit page any more

This commit is contained in:
Petr Skoda
2010-07-31 13:45:44 +00:00
parent c1adaed0b5
commit c9f9c90009
6 changed files with 273 additions and 294 deletions
-44
View File
@@ -1,44 +0,0 @@
<?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_self to specified course.
*
* @package enrol
* @subpackage self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
$id = required_param('id', PARAM_INT); // course id
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
require_login($course);
require_capability('moodle/course:enrolconfig', $context);
require_sesskey();
$enrol = enrol_get_plugin('self');
if ($enrol->get_candidate_link($course->id)) {
$enrol->add_default_instance($course);
}
redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
+90
View File
@@ -0,0 +1,90 @@
<?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_self to specified course
* or edits current instance.
*
* @package enrol
* @subpackage self
* @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('edit_form.php');
$courseid = required_param('courseid', PARAM_INT);
$instanceid = optional_param('id', 0, PARAM_INT); // instanceid
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
require_login($course);
require_capability('enrol/self:config', $context);
$PAGE->set_url('/enrol/self/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('self')) {
redirect($return);
}
$plugin = enrol_get_plugin('self');
if ($instanceid) {
$instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'id'=>$instanceid), '*', MUST_EXIST);
} else {
require_capability('moodle/course:enrolconfig', $context);
// no instance yet, we have to add new instance
$instance = new object();
$instance->id = null;
$instance->courseid = $course->id;
}
$mform = new enrol_self_edit_form(NULL, array($instance, $plugin, $context));
if ($mform->is_cancelled()) {
redirect($return);
} else if ($data = $mform->get_data()) {
if ($instance->id) {
$instance->status = $data->status;
$instance->name = $data->name;
$instance->password = $data->password;
$instance->customint1 = $data->customint1;
$instance->roleid = $data->roleid;
$instance->enrolperiod = $data->enrolperiod;
$instance->enrolstartdate = $data->enrolstartdate;
$instance->enrolenddate = $data->enrolenddate;
$instance->timemodified = time();
$DB->update_record('enrol', $instance);
} else {
$fields = array('status'=>$data->status, 'name'=>$data->name, 'password'=>$data->password, 'customint1'=>$data->customint1, 'roleid'=>$data->roleid,
'enrolperiod'=>$data->enrolperiod, 'enrolstartdate'=>$data->enrolstartdate, 'enrolenddate'=>$data->enrolenddate);
$plugin->add_instance($course, $fields);
}
redirect($return);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'enrol_self'));
$mform->display();
echo $OUTPUT->footer();
+126
View File
@@ -0,0 +1,126 @@
<?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_self to specified course
* or edits current instance.
*
* @package enrol
* @subpackage self
* @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_self_edit_form extends moodleform {
function definition() {
$mform = $this->_form;
list($instance, $plugin, $context) = $this->_customdata;
$mform->addElement('header', 'header', get_string('pluginname', 'enrol_self'));
$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_self'), $options);
$mform->addHelpButton('status', 'status', 'enrol_self');
$mform->setDefault('status', $plugin->get_config('status'));
$mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'));
$mform->addHelpButton('password', 'password', 'enrol_self');
if (empty($instance->id) and $plugin->get_config('requirepassword')) {
$mform->addRule('password', get_string('required'), 'required', null, 'client');
}
$options = array(1 => get_string('yes'),
0 => get_string('no'));
$mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options);
$mform->addHelpButton('customint1', 'groupkey', 'enrol_self');
$mform->setDefault('customint1', $plugin->get_config('groupkey'));
if ($instance->id) {
$roles = get_default_enrol_roles($context, $instance->roleid);
} else {
$roles = get_default_enrol_roles($context, $plugin->get_config('roleid'));
}
$mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles);
$mform->setDefault('roleid', $plugin->get_config('roleid'));
$mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400));
$mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
$mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true));
$mform->setDefault('enrolstartdate', 0);
$mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), array('optional' => true));
$mform->setDefault('enrolenddate', 0);
$mform->addElement('hidden', 'id');
$mform->addElement('hidden', 'courseid');
$this->add_action_buttons();
$this->set_data($instance);
}
function validation($data, $files) {
global $DB, $CFG;
$errors = parent::validation($data, $files);
list($instance, $plugin, $context) = $this->_customdata;
$checkpassword = false;
if ($instance->id) {
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
if ($instance->password !== $data['password']) {
$checkpassword = true;
}
}
} else {
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
$checkpassword = true;
}
}
if ($checkpassword) {
$require = $plugin->get_config('requirepassword');
$policy = $plugin->get_config('usepasswordpolicy');
if ($require and trim($data['password'])) {
$errors['password'] = get_string('required');
} else if ($policy) {
$errmsg = '';//prevent eclipse warning
if (!check_password_policy($data['password'], $errmsg)) {
$errors['password'] = $errmsg;
}
}
}
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
$errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self');
}
}
return $errors;
}
}
+48 -237
View File
@@ -72,17 +72,62 @@ class enrol_self_plugin extends enrol_plugin {
return ($instance->status == ENROL_INSTANCE_ENABLED);
}
/**
* Returns enrolment instance manage link.
*
* By defaults looks for manage.php file and tests for manage capability.
*
* @param object $instance
* @return moodle_url;
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
if ($instance->enrol !== 'self') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
if (has_capability('enrol/self:config', $context)) {
$managelink = new moodle_url('/enrol/self/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
*/
public function get_action_icons(stdClass $instance) {
global $OUTPUT;
if ($instance->enrol !== 'self') {
throw new coding_exception('invalid enrol instance!');
}
$context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
$icons = array();
if (has_capability('enrol/self:config', $context)) {
$editlink = new moodle_url("/enrol/self/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
$icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
}
return $icons;
}
/**
* 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_candidate_link($courseid) {
if (!has_capability('moodle/course:enrolconfig', get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST))) {
$context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/manual:config', $context)) {
return NULL;
}
// multiple instances supported - different roles with different password
return new moodle_url('/enrol/self/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
return new moodle_url('/enrol/self/edit.php', array('courseid'=>$courseid));
}
/**
@@ -144,238 +189,6 @@ class enrol_self_plugin extends enrol_plugin {
return $OUTPUT->box($output);
}
/**
* Adds enrol instance UI to course edit form
*
* @param object $instance enrol instance or null if does not exist yet
* @param MoodleQuickForm $mform
* @param object $data
* @param object $context context of existing course or parent category if course does not exist
* @return void
*/
public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
$i = isset($instance->id) ? $instance->id : 0;
$plugin = enrol_get_plugin('self');
$header = $plugin->get_instance_name($instance);
$config = has_capability('enrol/self:config', $context);
$mform->addElement('header', 'enrol_self_header_'.$i, $header);
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$mform->addElement('select', 'enrol_self_status_'.$i, get_string('status', 'enrol_self'), $options);
$mform->addHelpButton('enrol_self_status_'.$i, 'status', 'enrol_self');
$mform->setDefault('enrol_self_status_'.$i, $this->get_config('status'));
$mform->setAdvanced('enrol_self_status_'.$i, $this->get_config('status_adv'));
if (!$config) {
$mform->hardFreeze('enrol_self_status_'.$i);
}
$mform->addElement('passwordunmask', 'enrol_self_password_'.$i, get_string('password', 'enrol_self'));
$mform->addHelpButton('enrol_self_password_'.$i, 'password', 'enrol_self');
if (!$config) {
$mform->hardFreeze('enrol_self_password_'.$i);
} else {
$mform->disabledIf('enrol_self_password_'.$i, 'enrol_self_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
$options = array(1 => get_string('yes'),
0 => get_string('no'));
$mform->addElement('select', 'enrol_self_customint1_'.$i, get_string('groupkey', 'enrol_self'), $options);
$mform->addHelpButton('enrol_self_customint1_'.$i, 'groupkey', 'enrol_self');
$mform->setDefault('enrol_self_customint1_'.$i, $this->get_config('groupkey'));
$mform->setAdvanced('enrol_self_customint1_'.$i, $this->get_config('groupkey_adv'));
if (!$config) {
$mform->hardFreeze('enrol_self_customint1_'.$i);
} else {
$mform->disabledIf('enrol_self_customint1_'.$i, 'enrol_self_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
if ($instance) {
$roles = get_default_enrol_roles($context, $instance->roleid);
} else {
$roles = get_default_enrol_roles($context, $this->get_config('roleid'));
}
$mform->addElement('select', 'enrol_self_roleid_'.$i, get_string('role', 'enrol_self'), $roles);
$mform->setDefault('enrol_self_roleid_'.$i, $this->get_config('roleid'));
$mform->setAdvanced('enrol_self_roleid_'.$i, $this->get_config('roleid_adv'));
if (!$config) {
$mform->hardFreeze('enrol_self_roleid_'.$i);
} else {
$mform->disabledIf('enrol_self_roleid_'.$i, 'enrol_self_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
$mform->addElement('duration', 'enrol_self_enrolperiod_'.$i, get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400));
$mform->setDefault('enrol_self_enrolperiod_'.$i, $this->get_config('enrolperiod'));
$mform->setAdvanced('enrol_self_enrolperiod_'.$i, $this->get_config('enrolperiod_adv'));
if (!$config) {
$mform->hardFreeze('enrol_self_enrolperiod_'.$i);
} else {
$mform->disabledIf('enrol_self_enrolperiod_'.$i, 'enrol_self_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
$mform->addElement('date_selector', 'enrol_self_enrolstartdate_'.$i, get_string('enrolstartdate', 'enrol_self'), array('optional' => true));
$mform->setDefault('enrol_self_enrolstartdate_'.$i, 0);
$mform->setAdvanced('enrol_self_enrolstartdate_'.$i, 1);
if (!$config) {
$mform->hardFreeze('enrol_self_enrolstartdate_'.$i);
} else {
$mform->disabledIf('enrol_self_enrolstartdate_'.$i, 'enrol_self_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
$mform->addElement('date_selector', 'enrol_self_enrolenddate_'.$i, get_string('enrolenddate', 'enrol_self'), array('optional' => true));
$mform->setDefault('enrol_self_enrolenddate_'.$i, 0);
$mform->setAdvanced('enrol_self_enrolenddate_'.$i, 1);
if (!$config) {
$mform->hardFreeze('enrol_self_enrolenddate_'.$i);
} else {
$mform->disabledIf('enrol_self_enrolenddate_'.$i, 'enrol_self_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
// now add all values from enrol table
if ($instance) {
foreach($instance as $key=>$val) {
$data->{'enrol_self_'.$key.'_'.$i} = $val;
}
}
}
/**
* Validates course edit form data
*
* @param object $instance enrol instance or null if does not exist yet
* @param array $data
* @param object $context context of existing course or parent category if course does not exist
* @return array errors array
*/
public function course_edit_validation($instance, array $data, $context) {
$errors = array();
if (!has_capability('enrol/self:config', $context)) {
// we are going to ignore the data later anyway, they would not be able to fix the form anyway
return $errors;
}
$i = isset($instance->id) ? $instance->id : 0;
$password = empty($data['enrol_self_password_'.$i]) ? '' : $data['enrol_self_password_'.$i];
$checkpassword = false;
if ($instance) {
if ($data['enrol_self_status_'.$i] == ENROL_INSTANCE_ENABLED) {
if ($instance->password !== $password) {
$checkpassword = true;
}
}
} else {
if ($data['enrol_self_status_'.$i] == ENROL_INSTANCE_ENABLED) {
$checkpassword = true;
}
}
if ($checkpassword) {
$require = $this->get_config('requirepassword');
$policy = $this->get_config('usepasswordpolicy');
if ($require and empty($password)) {
$errors['enrol_self_password_'.$i] = get_string('required');
} else if ($policy) {
$errmsg = '';//prevent eclipse warning
if (!check_password_policy($password, $errmsg)) {
$errors['enrol_self_password_'.$i] = $errmsg;
}
}
}
if ($data['enrol_self_status_'.$i] == ENROL_INSTANCE_ENABLED) {
if (!empty($data['enrol_self_enrolenddate_'.$i]) and $data['enrol_self_enrolenddate_'.$i] < $data['enrol_self_enrolstartdate_'.$i]) {
$errors['enrol_self_enrolenddate_'.$i] = get_string('enrolenddaterror', 'enrol_self');
}
}
return $errors;
}
/**
* Called after updating/inserting course.
*
* @param bool $inserted true if course just inserted
* @param object $course
* @param object $data form data
* @return void
*/
public function course_updated($inserted, $course, $data) {
global $DB;
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('enrol/self:config', $context)) {
if ($inserted) {
if (isset($data->enrol_self_status_0)) {
$fields = array('status'=>$data->enrol_self_status_0);
if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
$fields['password'] = $data->enrol_self_password_0;
$fields['customint1'] = $data->enrol_self_customint1_0;
$fields['roleid'] = $data->enrol_self_roleid_0;
$fields['enrolperiod'] = $data->enrol_self_enrolperiod_0;
$fields['enrolstartdate'] = $data->enrol_self_enrolstartdate_0;
$fields['enrolenddate'] = $data->enrol_self_enrolenddate_0;
} else {
if ($this->get_config('requirepassword')) {
// make sure some password is set after enabling this plugin
$fields['password'] = generate_password(20);
}
$fields['customint1'] = $this->get_config('groupkey');
$fields['roleid'] = $this->get_config('roleid');
$fields['enrolperiod'] = $this->get_config('enrolperiod');
$fields['enrolstartdate'] = 0;
$fields['enrolenddate'] = 0;
}
$this->add_instance($course, $fields);
}
} else {
$instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'self'));
foreach ($instances as $instance) {
$i = $instance->id;
if (isset($data->{'enrol_self_status_'.$i})) {
$instance->status = $data->{'enrol_self_status_'.$i};
$instance->timemodified = time();
if ($instance->status == ENROL_INSTANCE_ENABLED) {
$instance->password = $data->{'enrol_self_password_'.$i};
$instance->customint1 = $data->{'enrol_self_customint1_'.$i};
$instance->roleid = $data->{'enrol_self_roleid_'.$i};
$instance->enrolperiod = $data->{'enrol_self_enrolperiod_'.$i};
$instance->enrolstartdate = $data->{'enrol_self_enrolstartdate_'.$i};
$instance->enrolenddate = $data->{'enrol_self_enrolenddate_'.$i};
}
$DB->update_record('enrol', $instance);
}
}
}
} else {
if ($inserted) {
if ($this->get_config('defaultenrol')) {
$this->add_default_instance($course);
}
} else {
// bad luck, user can not change anything
}
}
}
/**
* Add new instance of enrol plugin with default settings.
* @param object $course
@@ -384,11 +197,9 @@ class enrol_self_plugin extends enrol_plugin {
public function add_default_instance($course) {
global $DB;
$exists = $DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'self'));
$fields = array('customint1'=>$this->get_config('groupkey'), 'enrolperiod'=>$this->get_config('enrolperiod', 0), 'roleid'=>$this->get_config('roleid', 0));
$fields['status'] = $exists ? ENROL_INSTANCE_DISABLED : $this->get_config('status');
$fields['status'] = $this->get_config('status');
if ($this->get_config('requirepassword')) {
$fields['password'] = generate_password(20);
+8 -13
View File
@@ -53,28 +53,23 @@ if ($ADMIN->fulltree) {
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$settings->add(new admin_setting_configselect_with_advanced('enrol_self/status',
get_string('status', 'enrol_self'), get_string('status_desc', 'enrol_self'),
array('value'=>ENROL_INSTANCE_DISABLED, 'adv'=>false), $options));
$settings->add(new admin_setting_configselect('enrol_self/status',
get_string('status', 'enrol_self'), get_string('status_desc', 'enrol_self'), ENROL_INSTANCE_DISABLED, $options));
$options = array(1 => get_string('yes'),
0 => get_string('no'));
$settings->add(new admin_setting_configselect_with_advanced('enrol_self/groupkey',
get_string('groupkey', 'enrol_self'), get_string('groupkey_desc', 'enrol_self'),
array('value'=>0, 'adv'=>true), $options));
$settings->add(new admin_setting_configselect('enrol_self/groupkey',
get_string('groupkey', 'enrol_self'), get_string('groupkey_desc', 'enrol_self'), 0, $options));
if (!during_initial_install()) {
$options = get_default_enrol_roles(get_context_instance(CONTEXT_SYSTEM));
$student = get_archetype_roles('student');
$student = reset($student);
$settings->add(new admin_setting_configselect_with_advanced('enrol_self/roleid',
get_string('defaultrole', 'enrol_self'), get_string('defaultrole_desc', 'enrol_self'),
array('value'=>$student->id, 'adv'=>false), $options));
$settings->add(new admin_setting_configselect('enrol_self/roleid',
get_string('defaultrole', 'enrol_self'), get_string('defaultrole_desc', 'enrol_self'), $student->id, $options));
}
$settings->add(new admin_setting_configtext_with_advanced('enrol_self/enrolperiod',
get_string('enrolperiod', 'enrol_self'), get_string('enrolperiod_desc', 'enrol_self'),
array('value'=>0, 'adv'=>true), PARAM_INT));
$settings->add(new admin_setting_configtext('enrol_self/enrolperiod',
get_string('enrolperiod', 'enrol_self'), get_string('enrolperiod_desc', 'enrol_self'), 0, PARAM_INT));
}
+1
View File
@@ -31,6 +31,7 @@ $string['ajaxxusersfound'] = '[users] users found';
$string['ajaxnext25'] = 'Next 25...';
$string['assignnotpermitted'] = 'You do not have permission or can not assign roles in this course.';
$string['configenrolplugins'] = 'Please select all required plugins and arrange then in appropriate order.';
$string['custominstancename'] = 'Custom instance name';
$string['defaultenrol'] = 'Add instance to new courses';
$string['defaultenrol_desc'] = 'It is possible to add this plugin to all new courses by default.';
$string['deleteinstanceconfirm'] = 'Do you really want to delete enrol plugin instance "{$a->name}" with {$a->users} enrolled users?';