first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+124
View File
@@ -0,0 +1,124 @@
<?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 enrolment method external API
*
* @package enrol_guest
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/enrollib.php');
/**
* Guest enrolment method external API
*
* @package enrol_guest
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
class enrol_guest_external extends external_api {
/**
* Returns description of get_instance_info() parameters.
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_instance_info_parameters() {
return new external_function_parameters(
array('instanceid' => new external_value(PARAM_INT, 'Instance id of guest enrolment plugin.'))
);
}
/**
* Return guest enrolment instance information.
*
* @param int $instanceid instance id of guest enrolment plugin.
* @return array warnings and instance information.
* @since Moodle 3.1
*/
public static function get_instance_info($instanceid) {
global $DB;
$params = self::validate_parameters(self::get_instance_info_parameters(), array('instanceid' => $instanceid));
$warnings = array();
// Retrieve guest enrolment plugin.
$enrolplugin = enrol_get_plugin('guest');
if (empty($enrolplugin)) {
throw new moodle_exception('invaliddata', 'error');
}
self::validate_context(context_system::instance());
$enrolinstance = $DB->get_record('enrol', array('id' => $params['instanceid']), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $enrolinstance->courseid), '*', MUST_EXIST);
if (!core_course_category::can_view_course_info($course) && !can_access_course($course)) {
throw new moodle_exception('coursehidden');
}
$instanceinfo = $enrolplugin->get_enrol_info($enrolinstance);
// Specific instance information.
$instanceinfo->passwordrequired = $instanceinfo->requiredparam->passwordrequired;
unset($instanceinfo->requiredparam);
$result = array();
$result['instanceinfo'] = $instanceinfo;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of get_instance_info() result value.
*
* @return external_description
* @since Moodle 3.1
*/
public static function get_instance_info_returns() {
return new external_single_structure(
array(
'instanceinfo' => new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Id of course enrolment instance'),
'courseid' => new external_value(PARAM_INT, 'Id of course'),
'type' => new external_value(PARAM_PLUGIN, 'Type of enrolment plugin'),
'name' => new external_value(PARAM_RAW, 'Name of enrolment plugin'),
'status' => new external_value(PARAM_BOOL, 'Is the enrolment enabled?'),
'passwordrequired' => new external_value(PARAM_BOOL, 'Is a password required?'),
)
),
'warnings' => new external_warnings()
)
);
}
}
+127
View File
@@ -0,0 +1,127 @@
<?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/>.
namespace enrol_guest\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use context_system;
use moodle_exception;
use core_text;
use stdClass;
/**
* This is the external method validating a guest password.
*
* @package enrol_guest
* @since Moodle 4.3
* @copyright 2023 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class validate_password extends external_api {
/**
* Webservice parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'instanceid' => new external_value(PARAM_INT, 'instance id of guest enrolment plugin'),
'password' => new external_value(PARAM_RAW, 'the course password'),
]
);
}
/**
* Perform password validation.
*
* If password is correct: keep it as user preference.
* If password is not correct: remove existing user preference (if any)
*
* @throws moodle_exception
* @param int $instanceid instance id of guest enrolment plugin
* @param string $password the course password
* @return stdClass validation result info
*/
public static function execute(int $instanceid, string $password): stdClass {
global $CFG, $DB;
require_once($CFG->libdir . '/enrollib.php');
$params = external_api::validate_parameters(self::execute_parameters(), [
'instanceid' => $instanceid,
'password' => $password,
]);
$warnings = [];
$validated = false;
$hint = '';
// Retrieve guest enrolment plugin.
$enrolplugin = enrol_get_plugin('guest');
if (empty($enrolplugin)) {
throw new moodle_exception('invaliddata', 'error');
}
self::validate_context(context_system::instance());
$enrolinstance = $DB->get_record('enrol',
['id' => $params['instanceid'], 'status' => ENROL_INSTANCE_ENABLED], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $enrolinstance->courseid], '*', MUST_EXIST);
if (!\core_course_category::can_view_course_info($course) && !can_access_course($course)) {
throw new moodle_exception('coursehidden');
}
if ($enrolinstance->password) {
if ($params['password'] === $enrolinstance->password) {
$validated = true;
set_user_preference('enrol_guest_ws_password_' . $enrolinstance->id, $params['password']);
} else {
// Always unset in case there was something stored.
unset_user_preference('enrol_guest_ws_password_' . $enrolinstance->id);
if ($enrolplugin->get_config('showhint')) {
$hint = core_text::substr($enrolinstance->password, 0, 1);
$hint = get_string('passwordinvalidhint', 'enrol_guest', $hint);
}
}
}
$result = (object)[
'validated' => $validated,
'hint' => $hint,
'warnings' => $warnings,
];
return $result;
}
/**
* Describes the return information.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'validated' => new external_value(PARAM_BOOL, 'Whether the password was successfully validated'),
'hint' => new external_value(PARAM_RAW, 'Password hint (if enabled)', VALUE_OPTIONAL),
'warnings' => new external_warnings(),
]);
}
}
+41
View File
@@ -0,0 +1,41 @@
<?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/>.
/**
* Privacy Subsystem implementation for enrol_guest.
*
* @package enrol_guest
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_guest\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for enrol_guest implementing null_provider.
*
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+40
View File
@@ -0,0 +1,40 @@
<?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/>.
/**
* Capabilities for guest access plugin.
*
* @package enrol_guest
* @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(
'enrol/guest:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),
);
+43
View File
@@ -0,0 +1,43 @@
<?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 enrolment external functions and service definitions.
*
* @package enrol_guest
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
$functions = array(
'enrol_guest_get_instance_info' => array(
'classname' => 'enrol_guest_external',
'methodname' => 'get_instance_info',
'description' => 'Return guest enrolment instance information.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'enrol_guest_validate_password' => [
'classname' => 'enrol_guest\external\validate_password',
'description' => 'Perform password validation.',
'type' => 'write',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
);
+39
View File
@@ -0,0 +1,39 @@
<?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/>.
/**
* This file keeps track of upgrades to the guest enrolment plugin.
*
* @package enrol_guest
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function xmldb_enrol_guest_upgrade($oldversion) {
// Automatically generated Moodle v4.1.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.2.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.3.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.4.0 release upgrade line.
// Put any upgrade step following this.
return true;
}
+46
View File
@@ -0,0 +1,46 @@
<?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/>.
/**
* Strings for component 'enrol_guest', language 'en'.
*
* @package enrol_guest
* @copyright 2010 onwards Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['allowguests'] = 'This course allows guest users to enter';
$string['guest:config'] = 'Configure guest access instances';
$string['guestaccess_withpassword'] = 'Guest access requires password';
$string['guestaccess_withoutpassword'] = 'Guest access';
$string['password'] = 'Password';
$string['password_help'] = 'A password allows guest access to the course to be restricted to only those who know the password. Guests will be required to supply the password each time they access the course.';
$string['passwordinvalid'] = 'Incorrect access password, please try again';
$string['passwordinvalidhint'] = 'That access password was incorrect, please try again<br />
(Here\'s a hint - it starts with \'{$a}\')';
$string['pluginname'] = 'Guest access';
$string['pluginname_desc'] = 'Guest access plugin is only granting temporary access to courses, it is not actually enrolling users.';
$string['requirepassword'] = 'Require guest access password';
$string['requirepassword_desc'] = 'Require access password in new courses and prevent removing of access password from existing courses.';
$string['showhint'] = 'Show hint';
$string['showhint_desc'] = 'Show first letter of the guest access password.';
$string['status'] = 'Allow guest access';
$string['status_desc'] = 'Allow temporary guest access by default.';
$string['status_help'] = 'This setting determines whether a user can access the course as a guest, without being required to enrol.';
$string['status_link'] = 'enrol/guest';
$string['usepasswordpolicy'] = 'Use password policy';
$string['usepasswordpolicy_desc'] = 'Use standard password policy for guest access passwords.';
$string['privacy:metadata'] = 'The Guest access enrolment plugin does not store any personal data.';
+579
View File
@@ -0,0 +1,579 @@
<?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.
*
* This plugin does not add any entries into the user_enrolments table,
* the access control is granted on the fly via the tricks in require_login().
*
* @package enrol_guest
* @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();
/**
* 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 {
/**
* Returns optional enrolment information icons.
*
* This is used in course list for quick overview of enrolment options.
*
* We are not using single instance parameter because sometimes
* we might want to prevent icon repetition when multiple instances
* of one type exist. One instance may also produce several icons.
*
* @param array $instances all enrol instances of this type in one course
* @return array of pix_icon
*/
public function get_info_icons(array $instances) {
foreach ($instances as $instance) {
if ($instance->password !== '') {
return array(new pix_icon('withpassword', get_string('guestaccess_withpassword', 'enrol_guest'), 'enrol_guest'));
} else {
return array(new pix_icon('withoutpassword', get_string('guestaccess_withoutpassword', 'enrol_guest'), 'enrol_guest'));
}
}
}
/**
* 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;
}
/**
* Attempt to automatically gain temporary guest access to course,
* calling code has to make sure the plugin and instance are active.
*
* @param stdClass $instance course enrol instance
* @return bool|int false means no guest access, integer means end of cached time
*/
public function try_guestaccess(stdClass $instance) {
global $USER, $CFG;
$allow = false;
if ($instance->password === '') {
$allow = true;
} else if (isset($USER->enrol_guest_passwords[$instance->id])) { // this is a hack, ideally we should not add stuff to $USER...
if ($USER->enrol_guest_passwords[$instance->id] === $instance->password) {
$allow = true;
}
} else if (WS_SERVER) { // Mobile app mostly.
$storedpass = get_user_preferences('enrol_guest_ws_password_'. $instance->id);
// We check first if there is a supplied password.
if (!is_null($storedpass)) {
$allow = $storedpass === $instance->password;
if (!$allow) {
// Reset, probably the course password was changed.
unset_user_preference('enrol_guest_ws_password_' . $instance->id);
}
}
}
if ($allow) {
// Temporarily assign them some guest role for this context
$context = context_course::instance($instance->courseid);
load_temp_course_role($context, $CFG->guestroleid);
return ENROL_MAX_TIMESTAMP;
}
return false;
}
/**
* Returns true if the current user can add a new instance of enrolment plugin in course.
* @param int $courseid
* @return boolean
*/
public function can_add_instance($courseid) {
global $DB;
$context = context_course::instance($courseid, MUST_EXIST);
if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) {
return false;
}
if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) {
return false;
}
return true;
}
/**
* Creates course enrol form, checks if form submitted
* and enrols user if necessary. It can also redirect.
*
* @param stdClass $instance
* @return string html text, usually a form in a text box
*/
public function enrol_page_hook(stdClass $instance) {
global $CFG, $OUTPUT, $SESSION, $USER;
if ($instance->password === '') {
return null;
}
if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) {
// no need to show the guest access when user can already enter course as guest
return null;
}
require_once("$CFG->dirroot/enrol/guest/locallib.php");
$form = new enrol_guest_enrol_form(NULL, $instance);
$instanceid = optional_param('instance', 0, PARAM_INT);
if ($instance->id == $instanceid) {
if ($data = $form->get_data()) {
// add guest role
$context = context_course::instance($instance->courseid);
$USER->enrol_guest_passwords[$instance->id] = $data->guestpassword; // this is a hack, ideally we should not add stuff to $USER...
if (isset($USER->enrol['tempguest'][$instance->courseid])) {
remove_temp_course_roles($context);
}
load_temp_course_role($context, $CFG->guestroleid);
$USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP;
// go to the originally requested page
if (!empty($SESSION->wantsurl)) {
$destination = $SESSION->wantsurl;
unset($SESSION->wantsurl);
} else {
$destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid";
}
redirect($destination);
}
}
ob_start();
$form->display();
$output = ob_get_clean();
return $OUTPUT->box($output, 'generalbox');
}
/**
* 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;
if ($inserted) {
if (isset($data->enrol_guest_status_0)) {
$fields = array('status'=>$data->enrol_guest_status_0);
if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
$fields['password'] = $data->enrol_guest_password_0;
} else {
if ($this->get_config('requirepassword')) {
$fields['password'] = generate_password(20);
}
}
$this->add_instance($course, $fields);
} else {
if ($this->get_config('defaultenrol')) {
$this->add_default_instance($course);
}
}
} else {
$instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest'));
foreach ($instances as $instance) {
$i = $instance->id;
if (isset($data->{'enrol_guest_status_'.$i})) {
$reset = ($instance->status != $data->{'enrol_guest_status_'.$i});
$instance->status = $data->{'enrol_guest_status_'.$i};
$instance->timemodified = time();
if ($instance->status == ENROL_INSTANCE_ENABLED) {
if ($instance->password !== $data->{'enrol_guest_password_'.$i}) {
$reset = true;
}
$instance->password = $data->{'enrol_guest_password_'.$i};
}
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
if ($reset) {
$context = context_course::instance($course->id);
$context->mark_dirty();
}
}
}
}
}
/**
* Add new instance of enrol plugin.
* @param object $course
* @param array instance fields
* @return int id of new instance, null if can not be created
*/
public function add_instance($course, array $fields = NULL) {
$fields = (array)$fields;
if (!isset($fields['password'])) {
$fields['password'] = '';
}
return parent::add_instance($course, $fields);
}
/**
* Add new instance of enrol plugin with default settings.
* @param object $course
* @return int id of new instance
*/
public function add_default_instance($course) {
$fields = array('status'=>$this->get_config('status'));
if ($this->get_config('requirepassword')) {
$fields['password'] = generate_password(20);
}
return $this->add_instance($course, $fields);
}
/**
* Restore instance and map settings.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $course
* @param int $oldid
*/
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
global $DB;
if (!$DB->record_exists('enrol', array('courseid' => $data->courseid, 'enrol' => $this->get_name()))) {
$this->add_instance($course, (array)$data);
}
// No need to set mapping, we do not restore users or roles here.
$step->set_mapping('enrol', $oldid, 0);
}
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/guest:config', $context);
}
/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
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;
}
}
// Only check the password if it is set.
if (!empty($instance->password) && $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;
}
/**
* Return information for enrolment instance containing list of parameters required
* for enrolment, name of enrolment plugin etc.
*
* @param stdClass $instance enrolment instance
* @return stdClass instance info.
* @since Moodle 3.1
*/
public function get_enrol_info(stdClass $instance) {
$instanceinfo = new stdClass();
$instanceinfo->id = $instance->id;
$instanceinfo->courseid = $instance->courseid;
$instanceinfo->type = $this->get_name();
$instanceinfo->name = $this->get_instance_name($instance);
$instanceinfo->status = $instance->status == ENROL_INSTANCE_ENABLED;
// Specifics enrolment method parameters.
$instanceinfo->requiredparam = new stdClass();
$instanceinfo->requiredparam->passwordrequired = !empty($instance->password);
// If the plugin is enabled, return the URL for obtaining more information.
if ($instanceinfo->status) {
$instanceinfo->wsfunction = 'enrol_guest_get_instance_info';
}
return $instanceinfo;
}
/**
* 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;
}
/**
* Add elements to the edit instance form.
*
* @param stdClass $instance
* @param MoodleQuickForm $mform
* @param context $context
* @return bool
*/
public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
global $CFG;
$options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_guest'), $options);
$mform->addHelpButton('status', 'status', 'enrol_guest');
$mform->setDefault('status', $this->get_config('status'));
$mform->setAdvanced('status', $this->get_config('status_adv'));
$mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_guest'));
$mform->addHelpButton('password', 'password', 'enrol_guest');
// If we have a new instance and the password is required - make sure it is set. For existing
// instances we do not force the password to be required as it may have been set to empty before
// the password was required. We check in the validation function whether this check is required
// for existing instances.
if (empty($instance->id) && $this->get_config('requirepassword')) {
$mform->addRule('password', get_string('required'), 'required', null);
}
}
/**
* 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;
}
/**
* 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) {
$errors = array();
$checkpassword = false;
if ($data['id']) {
// Check the password if we are enabling the plugin again.
if (($instance->status == ENROL_INSTANCE_DISABLED) && ($data['status'] == ENROL_INSTANCE_ENABLED)) {
$checkpassword = true;
}
// Check the password if the instance is enabled and the password has changed.
if (($data['status'] == ENROL_INSTANCE_ENABLED) && ($instance->password !== $data['password'])) {
$checkpassword = true;
}
} else {
$checkpassword = true;
}
if ($checkpassword) {
$require = $this->get_config('requirepassword');
$policy = $this->get_config('usepasswordpolicy');
if ($require && trim($data['password']) === '') {
$errors['password'] = get_string('required');
} else if (!empty($data['password']) && $policy) {
$errmsg = '';
if (!check_password_policy($data['password'], $errmsg)) {
$errors['password'] = $errmsg;
}
}
}
$validstatus = array_keys($this->get_status_options());
$tovalidate = array(
'status' => $validstatus
);
$typeerrors = $this->validate_param_types($data, $tovalidate);
$errors = array_merge($errors, $typeerrors);
return $errors;
}
/**
* Check if enrolment plugin is supported in csv course upload.
*
* @return bool
*/
public function is_csv_upload_supported(): bool {
return true;
}
/**
* Finds matching instances for a given course.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return stdClass|null Matching instance
*/
public function find_instance(array $enrolmentdata, int $courseid): ?stdClass {
$instances = enrol_get_instances($courseid, false);
$instance = null;
foreach ($instances as $i) {
if ($i->enrol == 'guest') {
// There can be only one guest enrol instance so find first available.
$instance = $i;
break;
}
}
return $instance;
}
/**
* Fill custom fields data for a given enrolment plugin.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return array Updated enrolment data with custom fields info.
*/
public function fill_enrol_custom_fields(array $enrolmentdata, int $courseid): array {
return $enrolmentdata + ['password' => ''];
}
/**
* Updates enrol plugin instance with provided data.
* @param int $courseid Course ID.
* @param array $enrolmentdata enrolment data.
* @param stdClass $instance Instance to update.
*
* @return stdClass updated instance
*/
public function update_enrol_plugin_data(int $courseid, array $enrolmentdata, stdClass $instance): stdClass {
if (!empty($enrolmentdata['password'])) {
$instance->password = $enrolmentdata['password'];
}
return parent::update_enrol_plugin_data($courseid, $enrolmentdata, $instance);
}
/**
* Check if data is valid for a given enrolment plugin
*
* @param array $enrolmentdata enrolment data to validate.
* @param int|null $courseid Course ID.
* @return array Errors
*/
public function validate_enrol_plugin_data(array $enrolmentdata, ?int $courseid = null): array {
// If password is omitted or empty in csv it will be generated automatically if it is a required policy.
$errors = parent::validate_enrol_plugin_data($enrolmentdata, $courseid);
$policy = $this->get_config('usepasswordpolicy');
if (!empty($enrolmentdata['password']) && $policy) {
$errarray = get_password_policy_errors($enrolmentdata['password']);
foreach ($errarray as $i => $err) {
$errors['enrol_guest' . $i] = $err;
}
}
return $errors;
}
}
/**
* Get icon mapping for font-awesome.
*/
function enrol_guest_get_fontawesome_icon_map() {
return [
'enrol_guest:withpassword' => 'fa-key',
'enrol_guest:withoutpassword' => 'fa-unlock-alt',
];
}
+74
View File
@@ -0,0 +1,74 @@
<?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 implementation.
*
* @package enrol_guest
* @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_guest_enrol_form extends moodleform {
protected $instance;
public function definition() {
$mform = $this->_form;
$instance = $this->_customdata;
$this->instance = $instance;
$plugin = enrol_get_plugin('guest');
$heading = $plugin->get_instance_name($instance);
$mform->addElement('header', 'guestheader', $heading);
$mform->addElement('password', 'guestpassword', get_string('password', 'enrol_guest'));
$this->add_action_buttons(false, get_string('submit'));
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->setDefault('id', $instance->courseid);
$mform->addElement('hidden', 'instance');
$mform->setType('instance', PARAM_INT);
$mform->setDefault('instance', $instance->id);
}
public function validation($data, $files) {
global $DB, $CFG;
$errors = parent::validation($data, $files);
$instance = $this->instance;
if ($instance->password !== '') {
if ($data['guestpassword'] !== $instance->password) {
$plugin = enrol_get_plugin('guest');
if ($plugin->get_config('showhint')) {
$hint = core_text::substr($instance->password, 0, 1);
$errors['guestpassword'] = get_string('passwordinvalidhint', 'enrol_guest', $hint);
} else {
$errors['guestpassword'] = get_string('passwordinvalid', 'enrol_guest');
}
}
}
return $errors;
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 115 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

+3
View File
@@ -0,0 +1,3 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
]><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" preserveAspectRatio="xMinYMid meet" overflow="visible"><path d="M5.2 10.3L0 13.1V16h16v-3l-5.2-2.7c-.5-.3-.6-.9-.2-1.3 0 0 1.4-1.8 1.4-3.8C12 2.3 10.2 0 8 0S4 2.3 4 5.2C4 7.2 5.4 9 5.4 9c.4.5.3 1-.2 1.3z" fill="#888"/></svg>

After

Width:  |  Height:  |  Size: 462 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 349 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 482 B

+3
View File
@@ -0,0 +1,3 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
]><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" preserveAspectRatio="xMinYMid meet" overflow="visible"><path d="M3.4 9S2 7.2 2 5.2C2 2.3 3.8 0 6 0s4 2.3 4 5.2v.2c-1.2.8-2 2.2-2 3.8v.4l-4 4V16H0v-4l3.2-1.7c.5-.3.6-.8.2-1.3zm8.4 4.6L10.4 15H10v.4l-.6.6H9h3v-2.3c-.1-.1-.2-.1-.2-.1zM16 9.2c0 1.9-1.6 3.5-3.5 3.5-.4 0-.7-.1-1-.2L10 14H9v1H8v1H5v-2l4.1-4.1C9 9.6 9 9.4 9 9.2c0-1.9 1.6-3.5 3.5-3.5S16 7.2 16 9.2zM14.5 8c0-.6-.4-1-1-1s-1 .4-1 1 .4 1 1 1c.5 0 1-.5 1-1z" fill="#888"/></svg>

