Browse Source

Special fix in the document table when ordering elements by name see #2499

Julio Montoya 14 years ago
parent
commit
85a076b622
3 changed files with 85 additions and 46 deletions
  1. 26 21
      main/document/document.php
  2. 8 4
      main/inc/lib/sortabletable.class.php
  3. 51 21
      main/inc/lib/tablesort.lib.php

+ 26 - 21
main/document/document.php

@@ -119,7 +119,6 @@ $curdirpathurl = urlencode($curdirpath);
 // If the path is not found (no document id), set the path to /
 $document_id = DocumentManager::get_document_id($_course, $curdirpath);
 
-
 if (!$document_id) {    
     $document_id = DocumentManager::get_document_id(api_get_course_info(), $curdirpath);
 }
@@ -170,7 +169,6 @@ if ($tool_visibility == '0' && $to_group_id == '0' && !($is_allowed_to_edit || $
     api_not_allowed(true);
 }
 
-
 $htmlHeadXtra[] =
 "<script type=\"text/javascript\">
 function confirmation (name) {
@@ -361,7 +359,7 @@ $image_files_only = '';
 if ($is_certificate_mode) {
     $interbreadcrumb[]= array('url' => '../gradebook/index.php', 'name' => get_lang('Gradebook'));
 } else {
-    if ( (isset($_GET['id']) && $_GET['id'] != 0) || isset($_GET['curdirpath'])) {
+    if ((isset($_GET['id']) && $_GET['id'] != 0) || isset($_GET['curdirpath'])) {
         $interbreadcrumb[]= array('url' => 'document.php', 'name' => get_lang('Documents'));
     } else {
         $interbreadcrumb[]= array('url' => '#', 'name' => get_lang('Documents'));
@@ -683,7 +681,7 @@ if ($is_allowed_to_edit) {
 
 /*	TEMPLATE ACTION */
 //Only teacher and all users into their group
-if($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $current_session_id)){
+if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $current_session_id)){
     if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
 
         $document_id_for_template = intval($_GET['add_as_template']);
@@ -785,13 +783,13 @@ if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isse
     }
 }
 
-
 /*	GET ALL DOCUMENT DATA FOR CURDIRPATH */
 if(isset($_GET['keyword']) && !empty($_GET['keyword'])) {
-    $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, $search=true);    
+    $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, true);    
 } else {
-    $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, $search=false);
+    $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, false);
 }
