Skip to content

Commit 2a40088

Browse files
committed
Webservice: Add support for social groups/classes through REST API, update return values of usergroup.lib.php methods, fix getDataToExport filters - refs BT#20460
2 parents 094b9dc + 03e8c26 commit 2a40088

File tree

4 files changed

+455
-38
lines changed

4 files changed

+455
-38
lines changed

main/exercise/fill_blanks.class.php

-3
Original file line numberDiff line numberDiff line change
@@ -1384,9 +1384,6 @@ public static function getHtmlWrongAnswer(
13841384
$resultsDisabled = false,
13851385
$showTotalScoreAndUserChoices = false
13861386
) {
1387-
if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK) {
1388-
return '';
1389-
}
13901387

13911388
return self::getHtmlAnswer(
13921389
$answer,

main/inc/lib/usergroup.lib.php

+118-35
Original file line numberDiff line numberDiff line change
@@ -720,21 +720,34 @@ public function usergroup_was_added_in_course(
720720
* Gets a list of session ids by user group.
721721
*
722722
* @param int $id group id
723+
* @param bool $returnSessionData Whether to return an array with info (true) or just the session ID (false)
723724
*
724725
* @return array
725726
*/
726-
public function get_sessions_by_usergroup($id)
727+
public function get_sessions_by_usergroup($id, $returnSessionData = false)
727728
{
728-
$results = Database::select(
729-
'session_id',
730-
$this->usergroup_rel_session_table,
731-
['where' => ['usergroup_id = ?' => $id]]
732-
);
729+
if ($returnSessionData) {
730+
$results = Database::select(
731+
'g.session_id, s.name, s.description, s.nbr_users, s.nbr_courses',
732+
$this->usergroup_rel_session_table." g, ".$this->session_table." s",
733+
['where' => ['g.session_id = s.id AND g.usergroup_id = ?' => $id]]
734+
);
735+
} else {
736+
$results = Database::select(
737+
'session_id',
738+
$this->usergroup_rel_session_table,
739+
['where' => ['usergroup_id = ?' => $id]]
740+
);
741+
}
733742

734743
$array = [];
735744
if (!empty($results)) {
736745
foreach ($results as $row) {
737-
$array[] = $row['session_id'];
746+
if ($returnSessionData) {
747+
$array[$row['session_id']] = $row;
748+
} else {
749+
$array[] = $row['session_id'];
750+
}
738751
}
739752
}
740753

@@ -912,17 +925,22 @@ public function get_usergroup_by_user($userId)
912925
* @param int $usergroup_id usergroup id
913926
* @param array $list list of session ids
914927
* @param bool $deleteCurrentSessions Optional. Empty the session list for the usergroup (class)
928+
*
929+
* @return array List of IDs of the sessions added to the usergroup
915930
*/
916-
public function subscribe_sessions_to_usergroup($usergroup_id, $list, $deleteCurrentSessions = true)
931+
public function subscribe_sessions_to_usergroup($usergroup_id, $list, $deleteCurrentSessions = true): array
917932
{
918933
$current_list = $this->get_sessions_by_usergroup($usergroup_id);
919934
$user_list = $this->get_users_by_usergroup($usergroup_id);
920935

921936
$delete_items = $new_items = [];
922937
if (!empty($list)) {
923938
foreach ($list as $session_id) {
924-
if (!in_array($session_id, $current_list)) {
925-
$new_items[] = $session_id;
939+
if (SessionManager::isValidId($session_id)) {
940+
// Only if the session IDs given are not bogus
941+
if (!in_array($session_id, $current_list)) {
942+
$new_items[] = $session_id;
943+
}
926944
}
927945
}
928946
}
@@ -963,7 +981,7 @@ public function subscribe_sessions_to_usergroup($usergroup_id, $list, $deleteCur
963981
}
964982
}
965983

966-
$sessions = '';
984+
$sessions = [];
967985
// Adding new relationships.
968986
if (!empty($new_items)) {
969987
foreach ($new_items as $session_id) {
@@ -977,18 +995,19 @@ public function subscribe_sessions_to_usergroup($usergroup_id, $list, $deleteCur
977995
null,
978996
false
979997
);
980-
$sessions .= $session_id.',';
998+
$sessions[] = $session_id;
981999
}
9821000
}
9831001
// Add event to system log
9841002
Event::addEvent(
9851003
LOG_GROUP_PORTAL_SESSION_SUBSCRIBED,
9861004
LOG_GROUP_PORTAL_ID,
987-
'gid: '.$usergroup_id.' - sids: '.substr($sessions, 0, -1),
1005+
'gid: '.$usergroup_id.' - sids: '.implode(',', $sessions),
9881006
api_get_utc_datetime(),
9891007
api_get_user_id()
9901008
);
9911009
}
1010+
return $sessions;
9921011
}
9931012

9941013
/**
@@ -1024,9 +1043,10 @@ public function subscribe_courses_to_usergroup($usergroup_id, $list, $delete_gro
10241043
$this->unsubscribe_courses_from_usergroup($usergroup_id, $delete_items);
10251044
}
10261045

1046+
1047+
$courses = [];
10271048
// Adding new relationships
10281049
if (!empty($new_items)) {
1029-
$courses = '';
10301050
foreach ($new_items as $course_id) {
10311051
$course_info = api_get_course_info_by_id($course_id);
10321052
if ($course_info) {
@@ -1088,29 +1108,32 @@ public function subscribe_courses_to_usergroup($usergroup_id, $list, $delete_gro
10881108
$params
10891109
);
10901110
}
1091-
$courses .= $course_id.',';
1111+
$courses[] = $course_id;
10921112
}
10931113
// Add event to system log
10941114
Event::addEvent(
10951115
LOG_GROUP_PORTAL_COURSE_SUBSCRIBED,
10961116
LOG_GROUP_PORTAL_ID,
1097-
'gid: '.$usergroup_id.' - cids: '.substr($courses, 0, -1),
1117+
'gid: '.$usergroup_id.' - cids: '.implode(',', $courses),
10981118
api_get_utc_datetime(),
10991119
api_get_user_id()
11001120
);
11011121
}
1122+
1123+
return $courses;
11021124
}
11031125

11041126
/**
1127+
* Unsubscribe a usergroup from a list of courses
11051128
* @param int $usergroup_id
11061129
* @param array $delete_items
11071130
*/
11081131
public function unsubscribe_courses_from_usergroup($usergroup_id, $delete_items)
11091132
{
1133+
$courses = [];
11101134
// Deleting items.
11111135
if (!empty($delete_items)) {
11121136
$user_list = $this->get_users_by_usergroup($usergroup_id);
1113-
$courses = '';
11141137
foreach ($delete_items as $course_id) {
11151138
$course_info = api_get_course_info_by_id($course_id);
11161139
if ($course_info) {
@@ -1133,18 +1156,69 @@ public function unsubscribe_courses_from_usergroup($usergroup_id, $delete_items)
11331156
],
11341157
]
11351158
);
1136-
$courses .= $course_id.',';
1159+
$courses[] = $course_id;
11371160
}
11381161
}
11391162
// Add event to system log
11401163
Event::addEvent(
11411164
LOG_GROUP_PORTAL_COURSE_UNSUBSCRIBED,
11421165
LOG_GROUP_PORTAL_ID,
1143-
'gid: '.$usergroup_id.' - cids: '.substr($courses, 0, -1),
1166+
'gid: '.$usergroup_id.' - cids: '.implode(',', $courses),
1167+
api_get_utc_datetime(),
1168+
api_get_user_id()
1169+
);
1170+
}
1171+
1172+
return $courses;
1173+
}
1174+
1175+
/**
1176+
* Unsubscribe a usergroup from a list of sessions
1177+
* @param int $groupId
1178+
* @param array $items Session IDs to remove from the group
1179+
* @return array The list of session IDs that have been unsubscribed from the group
1180+
*/
1181+
public function unsubscribeSessionsFromUserGroup($groupId, $items)
1182+
{
1183+
// Deleting items.
1184+
$sessions = [];
1185+
if (!empty($items)) {
1186+
$users = $this->get_users_by_usergroup($groupId);
1187+
foreach ($items as $sessionId) {
1188+
if (SessionManager::isValidId($sessionId)) {
1189+
if (!api_get_configuration_value('usergroup_do_not_unsubscribe_users_from_session_on_session_unsubscribe')) {
1190+
if (!empty($users)) {
1191+
foreach ($users as $userId) {
1192+
SessionManager::unsubscribe_user_from_session(
1193+
$sessionId,
1194+
$userId
1195+
);
1196+
}
1197+
}
1198+
}
1199+
Database::delete(
1200+
$this->usergroup_rel_session_table,
1201+
[
1202+
'usergroup_id = ? AND session_id = ?' => [
1203+
$groupId,
1204+
$sessionId,
1205+
],
1206+
]
1207+
);
1208+
$sessions[] = $sessionId;
1209+
}
1210+
}
1211+
// Add event to system log
1212+
Event::addEvent(
1213+
LOG_GROUP_PORTAL_SESSION_UNSUBSCRIBED,
1214+
LOG_GROUP_PORTAL_ID,
1215+
'gid: '.$groupId.' - sids: '.implode(',', $sessions),
11441216
api_get_utc_datetime(),
11451217
api_get_user_id()
11461218
);
11471219
}
1220+
1221+
return $sessions;
11481222
}
11491223

