From 41b2d03d0fe7d498d384e13e8dad3907629b0a00 Mon Sep 17 00:00:00 2001 From: Mark Sharp Date: Wed, 29 Nov 2023 15:46:37 +0000 Subject: [PATCH] Make XDB friendly --- classes/api.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/classes/api.php b/classes/api.php index 1287f81..2321380 100644 --- a/classes/api.php +++ b/classes/api.php @@ -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 @@ -91,7 +92,7 @@ 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 @@ -99,7 +100,7 @@ public static function get_user_activities($studentid, $expectedhours) { 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; } @@ -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() '; + } + } }