After

Width:  |  Height:  |  Size: 672 B

+55
View File
@@ -0,0 +1,55 @@
<?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 settings and presets.
*
* @package enrol_guest
* @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();
if ($ADMIN->fulltree) {
//--- general settings -----------------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_guest_settings', '', get_string('pluginname_desc', 'enrol_guest')));
$settings->add(new admin_setting_configcheckbox('enrol_guest/requirepassword',
get_string('requirepassword', 'enrol_guest'), get_string('requirepassword_desc', 'enrol_guest'), 0));
$settings->add(new admin_setting_configcheckbox('enrol_guest/usepasswordpolicy',
get_string('usepasswordpolicy', 'enrol_guest'), get_string('usepasswordpolicy_desc', 'enrol_guest'), 0));
$settings->add(new admin_setting_configcheckbox('enrol_guest/showhint',
get_string('showhint', 'enrol_guest'), get_string('showhint_desc', 'enrol_guest'), 0));
//--- enrol instance defaults ----------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_guest_defaults',
get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin')));
$settings->add(new admin_setting_configcheckbox('enrol_guest/defaultenrol',
get_string('defaultenrol', 'enrol'), get_string('defaultenrol_desc', 'enrol'), 1));
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no'));
$settings->add(new admin_setting_configselect_with_advanced('enrol_guest/status',
get_string('status', 'enrol_guest'), get_string('status_desc', 'enrol_guest'),
array('value'=>ENROL_INSTANCE_DISABLED, 'adv'=>false), $options));
}
@@ -0,0 +1,44 @@
@enrol @enrol_guest
Feature: Guest users can auto-enrol themself in courses where guest access is allowed
In order to access courses contents
As a guest
I need to access courses as a guest
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "activity" exists:
| activity | forum |
| course | C1 |
| idnumber | 0001 |
| name | Test forum name |
And I am on the "Course 1" "enrolment methods" page logged in as teacher1
Scenario: Allow guest access without password
Given 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 changes"
When I am on the "Test forum name" "forum activity" page logged in as student1
Then I should not see "Subscribe to this forum"
Scenario: Allow guest access with password
Given 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 changes"
When I am on the "Course 1" course page logged in as student1
Then I should see "Guest access"
And I set the following fields to these values:
| Password | moodle_rules |
And I press "Submit"
And I should see "Test forum name"
+122
View File
@@ -0,0 +1,122 @@
<?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/>.
/**
* Self enrol external PHPunit tests
*
* @package enrol_guest
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
namespace enrol_guest\external;
use core_external\external_api;
use enrol_guest_external;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Guest enrolment external functions tests
*
* @package enrol_guest
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
class external_test extends externallib_advanced_testcase {
/**
* Test get_instance_info
*/
public function test_get_instance_info(): void {
global $DB;
$this->resetAfterTest(true);
// Check if guest enrolment plugin is enabled.
$guestplugin = enrol_get_plugin('guest');
$this->assertNotEmpty($guestplugin);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$coursedata = new \stdClass();
$coursedata->visible = 0;
$course = self::getDataGenerator()->create_course($coursedata);
$student = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
// Add enrolment methods for course.
$instance = $guestplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance',
'customint6' => 1,
'roleid' => $studentrole->id));
$this->setAdminUser();
$result = enrol_guest_external::get_instance_info($instance);
$result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
$this->assertEquals($instance, $result['instanceinfo']['id']);
$this->assertEquals($course->id, $result['instanceinfo']['courseid']);
$this->assertEquals('guest', $result['instanceinfo']['type']);
$this->assertEquals('Test instance', $result['instanceinfo']['name']);
$this->assertTrue($result['instanceinfo']['status']);
$this->assertFalse($result['instanceinfo']['passwordrequired']);
$DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id' => $instance));
$result = enrol_guest_external::get_instance_info($instance);
$result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
$this->assertEquals($instance, $result['instanceinfo']['id']);
$this->assertEquals($course->id, $result['instanceinfo']['courseid']);
$this->assertEquals('guest', $result['instanceinfo']['type']);
$this->assertEquals('Test instance', $result['instanceinfo']['name']);
$this->assertFalse($result['instanceinfo']['status']);
$this->assertFalse($result['instanceinfo']['passwordrequired']);
$DB->set_field('enrol', 'status', ENROL_INSTANCE_ENABLED, array('id' => $instance));
// Try to retrieve information using a normal user for a hidden course.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
enrol_guest_external::get_instance_info($instance);
} catch (\moodle_exception $e) {
$this->assertEquals('coursehidden', $e->errorcode);
}
// Student user.
$DB->set_field('course', 'visible', 1, array('id' => $course->id));
$this->setUser($student);
$result = enrol_guest_external::get_instance_info($instance);
$result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
$this->assertEquals($instance, $result['instanceinfo']['id']);
$this->assertEquals($course->id, $result['instanceinfo']['courseid']);
$this->assertEquals('guest', $result['instanceinfo']['type']);
$this->assertEquals('Test instance', $result['instanceinfo']['name']);
$this->assertTrue($result['instanceinfo']['status']);
$this->assertFalse($result['instanceinfo']['passwordrequired']);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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/>.
namespace enrol_guest\external;
use core_external\external_api;
/**
* Tests for validate_password class.
*
* @package enrol_guest
* @covers \enrol_guest\external\validate_password
*/
class validate_password_test extends \advanced_testcase {
public function test_execute(): void {
global $DB;
$this->resetAfterTest();
$course = self::getDataGenerator()->create_course();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$student = self::getDataGenerator()->create_user();
$pass = 'abc';
// Add enrolment methods for course.
$guestplugin = enrol_get_plugin('guest');
$instanceid = $guestplugin->add_instance($course, [
'status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance',
'customint6' => 1,
'password' => $pass,
'roleid' => $studentrole->id,
]);
$this->setUser($student);
// Invalid password.
$result = validate_password::execute($instanceid, 'z');
$result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
$this->assertFalse($result['validated']);
$this->assertEmpty($result['hint']);
// Set invalid password preference.
set_user_preference('enrol_guest_ws_password_' . $instanceid, 'y');
// Enable hint for invalid password.
set_config('showhint', 1, 'enrol_guest');
$result = validate_password::execute($instanceid, 'z');
$result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
$this->assertFalse($result['validated']);
$this->assertNotEmpty($result['hint']); // Check hint.
$this->assertNull(get_user_preferences('enrol_guest_ws_password_'. $instanceid)); // Check preference was reset.
// Try valid password.
$result = validate_password::execute($instanceid, $pass);
$result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
$this->assertTrue($result['validated']);
// Check correct user preference.
$this->assertEquals($pass, get_user_preferences('enrol_guest_ws_password_'. $instanceid));
// Course hidden, expect exception.
$DB->set_field('course', 'visible', 0, ['id' => $course->id]);
$this->expectException(\moodle_exception::class);
$result = validate_password::execute($instanceid, '');
}
}
+130
View File
@@ -0,0 +1,130 @@
<?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 enrolment tests.
*
* @package enrol_guest
* @category phpunit
* @copyright 2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_guest;
class lib_test extends \advanced_testcase {
/**
* Test the behaviour of validate_enrol_plugin_data().
*
* @covers ::validate_enrol_plugin_data
*/
public function test_validate_enrol_plugin_data(): void {
global $CFG;
$this->resetAfterTest();
$guestplugin = enrol_get_plugin('guest');
$guestplugin->set_config('usepasswordpolicy', false);
$enrolmentdata = [];
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertEmpty($errors);
// Now enable some controls, and check that the policy responds with policy text.
$guestplugin->set_config('usepasswordpolicy', true);
$CFG->minpasswordlength = 8;
$CFG->minpassworddigits = 1;
$CFG->minpasswordlower = 1;
$CFG->minpasswordupper = 1;
$CFG->minpasswordnonalphanum = 1;
$CFG->maxconsecutiveidentchars = 1;
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
// If password is omitted it will be autocreated so nothing to validate.
$this->assertEmpty($errors);
$enrolmentdata = ['password' => 'test'];
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertCount(4, $errors);
$this->assertEquals(get_string('errorminpasswordlength', 'auth', $CFG->minpasswordlength), $errors['enrol_guest0']);
$this->assertEquals(get_string('errorminpassworddigits', 'auth', $CFG->minpassworddigits), $errors['enrol_guest1']);
$this->assertEquals(get_string('errorminpasswordupper', 'auth', $CFG->minpasswordupper), $errors['enrol_guest2']);
$this->assertEquals(get_string('errorminpasswordnonalphanum', 'auth', $CFG->minpasswordnonalphanum), $errors['enrol_guest3']);
$enrolmentdata = ['password' => 'Testingtest123@'];
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertEmpty($errors);
}
/**
* Test the behaviour of update_enrol_plugin_data().
*
* @covers ::update_enrol_plugin_data
*/
public function test_update_enrol_plugin_data(): void {
global $DB;
$this->resetAfterTest();
$manualplugin = enrol_get_plugin('guest');
$admin = get_admin();
$this->setUser($admin);
$enrolmentdata = [];
$cat = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
$instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest'], '*', MUST_EXIST);
$expectedinstance = $instance;
$modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
$this->assertEquals($expectedinstance, $modifiedinstance);
$enrolmentdata['password'] = 'test';
$expectedinstance->password = 'test';
$modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
$this->assertEquals($expectedinstance, $modifiedinstance);
}
/**
* Test the behaviour of find_instance().
*
* @covers ::find_instance
*/
public function test_find_instance(): void {
global $DB;
$this->resetAfterTest();
$cat = $this->getDataGenerator()->create_category();
// When we create a course, a guest enrolment instance is also created.
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
$guestplugin = enrol_get_plugin('guest');
$expected = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest']);
// Let's try to add second instance - only 1 guest instance is possible.
$instanceid2 = null;
// Have to do this check since add_instance doesn't block adding second instance for guest plugin.
if ($guestplugin->can_add_instance($course->id)) {
$instanceid2 = $guestplugin->add_instance($course, []);
}
$this->assertNull($instanceid2);
$enrolmentdata = [];
$actual = $guestplugin->find_instance($enrolmentdata, $course->id);
$this->assertEquals($expected->id, $actual->id);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?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 version specification.
*
* @package enrol_guest
* @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 = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'enrol_guest'; // Full name of the plugin (used for diagnostics)