Browse Source

Adding pagination when searching users in the social network see BT#6708

Julio Montoya 11 years ago
parent
commit
6145ecbe5e

+ 14 - 2
main/inc/lib/display.lib.php

@@ -214,11 +214,23 @@ class Display {
      * @param array grid classes
      * @return 	string   html grid
      */
-    public static function return_sortable_grid($name, $header, $content, $paging_options = array(), $query_vars = null, $form_actions = array(), $visibility_options = true, $sort_data = true, $grid_class = array()) {
-        global $origin;
+    public static function return_sortable_grid(
+        $name,
+        $header,
+        $content,
+        $paging_options = array(),
+        $query_vars = null,
+        $form_actions = array(),
+        $visibility_options = true,
+        $sort_data = true,
+        $grid_class = array(),
+        $elementCount = 0
+    ) {
         $column =  0;
         $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
+
         $table = new SortableTableFromArray($content, $column, $default_items_per_page, $name);
+        $table->total_number_of_items = intval($elementCount);
         if (is_array($query_vars)) {
             $table->set_additional_parameters($query_vars);
         }

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

@@ -122,7 +122,7 @@ class SortableTable extends HTML_Table {
 
         parent :: __construct (array ('class' => 'data_table', 'id' => $table_id));
         $this->table_name = $table_name;
-        $this->additional_parameters = array ();
+        $this->additional_parameters = array();
         $this->param_prefix = $table_name.'_';
 
         $this->page_nr = isset ($_SESSION[$this->param_prefix.'page_nr']) ? intval($_SESSION[$this->param_prefix.'page_nr']) : 1;
@@ -220,7 +220,8 @@ class SortableTable extends HTML_Table {
         return $this->pager;
     }
 
-    public function display() {
+    public function display()
+    {
         echo $this->return_table();
     }
     /**
@@ -465,6 +466,7 @@ class SortableTable extends HTML_Table {
     public function display_simple_grid($visibility_options, $hide_navigation = true, $per_page = 20, $sort_data = true, $grid_class = array()) {
 
         $empty_table = false;
+        $total = $this->get_total_number_of_items();
         if ($this->get_total_number_of_items() == 0) {
             $cols = $this->getColCount();
             //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
@@ -473,10 +475,12 @@ class SortableTable extends HTML_Table {
             $empty_table = true;
         }
         $html = '';
+
         if (!$empty_table) {
             // If we show the pagination
             if (!$hide_navigation) {
                 $form = ' ';
+
                 if ($this->get_total_number_of_items() > $per_page) {
                     if ($per_page > 10) {
                         $form = $this->get_page_select_form();
@@ -820,7 +824,8 @@ class SortableTable extends HTML_Table {
      * to their default state when sorting this table.
      * @param array $tablenames An array of table names.
      */
-    public function set_other_tables ($tablenames) {
+    public function set_other_tables($tablenames)
+    {
         $this->other_tables = $tablenames;
     }
 
@@ -831,7 +836,8 @@ class SortableTable extends HTML_Table {
      * converted into a checkbox
      * @param array $row A row from the table.
      */
-    public function filter_data ($row) {
+    public function filter_data($row)
+    {
         $url_params = $this->get_sortable_table_param_string().'&'.$this->get_additional_url_paramstring();
         foreach ($this->column_filters as $column => & $function) {
             $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
@@ -845,10 +851,11 @@ class SortableTable extends HTML_Table {
                 $row[0] .= '/>';
             }
         }
-
-        foreach ($row as $index => & $value) {
-            if (empty($value)) {
-                 $value = '-';
+        if (is_array($row)) {
+            foreach ($row as & $value) {
+                if (empty($value)) {
+                     $value = '-';
+                }
             }
         }
         return $row;
@@ -859,7 +866,8 @@ class SortableTable extends HTML_Table {
      * 2nd argument in the constructor of a SortableTable. Make sure your
      * function has the same parameters as defined here.
      */
-    public function get_total_number_of_items () {
+    public function get_total_number_of_items()
+    {
         if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
             $this->total_number_of_items = call_user_func($this->get_total_number_function);
         }
@@ -878,11 +886,12 @@ class SortableTable extends HTML_Table {
      * @param string $direction In which order should the data be sorted (ASC
      * or DESC)
      */
-    public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null, $sort = null) {
+    public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null, $sort = null)
+    {
         if (!is_null($this->get_data_function)) {
             return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
         }
-        return array ();
+        return array();
     }
 }
 
@@ -902,8 +911,8 @@ class SortableTableFromArray extends SortableTable {
      * @param int $default_column
      * @param int $default_items_per_page
      */
-    public function __construct($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename') {
-        parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
+    public function __construct($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename', $get_total_number_function = null) {
+        parent :: __construct ($tablename, $get_total_number_function, null, $default_column, $default_items_per_page);
         $this->table_data = $table_data;
     }
 
@@ -924,8 +933,13 @@ class SortableTableFromArray extends SortableTable {
      * Get total number of items
      * @see SortableTable#get_total_number_of_items
      */
-    public function get_total_number_of_items() {
-        return count($this->table_data);
+    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 {
+            return count($this->table_data);
+        }
     }
 }
 

+ 40 - 51
main/inc/lib/usermanager.lib.php

@@ -3073,16 +3073,16 @@ class UserManager
     }
 
     /**
-     * Searchs an user (tags, firstname, lastname and email )
+     * Search an user (tags, first name, last name and email )
      * @param string the tag
      * @param int field id of the tag
      * @param int where to start in the query
      * @param int number of items
+     * @param bool get count or not
      * @return array
      */
-    public static function get_all_user_tags($tag, $field_id = 0, $from = 0, $number_of_items = 10)
+    public static function get_all_user_tags($tag, $field_id = 0, $from = 0, $number_of_items = 10, $getCount = false)
     {
-
         $user_table = Database::get_main_table(TABLE_MAIN_USER);
         $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
         $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
@@ -3096,69 +3096,58 @@ class UserManager
         if ($field_id != 0) {
             $where_field = " field_id = $field_id AND ";
         }
-        // all the information of the field
-        $sql = "SELECT u.user_id,u.username,firstname, lastname, email, tag, picture_uri FROM $table_user_tag ut INNER JOIN $table_user_tag_values uv ON (uv.tag_id=ut.id)
-                INNER JOIN $user_table u ON(uv.user_id =u.user_id)
-                WHERE $where_field tag LIKE '$tag%' ORDER BY tag";
-        $sql .= " LIMIT $from,$number_of_items";
 
-        $result = Database::query($sql);
-        $return = array();
-        if (Database::num_rows($result) > 0) {
-            while ($row = Database::fetch_array($result, 'ASSOC')) {
-                if (isset($return[$row['user_id']]) && !empty($return[$row['user_id']]['tag'])) {
-                    $row['tag'] = $return[$row['user_id']]['tag'].' '.Display::url($row['tag'], api_get_path(WEB_PATH).'main/social/search.php?q='.$row['tag'], array('class' => 'tag'));
-                } else {
-                    $row['tag'] = Display::url($row['tag'], api_get_path(WEB_PATH).'main/social/search.php?q='.$row['tag'], array('class' => 'tag'));
-                }
-                $return[$row['user_id']] = $row;
-            }
+        $addInnerJoin = null;
+        if (true) {
+            $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
+            $addInnerJoin = " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id = url_rel_user.user_id)";
         }
 
-        $keyword = $tag;
-        $sql = "SELECT u.user_id, u.username, firstname, lastname, email, picture_uri FROM $user_table u";
+        // all the information of the field
 
-        if (api_get_multiple_access_url()) {
-            $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
-            $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
+        if ($getCount) {
+            $select = "SELECT count(u.user_id) count";
+        } else {
+            $select = "SELECT u.user_id, u.username, firstname, lastname, email, tag, picture_uri";
         }
 
-        if (isset($keyword)) {
-            $keyword = Database::escape_string($keyword);
-            //OR u.official_code LIKE '%".$keyword."%'
-            // OR u.email LIKE '%".$keyword."%'
-            $sql .= " WHERE (u.firstname LIKE '%".$keyword."%' OR u.lastname LIKE '%".$keyword."%'  OR u.username LIKE '%".$keyword."%' OR concat(u.firstname,' ',u.lastname) LIKE '%".$keyword."%' OR concat(u.lastname,' ',u.firstname) LIKE '%".$keyword."%' )";
+        $sql = " $select
+                FROM $user_table u $addInnerJoin
+                LEFT JOIN $table_user_tag_values uv ON (u.user_id AND uv.user_id) LEFT JOIN $table_user_tag ut ON (uv.tag_id = ut.id)
+                WHERE
+                    ($where_field tag LIKE '$tag%') OR
+                    (u.firstname LIKE '%".$tag."%' OR u.lastname LIKE '%".$tag."%' OR
+                     u.username LIKE '%".$tag."%' OR concat(u.firstname,' ',u.lastname) LIKE '%".$tag."%' OR
+                     concat(u.lastname,' ',u.firstname) LIKE '%".$tag."%' )";
+
+        if (api_get_multiple_access_url() && api_get_current_access_url_id() != -1) {
+            $sql.= " AND url_rel_user.access_url_id=".api_get_current_access_url_id();
         }
+
         $keyword_active = true;
-        //only active users
+        // only active users
         if ($keyword_active) {
             $sql .= " AND u.active='1'";
         }
-        //avoid anonymous
+        // avoid anonymous
         $sql .= " AND u.status <> 6 ";
 
-        // adding the filter to see the user's only of the current access_url
-        if (api_get_multiple_access_url() && api_get_current_access_url_id() != -1) {
-            $sql.= " AND url_rel_user.access_url_id=".api_get_current_access_url_id();
-        }
-        $direction = 'ASC';
-        if (!in_array($direction, array('ASC', 'DESC'))) {
-            $direction = 'ASC';
-        }
-
-        //$column = intval($column);
-        $from = intval($from);
-        $number_of_items = intval($number_of_items);
-
-        //$sql .= " ORDER BY col$column $direction ";
+        $sql .= "ORDER BY username";
         $sql .= " LIMIT $from,$number_of_items";
-
-        $res = Database::query($sql);
-        if (Database::num_rows($res) > 0) {
-            while ($row = Database::fetch_array($res, 'ASSOC')) {
-                if (!in_array($row['user_id'], array_keys($return))) {
-                    $return[$row['user_id']] = $row;
+        $result = Database::query($sql);
+        $return = array();
+        if (Database::num_rows($result) > 0) {
+            if ($getCount) {
+                $row = Database::fetch_array($result, 'ASSOC');
+                return $row['count'];
+            }
+            while ($row = Database::fetch_array($result, 'ASSOC')) {
+                if (isset($return[$row['user_id']]) && !empty($return[$row['user_id']]['tag'])) {
+                    $row['tag'] = $return[$row['user_id']]['tag'].' '.Display::url($row['tag'], api_get_path(WEB_PATH).'main/social/search.php?q='.$row['tag'], array('class' => 'tag'));
+                } else {
+                    $row['tag'] = Display::url($row['tag'], api_get_path(WEB_PATH).'main/social/search.php?q='.$row['tag'], array('class' => 'tag'));
                 }
+                $return[$row['user_id']] = $row;
             }
         }
         return $return;

+ 18 - 9
main/social/search.php

@@ -136,17 +136,23 @@ $this_section      = SECTION_SOCIAL;
 $tool_name         = get_lang('Search');
 $interbreadcrumb[] = array('url' => 'profile.php', 'name' => get_lang('SocialNetwork'));
 
-$query_vars = array();
 $query      = isset($_GET['q']) ? $_GET['q'] : null;
+$query_vars = array('q' => $query);
 
 $social_left_content = SocialManager::show_social_menu('search');
 
 $social_right_content = '<div class="span9">'.UserManager::get_search_form($query).'</div>';
 
-//I'm searching something
+// I'm searching something
 if ($query != '') {
-    //get users from tags
-    $users  = UserManager::get_all_user_tags($_GET['q'], 0, 0, 5);
+
+    $itemPerPage = 5;
+    $page = isset($_GET['groups_page_nr']) ? intval($_GET['groups_page_nr']) : 1;
+    $total = UserManager::get_all_user_tags($_GET['q'], 0, 0, $itemPerPage, true);
+
+    $from = intval(($page - 1) * $itemPerPage);
+    // Get users from tags
+    $users  = UserManager::get_all_user_tags($_GET['q'], 0, $from, $itemPerPage);
     $groups = GroupPortalManager::get_all_group_tags($_GET['q']);
 
     if (empty($users) && empty($groups)) {
@@ -228,7 +234,7 @@ if ($query != '') {
         $social_right_content .= Display::page_subheader(get_lang('Groups'));
         foreach ($groups as $group) {
             $group['name']         = Security::remove_XSS($group['name'], STUDENT, true);
-            $group['description'] = Security::remove_XSS($group['description'], STUDENT, true);
+            $group['description']  = Security::remove_XSS($group['description'], STUDENT, true);
             $id                    = $group['id'];
             $url_open              = '<a href="groups.php?id='.$id.'" >';
             $url_close             = '</a>';
@@ -267,13 +273,16 @@ if ($query != '') {
     }
     $visibility = array(true, true, true, true, true);
     $social_right_content .= Display::return_sortable_grid(
-        'mygroups',
-        array(),
+        'groups',
+        null,
         $grid_groups,
-        array('hide_navigation' => true, 'per_page' => 5),
+        array('hide_navigation' => false, 'per_page' => 5),
         $query_vars,
         false,
-        $visibility
+        $visibility,
+        true,
+        array(),
+        $total
     );
 }
 $social_right_content .= MessageManager::generate_message_form('send_message');