Merge branch 'MDL-30674-master-wip' of https://github.com/andrewnicols/moodle
This commit is contained in:
@@ -1,42 +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_guest to specified course.
|
||||
*
|
||||
* @package enrol_guest
|
||||
* @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 = context_course::instance($course->id, MUST_EXIST);
|
||||
|
||||
require_login($course);
|
||||
require_capability('moodle/course:enrolconfig', $context);
|
||||
require_sesskey();
|
||||
|
||||
$enrol = enrol_get_plugin('guest');
|
||||
|
||||
if ($enrol->get_newinstance_link($course->id)) {
|
||||
$enrol->add_default_instance($course);
|
||||
}
|
||||
|
||||
redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
|
||||
@@ -0,0 +1,110 @@
|
||||
<?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 <[email protected]>
|
||||
* @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 <[email protected]>
|
||||
* @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 ($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']) {
|
||||
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
|
||||
if ($instance->password !== $data['password']) {
|
||||
$checkpassword = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
|
||||
$checkpassword = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($checkpassword) {
|
||||
$policy = $plugin->get_config('usepasswordpolicy');
|
||||
if ($policy) {
|
||||
$errmsg = '';
|
||||
if (!check_password_policy($data['password'], $errmsg)) {
|
||||
$errors['password'] = $errmsg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
@@ -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 <[email protected]>
|
||||
* @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();
|
||||
+101
-124
@@ -27,6 +27,12 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class enrol_guest_plugin
|
||||
*
|
||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class enrol_guest_plugin extends enrol_plugin {
|
||||
|
||||
/**
|
||||
@@ -51,16 +57,78 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enrol a user using a given enrolment instance.
|
||||
*
|
||||
* @param stdClass $instance
|
||||
* @param int $userid
|
||||
* @param null $roleid
|
||||
* @param int $timestart
|
||||
* @param int $timeend
|
||||
* @param null $status
|
||||
* @param null $recovergrades
|
||||
*/
|
||||
public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null) {
|
||||
// no real enrolments here!
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enrol a user from a given enrolment instance.
|
||||
*
|
||||
* @param stdClass $instance
|
||||
* @param int $userid
|
||||
*/
|
||||
public function unenrol_user(stdClass $instance, $userid) {
|
||||
// nothing to do, we never enrol here!
|
||||
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.
|
||||
@@ -110,7 +178,7 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return new moodle_url('/enrol/guest/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
|
||||
return new moodle_url('/enrol/guest/edit.php', array('courseid' => $courseid));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,128 +233,6 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
return $OUTPUT->box($output, 'generalbox');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
if (!$i and !$this->get_config('defaultenrol')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$header = $this->get_instance_name($instance);
|
||||
if (!$i) {
|
||||
$config = guess_if_creator_will_have_course_capability('enrol/guest:config', $context);
|
||||
} else {
|
||||
$config = has_capability('enrol/guest:config', $context);
|
||||
}
|
||||
|
||||
$mform->addElement('header', 'enrol_guest_header_'.$i, $header);
|
||||
|
||||
|
||||
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
|
||||
ENROL_INSTANCE_DISABLED => get_string('no'));
|
||||
$mform->addElement('select', 'enrol_guest_status_'.$i, get_string('status', 'enrol_guest'), $options);
|
||||
$mform->addHelpButton('enrol_guest_status_'.$i, 'status', 'enrol_guest');
|
||||
$mform->setDefault('enrol_guest_status_'.$i, $this->get_config('status'));
|
||||
$mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv'));
|
||||
if (!$config) {
|
||||
$mform->hardFreeze('enrol_guest_status_'.$i);
|
||||
if (!$i) {
|
||||
$mform->setConstant('enrol_guest_status_'.$i, $this->get_config('status'));
|
||||
} else {
|
||||
$mform->setConstant('enrol_guest_status_'.$i, $instance->status);
|
||||
}
|
||||
}
|
||||
|
||||
$mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest'));
|
||||
$mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest');
|
||||
if (!$config) {
|
||||
$mform->hardFreeze('enrol_guest_password_'.$i);
|
||||
if (!$i) {
|
||||
if ($this->get_config('requirepassword')) {
|
||||
$password = generate_password(20);
|
||||
} else {
|
||||
$password = '';
|
||||
}
|
||||
$mform->setConstant('enrol_guest_password_'.$i, $password);
|
||||
} else {
|
||||
$mform->setConstant('enrol_guest_password_'.$i, $instance->password);
|
||||
}
|
||||
} else {
|
||||
$mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
|
||||
}
|
||||
|
||||
|
||||
// now add all values from enrol table
|
||||
if ($instance) {
|
||||
foreach($instance as $key=>$val) {
|
||||
$data->{'enrol_guest_'.$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/guest:config', $context)) {
|
||||
// we are going to ignore the data later anyway, they would nto be able to fix the form anyway
|
||||
return $errors;
|
||||
}
|
||||
|
||||
$i = isset($instance->id) ? $instance->id : 0;
|
||||
|
||||
if (!isset($data['enrol_guest_status_'.$i])) {
|
||||
return $errors;
|
||||
}
|
||||
|
||||
$password = empty($data['enrol_guest_password_'.$i]) ? '' : $data['enrol_guest_password_'.$i];
|
||||
$checkpassword = false;
|
||||
|
||||
if ($instance) {
|
||||
if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
|
||||
if ($instance->password !== $password) {
|
||||
$checkpassword = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($data['enrol_guest_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_guest_password_'.$i] = get_string('required');
|
||||
} else if ($policy) {
|
||||
$errmsg = '';//prevent eclipse warning
|
||||
if (!check_password_policy($password, $errmsg)) {
|
||||
$errors['enrol_guest_password_'.$i] = $errmsg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after updating/inserting course.
|
||||
*
|
||||
@@ -412,6 +358,37 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||
*/
|
||||
public function can_hide_show_instance($instance) {
|
||||
$context = context_course::instance($instance->courseid);
|
||||
return has_capability('enrol/guest:config', $context);
|
||||
if (!has_capability('enrol/guest:config', $context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the instance is currently disabled, before it can be enabled, we must check whether the password meets the
|
||||
// password policies.
|
||||
if ($instance->status == ENROL_INSTANCE_DISABLED) {
|
||||
if ($this->get_config('requirepassword')) {
|
||||
if (empty($instance->password)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->get_config('usepasswordpolicy')) {
|
||||
if (!check_password_policy($instance->password, $errmsg)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default settings for enrol_guest.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_instance_defaults() {
|
||||
$fields = array();
|
||||
$fields['status'] = $this->get_config('status');
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,13 @@ Feature: Guest users can auto-enrol themself in courses where guest access is al
|
||||
And I add a "Forum" to section "1" and I fill the form with:
|
||||
| Forum name | Test forum name |
|
||||
| Description | Test forum description |
|
||||
And I click on "Edit settings" "link" in the "Administration" "block"
|
||||
|
||||
Scenario: Allow guest access without password
|
||||
Given I set the following fields to these values:
|
||||
Given I navigate to "Enrolment methods" node in "Course administration > Users"
|
||||
And I click on "Edit" "link" in the "Guest access" "table_row"
|
||||
And I set the following fields to these values:
|
||||
| Allow guest access | Yes |
|
||||
And I press "Save and display"
|
||||
And I press "Save changes"
|
||||
And I log out
|
||||
And I log in as "student1"
|
||||
And I am on site homepage
|
||||
@@ -35,10 +36,12 @@ Feature: Guest users can auto-enrol themself in courses where guest access is al
|
||||
Then I should not see "Subscribe to this forum"
|
||||
|
||||
Scenario: Allow guest access with password
|
||||
Given I set the following fields to these values:
|
||||
Given I navigate to "Enrolment methods" node in "Course administration > Users"
|
||||
And I click on "Edit" "link" in the "Guest access" "table_row"
|
||||
And I set the following fields to these values:
|
||||
| Allow guest access | Yes |
|
||||
| Password | moodle_rules |
|
||||
And I press "Save and display"
|
||||
And I press "Save changes"
|
||||
And I log out
|
||||
And I log in as "student1"
|
||||
And I am on site homepage
|
||||
@@ -47,4 +50,4 @@ Feature: Guest users can auto-enrol themself in courses where guest access is al
|
||||
And I set the following fields to these values:
|
||||
| Password | moodle_rules |
|
||||
And I press "Submit"
|
||||
And I should see "Test forum name"
|
||||
And I should see "Test forum name"
|
||||
Reference in New Issue
Block a user