Sfoglia il codice sorgente

Fix exercise pagination

See

https://github.com/chamilo/chamilo-lms/commit/c764cb00459c81b0fd8c9a0d4a59471167b5b629#diff-2d6fb5c7b4fa49531a41e53e34bc97daR8460
Yannick Warnier 5 anni fa
parent
commit
b7801d78ad

+ 27 - 29
main/exercise/exercise.class.php

@@ -22,6 +22,7 @@ use Doctrine\DBAL\Types\Type;
  */
 class Exercise
 {
+    const PAGINATION_ITEMS_PER_PAGE = 5;
     public $iId;
     public $id;
     public $name;
@@ -8382,12 +8383,11 @@ class Exercise
      * Return an HTML table of exercises for on-screen printing, including
      * action icons. If no exercise is present and the user can edit the
      * course, show a "create test" button.
+
      *
      * @param int    $categoryId
      * @param string $keyword
      *
-     * @throws \Doctrine\ORM\Query\QueryException
-     *
      * @return string
      */
     public static function exerciseGrid($categoryId, $keyword = '')
@@ -8398,9 +8398,10 @@ class Exercise
         $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
         $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
 
-        //$page = (int) $page;
-        //$from = (int) $from;
-        //$limit = (int) $limit;
+        $categoryId = (int) $categoryId;
+        $keyword = Database::escape_string($keyword);
+        $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : null;
+        $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : null;
 
         $autoLaunchAvailable = false;
         if (api_get_course_setting('enable_exercise_auto_launch') == 1 &&
@@ -8421,22 +8422,26 @@ class Exercise
         $charset = 'utf-8';
         $token = Security::get_token();
         $userId = api_get_user_id();
-        $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
-            $userId,
-            $courseInfo
-        );
+        $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh($userId, $courseInfo);
         $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
         $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
 
-        $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : null;
-        $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : null;
-
         // Condition for the session
         $condition_session = api_get_session_condition($sessionId, true, true);
         $content = '';
 
+        $table = new SortableTableFromArrayConfig(
+            [],
+            1,
+            self::PAGINATION_ITEMS_PER_PAGE,
+            'exercises_cat_'.$categoryId
+        );
+
+        $limit = self::PAGINATION_ITEMS_PER_PAGE;
+        $page = $table->page_nr;
+        $from = $limit * ($page - 1);
+
         $categoryCondition = '';
-        $categoryId = (int) $categoryId;
         if (api_get_configuration_value('allow_exercise_categories')) {
             if (!empty($categoryId)) {
                 $categoryCondition = " AND exercise_category_id = $categoryId ";
@@ -8447,7 +8452,6 @@ class Exercise
 
         $keywordCondition = '';
         if (!empty($keyword)) {
-            $keyword = Database::escape_string($keyword);
             $keywordCondition = " AND title LIKE '%$keyword%' ";
         }
 
@@ -8469,8 +8473,8 @@ class Exercise
                         $condition_session 
                         $categoryCondition
                         $keywordCondition
-                    ORDER BY title";
-        // LIMIT $from , $limit";
+                    ORDER BY title
+                    LIMIT $from , $limit";
         } else {
             // Only for students
             $total_sql = "SELECT count(iid) as count 
@@ -8488,8 +8492,8 @@ class Exercise
                           $condition_session
                           $categoryCondition
                           $keywordCondition 
-                    ORDER BY title ";
-            // LIMIT $from , $limit";
+                    ORDER BY title
+                    LIMIT $from , $limit";
         }
 
         $result = Database::query($sql);
@@ -9180,16 +9184,16 @@ class Exercise
                     WHERE
                         d.c_id = $courseId AND
                         (d.path LIKE '%htm%') AND
-                        d.path  LIKE '".Database :: escape_string($uploadPath.'/%/%')."' ";
-        // LIMIT $from , $limit"; // only .htm or .html files listed
+                        d.path  LIKE '".Database :: escape_string($uploadPath.'/%/%')."'
+                    LIMIT $from , $limit"; // only .htm or .html files listed
         } else {
             $sql = "SELECT d.iid, d.path as path, d.comment as comment
                     FROM $TBL_DOCUMENT d
                     WHERE
                         d.c_id = $courseId AND
                         (d.path LIKE '%htm%') AND
-                        d.path  LIKE '".Database :: escape_string($uploadPath.'/%/%')."' ";
-            // LIMIT $from , $limit";
+                        d.path  LIKE '".Database :: escape_string($uploadPath.'/%/%')."'
+                    LIMIT $from , $limit";
         }
 
         $result = Database::query($sql);
@@ -9308,7 +9312,6 @@ class Exercise
                         )
                     );
 
