MDL-55630 mod_assign: Exclude users courses from user details

Exclude users' enrolled courses for performance reason.
A new 'includeenrolments' parameter was added
to mod_assign::list_particpants.
This commit is contained in:
Jean-Philippe Gaudreau 2016-09-13 12:08:46 -04:00
parent 6a69cda97d
commit 6399ecd743
5 changed files with 76 additions and 7 deletions

View File

@ -1 +1 @@
define(["core/ajax","jquery","core/templates"],function(a,b,c){return{processResults:function(a,b){var c=[],d=0;for(d=0;d<b.length;d++)c[d]={value:b[d].id,label:b[d].label};return c},transport:function(d,e,f,g){var h=b(d).attr("data-assignmentid"),i=b('[data-region="configure-filters"] input[type="checkbox"]'),j=[];i.each(function(a,c){j[b(c).attr("name")]=b(c).prop("checked")});var k=a.call([{methodname:"mod_assign_list_participants",args:{assignid:h,groupid:0,filter:e,limit:30}}]);k[0].then(function(a){var d=[],e=b("[data-showuseridentity]").data("showuseridentity").split(",");b.each(a,function(a,f){var g=f,h=[],i=!0;j.filter_submitted&&!f.submitted&&(i=!1),j.filter_notsubmitted&&f.submitted&&(i=!1),j.filter_requiregrading&&!f.requiregrading&&(i=!1),i&&(b.each(e,function(a,b){"undefined"!=typeof f[b]&&""!==f[b]&&(g.hasidentity=!0,h.push(f[b]))}),g.identity=h.join(", "),d.push(c.render("mod_assign/list_participant_user_summary",g)))}),b.when.apply(b.when,d).then(function(){var c=arguments,d=0;b.each(a,function(a,b){b.label=c[d],d++}),f(a)})},g)}}});
define(["core/ajax","jquery","core/templates"],function(a,b,c){return{processResults:function(a,b){var c=[],d=0;for(d=0;d<b.length;d++)c[d]={value:b[d].id,label:b[d].label};return c},transport:function(d,e,f,g){var h=b(d).attr("data-assignmentid"),i=b('[data-region="configure-filters"] input[type="checkbox"]'),j=[];i.each(function(a,c){j[b(c).attr("name")]=b(c).prop("checked")});var k=a.call([{methodname:"mod_assign_list_participants",args:{assignid:h,groupid:0,filter:e,limit:30,includeenrolments:!1}}]);k[0].then(function(a){var d=[],e=b("[data-showuseridentity]").data("showuseridentity").split(",");b.each(a,function(a,f){var g=f,h=[],i=!0;j.filter_submitted&&!f.submitted&&(i=!1),j.filter_notsubmitted&&f.submitted&&(i=!1),j.filter_requiregrading&&!f.requiregrading&&(i=!1),i&&(b.each(e,function(a,b){"undefined"!=typeof f[b]&&""!==f[b]&&(g.hasidentity=!0,h.push(f[b]))}),g.identity=h.join(", "),d.push(c.render("mod_assign/list_participant_user_summary",g)))}),b.when.apply(b.when,d).then(function(){var c=arguments,d=0;b.each(a,function(a,b){b.label=c[d],d++}),f(a)})},g)}}});

View File

