Commit 1f87a7f6 authored by Juan Leyva's avatar Juan Leyva
Browse files

MDL-64084 core_enrol: Return expect last access course time

parent 07203d34
......@@ -165,8 +165,13 @@ class core_enrol_external extends external_api {
$courseusers['capability'] = $capability;
list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $capability, $groupid, $onlyactive);
$enrolledparams['courseid'] = $courseid;
$sql = "SELECT u.* FROM {user} u WHERE u.id IN ($enrolledsql) ORDER BY u.id ASC";
$sql = "SELECT u.*, COALESCE(ul.timeaccess, 0) AS lastcourseaccess
FROM {user} u
LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)
WHERE u.id IN ($enrolledsql)
ORDER BY u.id ASC";
$enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
$users = array();
......@@ -216,6 +221,7 @@ class core_enrol_external extends external_api {
'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
'lastcourseaccess' => new external_value(PARAM_INT, 'last access to the course (0 if never)', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL),
'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
......@@ -720,15 +726,18 @@ class core_enrol_external extends external_api {
return array();
}
}
$sql = "SELECT us.*
$sql = "SELECT us.*, COALESCE(ul.timeaccess, 0) AS lastcourseaccess
FROM {user} us
JOIN (
SELECT DISTINCT u.id $ctxselect
FROM {user} u $ctxjoin $groupjoin
WHERE u.id IN ($enrolledsql)
) q ON q.id = us.id
LEFT JOIN {user_lastaccess} ul ON (ul.userid = us.id AND ul.courseid = :courseid)
ORDER BY $sortby $sortdirection";
$enrolledparams = array_merge($enrolledparams, $sortparams);
$enrolledparams['courseid'] = $courseid;
$enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
$users = array();
foreach ($enrolledusers as $user) {
......@@ -771,6 +780,7 @@ class core_enrol_external extends external_api {
'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
'lastcourseaccess' => new external_value(PARAM_INT, 'last access to the course (0 if never)', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
......
......@@ -656,6 +656,46 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->assertArrayNotHasKey('email', $enrolledusers[0]);
}
/**
* Test get_enrolled_users last course access.
*/
public function test_get_enrolled_users_including_lastcourseaccess() {
global $DB;
$capability = 'moodle/course:viewparticipants';
$data = $this->get_enrolled_users_setup($capability);
// Call the external function.
$enrolledusers = core_enrol_external::get_enrolled_users($data->course->id);
// We need to execute the return values cleaning process to simulate the web service server.
$enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
// Check the result set.
$this->assertEquals(3, count($enrolledusers));
$this->assertArrayHasKey('email', $enrolledusers[0]);
$this->assertEquals(0, $enrolledusers[0]['lastcourseaccess']);
$this->assertEquals(0, $enrolledusers[1]['lastcourseaccess']);
$this->assertNotEquals(0, $enrolledusers[2]['lastcourseaccess']); // We forced an access to the course via setUser.
// Force last access.
$timenow = time();
$lastaccess = array(
'userid' => $enrolledusers[0]['id'],
'courseid' => $data->course->id,
'timeaccess' => $timenow
);
$DB->insert_record('user_lastaccess', $lastaccess);
$enrolledusers = core_enrol_external::get_enrolled_users($data->course->id);
$enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
// Check the result set.
$this->assertEquals(3, count($enrolledusers));
$this->assertEquals($timenow, $enrolledusers[0]['lastcourseaccess']);
$this->assertEquals(0, $enrolledusers[1]['lastcourseaccess']);
$this->assertNotEquals(0, $enrolledusers[2]['lastcourseaccess']);
}
/**
* Test get_enrolled_users from core_enrol_external with capability to
* viewparticipants removed.
......@@ -772,6 +812,56 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->assertEquals($data->student1->id, $expecteduser['id']);
}
/**
* Test get_enrolled_users last course access.
*/
public function test_get_enrolled_users_with_capability_including_lastcourseaccess() {
global $DB;
$capability = 'moodle/course:viewparticipants';
$data = $this->get_enrolled_users_with_capability_setup($capability);
$parameters = array(
'coursecapabilities' => array(
'courseid' => $data->course->id,
'capabilities' => array(
$capability,
),
),
);
$result = core_enrol_external::get_enrolled_users_with_capability($parameters, array());
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
// Check an array containing the expected user for the course capability is returned.
$expecteduserlist = $result[0];
$this->assertEquals($data->course->id, $expecteduserlist['courseid']);
$this->assertEquals($capability, $expecteduserlist['capability']);
$this->assertEquals(2, count($expecteduserlist['users']));
// We forced an access to the course via setUser.
$this->assertNotEquals(0, $expecteduserlist['users'][0]['lastcourseaccess']);
$this->assertEquals(0, $expecteduserlist['users'][1]['lastcourseaccess']);
// Force last access.
$timenow = time();
$lastaccess = array(
'userid' => $expecteduserlist['users'][1]['id'],
'courseid' => $data->course->id,
'timeaccess' => $timenow
);
$DB->insert_record('user_lastaccess', $lastaccess);
$result = core_enrol_external::get_enrolled_users_with_capability($parameters, array());
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
// Check the result set.
$expecteduserlist = $result[0];
$this->assertEquals(2, count($expecteduserlist['users']));
$this->assertNotEquals(0, $expecteduserlist['users'][0]['lastcourseaccess']);
$this->assertEquals($timenow, $expecteduserlist['users'][1]['lastcourseaccess']);
}
/**
* Test for core_enrol_external::edit_user_enrolment().
*/
......
......@@ -20,6 +20,8 @@ information provided here is intended especially for developers.
- completionhascriteria: Whether completion criteria is set for the course.
- isfavourite: Whether the user marked the course as favourite.
- hidden: Whether the user hide the course from the dashboard.
* External functions core_enrol_external::get_enrolled_users and core_enrol_external::get_enrolled_users_with_capability now return
the last access time for the users in the given course.
=== 3.5 ===
......
......@@ -246,7 +246,7 @@ function user_get_default_fields() {
'institution', 'interests', 'firstaccess', 'lastaccess', 'auth', 'confirmed',
'idnumber', 'lang', 'theme', 'timezone', 'mailformat', 'description', 'descriptionformat',
'city', 'url', 'country', 'profileimageurlsmall', 'profileimageurl', 'customfields',
'groups', 'roles', 'preferences', 'enrolledcourses', 'suspended'
'groups', 'roles', 'preferences', 'enrolledcourses', 'suspended', 'lastcourseaccess'
);
}
......@@ -471,6 +471,15 @@ function user_get_user_details($user, $course = null, array $userfields = array(
}
}
// Hidden fields restriction to lastaccess field applies to both site and course access time.
if (in_array('lastcourseaccess', $userfields) && (!isset($hiddenfields['lastaccess']) or $isadmin)) {
if (isset($user->lastcourseaccess)) {
$userdetails['lastcourseaccess'] = $user->lastcourseaccess;
} else {
$userdetails['lastcourseaccess'] = 0;
}
}
if (in_array('email', $userfields) && (
$currentuser
or (!isset($hiddenfields['email']) and (
......
Markdown is supported
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