11501224
/**
@@ -1340,6 +1414,8 @@ public function usergroup_exists($name)
13401414
}
13411415

13421416
/**
1417+
* Returns whether teachers can access the classes, as per 'allow_teachers_to_classes' setting
1418+
*
13431419
* @return bool
13441420
*/
13451421
public function allowTeachers()
@@ -1435,21 +1511,25 @@ public function getUsergroupsPagination($sidx, $sord, $start, $limit, $extraWher
14351511
*/
14361512
public function getDataToExport($options = [])
14371513
{
1514+
$and = '';
1515+
if (!empty($options) && !empty($options['where'])) {
1516+
$and = ' AND ';
1517+
}
14381518
if ($this->getUseMultipleUrl()) {
14391519
$urlId = api_get_current_access_url_id();
14401520
$from = $this->table." u
14411521
INNER JOIN {$this->access_url_rel_usergroup} a
14421522
ON (u.id = a.usergroup_id)";
1443-
$options = ['where' => ['access_url_id = ? ' => $urlId]];
1523+
$options['where'][$and.' access_url_id = ? '] = $urlId;
14441524
if ($this->allowTeachers()) {
1445-
$options['where'] = [' author_id = ? ' => api_get_user_id()];
1525+
$options['where'] = [' AND author_id = ? ' => api_get_user_id()];
14461526
}
1447-
$classes = Database::select('u.id, name, description', $from, $options);
1527+
$classes = Database::select('u.id, name, description, group_type, visibility', $from, $options);
14481528
} else {
14491529
if ($this->allowTeachers()) {
1450-
$options['where'] = [' author_id = ? ' => api_get_user_id()];
1530+
$options['where'] = [$and.' author_id = ? ' => api_get_user_id()];
14511531
}
1452-
$classes = Database::select('id, name, description', $this->table, $options);
1532+
$classes = Database::select('id, name, description, group_type, visibility', $this->table, $options);
14531533
}
14541534

14551535
$result = [];
@@ -1841,15 +1921,18 @@ public function delete($id)
18411921
WHERE usergroup_id = $id";
18421922
Database::query($sql);
18431923

1844-
parent::delete($id);
1924+
$res = parent::delete($id);
18451925
// Add event to system log
1846-
Event::addEvent(
1847-
LOG_GROUP_PORTAL_DELETED,
1848-
LOG_GROUP_PORTAL_ID,
1849-
'id: '.$id,
1850-
api_get_utc_datetime(),
1851-
api_get_user_id()
1852-
);
1926+
if ($res) {
1927+
Event::addEvent(
1928+
LOG_GROUP_PORTAL_DELETED,
1929+
LOG_GROUP_PORTAL_ID,
1930+
'id: '.$id,
1931+
api_get_utc_datetime(),
1932+
api_get_user_id()
1933+
);
1934+
}
1935+
return $res;
18531936
}
18541937

18551938
/**
@@ -2360,14 +2443,14 @@ public function add_users_to_groups($user_list, $group_list, $relation_type = GR
23602443
}
23612444

23622445
/**
2363-
* Deletes an url and session relationship.
2446+
* Deletes the subscription of a user to a usergroup
23642447
*
23652448
* @author Julio Montoya
23662449
*
23672450
* @param int $userId
23682451
* @param int $groupId
23692452
*
2370-
* @return bool true if success
2453+
* @return bool true on success
23712454
* */
23722455
public function delete_user_rel_group($userId, $groupId)
23732456
{
@@ -2769,7 +2852,7 @@ public function get_all_users_by_group($group_id)
27692852
return [];
27702853
}
27712854

2772-
$sql = "SELECT u.id, u.firstname, u.lastname, relation_type
2855+
$sql = "SELECT u.id, u.firstname, u.lastname, gu.relation_type
27732856
FROM $tbl_user u
27742857
INNER JOIN $table_group_rel_user gu
27752858
ON (gu.user_id = u.id)

0 commit comments

Comments
 (0)