diff --git a/public/main/admin/usergroup_users.php b/public/main/admin/usergroup_users.php index f83755fe6eb..cba30e325ab 100644 --- a/public/main/admin/usergroup_users.php +++ b/public/main/admin/usergroup_users.php @@ -7,8 +7,6 @@ require_once __DIR__.'/../inc/global.inc.php'; -$this_section = SECTION_PLATFORM_ADMIN; - $id = isset($_GET['id']) ? (int) $_GET['id'] : 0; $usergroup = new UserGroupModel(); @@ -17,27 +15,81 @@ api_not_allowed(true); } -$usergroup->protectScript($userGroupInfo, true, true); -$allowEdit = api_is_platform_admin() || isset($userGroupInfo['author_id']) && $userGroupInfo['author_id'] == api_get_user_id(); +$action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null; +$userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0; +$calendarId = isset($_REQUEST['calendar_id']) ? (int) $_REQUEST['calendar_id'] : 0; + +$courseInfo = api_get_course_info(); +$sessionId = api_get_session_id(); +$courseId = !empty($courseInfo) ? (int) $courseInfo['real_id'] : 0; +$cidReq = !empty($courseInfo) ? '&'.api_get_cidreq() : ''; + +$this_section = empty($courseInfo) ? SECTION_PLATFORM_ADMIN : SECTION_COURSES; + +$canViewFromCourse = false; + +if (!empty($courseInfo) && api_is_allowed_to_edit()) { + if ($sessionId > 0) { + $table = Database::get_main_table(TABLE_USERGROUP_REL_SESSION); + $sql = "SELECT session_id + FROM $table + WHERE usergroup_id = $id AND session_id = $sessionId + LIMIT 1"; + $result = Database::query($sql); + $canViewFromCourse = Database::num_rows($result) > 0; + } else { + $table = Database::get_main_table(TABLE_USERGROUP_REL_COURSE); + $sql = "SELECT course_id + FROM $table + WHERE usergroup_id = $id AND course_id = $courseId + LIMIT 1"; + $result = Database::query($sql); + $canViewFromCourse = Database::num_rows($result) > 0; + } +} + +if (!$canViewFromCourse) { + $usergroup->protectScript($userGroupInfo, true, true); +} + +$allowEdit = api_is_platform_admin() || ( + isset($userGroupInfo['author_id']) && (int) $userGroupInfo['author_id'] === api_get_user_id() + ); + +if (!empty($action) && !$allowEdit) { + api_not_allowed(true); +} $calendarPlugin = null; if ($allowEdit && 'true' === api_get_plugin_setting('learning_calendar', 'enabled')) { $calendarPlugin = LearningCalendarPlugin::create(); } -$action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null; -$userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0; -$calendarId = isset($_REQUEST['calendar_id']) ? (int) $_REQUEST['calendar_id'] : 0; +$calendarActions = [ + 'add_calendar', + 'edit_calendar', + 'create_control_point', + 'add_multiple_users_to_calendar', +]; + +if (in_array($action, $calendarActions, true) && null === $calendarPlugin) { + api_not_allowed(true); +} -$courseInfo = api_get_course_info(); if (empty($courseInfo)) { - $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')]; + $interbreadcrumb[] = [ + 'url' => 'usergroups.php', + 'name' => get_lang('Classes'), + ]; } else { - $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'user/class.php?'.api_get_cidreq(), 'name' => get_lang('Classes')]; + $interbreadcrumb[] = [ + 'url' => api_get_path(WEB_CODE_PATH).'user/class.php?'.api_get_cidreq(), + 'name' => get_lang('Classes'), + ]; } $interbreadcrumb[] = ['url' => '#', 'name' => $userGroupInfo['title']]; -if (!empty($action)) { +if (!empty($action) && $allowEdit) { $usergroup->protectScript($userGroupInfo); } @@ -46,100 +98,117 @@ $form = new FormValidator( 'add_calendar', 'post', - api_get_self().'?id='.$id.'&user_id='.$userId.'&action=add_calendar' + api_get_self().'?id='.$id.$cidReq.'&user_id='.$userId.'&action=add_calendar' ); $userInfo = api_get_user_info($userId); $form->addHeader($userInfo['complete_name']); $calendarPlugin->getAddUserToCalendarForm($form); $form->addButtonSave(get_lang('Add')); - $form->display(); if ($form->validate()) { - $calendarId = $form->getSubmitValue('calendar_id'); + $calendarId = (int) $form->getSubmitValue('calendar_id'); if (!empty($calendarId)) { $calendarPlugin->addUserToCalendar($calendarId, $userId); Display::addFlash(Display::return_message(get_lang('Added'), 'confirmation')); - header('Location: '.api_get_self().'?id='.$id); + header('Location: '.api_get_self().'?id='.$id.$cidReq); exit; } } + + Display::display_header(); + $form->display(); + Display::display_footer(); exit; - break; + case 'edit_calendar': $form = new FormValidator( 'add_calendar', 'post', - api_get_self().'?id='.$id.'&user_id='.$userId.'&action=edit_calendar&calendar_id='.$calendarId + api_get_self().'?id='.$id.$cidReq.'&user_id='.$userId.'&action=edit_calendar&calendar_id='.$calendarId ); + $userInfo = api_get_user_info($userId); $form->addHeader($userInfo['complete_name']); $calendarPlugin->getAddUserToCalendarForm($form); $form->setDefaults(['calendar_id' => $calendarId]); $form->addButtonSave(get_lang('Update')); - $form->display(); if ($form->validate()) { - $calendarId = $form->getSubmitValue('calendar_id'); + $calendarId = (int) $form->getSubmitValue('calendar_id'); if (!empty($calendarId)) { $calendarPlugin->updateUserToCalendar($calendarId, $userId); Display::addFlash(Display::return_message(get_lang('Added'), 'confirmation')); - header('Location: '.api_get_self().'?id='.$id); + header('Location: '.api_get_self().'?id='.$id.$cidReq); exit; } } + + Display::display_header(); + $form->display(); + Display::display_footer(); exit; - break; + case 'delete': - $res = $usergroup->delete_user_rel_group($_GET['user_id'], $_GET['id']); + $usergroup->delete_user_rel_group($userId, $id); Display::addFlash(Display::return_message(get_lang('Deleted'), 'confirmation')); - header('Location: '.api_get_self().'?id='.$id); + header('Location: '.api_get_self().'?id='.$id.$cidReq); exit; - break; + case 'create_control_point': $value = isset($_GET['value']) ? (int) $_GET['value'] : 0; $calendarPlugin->addControlPoint($userId, $value); Display::addFlash( Display::return_message($calendarPlugin->get_lang('Control point added'), 'confirmation') ); - header('Location: '.api_get_self().'?id='.$id); + header('Location: '.api_get_self().'?id='.$id.$cidReq); exit; + case 'add_multiple_users_to_calendar': - $userList = isset($_REQUEST['user_list']) ? explode(',', $_REQUEST['user_list']) : 0; - foreach ($userList as $userId) { - $isAdded = $calendarPlugin->addUserToCalendar($calendarId, $userId); + $userList = isset($_REQUEST['user_list']) ? explode(',', $_REQUEST['user_list']) : []; + foreach ($userList as $selectedUserId) { + $selectedUserId = (int) $selectedUserId; + if (empty($selectedUserId)) { + continue; + } + + $isAdded = $calendarPlugin->addUserToCalendar($calendarId, $selectedUserId); if (!$isAdded) { - $isAdded = $calendarPlugin->updateUserToCalendar($calendarId, $userId); + $calendarPlugin->updateUserToCalendar($calendarId, $selectedUserId); } } Display::addFlash( Display::return_message(get_lang('Added'), 'confirmation') ); - - header('Location: '.api_get_self().'?id='.$id); + header('Location: '.api_get_self().'?id='.$id.$cidReq); exit; - break; } Display::display_header(); -// jqgrid will use this URL to do the selects -$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups_users&id='.$id; +// jqGrid data source +$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups_users&id='.$id.$cidReq; -// The order is important you need to check the the $column variable in the model.ajax.php file +// The order is important; it must match the model.ajax.php response. $columns = [ get_lang('Name'), get_lang('Detail'), ]; -// Column config +// Use proportional widths so the grid can stretch naturally. $column_model = [ - ['name' => 'name', 'index' => 'name', 'width' => '400', 'align' => 'left', 'sortable' => 'false'], + [ + 'name' => 'name', + 'index' => 'name', + 'width' => '70', + 'align' => 'left', + 'sortable' => 'false', + ], [ 'name' => 'actions', 'index' => 'actions', - 'width' => '250', + 'width' => '30', 'align' => 'center', 'sortable' => 'false', 'formatter' => 'action_formatter', @@ -157,13 +226,18 @@ get_lang('Detail'), ]; - // Column config $column_model = [ - ['name' => 'name', 'index' => 'name', 'width' => '35', 'align' => 'left', 'sortable' => 'false'], + [ + 'name' => 'name', + 'index' => 'name', + 'width' => '28', + 'align' => 'left', + 'sortable' => 'false', + ], [ 'name' => 'calendar', 'index' => 'calendar', - 'width' => '35', + 'width' => '14', 'align' => 'left', 'sortable' => 'false', 'formatter' => 'extra_formatter', @@ -171,23 +245,35 @@ [ 'name' => 'gradebook_items', 'index' => 'gradebook_items', - 'width' => '35', + 'width' => '12', + 'align' => 'left', + 'sortable' => 'false', + ], + [ + 'name' => 'time_spent', + 'index' => 'time_spent', + 'width' => '12', 'align' => 'left', 'sortable' => 'false', ], - ['name' => 'time_spent', 'index' => 'time_spent', 'width' => '35', 'align' => 'left', 'sortable' => 'false'], [ 'name' => 'lp_day_completed', 'index' => 'lp_day_completed', - 'width' => '35', + 'width' => '12', + 'align' => 'left', + 'sortable' => 'false', + ], + [ + 'name' => 'days_diff', + 'index' => 'days_diff', + 'width' => '10', 'align' => 'left', 'sortable' => 'false', ], - ['name' => 'days_diff', 'index' => 'days_diff', 'width' => '35', 'align' => 'left', 'sortable' => 'false'], [ 'name' => 'actions', 'index' => 'actions', - 'width' => '20', + 'width' => '12', 'align' => 'center', 'sortable' => 'false', 'formatter' => 'action_formatter', @@ -195,32 +281,54 @@ ]; } -// Autowidth $extraParams['autowidth'] = 'true'; -// height auto $extraParams['height'] = 'auto'; $extraParams['sortname'] = 'name'; $extraParams['sortorder'] = 'desc'; $extraParams['multiselect'] = $allowEdit; +$extraParams['shrinkToFit'] = true; +$extraParams['forceFit'] = true; +$extraParams['viewrecords'] = true; + +$deleteIcon = Display::getMdiIcon( + ActionIcon::DELETE, + 'ch-tool-icon', + null, + ICON_SIZE_SMALL, + get_lang('Delete') +); -$deleteIcon = Display::getMdiIcon(ActionIcon::DELETE, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Delete')); $urlStats = api_get_path(WEB_CODE_PATH); -$reportingIcon = Display::getMdiIcon(ToolIcon::TRACKING, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Reporting')); -$controlPoint = Display::getMdiIcon(ActionIcon::ADD, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Control point')); +$reportingIcon = Display::getMdiIcon( + ToolIcon::TRACKING, + 'ch-tool-icon', + null, + ICON_SIZE_SMALL, + get_lang('Reporting') +); + +$controlPoint = Display::getMdiIcon( + ActionIcon::ADD, + 'ch-tool-icon', + null, + ICON_SIZE_SMALL, + get_lang('Control point') +); $link = ''; if ($calendarPlugin) { - $link = ''.$controlPoint.''; + $link = ''.$controlPoint.''; } $deleteButton = ''; if ($allowEdit) { - $deleteButton = ''.$deleteIcon.''; + $deleteButton = ''.$deleteIcon.''; } -//return \''.Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Edit')).''. -// With this function we can add actions to the jgrid + $action_links = ' function action_formatter(cellvalue, options, rowObject) { var value = rowObject[5]; @@ -236,105 +344,185 @@ function extra_formatter(cellvalue, options, rowObject) { if (calendarName == "") { return \''.get_lang('Add').'\'; + api_get_path(WEB_CODE_PATH).'admin/usergroup_users.php?user_id=\'+options.rowId+\'&id='.$id.$cidReq.'&action=add_calendar&width=700" class="btn btn--primary ajax">'.get_lang('Add').'\'; } else { - return \' \'+calendarName+\' '.get_lang('Edit').'\'; + return \' \'+calendarName+\' '.get_lang('Edit').'\'; } - - return calendarName; - - return \''. - ' '.Display::getMdiIcon(ToolIcon::TRACKING, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Reporting')).''. - ' '.$deleteIcon.'\'; }'; -$deleteUrl = api_get_path(WEB_AJAX_PATH).'usergroup.ajax.php?a=delete_user_in_usergroup&group_id='.$id; +$deleteUrl = api_get_path(WEB_AJAX_PATH).'usergroup.ajax.php?a=delete_user_in_usergroup&group_id='.$id.$cidReq; if ($calendarPlugin) { $form = new FormValidator( 'add_multiple_calendar', 'post', - api_get_self().'?id='.$id.'&action=add_multiple_users_to_calendar' + api_get_self().'?id='.$id.$cidReq.'&action=add_multiple_users_to_calendar' ); $calendarPlugin->getAddUserToCalendarForm($form); $form->addHidden('user_list', ''); $form->addButtonSave(get_lang('Add')); } - ?> - + + $("#usergroups").jqGrid( + "navGrid", + "#usergroups_pager", + { edit: false, add: false, del: , search: false}, + { height:280, reloadAfterSubmit:false }, + { height:280, reloadAfterSubmit:false }, + { reloadAfterSubmit:false, url: "" }, + { width:500 } + ) + + .navButtonAdd('#usergroups_pager',{ + caption:"get_lang('Update calendar')); ?>", + buttonicon:"ui-icon ui-icon-plus", + onClickButton: function(a) { + var userIdList = $("#usergroups").jqGrid('getGridParam', 'selarrrow'); + if (userIdList.length) { + $(".modal-body #add_multiple_calendar_user_list").val(userIdList); + $('#myModal').modal(); + } else { + alert(""); + } + }, + position:"last" + }) + + ; + + resizeUsergroupsGrid(); + + $(window).on("resize.usergroupsGrid", function() { + resizeUsergroupsGrid(); + }); + + setTimeout(function() { + resizeUsergroupsGrid(); + }, 0); + }); + + -