2023-06-30 12:14:11 +02:00
|
|
|
<?php
|
2023-08-24 23:02:41 +02:00
|
|
|
// This file is part of the Studyplan plugin for Moodle
|
|
|
|
//
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2023-06-30 12:14:11 +02:00
|
|
|
namespace local_treestudyplan;
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
use \local_treestudyplan\studyplan;
|
|
|
|
|
|
|
|
class cascadeusersync {
|
|
|
|
private const METHOD = "manual";
|
|
|
|
private $studyplan;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
function __construct(studyplan $studyplan) {
|
2023-06-30 12:14:11 +02:00
|
|
|
$this->studyplan = $studyplan;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
function sync() {
|
2023-06-30 12:14:11 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
/* Explainer:
|
|
|
|
This script uses {enrol}.customtext4 to store a json array of all studyplans that need this cohort sync to exist.
|
|
|
|
Since the cohort-sync enrolment method uses only customint1 and customint2, this is a safe place to store the data.
|
2023-08-24 23:02:41 +02:00
|
|
|
(Should the cohortsync script at any future time be modified to use customtext fields, it is still extremely unlikely that
|
2023-06-30 12:14:11 +02:00
|
|
|
customtext4 will be used.)
|
|
|
|
Because of the overhead involved in keeping an extra table up to date and clean it up if cohort syncs are removed outside of this script,
|
|
|
|
it was determined to be the simplest and cleanest solution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
$enrol = \enrol_get_plugin(self::METHOD);
|
2023-08-24 23:02:41 +02:00
|
|
|
// Find the courses that need to be synced to the associated cohorts.
|
2023-06-30 12:14:11 +02:00
|
|
|
$courseids = $this->studyplan->get_linked_course_ids();
|
|
|
|
// And find the users that are linked to this studyplan.
|
|
|
|
$userids = $this->studyplan->get_linked_user_ids();
|
2023-08-24 23:02:41 +02:00
|
|
|
// Get the roleid to use for synchronizations.
|
|
|
|
$roleid = get_config("local_treestudyplan", "csync_roleid");
|
2023-06-30 12:14:11 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Next, for each course that is linked:.
|
|
|
|
foreach ($courseids as $courseid) {
|
2023-06-30 12:14:11 +02:00
|
|
|
$course = \get_course($courseid);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (count($userids) > 0) {
|
|
|
|
// Get the manual enrol instance for this course.
|
2023-06-30 12:14:11 +02:00
|
|
|
$instanceparams = ['courseid' => $courseid, 'enrol' => 'manual'];
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!($instance = $DB->get_record('enrol', $instanceparams))) {
|
2023-06-30 12:14:11 +02:00
|
|
|
if ($instanceid = $enrol->add_default_instance($course)) {
|
|
|
|
$instance = $DB->get_record('enrol', array('id' => $instanceid));
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Instance not added for some reason, so report an error somewhere.
|
|
|
|
// (or not).
|
|
|
|
$instance = null;
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($instance !== null) {
|
|
|
|
foreach ($userids as $uid) {
|
2023-06-30 12:14:11 +02:00
|
|
|
// Try a manual registration - it will just be updated if it is already there....
|
2023-08-24 23:02:41 +02:00
|
|
|
$enrol->enrol_user($instance, $uid, $roleid);
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// We do not do any autoremoval for user syncs, to avoid students losing access to the course data.
|
|
|
|
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|