Browse Source

Replace custom queries to use CourseManager and UserManager libs

- Take into account the url id in UserManager lib
- Use CourseManager::count_courses instead custom sql
- Use CourseManager::get_courses_list instead custom sql
- Use UserManager::get_number_of_users instead custom sql
- Use UserManager::get_user_list instead custom sql
- Minor format code

See BT#12534
jmontoyaa 8 years ago
parent
commit
445cf69bfc
2 changed files with 138 additions and 133 deletions
  1. 117 118
      main/inc/lib/myspace.lib.php
  2. 21 15
      main/inc/lib/usermanager.lib.php

+ 117 - 118
main/inc/lib/myspace.lib.php

@@ -261,7 +261,6 @@ class MySpace
             // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
             $exercises_results = self::exercises_results($user_id, $courseCode);
             $return .= '    <td width="105px"><div>'.(is_null($exercises_results['percentage']) ? '' : $exercises_results['score_obtained'].'/'.$exercises_results['score_possible'].' ( '.$exercises_results['percentage'].'% )').'</div></td>';
-            //$return .= '  <td><div>'.$exercises_results['score_possible'].'</div></td>';
             $return .= '    <td><div>'.$exercises_results['questions_answered'].'</div></td>';
             $return .= '    <td><div>'.Tracking::get_last_connection_date_on_the_course($user_id, $courseInfo).'</div></td>';
             $return .= '<tr>';
