1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Adhoc task handling course module deletion.
|
|
|
19 |
*
|
|
|
20 |
* @package core_course
|
|
|
21 |
* @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_course\task;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
/**
|
|
|
29 |
* Class handling course module deletion.
|
|
|
30 |
*
|
|
|
31 |
* This task supports an array of course module object as custom_data, and calls course_delete_module() in synchronous deletion
|
|
|
32 |
* mode for each of them.
|
|
|
33 |
* This will:
|
|
|
34 |
* 1. call any 'mod_xxx_pre_course_module_deleted' functions (e.g. Recycle bin)
|
|
|
35 |
* 2. delete the module
|
|
|
36 |
* 3. fire the deletion event
|
|
|
37 |
*
|
|
|
38 |
* @package core_course
|
|
|
39 |
* @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
*/
|
|
|
42 |
class course_delete_modules extends \core\task\adhoc_task {
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Run the deletion task.
|
|
|
46 |
*
|
|
|
47 |
* @throws \coding_exception if the module could not be removed.
|
|
|
48 |
*/
|
|
|
49 |
public function execute() {
|
|
|
50 |
global $CFG;
|
|
|
51 |
require_once($CFG->dirroot. '/course/lib.php');
|
|
|
52 |
|
|
|
53 |
// Set the proper user.
|
|
|
54 |
if ($this->get_custom_data()->userid !== $this->get_custom_data()->realuserid) {
|
|
|
55 |
$realuser = \core_user::get_user($this->get_custom_data()->realuserid, '*', MUST_EXIST);
|
|
|
56 |
\core\cron::setup_user($realuser);
|
|
|
57 |
\core\session\manager::loginas($this->get_custom_data()->userid, \context_system::instance(), false);
|
|
|
58 |
} else {
|
|
|
59 |
$user = \core_user::get_user($this->get_custom_data()->userid, '*', MUST_EXIST);
|
|
|
60 |
\core\cron::setup_user($user);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$cms = $this->get_custom_data()->cms;
|
|
|
64 |
$exceptions = [];
|
|
|
65 |
$cmsfailed = [];
|
|
|
66 |
foreach ($cms as $key => $cm) {
|
|
|
67 |
try {
|
|
|
68 |
course_delete_module($cm->id);
|
|
|
69 |
} catch (\Exception $e) {
|
|
|
70 |
// Keep the information instead of throw an exception and continue with next cms.
|
|
|
71 |
$exceptions[] = ("The course module {$cm->id} could not be deleted. "
|
|
|
72 |
. "{$e->getMessage()}: {$e->getFile()}({$e->getLine()}) {$e->getTraceAsString()}");
|
|
|
73 |
// Save the cms that has failed to set the data only with this values.
|
|
|
74 |
$cmsfailed[$key] = $cm;
|
|
|
75 |
continue;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// Throw the existing exceptions if there is any.
|
|
|
80 |
if (!empty($exceptions)) {
|
|
|
81 |
// Save the failed CMS.
|
|
|
82 |
$customdata = $this->get_custom_data();
|
|
|
83 |
$customdata->cms = $cmsfailed;
|
|
|
84 |
$this->set_custom_data($customdata);
|
|
|
85 |
|
|
|
86 |
throw new \coding_exception("The following course modules could not be deleted:\n " .
|
|
|
87 |
implode('\n', $exceptions));
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Sets attemptsavailable to false.
|
|
|
93 |
*
|
|
|
94 |
* @return boolean
|
|
|
95 |
*/
|
|
|
96 |
public function retry_until_success(): bool {
|
|
|
97 |
return false;
|
|
|
98 |
}
|
|
|
99 |
}
|