-                    $actions = '';
                     if (!empty($attempt)) {
                         $actions = '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq().'&path='.$path.'&filter_by_user='.$userId.'">'.Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
                         $attemptText = get_lang('LatestAttempt').' : ';
@@ -9356,12 +9359,7 @@ class Exercise
                 return '';
             }
 
-            $table = new SortableTableFromArrayConfig(
-                $tableRows,
-                1,
-                20,
-                'exercises_cat'.$categoryId
-            );
+            $table->setTableData($tableRows);
 
             $table->setTotalNumberOfItems($total);
             $table->set_additional_parameters([

+ 6 - 4
main/exercise/exercise.php

@@ -275,7 +275,6 @@ Event::event_access_tool(TOOL_QUIZ);
 $logInfo = [
     'tool' => TOOL_QUIZ,
     'tool_id' => (int) $exerciseId,
-    'tool_id_detail' => 0,
     'action' => isset($_REQUEST['learnpath_id']) ? 'learnpath_id' : '',
     'action_details' => isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : '',
 ];
@@ -514,6 +513,10 @@ if ($origin !== 'learnpath') {
 }
 Display::display_introduction_section(TOOL_QUIZ);
 
+// Selects $limit exercises at the same time
+// maximum number of exercises on a same page
+$limit = Exercise::PAGINATION_ITEMS_PER_PAGE;
+
 HotPotGCt($documentPath, 1, $userId);
 
 $token = Security::get_token();
@@ -609,13 +612,12 @@ if ($is_allowedToEdit) {
         [6, 1, 5]
     );
 }
-
 if (api_get_configuration_value('allow_exercise_categories') === false) {
-    echo Exercise::exerciseGrid(0, $keyword);
+    echo Exercise::exerciseGrid(0,  $keyword);
 } else {
     if (empty($categoryId)) {
         echo Display::page_subheader(get_lang('NoCategory'));
-        echo Exercise::exerciseGrid(0, $keyword);
+        echo Exercise::exerciseGrid(0,  $keyword);
         $counter = 0;
         $manager = new ExerciseCategoryManager();
         $categories = $manager->getCategories($courseId);

+ 88 - 0
main/inc/lib/SortableTableFromArray.php

@@ -0,0 +1,88 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * Sortable table which can be used for data available in an array.
+ *
+ * @package chamilo.library
+ */
+class SortableTableFromArray extends SortableTable
+{
+    /**
+     * The array containing all data for this table.
+     */
+    public $table_data;
+
+    /**
+     * Constructor.
+     *
+     * @param array  $table_data
+     * @param int    $default_column
+     * @param int    $default_items_per_page
+     * @param string $tableName
+     * @param string $get_total_number_function
+     * @param string $tableId
+     */
+    public function __construct(
+        $table_data,
+        $default_column = 1,
+        $default_items_per_page = 20,
+        $tableName = 'tablename',
+        $get_total_number_function = null,
+        $tableId = ''
+    ) {
+        parent:: __construct(
+            $tableName,
+            $get_total_number_function,
+            null,
+            $default_column,
+            $default_items_per_page,
+            null,
+            $tableId
+        );
+        $this->table_data = $table_data;
+    }
+
+    /**
+     * Get table data to show on current page.
+     *
+     * @see SortableTable#get_table_data
+     */
+    public function get_table_data(
+        $from = 1,
+        $per_page = null,
+        $column = null,
+        $direction = null,
+        $sort = true
+    ) {
+        if ($sort) {
+            $content = TableSort::sort_table(
+                $this->table_data,
+                $this->column,
+                $this->direction === 'ASC' ? SORT_ASC : SORT_DESC
+            );
+        } else {
+            $content = $this->table_data;
+        }
+
+        return array_slice($content, $from, $this->per_page);
+    }
+
+    /**
+     * Get total number of items.
+     *
+     * @see SortableTable#get_total_number_of_items
+     */
+    public function get_total_number_of_items()
+    {
+        if (isset($this->total_number_of_items) && !empty($this->total_number_of_items)) {
+            return $this->total_number_of_items;
+        } else {
+            if (!empty($this->table_data)) {
+                return count($this->table_data);
+            }
+
+            return 0;
+        }
+    }
+}

+ 113 - 0
main/inc/lib/SortableTableFromArrayConfig.php

@@ -0,0 +1,113 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+
+/**
+ * Sortable table which can be used for data available in an array.
+ *
+ * Is a variation of SortableTableFromArray because we add 2 new arrays  $column_show and $column_order
+ * $column_show is an array that lets us decide which are going to be the columns to show
+ * $column_order is an array that lets us decide the ordering of the columns
+ * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
+ * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
+ *
+ * @package chamilo.library
+ */
+class SortableTableFromArrayConfig extends SortableTable
+{
+    /**
+     * The array containing the columns that will be show
+     * i.e $column_show=array('1','0','0'); we will show only the 1st column.
+     */
+    private $column_show;
+
+    /**
+     * The array containing the real sort column
+     * $column_order=array('1''4','3','4');
+     * The 2nd column will be order like the 4th column.
+     */
+    private $column_order;
+
+    private $doc_filter;
+
+    /**
+     * Constructor.
+     *
+     * @param array  $data         All the information of the table
+     * @param int    $column       Default column that will be use in the sorts functions
+     * @param int    $itemsPerPage quantity of pages that we are going to see
+     * @param string $tableName    Name of the table
+     * @param array  $column_show  An array with binary values 1: we show the column 2: we don't show it
+     * @param array  $column_order an array of integers that let us decide how the columns are going to be sort
+     * @param string $direction
+     * @param bool   $doc_filter   special modification to fix the document name order
+     */
+    public function __construct(
+        $data,
+        $column = 1,
+        $itemsPerPage = 20,
+        $tableName = 'tablename',
+        $column_show = [],
+        $column_order = [],
+        $direction = 'ASC',
+        $doc_filter = false
+    ) {
+        $this->column_show = $column_show;
+        $this->column_order = $column_order;
+        $this->doc_filter = $doc_filter;
+
+        parent::__construct(
+            $tableName,
+            null,
+            null,
+            $column,
+            $itemsPerPage,
+            $direction
+        );
+        $this->table_data = $data;
+    }
+
+    /**
+     * Get table data to show on current page.
+     *
+     * @see SortableTable#get_table_data
+     */
+    public function get_table_data(
+        $from = 1,
+        $per_page = null,
+        $column = null,
+        $direction = null,
+        $sort = true
+    ) {
+        $table = TableSort::sort_table_config(
+            $this->table_data,
+            $this->column,
+            $this->direction === 'ASC' ? SORT_ASC : SORT_DESC,
+            $this->column_show,
+            $this->column_order,
+            SORT_REGULAR,
+            $this->doc_filter
+        );
+
+//        return array_slice($table, $from, $this->per_page);
+        return $table;
+    }
+
+    /**
+     * Get total number of items.
+     *
+     * @see SortableTable#get_total_number_of_items
+     */
+    public function get_total_number_of_items()
+    {
+        if (isset($this->total_number_of_items) && !empty($this->total_number_of_items)) {
+            return $this->total_number_of_items;
+        } else {
+            if (!empty($this->table_data)) {
+                return count($this->table_data);
+            }
+
+            return 0;
+        }
+    }
+}

+ 1 - 1
main/inc/lib/exercise.lib.php

@@ -184,7 +184,7 @@ class ExerciseLib
                     $form = new FormValidator('free_choice_'.$questionId);
                     $config = [
                         'ToolbarSet' => 'TestFreeAnswer',
-                        'id' => 'choice['.$questionId.']'
+                        'id' => 'choice['.$questionId.']',
                     ];
                     $form->addHtmlEditor(
                         'choice['.$questionId.']',

+ 8 - 184
main/inc/lib/sortable_table.class.php

@@ -95,7 +95,13 @@ class SortableTable extends HTML_Table
     public $use_jqgrid = false;
     public $table_id = null;
     public $headers = [];
+
+    /**
+     * The array containing all data for this table.
+     */
+    public $table_data;
     /**
+     *
      * @var array Columns to hide
      */
     private $columnsToHide = [];
@@ -1060,194 +1066,12 @@ class SortableTable extends HTML_Table
 
         return $data;
     }
-}
-
-/**
- * Sortable table which can be used for data available in an array.
- *
- * @package chamilo.library
- */
-class SortableTableFromArray extends SortableTable
-{
-    /**
-     * The array containing all data for this table.
-     */
-    public $table_data;
-
-    /**
-     * Constructor.
-     *
-     * @param array  $table_data
-     * @param int    $default_column
-     * @param int    $default_items_per_page
-     * @param string $tableName
-     * @param string $get_total_number_function
-     * @param string $tableId
-     */
-    public function __construct(
-        $table_data,
-        $default_column = 1,
-        $default_items_per_page = 20,
-        $tableName = 'tablename',
-        $get_total_number_function = null,
-        $tableId = ''
-    ) {
-        parent:: __construct(
-            $tableName,
-            $get_total_number_function,
-            null,
-            $default_column,
-            $default_items_per_page,
-            null,
-            $tableId
-        );
-        $this->table_data = $table_data;
-    }
 
     /**
-     * Get table data to show on current page.
-     *
-     * @see SortableTable#get_table_data
+     * @param array $data
      */
-    public function get_table_data(
-        $from = 1,
-        $per_page = null,
-        $column = null,
-        $direction = null,
-        $sort = true
-    ) {
-        if ($sort) {
-            $content = TableSort::sort_table(
-                $this->table_data,
-                $this->column,
-                $this->direction === 'ASC' ? SORT_ASC : SORT_DESC
-            );
-        } else {
-            $content = $this->table_data;
-        }
-
-        return array_slice($content, $from, $this->per_page);
-    }
-
-    /**
-     * Get total number of items.
-     *
-     * @see SortableTable#get_total_number_of_items
-     */
-    public function get_total_number_of_items()
+    public function setTableData($data)
     {
-        if (isset($this->total_number_of_items) && !empty($this->total_number_of_items)) {
-            return $this->total_number_of_items;
-        } else {
-            if (!empty($this->table_data)) {
-                return count($this->table_data);
-            }
-
-            return 0;
-        }
-    }
-}
-
-/**
- * Sortable table which can be used for data available in an array.
- *
- * Is a variation of SortableTableFromArray because we add 2 new arrays  $column_show and $column_order
- * $column_show is an array that lets us decide which are going to be the columns to show
- * $column_order is an array that lets us decide the ordering of the columns
- * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
- * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
- *
- * @package chamilo.library
- */
-class SortableTableFromArrayConfig extends SortableTable
-{
-    /**
-     * The array containing the columns that will be show
-     * i.e $column_show=array('1','0','0'); we will show only the 1st column.
-     */
-    private $column_show;
-
-    /**
-     * The array containing the real sort column
-     * $column_order=array('1''4','3','4');
-     * The 2nd column will be order like the 4th column.
-     */
-    private $column_order;
-    /**
-     * The array containing all data for this table.
-     */
-    private $table_data;
-    private $doc_filter;
-
-    /**
-     * Constructor.
-     *
-     * @param array  $data         All the information of the table
-     * @param int    $column       Default column that will be use in the sorts functions
-     * @param int    $itemsPerPage quantity of pages that we are going to see
-     * @param string $tableName    Name of the table
-     * @param array  $column_show  An array with binary values 1: we show the column 2: we don't show it
-     * @param array  $column_order an array of integers that let us decide how the columns are going to be sort
-     * @param string $direction
-     * @param bool   $doc_filter   special modification to fix the document name order
-     */
-    public function __construct(
-        $data,
-        $column = 1,
-        $itemsPerPage = 20,
-        $tableName = 'tablename',
-        $column_show = [],
-        $column_order = [],
-        $direction = 'ASC',
-        $doc_filter = false
-    ) {
-        $this->column_show = $column_show;
-        $this->column_order = $column_order;
-        $this->doc_filter = $doc_filter;
-
-        parent::__construct(
-            $tableName,
-            null,
-            null,
-            $column,
-            $itemsPerPage,
-            $direction
-        );
         $this->table_data = $data;
     }
-
-    /**
-     * Get table data to show on current page.
-     *
-     * @see SortableTable#get_table_data
-     */
-    public function get_table_data(
-        $from = 1,
-        $per_page = null,
-        $column = null,
-        $direction = null,
-        $sort = true
-    ) {
-        $content = TableSort::sort_table_config(
-            $this->table_data,
-            $this->column,
-            $this->direction === 'ASC' ? SORT_ASC : SORT_DESC,
-            $this->column_show,
-            $this->column_order,
-            SORT_REGULAR,
-            $this->doc_filter
-        );
-
-        return array_slice($content, $from, $this->per_page);
-    }
-
-    /**
-     * Get total number of items.
-     *
-     * @see SortableTable#get_total_number_of_items
-     */
-    public function get_total_number_of_items()
-    {
-        return count($this->table_data);
-    }
 }