Commit 8e7b8cf0 authored by Juan Leyva's avatar Juan Leyva
Browse files

MDL-63739 core_enrol: Add completion criteria, hiden and favourited data

parent d1d7d139
...@@ -300,12 +300,28 @@ class core_enrol_external extends external_api { ...@@ -300,12 +300,28 @@ class core_enrol_external extends external_api {
// Do basic automatic PARAM checks on incoming data, using params description // Do basic automatic PARAM checks on incoming data, using params description
// If any problems are found then exceptions are thrown with helpful error messages // If any problems are found then exceptions are thrown with helpful error messages
$params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid)); $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid));
$userid = $params['userid'];
$courses = enrol_get_users_courses($params['userid'], true, '*'); $courses = enrol_get_users_courses($userid, true, '*');
$result = array(); $result = array();
// Get user data including last access to courses. // Get user data including last access to courses.
$user = get_complete_user_data('id', $userid); $user = get_complete_user_data('id', $userid);
$sameuser = $USER->id == $userid;
// Retrieve favourited courses (starred).
$favouritecourseids = array();
if ($sameuser) {
$ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($userid));
$favourites = $ufservice->find_favourites_by_type('core_course', 'courses');
if ($favourites) {
$favouritecourseids = array_flip(array_map(
function($favourite) {
return $favourite->itemid;
}, $favourites));
}
}
foreach ($courses as $course) { foreach ($courses as $course) {
$context = context_course::instance($course->id, IGNORE_MISSING); $context = context_course::instance($course->id, IGNORE_MISSING);
...@@ -316,7 +332,6 @@ class core_enrol_external extends external_api { ...@@ -316,7 +332,6 @@ class core_enrol_external extends external_api {
continue; continue;
} }
$sameuser = $USER->id == $userid;
if (!$sameuser and !course_can_view_participants($context)) { if (!$sameuser and !course_can_view_participants($context)) {
// we need capability to view participants // we need capability to view participants
continue; continue;
...@@ -334,12 +349,14 @@ class core_enrol_external extends external_api { ...@@ -334,12 +349,14 @@ class core_enrol_external extends external_api {
$progress = null; $progress = null;
$completed = null; $completed = null;
$completionhascriteria = false;
// Return only private information if the user should be able to see it. // Return only private information if the user should be able to see it.
if ($sameuser || completion_can_view_data($userid, $course)) { if ($sameuser || completion_can_view_data($userid, $course)) {
if ($course->enablecompletion) { if ($course->enablecompletion) {
$completion = new completion_info($course); $completion = new completion_info($course);
$completed = $completion->is_course_complete($userid); $completed = $completion->is_course_complete($userid);
$completionhascriteria = $completion->has_criteria();
$progress = \core_completion\progress::get_course_progress_percentage($course, $userid); $progress = \core_completion\progress::get_course_progress_percentage($course, $userid);
} }
} }
...@@ -356,6 +373,11 @@ class core_enrol_external extends external_api { ...@@ -356,6 +373,11 @@ class core_enrol_external extends external_api {
$lastaccess = $user->lastcourseaccess[$course->id]; $lastaccess = $user->lastcourseaccess[$course->id];
} }
$hidden = false;
if ($sameuser) {
$hidden = boolval(get_user_preferences('block_myoverview_hidden_course_' . $course->id, 0));
}
// Retrieve course overview used files. // Retrieve course overview used files.
$courselist = new core_course_list_element($course); $courselist = new core_course_list_element($course);
$overviewfiles = array(); $overviewfiles = array();
...@@ -387,6 +409,7 @@ class core_enrol_external extends external_api { ...@@ -387,6 +409,7 @@ class core_enrol_external extends external_api {
'showgrades' => $course->showgrades, 'showgrades' => $course->showgrades,
'lang' => clean_param($course->lang, PARAM_LANG), 'lang' => clean_param($course->lang, PARAM_LANG),
'enablecompletion' => $course->enablecompletion, 'enablecompletion' => $course->enablecompletion,
'completionhascriteria' => $completionhascriteria,
'category' => $course->category, 'category' => $course->category,
'progress' => $progress, 'progress' => $progress,
'completed' => $completed, 'completed' => $completed,
...@@ -394,6 +417,8 @@ class core_enrol_external extends external_api { ...@@ -394,6 +417,8 @@ class core_enrol_external extends external_api {
'enddate' => $course->enddate, 'enddate' => $course->enddate,
'marker' => $course->marker, 'marker' => $course->marker,
'lastaccess' => $lastaccess, 'lastaccess' => $lastaccess,
'isfavourite' => isset($favouritecourseids[$course->id]),
'hidden' => $hidden,
'overviewfiles' => $overviewfiles, 'overviewfiles' => $overviewfiles,
); );
} }
...@@ -416,7 +441,7 @@ class core_enrol_external extends external_api { ...@@ -416,7 +441,7 @@ class core_enrol_external extends external_api {
'displayname' => new external_value(PARAM_TEXT, 'course display name for lists.', VALUE_OPTIONAL), 'displayname' => new external_value(PARAM_TEXT, 'course display name for lists.', VALUE_OPTIONAL),
'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'), 'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'),
'idnumber' => new external_value(PARAM_RAW, 'id number of course'), 'idnumber' => new external_value(PARAM_RAW, 'id number of course'),
'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'), 'visible' => new external_value(PARAM_INT, '1 means visible, 0 means not yet visible course'),
'summary' => new external_value(PARAM_RAW, 'summary', VALUE_OPTIONAL), 'summary' => new external_value(PARAM_RAW, 'summary', VALUE_OPTIONAL),
'summaryformat' => new external_format_value('summary', VALUE_OPTIONAL), 'summaryformat' => new external_format_value('summary', VALUE_OPTIONAL),
'format' => new external_value(PARAM_PLUGIN, 'course format: weeks, topics, social, site', VALUE_OPTIONAL), 'format' => new external_value(PARAM_PLUGIN, 'course format: weeks, topics, social, site', VALUE_OPTIONAL),
...@@ -424,6 +449,7 @@ class core_enrol_external extends external_api { ...@@ -424,6 +449,7 @@ class core_enrol_external extends external_api {
'lang' => new external_value(PARAM_LANG, 'forced course language', VALUE_OPTIONAL), 'lang' => new external_value(PARAM_LANG, 'forced course language', VALUE_OPTIONAL),
'enablecompletion' => new external_value(PARAM_BOOL, 'true if completion is enabled, otherwise false', 'enablecompletion' => new external_value(PARAM_BOOL, 'true if completion is enabled, otherwise false',
VALUE_OPTIONAL), VALUE_OPTIONAL),
'completionhascriteria' => new external_value(PARAM_BOOL, 'If completion criteria is set.', VALUE_OPTIONAL),
'category' => new external_value(PARAM_INT, 'course category id', VALUE_OPTIONAL), 'category' => new external_value(PARAM_INT, 'course category id', VALUE_OPTIONAL),
'progress' => new external_value(PARAM_FLOAT, 'Progress percentage', VALUE_OPTIONAL), 'progress' => new external_value(PARAM_FLOAT, 'Progress percentage', VALUE_OPTIONAL),
'completed' => new external_value(PARAM_BOOL, 'Whether the course is completed.', VALUE_OPTIONAL), 'completed' => new external_value(PARAM_BOOL, 'Whether the course is completed.', VALUE_OPTIONAL),
...@@ -431,6 +457,8 @@ class core_enrol_external extends external_api { ...@@ -431,6 +457,8 @@ class core_enrol_external extends external_api {
'enddate' => new external_value(PARAM_INT, 'Timestamp when the course end', VALUE_OPTIONAL), 'enddate' => new external_value(PARAM_INT, 'Timestamp when the course end', VALUE_OPTIONAL),
'marker' => new external_value(PARAM_INT, 'Course section marker.', VALUE_OPTIONAL), 'marker' => new external_value(PARAM_INT, 'Course section marker.', VALUE_OPTIONAL),
'lastaccess' => new external_value(PARAM_INT, 'Last access to the course (timestamp).', VALUE_OPTIONAL), 'lastaccess' => new external_value(PARAM_INT, 'Last access to the course (timestamp).', VALUE_OPTIONAL),
'isfavourite' => new external_value(PARAM_BOOL, 'If the user marked this course a favourite.', VALUE_OPTIONAL),
'hidden' => new external_value(PARAM_BOOL, 'If the user hide the course from the dashboard.', VALUE_OPTIONAL),
'overviewfiles' => new external_files('Overview files attached to this course.', VALUE_OPTIONAL), 'overviewfiles' => new external_files('Overview files attached to this course.', VALUE_OPTIONAL),
) )
) )
......
...@@ -360,6 +360,7 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase { ...@@ -360,6 +360,7 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
*/ */
public function test_get_users_courses() { public function test_get_users_courses() {
global $CFG, $DB; global $CFG, $DB;
require_once($CFG->dirroot . '/completion/criteria/completion_criteria_self.php');
$this->resetAfterTest(true); $this->resetAfterTest(true);
$CFG->enablecompletion = 1; $CFG->enablecompletion = 1;
...@@ -404,10 +405,24 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase { ...@@ -404,10 +405,24 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
); );
$DB->insert_record('user_lastaccess', $lastaccess); $DB->insert_record('user_lastaccess', $lastaccess);
// Force completion. // Force completion, setting at least one criteria.
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
$criteriadata = new stdClass();
$criteriadata->id = $course1->id;
// Self completion.
$criteriadata->criteria_self = 1;
$criterion = new completion_criteria_self();
$criterion->update_config($criteriadata);
$ccompletion = new completion_completion(array('course' => $course1->id, 'userid' => $student->id)); $ccompletion = new completion_completion(array('course' => $course1->id, 'userid' => $student->id));
$ccompletion->mark_complete(); $ccompletion->mark_complete();
// Set course hidden and favourited.
set_user_preference('block_myoverview_hidden_course_' . $course1->id, 1, $student);
$ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($student->id));
$ufservice->create_favourite('core_course', 'courses', $course1->id, \context_system::instance());
$this->setUser($student); $this->setUser($student);
// Call the external function. // Call the external function.
$enrolledincourses = core_enrol_external::get_users_courses($student->id); $enrolledincourses = core_enrol_external::get_users_courses($student->id);
...@@ -437,6 +452,9 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase { ...@@ -437,6 +452,9 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->assertEquals($timenow, $courseenrol['lastaccess']); $this->assertEquals($timenow, $courseenrol['lastaccess']);
$this->assertEquals(100.0, $courseenrol['progress']); $this->assertEquals(100.0, $courseenrol['progress']);
$this->assertEquals(true, $courseenrol['completed']); $this->assertEquals(true, $courseenrol['completed']);
$this->assertTrue($courseenrol['completionhascriteria']);
$this->assertTrue($courseenrol['hidden']);
$this->assertTrue($courseenrol['isfavourite']);
} else { } else {
// Check language pack. Should be empty since an incorrect one was used when creating the course. // Check language pack. Should be empty since an incorrect one was used when creating the course.
$this->assertEmpty($courseenrol['lang']); $this->assertEmpty($courseenrol['lang']);
...@@ -445,6 +463,9 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase { ...@@ -445,6 +463,9 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
$this->assertEquals(0, $courseenrol['lastaccess']); $this->assertEquals(0, $courseenrol['lastaccess']);
$this->assertEquals(0, $courseenrol['progress']); $this->assertEquals(0, $courseenrol['progress']);
$this->assertEquals(false, $courseenrol['completed']); $this->assertEquals(false, $courseenrol['completed']);
$this->assertFalse($courseenrol['completionhascriteria']);
$this->assertFalse($courseenrol['hidden']);
$this->assertFalse($courseenrol['isfavourite']);
} }
} }
...@@ -458,8 +479,14 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase { ...@@ -458,8 +479,14 @@ class core_enrol_externallib_testcase extends externallib_advanced_testcase {
if ($courseenrol['id'] == $course1->id) { if ($courseenrol['id'] == $course1->id) {
$this->assertEquals($timenow, $courseenrol['lastaccess']); $this->assertEquals($timenow, $courseenrol['lastaccess']);
$this->assertEquals(100.0, $courseenrol['progress']); $this->assertEquals(100.0, $courseenrol['progress']);
$this->assertTrue($courseenrol['completionhascriteria']);
$this->assertFalse($courseenrol['isfavourite']); // This always false.
$this->assertFalse($courseenrol['hidden']); // This always false.
} else { } else {
$this->assertEquals(0, $courseenrol['progress']); $this->assertEquals(0, $courseenrol['progress']);
$this->assertFalse($courseenrol['completionhascriteria']);
$this->assertFalse($courseenrol['isfavourite']); // This always false.
$this->assertFalse($courseenrol['hidden']); // This always false.
} }
} }
......
...@@ -10,6 +10,9 @@ information provided here is intended especially for developers. ...@@ -10,6 +10,9 @@ information provided here is intended especially for developers.
- completed: Whether the given user completed the course or not. - completed: Whether the given user completed the course or not.
- lastaccess: Last time the user accessed the course. - lastaccess: Last time the user accessed the course.
- overviewfiles: Course overview files. - overviewfiles: Course overview files.
- 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.
=== 3.5 === === 3.5 ===
......
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