Browse Source

Adding columnsToHide array in SortableTable see BT#9078

Now you can hide a column more easy using $table->setHideColumn(0);
Adding option "hide_user_field_from_list".
Julio Montoya 10 years ago
parent
commit
b800697bc2
3 changed files with 115 additions and 32 deletions
  1. 91 29
      main/inc/lib/sortable_table.class.php
  2. 2 1
      main/install/configuration.dist.php
  3. 22 2
      main/user/user.php

+ 91 - 29
main/inc/lib/sortable_table.class.php

@@ -95,6 +95,11 @@ class SortableTable extends HTML_Table
     public $use_jqgrid = false;
     public $table_id = null;
     public $headers = array();
+    /**
+     * @var array
+     * Columns to hide
+     */
+    private $columnsToHide = array();
 
     /**
      * Create a new SortableTable
@@ -124,7 +129,6 @@ class SortableTable extends HTML_Table
             $table_id = $table_name.uniqid();
         }
         $this->table_id = $table_id;
-
         parent::__construct(array('class' => 'data_table', 'id' => $table_id));
         $this->table_name = $table_name;
         $this->additional_parameters = array();
@@ -240,6 +244,7 @@ class SortableTable extends HTML_Table
     public function return_table()
     {
         $empty_table = false;
+        $this->processHeaders();
         $content = $this->get_table_html();
 
         if ($this->get_total_number_of_items() == 0) {
@@ -293,7 +298,6 @@ class SortableTable extends HTML_Table
             $html .= '<td>';
 
             if (count($this->form_actions) > 0) {
-
                 $html .= '<div class="btn-toolbar">';
                     $html .= '<div class="btn-group">';
                     $html .= '<a class="btn" href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript: setCheckbox(true, \''.$table_id.'\'); return false;">'.get_lang('SelectAll').'</a>';
@@ -588,6 +592,7 @@ class SortableTable extends HTML_Table
 
         foreach ($this->th_attributes as $column => $attributes) {
             $this->setCellAttributes(0, $column, $attributes);
+
         }
         foreach ($this->td_attributes as $column => $attributes) {
             $this->setColAttributes($column, $attributes);
@@ -667,6 +672,66 @@ class SortableTable extends HTML_Table
         return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
     }
 
+    /**
+     * @return array
+     */
+    public function getHeaders()
+    {
+        return $this->headers;
+    }
+
+    /**
+     * Process headers
+     */
+    public function processHeaders()
+    {
+        foreach ($this->headers as $column => $columnInfo) {
+            $label = $columnInfo['label'];
+            $sortable = $columnInfo['sortable'];
+            $th_attributes = $columnInfo['th_attributes'];
+            $td_attributes = $columnInfo['td_attributes'];
+
+            $param['direction'] = 'ASC';
+            if ($this->column == $column && $this->direction == 'ASC') {
+                $param['direction'] = 'DESC';
+            }
+            $param['page_nr'] = $this->page_nr;
+            $param['per_page'] = $this->per_page;
+            $param['column'] = $column;
+            if ($sortable) {
+                $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
+                foreach ($param as $key => & $value) {
+                    $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
+                }
+                $link .= $this->get_additional_url_paramstring();
+                $link .= '">'.$label.'</a>';
+                if ($this->column == $column) {
+                    $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
+                }
+            } else {
+                $link = $label;
+            }
+
+            $addHeader = true;
+            if (!empty($this->columnsToHide)) {
+                if (isset($this->columnsToHide[$column])) {
+                    $addHeader = false;
+                }
+            }
+
+            if ($addHeader) {
+                $this->setHeaderContents(0, $column, $link);
+
+                if (!is_null($td_attributes)) {
+                    $this->td_attributes[$column] = $td_attributes;
+                }
+                if (!is_null($th_attributes)) {
+                    $this->th_attributes[$column] = $th_attributes;
+                }
+            }
+        }
+    }
+
     /**
      * Set the header-label
      * @param int $column The column number
@@ -680,33 +745,12 @@ class SortableTable extends HTML_Table
      */
     public function set_header($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null)
     {
-        $param['direction'] = 'ASC';
-        if ($this->column == $column && $this->direction == 'ASC') {
-            $param['direction'] = 'DESC';
-        }
-        $param['page_nr'] = $this->page_nr;
-        $param['per_page'] = $this->per_page;
-        $param['column'] = $column;
-        if ($sortable) {
-            $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
-            foreach ($param as $key => & $value) {
-                $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
-            }
-            $link .= $this->get_additional_url_paramstring();
-            $link .= '">'.$label.'</a>';
-            if ($this->column == $column) {
-                $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
-            }
-        } else {
-            $link = $label;
-        }
-        $this->setHeaderContents(0, $column, $link);
-        if (!is_null($td_attributes)) {
-            $this->td_attributes[$column] = $td_attributes;
-        }
-        if (!is_null($th_attributes)) {
-            $this->th_attributes[$column] = $th_attributes;
-        }
+        $this->headers[$column] = array(
+            'label' => $label,
+            'sortable' => $sortable,
+            'th_attributes' => $th_attributes,
+            'td_attributes' => $td_attributes,
+        );
     }
 
     /**
@@ -782,6 +826,15 @@ class SortableTable extends HTML_Table
         $this->column_filters[$column] = $function;
     }
 
+    /**
+     * List of columns to hide
+     * @param int $column
+     */
+    public function setHideColumn($column)
+    {
+        $this->columnsToHide[$column] = $column;
+    }
+
     /**
      * Define a list of actions which can be performed on the table-date.
      * If you define a list of actions, the first column of the table will be
@@ -832,6 +885,15 @@ class SortableTable extends HTML_Table
         foreach ($this->column_filters as $column => & $function) {
             $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
         }
+
+        if (!empty($this->columnsToHide)) {
+            foreach ($this->columnsToHide as $index) {
+                if (isset($row[$index])) {
+                    unset($row[$index]);
+                }
+            }
+        }
+
         if (count($this->form_actions) > 0) {
             if (strlen($row[0]) > 0) {
                 $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';

+ 2 - 1
main/install/configuration.dist.php

@@ -253,7 +253,8 @@ $_configuration['system_stable']     = NEW_VERSION_STABLE;
 //$_configuration['allow_session_course_copy_for_teachers'] = true;
 // Hide the logout button
 //$_configuration['hide_logout_button'] = true;
-
+// Hide fields in the main/user/user.php page
+//$_configuration['hide_user_field_from_list'] = array('username');
 // Aspell Settings
 //$_configuration['aspell_bin'] = '/usr/bin/hunspell';
 //$_configuration['aspell_opts'] = '-a -d en_GB -H -i utf-8';

+ 22 - 2
main/user/user.php

@@ -842,20 +842,31 @@ $parameters['sec_token'] = Security::get_token();
 
 $table->set_additional_parameters($parameters);
 $header_nr = 0;
-
+$indexList = array();
 $table->set_header($header_nr++, '', false);
+
+$indexList['photo'] = $header_nr;
 $table->set_header($header_nr++, get_lang('Photo'), false);
+
 $table->set_header($header_nr++, get_lang('OfficialCode'));
+$indexList['official_code'] = $header_nr;
 
 if ($is_western_name_order) {
+    $indexList['firstname'] = $header_nr;
     $table->set_header($header_nr++, get_lang('FirstName'));
+    $indexList['lastname'] = $header_nr;
     $table->set_header($header_nr++, get_lang('LastName'));
 } else {
+    $indexList['lastname'] = $header_nr;
     $table->set_header($header_nr++, get_lang('LastName'));
+    $indexList['firstname'] = $header_nr;
     $table->set_header($header_nr++, get_lang('FirstName'));
 }
-$table->set_header($header_nr++, get_lang('LoginName'));  //
+$indexList['username'] = $header_nr;
+$table->set_header($header_nr++, get_lang('LoginName'));
+$indexList['description'] = $header_nr;
 $table->set_header($header_nr++, get_lang('Description'), false);
+$indexList['groups'] = $header_nr;
 $table->set_header($header_nr++, get_lang('GroupSingle'), false);
 
 if (api_is_allowed_to_edit(null, true) && api_get_setting('allow_user_course_subscription_by_course_admin') == 'true') {
@@ -864,6 +875,15 @@ if (api_is_allowed_to_edit(null, true) && api_get_setting('allow_user_course_sub
     $table->set_column_filter(0, 'hide_field');
 }
 
+$hideFields = api_get_configuration_value('hide_user_field_from_list');
+if (!empty($hideFields)) {
+    foreach ($hideFields as $fieldToHide) {
+        if (isset($indexList[$fieldToHide])) {
+            $table->setHideColumn($indexList[$fieldToHide]);
+        }
+    }
+}
+
 if (api_is_allowed_to_edit(null, true)) {
     $table->set_header($header_nr++, get_lang('Status'), false);
     $table->set_header($header_nr++, get_lang('Active'), false);