This commit is contained in:
Sara Arjona 2023-06-07 12:42:57 +02:00
commit 357ad39138
5 changed files with 85 additions and 2 deletions

View File

@ -179,6 +179,7 @@ class api {
AND ra.roleid = :roleid
AND ra.userid = :userid
WHERE cm.cohortid ' . $cohortsql . '
AND u.deleted = 0
AND ra.id IS NULL';
$toadd = $DB->get_records_sql($sql, $params);

View File

@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace tool_cohortroles;
use core\event\user_deleted;
/**
* Plugin event observer callbacks
*
* @package tool_cohortroles
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class observers {
/**
* User deleted event, remove cohort role assignments specific to them
*
* @param user_deleted $event
*/
public static function user_deleted(user_deleted $event): void {
$cohortroleassignments = cohort_role_assignment::get_records(['userid' => $event->objectid]);
foreach ($cohortroleassignments as $cohortroleassignment) {
$cohortroleassignment->delete();
}
}
}

View File

@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
/**
* Plugin event observers
*
* @package tool_cohortroles
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$observers = [
[
'eventname' => '\core\event\user_deleted',
'callback' => '\tool_cohortroles\observers::user_deleted',
],
];

View File

@ -53,5 +53,13 @@ function xmldb_tool_cohortroles_upgrade($oldversion) {
// Automatically generated Moodle v4.2.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2023042401) {
// Delete any tool_cohortroles mappings for users who no longer exist.
$DB->delete_records_select('tool_cohortroles', 'userid NOT IN (SELECT id FROM {user} WHERE deleted = 0)');
// Cohortroles savepoint reached.
upgrade_plugin_savepoint(true, 2023042401, 'tool', 'cohortroles');
}
return true;
}

View File

@ -24,7 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2023042400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023042401; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2023041800; // Requires this Moodle version.
$plugin->component = 'tool_cohortroles'; // Full name of the plugin (used for diagnostics).