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/>.
|
|
|
|
/**
|
2023-08-27 17:00:23 +02:00
|
|
|
* Synchronize enrolled users in courses with users associated with studyplans these courses are in
|
2023-08-24 23:02:41 +02:00
|
|
|
* @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;
|
2023-08-25 12:04:27 +02:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2023-06-30 12:14:11 +02:00
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
2024-06-02 18:47:23 +02:00
|
|
|
use local_treestudyplan\studyplan;
|
2023-06-30 12:14:11 +02:00
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Task class to synchronize enrolled users in courses with users associated with studyplans these courses are in
|
|
|
|
*/
|
2023-06-30 12:14:11 +02:00
|
|
|
class cascadeusersync {
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* The method to use for enrolling students
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-06-30 12:14:11 +02:00
|
|
|
private const METHOD = "manual";
|
2023-08-27 17:00:23 +02:00
|
|
|
/** @var studyplan */
|
2023-06-30 12:14:11 +02:00
|
|
|
private $studyplan;
|
2024-03-08 11:54:39 +01:00
|
|
|
/** @var object */
|
|
|
|
private $enrol;
|
|
|
|
/** @var int */
|
|
|
|
private $roleid;
|
|
|
|
/** @var array */
|
|
|
|
private $userids;
|
2023-06-30 12:14:11 +02:00
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Create a synchronization task for a studyplan
|
|
|
|
* @param studyplan $studyplan The studyplan to enrol students for
|
|
|
|
*/
|
2023-08-25 12:04:27 +02:00
|
|
|
public function __construct(studyplan $studyplan) {
|
2023-06-30 12:14:11 +02:00
|
|
|
$this->studyplan = $studyplan;
|
2024-03-08 11:54:39 +01:00
|
|
|
$this->enrol = \enrol_get_plugin(self::METHOD);
|
|
|
|
// Get the roleid to use for synchronizations.
|
|
|
|
$this->roleid = get_config("local_treestudyplan", "csync_roleid");
|
|
|
|
// And find the users that are linked to this studyplan.
|
|
|
|
$this->userids = $this->studyplan->get_linked_user_ids();
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Enroll all users associated to the studyplan in the courses linked to this studyplan
|
|
|
|
*/
|
2023-08-25 12:04:27 +02:00
|
|
|
public 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-25 11:52:05 +02:00
|
|
|
(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.
|
2023-06-30 12:14:11 +02:00
|
|
|
*/
|
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
// Find the study lines associated to this studyplan.
|
|
|
|
$lines = $this->studyplan->get_all_studylines();
|
2023-06-30 12:14:11 +02:00
|
|
|
// And find the users that are linked to this studyplan.
|
|
|
|
$userids = $this->studyplan->get_linked_user_ids();
|
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
foreach($lines as $line) {
|
|
|
|
$this->syncline($line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
/**
|
2024-03-08 11:54:39 +01:00
|
|
|
* Enroll all cohorts associated to the studyplan in the courses linked to the specified study line
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function syncline(studyline $line) {
|
2024-03-08 11:54:39 +01:00
|
|
|
// Find the courses that need to be synced to the associated cohorts.
|
|
|
|
$courseids = $line->get_linked_course_ids();
|
|
|
|
if ($line->enrollable()) {
|
|
|
|
$lineuids = $line->get_enrolled_userids();
|
|
|
|
// Next, for each course that is linked:.
|
|
|
|
foreach ($courseids as $courseid) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$this->perform_enrol($courseid, $lineuids);
|
2024-03-08 11:54:39 +01:00
|
|
|
// We do not do any autoremoval for user syncs, to avoid students losing access to the course data.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Next, for each course that is linked:.
|
|
|
|
foreach ($courseids as $courseid) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$this->perform_enrol($courseid, $this->userids);
|
2024-03-08 11:54:39 +01:00
|
|
|
// We do not do any autoremoval for user syncs, to avoid students losing access to the course data.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Enrol a list of users into a specific course
|
2024-06-02 19:23:40 +02:00
|
|
|
*
|
|
|
|
*
|
2024-03-08 11:54:39 +01:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
private function perform_enrol($courseid, $userids) {
|
2024-03-08 11:54:39 +01:00
|
|
|
global $DB;
|
|
|
|
$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 = $this->enrol->add_default_instance($course)) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$instance = $DB->get_record('enrol', ['id' => $instanceid]);
|
2024-03-08 11:54:39 +01:00
|
|
|
} else {
|
|
|
|
// Instance not added for some reason, so report an error somewhere.
|
|
|
|
// (or not).
|
|
|
|
$instance = null;
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
2024-03-08 11:54:39 +01:00
|
|
|
}
|
|
|
|
if ($instance !== null) {
|
|
|
|
foreach ($userids as $uid) {
|
|
|
|
// Try a manual registration - it will just be updated if it is already there....
|
|
|
|
$this->enrol->enrol_user($instance, $uid, $this->roleid);
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-06-30 12:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|