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
View File
@@ -0,0 +1,37 @@
<?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/>.
/**
* The form to confirm the intention to bulk delete users enrolments.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->dirroot/enrol/bulkchange_forms.php");
/**
* The form to confirm the intention to bulk delete users enrolments.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_self_deleteselectedusers_form extends enrol_bulk_enrolment_confirm_form {
}
@@ -0,0 +1,98 @@
<?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/>.
/**
* A bulk operation for the self enrolment plugin to delete selected users enrolments.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* A bulk operation for the self enrolment plugin to delete selected users enrolments.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_self_deleteselectedusers_operation extends enrol_bulk_enrolment_operation {
/**
* Returns the title to display for this bulk operation.
*
* @return string
*/
public function get_identifier() {
return 'deleteselectedusers';
}
/**
* Returns the identifier for this bulk operation. This is the key used when the plugin
* returns an array containing all of the bulk operations it supports.
*
* @return string
*/
public function get_title() {
return get_string('deleteselectedusers', 'enrol_self');
}
/**
* Returns a enrol_bulk_enrolment_operation extension form to be used
* in collecting required information for this operation to be processed.
*
* @param string|moodle_url|null $defaultaction
* @param mixed $defaultcustomdata
* @return enrol_self_deleteselectedusers_form
*/
public function get_form($defaultaction = null, $defaultcustomdata = null) {
if (!array($defaultcustomdata)) {
$defaultcustomdata = array();
}
$defaultcustomdata['title'] = $this->get_title();
$defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_self');
$defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_self');
return new enrol_self_deleteselectedusers_form($defaultaction, $defaultcustomdata);
}
/**
* Processes the bulk operation request for the given userids with the provided properties.
*
* @param course_enrolment_manager $manager
* @param array $users
* @param stdClass $properties The data returned by the form.
*/
public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
if (!has_capability("enrol/self:unenrol", $manager->get_context())) {
return false;
}
foreach ($users as $user) {
foreach ($user->enrolments as $enrolment) {
$plugin = $enrolment->enrolmentplugin;
$instance = $enrolment->enrolmentinstance;
if ($plugin->allow_unenrol_user($instance, $enrolment)) {
$plugin->unenrol_user($instance, $user->id);
}
}
}
return true;
}
}
@@ -0,0 +1,37 @@
<?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/>.
/**
* The form to collect required information when bulk editing users enrolments.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->dirroot/enrol/bulkchange_forms.php");
/**
* The form to collect required information when bulk editing users enrolments.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_self_editselectedusers_form extends enrol_bulk_enrolment_change_form {
}
@@ -0,0 +1,161 @@
<?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/>.
/**
* A bulk operation for the manual enrolment plugin to edit selected users.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* A bulk operation for the manual enrolment plugin to edit selected users.
*
* @package enrol_self
* @copyright 2018 Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_self_editselectedusers_operation extends enrol_bulk_enrolment_operation {
/**
* Returns the title to display for this bulk operation.
*
* @return string
*/
public function get_title() {
return get_string('editselectedusers', 'enrol_self');
}
/**
* Returns the identifier for this bulk operation. This is the key used when the plugin
* returns an array containing all of the bulk operations it supports.
*/
public function get_identifier() {
return 'editselectedusers';
}
/**
* Processes the bulk operation request for the given userids with the provided properties.
*
* @param course_enrolment_manager $manager
* @param array $users
* @param stdClass $properties The data returned by the form.
*/
public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
global $DB, $USER;
if (!has_capability("enrol/self:manage", $manager->get_context())) {
return false;
}
// Get all of the user enrolment id's.
$ueids = array();
$instances = array();
foreach ($users as $user) {
foreach ($user->enrolments as $enrolment) {
$ueids[] = $enrolment->id;
if (!array_key_exists($enrolment->id, $instances)) {
$instances[$enrolment->id] = $enrolment;
}
}
}
// Check that each instance is manageable by the current user.
foreach ($instances as $instance) {
if (!$this->plugin->allow_manage($instance)) {
return false;
}
}
// Collect the known properties.
$status = $properties->status;
$timestart = $properties->timestart;
$timeend = $properties->timeend;
list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED);
$updatesql = array();
if ($status == ENROL_USER_ACTIVE || $status == ENROL_USER_SUSPENDED) {
$updatesql[] = 'status = :status';
$params['status'] = (int)$status;
}
if (!empty($timestart)) {
$updatesql[] = 'timestart = :timestart';
$params['timestart'] = (int)$timestart;
}
if (!empty($timeend)) {
$updatesql[] = 'timeend = :timeend';
$params['timeend'] = (int)$timeend;
}
if (empty($updatesql)) {
return true;
}
// Update the modifierid.
$updatesql[] = 'modifierid = :modifierid';
$params['modifierid'] = (int)$USER->id;
// Update the time modified.
$updatesql[] = 'timemodified = :timemodified';
$params['timemodified'] = time();
// Build the SQL statement.
$updatesql = join(', ', $updatesql);
$sql = "UPDATE {user_enrolments}
SET $updatesql
WHERE id $ueidsql";
if ($DB->execute($sql, $params)) {
foreach ($users as $user) {
foreach ($user->enrolments as $enrolment) {
$enrolment->courseid = $enrolment->enrolmentinstance->courseid;
$enrolment->enrol = 'self';
// Trigger event.
$event = \core\event\user_enrolment_updated::create(
array(
'objectid' => $enrolment->id,
'courseid' => $enrolment->courseid,
'context' => context_course::instance($enrolment->courseid),
'relateduserid' => $user->id,
'other' => array('enrol' => 'self')
)
);
$event->trigger();
}
}
// Delete cached course contacts for this course because they may be affected.
cache::make('core', 'coursecontacts')->delete($manager->get_context()->instanceid);
return true;
}
return false;
}
/**
* Returns a enrol_bulk_enrolment_operation extension form to be used
* in collecting required information for this operation to be processed.
*
* @param string|moodle_url|null $defaultaction
* @param mixed $defaultcustomdata
* @return enrol_self_editselectedusers_form
*/
public function get_form($defaultaction = null, $defaultcustomdata = null) {
return new enrol_self_editselectedusers_form($defaultaction, $defaultcustomdata);
}
}
+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/>.
/**
* Empty enrol_self form.
*
* Useful to mimic valid enrol instances UI when the enrolment instance is not available.
*
* @package enrol_self
* @copyright 2015 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
class enrol_self_empty_form extends moodleform {
/**
* Form definition.
* @return void
*/
public function definition() {
$this->_form->addElement('header', 'selfheader', $this->_customdata->header);
$this->_form->addElement('static', 'info', '', $this->_customdata->info);
}
}
+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_self.
*
* @package enrol_self
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_self\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for enrol_self 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';
}
}
@@ -0,0 +1,58 @@
<?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/>.
/**
* Send expiry notifications task.
*
* @package enrol_self
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_self\task;
defined('MOODLE_INTERNAL') || die();
/**
* Send expiry notifications task.
*
* @package enrol_self
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class send_expiry_notifications extends \core\task\scheduled_task {
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('sendexpirynotificationstask', 'enrol_self');
}
/**
* Run task for sending expiry notifications.
*/
public function execute() {
$enrol = enrol_get_plugin('self');
$trace = new \text_progress_trace();
$enrol->send_expiry_notifications($trace);
}
}
@@ -0,0 +1,58 @@
<?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/>.
/**
* Sync enrolments task.
*
* @package enrol_self
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_self\task;
defined('MOODLE_INTERNAL') || die();
/**
* Sync enrolments task.
*
* @package enrol_self
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sync_enrolments extends \core\task\scheduled_task {
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('syncenrolmentstask', 'enrol_self');
}
/**
* Run task for syncing enrolments.
*/
public function execute() {
$enrol = enrol_get_plugin('self');
$trace = new \text_progress_trace();
$enrol->sync($trace);
}
}
@@ -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/>.
namespace enrol_self;
/**
* Hook callbacks to get the enrolment information.
*
* @package enrol_self
* @copyright 2024 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_enrolment_callbacks {
/**
* Callback for the user_enrolment hook.
*
* @param \core_enrol\hook\after_user_enrolled $hook
*/
public static function send_course_welcome_message(\core_enrol\hook\after_user_enrolled $hook): void {
$instance = $hook->get_enrolinstance();
// Send welcome message.
if ($instance->enrol == 'self' && $instance->customint4 && $instance->customint4 !== ENROL_DO_NOT_SEND_EMAIL) {
$plugin = enrol_get_plugin($instance->enrol);
$plugin->send_course_welcome_message_to_user(
instance: $instance,
userid: $hook->get_userid(),
sendoption: $instance->customint4,
message: $instance->customtext1,
);
}
}
}
+76
View File
@@ -0,0 +1,76 @@
<?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/>.
/**
* CLI update for self enrolments, use for debugging or immediate update
* of all courses.
*
* Notes:
* - it is required to use the web server account when executing PHP CLI scripts
* - you need to change the "www-data" to match the apache user account
* - use "su" if "sudo" not available
*
* @package enrol_self
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require(__DIR__.'/../../../config.php');
require_once("$CFG->libdir/clilib.php");
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array('verbose'=>false, 'help'=>false), array('v'=>'verbose', 'h'=>'help'));
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help']) {
$help =
"Execute self course enrol updates.
Options:
-v, --verbose Print verbose progress information
-h, --help Print out this help
Example:
\$ sudo -u www-data /usr/bin/php enrol/self/cli/sync.php
";
echo $help;
die;
}
if (!enrol_is_enabled('self')) {
cli_error('enrol_self plugin is disabled, synchronisation stopped', 2);
}
if (empty($options['verbose'])) {
$trace = new null_progress_trace();
} else {
$trace = new text_progress_trace();
}
/** @var $plugin enrol_self_plugin */
$plugin = enrol_get_plugin('self');
$result = $plugin->sync($trace, null);
$plugin->send_expiry_notifications($trace);
exit($result);
+85
View File
@@ -0,0 +1,85 @@
<?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 self enrolment plugin.
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
/* Add or edit enrol-self instance in course. */
'enrol/self:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
)
),
/* Manage user self-enrolments. */
'enrol/self:manage' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
)
),
'enrol/self:holdkey' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
),
/* Voluntarily unenrol self from course - watch out for data loss. */
'enrol/self:unenrolself' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'student' => CAP_ALLOW,
)
),
/* Unenrol anybody from course (including self) - watch out for data loss. */
'enrol/self:unenrol' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
)
),
/* Ability to enrol self in courses. */
'enrol/self:enrolself' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'user' => CAP_ALLOW,
)
),
);
+32
View File
@@ -0,0 +1,32 @@
<?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/>.
/**
* Hook callbacks for enrol_self
*
* @package enrol_self
* @copyright 2024 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$callbacks = [
[
'hook' => core_enrol\hook\after_user_enrolled::class,
'callback' => 'enrol_self\user_enrolment_callbacks::send_course_welcome_message',
],
];
+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/>.
/**
* Self enrol plugin installation script
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
function xmldb_enrol_self_install() {
global $CFG, $DB;
}
+34
View File
@@ -0,0 +1,34 @@
<?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/>.
/**
* Defines message providers for self enrolments.
*
* @package enrol_self
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$messageproviders = array (
'expiry_notification' => [
'defaults' => [
'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
],
],
);
+44
View File
@@ -0,0 +1,44 @@
<?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 plugin external functions and service definitions.
*
* @package enrol_self
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.6
*/
$functions = array(
'enrol_self_get_instance_info' => array(
'classname' => 'enrol_self_external',
'methodname' => 'get_instance_info',
'classpath' => 'enrol/self/externallib.php',
'description' => 'self enrolment instance information.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'enrol_self_enrol_user' => array(
'classname' => 'enrol_self_external',
'methodname' => 'enrol_user',
'classpath' => 'enrol/self/externallib.php',
'description' => 'Self enrol the current user in the given course.',
'type' => 'write',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
)
);
+49
View File
@@ -0,0 +1,49 @@
<?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/>.
/**
* Task definition for enrol_self.
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @package enrol_self
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => '\enrol_self\task\sync_enrolments',
'blocking' => 0,
'minute' => '*/10',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 0
),
array(
'classname' => '\enrol_self\task\send_expiry_notifications',
'blocking' => 0,
'minute' => '*/10',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 0
)
);
+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 self enrolment plugin
*
* @package enrol_self
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function xmldb_enrol_self_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;
}
+251
View File
@@ -0,0 +1,251 @@
<?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/>.
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;
/**
* Self enrolment external functions.
*
* @package enrol_self
* @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.6
*/
class enrol_self_external extends external_api {
/**
* Returns description of get_instance_info() parameters.
*
* @return external_function_parameters
*/
public static function get_instance_info_parameters() {
return new external_function_parameters([
'instanceid' => new external_value(PARAM_INT, 'instance id of self enrolment plugin.'),
]);
}
/**
* Return self-enrolment instance information.
*
* @param int $instanceid instance id of self enrolment plugin.
* @return array instance information.
* @throws moodle_exception
*/
public static function get_instance_info($instanceid) {
global $DB, $CFG;
require_once($CFG->libdir . '/enrollib.php');
$params = self::validate_parameters(self::get_instance_info_parameters(), array('instanceid' => $instanceid));
// Retrieve self enrolment plugin.
$enrolplugin = enrol_get_plugin('self');
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 = (array) $enrolplugin->get_enrol_info($enrolinstance);
if (isset($instanceinfo['requiredparam']->enrolpassword)) {
$instanceinfo['enrolpassword'] = $instanceinfo['requiredparam']->enrolpassword;
}
unset($instanceinfo->requiredparam);
return $instanceinfo;
}
/**
* Returns description of get_instance_info() result value.
*
* @return \core_external\external_description
*/
public static function get_instance_info_returns() {
return 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_RAW, 'status of enrolment plugin'),
'enrolpassword' => new external_value(PARAM_RAW, 'password required for enrolment', VALUE_OPTIONAL),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function enrol_user_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'Id of the course'),
'password' => new external_value(PARAM_RAW, 'Enrolment key', VALUE_DEFAULT, ''),
'instanceid' => new external_value(PARAM_INT, 'Instance id of self enrolment plugin.', VALUE_DEFAULT, 0)
)
);
}
/**
* Self enrol the current user in the given course.
*
* @param int $courseid id of course
* @param string $password enrolment key
* @param int $instanceid instance id of self enrolment plugin
* @return array of warnings and status result
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function enrol_user($courseid, $password = '', $instanceid = 0) {
global $CFG;
require_once($CFG->libdir . '/enrollib.php');
$params = self::validate_parameters(self::enrol_user_parameters(),
array(
'courseid' => $courseid,
'password' => $password,
'instanceid' => $instanceid
));
$warnings = array();
$course = get_course($params['courseid']);
$context = context_course::instance($course->id);
self::validate_context(context_system::instance());
if (!core_course_category::can_view_course_info($course)) {
throw new moodle_exception('coursehidden');
}
// Retrieve the self enrolment plugin.
$enrol = enrol_get_plugin('self');
if (empty($enrol)) {
throw new moodle_exception('canntenrol', 'enrol_self');
}
// We can expect multiple self-enrolment instances.
$instances = array();
$enrolinstances = enrol_get_instances($course->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "self") {
// Instance specified.
if (!empty($params['instanceid'])) {
if ($courseenrolinstance->id == $params['instanceid']) {
$instances[] = $courseenrolinstance;
break;
}
} else {
$instances[] = $courseenrolinstance;
}
}
}
if (empty($instances)) {
throw new moodle_exception('canntenrol', 'enrol_self');
}
// Try to enrol the user in the instance/s.
$enrolled = false;
foreach ($instances as $instance) {
$enrolstatus = $enrol->can_self_enrol($instance);
if ($enrolstatus === true) {
if ($instance->password and $params['password'] !== $instance->password) {
// Check if we are using group enrolment keys.
if ($instance->customint1) {
require_once($CFG->dirroot . "/enrol/self/locallib.php");
if (!enrol_self_check_group_enrolment_key($course->id, $params['password'])) {
$warnings[] = array(
'item' => 'instance',
'itemid' => $instance->id,
'warningcode' => '2',
'message' => get_string('passwordinvalid', 'enrol_self')
);
continue;
}
} else {
if ($enrol->get_config('showhint')) {
$hint = core_text::substr($instance->password, 0, 1);
$warnings[] = array(
'item' => 'instance',
'itemid' => $instance->id,
'warningcode' => '3',
'message' => s(get_string('passwordinvalidhint', 'enrol_self', $hint)) // message is PARAM_TEXT.
);
continue;
} else {
$warnings[] = array(
'item' => 'instance',
'itemid' => $instance->id,
'warningcode' => '4',
'message' => get_string('passwordinvalid', 'enrol_self')
);
continue;
}
}
}
// Do the enrolment.
$data = array('enrolpassword' => $params['password']);
$enrol->enrol_self($instance, (object) $data);
$enrolled = true;
break;
} else {
$warnings[] = array(
'item' => 'instance',
'itemid' => $instance->id,
'warningcode' => '1',
'message' => $enrolstatus
);
}
}
$result = array();
$result['status'] = $enrolled;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.0
*/
public static function enrol_user_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if the user is enrolled, false otherwise'),
'warnings' => new external_warnings()
)
);
}
}
+4
View File
@@ -0,0 +1,4 @@
customwelcomemessage,enrol_self
customwelcomemessage_help,enrol_self
welcometocourse,enrol_self
welcometocoursetext,enrol_self
+138
View File
@@ -0,0 +1,138 @@
<?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_self', language 'en'.
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['canntenrol'] = 'Enrolment is disabled or inactive';
$string['canntenrolearly'] = 'You cannot enrol yet; enrolment starts on {$a}.';
$string['canntenrollate'] = 'You cannot enrol any more, since enrolment ended on {$a}.';
$string['cohortnonmemberinfo'] = 'Only members of cohort \'{$a}\' can self-enrol.';
$string['cohortonly'] = 'Only cohort members';
$string['cohortonly_help'] = 'Self enrolment may be restricted to members of a specified cohort only. Note that changing this setting has no effect on existing enrolments.';
$string['confirmbulkdeleteenrolment'] = 'Are you sure you want to delete these user enrolments?';
$string['defaultrole'] = 'Default role assignment';
$string['defaultrole_desc'] = 'Select role which should be assigned to users during self enrolment';
$string['deleteselectedusers'] = 'Delete selected user enrolments';
$string['editselectedusers'] = 'Edit selected user enrolments';
$string['enrolenddate'] = 'End date';
$string['enrolenddate_help'] = 'If enabled, users can enrol themselves until this date only.';
$string['enrolenddaterror'] = 'Enrolment end date cannot be earlier than start date';
$string['enrolme'] = 'Enrol me';
$string['enrolperiod'] = 'Enrolment duration';
$string['enrolperiod_desc'] = 'Default length of time that the enrolment is valid. If set to zero, the enrolment duration will be unlimited by default.';
$string['enrolperiod_help'] = 'Length of time that the enrolment is valid, starting with the moment the user enrols themselves. If disabled, the enrolment duration will be unlimited.';
$string['enrolstartdate'] = 'Start date';
$string['enrolstartdate_help'] = 'If enabled, users can enrol themselves from this date onward only.';
$string['expiredaction'] = 'Enrolment expiry action';
$string['expiredaction_help'] = 'Select action to carry out when user enrolment expires. Please note that some user data and settings are purged from course during course unenrolment.';
$string['expiryinactivemessageenrolledbody'] = 'Hi {$a->user},
Your enrolment in the course {$a->course} expires on {$a->timeend} as you have not accessed it in the last {$a->inactivetime} days.
To keep your enrolment active, log in and access <a href="{$a->url}">{$a->course}</a> before {$a->timeend}.';
$string['expiryinactivemessageenrolledsubject'] = 'Your enrolment is expiring: {$a->course}';
$string['expirymessageenrollersubject'] = 'Self enrolment expiry notification';
$string['expirymessageenrollerbody'] = 'Self enrolment in the course \'{$a->course}\' will expire within the next {$a->threshold} for the following users:
{$a->users}
To extend their enrolment, go to {$a->extendurl}';
$string['expirymessageenrolledsubject'] = 'Self enrolment expiry notification';
$string['expirymessageenrolledbody'] = 'Dear {$a->user},
This is a notification that your enrolment in the course \'{$a->course}\' is due to expire on {$a->timeend}.
If you need help, please contact {$a->enroller}.';
$string['expirynotifyall'] = 'Teacher and enrolled user';
$string['expirynotifyenroller'] = 'Teacher only';
$string['groupkey'] = 'Use group enrolment keys';
$string['groupkey_desc'] = 'Use group enrolment keys by default.';
$string['groupkey_help'] = 'In addition to restricting access to the course to only those who know the key, use of group enrolment keys means users are automatically added to groups when they enrol in the course.
Note: An enrolment key for the course must be specified in the self enrolment settings as well as group enrolment keys in the group settings.';
$string['keyholder'] = 'You should have received this enrolment key from:';
$string['longtimenosee'] = 'Unenrol inactive after';
$string['longtimenosee_help'] = 'If users haven\'t accessed a course for a long time, then they are automatically unenrolled. This parameter specifies that time limit.';
$string['maxenrolled'] = 'Max enrolled users';
$string['maxenrolled_help'] = 'Specifies the maximum number of users that can self enrol. 0 means no limit.';
$string['maxenrolledreached'] = 'Maximum number of users allowed to self-enrol was already reached.';
$string['messageprovider:expiry_notification'] = 'Self enrolment expiry notifications';
$string['newenrols'] = 'Allow new self enrolments';
$string['newenrols_desc'] = 'Allow users to self enrol into new courses by default.';
$string['newenrols_help'] = 'This setting determines whether a user can enrol into this course.';
$string['nopassword'] = 'No enrolment key required.';
$string['password'] = 'Enrolment key';
$string['password_help'] = 'An enrolment key enables access to the course to be restricted to only those who know the key.
If the field is left blank, any user may enrol in the course.
If an enrolment key is specified, any user attempting to enrol in the course will be required to supply the key. Note that a user only needs to supply the enrolment key ONCE, when they enrol in the course.';
$string['passwordinvalid'] = 'Incorrect enrolment key, please try again';
$string['passwordinvalidhint'] = 'That enrolment key was incorrect, please try again<br />
(Here\'s a hint - it starts with \'{$a}\')';
$string['passwordmatchesgroupkey'] = 'This enrolment key is already used as a group enrolment key.';
$string['pluginname'] = 'Self enrolment';
$string['pluginname_desc'] = 'The self enrolment plugin allows users to choose which courses they want to participate in. The courses may be protected by an enrolment key. Internally the enrolment is done via the manual enrolment plugin which has to be enabled in the same course.';
$string['requirepassword'] = 'Require enrolment key';
$string['requirepassword_desc'] = 'Require enrolment key in new courses and prevent removing of enrolment key from existing courses.';
$string['role'] = 'Default assigned role';
$string['self:config'] = 'Configure self enrol instances';
$string['self:enrolself'] = 'Self enrol in course';
$string['self:holdkey'] = 'Appear as the self enrolment key holder';
$string['self:manage'] = 'Manage enrolled users';
$string['self:unenrol'] = 'Unenrol users from course';
$string['self:unenrolself'] = 'Unenrol self from the course';
$string['sendcoursewelcomemessage'] = 'Send course welcome message';
$string['sendcoursewelcomemessage_help'] = 'When a user self enrols in the course, they may be sent a welcome message email. If sent from the course contact (by default the teacher), and more than one user has this role, the email is sent from the first user to be assigned the role.';
$string['sendexpirynotificationstask'] = "Self enrolment send expiry notifications task";
$string['showhint'] = 'Show hint';
$string['showhint_desc'] = 'Show first letter of the guest access key.';
$string['status'] = 'Keep current self enrolments active';
$string['status_desc'] = 'Enable self enrolment method in new courses.';
$string['status_help'] = 'If set to No, any existing participants who enrolled themselves into the course will no longer have access.';
$string['syncenrolmentstask'] = 'Synchronise self enrolments task';
$string['unenrol'] = 'Unenrol user';
$string['unenrolselfconfirm'] = 'Do you really want to unenrol yourself from course "{$a}"?';
$string['unenroluser'] = 'Do you really want to unenrol "{$a->user}" from course "{$a->course}"?';
$string['unenrolusers'] = 'Unenrol users';
$string['usepasswordpolicy'] = 'Use password policy';
$string['usepasswordpolicy_desc'] = 'Use standard password policy for enrolment keys.';
$string['privacy:metadata'] = 'The Self enrolment plugin does not store any personal data.';
// Deprecated since Moodle 4.4.
$string['customwelcomemessage'] = 'Custom welcome message';
$string['customwelcomemessage_help'] = 'Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:
<ul>
<li>Course name {$a->coursename}</li>
<li>Link to user\'s profile page {$a->profileurl}</li>
<li>User email {$a->email}</li>
<li>User fullname {$a->fullname}</li>
<li>User first name {$a->firstname}</li>
<li>User last name {$a->lastname}</li>
<li>User course role {$a->courserole}</li>
</ul>';
$string['welcometocourse'] = 'Welcome to {$a}';
$string['welcometocoursetext'] = 'Welcome to {$a->coursename}!
If you have not done so already, you should edit your profile page so that we can learn more about you:
{$a->profileurl}';
+1270
View File
File diff suppressed because it is too large Load Diff
+155
View File
@@ -0,0 +1,155 @@
<?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 plugin implementation.
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/formslib.php");
require_once($CFG->dirroot . '/enrol/locallib.php');
/**
* Check if the given password match a group enrolment key in the specified course.
*
* @param int $courseid course id
* @param string $enrolpassword enrolment password
* @return bool True if match
* @since Moodle 3.0
*/
function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
global $DB;
$found = false;
$groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
foreach ($groups as $group) {
if (empty($group->enrolmentkey)) {
continue;
}
if ($group->enrolmentkey === $enrolpassword) {
$found = true;
break;
}
}
return $found;
}
class enrol_self_enrol_form extends moodleform {
protected $instance;
protected $toomany = false;
/**
* Overriding this function to get unique form id for multiple self enrolments.
*
* @return string form identifier
*/
protected function get_form_identifier() {
$formid = $this->_customdata->id.'_'.get_class($this);
return $formid;
}
public function definition() {
global $USER, $OUTPUT, $CFG;
$mform = $this->_form;
$instance = $this->_customdata;
$this->instance = $instance;
$plugin = enrol_get_plugin('self');
$heading = $plugin->get_instance_name($instance);
$mform->addElement('header', 'selfheader', $heading);
if ($instance->password) {
// Change the id of self enrolment key input as there can be multiple self enrolment methods.
$mform->addElement('password', 'enrolpassword', get_string('password', 'enrol_self'),
array('id' => 'enrolpassword_'.$instance->id));
$context = context_course::instance($this->instance->courseid);
$userfieldsapi = \core_user\fields::for_userpic();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$keyholders = get_users_by_capability($context, 'enrol/self:holdkey', $ufields);
$keyholdercount = 0;
foreach ($keyholders as $keyholder) {
$keyholdercount++;
if ($keyholdercount === 1) {
$mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
}
$keyholdercontext = context_user::instance($keyholder->id);
if ($USER->id == $keyholder->id || has_capability('moodle/user:viewdetails', context_system::instance()) ||
has_coursecontact_role($keyholder->id)) {
$profilelink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $keyholder->id . '&amp;course=' .
$this->instance->courseid . '">' . fullname($keyholder) . '</a>';
} else {
$profilelink = fullname($keyholder);
}
$profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
$mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
}
} else {
$mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
}
$this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
$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 ($this->toomany) {
$errors['notice'] = get_string('error');
return $errors;
}
if ($instance->password) {
if ($data['enrolpassword'] !== $instance->password) {
if ($instance->customint1) {
// Check group enrolment key.
if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
// We can not hint because there are probably multiple passwords.
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
}
} else {
$plugin = enrol_get_plugin('self');
if ($plugin->get_config('showhint')) {
$hint = core_text::substr($instance->password, 0, 1);
$errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
} else {
$errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
}
}
}
}
return $errors;
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 608 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 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="M9 16H1c-.5 0-1-.5-1-1V1c0-.5.5-1 1-1h8v2H3c-.6 0-1 .4-1 1v10c0 .6.4 1 1 1h6v2zm7-10.8c0 1.9-1.6 3.5-3.5 3.5-.4 0-.7-.1-1-.2L10 10H9v1H8v1H5v-2l4.1-4.1C9 5.7 9 5.5 9 5.2c0-1.9 1.6-3.5 3.5-3.5S16 3.3 16 5.2zM14.5 4c0-.6-.4-1-1-1s-1 .4-1 1 .4 1 1 1c.5 0 1-.4 1-1z" fill="#888"/></svg>

After

Width:  |  Height:  |  Size: 584 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 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="M9 16H1c-.5 0-1-.5-1-1V1c0-.5.5-1 1-1h8v2H3c-.6 0-1 .4-1 1v10c0 .6.4 1 1 1h6v2zm6-9.5h-4.7l1-1c.4-.4.4-1 0-1.4l-.7-.7c-.4-.4-1-.4-1.4 0l-4 4c-.4.4-.4 1 0 1.4l4 4c.4.4 1 .4 1.4 0l.7-.7c.4-.4.4-1 0-1.4l-1.1-1.1H15c.5 0 1-.4 1-1v-1c0-.6-.5-1.1-1-1.1z" fill="#888"/></svg>

After

Width:  |  Height:  |  Size: 570 B

+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/>.
/**
* Self enrolment plugin settings and presets.
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
//--- general settings -----------------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_self_settings', '', get_string('pluginname_desc', 'enrol_self')));
$settings->add(new admin_setting_configcheckbox('enrol_self/requirepassword',
get_string('requirepassword', 'enrol_self'), get_string('requirepassword_desc', 'enrol_self'), 0));
$settings->add(new admin_setting_configcheckbox('enrol_self/usepasswordpolicy',
get_string('usepasswordpolicy', 'enrol_self'), get_string('usepasswordpolicy_desc', 'enrol_self'), 0));
$settings->add(new admin_setting_configcheckbox('enrol_self/showhint',
get_string('showhint', 'enrol_self'), get_string('showhint_desc', 'enrol_self'), 0));
// Note: let's reuse the ext sync constants and strings here, internally it is very similar,
// it describes what should happend when users are not supposed to be enerolled any more.
$options = array(
ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'),
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'),
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
);
$settings->add(new admin_setting_configselect('enrol_self/expiredaction', get_string('expiredaction', 'enrol_self'), get_string('expiredaction_help', 'enrol_self'), ENROL_EXT_REMOVED_KEEP, $options));
$options = array();
for ($i=0; $i<24; $i++) {
$options[$i] = $i;
}
$settings->add(new admin_setting_configselect('enrol_self/expirynotifyhour', get_string('expirynotifyhour', 'core_enrol'), '', 6, $options));
//--- enrol instance defaults ----------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_self_defaults',
get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin')));
$settings->add(new admin_setting_configcheckbox('enrol_self/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('enrol_self/status',
get_string('status', 'enrol_self'), get_string('status_desc', 'enrol_self'), ENROL_INSTANCE_DISABLED, $options));
$options = array(1 => get_string('yes'), 0 => get_string('no'));
$settings->add(new admin_setting_configselect('enrol_self/newenrols',
get_string('newenrols', 'enrol_self'), get_string('newenrols_desc', 'enrol_self'), 1, $options));
$options = array(1 => get_string('yes'),
0 => get_string('no'));
$settings->add(new admin_setting_configselect('enrol_self/groupkey',
get_string('groupkey', 'enrol_self'), get_string('groupkey_desc', 'enrol_self'), 0, $options));
if (!during_initial_install()) {
$options = get_default_enrol_roles(context_system::instance());
$student = get_archetype_roles('student');
$student = reset($student);
$settings->add(new admin_setting_configselect('enrol_self/roleid',
get_string('defaultrole', 'enrol_self'),
get_string('defaultrole_desc', 'enrol_self'),
$student->id ?? null,
$options));
}
$settings->add(new admin_setting_configduration('enrol_self/enrolperiod',
get_string('enrolperiod', 'enrol_self'), get_string('enrolperiod_desc', 'enrol_self'), 0));
$options = array(0 => get_string('no'),
1 => get_string('expirynotifyenroller', 'enrol_self'),
2 => get_string('expirynotifyall', 'enrol_self'));
$settings->add(new admin_setting_configselect('enrol_self/expirynotify',
get_string('expirynotify', 'core_enrol'), get_string('expirynotify_help', 'core_enrol'), 0, $options));
$settings->add(new admin_setting_configduration('enrol_self/expirythreshold',
get_string('expirythreshold', 'core_enrol'), get_string('expirythreshold_help', 'core_enrol'), 86400, 86400));
$options = array(0 => get_string('never'),
1800 * 3600 * 24 => get_string('numdays', '', 1800),
1000 * 3600 * 24 => get_string('numdays', '', 1000),
365 * 3600 * 24 => get_string('numdays', '', 365),
180 * 3600 * 24 => get_string('numdays', '', 180),
150 * 3600 * 24 => get_string('numdays', '', 150),
120 * 3600 * 24 => get_string('numdays', '', 120),
90 * 3600 * 24 => get_string('numdays', '', 90),
60 * 3600 * 24 => get_string('numdays', '', 60),
30 * 3600 * 24 => get_string('numdays', '', 30),
21 * 3600 * 24 => get_string('numdays', '', 21),
14 * 3600 * 24 => get_string('numdays', '', 14),
7 * 3600 * 24 => get_string('numdays', '', 7));
$settings->add(new admin_setting_configselect('enrol_self/longtimenosee',
get_string('longtimenosee', 'enrol_self'), get_string('longtimenosee_help', 'enrol_self'), 0, $options));
$settings->add(new admin_setting_configtext('enrol_self/maxenrolled',
get_string('maxenrolled', 'enrol_self'), get_string('maxenrolled_help', 'enrol_self'), 0, PARAM_INT));
$settings->add(new admin_setting_configselect('enrol_self/sendcoursewelcomemessage',
get_string('sendcoursewelcomemessage', 'enrol_self'),
get_string('sendcoursewelcomemessage_help', 'enrol_self'),
ENROL_SEND_EMAIL_FROM_COURSE_CONTACT,
enrol_send_welcome_email_options()));
}
+44
View File
@@ -0,0 +1,44 @@
@enrol @enrol_self
Feature: Users can be defined as key holders in courses where self enrolment is allowed
In order to participate in courses
As a user
I need to auto enrol me in courses
Background:
Given the following "roles" exist:
| shortname | name | context_course | enrol/self:holdkey |
| keyholder | Key holder | 1 | allow |
And the following "users" exist:
| username | firstname | lastname | email |
| manager1 | Manager | 1 | manager1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And I log in as "admin"
And I navigate to "Appearance > Courses" in site administration
And I set the following fields to these values:
| Key holder | 1 |
And I press "Save changes"
And the following "course enrolments" exist:
| user | course | role |
| manager1 | C1 | keyholder |
And I log out
@javascript
Scenario: The key holder name is displayed on site home page
Given I log in as "admin"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
| Enrolment key | moodle_rules |
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I should see "You should have received this enrolment key from:"
And I should see "Manager 1"
And I set the following fields to these values:
| Enrolment key | moodle_rules |
And I press "Enrol me"
Then I should see "New section"
And I should not see "Enrolment options"
And I should not see "Enrol me in this course"
@@ -0,0 +1,169 @@
@enrol @enrol_self
Feature: Users can auto-enrol themself in courses where self enrolment is allowed
In order to participate in courses
As a user
I need to auto enrol me in courses
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@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 |
# Note: Please keep the javascript tag on this Scenario to ensure that we
# test use of the singleselect functionality.
@javascript
Scenario: Self-enrolment enabled as guest
Given I log in as "teacher1"
And I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
And I log out
When I am on "Course 1" course homepage
And I press "Access as a guest"
Then I should see "Guests cannot access this course. Please log in."
And I press "Continue"
And I should see "Log in"
Scenario: Self-enrolment enabled
Given I log in as "teacher1"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I press "Enrol me"
Then I should see "New section"
And I should not see "Enrolment options"
Scenario: Self-enrolment enabled requiring an enrolment key
Given I log in as "teacher1"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
| Enrolment key | moodle_rules |
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I set the following fields to these values:
| Enrolment key | moodle_rules |
And I press "Enrol me"
Then I should see "New section"
And I should not see "Enrolment options"
And I should not see "Enrol me in this course"
Scenario: Self-enrolment disabled
Given I log in as "student1"
When I am on "Course 1" course homepage
Then I should see "You cannot enrol yourself in this course"
Scenario: Self-enrolment enabled requiring a group enrolment key
Given I log in as "teacher1"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
| Enrolment key | moodle_rules |
| Use group enrolment keys | Yes |
And I am on the "Course 1" "groups" page
And I press "Create group"
And I set the following fields to these values:
| Group name | Group 1 |
| Enrolment key | Test-groupenrolkey1 |
And I press "Save changes"
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I set the following fields to these values:
| Enrolment key | Test-groupenrolkey1 |
And I press "Enrol me"
Then I should see "New section"
And I should not see "Enrolment options"
And I should not see "Enrol me in this course"
And I am on the "Course 1" course page logged in as student2
And I set the following fields to these values:
| Enrolment key | moodle_rules |
And I press "Enrol me"
And I am on the "Course 1" course page logged in as teacher1
And I navigate to course participants
And the following should exist in the "participants" table:
| First name | Email address | Roles | Groups |
| Student 1 | student1@example.com | Student | Group 1 |
| Student 2 | student2@example.com | Student | No groups |
@javascript
Scenario: Edit a self-enrolled user's enrolment from the course participants page
Given I log in as "teacher1"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I press "Enrol me"
And I should see "You are enrolled in the course"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
When I click on "//a[@data-action='editenrolment']" "xpath_element" in the "student1" "table_row"
And I should see "Edit Student 1's enrolment"
And I set the field "Status" to "Suspended"
And I click on "Save changes" "button"
Then I should see "Suspended" in the "student1" "table_row"
@javascript
Scenario: Unenrol a self-enrolled student from the course participants page
Given I log in as "teacher1"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I press "Enrol me"
And I should see "You are enrolled in the course"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
When I click on "//a[@data-action='unenrol']" "xpath_element" in the "student1" "table_row"
And I click on "Unenrol" "button" in the "Unenrol" "dialogue"
Then I should not see "Student 1" in the "participants" "table"
@javascript
Scenario: Self unenrol as a self-enrolled student from the course
Given the "multilang" filter is "on"
And the "multilang" filter applies to "content and headings"
And I am on the "C1" "Course" page logged in as "teacher1"
When I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
And I am on "Course 1" course homepage
And I navigate to "Settings" in current page administration
And I set the field "Course full name" in the "General" "fieldset" to "<span lang=\"en\" class=\"multilang\">Course</span><span lang=\"it\" class=\"multilang\">Corso</span> 1"
And I press "Save and display"
And I log out
And I am on the "C1" "Course" page logged in as "student1"
And I press "Enrol me"
And I should see "You are enrolled in the course"
And I am on the "C1" "course" page
And I navigate to "Unenrol me from this course" in current page administration
And I click on "Continue" "button" in the "Confirm" "dialogue"
Then I should see "You are unenrolled from the course \"Course 1\""
@javascript
Scenario: Self-enrolment enabled with simultaneous guest access
Given I log in as "teacher1"
And I am on the "Course 1" "enrolment methods" page
And I click on "Enable" "link" in the "Self enrolment (Student)" "table_row"
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 changes"
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I navigate to "Enrol me in this course" in current page administration
And I click on "Enrol me" "button"
Then I should see "New section"
@@ -0,0 +1,115 @@
@enrol @enrol_self
Feature: A course welcome message will be sent to the user when they auto-enrol themself in a course
In order to let the user know they have been auto-enrol themself in a course successfully
As a teacher
I want the user to receive a welcome message when they auto-enrol themself in a course
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| manager | Manager | User | manager@example.com |
| teacher | Teacher | User | teacher@example.com |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
| Course 2 | C2 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| manager | C1 | manager |
| teacher | C1 | editingteacher |
| teacher | C2 | editingteacher |
And I log in as "admin"
And I add "Self enrolment" enrolment method in "Course 1" with:
| Custom instance name | Test student enrolment |
And I add "Self enrolment" enrolment method in "Course 2" with:
| Custom instance name | Test student enrolment |
@javascript
Scenario: Manager should see the new settings for course welcome message
Given I am on the "C1" "Enrolled users" page logged in as manager
And I set the field "Participants tertiary navigation" to "Enrolment methods"
When I click on "Edit" "link" in the "Test student enrolment" "table_row"
Then I should see "Send course welcome message"
And the field "Send course welcome message" matches value "From the course contact"
And I should see "Custom welcome message"
And the field "Custom welcome message" matches value "Dear {$a->fullname}, you have successfully been enrolled to course {$a->coursename}"
And I should see "Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:"
And I set the field "Send course welcome message" to "No"
And I should not see "Custom welcome message"
And I should not see "Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:"
@javascript
Scenario: Student should not receive a welcome message if the setting is disabled
Given I am on the "C1" "Enrolled users" page logged in as manager
And I set the field "Participants tertiary navigation" to "Enrolment methods"
And I click on "Edit" "link" in the "Test student enrolment" "table_row"
And I set the field "Send course welcome message" to "No"
And I press "Save changes"
And I log in as "user1"
And I am on "Course 1" course homepage
When I press "Enrol me"
Then I should not see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element"
@javascript
Scenario: Students should receive a welcome message if the setting is enabled - Default message
# Login as first user and check the notification.
Given I log in as "user1"
And I am on "Course 1" course homepage
When I press "Enrol me"
Then I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element"
And I open the notification popover
And I should see "Welcome to Course 1"
And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element"
And I should see "Dear First User, you have successfully been enrolled to course Course 1"
# Login as second user and check the notification.
And I log in as "user2"
And I am on "Course 2" course homepage
And I press "Enrol me"
And I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element"
And I open the notification popover
And I should see "Welcome to Course 2"
And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element"
And I should see "Dear Second User, you have successfully been enrolled to course Course 2"
@javascript
Scenario: Students should receive a welcome message if the setting is enabled - Custom message
Given I am on the "C1" "Enrolled users" page logged in as manager
And I set the field "Participants tertiary navigation" to "Enrolment methods"
And I click on "Edit" "link" in the "Test student enrolment" "table_row"
And I set the field "Custom welcome message" to multiline:
"""
Dear {$a->fullname}, you have successfully been enrolled to course {$a->coursename}.
Your email address: {$a->email}
Your first name: {$a->firstname}
Your last name: {$a->lastname}
Your course role: {$a->courserole}
"""
And I press "Save changes"
# Login as first user and check the notification.
And I log in as "user1"
And I am on "Course 1" course homepage
When I press "Enrol me"
Then I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element"
And I open the notification popover
And I should see "Welcome to Course 1"
And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element"
And I should see "Dear First User, you have successfully been enrolled to course Course 1"
And I should see "Your email address: first@example.com"
And I should see "Your first name: First"
And I should see "Your last name: User"
And I should see "Your course role: student"
# Login as second user and check the notification.
And I log in as "user2"
And I am on "Course 1" course homepage
And I press "Enrol me"
And I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element"
And I open the notification popover
And I should see "Welcome to Course 1"
And I click on "View full notification" "link" in the ".popover-region-notifications" "css_element"
And I should see "Dear Second User, you have successfully been enrolled to course Course 1"
And I should see "Your email address: second@example.com"
And I should see "Your first name: Second"
And I should see "Your last name: User"
And I should see "Your course role: student"
+259
View File
@@ -0,0 +1,259 @@
<?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_self;
use core_external\external_api;
use enrol_self_external;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/enrol/self/externallib.php');
/**
* Self enrol external PHPunit tests
*
* @package enrol_self
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.6
*/
class externallib_test extends externallib_advanced_testcase {
/**
* Test get_instance_info
*/
public function test_get_instance_info(): void {
global $DB;
$this->resetAfterTest(true);
// Check if self enrolment plugin is enabled.
$selfplugin = enrol_get_plugin('self');
$this->assertNotEmpty($selfplugin);
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
$this->assertNotEmpty($studentrole);
$coursedata = new \stdClass();
$coursedata->visible = 0;
$course = self::getDataGenerator()->create_course($coursedata);
// Add enrolment methods for course.
$instanceid1 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance 1',
'customint6' => 1,
'roleid' => $studentrole->id));
$instanceid2 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_DISABLED,
'customint6' => 1,
'name' => 'Test instance 2',
'roleid' => $studentrole->id));
$instanceid3 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
'roleid' => $studentrole->id,
'customint6' => 1,
'name' => 'Test instance 3',
'password' => 'test'));
$enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED));
$this->assertCount(3, $enrolmentmethods);
$this->setAdminUser();
$instanceinfo1 = enrol_self_external::get_instance_info($instanceid1);
$instanceinfo1 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1);
$this->assertEquals($instanceid1, $instanceinfo1['id']);
$this->assertEquals($course->id, $instanceinfo1['courseid']);
$this->assertEquals('self', $instanceinfo1['type']);
$this->assertEquals('Test instance 1', $instanceinfo1['name']);
$this->assertTrue($instanceinfo1['status']);
$this->assertFalse(isset($instanceinfo1['enrolpassword']));
$instanceinfo2 = enrol_self_external::get_instance_info($instanceid2);
$instanceinfo2 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2);
$this->assertEquals($instanceid2, $instanceinfo2['id']);
$this->assertEquals($course->id, $instanceinfo2['courseid']);
$this->assertEquals('self', $instanceinfo2['type']);
$this->assertEquals('Test instance 2', $instanceinfo2['name']);
$this->assertEquals(get_string('canntenrol', 'enrol_self'), $instanceinfo2['status']);
$this->assertFalse(isset($instanceinfo2['enrolpassword']));
$instanceinfo3 = enrol_self_external::get_instance_info($instanceid3);
$instanceinfo3 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3);
$this->assertEquals($instanceid3, $instanceinfo3['id']);
$this->assertEquals($course->id, $instanceinfo3['courseid']);
$this->assertEquals('self', $instanceinfo3['type']);
$this->assertEquals('Test instance 3', $instanceinfo3['name']);
$this->assertTrue($instanceinfo3['status']);
$this->assertEquals(get_string('password', 'enrol_self'), $instanceinfo3['enrolpassword']);
// Try to retrieve information using a normal user for a hidden course.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
enrol_self_external::get_instance_info($instanceid3);
} catch (\moodle_exception $e) {
$this->assertEquals('coursehidden', $e->errorcode);
}
}
/**
* Test enrol_user
*/
public function test_enrol_user(): void {
global $DB;
self::resetAfterTest(true);
$user = self::getDataGenerator()->create_user();
self::setUser($user);
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$selfplugin = enrol_get_plugin('self');
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance 1',
'customint6' => 1,
'roleid' => $studentrole->id));
$instance2id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_DISABLED,
'customint6' => 1,
'name' => 'Test instance 2',
'roleid' => $studentrole->id));
$instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
$instance2 = $DB->get_record('enrol', array('id' => $instance2id), '*', MUST_EXIST);
self::setUser($user1);
// Self enrol me.
$result = enrol_self_external::enrol_user($course1->id);
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertTrue($result['status']);
self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
self::assertTrue(is_enrolled($context1, $user1));
// Add password.
$instance2->password = 'abcdef';
$DB->update_record('enrol', $instance2);
// Try instance not enabled.
try {
enrol_self_external::enrol_user($course2->id);
} catch (\moodle_exception $e) {
self::assertEquals('canntenrol', $e->errorcode);
}
// Enable the instance.
$selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED);
// Try not passing a key.
$result = enrol_self_external::enrol_user($course2->id);
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertFalse($result['status']);
self::assertCount(1, $result['warnings']);
self::assertEquals('4', $result['warnings'][0]['warningcode']);
// Try passing an invalid key.
$result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertFalse($result['status']);
self::assertCount(1, $result['warnings']);
self::assertEquals('4', $result['warnings'][0]['warningcode']);
// Try passing an invalid key with hint.
$selfplugin->set_config('showhint', true);
$result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertFalse($result['status']);
self::assertCount(1, $result['warnings']);
self::assertEquals('3', $result['warnings'][0]['warningcode']);
// Everything correct, now.
$result = enrol_self_external::enrol_user($course2->id, 'abcdef');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertTrue($result['status']);
self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
self::assertTrue(is_enrolled($context2, $user1));
// Try group password now, other user.
$instance2->customint1 = 1;
$instance2->password = 'zyx';
$DB->update_record('enrol', $instance2);
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enrolmentkey' => 'zyx'));
self::setUser($user2);
// Try passing and invalid key for group.
$result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertFalse($result['status']);
self::assertCount(1, $result['warnings']);
self::assertEquals('2', $result['warnings'][0]['warningcode']);
// Now, everything ok.
$result = enrol_self_external::enrol_user($course2->id, 'zyx');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertTrue($result['status']);
self::assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
self::assertTrue(is_enrolled($context2, $user2));
// Try multiple instances now, multiple errors.
$instance3id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED,
'customint6' => 1,
'name' => 'Test instance 2',
'roleid' => $studentrole->id));
$instance3 = $DB->get_record('enrol', array('id' => $instance3id), '*', MUST_EXIST);
$instance3->password = 'abcdef';
$DB->update_record('enrol', $instance3);
self::setUser($user3);
$result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertFalse($result['status']);
self::assertCount(2, $result['warnings']);
// Now, everything ok.
$result = enrol_self_external::enrol_user($course2->id, 'zyx');
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertTrue($result['status']);
self::assertTrue(is_enrolled($context2, $user3));
// Now test passing an instance id.
self::setUser($user4);
$result = enrol_self_external::enrol_user($course2->id, 'abcdef', $instance3id);
$result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
self::assertTrue($result['status']);
self::assertTrue(is_enrolled($context2, $user3));
self::assertCount(0, $result['warnings']);
self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance3->id)));
}
}
File diff suppressed because it is too large Load Diff
+64
View File
@@ -0,0 +1,64 @@
<?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 enrolment plugin - support for user self unenrolment.
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
$enrolid = required_param('enrolid', PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$instance = $DB->get_record('enrol', array('id'=>$enrolid, 'enrol'=>'self'), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id, MUST_EXIST);
require_login();
if (!is_enrolled($context)) {
redirect(new moodle_url('/'));
}
require_login($course);
$plugin = enrol_get_plugin('self');
// Security defined inside following function.
if (!$plugin->get_unenrolself_link($instance)) {
redirect(new moodle_url('/course/view.php', array('id'=>$course->id)));
}
$PAGE->set_url('/enrol/self/unenrolself.php', array('enrolid'=>$instance->id));
$PAGE->set_title($plugin->get_instance_name($instance));
if ($confirm and confirm_sesskey()) {
$plugin->unenrol_user($instance, $USER->id);
\core\notification::success(get_string('youunenrolledfromcourse', 'enrol', format_string($course->fullname, true,
["context" => $context])));
redirect(new moodle_url('/index.php'));
}
echo $OUTPUT->header();
$yesurl = new moodle_url($PAGE->url, array('confirm'=>1, 'sesskey'=>sesskey()));
$nourl = new moodle_url('/course/view.php', array('id'=>$course->id));
$message = get_string('unenrolselfconfirm', 'enrol_self', format_string($course->fullname));
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
+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/>.
/**
* Self enrolment plugin version specification.
*
* @package enrol_self
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'enrol_self'; // Full name of the plugin (used for diagnostics)