Skip to content

Commit

Permalink
Add group filter see BT#4301
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed May 27, 2015
1 parent 6c208e0 commit cab5e96
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 23 deletions.
2 changes: 1 addition & 1 deletion main/group/group_creation.php
Expand Up @@ -255,7 +255,7 @@ function copy_value(key) {
*/
$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>';
Expand Down
14 changes: 9 additions & 5 deletions main/inc/ajax/model.ajax.php
Expand Up @@ -474,11 +474,13 @@ function getWhereClause($col, $oper, $val)
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:
Expand Down Expand Up @@ -1392,11 +1394,11 @@ function getWhereClause($col, $oper, $val)
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;
}

Expand All @@ -1422,8 +1424,10 @@ function getWhereClause($col, $oper, $val)
}

$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;
Expand Down
2 changes: 1 addition & 1 deletion main/inc/lib/groupmanager.lib.php
Expand Up @@ -326,7 +326,7 @@ public static function create_class_groups($category_id)
{
$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']);
Expand Down
84 changes: 69 additions & 15 deletions main/inc/lib/usergroup.lib.php
Expand Up @@ -71,7 +71,7 @@ public function getTotalCount()
/**
* @return int
*/
public function get_count()
public function get_count($type = -1)
{
if ($this->useMultipleUrl) {
$urlId = api_get_current_access_url_id();
Expand All @@ -89,22 +89,40 @@ public function get_count()

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);
Expand All @@ -115,14 +133,26 @@ public function get_usergroup_by_course_with_data_count($course_id)

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;
}
}

Expand Down Expand Up @@ -231,15 +261,16 @@ public function get_courses_by_usergroup($id, $loadCourseData = false)
*
* @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
INNER JOIN {$this->table} u
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
Expand All @@ -249,13 +280,22 @@ public function get_usergroup_in_course($options = array())
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);
Expand All @@ -272,10 +312,11 @@ public function get_usergroup_in_course($options = array())

/**
* @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'])) {
Expand All @@ -287,6 +328,12 @@ public function get_usergroup_not_in_course($options = array())
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.*
Expand All @@ -304,6 +351,13 @@ public function get_usergroup_not_in_course($options = array())
";
}
$conditions = Database::parse_conditions($options);

if (empty($conditions)) {
$conditions .= "WHERE 1 = 1 $typeCondition ";
} else {
$conditions .= " $typeCondition ";
}

$sql .= $conditions;

if ($this->useMultipleUrl) {
Expand Down
24 changes: 23 additions & 1 deletion main/user/class.php
Expand Up @@ -23,6 +23,18 @@
$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");

Expand All @@ -36,6 +48,16 @@
} 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>';
}
Expand Down Expand Up @@ -69,7 +91,7 @@

//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(
Expand Down

0 comments on commit cab5e96

Please sign in to comment.