Skip to content

Commit

Permalink
List session from students in groups for admin group - refs BT#9442
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Apr 14, 2015
1 parent a91dbce commit 2666343
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions main/gradebook/certificate_report.php
Expand Up @@ -26,7 +26,12 @@
$selectedYear = isset($_POST['year']) && !empty($_POST['year']) ? $_POST['year'] : null;

$userId = api_get_user_id();

if (api_is_student_boss()) {
$sessions = SessionManager::getSessionsFollowedForGroupAdmin($userId);
} else {
$sessions = SessionManager::getSessionsCoachedByUser($userId);
}

if ($selectedSession > 0) {
if (!SessionManager::isValidId($selectedSession)) {
Expand Down
86 changes: 86 additions & 0 deletions main/inc/lib/sessionmanager.lib.php
Expand Up @@ -5952,4 +5952,90 @@ public static function isValidId($sessionId)

return false;
}

/**
* Get list of sessions based on users of a group for a group admin
* @param int $userId The user id
* @return array
*/
public static function getSessionsFollowedForGroupAdmin($userId)
{
$sessionList = array();

$sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
$sessionUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);

$groups = GroupPortalManager::get_groups_by_user($userId, GROUP_USER_PERMISSION_ADMIN);

$groupsId = array_keys($groups);
$subgroupsId = [];
$userIdList = [];

foreach ($groupsId as $groupId) {
$subgroupsId = array_merge($subgroupsId, GroupPortalManager::getGroupsByDepthLevel($groupId));
}

$groupsId = array_merge($groupsId, $subgroupsId);

$groupsId = array_unique($groupsId);

if (empty($groupsId)) {
return [];
}

foreach ($groupsId as $groupId) {
$groupUsers = GroupPortalManager::get_users_by_group($groupId);

if (empty($groupUsers)) {
continue;
}

foreach ($groupUsers as $member) {
if ($member['user_id'] == $userId ) {
continue;
}

$userIdList[] = intval($member['user_id']);
}
}

$userIdList = array_unique($userIdList);

if (empty($userIdList)) {
return [];
}

$sql = "SELECT DISTINCT s.* "
. "FROM $sessionTable s "
. "INNER JOIN $sessionUserTable sru ON s.id = sru.id_session "
. "WHERE ( "
. "sru.id_user IN (" . implode(', ', $userIdList) . ") "
. "AND sru.relation_type = 0"
. ")";

if (api_is_multiple_url_enabled()) {
$sessionAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
$accessUrlId = api_get_current_access_url_id();

if ($accessUrlId != -1) {
$sql = "SELECT DISTINCT s.* "
. "FROM $sessionTable s "
. "INNER JOIN $sessionUserTable sru ON s.id = sru.id_session "
. "INNER JOIN $sessionAccessUrlTable srau ON s.id = srau.session_id "
. "WHERE srau.access_url_id = $accessUrlId "
. "AND ( "
. "sru.id_user IN (" . implode(', ', $userIdList) . ") "
. "AND sru.relation_type = 0"
. ")";
}
}

$result = Database::query($sql);

while ($row = Database::fetch_assoc($result)) {
$sessionList[] = $row;
}

return $sessionList;
}
}

0 comments on commit 2666343

Please sign in to comment.