moodle_local_treestudyplan/classes/task/autocohortsync.php

108 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2023-06-26 11:40:49 +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/>.
2024-07-19 12:31:26 +02:00
2023-08-24 23:02:41 +02:00
/**
2023-08-27 15:12:54 +02:00
* Task to autosync studyplan associated cohorts and users with the courses in the studyplan
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-26 11:40:49 +02:00
namespace local_treestudyplan\task;
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
2023-06-26 11:40:49 +02:00
require_once($CFG->dirroot.'/course/externallib.php');
use local_treestudyplan\studyplan;
use local_treestudyplan\studyline;
2023-06-26 21:44:31 +02:00
use local_treestudyplan\cascadecohortsync;
2023-06-30 12:14:11 +02:00
use local_treestudyplan\cascadeusersync;
2023-06-26 11:40:49 +02:00
2023-08-27 15:12:54 +02:00
/**
2023-08-27 22:20:17 +02:00
* Task to enrol users and cohorts associated with a studyplan in all courses linked in that studyplan
2023-08-27 15:12:54 +02:00
*/
2023-08-25 13:04:19 +02:00
class autocohortsync extends \core\task\scheduled_task {
2023-06-26 11:40:49 +02:00
/**
* Return the task's name as shown in admin screens.
*
* @return string
*/
public function get_name() {
return get_string('autocohortsync_name', 'local_treestudyplan');
}
/**
* Execute the task.
*/
public function execute() {
2023-08-24 23:02:41 +02:00
if (get_config("local_treestudyplan", "csync_enable")) {
2023-06-27 07:33:27 +02:00
\mtrace("Automatic csync cascading enabled");
2023-06-26 11:40:49 +02:00
$studyplans = studyplan::find_all();
2023-08-24 23:02:41 +02:00
foreach ($studyplans as $studyplan) {
// Only process studyplans that have been marked for change because .
2023-08-25 09:44:34 +02:00
// A cohort change has occurred or a course has been added....
2023-08-24 23:02:41 +02:00
if ($studyplan->has_csync_changed()) {
2023-06-27 07:33:27 +02:00
\mtrace("Studyplan {$studyplan->shortname()} needs processing");
$enroller = new cascadecohortsync($studyplan);
$enroller->sync();
2023-08-24 23:02:41 +02:00
if (get_config("local_treestudyplan", "csync_users")) {
2023-06-30 12:14:11 +02:00
$userenroller = new cascadeusersync($studyplan);
$userenroller->sync();
}
2023-06-27 07:33:27 +02:00
$studyplan->clear_csync_changed();
2023-06-30 12:14:11 +02:00
2023-08-24 23:02:41 +02:00
}
2023-06-26 11:40:49 +02:00
}
2023-06-27 07:33:27 +02:00
\mtrace("Task done");
} else {
\mtrace("Automatic csync cascading disabled");
2023-06-26 11:40:49 +02:00
}
}
2024-06-02 19:23:40 +02:00
/**
* Perform immediate syncronization on a single studyplan.
2024-06-03 04:00:46 +02:00
* @param studyplan $studyplan Relevant study plan
*/
public static function syncplan(studyplan $studyplan) {
$enroller = new cascadecohortsync($studyplan);
$enroller->sync();
if (get_config("local_treestudyplan", "csync_users")) {
$userenroller = new cascadeusersync($studyplan);
$userenroller->sync();
}
$studyplan->clear_csync_changed(); // Clear the csync required flag.
}
2024-06-02 19:23:40 +02:00
/**
* Perform immediate syncronization on a single studyline.
2024-06-03 04:00:46 +02:00
* @param studyline $line Relevant study line
*/
public static function syncline(studyline $line) {
$plan = $line->studyplan();
$enroller = new cascadecohortsync($plan);
$enroller->sync($line);
if (get_config("local_treestudyplan", "csync_users")) {
$userenroller = new cascadeusersync($plan);
$userenroller->sync($line);
}
2024-06-02 23:23:32 +02:00
// Leave the csync required flag for the next auto update.
}
2023-08-25 11:52:05 +02:00
}