first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+170
View File
@@ -0,0 +1,170 @@
<?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/>.
/**
* Course copy class.
*
* Handles procesing data submitted by UI copy form
* and sets up the course copy process.
*
* @package core_backup
* @copyright 2020 onward The Moodle Users Association <https://moodleassociation.org/>
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated since Moodle 4.1. Use copy_helper instead
*/
namespace core_backup\copy;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Course copy class.
*
* Handles procesing data submitted by UI copy form
* and sets up the course copy process.
*
* @package core_backup
* @copyright 2020 onward The Moodle Users Association <https://moodleassociation.org/>
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated since Moodle 4.1 MDL-74548 - please use copy_helper instead
* @todo MDL-75022 This class will be deleted in Moodle 4.5
* @see copy_helper
*/
class copy {
/**
* The fields required for copy operations.
*
* @var array
*/
private $copyfields = array(
'courseid', // Course id integer.
'fullname', // Fullname of the destination course.
'shortname', // Shortname of the destination course.
'category', // Category integer ID that contains the destination course.
'visible', // Integer to detrmine of the copied course will be visible.
'startdate', // Integer timestamp of the start of the destination course.
'enddate', // Integer timestamp of the end of the destination course.
'idnumber', // ID of the destination course.
'userdata', // Integer to determine if the copied course will contain user data.
);
/**
* Data required for course copy operations.
*
* @var array
*/
private $copydata = array();
/**
* List of role ids to keep enrolments for in the destination course.
*
* @var array
*/
private $roles = array();
/**
* Constructor for the class.
*
* @param \stdClass $formdata Data from the validated course copy form.
*/
public function __construct(\stdClass $formdata) {
debugging('Class \course_backup\copy\copy is deprecated. Please use the copy_helper class instead.');
$this->copydata = $this->get_copy_data($formdata);
$this->roles = $this->get_enrollment_roles($formdata);
}
/**
* Extract the enrolment roles to keep in the copied course
* from the raw submitted form data.
*
* @param \stdClass $formdata Data from the validated course copy form.
* @return array $keptroles The roles to keep.
*/
private function get_enrollment_roles(\stdClass $formdata): array {
$keptroles = array();
foreach ($formdata as $key => $value) {
if ((substr($key, 0, 5 ) === 'role_') && ($value != 0)) {
$keptroles[] = $value;
}
}
return $keptroles;
}
/**
* Take the validated form data and extract the required information for copy operations.
*
* @param \stdClass $formdata Data from the validated course copy form.
* @return \stdClass $copydata Data required for course copy operations.
* @throws \moodle_exception If one of the required copy fields is missing
*/
private function get_copy_data(\stdClass $formdata): \stdClass {
$copydata = new \stdClass();
foreach ($this->copyfields as $field) {
if (isset($formdata->{$field})) {
$copydata->{$field} = $formdata->{$field};
} else {
throw new \moodle_exception('copyfieldnotfound', 'backup', '', null, $field);
}
}
return $copydata;
}
/**
* Creates a course copy.
* Sets up relevant controllers and adhoc task.
*
* @return array $copyids THe backup and restore controller ids.
* @deprecated since Moodle 4.1 MDL-74548 - please use copy_helper instead.
* @todo MDL-75023 This method will be deleted in Moodle 4.5
* @see copy_helper::process_formdata()
* @see copy_helper::create_copy()
*/
public function create_copy(): array {
debugging('The method \core_backup\copy\copy::create_copy() is deprecated.
Please use the methods provided by copy_helper instead.', DEBUG_DEVELOPER);
$copydata = clone($this->copydata);
$copydata->keptroles = $this->roles;
return \copy_helper::create_copy($copydata);
}
/**
* Get the in progress course copy operations for a user.
*
* @param int $userid User id to get the course copies for.
* @param int $courseid The optional source course id to get copies for.
* @return array $copies Details of the inprogress copies.
* @deprecated since Moodle 4.1 MDL-74548 - please use copy_helper::get_copies() instead.
* @todo MDL-75024 This method will be deleted in Moodle 4.5
* @see copy_helper::get_copies()
*/
public static function get_copies(int $userid, int $courseid=0): array {
debugging('The method \core_backup\copy\copy::get_copies() is deprecated.
Please use copy_helper::get_copies() instead.', DEBUG_DEVELOPER);
return \copy_helper::get_copies($userid, $coursied);
}
}
@@ -0,0 +1,52 @@
<?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/>.
namespace core_backup\hook;
/**
* Get a list of event names which are excluded to trigger from course changes in automated backup.
*
* @package core_backup
* @copyright 2023 Tomo Tsuyuki <tomotsuyuki@catalyst-au.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Get a list of event names which are excluded to trigger from course changes in automated backup.')]
#[\core\attribute\tags('backup')]
final class before_course_modified_check {
/**
* @var string[] Array of event names.
*/
private $events = [];
/**
* Add an array of event names which are excluded to trigger from course changes in automated backup.
*
* @param string $events,... Array of event name strings
*/
public function exclude_events(string ...$events): void {
$this->events = array_merge($this->events, $events);
}
/**
* Get an array of event names which are excluded to trigger from course changes in automated backup.
* This is called after dispatch for the hook and use values to exclude events for backup.
*
* @return array
*/
public function get_excluded_events(): array {
return $this->events;
}
}
+239
View File
@@ -0,0 +1,239 @@
<?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/>.
/**
* Course copy form class.
*
* @package core_backup
* @copyright 2020 onward The Moodle Users Association <https://moodleassociation.org/>
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_backup\output;
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/formslib.php");
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Course copy form class.
*
* @package core_backup
* @copyright 2020 onward The Moodle Users Association <https://moodleassociation.org/>
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class copy_form extends \moodleform {
/**
* Build form for the course copy settings.
*
* {@inheritDoc}
* @see \moodleform::definition()
*/
public function definition() {
global $CFG, $OUTPUT, $USER;
$mform = $this->_form;
$course = $this->_customdata['course'];
$coursecontext = \context_course::instance($course->id);
$courseconfig = get_config('moodlecourse');
$returnto = $this->_customdata['returnto'];
$returnurl = $this->_customdata['returnurl'];
if (empty($course->category)) {
$course->category = $course->categoryid;
}
// Course ID.
$mform->addElement('hidden', 'courseid', $course->id);
$mform->setType('courseid', PARAM_INT);
// Return to type.
$mform->addElement('hidden', 'returnto', null);
$mform->setType('returnto', PARAM_ALPHANUM);
$mform->setConstant('returnto', $returnto);
// Notifications of current copies.
$copies = \copy_helper::get_copies($USER->id, $course->id);
if (!empty($copies)) {
$progresslink = new \moodle_url('/backup/copyprogress.php?', array('id' => $course->id));
$notificationmsg = get_string('copiesinprogress', 'backup', $progresslink->out());
$notification = $OUTPUT->notification($notificationmsg, 'notifymessage');
$mform->addElement('html', $notification);
}
// Return to URL.
$mform->addElement('hidden', 'returnurl', null);
$mform->setType('returnurl', PARAM_LOCALURL);
$mform->setConstant('returnurl', $returnurl);
// Form heading.
$mform->addElement('html', \html_writer::div(get_string('copycoursedesc', 'backup'), 'form-description mb-6'));
// Course fullname.
$mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"');
$mform->addHelpButton('fullname', 'fullnamecourse');
$mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
$mform->setType('fullname', PARAM_TEXT);
// Course shortname.
$mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
$mform->addHelpButton('shortname', 'shortnamecourse');
$mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
$mform->setType('shortname', PARAM_TEXT);
// Course category.
$displaylist = \core_course_category::make_categories_list(\core_course\management\helper::get_course_copy_capabilities());
if (!isset($displaylist[$course->category])) {
// Always keep current category.
$displaylist[$course->category] = \core_course_category::get($course->category, MUST_EXIST, true)->get_formatted_name();
}
$mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
$mform->addRule('category', null, 'required', null, 'client');
$mform->addHelpButton('category', 'coursecategory');
// Course visibility.
$choices = array();
$choices['0'] = get_string('hide');
$choices['1'] = get_string('show');
$mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
$mform->addHelpButton('visible', 'coursevisibility');
$mform->setDefault('visible', $courseconfig->visible);
if (!has_capability('moodle/course:visibility', $coursecontext)) {
$mform->hardFreeze('visible');
$mform->setConstant('visible', $course->visible);
}
// Course start date.
$mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
$mform->addHelpButton('startdate', 'startdate');
$date = (new \DateTime())->setTimestamp(usergetmidnight(time()));
$date->modify('+1 day');
$mform->setDefault('startdate', $date->getTimestamp());
// Course enddate.
$mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
$mform->addHelpButton('enddate', 'enddate');
if (!empty($CFG->enablecourserelativedates)) {
$attributes = [
'aria-describedby' => 'relativedatesmode_warning'
];
if (!empty($course->id)) {
$attributes['disabled'] = true;
}
$relativeoptions = [
0 => get_string('no'),
1 => get_string('yes'),
];
$relativedatesmodegroup = [];
$relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
$relativeoptions, $attributes);
$relativedatesmodegroup[] = $mform->createElement('html', \html_writer::span(get_string('relativedatesmode_warning'),
'', ['id' => 'relativedatesmode_warning']));
$mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
$mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
}
// Course ID number (default to the current course ID number; blank for users who can't change ID numbers).
$mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100" size="10"');
$mform->setDefault('idnumber', $course->idnumber);
$mform->addHelpButton('idnumber', 'idnumbercourse');
$mform->setType('idnumber', PARAM_RAW);
if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
$mform->hardFreeze('idnumber');
$mform->setConstant('idnumber', '');
}
// Keep source course user data.
$mform->addElement('select', 'userdata', get_string('userdata', 'backup'),
[0 => get_string('no'), 1 => get_string('yes')]);
$mform->setDefault('userdata', 0);
$mform->addHelpButton('userdata', 'userdata', 'backup');
$requiredcapabilities = array(
'moodle/restore:createuser', 'moodle/backup:userinfo', 'moodle/restore:userinfo'
);
if (!has_all_capabilities($requiredcapabilities, $coursecontext)) {
$mform->hardFreeze('userdata');
$mform->setConstant('userdata', 0);
}
// Keep manual enrolments.
// Only get roles actually used in this course.
$roles = role_fix_names(get_roles_used_in_context($coursecontext, false), $coursecontext);
// Only add the option if there are roles in this course.
if (!empty($roles) && has_capability('moodle/restore:createuser', $coursecontext)) {
$rolearray = array();
foreach ($roles as $role) {
$roleid = 'role_' . $role->id;
$rolearray[] = $mform->createElement('advcheckbox', $roleid,
$role->localname, '', array('group' => 2), array(0, $role->id));
}
$mform->addGroup($rolearray, 'rolearray', get_string('keptroles', 'backup'), ' ', false);
$mform->addHelpButton('rolearray', 'keptroles', 'backup');
$this->add_checkbox_controller(2);
}
$buttonarray = array();
$buttonarray[] = $mform->createElement('submit', 'submitreturn', get_string('copyreturn', 'backup'));
$buttonarray[] = $mform->createElement('submit', 'submitdisplay', get_string('copyview', 'backup'));
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
}
/**
* Validation of the form.
*
* @param array $data
* @param array $files
* @return array the errors that were found
*/
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
// Add field validation check for duplicate shortname.
$courseshortname = $DB->get_record('course', array('shortname' => $data['shortname']), 'fullname', IGNORE_MULTIPLE);
if ($courseshortname) {
$errors['shortname'] = get_string('shortnametaken', '', $courseshortname->fullname);
}
// Add field validation check for duplicate idnumber.
if (!empty($data['idnumber'])) {
$courseidnumber = $DB->get_record('course', array('idnumber' => $data['idnumber']), 'fullname', IGNORE_MULTIPLE);
if ($courseidnumber) {
$errors['idnumber'] = get_string('courseidnumbertaken', 'error', $courseidnumber->fullname);
}
}
// Validate the dates (make sure end isn't greater than start).
if ($errorcode = course_validate_dates($data)) {
$errors['enddate'] = get_string($errorcode, 'error');
}
return $errors;
}
}
+366
View File
@@ -0,0 +1,366 @@
<?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/>.
/**
* Privacy Subsystem implementation for core_backup.
*
* @package core_backup
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_backup\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use core_privacy\local\request\userlist;
use core_privacy\local\request\approved_userlist;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
/**
* Privacy Subsystem implementation for core_backup.
*
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\subsystem\provider {
/**
* Return the fields which contain personal data.
*
* @param collection $items a reference to the collection to use to store the metadata.
* @return collection the updated collection of metadata items.
*/
public static function get_metadata(collection $items): collection {
$items->link_external_location(
'Backup',
[
'detailsofarchive' => 'privacy:metadata:backup:detailsofarchive'
],
'privacy:metadata:backup:externalpurpose'
);
$items->add_database_table(
'backup_controllers',
[
'operation' => 'privacy:metadata:backup_controllers:operation',
'type' => 'privacy:metadata:backup_controllers:type',
'itemid' => 'privacy:metadata:backup_controllers:itemid',
'timecreated' => 'privacy:metadata:backup_controllers:timecreated',
'timemodified' => 'privacy:metadata:backup_controllers:timemodified'
],
'privacy:metadata:backup_controllers'
);
return $items;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$contextlist = new contextlist();
$sql = "SELECT ctx.id
FROM {backup_controllers} bc
JOIN {context} ctx
ON ctx.instanceid = bc.itemid
AND ctx.contextlevel = :contextlevel
AND bc.type = :type
WHERE bc.userid = :userid";
$params = [
'contextlevel' => CONTEXT_COURSE,
'userid' => $userid,
'type' => 'course',
];
$contextlist->add_from_sql($sql, $params);
$sql = "SELECT ctx.id
FROM {backup_controllers} bc
JOIN {course_sections} c
ON bc.itemid = c.id
AND bc.type = :type
JOIN {context} ctx
ON ctx.instanceid = c.course
AND ctx.contextlevel = :contextlevel
WHERE bc.userid = :userid";
$params = [
'contextlevel' => CONTEXT_COURSE,
'userid' => $userid,
'type' => 'section',
];
$contextlist->add_from_sql($sql, $params);
$sql = "SELECT ctx.id
FROM {backup_controllers} bc
JOIN {context} ctx
ON ctx.instanceid = bc.itemid
AND ctx.contextlevel = :contextlevel
AND bc.type = :type
WHERE bc.userid = :userid";
$params = [
'contextlevel' => CONTEXT_MODULE,
'userid' => $userid,
'type' => 'activity',
];
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Get the list of users within a specific context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if ($context instanceof \context_course) {
$params = ['courseid' => $context->instanceid];
$sql = "SELECT bc.userid
FROM {backup_controllers} bc
WHERE bc.itemid = :courseid
AND bc.type = :typecourse";
$courseparams = ['typecourse' => 'course'] + $params;
$userlist->add_from_sql('userid', $sql, $courseparams);
$sql = "SELECT bc.userid
FROM {backup_controllers} bc
JOIN {course_sections} c
ON bc.itemid = c.id
WHERE c.course = :courseid
AND bc.type = :typesection";
$sectionparams = ['typesection' => 'section'] + $params;
$userlist->add_from_sql('userid', $sql, $sectionparams);
}
if ($context instanceof \context_module) {
$params = [
'cmid' => $context->instanceid,
'typeactivity' => 'activity'
];
$sql = "SELECT bc.userid
FROM {backup_controllers} bc
WHERE bc.itemid = :cmid
AND bc.type = :typeactivity";
$userlist->add_from_sql('userid', $sql, $params);
}
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
if (empty($contextlist->count())) {
return;
}
$user = $contextlist->get_user();
list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
$sql = "SELECT bc.*
FROM {backup_controllers} bc
JOIN {context} ctx
ON ctx.instanceid = bc.itemid AND ctx.contextlevel = :contextlevel
WHERE ctx.id {$contextsql}
AND bc.userid = :userid
ORDER BY bc.timecreated ASC";
$params = ['contextlevel' => CONTEXT_COURSE, 'userid' => $user->id] + $contextparams;
$backupcontrollers = $DB->get_recordset_sql($sql, $params);
self::recordset_loop_and_export($backupcontrollers, 'itemid', [], function($carry, $record) {
$carry[] = [
'operation' => $record->operation,
'type' => $record->type,
'itemid' => $record->itemid,
'timecreated' => transform::datetime($record->timecreated),
'timemodified' => transform::datetime($record->timemodified),
];
return $carry;
}, function($courseid, $data) {
$context = \context_course::instance($courseid);
$finaldata = (object) $data;
writer::with_context($context)->export_data([get_string('backup'), $courseid], $finaldata);
});
}
/**
* Delete all user data which matches the specified context.
* Only dealing with the specific context - not it's child contexts.
*
* @param \context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if ($context instanceof \context_course) {
$sectionsql = "itemid IN (SELECT id FROM {course_sections} WHERE course = ?) AND type = ?";
$DB->delete_records_select('backup_controllers', $sectionsql, [$context->instanceid, \backup::TYPE_1SECTION]);
$DB->delete_records('backup_controllers', ['itemid' => $context->instanceid, 'type' => \backup::TYPE_1COURSE]);
}
if ($context instanceof \context_module) {
$DB->delete_records('backup_controllers', ['itemid' => $context->instanceid, 'type' => \backup::TYPE_1ACTIVITY]);
}
return;
}
/**
* Delete multiple users within a single context.
* Only dealing with the specific context - not it's child contexts.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
if (empty($userlist->get_userids())) {
return;
}
$context = $userlist->get_context();
if ($context instanceof \context_course) {
list($usersql, $userparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$select = "itemid = :itemid AND userid {$usersql} AND type = :type";
$params = $userparams;
$params['itemid'] = $context->instanceid;
$params['type'] = \backup::TYPE_1COURSE;
$DB->delete_records_select('backup_controllers', $select, $params);
$params = $userparams;
$params['course'] = $context->instanceid;
$params['type'] = \backup::TYPE_1SECTION;
$sectionsql = "itemid IN (SELECT id FROM {course_sections} WHERE course = :course)";
$select = $sectionsql . " AND userid {$usersql} AND type = :type";
$DB->delete_records_select('backup_controllers', $select, $params);
}
if ($context instanceof \context_module) {
list($usersql, $userparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$select = "itemid = :itemid AND userid {$usersql} AND type = :type";
$params = $userparams;
$params['itemid'] = $context->instanceid;
$params['type'] = \backup::TYPE_1ACTIVITY;
// Delete activity backup data.
$select = "itemid = :itemid AND type = :type AND userid {$usersql}";
$params = ['itemid' => $context->instanceid, 'type' => 'activity'] + $userparams;
$DB->delete_records_select('backup_controllers', $select, $params);
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
* Only dealing with the specific context - not it's child contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
if (empty($contextlist->count())) {
return;
}
$userid = $contextlist->get_user()->id;
foreach ($contextlist->get_contexts() as $context) {
if ($context instanceof \context_course) {
$select = "itemid = :itemid AND userid = :userid AND type = :type";
$params = [
'userid' => $userid,
'itemid' => $context->instanceid,
'type' => \backup::TYPE_1COURSE
];
$DB->delete_records_select('backup_controllers', $select, $params);
$params = [
'userid' => $userid,
'course' => $context->instanceid,
'type' => \backup::TYPE_1SECTION
];
$sectionsql = "itemid IN (SELECT id FROM {course_sections} WHERE course = :course)";
$select = $sectionsql . " AND userid = :userid AND type = :type";
$DB->delete_records_select('backup_controllers', $select, $params);
}
if ($context instanceof \context_module) {
list($usersql, $userparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$select = "itemid = :itemid AND userid = :userid AND type = :type";
$params = [
'itemid' => $context->instanceid,
'userid' => $userid,
'type' => \backup::TYPE_1ACTIVITY
];
$DB->delete_records_select('backup_controllers', $select, $params);
}
}
}
/**
* Loop and export from a recordset.
*
* @param \moodle_recordset $recordset The recordset.
* @param string $splitkey The record key to determine when to export.
* @param mixed $initial The initial data to reduce from.
* @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
* @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
* @return void
*/
protected static function recordset_loop_and_export(\moodle_recordset $recordset, $splitkey, $initial,
callable $reducer, callable $export) {
$data = $initial;
$lastid = null;
foreach ($recordset as $record) {
if ($lastid && $record->{$splitkey} != $lastid) {
$export($lastid, $data);
$data = $initial;
}
$data = $reducer($data, $record);
$lastid = $record->{$splitkey};
}
$recordset->close();
if (!empty($lastid)) {
$export($lastid, $data);
}
}
}