Skip to content

Commit

Permalink
MDL-59174 analytics: Analyser queries to enrollib
Browse files Browse the repository at this point in the history
Part of MDL-57791 epic.
  • Loading branch information
David Monllao committed Jul 24, 2017
1 parent bd16408 commit 8970ff9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 21 deletions.
41 changes: 20 additions & 21 deletions analytics/classes/local/analyser/student_enrolments.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function get_samples_origin() {
}

public function sample_access_context($sampleid) {
return \context_course::instance($this->get_sample_course($sampleid));
return \context_course::instance($this->get_sample_courseid($sampleid));
}

public function get_sample_analysable($sampleid) {
Expand All @@ -64,17 +64,8 @@ protected function provided_sample_data() {
* @return void
*/
protected function get_all_samples(\core_analytics\analysable $course) {
global $DB;

// Using a custom SQL query because we want to include all course enrolments.
// TODO Review this is future as does not look ideal
// Although we load all the course users data in memory anyway, using recordsets we will
// not use the double of the memory required by the end of the iteration.
$sql = "SELECT ue.id AS enrolmentid, u.* FROM {user_enrolments} ue
JOIN {enrol} e ON e.id = ue.enrolid
JOIN {user} u ON ue.userid = u.id
WHERE e.courseid = :courseid";
$enrolments = $DB->get_recordset_sql($sql, array('courseid' => $course->get_id()));
$enrolments = enrol_get_course_users($course->get_id());

// We fetch all enrolments, but we are only interested in students.
$studentids = $course->get_students();
Expand All @@ -97,7 +88,6 @@ protected function get_all_samples(\core_analytics\analysable $course) {
// Fill the cache.
$this->samplecourses[$sampleid] = $course->get_id();
}
$enrolments->close();

$enrolids = array_keys($samplesdata);
return array(array_combine($enrolids, $enrolids), $samplesdata);
Expand All @@ -124,7 +114,7 @@ public function get_samples($sampleids) {

// Enrolment samples are grouped by the course they belong to, so all $sampleids belong to the same
// course, $courseid and $coursemodinfo will only query the DB once and cache the course data in memory.
$courseid = $this->get_sample_course($sampleid);
$courseid = $this->get_sample_courseid($sampleid);
$coursemodinfo = get_fast_modinfo($courseid);
$coursecontext = \context_course::instance($courseid);

Expand All @@ -141,22 +131,31 @@ public function get_samples($sampleids) {
return array(array_combine($enrolids, $enrolids), $samplesdata);
}

protected function get_sample_course($sampleid) {
/**
* Returns the student enrolment course id.
*
* @param int $sampleid
* @return int
*/
protected function get_sample_courseid($sampleid) {
global $DB;

if (empty($this->samplecourses[$sampleid])) {
// TODO New function in enrollib.php.
$sql = "SELECT e.courseid
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
WHERE ue.id = :userenrolmentid";

$this->samplecourses[$sampleid] = $DB->get_field_sql($sql, array('userenrolmentid' => $sampleid));
$course = enrol_get_course_by_user_enrolment_id($sampleid);
$this->samplecourses[$sampleid] = $course->id;
}

return $this->samplecourses[$sampleid];
}

/**
* Returns the visible name of a sample + a renderable to display as sample picture.
*
* @param int $sampleid
* @param int $contextid
* @param array $sampledata
* @return array
*/
public function sample_description($sampleid, $contextid, $sampledata) {
$description = fullname($sampledata['user'], true, array('context' => $contextid));
return array($description, new \user_picture($sampledata['user']));
Expand Down
29 changes: 29 additions & 0 deletions enrol/tests/enrollib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,33 @@ public function test_enrol_get_my_courses_only_enrolled_courses() {
$this->assertEquals($course1->id, $courses[$course1->id]->id);
$this->assertEquals($course2->id, $courses[$course2->id]->id);
}

/**
* test_course_users
*
* @return void
*/
public function test_course_users() {
$this->resetAfterTest();

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();

$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course2->id);

$this->assertCount(2, enrol_get_course_users($course1->id));
$this->assertCount(2, enrol_get_course_users($course1->id, true));

$instances = enrol_get_instances($course1->id, true);
$manualinstance = reset($instances);

$manualplugin = enrol_get_plugin('manual');
$manualplugin->update_user_enrol($manualinstance, $user1->id, ENROL_USER_SUSPENDED);
$this->assertCount(2, enrol_get_course_users($course1->id, false));
$this->assertCount(1, enrol_get_course_users($course1->id, true));
}
}
29 changes: 29 additions & 0 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,35 @@ function enrol_get_course_by_user_enrolment_id($ueid) {
return $DB->get_record_sql($sql, array('ueid' => $ueid));
}

/**
* Return all users enrolled in a course.
*
* @param int $courseid
* @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
* @return stdClass
*/
function enrol_get_course_users($courseid, $onlyactive = false) {
global $DB;

$sql = "SELECT ue.id AS enrolmentid, u.* FROM {user_enrolments} ue
JOIN {enrol} e ON e.id = ue.enrolid
JOIN {user} u ON ue.userid = u.id
WHERE e.courseid = :courseid";
$params = array('courseid' => $courseid);

if ($onlyactive) {
$subwhere = "AND ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
$params['now1'] = round(time(), -2); // improves db caching
$params['now2'] = $params['now1'];
$params['active'] = ENROL_USER_ACTIVE;
$params['enabled'] = ENROL_INSTANCE_ENABLED;
} else {
$subwhere = "";
}
$sql = $sql . $subwhere;
return $DB->get_records_sql($sql, $params);
}

/**
* All enrol plugins should be based on this class,
* this is also the main source of documentation.
Expand Down

0 comments on commit 8970ff9

Please sign in to comment.