2024-05-12 23:34:48 +02:00
|
|
|
<?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/>.
|
|
|
|
/**
|
|
|
|
* Background task to refresh the list of associaded teachers with studyplans
|
|
|
|
* @package tool_sptoolboxmgr
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace tool_sptoolboxmgr\task;
|
|
|
|
|
2024-05-13 21:56:41 +02:00
|
|
|
use \local_treestudyplan\associationservice;
|
|
|
|
use \tool_sptoolboxmgr\profilehelper;
|
2024-05-12 23:34:48 +02:00
|
|
|
use \mtrace;
|
|
|
|
use \mtrace_exception;
|
|
|
|
use \ValueError;
|
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
require_once($CFG->dirroot.'/user/profile/lib.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background task to process changes in
|
|
|
|
*/
|
|
|
|
class profilescannertask extends \core\task\scheduled_task {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the task's name as shown in admin screens.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_name() {
|
|
|
|
return get_string('profilescannertask', 'tool_sptoolboxmgr');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the task.
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
global $DB;
|
|
|
|
// We need to test for changes in the profile fields...
|
|
|
|
|
|
|
|
// First check the userids eligable for enrolment
|
|
|
|
$enrollable = profilehelper::enrollable_user_ids();
|
|
|
|
|
|
|
|
// Next, check each of these fields for changes
|
|
|
|
mtrace("Checking ".count($enrollable)." enrollable uids for changes ....");
|
|
|
|
foreach ($enrollable as $uid) {
|
|
|
|
$user = $DB->get_record("user",["id"=>$uid]);
|
|
|
|
try {
|
|
|
|
$changes = profilehelper::check_changes($uid);
|
|
|
|
mtrace("Checking user {$user->username}... ".(($changes->changed)?"Has changes":"No changes"));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var studyplan
|
|
|
|
*/
|
|
|
|
$toolbox = null;
|
|
|
|
if ($changes->trackchanged) {
|
|
|
|
if ($changes->toolbox) {
|
|
|
|
mtrace(" User changed track - removing coaches from old toolbox '{$changes->toolbox->name()}' ");
|
|
|
|
// Remove coach from old toolbox.
|
|
|
|
$coaches = associationservice::associated_coaches($changes->toolbox->id());
|
|
|
|
foreach ($coaches as $coach) {
|
|
|
|
$coach = (object)$coach; // Convert from array to object.
|
|
|
|
associationservice::disconnect_coach($changes->toolbox->id(),$coach->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Create new toolbox based on new track and add coach.
|
|
|
|
$template = profilehelper::get_track_plan($changes->track);
|
|
|
|
|
|
|
|
mtrace(" Created new toolbox");
|
|
|
|
$toolbox = $template->duplicate(
|
|
|
|
"{$user->firstname} {$user->lastname} - {$template->name()}",
|
|
|
|
"{$user->username}-{$template->shortname()}",
|
|
|
|
$template->context()->id,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
mtrace(" Creating new toolbox - Created {$toolbox->id()} - {$toolbox->name()}");
|
|
|
|
|
|
|
|
// Add user to toolbox.
|
|
|
|
associationservice::connect_user($toolbox->id(),$uid);
|
|
|
|
mtrace(" Creating new toolbox - Added user '{$user->username}' to toolbox");
|
|
|
|
// Add new coach to toolbox.
|
|
|
|
$coach = $DB->get_record("user", ["id"=> $changes->coach]);
|
|
|
|
mtrace(" Creating new toolbox - adding coach '{$coach->username}' to toolbox");
|
|
|
|
associationservice::connect_coach($toolbox->id(), $changes->coach);
|
|
|
|
} catch (ValueError $x) {
|
|
|
|
mtrace("Cannot find track template '{$changes->track}' for user '{$user->username}'");
|
|
|
|
mtrace_exception($x);
|
|
|
|
}
|
|
|
|
} elseif ($changes->coachchanged || $changes->enrolchanged) {
|
|
|
|
if ($changes->toolbox) { // If there is an associated toolbox. Otherwise, it should have been created above.
|
|
|
|
$toolbox = $changes->toolbox;
|
|
|
|
|
|
|
|
if ($changes->coachchanged) {
|
|
|
|
// Remove coach from old toolbox.
|
|
|
|
$coaches = associationservice::associated_coaches($changes->toolbox->id());
|
|
|
|
foreach ($coaches as $coach) {
|
|
|
|
$coach = (object)$coach; // Convert from array to object.
|
|
|
|
mtrace(" Coach changed - removing coach {$coach->username} from toolbox");
|
|
|
|
associationservice::disconnect_coach($changes->toolbox->id(),$coach->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add new coach to toolbox.
|
|
|
|
$coach = $DB->get_record("user", ["id"=> $changes->coach]);
|
|
|
|
mtrace(" Coach or enrol changed - (re)adding coach {$coach->username} to toolbox");
|
2024-05-13 17:52:40 +02:00
|
|
|
$result = (object)associationservice::connect_coach($toolbox->id(), $changes->coach);
|
|
|
|
if (! $result->success) {
|
|
|
|
$ci = new \local_treestudyplan\contextinfo($toolbox->context());
|
|
|
|
mtrace(" Error: {$result->msg} - Toolbox '".$toolbox->name()."' context: ". $ci->pathstr());
|
|
|
|
}
|
2024-05-12 23:34:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mtrace("Registering new status");
|
|
|
|
profilehelper::register_changes($uid,$toolbox);
|
|
|
|
|
|
|
|
} catch (\ValueError $x) {
|
|
|
|
// This is thrown if any of the fields are invalid.
|
|
|
|
|
|
|
|
mtrace("Error retrieving fields for user {$user->username}");
|
|
|
|
mtrace_exception($x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next process the previously enrollable uids
|
|
|
|
$prevenrollable = profilehelper::previously_enrollable_user_ids();
|
|
|
|
|
|
|
|
// Next, check each of these fields for changes
|
|
|
|
mtrace("Processing ".count($prevenrollable)." previously enrollable uids for the change in enrollability");
|
|
|
|
foreach ($enrollable as $uid) {
|
|
|
|
try {
|
|
|
|
$changes = profilehelper::check_changes($uid);
|
|
|
|
mtrace("Checking user {$user->username}... ".(($changes->changed)?"Has changes":"No changes"));
|
|
|
|
// There should be changes in the enrol now, since all of these uids are no longer eligable for an active toolbox.
|
|
|
|
// Do not remove the old toolbox, but do remove the coach access - if
|
|
|
|
if ($changes->enrolchanged) {
|
|
|
|
// Remove coach from old toolbox.
|
|
|
|
$coaches = associationservice::associated_coaches($changes->toolbox->id());
|
|
|
|
foreach ($coaches as $coach) {
|
|
|
|
$coach = (object)$coach; // Convert from array to object.
|
|
|
|
mtrace(" User enrol changed - removing coach {$coach->username} from toolbox");
|
|
|
|
associationservice::disconnect_coach($changes->toolbox->id(),$coach->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtrace("Registering new status");
|
|
|
|
profilehelper::register_changes($uid,$changes->toolbox);
|
|
|
|
|
|
|
|
} catch (\ValueError $x) {
|
|
|
|
// This is thrown if any of the fields are invalid.
|
|
|
|
|
|
|
|
mtrace("Error retrieving fields for user {$user->username}");
|
|
|
|
mtrace_exception($x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|