+
 $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
 if ($folders === false) {
     $folders = array();
@@ -838,7 +836,9 @@ if (isset($docs_and_folders) && is_array($docs_and_folders)) {
 
     //while (list($key, $id) = each($docs_and_folders)) {
     foreach ($docs_and_folders as $key => $document_data) {
-        $row = array();
+        $row = array();        
+        $row['id']   = $document_data['id'];
+        $row['type'] = $document_data['filetype'];        
 
         // If the item is invisible, wrap it in a span with class invisible
         $invisibility_span_open  = ($document_data['visibility'] == 0) ? '<span class="invisible">' : '';
@@ -852,6 +852,7 @@ if (isset($docs_and_folders) && is_array($docs_and_folders)) {
         } else {
             $document_name = basename($document_data['path']);
         }
+        $row['name'] = $document_name;
         // Data for checkbox
         if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
             $row[] = $document_data['path'];
@@ -920,6 +921,7 @@ if (isset($docs_and_folders) && is_array($docs_and_folders)) {
         }
         $row[] = $last_edit_date;
         $row[] = $size;
+        $row[] = $document_name;
         $total_size = $total_size + $size;
 
         if ((isset($_GET['keyword']) && search_keyword($document_name, $_GET['keyword'])) || !isset($_GET['keyword']) || empty($_GET['keyword'])) {            
@@ -1043,22 +1045,29 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
     $column_show[] = 1;
 }
 $column_show[] = 0;
+$column_show[] = 0;
 
 $column_order = array();
 
-if (count($row) == 8) {
+if (count($row) == 12) {
+    //teacher
+    $column_order[2] = 8; //name
     $column_order[3] = 7;
     $column_order[4] = 6;
-} elseif (count($row) == 6) {
+} elseif (count($row) == 10) {
+    //student
+    $column_order[1] = 6;
     $column_order[2] = 5;
     $column_order[3] = 4;
 }
 
 $default_column = $is_allowed_to_edit ? 2 : 1;
 $tablename = $is_allowed_to_edit ? 'teacher_table' : 'student_table';
-$table = new SortableTableFromArrayConfig($sortable_data, $default_column, 20, $tablename, $column_show, $column_order, 'ASC');
 
-if(isset($_GET['keyword'])){
+//var_dump($column_show,$column_order);
+$table = new SortableTableFromArrayConfig($sortable_data, $default_column, 20, $tablename, $column_show, $column_order, 'ASC', true);
+
+if(isset($_GET['keyword'])) {
     $query_vars['keyword'] = Security::remove_XSS($_GET['keyword']);
 }else{
     $query_vars['curdirpath'] = $curdirpath;
@@ -1073,17 +1082,15 @@ $table->set_additional_parameters($query_vars);
 $column = 0;
 
 if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
-    $table->set_header($column++, '', false,array ('style' => 'width:30px;'));
+    $table->set_header($column++, '', false, array('style' => 'width:30px;'));
 }
-$table->set_header($column++, get_lang('Type'),true,array ('style' => 'width:30px;'));
+$table->set_header($column++, get_lang('Type'),true, array('style' => 'width:30px;'));
 $table->set_header($column++, get_lang('Name'));
-
-//$column_header[] = array(get_lang('Comment'), true); // Display comment under the document name
-$table->set_header($column++, get_lang('Size'),true,array ('style' => 'width:50px;'));
-$table->set_header($column++, get_lang('Date'),true,array ('style' => 'width:150px;'));
+$table->set_header($column++, get_lang('Size'),true, array('style' => 'width:50px;'));
+$table->set_header($column++, get_lang('Date'),true, array('style' => 'width:150px;'));
 // Admins get an edit column
 if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $current_session_id)) {
-    $table->set_header($column++, get_lang('Actions'), false,array ('style' => 'width:180px;'));
+    $table->set_header($column++, get_lang('Actions'), false, array ('style' => 'width:180px;'));
 }
 
 // Actions on multiple selected documents
@@ -1096,9 +1103,7 @@ if (count($docs_and_folders) > 1) {
         $table->set_form_actions($form_action, 'path');        
     }    
 }
-//echo '<div class="thumbnails yoxview">';
 $table->display();
-//echo '</div>';
 
 if (count($docs_and_folders) > 1) {
     if ($is_allowed_to_edit || $group_member_with_upload_rights) {

+ 8 - 4
main/inc/lib/sortabletable.class.php

@@ -901,6 +901,8 @@ class SortableTableFromArrayConfig extends SortableTable {
 	 * The array containing all data for this table
 	 */
 	private $table_data;
+	
+	private $doc_filter;
 
 	/**
 	 * Constructor
@@ -910,10 +912,12 @@ class SortableTableFromArrayConfig extends SortableTable {
 	 * @param int $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 bool special modification to fix the document name order 
 	 */
-	public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename', $column_show = null, $column_order = null, $direction = 'ASC') {
-		$this->column_show = $column_show;
+	public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename', $column_show = null, $column_order = null, $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, $default_column, $default_items_per_page, $direction);
 		$this->table_data = $table_data;
 	}
@@ -923,7 +927,7 @@ class SortableTableFromArrayConfig extends SortableTable {
 	 * @see SortableTable#get_table_data
 	 */
 	public function get_table_data($from = 1) {
-		$content = TableSort :: sort_table_config($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC, $this->column_show, $this->column_order);
+		$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);
 	}
 
@@ -931,7 +935,7 @@ class SortableTableFromArrayConfig extends SortableTable {
 	 * Get total number of items
 	 * @see SortableTable#get_total_number_of_items
 	 */
-	public function get_total_number_of_items () {
+	public function get_total_number_of_items() {
 		return count($this->table_data);
 	}
 }

+ 51 - 21
main/inc/lib/tablesort.lib.php

@@ -81,7 +81,7 @@ class TableSort {
 	 * @return array The sorted dataset
 	 * @author bart.mollet@hogent.be
 	 */
-	public function sort_table_config($data, $column = 0, $direction = SORT_ASC, $column_show = null, $column_order = null, $type = SORT_REGULAR) {
+	public function sort_table_config($data, $column = 0, $direction = SORT_ASC, $column_show = null, $column_order = null, $type = SORT_REGULAR, $doc_filter = false) {
 
 		if (!is_array($data) || empty($data)) {
 			return array();
@@ -111,27 +111,57 @@ class TableSort {
 			} else {
 				$type = SORT_STRING;
 			}
-		}
-
-		$compare_operator = $direction == SORT_ASC ? '>' : '<=';
-		switch ($type) {
-			case SORT_NUMERIC:
-				$compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
-				break;
-			case SORT_IMAGE:
-				$compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
-				break;
-			case SORT_DATE:
-				$compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
-				break;
-			case SORT_STRING:
-			default:
-				$compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
-				break;
 		}
-
-		// Sort the content
-		usort($data, create_function('$a, $b', $compare_function));
+		
+		//This fixs only works in the document tool when ordering by name
+		if ($doc_filter && in_array($type, array(SORT_STRING))) {
+    		$data_to_sort = $folder_to_sort = array();
+    		$new_data = array();
+    		if (!empty($data)) {
+        		foreach ($data as $document) {
+        		    if ($document['type'] == 'folder') {
+        		      $docs_to_sort[$document['id']]   = api_strtolower($document['name']);
+        		    } else {
+        		      $folder_to_sort[$document['id']] = api_strtolower($document['name']);  
+        		    }
+        		    $new_data[$document['id']] = $document;    		        		     
+        		}   
+                if ($direction == SORT_ASC) {
+                    api_natrsort($docs_to_sort);
+                    api_natrsort($folder_to_sort);                
+                } else {
+                    api_natsort($docs_to_sort);
+                    api_natsort($folder_to_sort);
+                }
+                $new_data_order = array();
+                foreach($docs_to_sort as $id => $document) {
+                    $new_data_order[] = $new_data[$id];
+                }            
+                foreach($folder_to_sort as $id => $document) {
+                    $new_data_order[] = $new_data[$id];
+                }
+                $data = $new_data_order; 		
+    		}              
+		} else {		
+    		$compare_operator = $direction == SORT_ASC ? '>' : '<=';    		
+    		switch ($type) {
+    			case SORT_NUMERIC:
+    				$compare_function = 'return strip_tags($a['.$column.']) '.$compare_operator.' strip_tags($b['.$column.']);';
+    				break;
+    			case SORT_IMAGE:
+    				$compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'], "<img>")), api_strtolower(strip_tags($b['.$column.'], "<img>"))) '.$compare_operator.' 0;';
+    				break;
+    			case SORT_DATE:
+    				$compare_function = 'return strtotime(strip_tags($a['.$column.'])) '.$compare_operator.' strtotime(strip_tags($b['.$column.']));';
+    				break;
+    			case SORT_STRING:
+    			default:
+    			    $compare_function = 'return api_strnatcmp(api_strtolower(strip_tags($a['.$column.'])), api_strtolower(strip_tags($b['.$column.']))) '.$compare_operator.' 0;';
+    				break;
+    		}		
+        	// Sort the content
+    		usort($data, create_function('$a, $b', $compare_function));
+		}
 
 		if (is_array($column_show)) {
 			// We show only the columns data that were set up on the $column_show array