Skip to content

Commit

Permalink
Make XDB friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchi committed Nov 29, 2023
1 parent ab2f6cd commit 41b2d03
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ public static function get_apprentice_courses() {
*/
public static function get_user_activities($studentid, $expectedhours) {
global $DB;
$random = self::db_random();
// Expected hours are set by the teacher. If there are none, it's still possible the student has entered something.
if (count($expectedhours) == 0) {
$activities = $DB->get_records_sql("SELECT (FLOOR( 1 + RAND( ) *5000 )) id,
$activities = $DB->get_records_sql("SELECT {$random} idx,
a.id activityid, a.activitydate, a.activitytype, a.activitydetails, a.activityhours, aa.activityname,
c.fullname, a.userid
FROM {local_apprentice} a
Expand All @@ -91,15 +92,15 @@ public static function get_user_activities($studentid, $expectedhours) {
WHERE a.userid = :studentid
ORDER BY a.activitytype", ['studentid' => $studentid]);
} else {
$activities = $DB->get_records_sql('SELECT (FLOOR( 1 + RAND( ) *5000 )) id,
$activities = $DB->get_records_sql("SELECT {$random} idx,
a.id activityid, a.activitydate, aa.id activitytype, a.activitydetails, a.activityhours, aa.activityname,
c.fullname, a.userid
FROM {report_apprentice} r
JOIN {local_apprenticeactivities} aa ON aa.id = r.activityid
LEFT OUTER JOIN {local_apprentice} a ON a.activitytype = r.activityid AND a.userid = :userid
LEFT JOIN {course} c ON c.id = a.course
WHERE r.studentid = :studentid
ORDER BY r.id, a.activitytype', ['userid' => $studentid, 'studentid' => $studentid]);
ORDER BY r.id, a.activitytype", ['userid' => $studentid, 'studentid' => $studentid]);
}
return $activities;
}
Expand Down Expand Up @@ -243,4 +244,23 @@ public static function report_exists() {
$dbman = $DB->get_manager();
return $dbman->table_exists('report_apprentice');
}

/**
* Return database specific random function
*
* @return string
*/
private static function db_random() {
global $DB;
switch ($DB->get_dbfamily()) {
case 'oracle':
return ' dbms_random.value ';
case 'postgres':
return ' RANDOM() ';
case 'mssql':
return ' NEWID() ';
default:
return ' RAND() ';
}
}
}

0 comments on commit 41b2d03

Please sign in to comment.