Browse Source

Minor - Micro-optimization of queries by avoiding concatenations - extends 2f581531998a521e0965085ed192376d4e568779

Yannick Warnier 6 years ago
parent
commit
441b42c95b
1 changed files with 25 additions and 25 deletions
  1. 25 25
      main/inc/lib/course.lib.php

+ 25 - 25
main/inc/lib/course.lib.php

@@ -1084,7 +1084,7 @@ class CourseManager
                 return false;
             }
             $courseId = $courseInfo['real_id'];
-            $condition_course = ' AND c_id = '.$courseId;
+            $condition_course = " AND c_id = $courseId";
         }
 
         $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
@@ -1106,22 +1106,22 @@ class CourseManager
         }
 
         $tableSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
-        $sql = 'SELECT 1 FROM '.$tableSessionCourseUser.'
-                WHERE user_id = '.$user_id.' AND session_id = '.$session_id.' '.$condition_course;
+        $sql = "SELECT 1 FROM $tableSessionCourseUser
+                WHERE user_id = $user_id AND session_id = $session_id $condition_course";
 
         if (Database::num_rows(Database::query($sql)) > 0) {
             return true;
         }
 
-        $sql = 'SELECT 1 FROM '.$tableSessionCourseUser.'
-                WHERE user_id = '.$user_id.' AND session_id = '.$session_id.' AND status = 2 '.$condition_course;
+        $sql = "SELECT 1 FROM $tableSessionCourseUser
+                WHERE user_id = $user_id AND session_id = $session_id AND status = 2 $condition_course";
 
         if (Database::num_rows(Database::query($sql)) > 0) {
             return true;
         }
 
         $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_SESSION).
-              ' WHERE id = '.$session_id.' AND id_coach='.$user_id;
+              " WHERE id = $session_id AND id_coach = $user_id";
 
         if (Database::num_rows(Database::query($sql)) > 0) {
             return true;
@@ -1292,7 +1292,7 @@ class CourseManager
         // we have to check if it is a valid field that can be sorted on
         if (!strstr($order_by, 'ORDER BY')) {
             if (!empty($order_by)) {
-                $order_by = 'ORDER BY '.$order_by;
+                $order_by = "ORDER BY $order_by";
             } else {
                 $order_by = '';
             }
@@ -1325,7 +1325,7 @@ class CourseManager
             if (!empty($courseCodeList)) {
                 $courseCodeListForSession = array_map(['Database', 'escape_string'], $courseCodeList);
                 $courseCodeListForSession = implode('","', $courseCodeListForSession);
-                $courseCondition = ' course.code IN ("'.$courseCodeListForSession.'")  ';
+                $courseCondition = " course.code IN ('$courseCodeListForSession')  ";
             }
 
             $sql .= ' FROM '.Database::get_main_table(TABLE_MAIN_USER).' as user ';
@@ -1380,7 +1380,7 @@ class CourseManager
                        ON course_rel_user.c_id = course.id ";
 
             if (!empty($course_code)) {
-                $sql .= ' AND course_rel_user.c_id = "'.$courseId.'"';
+                $sql .= " AND course_rel_user.c_id = $courseId";
             }
             $where[] = ' course_rel_user.c_id IS NOT NULL ';
 
@@ -1402,17 +1402,17 @@ class CourseManager
                 $extraFieldInfo = UserManager::get_extra_field_information_by_name($extraField);
                 if (!empty($extraFieldInfo)) {
                     $fieldValuesTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
-                    $sql .= ' LEFT JOIN '.$fieldValuesTable.' as ufv
+                    $sql .= " LEFT JOIN $fieldValuesTable as ufv
                             ON (
                                 user.id = ufv.item_id AND
-                                (field_id = '.$extraFieldInfo['id'].' OR field_id IS NULL)
-                            )';
+                                (field_id = ".$extraFieldInfo['id']." OR field_id IS NULL)
+                            )";
                     $extraFieldWasAdded = true;
                 }
             }
         }
 
-        $sql .= ' WHERE '.$filter_by_status_condition.' '.implode(' OR ', $where);
+        $sql .= " WHERE $filter_by_status_condition ".implode(' OR ', $where);
 
         if ($multiple_access_url) {
             $current_access_url_id = api_get_current_access_url_id();
@@ -1439,7 +1439,7 @@ class CourseManager
 
         if (isset($filterByActive)) {
             $filterByActive = (int) $filterByActive;
-            $sql .= ' AND user.active = '.$filterByActive;
+            $sql .= " AND user.active = $filterByActive";
         }
 
         if (!empty($searchByKeyword)) {
@@ -1451,7 +1451,7 @@ class CourseManager
                     ) ";
         }
 
