Commit d8516bce authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add function to create or update a favourites list



This function must be called by a user with institutional admin
permission over the favourites list owner.

Change-Id: I0e1597a662e1af0708fdc1ea9ebce27212e08203
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 1d4b387f
......@@ -2222,3 +2222,97 @@ function username_to_id($usernames) {
}
return empty($ids) ? array() : $ids;
}
/**
* Update or create a favourites list for a user
*
* @param $owner integer owner of the favorites list
* @param $shortname string name for the favorites list
* @param $institution string institution with permission to update the favorites list
* @param $userlist array array of userids to add to the list
*/
function update_favorites($owner, $shortname, $institution, $userlist) {
global $USER;
if (empty($institution)) {
// User-editable favorites lists are not implemented yet.
return;
}
if (!$USER->can_edit_institution($institution)) {
throw new AccessDeniedException("update_favorites: access denied");
}
$owner = (int) $owner;
if ($institution == 'mahara') {
if (!record_exists('usr', 'id', $owner, 'deleted', 0)) {
throw new NotFoundException("update_favorites: user $owner not found");
}
}
else {
$sql = '
SELECT u.id
FROM {usr} u JOIN {usr_institution} ui ON u.id = ui.usr AND ui.institution = ?
WHERE u.id = ? AND u.deleted = 0';
if (!record_exists_sql($sql, array($institution, $owner))) {
throw new NotFoundException("update_favorites: user $owner not found in institution $institution");
}
}
$listdata = get_record('favorite', 'owner', $owner, 'shortname', $shortname);
if ($listdata && $listdata->institution != $institution) {
throw new AccessDeniedException("update_favorites: user $owner already has a favorites list called $shortname which is updated by another institution");
}
if (!is_array($userlist)) {
throw new SystemException("update_favorites: userlist is not an array");
}
if (!empty($userlist)) {
$idstr = join(',', array_map('intval', $userlist));
if ($institution == 'mahara') {
$userids = get_column_sql("SELECT id FROM {usr} WHERE id IN ($idstr) AND deleted = 0", array());
}
else {
// Remove anyone who is not in this institution
$userids = get_column_sql('
SELECT u.id
FROM {usr} u JOIN {usr_institution} ui ON u.id = ui.usr AND ui.institution = ?
WHERE u.id IN (' . $idstr . ') AND u.deleted = 0',
array($institution)
);
}
}
if (empty($userids)) {
$userids = array();
}
db_begin();
$now = db_format_timestamp(time());
if ($listdata) {
delete_records('favorite_usr', 'favorite', $listdata->id);
$listdata->mtime = $now;
update_record('favorite', $listdata, 'id');
}
else {
$listdata = (object) array(
'owner' => $owner,
'shortname' => $shortname,
'institution' => $institution,
'ctime' => $now,
'mtime' => $now,
);
$listdata->id = insert_record('favorite', $listdata, 'id', true);
}
foreach ($userids as $userid) {
insert_record('favorite_usr', (object) array('favorite' => $listdata->id, 'usr' => $userid));
}
db_commit();
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment