Bläddra i källkod

Add group filter see BT#4301

Julio Montoya 9 år sedan
förälder
incheckning
cab5e96359

+ 1 - 1
main/group/group_creation.php

@@ -255,7 +255,7 @@ EOT;
 	 */
     $options['where'] = array(" usergroup.course_id = ? " =>  api_get_real_course_id());
     $obj = new UserGroup();
-    $classes = $obj->get_usergroup_in_course($options);
+    $classes = $obj->getUserGroupInCourse($options);
 	if (count($classes) > 0) {
 		echo '<b>'.get_lang('GroupsFromClasses').'</b>';
 		echo '<blockquote>';

+ 9 - 5
main/inc/ajax/model.ajax.php

@@ -474,11 +474,13 @@ switch ($action) {
     case 'get_usergroups_teacher':
         $obj = new UserGroup();
         $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'registered';
+        $groupFilter = isset($_REQUEST['group_filter']) ? intval($_REQUEST['group_filter']) : 0;
+
         $course_id = api_get_course_int_id();
         if ($type == 'registered') {
-            $count = $obj->get_usergroup_by_course_with_data_count($course_id);
+            $count = $obj->getUserGroupByCourseWithDataCount($course_id, $groupFilter);
         } else {
-            $count = $obj->get_count();
+            $count = $obj->get_count($groupFilter);
         }
         break;
     default:
@@ -1392,11 +1394,11 @@ switch ($action) {
         switch ($type) {
             case 'not_registered':
                 $options['where'] = array(" (course_id IS NULL OR course_id != ?) " => $course_id);
-                $result = $obj->get_usergroup_not_in_course($options);
+                $result = $obj->getUserGroupNotInCourse($options, $groupFilter);
                 break;
             case 'registered':
                 $options['where'] = array(" usergroup.course_id = ? " =>  $course_id);
-                $result = $obj->get_usergroup_in_course($options);
+                $result = $obj->getUserGroupInCourse($options);
                 break;
         }
 
@@ -1422,8 +1424,10 @@ switch ($action) {
                 }
 
                 $role = $obj->getUserRoleToString(api_get_user_id(), $group['id']);
+
                 $group['status'] = $role;
-                $group['actions'] = Display::url($icon, $url);
+
+                $group['actions'] = Display::url(get_lang('reg'), $url, ['class' => 'btn btn-primary']);
                 $new_result[] = $group;
             }
             $result = $new_result;

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

@@ -326,7 +326,7 @@ class GroupManager
     {
         $options['where'] = array(" usergroup.course_id = ? " =>  api_get_real_course_id());
         $obj = new UserGroup();
-        $classes = $obj->get_usergroup_in_course($options);
+        $classes = $obj->getUserGroupInCourse($options);
         $group_ids = array();
         foreach ($classes as $class) {
             $users_ids = $obj->get_users_by_usergroup($class['id']);

+ 69 - 15
main/inc/lib/usergroup.lib.php

@@ -71,7 +71,7 @@ class UserGroup extends Model
     /**
      * @return int
      */
-    public function get_count()
+    public function get_count($type = -1)
     {
         if ($this->useMultipleUrl) {
             $urlId = api_get_current_access_url_id();
@@ -89,22 +89,40 @@ class UserGroup extends Model
 
             return 0;
         } else {
-            return $this->getTotalCount();
+
+            $typeCondition = '';
+            if ($type != -1) {
+                $type = intval($type);
+                $typeCondition = " WHERE group_type = $type ";
+            }
+
+            $sql = "SELECT count(a.id) as count
+                    FROM {$this->table} a
+                    $typeCondition
+            ";
+            $result = Database::query($sql);
+            if (Database::num_rows($result)) {
+                $row  = Database::fetch_array($result);
+                return $row['count'];
+            }
         }
     }
 
     /**
      * @param int $course_id
+     * @param int $type
      *
      * @return mixed
      */
-    public function get_usergroup_by_course_with_data_count($course_id)
+    public function getUserGroupByCourseWithDataCount($course_id, $type = -1)
     {
         if ($this->useMultipleUrl) {
             $course_id = intval($course_id);
             $urlId = api_get_current_access_url_id();
-            $sql = "SELECT count(c.usergroup_id) as count FROM {$this->usergroup_rel_course_table} c
-                    INNER JOIN {$this->access_url_rel_usergroup} a ON (c.usergroup_id = a.usergroup_id)
+            $sql = "SELECT count(c.usergroup_id) as count
+                    FROM {$this->usergroup_rel_course_table} c
+                    INNER JOIN {$this->access_url_rel_usergroup} a
+                    ON (c.usergroup_id = a.usergroup_id)
                     WHERE access_url_id = $urlId AND course_id = $course_id
             ";
             $result = Database::query($sql);
@@ -115,14 +133,26 @@ class UserGroup extends Model
 
             return 0;
         } else {
-            $row = Database::select(
-                'count(*) as count',
-                $this->usergroup_rel_course_table,
-                array('where' => array('course_id = ?' => $course_id)),
-                'first'
-            );
+            $typeCondition = '';
+            if ($type != -1) {
+                $type = intval($type);
+                $typeCondition = " AND group_type = $type ";
+            }
+            $sql = "SELECT count(c.usergroup_id) as count
+                    FROM {$this->usergroup_rel_course_table} c
+                    INNER JOIN {$this->table} a
+                    ON (c.usergroup_id = a.id)
+                    WHERE
+                        course_id = $course_id
+                        $typeCondition
+            ";
+            $result = Database::query($sql);
+            if (Database::num_rows($result)) {
+                $row  = Database::fetch_array($result);
+                return $row['count'];
+            }
 
-            return $row['count'];
+            return 0;
         }
     }
 
@@ -231,7 +261,7 @@ class UserGroup extends Model
      *
      * @return array
      */
-    public function get_usergroup_in_course($options = array())
+    public function getUserGroupInCourse($options = array())
     {
         if ($this->useMultipleUrl) {
             $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup
@@ -239,7 +269,8 @@ class UserGroup extends Model
                     ON (u.id = usergroup.usergroup_id)
                     INNER JOIN {$this->table_course} c
                     ON (usergroup.course_id = c.id)
-                    INNER JOIN {$this->access_url_rel_usergroup} a ON (a.usergroup_id = u.id)
+                    INNER JOIN {$this->access_url_rel_usergroup} a
+                    ON (a.usergroup_id = u.id)
                    ";
         } else {
             $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup
@@ -249,13 +280,22 @@ class UserGroup extends Model
                     ON (usergroup.course_id = c.id)
                    ";
         }
+
         $conditions = Database::parse_conditions($options);
+
+        if (empty($conditions)) {
+            $conditions .= "WHERE 1 = 1 $typeCondition ";
+        } else {
+            $conditions .= " $typeCondition ";
+        }
+
         $sql .= $conditions;
         if ($this->useMultipleUrl) {
             $urlId = api_get_current_access_url_id();
             $sql .= " AND access_url_id = $urlId ";
         }
 
+
         if (isset($options['LIMIT'])) {
             $limits = explode(',', $options['LIMIT']);
             $limits = array_map('intval', $limits);
@@ -272,10 +312,11 @@ class UserGroup extends Model
 
     /**
      * @param array $options
+     * @param int   $type
      *
      * @return array|bool
      */
-    public function get_usergroup_not_in_course($options = array())
+    public function getUserGroupNotInCourse($options = array(), $type = -1)
     {
         $course_id = null;
         if (isset($options['course_id'])) {
@@ -287,6 +328,12 @@ class UserGroup extends Model
             return false;
         }
 
+        $typeCondition = '';
+        if ($type != -1) {
+            $type = intval($type);
+            $typeCondition = " AND group_type = $type ";
+        }
+
         if ($this->useMultipleUrl) {
             $urlId = api_get_current_access_url_id();
             $sql = "SELECT DISTINCT u.*
@@ -304,6 +351,13 @@ class UserGroup extends Model
             ";
         }
         $conditions = Database::parse_conditions($options);
+
+        if (empty($conditions)) {
+            $conditions .= "WHERE 1 = 1 $typeCondition ";
+        } else {
+            $conditions .= " $typeCondition ";
+        }
+
         $sql .= $conditions;
 
         if ($this->useMultipleUrl) {

+ 23 - 1
main/user/class.php

@@ -23,6 +23,18 @@ $htmlHeadXtra[] = api_get_jqgrid_js();
 $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("ToolUser"));
 
 $type = isset($_GET['type']) ? Security::remove_XSS($_GET['type']) : 'registered';
+$groupFilter = isset($_GET['group_filter']) ? intval($_GET['group_filter']) : 0;
+
+$htmlHeadXtra[] = '
+<script>
+
+$(document).ready( function() {
+    $("#group_filter").change(function() {
+        window.location = "class.php?'.api_get_cidreq().'&type='.$type.'" +"&group_filter=" + $(this).val();
+    });
+});
+</script>
+';
 
 Display :: display_header($tool_name, "User");
 
@@ -36,6 +48,16 @@ if (api_is_allowed_to_edit()) {
     } else {
         echo '<a href="class.php?'.api_get_cidreq().'&type=registered">'.
             Display::return_icon('empty_evaluation.png', get_lang("Classes"), array(), ICON_SIZE_MEDIUM).'</a>';
+
+        $form = new FormValidator('groups', 'post', api_get_self(), '', '', FormValidator::LAYOUT_INLINE);
+        $options = [
+            -1 => get_lang('All'),
+            1 => get_lang('SocialGroups'),
+            0 => get_lang('Classes'),
+        ];
+        $form->addSelect('group_filter', get_lang('Groups'), $options, ['id' => 'group_filter']);
+        $form->setDefaults(['group_filter' => $groupFilter]);
+        $form->display();
     }
     echo '</div>';
 }
@@ -69,7 +91,7 @@ if (api_is_allowed_to_edit()) {
 
 //jqgrid will use this URL to do the selects
 
-$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups_teacher&type='.$type;
+$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups_teacher&type='.$type.'&group_filter='.$groupFilter;
 
 //The order is important you need to check the the $column variable in the model.ajax.php file
 $columns = array(