@ -62,7 +62,8 @@ define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
});
var promise = ajax.call([{
methodname: 'mod_assign_list_participants', args: {assignid: assignmentid, groupid: 0, filter: query, limit: 30}
methodname: 'mod_assign_list_participants',
args: {assignid: assignmentid, groupid: 0, filter: query, limit: 30, includeenrolments: false}
}]);
promise[0].then(function(results) {

View File

@ -2510,6 +2510,8 @@ class mod_assign_external extends external_api {
'skip' => new external_value(PARAM_INT, 'number of records to skip', VALUE_DEFAULT, 0),
'limit' => new external_value(PARAM_INT, 'maximum number of records to return', VALUE_DEFAULT, 0),
'onlyids' => new external_value(PARAM_BOOL, 'Do not return all user fields', VALUE_DEFAULT, false),
'includeenrolments' => new external_value(PARAM_BOOL, 'Do return courses where the user is enrolled',
VALUE_DEFAULT, true)
)
);
}
@ -2523,11 +2525,12 @@ class mod_assign_external extends external_api {
* @param int $skip Number of records to skip
* @param int $limit Maximum number of records to return
* @param bool $onlyids Only return user ids.
* @param bool $includeenrolments Return courses where the user is enrolled.
* @return array of warnings and status result
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function list_participants($assignid, $groupid, $filter, $skip, $limit, $onlyids) {
public static function list_participants($assignid, $groupid, $filter, $skip, $limit, $onlyids, $includeenrolments) {
global $DB, $CFG;
require_once($CFG->dirroot . "/mod/assign/locallib.php");
require_once($CFG->dirroot . "/user/lib.php");
@ -2539,7 +2542,8 @@ class mod_assign_external extends external_api {
'filter' => $filter,
'skip' => $skip,
'limit' => $limit,
'onlyids' => $onlyids
'onlyids' => $onlyids,
'includeenrolments' => $includeenrolments
));
$warnings = array();
@ -2551,6 +2555,17 @@ class mod_assign_external extends external_api {
$participants = $assign->list_participants_with_filter_status_and_group($params['groupid']);
$userfields = user_get_default_fields();
if (!$params['includeenrolments']) {
// Remove enrolled courses from users fields to be returned.
$key = array_search('enrolledcourses', $userfields);
if ($key !== false) {
unset($userfields[$key]);
} else {
throw new moodle_exception('invaliduserfield', 'error', '', 'enrolledcourses');
}
}
$result = array();
$index = 0;
foreach ($participants as $record) {
@ -2577,7 +2592,7 @@ class mod_assign_external extends external_api {
}
// Now we do the expensive lookup of user details because we completed the filtering.
if (!$assign->is_blind_marking() && !$params['onlyids']) {
$userdetails = user_get_user_details($record, $course);
$userdetails = user_get_user_details($record, $course, $userfields);
} else {
$userdetails = array('id' => $record->id);
}
@ -2620,7 +2635,6 @@ class mod_assign_external extends external_api {
$userdesc->keys['profileimageurlsmall']->required = VALUE_OPTIONAL;
$userdesc->keys['profileimageurl']->required = VALUE_OPTIONAL;
$userdesc->keys['email']->desc = 'Email address';
$userdesc->keys['email']->desc = 'Email address';
$userdesc->keys['idnumber']->desc = 'The idnumber of the user';
// Define other keys.

View File

@ -2386,7 +2386,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
$DB->update_record('user', $student);
$this->setUser($teacher);
$participants = mod_assign_external::list_participants($assignment->id, 0, '', 0, 0, false);
$participants = mod_assign_external::list_participants($assignment->id, 0, '', 0, 0, false, true);
$this->assertCount(1, $participants);
// Asser that we have a valid response data.
@ -2401,6 +2401,12 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
$this->assertEquals($student->phone2, $participant['phone2']);
$this->assertEquals($student->department, $participant['department']);
$this->assertEquals($student->institution, $participant['institution']);
$this->assertArrayHasKey('enrolledcourses', $participant);
$participants = mod_assign_external::list_participants($assignment->id, 0, '', 0, 0, false, false);
// Check that the list of courses the participant is enrolled is not returned.
$participant = $participants[0];
$this->assertArrayNotHasKey('enrolledcourses', $participant);
}
/**

View File

@ -575,4 +575,52 @@ class core_userliblib_testcase extends advanced_testcase {
$CFG->coursecontact = null;
}
/**
* Test user_get_user_details
*/
public function test_user_get_user_details() {
global $DB;
$this->resetAfterTest();
// Create user and modify user profile.
$teacher = $this->getDataGenerator()->create_user();
$student = $this->getDataGenerator()->create_user();
$studentfullname = fullname($student);
$course1 = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course1->id);
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id);
$this->getDataGenerator()->enrol_user($student->id, $course1->id);
role_assign($teacherrole->id, $teacher->id, $coursecontext->id);
role_assign($studentrole->id, $student->id, $coursecontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Get student details as a user with super system capabilities.
$result = user_get_user_details($student, $course1);
$this->assertEquals($student->id, $result['id']);
$this->assertEquals($studentfullname, $result['fullname']);
$this->assertEquals($course1->id, $result['enrolledcourses'][0]['id']);
$this->setUser($teacher);
// Get student details as a user who can only see this user in a course.
$result = user_get_user_details($student, $course1);
$this->assertEquals($student->id, $result['id']);
$this->assertEquals($studentfullname, $result['fullname']);
$this->assertEquals($course1->id, $result['enrolledcourses'][0]['id']);
// Get student details with required fields.
$result = user_get_user_details($student, $course1, array('id', 'fullname'));
$this->assertCount(2, $result);
$this->assertEquals($student->id, $result['id']);
$this->assertEquals($studentfullname, $result['fullname']);
// Get exception for invalid required fields.
$this->expectException('moodle_exception');
$result = user_get_user_details($student, $course1, array('wrongrequiredfield'));
}
}