Permalink
Browse files

Merge branch 'MDL-63739-master' of git://github.com/jleyva/moodle

  • Loading branch information...
dmonllao committed Nov 5, 2018
2 parents d9c54de + 8e7b8cf commit 83868e14163e3aa1a4de5c05e7d9892dfa707148
Showing with 62 additions and 4 deletions.
  1. +31 −3 enrol/externallib.php
  2. +28 −1 enrol/tests/externallib_test.php
  3. +3 −0 enrol/upgrade.txt
@@ -300,12 +300,28 @@ public static function get_users_courses($userid) {
// Do basic automatic PARAM checks on incoming data, using params description
// 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));
$userid = $params['userid'];
$courses = enrol_get_users_courses($params['userid'], true, '*');
$courses = enrol_get_users_courses($userid, true, '*');
$result = array();
// Get user data including last access to courses.
$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) {
$context = context_course::instance($course->id, IGNORE_MISSING);
@@ -316,7 +332,6 @@ public static function get_users_courses($userid) {
continue;
}
$sameuser = $USER->id == $userid;
if (!$sameuser and !course_can_view_participants($context)) {
// we need capability to view participants
continue;
@@ -334,12 +349,14 @@ public static function get_users_courses($userid) {
$progress = null;
$completed = null;
$completionhascriteria = false;
// Return only private information if the user should be able to see it.
if ($sameuser || completion_can_view_data($userid, $course)) {
if ($course->enablecompletion) {
$completion = new completion_info($course);
$completed = $completion->is_course_complete($userid);
$completionhascriteria = $completion->has_criteria();
$progress = \core_completion\progress::get_course_progress_percentage($course, $userid);
}
}
@@ -356,6 +373,11 @@ public static function get_users_courses($userid) {
$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.
$courselist = new core_course_list_element($course);
$overviewfiles = array();
@@ -387,13 +409,16 @@ public static function get_users_courses($userid) {
'showgrades' => $course->showgrades,
'lang' => clean_param($course->lang, PARAM_LANG),
'enablecompletion' => $course->enablecompletion,
'completionhascriteria' => $completionhascriteria,
'category' => $course->category,
'progress' => $progress,
'completed' => $completed,
'startdate' => $course->startdate,
'enddate' => $course->enddate,
'marker' => $course->marker,
'lastaccess' => $lastaccess,
'isfavourite' => isset($favouritecourseids[$course->id]),
'hidden' => $hidden,
'overviewfiles' => $overviewfiles,
);
}
@@ -416,21 +441,24 @@ public static function get_users_courses_returns() {
'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'),
'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),
'summaryformat' => new external_format_value('summary', VALUE_OPTIONAL),
'format' => new external_value(PARAM_PLUGIN, 'course format: weeks, topics, social, site', VALUE_OPTIONAL),
'showgrades' => new external_value(PARAM_BOOL, 'true if grades are shown, otherwise false', 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',
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),
'progress' => new external_value(PARAM_FLOAT, 'Progress percentage', VALUE_OPTIONAL),
'completed' => new external_value(PARAM_BOOL, 'Whether the course is completed.', VALUE_OPTIONAL),
'startdate' => new external_value(PARAM_INT, 'Timestamp when the course start', 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),
'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),
)
)
@@ -360,6 +360,7 @@ public function test_get_enrolled_users_visibility($settings, $results) {
*/
public function test_get_users_courses() {
global $CFG, $DB;
require_once($CFG->dirroot . '/completion/criteria/completion_criteria_self.php');
$this->resetAfterTest(true);
$CFG->enablecompletion = 1;
@@ -404,10 +405,24 @@ public function test_get_users_courses() {
);
$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->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);
// Call the external function.
$enrolledincourses = core_enrol_external::get_users_courses($student->id);
@@ -437,6 +452,9 @@ public function test_get_users_courses() {
$this->assertEquals($timenow, $courseenrol['lastaccess']);
$this->assertEquals(100.0, $courseenrol['progress']);
$this->assertEquals(true, $courseenrol['completed']);
$this->assertTrue($courseenrol['completionhascriteria']);
$this->assertTrue($courseenrol['hidden']);
$this->assertTrue($courseenrol['isfavourite']);
} else {
// Check language pack. Should be empty since an incorrect one was used when creating the course.
$this->assertEmpty($courseenrol['lang']);
@@ -445,6 +463,9 @@ public function test_get_users_courses() {
$this->assertEquals(0, $courseenrol['lastaccess']);
$this->assertEquals(0, $courseenrol['progress']);
$this->assertEquals(false, $courseenrol['completed']);
$this->assertFalse($courseenrol['completionhascriteria']);
$this->assertFalse($courseenrol['hidden']);
$this->assertFalse($courseenrol['isfavourite']);
}
}
@@ -458,8 +479,14 @@ public function test_get_users_courses() {
if ($courseenrol['id'] == $course1->id) {
$this->assertEquals($timenow, $courseenrol['lastaccess']);
$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 {
$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.
- completed: Whether the given user completed the course or not.
- lastaccess: Last time the user accessed the course.
- 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 ===

0 comments on commit 83868e1

Please sign in to comment.