Commit 44eb1490 authored by David Monllaó's avatar David Monllaó
Browse files

MDL-54751 course: Set adhoc task user

We should complete the deletion process using the same
user that started it.

Added a new param to loginas() to prevent the event to be generated as
there is no need to generate an new event for that as the user didn't
explicitly loginas again.
parent a584a403
......@@ -50,12 +50,22 @@ class course_delete_modules extends \core\task\adhoc_task {
global $CFG;
require_once($CFG->dirroot. '/course/lib.php');
// Set the proper user.
if ($this->get_custom_data()->userid !== $this->get_custom_data()->realuserid) {
$realuser = \core_user::get_user($this->get_custom_data()->realuserid, '*', MUST_EXIST);
cron_setup_user($realuser);
\core\session\manager::loginas($this->get_custom_data()->userid, \context_system::instance(), false);
} else {
$user = \core_user::get_user($this->get_custom_data()->userid, '*', MUST_EXIST);
cron_setup_user($user);
}
$cms = $this->get_custom_data()->cms;
foreach ($cms as $cm) {
try {
course_delete_module($cm->id);
} catch (\Exception $e) {
throw new \coding_exception("The course module {$cm->id} could not be deleted. $e->getTraceAsString()");
throw new \coding_exception("The course module {$cm->id} could not be deleted. {$e->getTraceAsString()}");
}
}
}
......
......@@ -1220,7 +1220,7 @@ function course_delete_module($cmid, $async = false) {
* @throws \moodle_exception
*/
function course_module_flag_for_async_deletion($cmid) {
global $CFG, $DB;
global $CFG, $DB, $USER;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->dirroot.'/blog/lib.php');
......@@ -1262,7 +1262,11 @@ function course_module_flag_for_async_deletion($cmid) {
// Create an adhoc task for the deletion of the course module. The task takes an array of course modules for removal.
$removaltask = new \core_course\task\course_delete_modules();
$removaltask->set_custom_data(array('cms' => array($cm)));
$removaltask->set_custom_data(array(
'cms' => array($cm),
'userid' => $USER->id,
'realuserid' => \core\session\manager::get_realuser()->id
));
// Queue the task for the next run.
\core\task\manager::queue_adhoc_task($removaltask);
......@@ -1466,7 +1470,7 @@ function course_delete_section($course, $section, $forcedeleteifnotempty = true,
* @return bool true if the section was scheduled for deletion, false otherwise.
*/
function course_delete_section_async($section, $forcedeleteifnotempty = true) {
global $DB;
global $DB, $USER;
// Objects only, and only valid ones.
if (!is_object($section) || empty($section->id)) {
......@@ -1504,7 +1508,9 @@ function course_delete_section_async($section, $forcedeleteifnotempty = true) {
// Create and queue an adhoc task for the deletion of the modules.
$removaltask = new \core_course\task\course_delete_modules();
$data = array(
'cms' => $affectedmods
'cms' => $affectedmods,
'userid' => $USER->id,
'realuserid' => \core\session\manager::get_realuser()->id
);
$removaltask->set_custom_data($data);
\core\task\manager::queue_adhoc_task($removaltask);
......
......@@ -830,9 +830,10 @@ class manager {
* Login as another user - no security checks here.
* @param int $userid
* @param \context $context
* @param bool $generateevent Set to false to prevent the loginas event to be generated
* @return void
*/
public static function loginas($userid, \context $context) {
public static function loginas($userid, \context $context, $generateevent = true) {
global $USER;
if (self::is_loggedinas()) {
......@@ -854,21 +855,27 @@ class manager {
// Let enrol plugins deal with new enrolments if necessary.
enrol_check_plugins($user);
// Create event before $USER is updated.
$event = \core\event\user_loggedinas::create(
array(
'objectid' => $USER->id,
'context' => $context,
'relateduserid' => $userid,
'other' => array(
'originalusername' => fullname($USER, true),
'loggedinasusername' => fullname($user, true)
if ($generateevent) {
// Create event before $USER is updated.
$event = \core\event\user_loggedinas::create(
array(
'objectid' => $USER->id,
'context' => $context,
'relateduserid' => $userid,
'other' => array(
'originalusername' => fullname($USER, true),
'loggedinasusername' => fullname($user, true)
)
)
)
);
);
}
// Set up global $USER.
\core\session\manager::set_user($user);
$event->trigger();
if ($generateevent) {
$event->trigger();
}
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment