Browse Source

Speed performance: Improve api_get_course_setting using static + fix phpdoc

Julio Montoya 5 years ago
parent
commit
c5592cf7a6
2 changed files with 44 additions and 27 deletions
  1. 4 5
      main/course_info/infocours.php
  2. 40 22
      main/inc/lib/api.lib.php

+ 4 - 5
main/course_info/infocours.php

@@ -943,7 +943,7 @@ $values['show_score'] = $all_course_information['show_score'];
 $courseSettings = CourseManager::getCourseSettingVariables($appPlugin);
 
 foreach ($courseSettings as $setting) {
-    $result = api_get_course_setting($setting);
+    $result = api_get_course_setting($setting, $_course, true);
     if ($result != '-1') {
         $values[$setting] = $result;
     }
@@ -961,7 +961,7 @@ if ($form->validate() && is_settings_editable()) {
     // update course picture
     $picture = $_FILES['picture'];
     if (!empty($picture['name'])) {
-        $picture_uri = CourseManager::update_course_picture(
+        CourseManager::update_course_picture(
             $_course,
             $picture['name'],
             $picture['tmp_name'],
@@ -1008,7 +1008,7 @@ if ($form->validate() && is_settings_editable()) {
         : null;
 
     if (!empty($pdf_export_watermark_path['name'])) {
-        $pdf_export_watermark_path_result = PDF::upload_watermark(
+        PDF::upload_watermark(
             $pdf_export_watermark_path['name'],
             $pdf_export_watermark_path['tmp_name'],
             $course_code
@@ -1054,6 +1054,7 @@ if ($form->validate() && is_settings_editable()) {
     $cidReset = true;
     $cidReq = $course_code;
     Display::addFlash(Display::return_message(get_lang('Updated')));
+
     require '../inc/local.inc.php';
     $url = api_get_path(WEB_CODE_PATH).'course_info/infocours.php?'.$courseParams;
     header("Location: $url");
@@ -1066,10 +1067,8 @@ if ($show_delete_watermark_text_message) {
     );
 }
 
-/*	Header */
 Display::display_header($nameTools, MODULE_HELP_NAME);
 
-// Display the form
 echo '<div id="course_settings">';
 $form->display();
 echo '</div>';

+ 40 - 22
main/inc/lib/api.lib.php

@@ -1904,37 +1904,58 @@ function api_get_course_path($course_code = null)
 /**
  * Gets a course setting from the current course_setting table. Try always using integer values.
  *
- * @param string $setting_name The name of the setting we want from the table
- * @param array  $course_info
- *
+ * @param string $settingName The name of the setting we want from the table
+ * @param array  $courseInfo
+ * @param bool   $force force checking the value in the database
  * @return mixed The value of that setting in that table. Return -1 if not found.
  */
-function api_get_course_setting($setting_name, $course_info = [])
+function api_get_course_setting($settingName, $courseInfo = [], $force = false)
 {
-    if (empty($course_info)) {
-        $course_info = api_get_course_info();
+    if (empty($courseInfo)) {
+        $courseInfo = api_get_course_info();
+    }
+
+    if (empty($courseInfo) || empty($settingName)) {
+        return -1;
     }
 
-    if (isset($course_info['real_id']) && !empty($course_info['real_id']) && !empty($setting_name)) {
+    $courseId = isset($courseInfo['real_id']) && !empty($courseInfo['real_id']) ? $courseInfo['real_id'] : 0;
+
+    if (empty($courseId)) {
+        return -1;
+    }
+
+    static $courseSettingInfo = [];
+
+    if ($force) {
+        $courseSettingInfo = [];
+    }
+
+    if (!isset($courseSettingInfo[$courseId])) {
         $table = Database::get_course_table(TABLE_COURSE_SETTING);
-        $setting_name = Database::escape_string($setting_name);
+        $settingName = Database::escape_string($settingName);
 
-        $sql = "SELECT value FROM $table
-                WHERE c_id = {$course_info['real_id']} AND variable = '$setting_name'";
+        $sql = "SELECT variable, value FROM $table
+                WHERE c_id = $courseId ";
         $res = Database::query($sql);
         if (Database::num_rows($res) > 0) {
-            $row = Database::fetch_array($res);
-            if ($setting_name === 'email_alert_manager_on_new_quiz') {
-                if (!is_null($row['value'])) {
-                    $result = explode(',', $row['value']);
-                    $row['value'] = $result;
+            $result = Database::store_result($res, 'ASSOC');
+            $courseSettingInfo[$courseId] = array_column($result, 'value', 'variable');
+
+            if (isset($courseSettingInfo[$courseId]['email_alert_manager_on_new_quiz'])) {
+                $value = $courseSettingInfo[$courseId]['email_alert_manager_on_new_quiz'];
+                if (!is_null($value)) {
+                    $result = explode(',', $value);
+                    $courseSettingInfo[$courseId]['email_alert_manager_on_new_quiz'] = $result;
                 }
             }
-
-            return $row['value'];
         }
     }
 
+    if (isset($courseSettingInfo[$courseId]) && isset($courseSettingInfo[$courseId][$settingName])) {
+        return $courseSettingInfo[$courseId][$settingName];
+    }
+
     return -1;
 }
 
@@ -4409,12 +4430,9 @@ function api_item_property_update(
 /**
  * Gets item property by tool.
  *
- * @param string    course code
- * @param string    tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
- * @param int       id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
- * @param int    $session_id
- * @param string $tool
+ * @param string $tool tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  * @param string $course_code
+ * @param int    $session_id
  *
  * @return array All fields from c_item_property (all rows found) or empty array
  */