MDL-23502 manual enrol plugin is not part of course edit screen any more

This commit is contained in:
Petr Skoda 2010-07-31 12:13:38 +00:00
parent 9eb7b1cb81
commit eafb7a72dc
13 changed files with 225 additions and 210 deletions

View File

@ -1,43 +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_manual to specified course.
*
* @package enrol_manual
* @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('manual');
if ($enrol->get_candidate_link($course->id)) {
$enrol->add_default_instance($course);
}
redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));

View File

@ -18,11 +18,13 @@
/**
* Capabilities for manual enrolment plugin.
*
* @package enrol_manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @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;
$capabilities = array(

View File

@ -18,11 +18,14 @@
/**
* Manual enrol plugin installation script
*
* @package enrol_manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @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;
function xmldb_enrol_manual_install() {
global $CFG;

89
enrol/manual/edit.php Normal file
View File

@ -0,0 +1,89 @@
<?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_manual to specified course
* or edits current instance.
*
* @package enrol
* @subpackage manual
* @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);
$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/manual:config', $context);
$PAGE->set_url('/enrol/manual/edit.php', array('courseid'=>$course->id));
$PAGE->set_pagelayout('admin');
$return = new moodle_url('/enrol/instances.php', array('id'=>$course->id));
if (!enrol_is_enabled('manual')) {
redirect($return);
}
$plugin = enrol_get_plugin('manual');
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'), 'id ASC')) {
$instance = array_shift($instances);
if ($instances) {
// oh - we allow only one instance per course!!
foreach ($instances as $del) {
$plugin->delete_instance($del);
}
}
} 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_manual_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->enrolperiod = $data->enrolperiod;
$instance->roleid = $data->roleid;
$instance->timemodified = time();
$DB->update_record('enrol', $instance);
} else {
$fields = array('status'=>$data->status, 'enrolperiod'=>$data->enrolperiod, 'roleid'=>$data->roleid);
$plugin->add_instance($course, $fields);
}
redirect($return);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'enrol_manual'));
$mform->display();
echo $OUTPUT->footer();

View File

@ -0,0 +1,66 @@
<?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_manual to specified course
* or edits current instance.
*
* @package enrol
* @subpackage manual
* @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_manual_edit_form extends moodleform {
function definition() {
$mform = $this->_form;
list($instance, $plugin, $context) = $this->_customdata;
$mform->addElement('header', 'header', get_string('pluginname', 'enrol_manual'));
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$mform->addElement('select', 'status', get_string('status', 'enrol_manual'), $options);
$mform->addHelpButton('status', 'status', 'enrol_manual');
$mform->setDefault('status', $plugin->get_config('status'));
$mform->addElement('duration', 'enrolperiod', get_string('defaultperiod', 'enrol_manual'), array('optional' => true, 'defaultunit' => 86400));
$mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
$mform->disabledIf('enrolperiod', 'status', 'noteq', ENROL_INSTANCE_ENABLED);
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('defaultrole', 'role'), $roles);
$mform->setDefault('roleid', $plugin->get_config('roleid'));
$mform->disabledIf('roleid', 'status', 'noteq', ENROL_INSTANCE_ENABLED);
$mform->addElement('hidden', 'courseid');
$this->add_action_buttons();
$this->set_data($instance);
}
}

View File

@ -18,9 +18,10 @@
/**
* Strings for component 'enrol_manual', language 'en', branch 'MOODLE_20_STABLE'
*
* @package enrol_manual
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['assignrole'] = 'Assignrole';

View File

@ -18,9 +18,10 @@
/**
* Manual enrolment plugin main library file.
*
* @package enrol_manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @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;
@ -82,19 +83,15 @@ class enrol_manual_plugin extends enrol_plugin {
* @return moodle_url;
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
$name = $this->get_name();
if ($instance->enrol !== $name) {
if ($instance->enrol !== 'manual') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
if (!has_capability('enrol/manual:manage', $context)) {
return;
if (has_capability('enrol/manual:config', $context)) {
$managelink = new moodle_url('/enrol/manual/edit.php', array('courseid'=>$instance->courseid));
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
$managelink = new moodle_url("/enrol/$name/manage.php", array('enrolid'=>$instance->id));
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
/**
@ -105,19 +102,23 @@ class enrol_manual_plugin extends enrol_plugin {
public function get_action_icons(stdClass $instance) {
global $OUTPUT;
$name = $this->get_name();
if ($instance->enrol !== $name) {
if ($instance->enrol !== 'manual') {
throw new coding_exception('invalid enrol instance!');
}
$context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
if (!has_capability('enrol/manual:manage', $context)) {
return;
$icons = array();
if (has_capability('enrol/manual:manage', $context)) {
$managelink = new moodle_url("/enrol/manual/manage.php", array('enrolid'=>$instance->id));
$icons[] = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('enrolusers', 'enrol_manual'), 'core', array('class'=>'iconsmall')));
}
if (has_capability('enrol/manual:config', $context)) {
$editlink = new moodle_url("/enrol/manual/edit.php", array('courseid'=>$instance->courseid));
$icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
}
$managelink = new moodle_url("/enrol/$name/manage.php", array('enrolid'=>$instance->id));
$icon = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('enrolusers', 'enrol_manual'), 'core', array('class'=>'iconsmall')));
return array($icon);
return $icons;
}
/**
@ -128,7 +129,9 @@ class enrol_manual_plugin extends enrol_plugin {
public function get_candidate_link($courseid) {
global $DB;
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;
}
@ -136,122 +139,7 @@ class enrol_manual_plugin extends enrol_plugin {
return NULL;
}
return new moodle_url('/enrol/manual/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
}
/**
* 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('manual');
$header = $plugin->get_instance_name($instance);
$config = has_capability('enrol/manual:config', $context);
$mform->addElement('header', 'enrol_manual_header_'.$i, $header);
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$mform->addElement('select', 'enrol_manual_status_'.$i, get_string('status', 'enrol_manual'), $options);
$mform->addHelpButton('enrol_manual_status_'.$i, 'status', 'enrol_manual');
$mform->setDefault('enrol_manual_status_'.$i, $this->get_config('status'));
$mform->setAdvanced('enrol_manual_status_'.$i, $this->get_config('status_adv'));
if (!$config) {
$mform->hardFreeze('enrol_manual_status_'.$i);
}
$mform->addElement('duration', 'enrol_manual_enrolperiod_'.$i, get_string('defaultperiod', 'enrol_manual'), array('optional' => true, 'defaultunit' => 86400));
$mform->setDefault('enrol_manual_enrolperiod_'.$i, $this->get_config('enrolperiod'));
$mform->setAdvanced('enrol_manual_enrolperiod_'.$i, $this->get_config('enrolperiod_adv'));
if (!$config) {
$mform->hardFreeze('enrol_manual_enrolperiod_'.$i);
} else {
$mform->disabledIf('enrol_manual_enrolperiod_'.$i, 'enrol_manual_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_manual_roleid_'.$i, get_string('defaultrole', 'role'), $roles);
$mform->setDefault('enrol_manual_roleid_'.$i, $this->get_config('roleid'));
$mform->setAdvanced('enrol_manual_roleid_'.$i, $this->get_config('roleid_adv'));
if (!$config) {
$mform->hardFreeze('enrol_manual_roleid_'.$i);
} else {
$mform->disabledIf('enrol_manual_roleid_'.$i, 'enrol_manual_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
}
// now add all values from enrol table
if ($instance) {
foreach($instance as $key=>$val) {
$data->{'enrol_manual_'.$key.'_'.$i} = $val;
}
}
}
/**
* 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/manual:config', $context)) {
if ($inserted) {
if (isset($data->enrol_manual_status_0)) {
$fields = array('status'=>$data->enrol_manual_status_0);
if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
$fields['enrolperiod'] = $data->enrol_manual_enrolperiod_0;
$fields['roleid'] = $data->enrol_manual_roleid_0;
} else {
$fields['enrolperiod'] = $this->get_config('enrolperiod');
$fields['roleid'] = $this->get_config('roleid');
}
$this->add_instance($course, $fields);
}
} else {
$instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'));
foreach ($instances as $instance) {
$i = $instance->id;
if (isset($data->{'enrol_manual_status_'.$i})) {
$instance->status = $data->{'enrol_manual_status_'.$i};
$instance->timemodified = time();
if ($instance->status == ENROL_INSTANCE_ENABLED) {
$instance->enrolperiod = $data->{'enrol_manual_enrolperiod_'.$i};
$instance->roleid = $data->{'enrol_manual_roleid_'.$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
}
}
return new moodle_url('/enrol/manual/edit.php', array('courseid'=>$courseid));
}
/**

View File

@ -17,7 +17,8 @@
/**
* Auxiliary manual user enrolment lib, the main purpose is to lower memory requirements...
*
* @package enrol_manual
* @package enrol
* @subpackage manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

View File

@ -17,7 +17,8 @@
/**
* Manual user enrolment UI.
*
* @package enrol_manual
* @package enrol
* @subpackage manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -59,6 +60,7 @@ $instancename = $enrol_manual->get_instance_name($instance);
$PAGE->set_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id));
$PAGE->set_pagelayout('admin');
$PAGE->set_title($enrol_manual->get_instance_name($instance));
navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
// Create the user selector objects.
$options = array('enrolid' => $enrolid);

View File

@ -18,9 +18,10 @@
/**
* Manual enrolment plugin settings and presets.
*
* @package enrol_manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @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;
@ -40,21 +41,18 @@ 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_manual/status',
get_string('status', 'enrol_manual'), get_string('status_desc', 'enrol_manual'),
array('value'=>ENROL_INSTANCE_ENABLED, 'adv'=>false), $options));
$settings->add(new admin_setting_configselect('enrol_manual/status',
get_string('status', 'enrol_manual'), get_string('status_desc', 'enrol_manual'), ENROL_INSTANCE_ENABLED, $options));
$settings->add(new admin_setting_configtext_with_advanced('enrol_manual/enrolperiod',
get_string('defaultperiod', 'enrol_manual'), get_string('defaultperiod_desc', 'enrol_manual'),
array('value'=>0, 'adv'=>true), PARAM_INT));
$settings->add(new admin_setting_configtext('enrol_manual/enrolperiod',
get_string('defaultperiod', 'enrol_manual'), get_string('defaultperiod_desc', 'enrol_manual'), 0, PARAM_INT));
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_manual/roleid',
get_string('defaultrole', 'role'), '',
array('value'=>$student->id, 'adv'=>true), $options));
$settings->add(new admin_setting_configselect('enrol_manual/roleid',
get_string('defaultrole', 'role'), '', $student->id, $options));
}
}

View File

@ -18,9 +18,10 @@
/**
* Manual enrolment plugin - support for user self unenrolment.
*
* @package enrol_manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');

View File

@ -18,9 +18,12 @@
/**
* Manual enrolment plugin version specification.
*
* @package enrol_manual
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package enrol
* @subpackage manual
* @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;
$plugin->version = 2010071200;

View File

@ -1158,11 +1158,15 @@ abstract class enrol_plugin {
* @return void
*/
public function course_updated($inserted, $course, $data) {
// override if settings on course edit page or some automatic sync needed
if ($inserted) {
if ($this->get_config('defaultenrol')) {
$this->add_default_instance($course);
}
}
}
/**
* Add new instance of enrol plugin settings.
* Add new instance of enrol plugin.
* @param object $course
* @param array instance fields
* @return int id of new instance