Sfoglia il codice sorgente

Fixes after merge with 1.11.x

Julio Montoya 5 anni fa
parent
commit
f42031e22c
1 ha cambiato i file con 271 aggiunte e 0 eliminazioni
  1. 271 0
      main/inc/lib/sessionmanager.lib.php

+ 271 - 0
main/inc/lib/sessionmanager.lib.php

@@ -455,6 +455,146 @@ class SessionManager
         return $num;
     }
 
+     /**
+     * Get session list for a session admin or platform admin.
+     *
+     * @param int   $userId   User Id for the session admin.
+     * @param array $options  Optional. Order and limit keys.
+     * @param bool  $getCount Optional. Whether to get all the results or only the count.
+     * @param array $columns  Optional. Columns from jqGrid.
+     *
+     * @return array
+     */
+    public static function getSessionsForAdmin(
+        $userId,
+        $options = [],
+        $getCount = false,
+        $columns = []
+    ) {
+        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
+        $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
+
+        $where = 'WHERE 1 = 1 ';
+
+        $userId = (int) $userId;
+
+        if (!api_is_platform_admin()) {
+            if (api_is_session_admin() &&
+                api_get_setting('allow_session_admins_to_manage_all_sessions') === 'false'
+            ) {
+                $where .= " AND s.session_admin_id = $userId ";
+            }
+        }
+
+        if (!api_is_platform_admin() &&
+            api_is_teacher() &&
+            api_get_setting('allow_teachers_to_create_sessions') === 'true'
+        ) {
+            $where .= " AND s.id_coach = $userId ";
+        }
+
+        $extraFieldModel = new ExtraFieldModel('session');
+        $conditions = $extraFieldModel->parseConditions($options);
+        $sqlInjectJoins = $conditions['inject_joins'];
+        $where .= $conditions['where'];
+        $sqlInjectWhere = $conditions['inject_where'];
+        $inject_extra_fields = $conditions['inject_extra_fields'];
+        $order = $conditions['order'];
+        $limit = $conditions['limit'];
+
+        $isMakingOrder = false;
+        $showCountUsers = false;
+
+        if ($getCount === true) {
+            $select = ' SELECT count(DISTINCT s.id) as total_rows ';
+        } else {
+            if (!empty($columns['column_model'])) {
+                foreach ($columns['column_model'] as $column) {
+                    if ($column['name'] == 'users') {
+                        $showCountUsers = true;
+                    }
+                }
+            }
+
+            $select =
+                "SELECT DISTINCT 
+                     s.name,
+                     s.display_start_date, 
+                     s.display_end_date, 
+                     access_start_date, 
+                     access_end_date, 
+                     s.visibility, 
+                     s.session_category_id, 
+                     $inject_extra_fields 
+                     s.id 
+             ";
+
+            if ($showCountUsers) {
+                $select .= ', count(su.user_id) users';
+            }
+            if (isset($options['order'])) {
+                $isMakingOrder = strpos($options['order'], 'category_name') === 0;
+            }
+        }
+
+        $isFilteringSessionCategory = strpos($where, 'category_name') !== false;
+        $isFilteringSessionCategoryWithName = strpos($where, 'sc.name') !== false;
+
+        if ($isMakingOrder || $isFilteringSessionCategory || $isFilteringSessionCategoryWithName) {
+            $sqlInjectJoins .= " LEFT JOIN $sessionCategoryTable sc ON s.session_category_id = sc.id ";
+
+            if ($isFilteringSessionCategory) {
+                $where = str_replace('category_name', 'sc.name', $where);
+            }
+
+            if ($isMakingOrder) {
+                $order = str_replace('category_name', 'sc.name', $order);
+            }
+        }
+
+        if ($showCountUsers) {
+            $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
+            $sqlInjectJoins .= " LEFT JOIN $tblSessionRelUser su ON (su.session_id = s.id)";
+        }
+
+        $query = "$select FROM $tblSession s $sqlInjectJoins $where $sqlInjectWhere";
+
+        if (api_is_multiple_url_enabled()) {
+            $tblAccessUrlRelSession = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
+            $accessUrlId = api_get_current_access_url_id();
+
+            if ($accessUrlId != -1) {
+                $where .= " AND ar.access_url_id = $accessUrlId ";
+                $query = "$select
+                    FROM $tblSession s $sqlInjectJoins
+                    INNER JOIN $tblAccessUrlRelSession ar
+                    ON (ar.session_id = s.id) $where";
+            }
+        }
+
+        if ($showCountUsers) {
+            $query .= ' GROUP by s.id';
+        }
+
+        $allowOrder = api_get_configuration_value('session_list_order');
+
+        if ($allowOrder) {
+            $order = ' ORDER BY position ASC';
+        }
+
+        $query .= $order;
+        $query .= $limit;
+        $result = Database::query($query);
+
+        $sessions = Database::store_result($result, 'ASSOC');
+
+        if ($getCount) {
+            return $sessions[0]['total_rows'];
+        }
+
+        return $sessions;
+    }
+
     /**
      * Gets the admin session list callback of the session/session_list.php page.
      *
@@ -765,6 +905,137 @@ class SessionManager
         return $num;
     }
 
+    /**
+     * Gets the admin session list callback of the session/session_list.php page.
+     *
+     * @param array $options           order and limit keys
+     * @param bool  $getCount          Whether to get all the results or only the count
+     * @param array $columns
+     * @param array $extraFieldsToLoad
+     *
+     * @return mixed Integer for number of rows, or array of results
+     * @assert ([],true) !== false
+     */
+    public static function formatSessionsAdminForGrid(
+        $options = [],
+        $getCount = false,
+        $columns = [],
+        $extraFieldsToLoad = []
+    ) {
+        $showCountUsers = false;
+
+        if (!$getCount && !empty($columns['column_model'])) {
+            foreach ($columns['column_model'] as $column) {
+                if ($column['name'] == 'users') {
+                    $showCountUsers = true;
+                }
+            }
+        }
+
+        $userId = api_get_user_id();
+        $sessions = self::getSessionsForAdmin($userId, $options, $getCount, $columns);
+
+        if ($getCount) {
+            return (int) $sessions;
+        }
+
+        $formattedSessions = [];
+
+        $categories = self::get_all_session_category();
+        $orderedCategories = [];
+        if (!empty($categories)) {
+            foreach ($categories as $category) {
+                $orderedCategories[$category['id']] = $category['name'];
+            }
+        }
+
+        $activeIcon = Display::return_icon('accept.png', get_lang('Active'));
+        $inactiveIcon = Display::return_icon('error.png', get_lang('Inactive'));
+
+        foreach ($sessions as $session) {
+            if ($showCountUsers) {
+                $session['users'] = self::get_users_by_session($session['id'], 0, true);
+            }
+            $url = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$session['id'];
+            if ($extraFieldsToLoad || api_is_drh()) {
+                $url = api_get_path(WEB_PATH).'session/'.$session['id'].'/about/';
+            }
+
+            $session['name'] = Display::url($session['name'], $url);
+
+            if (!empty($extraFieldsToLoad)) {
+                foreach ($extraFieldsToLoad as $field) {
+                    $extraFieldValue = new ExtraFieldValue('session');
+                    $fieldData = $extraFieldValue->getAllValuesByItemAndField(
+                        $session['id'],
+                        $field['id']
+                    );
+                    $fieldDataArray = [];
+                    $fieldDataToString = '';
+                    if (!empty($fieldData)) {
+                        foreach ($fieldData as $data) {
+                            $fieldDataArray[] = $data['value'];
+                        }
+                        $fieldDataToString = implode(', ', $fieldDataArray);
+                    }
+                    $session[$field['variable']] = $fieldDataToString;
+                }
+            }
+
+            if (isset($session['session_active']) && $session['session_active'] == 1) {
+                $session['session_active'] = $activeIcon;
+            } else {
+                $session['session_active'] = $inactiveIcon;
+            }
+
+            $session = self::convert_dates_to_local($session, true);
+
+            switch ($session['visibility']) {
+                case SESSION_VISIBLE_READ_ONLY: //1
+                    $session['visibility'] = get_lang('ReadOnly');
+                    break;
+                case SESSION_VISIBLE:           //2
+                case SESSION_AVAILABLE:         //4
+                    $session['visibility'] = get_lang('Visible');
+                    break;
+                case SESSION_INVISIBLE:         //3
+                    $session['visibility'] = api_ucfirst(get_lang('Invisible'));
+                    break;
+            }
+
+            // Cleaning double selects.
+            foreach ($session as $key => &$value) {
+                if (isset($optionsByDouble[$key]) || isset($optionsByDouble[$key.'_second'])) {
+                    $options = explode('::', $value);
+                }
+                $original_key = $key;
+                if (strpos($key, '_second') !== false) {
+                    $key = str_replace('_second', '', $key);
+                }
+
+                if (isset($optionsByDouble[$key]) &&
+                    isset($options[0]) &&
+                    isset($optionsByDouble[$key][$options[0]])
+                ) {
+                    if (strpos($original_key, '_second') === false) {
+                        $value = $optionsByDouble[$key][$options[0]]['option_display_text'];
+                    } else {
+                        $value = $optionsByDouble[$key][$options[1]]['option_display_text'];
+                    }
+                }
+            }
+
+            $categoryName = isset($orderedCategories[$session['session_category_id']])
+                ? $orderedCategories[$session['session_category_id']]
+                : '';
+            $session['category_name'] = $categoryName;
+            $formattedSessions[] = $session;
+        }
+
+        return $formattedSessions;
+    }
+
+
     /**
      * Gets the progress of learning paths in the given session.
      *