mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 19:50:14 +01:00
MDL-63668 core_completion: Add get and delete by approved userlist.
This issue is a part of the MDL-62560 Epic.
This commit is contained in:
parent
cc486e6125
commit
543c465579
@ -27,8 +27,10 @@ namespace core_completion\privacy;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use \core_privacy\local\metadata\collection;
|
||||
use \core_privacy\local\request\transform;
|
||||
use core_privacy\local\request\approved_userlist;
|
||||
use \core_privacy\local\request\contextlist;
|
||||
use \core_privacy\local\request\transform;
|
||||
use \core_privacy\local\request\userlist;
|
||||
|
||||
require_once($CFG->dirroot . '/comment/lib.php');
|
||||
|
||||
@ -98,6 +100,38 @@ class provider implements \core_privacy\local\metadata\provider, \core_privacy\l
|
||||
return [$join, $where, $params];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find users' course completion by context and add to the provided userlist.
|
||||
*
|
||||
* @param userlist $userlist The userlist to add to.
|
||||
*/
|
||||
public static function add_course_completion_users_to_userlist(userlist $userlist) {
|
||||
$params = [
|
||||
'contextid' => $userlist->get_context()->id,
|
||||
'contextcourse' => CONTEXT_COURSE,
|
||||
];
|
||||
|
||||
$sql = "SELECT cmc.userid
|
||||
FROM {context} ctx
|
||||
JOIN {course} c ON ctx.instanceid = c.id
|
||||
JOIN {course_completion_criteria} ccc ON ccc.course = c.id
|
||||
JOIN {course_modules_completion} cmc ON cmc.coursemoduleid = ccc.moduleinstance
|
||||
WHERE ctx.id = :contextid
|
||||
AND ctx.contextlevel = :contextcourse";
|
||||
|
||||
$userlist->add_from_sql('userid', $sql, $params);
|
||||
|
||||
$sql = "SELECT ccc_compl.userid
|
||||
FROM {context} ctx
|
||||
JOIN {course} c ON ctx.instanceid = c.id
|
||||
JOIN {course_completion_criteria} ccc ON ccc.course = c.id
|
||||
JOIN {course_completion_crit_compl} ccc_compl ON ccc_compl.criteriaid = ccc.id
|
||||
WHERE ctx.id = :contextid
|
||||
AND ctx.contextlevel = :contextcourse";
|
||||
|
||||
$userlist->add_from_sql('userid', $sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns activity completion information about a user.
|
||||
*
|
||||
@ -204,4 +238,52 @@ class provider implements \core_privacy\local\metadata\provider, \core_privacy\l
|
||||
$DB->delete_records('course_completions', $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete completion information for users within an approved userlist.
|
||||
*
|
||||
* @param approved_userlist $userlist The approved userlist of users to delete completion information for.
|
||||
* @param int $courseid The course id. Provide this if you want course completion and activity completion deleted.
|
||||
* @param int $cmid The course module id. Provide this if you only want activity completion deleted.
|
||||
*/
|
||||
public static function delete_completion_by_approved_userlist(approved_userlist $userlist, int $courseid = null, int $cmid = null) {
|
||||
global $DB;
|
||||
$userids = $userlist->get_userids();
|
||||
|
||||
if (empty($userids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
list($useridsql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
|
||||
|
||||
if (isset($cmid)) {
|
||||
$params['coursemoduleid'] = $cmid;
|
||||
|
||||
// Only delete the record for course modules completion.
|
||||
$sql = "coursemoduleid = :coursemoduleid AND userid {$useridsql}";
|
||||
$DB->delete_records_select('course_modules_completion', $sql, $params);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($courseid)) {
|
||||
$params['course'] = $courseid;
|
||||
|
||||
// Find records relating to course modules.
|
||||
$sql = "SELECT cmc.id
|
||||
FROM {course_completion_criteria} ccc
|
||||
JOIN {course_modules_completion} cmc ON ccc.moduleinstance = cmc.coursemoduleid
|
||||
WHERE ccc.course = :course AND cmc.userid {$useridsql}";
|
||||
$recordids = $DB->get_records_sql($sql, $params);
|
||||
$ids = array_keys($recordids);
|
||||
if (!empty($ids)) {
|
||||
list($deletesql, $deleteparams) = $DB->get_in_or_equal($ids);
|
||||
$deletesql = 'id ' . $deletesql;
|
||||
$DB->delete_records_select('course_modules_completion', $deletesql, $deleteparams);
|
||||
}
|
||||
|
||||
$sql = "course = :course AND userid {$useridsql}";
|
||||
$DB->delete_records_select('course_completion_crit_compl', $sql, $params);
|
||||
$DB->delete_records_select('course_completions', $sql, $params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,6 +59,36 @@ class core_completion_privacy_test extends \core_privacy\tests\provider_testcase
|
||||
$this->assertEquals($this->course->id, $data->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test fetching users' course completion by context and adding to a userlist.
|
||||
*/
|
||||
public function test_add_course_completion_users_to_userlist() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
$user3 = $this->getDataGenerator()->create_user();
|
||||
|
||||
// User1 and user2 complete course.
|
||||
$this->create_course_completion();
|
||||
$this->complete_course($user1);
|
||||
$this->complete_course($user2);
|
||||
|
||||
// User3 is enrolled but has not completed course.
|
||||
$this->getDataGenerator()->enrol_user($user3->id, $this->course->id, 'student');
|
||||
|
||||
$userlist = new \core_privacy\local\request\userlist($this->coursecontext, 'test');
|
||||
\core_completion\privacy\provider::add_course_completion_users_to_userlist($userlist);
|
||||
|
||||
// Ensure only users that have course completion are returned.
|
||||
$expected = [$user1->id, $user2->id];
|
||||
$actual = $userlist->get_userids();
|
||||
sort($expected);
|
||||
sort($actual);
|
||||
$this->assertCount(2, $actual);
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting course completion information.
|
||||
*/
|
||||
@ -115,4 +145,61 @@ class core_completion_privacy_test extends \core_privacy\tests\provider_testcase
|
||||
$this->assertEquals('No', $criterion['completed']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting course completion information by approved userlist.
|
||||
*/
|
||||
public function test_delete_completion_by_approved_userlist() {
|
||||
$this->resetAfterTest();
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
$user3 = $this->getDataGenerator()->create_user();
|
||||
$user4 = $this->getDataGenerator()->create_user();
|
||||
|
||||
$this->create_course_completion();
|
||||
$this->complete_course($user1);
|
||||
$this->complete_course($user2);
|
||||
$this->complete_course($user3);
|
||||
$this->complete_course($user4);
|
||||
|
||||
// Prepare approved userlist (context/component are irrelevant for this test).
|
||||
$approveduserids = [$user1->id, $user3->id];
|
||||
$userlist = new \core_privacy\local\request\approved_userlist($this->coursecontext, 'completion', $approveduserids);
|
||||
|
||||
// Test deleting activity completion information only affects approved userlist.
|
||||
\core_completion\privacy\provider::delete_completion_by_approved_userlist(
|
||||
$userlist, null, $this->cm->id);
|
||||
$activitycompletion1 = \core_completion\privacy\provider::get_activity_completion_info($user1, $this->course,
|
||||
$this->cm);
|
||||
$this->assertEquals(0, $activitycompletion1->completionstate);
|
||||
$activitycompletion2 = \core_completion\privacy\provider::get_activity_completion_info($user2, $this->course,
|
||||
$this->cm);
|
||||
$this->assertNotEquals(0, $activitycompletion2->completionstate);
|
||||
$activitycompletion3 = \core_completion\privacy\provider::get_activity_completion_info($user3, $this->course,
|
||||
$this->cm);
|
||||
$this->assertEquals(0, $activitycompletion3->completionstate);
|
||||
$activitycompletion4 = \core_completion\privacy\provider::get_activity_completion_info($user4, $this->course,
|
||||
$this->cm);
|
||||
$this->assertNotEquals(0, $activitycompletion4->completionstate);
|
||||
|
||||
// Prepare different approved userlist (context/component are irrelevant for this test).
|
||||
$approveduserids = [$user2->id, $user4->id];
|
||||
$userlist = new \core_privacy\local\request\approved_userlist($this->coursecontext, 'completion', $approveduserids);
|
||||
|
||||
// Test deleting course completion information only affects approved userlist.
|
||||
\core_completion\privacy\provider::delete_completion_by_approved_userlist($userlist, $this->course->id);
|
||||
|
||||
$coursecompletion1 = \core_completion\privacy\provider::get_course_completion_info($user1, $this->course);
|
||||
$hasno = array_search('No', $coursecompletion1['criteria'], true);
|
||||
$this->assertFalse($hasno);
|
||||
$coursecompletion2 = \core_completion\privacy\provider::get_course_completion_info($user2, $this->course);
|
||||
$hasyes = array_search('Yes', $coursecompletion1['criteria'], true);
|
||||
$this->assertFalse($hasyes);
|
||||
$coursecompletion3 = \core_completion\privacy\provider::get_course_completion_info($user3, $this->course);
|
||||
$hasno = array_search('No', $coursecompletion1['criteria'], true);
|
||||
$this->assertFalse($hasno);
|
||||
$coursecompletion4 = \core_completion\privacy\provider::get_course_completion_info($user4, $this->course);
|
||||
$hasyes = array_search('Yes', $coursecompletion1['criteria'], true);
|
||||
$this->assertFalse($hasyes);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user