102 lines
4.1 KiB
PHP
102 lines
4.1 KiB
PHP
<?php
|
|
// 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/>.
|
|
/**
|
|
* Synchronize enrolled users in courses with users associated with studyplans these courses are in
|
|
* @package local_treestudyplan
|
|
* @copyright 2023 P.M. Kuipers
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
namespace local_treestudyplan;
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
use \local_treestudyplan\studyplan;
|
|
|
|
/**
|
|
* Task class to synchronize enrolled users in courses with users associated with studyplans these courses are in
|
|
*/
|
|
class cascadeusersync {
|
|
/**
|
|
* The method to use for enrolling students
|
|
* @var string
|
|
*/
|
|
private const METHOD = "manual";
|
|
/** @var studyplan */
|
|
private $studyplan;
|
|
|
|
/**
|
|
* Create a synchronization task for a studyplan
|
|
* @param studyplan $studyplan The studyplan to enrol students for
|
|
*/
|
|
public function __construct(studyplan $studyplan) {
|
|
$this->studyplan = $studyplan;
|
|
}
|
|
|
|
|
|
/**
|
|
* Enroll all users associated to the studyplan in the courses linked to this studyplan
|
|
*/
|
|
public function sync() {
|
|
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.
|
|
(Should the cohortsync script at any future time be modified to use customtext fields, it is still extremely
|
|
unlikely that 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);
|
|
// Find the courses that need to be synced to the associated cohorts.
|
|
$courseids = $this->studyplan->get_linked_course_ids();
|
|
// And find the users that are linked to this studyplan.
|
|
$userids = $this->studyplan->get_linked_user_ids();
|
|
// Get the roleid to use for synchronizations.
|
|
$roleid = get_config("local_treestudyplan", "csync_roleid");
|
|
|
|
// Next, for each course that is linked:.
|
|
foreach ($courseids as $courseid) {
|
|
$course = \get_course($courseid);
|
|
if (count($userids) > 0) {
|
|
// Get the manual enrol instance for this course.
|
|
$instanceparams = ['courseid' => $courseid, 'enrol' => 'manual'];
|
|
if (!($instance = $DB->get_record('enrol', $instanceparams))) {
|
|
if ($instanceid = $enrol->add_default_instance($course)) {
|
|
$instance = $DB->get_record('enrol', array('id' => $instanceid));
|
|
} else {
|
|
// Instance not added for some reason, so report an error somewhere.
|
|
// (or not).
|
|
$instance = null;
|
|
}
|
|
}
|
|
if ($instance !== null) {
|
|
foreach ($userids as $uid) {
|
|
// Try a manual registration - it will just be updated if it is already there....
|
|
$enrol->enrol_user($instance, $uid, $roleid);
|
|
}
|
|
}
|
|
}
|
|
|
|
// We do not do any autoremoval for user syncs, to avoid students losing access to the course data.
|
|
|
|
}
|
|
}
|
|
}
|