Skip to content

Commit

Permalink
MDL-30674 enrol_guest: Moved settings from course to enrol form
Browse files Browse the repository at this point in the history
Added proper enrol settings form with status (enabled or disabled)
and password option in line with other core enrol plugins.
Removed enrol_guest settings from course settings page.
When adding a guest enrol plugin you now get presented with the
edit form as with other core enrol plugins.
  • Loading branch information
andrewhancox committed Oct 22, 2015
1 parent 35d3e8b commit 0c93dda
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 171 deletions.
42 changes: 0 additions & 42 deletions enrol/guest/addinstance.php

This file was deleted.

109 changes: 109 additions & 0 deletions enrol/guest/classes/enrol_guest_edit_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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');

$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']) {
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 && empty($data['password'])) {
$errors['password'] = get_string('required');
} else if ($policy) {
$errmsg = '';
if (!check_password_policy($data['password'], $errmsg)) {
$errors['password'] = $errmsg;
}
}
}

return $errors;
}
}
99 changes: 99 additions & 0 deletions enrol/guest/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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();
Loading

0 comments on commit 0c93dda

Please sign in to comment.