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
+164
View File
@@ -0,0 +1,164 @@
<?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_meta\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use invalid_parameter_exception;
use context_course;
use moodle_exception;
/**
* Web service function relating to add enrol meta instances
*
* @package enrol_meta
* @copyright 2021 WKS KV Bildung
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_instances extends external_api {
/**
* Parameters for adding meta enrolment instances
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'instances' => new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is added.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
'creategroup' => new external_value(PARAM_BOOL,
'Creates group in meta course named after linked course and '
. 'puts all enrolled users in this group', VALUE_DEFAULT, false),
]
), 'List of course meta enrolment instances to create.', VALUE_DEFAULT, []
),
]);
}
/**
* Adding meta enrolment instances
*
* @param array $instances
* @return array
*/
public static function execute(array $instances): array {
global $DB;
// Parameter validation.
$params = self::validate_parameters(self::execute_parameters(), [
'instances' => $instances,
]);
if (!count($params['instances'])) {
throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta'));
}
$result = [];
foreach ($params['instances'] as $instance) {
// Ensure the metacourse exists.
$metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible');
if (!$metacourserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}
// Ensure the current user is allowed to access metacourse.
$contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING);
try {
self::validate_context($contextmeta);
require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}
// Ensure the linked course exists.
$courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible');
if (!$courserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
}
// Ensure the current user is allowed to access linked course.
$context = context_course::instance($instance['courseid'], IGNORE_MISSING);
try {
self::validate_context($context);
if (!$courserecord->visible) {
require_capability('moodle/course:viewhiddencourses', $context);
}
require_capability('enrol/meta:selectaslinked', $context);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
}
// Check for existing meta course link.
$enrolrecord = $DB->get_record('enrol',
['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]);
if ($enrolrecord) {
// Link exists.
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => false,
];
continue;
}
// Check for permission to create group.
if ($instance['creategroup']) {
try {
require_capability('moodle/course:managegroups', $context);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wscannotcreategroup', 'enrol_meta', $instance['courseid']));
}
}
// Create instance.
$enrolplugin = enrol_get_plugin('meta');
$fields = [
'customint1' => $instance['courseid'],
'customint2' => $instance['creategroup'] ? ENROL_META_CREATE_GROUP : 0,
];
$addresult = $enrolplugin->add_instance($metacourserecord, $fields);
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => (bool) $addresult,
];
}
return $result;
}
/**
* Return for adding enrolment instances.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is added.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
'status' => new external_value(PARAM_BOOL, 'True on success, false if link already exists.'),
]
), 'List of course meta enrolment instances that were created.', VALUE_DEFAULT, []
);
}
}
+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/>.
namespace enrol_meta\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use invalid_parameter_exception;
use context_course;
use moodle_exception;
/**
* Web service function relating to add enrol meta instances
*
* @package enrol_meta
* @copyright 2021 WKS KV Bildung
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_instances extends external_api {
/**
* Parameters for deleting meta enrolment instances
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'instances' => new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course with meta enrolment.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
]
), 'List of course meta enrolment instances to delete.', VALUE_DEFAULT, []
),
]);
}
/**
* Deleting meta enrolment instances
*
* @param array $instances
* @return array
*/
public static function execute(array $instances): array {
global $DB;
// Parameter validation.
$params = self::validate_parameters(self::execute_parameters(), [
'instances' => $instances,
]);
if (!count($params['instances'])) {
throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta'));
}
$result = [];
foreach ($params['instances'] as $instance) {
// Ensure the metacourse exists.
$metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible');
if (!$metacourserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}
// Ensure the current user is allowed to access metacourse.
$contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING);
try {
self::validate_context($contextmeta);
require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta);
} catch (moodle_exception $e) {
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
}
// Ensure the linked course exists.
$courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible');
if (!$courserecord) {
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
}
// It is probably needed to check if user is allowed to access linked course.
// but can_delete_instance does not do that, so we stop permission check here.
// Check for existing meta course link.
$enrolrecord = $DB->get_record('enrol',
['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]);
if ($enrolrecord) {
// Link exists. Delete instance.
$enrolplugin = enrol_get_plugin('meta');
$enrolplugin->delete_instance($enrolrecord);
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => true,
];
continue;
}
$result[] = [
'metacourseid' => $instance['metacourseid'],
'courseid' => $instance['courseid'],
'status' => false,
];
}
return $result;
}
/**
* Return for deleting enrolment instances.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
[
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is deleted.'),
'courseid' => new external_value(PARAM_RAW, 'ID of the course that was meta linked.'),
'status' => new external_value(PARAM_BOOL, 'True on success, false if meta link did not exist.'),
]
), 'List of course meta enrolment instances that were deleted.', VALUE_DEFAULT, []
);
}
}
+233
View File
@@ -0,0 +1,233 @@
<?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/>.
/**
* Event observer for meta enrolment plugin.
*
* @package enrol_meta
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/enrol/meta/locallib.php');
/**
* Event observer for enrol_meta.
*
* @package enrol_meta
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_meta_observer extends enrol_meta_handler {
/**
* Triggered via user_enrolment_created event.
*
* @param \core\event\user_enrolment_created $event
* @return bool true on success.
*/
public static function user_enrolment_created(\core\event\user_enrolment_created $event) {
if (!enrol_is_enabled('meta')) {
// No more enrolments for disabled plugins.
return true;
}
if ($event->other['enrol'] === 'meta') {
// Prevent circular dependencies - we can not sync meta enrolments recursively.
return true;
}
self::sync_course_instances($event->courseid, $event->relateduserid);
return true;
}
/**
* Triggered via user_enrolment_deleted event.
*
* @param \core\event\user_enrolment_deleted $event
* @return bool true on success.
*/
public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) {
if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}
if ($event->other['enrol'] === 'meta') {
// Prevent circular dependencies - we can not sync meta enrolments recursively.
return true;
}
self::sync_course_instances($event->courseid, $event->relateduserid);
return true;
}
/**
* Triggered via user_enrolment_updated event.
*
* @param \core\event\user_enrolment_updated $event
* @return bool true on success
*/
public static function user_enrolment_updated(\core\event\user_enrolment_updated $event) {
if (!enrol_is_enabled('meta')) {
// No modifications if plugin disabled.
return true;
}
if ($event->other['enrol'] === 'meta') {
// Prevent circular dependencies - we can not sync meta enrolments recursively.
return true;
}
self::sync_course_instances($event->courseid, $event->relateduserid);
return true;
}
/**
* Triggered via role_assigned event.
*
* @param \core\event\role_assigned $event
* @return bool true on success.
*/
public static function role_assigned(\core\event\role_assigned $event) {
if (!enrol_is_enabled('meta')) {
return true;
}
// Prevent circular dependencies - we can not sync meta roles recursively.
if ($event->other['component'] === 'enrol_meta') {
return true;
}
// Only course level roles are interesting.
if (!$parentcontext = context::instance_by_id($event->contextid, IGNORE_MISSING)) {
return true;
}
if ($parentcontext->contextlevel != CONTEXT_COURSE) {
return true;
}
self::sync_course_instances($parentcontext->instanceid, $event->relateduserid);
return true;
}
/**
* Triggered via role_unassigned event.
*
* @param \core\event\role_unassigned $event
* @return bool true on success
*/
public static function role_unassigned(\core\event\role_unassigned $event) {
if (!enrol_is_enabled('meta')) {
// All roles are removed via cron automatically.
return true;
}
// Prevent circular dependencies - we can not sync meta roles recursively.
if ($event->other['component'] === 'enrol_meta') {
return true;
}
// Only course level roles are interesting.
if (!$parentcontext = context::instance_by_id($event->contextid, IGNORE_MISSING)) {
return true;
}
if ($parentcontext->contextlevel != CONTEXT_COURSE) {
return true;
}
self::sync_course_instances($parentcontext->instanceid, $event->relateduserid);
return true;
}
/**
* Triggered via course_deleted event.
*
* @param \core\event\course_deleted $event
* @return bool true on success
*/
public static function course_deleted(\core\event\course_deleted $event) {
global $DB;
if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}
// Does anything want to sync with this parent?
if (!$enrols = $DB->get_records('enrol', array('customint1' => $event->objectid, 'enrol' => 'meta'),
'courseid ASC, id ASC')) {
return true;
}
$plugin = enrol_get_plugin('meta');
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// Simple, just delete this instance which purges all enrolments,
// admins were warned that this is risky setting!
foreach ($enrols as $enrol) {
$plugin->delete_instance($enrol);
}
return true;
}
foreach ($enrols as $enrol) {
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// This makes all enrolments suspended very quickly.
$plugin->update_status($enrol, ENROL_INSTANCE_DISABLED);
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$context = context_course::instance($enrol->courseid);
role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id));
}
}
return true;
}
/**
* Triggered via enrol_instance_updated event.
*
* @param \core\event\enrol_instance_updated $event
* @return boolean
*/
public static function enrol_instance_updated(\core\event\enrol_instance_updated $event) {
global $DB;
if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}
// Does anything want to sync with this parent?
$affectedcourses = $DB->get_fieldset_sql('SELECT DISTINCT courseid FROM {enrol} '.
'WHERE customint1 = ? AND enrol = ?',
array($event->courseid, 'meta'));
foreach ($affectedcourses as $courseid) {
enrol_meta_sync($courseid);
}
return true;
}
}
+144
View File
@@ -0,0 +1,144 @@
<?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_meta.
*
* @package enrol_meta
* @category privacy
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_meta\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\userlist;
/**
* Privacy provider for enrol_meta.
*
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// This plugin stores user data.
\core_privacy\local\metadata\provider,
// This plugin contains user's enrolments.
\core_privacy\local\request\plugin\provider,
// This plugin is capable of determining which users have data within it.
\core_privacy\local\request\core_userlist_provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised item collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_subsystem_link('core_group', [], 'privacy:metadata:core_group');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
return \core_group\privacy\provider::get_contexts_for_group_member($userid, 'enrol_meta');
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!$context instanceof \context_course) {
return;
}
\core_group\privacy\provider::get_group_members_in_context($userlist, 'enrol_meta');
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
if (empty($contextlist)) {
return;
}
foreach ($contextlist as $context) {
if ($context->contextlevel == CONTEXT_COURSE) {
\core_group\privacy\provider::export_groups(
$context,
'enrol_meta',
[get_string('pluginname', 'enrol_meta')]
);
}
}
}
/**
* Delete all use data which matches the specified deletion_criteria.
*
* @param \context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
if (empty($context)) {
return;
}
if ($context->contextlevel == CONTEXT_COURSE) {
// Delete all the associated groups.
\core_group\privacy\provider::delete_groups_for_all_users($context, 'enrol_meta');
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
if (empty($contextlist->count())) {
return;
}
\core_group\privacy\provider::delete_groups_for_user($contextlist, 'enrol_meta');
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
\core_group\privacy\provider::delete_groups_for_users($userlist, 'enrol_meta');
}
}
@@ -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/>.
/**
* Meta sync enrolments task.
*
* @package enrol_meta
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_meta\task;
defined('MOODLE_INTERNAL') || die();
/**
* Meta sync enrolments task.
*
* @package enrol_meta
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_meta_sync extends \core\task\scheduled_task {
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('enrolmetasynctask', 'enrol_meta');
}
/**
* Run task for syncing meta enrolments.
*/
public function execute() {
global $CFG;
require_once("$CFG->dirroot/enrol/meta/locallib.php");
enrol_meta_sync();
}
}