-        $sql .= ' '.$order_by.' '.$limit;
+        $sql .= " $order_by $limit";
 
         $rs = Database::query($sql);
         $users = [];
@@ -1759,7 +1759,7 @@ class CourseManager
 
         // We get the coach for the given course in a given session.
         $sql = 'SELECT user_id FROM '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).
-               ' WHERE session_id = '.$session_id.' AND c_id = '.$courseId.' AND status = 2';
+               " WHERE session_id = $session_id AND c_id = $courseId AND status = 2";
         $rs = Database::query($sql);
         while ($user = Database::fetch_array($rs)) {
             $userInfo = api_get_user_info($user['user_id']);
@@ -1822,7 +1822,7 @@ class CourseManager
                         FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." cu
                         INNER JOIN $userTable u
                         ON cu.user_id = u.user_id
-                        WHERE c_id = '$courseId' AND cu.status = ".STUDENT;
+                        WHERE c_id = $courseId AND cu.status = ".STUDENT;
 
                 if (!$includeInvitedUsers) {
                     $sql .= " AND u.status != ".INVITEE;
@@ -1865,7 +1865,7 @@ class CourseManager
             }
 
             if ($session_id != 0) {
-                $sql_query .= ' AND scu.session_id = '.$session_id;
+                $sql_query .= " AND scu.session_id = $session_id";
             }
 
             if (!$includeInvitedUsers) {
@@ -2849,7 +2849,7 @@ class CourseManager
                     ON (course.id = url.c_id) 
                     WHERE 
                         url.access_url_id = $urlId AND 
-                        cru.user_id = '$user_id' 
+                        cru.user_id = $user_id 
                         $withoutSpecialCourses
                         $languageCondition
                     ORDER BY course.title
@@ -3696,7 +3696,7 @@ class CourseManager
         // Step 1: We get all the categories of the user
         $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
         $sql = "SELECT * FROM $table
-                WHERE user_id = '".$user_id."'
+                WHERE user_id = $user_id
                 ORDER BY sort ASC";
 
         $result = Database::query($sql);
@@ -3776,7 +3776,7 @@ class CourseManager
         $user_id = 0,
         $useUserLanguageFilterIfAvailable = true
     ) {
-        $user_id = $user_id ?: api_get_user_id();
+        $user_id = $user_id ? (int) $user_id : api_get_user_id();
         $user_category_id = (int) $user_category_id;
 
         // Table definitions
@@ -3818,7 +3818,7 @@ class CourseManager
                 INNER JOIN $TABLE_ACCESS_URL_REL_COURSE url
                 ON (url.c_id = course.id)
                 WHERE
-                    course_rel_user.user_id = '".$user_id."' AND
+                    course_rel_user.user_id = $user_id AND
                     $userCategoryCondition
                     $without_special_courses
                     $languageCondition
@@ -3826,7 +3826,7 @@ class CourseManager
         // If multiple URL access mode is enabled, only fetch courses
         // corresponding to the current URL.
         if (api_get_multiple_access_url() && $current_url_id != -1) {
-            $sql .= " AND access_url_id='".$current_url_id."'";
+            $sql .= " AND access_url_id = $current_url_id";
         }
         // Use user's classification for courses (if any).
         $sql .= " ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
@@ -5041,10 +5041,10 @@ class CourseManager
         $visibilityCondition = '';
         $hidePrivate = api_get_setting('course_catalog_hide_private');
         if ($hidePrivate === 'true') {
-            $visibilityCondition .= ' AND '.$courseTableAlias.'.visibility <> '.COURSE_VISIBILITY_REGISTERED;
+            $visibilityCondition .= " AND $courseTableAlias.visibility <> ".COURSE_VISIBILITY_REGISTERED;
         }
         if ($hideClosed) {
-            $visibilityCondition .= ' AND '.$courseTableAlias.'.visibility NOT IN ('.COURSE_VISIBILITY_CLOSED.','.COURSE_VISIBILITY_HIDDEN.')';
+            $visibilityCondition .= " AND $courseTableAlias.visibility NOT IN (".COURSE_VISIBILITY_CLOSED.','.COURSE_VISIBILITY_HIDDEN.')';
         }
 
         // Check if course have users allowed to see it in the catalogue, then show only if current user is allowed to see it