@@ -271,7 +270,8 @@ class MySpace
     }
 
     /**
-     * Display a sortable table that contains an overview off all the reporting progress of all users and all courses the user is subscribed to
+     * Display a sortable table that contains an overview off all the
+     * reporting progress of all users and all courses the user is subscribed to
      * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
      * @version Dokeos 1.8.6
      * @since October 2008
@@ -279,30 +279,26 @@ class MySpace
     public static function display_tracking_user_overview()
     {
         self::display_user_overview_export_options();
-
-        $t_head = '    <table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
-        //$t_head .= '  <caption>'.get_lang('CourseInformation').'</caption>';
-        $t_head .=      '<tr>';
-        $t_head .= '        <th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
-        //$t_head .= '      <th><div style="width:40px">'.get_lang('AvgExercisesScore').'</div></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
-        $t_head .= '        <th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
-        //$t_head .= '      <th><div>'.get_lang('TotalExercisesScorePossible').'</div></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
-        //$t_head .= '      <th><div>'.get_lang('TotalExercisesScorePercentage').'</div></th>';
-        //$t_head .= '      <th><div style="width:60px">'.get_lang('FirstLogin').'</div></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
-        $t_head .= '    </tr></table>';
-
+        $t_head = '<table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
+        $t_head .= '<tr>';
+        $t_head .= '<th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
+        $t_head .= '<th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
+        $t_head .= '</tr></table>';
         $addparams = array('view' => 'admin', 'display' => 'user');
-
-        $table = new SortableTable('tracking_user_overview', array('MySpace','get_number_of_users_tracking_overview'), array('MySpace','get_user_data_tracking_overview'), 0);
+        $table = new SortableTable(
+            'tracking_user_overview',
+            array('MySpace', 'get_number_of_users_tracking_overview'),
+            array('MySpace', 'get_user_data_tracking_overview'),
+            0
+        );
         $table->additional_parameters = $addparams;
-
         $table->set_header(0, get_lang('OfficialCode'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
         if (api_is_western_name_order()) {
             $table->set_header(1, get_lang('FirstName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
@@ -841,26 +837,24 @@ class MySpace
      */
     public static function display_tracking_course_overview()
     {
-        $t_head = '    <table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
-        //$t_head .= '  <caption>'.get_lang('CourseInformation').'</caption>';
-        $t_head .=      '<tr>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
-        //$t_head .= '      <th><div style="width:40px">'.get_lang('AvgExercisesScore').'</div></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
-        $t_head .= '        <th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
-        //$t_head .= '      <th><div>'.get_lang('TotalExercisesScorePossible').'</div></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
-        //$t_head .= '      <th><div>'.get_lang('TotalExercisesScorePercentage').'</div></th>';
-        //$t_head .= '      <th><div style="width:60px">'.get_lang('FirstLogin').'</div></th>';
-        $t_head .= '        <th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
-        $t_head .= '    </tr></table>';
-
+        $t_head = '<table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
+        $t_head .= '<tr>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
+        $t_head .= '<th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
+        $t_head .= '<th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
+        $t_head .= '</tr></table>';
         $addparams = array('view' => 'admin', 'display' => 'courseoverview');
-
-        $table = new SortableTable('tracking_session_overview', array('MySpace', 'get_total_number_courses'), array('MySpace','get_course_data_tracking_overview'), 1);
+        $table = new SortableTable(
+            'tracking_session_overview',
+            array('MySpace', 'get_total_number_courses'),
+            array('MySpace', 'get_course_data_tracking_overview'),
+            1
+        );
         $table->additional_parameters = $addparams;
 
         $table->set_header(0, '', false, null, array('style' => 'display: none'));
@@ -877,38 +871,41 @@ class MySpace
      */
     public static function get_total_number_courses()
     {
-        $table = Database::get_main_table(TABLE_MAIN_COURSE);
-        $sql = "SELECT COUNT(id) count FROM $table";
-        $result = Database::query($sql);
-        $row = Database::fetch_assoc($result);
-
-        return $row['count'];
+        return CourseManager::count_courses(api_get_current_access_url_id());
     }
 
     /**
      * Get data for the courses
      *
-     * @param int Inferior limit
-     * @param int Number of items to select
-     * @param string Column to order on
-     * @param string Order direction
+     * @param int $from Inferior limit
+     * @param int $numberItems Number of items to select
+     * @param string $column Column to order on
+     * @param string $direction Order direction
      * @return array Results
      */
-    public static function get_course_data_tracking_overview($from, $number_of_items, $column, $direction)
+    public static function get_course_data_tracking_overview($from, $numberItems, $column, $direction)
     {
-        $main_course_table = Database::get_main_table(TABLE_MAIN_COURSE);
-        $from = intval($from);
-        $number_of_items = intval($number_of_items);
+        $courses = CourseManager::get_courses_list(
+            $from,
+            $numberItems,
+            $column,
+            $direction,
+             -1,
+            '',
+            api_get_current_access_url_id()
+        );
 
-        $sql = "SELECT code AS col0, title AS col1 FROM $main_course_table";
-        $sql .= " ORDER BY col$column $direction ";
-        $sql .= " LIMIT $from,$number_of_items";
-        $result = Database::query($sql);
-        $return = array ();
-        while ($course = Database::fetch_row($result)) {
-            $return[] = $course;
+        $list = [];
+        foreach ($courses as $course) {
+            $list[] = [
+                '0' => $course['code'],
+                'col0' => $course['code'],
+                '1' => $course['title'],
+                'col1' => $course['title']
+            ];
         }
-        return $return;
+
+        return $list;
     }
 
     /**
@@ -1246,8 +1243,8 @@ class MySpace
             $list[] = [
                 '0' => $session['id'],
                 'col0' => $session['id'],
-                '1' => $session['name'],
-                'col1' => $session['name']
+                '1' => strip_tags($session['name']),
+                'col1' => strip_tags($session['name'])
             ];
         }
 
@@ -1278,7 +1275,7 @@ class MySpace
         $sql = "SELECT * FROM $tbl_course AS c
                 INNER JOIN $tbl_session_rel_course AS sc
                 ON sc.c_id = c.id
-                WHERE sc.session_id = '".$session_id."';";
+                WHERE sc.session_id = '".$session_id."'";
         $result = Database::query($sql);
         while ($row = Database::fetch_object($result)) {
             $courseId = $row->c_id;
@@ -1851,70 +1848,73 @@ class MySpace
     }
 
     /**
-     * get the numer of users of the platform
+     * Get the number of users of the platform
      *
      * @return integer
      *
-     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
-     * @version Dokeos 1.8.6
-     * @since October 2008
      */
     public static function get_number_of_users_tracking_overview()
     {
-        $table = Database::get_main_table(TABLE_MAIN_USER);
-        $sql = "SELECT COUNT(id) count FROM $table";
-        $result = Database::query($sql);
-        $row = Database::fetch_assoc($result);
-
-        return $row['count'];
+        return UserManager::get_number_of_users(0, api_get_current_access_url_id());
     }
 
     /**
-     * get all the data for the sortable table of the reporting progress of all users and all the courses the user is subscribed to.
+     * Get all the data for the sortable table of the reporting progress of
+     * all users and all the courses the user is subscribed to.
      *
-     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
-     * @version Dokeos 1.8.6
-     * @since October 2008
+     * @param int $from
+     * @param int $numberItems
+     * @param int $column
+     * @param string $direction
      */
-    public static function get_user_data_tracking_overview($from, $number_of_items, $column, $direction)
+    public static function get_user_data_tracking_overview($from, $numberItems, $column, $direction)
     {
-        // database table definition
-        $access_url_id = api_get_current_access_url_id();
-        $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
-        $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
-        $condition_multi_url = null;
-        if (api_is_multiple_url_enabled()) {
-            $condition_multi_url = ", $tbl_url_rel_user as url_user
-            WHERE user.user_id=url_user.user_id AND access_url_id='$access_url_id'";
+        $conditions = [];
+        $isWestern = api_is_western_name_order();
+        switch ($column) {
+            case '0':
+                $column = 'official_code';
+                break;
+            case '1':
+                $column = $isWestern ? 'firstname' : 'lastname';
+                break;
+            case '2':
+                $column = $isWestern ? 'lastname' : 'firstname';
+                break;
+            case '3':
+                $column = 'username';
+                break;
+            case '4':
+                $column = 'username';
+                break;
         }
 
-        global $export_csv;
-        if ($export_csv) {
-            $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
-        } else {
-            $is_western_name_order = api_is_western_name_order();
-        }
-        $sql = "SELECT
-                    official_code AS col0,
-                    ".($is_western_name_order ? "
-                    firstname       AS col1,
-                    lastname        AS col2,
-                    " : "
-                    lastname        AS col1,
-                    firstname       AS col2,
-                    ").
-                    "username       AS col3,
-                    user.user_id        AS col4
-                FROM
-                $main_user_table as user $condition_multi_url
-                ";
-        $sql .= " ORDER BY col$column $direction ";
-        $sql .= " LIMIT $from,$number_of_items";
-        $result = Database::query($sql);
-        $return = array ();
-        while ($user = Database::fetch_row($result)) {
-            $return[] = $user;
+        $order = [
+            "$column $direction"
+        ];
+        $userList = UserManager::get_user_list($conditions, $order, $from, $numberItems);
+        $return = [];
+        foreach ($userList as $user) {
+            $firstPosition = $user['lastname'];
+            $secondPosition = $user['firstname'];
+            if ($isWestern) {
+                $firstPosition = $user['firstname'];
+                $secondPosition = $user['lastname'];
+            }
+            $return[] = [
+                '0' => $user['official_code'],
+                'col0' => $user['official_code'],
+                '1' => $firstPosition,
+                'col1' => $firstPosition,
+                '2' => $secondPosition,
+                'col2' => $secondPosition,
+                '3' => $user['username'],
+                'col3' => $user['username'],
+                '4' => $user['user_id'],
+                'col4' => $user['user_id']
+            ];
         }
+
         return $return;
     }
 
@@ -1929,7 +1929,6 @@ class MySpace
     public static function get_user_overview_export_extra_fields($user_id)
     {
         // include the user manager
-
         $extra_data = UserManager::get_extra_user_data($user_id, true);
         return $extra_data;
     }

+ 21 - 15
main/inc/lib/usermanager.lib.php

@@ -1425,26 +1425,33 @@ class UserManager
         $limit_to = false
     ) {
         $user_table = Database::get_main_table(TABLE_MAIN_USER);
+        $userUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
+        $urlId = api_get_current_access_url_id();
+
         $return_array = array();
-        $sql_query = "SELECT * FROM $user_table";
+        $sql = "SELECT user.* FROM $user_table user
+                INNER JOIN $userUrlTable url_user
+                ON (user.user_id = url_user.user_id)
+                WHERE url_user.access_url_id = $urlId
+        ";
+
         if (count($conditions) > 0) {
-            $sql_query .= ' WHERE ';
             foreach ($conditions as $field => $value) {
                 $field = Database::escape_string($field);
                 $value = Database::escape_string($value);
-                $sql_query .= "$field = '$value'";
+                $sql .= "$field = '$value'";
             }
         }
         if (count($order_by) > 0) {
-            $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false);
+            $sql .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false);
         }
 
         if (is_numeric($limit_from) && is_numeric($limit_from)) {
             $limit_from = intval($limit_from);
             $limit_to = intval($limit_to);
-            $sql_query .= " LIMIT $limit_from, $limit_to";
+            $sql .= " LIMIT $limit_from, $limit_to";
         }
-        $sql_result = Database::query($sql_query);
+        $sql_result = Database::query($sql);
         while ($result = Database::fetch_array($sql_result)) {
             $result['complete_name'] = api_get_person_name($result['firstname'], $result['lastname']);
             $return_array[] = $result;
@@ -3372,20 +3379,19 @@ class UserManager
      * @param   int     Access URL ID (optional)
      * @return    mixed    Number of users or false on error
      */
-    public static function get_number_of_users($status = 0, $access_url_id = null)
+    public static function get_number_of_users($status = 0, $access_url_id = 1)
     {
         $t_u = Database::get_main_table(TABLE_MAIN_USER);
         $t_a = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
-        $sql = "SELECT count(*) FROM $t_u u";
-        $sql2 = '';
+        $sql = "SELECT count(u.id) 
+                FROM $t_u u 
+                INNER JOIN $t_a url_user
+                ON (u.id = url_user.user_id)
+                WHERE url_user.access_url_id = $access_url_id                
+        ";
         if (is_int($status) && $status > 0) {
-            $sql2 .= " WHERE u.status = $status ";
-        }
-        if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) {
-            $sql .= ", $t_a a ";
-            $sql2 .= " AND a.access_url_id = $access_url_id AND u.id = a.user_id ";
+            $sql .= " AND u.status = $status ";
         }
-        $sql = $sql.$sql2;
         $res = Database::query($sql);
         if (Database::num_rows($res) === 1) {
             return (int) Database::result($res, 0, 0);