moodle_local_treestudyplan/classes/cascadeusersync.php

102 lines
4.1 KiB
PHP
Raw Permalink Normal View History

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');
use \local_treestudyplan\studyplan;
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;
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;
}
2023-08-27 22:20:17 +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
*/
$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
}
}
2023-08-25 11:52:05 +02:00
}