Commit 0e2f1c39 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Update queries using view_access_group,view_access_usr,view_access_token


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent c3c9f247
......@@ -47,7 +47,7 @@ define('TITLE', get_string('deletespecifiedgroup', 'group', $group->name));
$views = count_records_sql(
'SELECT COUNT(*)
FROM {view_access_group} a
FROM {view_access} a
WHERE a.group = ?',
array($groupid)
);
......
......@@ -48,16 +48,6 @@ if (!group_user_can_leave($group)) {
$goto = get_config('wwwroot') . 'group/' . $returnto . '.php' . ($returnto == 'view' ? ('?id=' . $groupid) : '');
$views = count_records_sql(
'SELECT COUNT(*)
FROM {view} v
INNER JOIN {view_access_group} a
ON a.group = ?
AND a.view = v.id
WHERE v.owner = ?',
array($groupid, $USER->get('id'))
);
$form = pieform(array(
'name' => 'leavegroup',
'renderer' => 'div',
......@@ -79,7 +69,7 @@ $form = pieform(array(
$smarty = smarty();
$smarty->assign('subheading', get_string('leavespecifiedgroup', 'group', $group->name));
$smarty->assign('form', $form);
$smarty->assign('message', $views ? get_string('groupconfirmleavehasviews', 'group') : get_string('groupconfirmleave', 'group'));
$smarty->assign('message', get_string('groupconfirmleave', 'group');
$smarty->assign('group', $group);
$smarty->display('group/leave.tpl');
......
......@@ -231,20 +231,20 @@ function activity_get_viewaccess_users($view, $owner, $type) {
FROM {usr_friend} f
JOIN {view} v ON (v.owner = f.usr1 OR v.owner = f.usr2)
JOIN {view_access} vu ON vu.view = v.id
WHERE (usr1 = ? OR usr2 = ?) AND vu.accesstype = ? AND v.id = ?
WHERE (usr1 = ? OR usr2 = ?) AND vu.accesstype = 'friends' AND v.id = ?
UNION SELECT usr AS userid
FROM {view_access_usr} u
FROM {view_access} u
WHERE u.view = ?
UNION SELECT m.member
FROM {group_member} m
JOIN {view_access_group} vg ON vg.group = m.group
JOIN {view_access} vg ON vg.group = m.group
JOIN {group} g ON (g.id = vg.group AND g.deleted = 0)
WHERE vg.view = ? AND (vg.role IS NULL OR vg.role = m.role)
) AS userlist
JOIN {usr} u ON u.id = userlist.userid
LEFT JOIN {usr_activity_preference} p ON p.usr = u.id AND p.activity = ?
LEFT JOIN {usr_account_preference} ap ON ap.usr = u.id AND ap.field = 'lang'";
$values = array($owner, $owner, $owner, 'friends', $view, $view, $view, $type->id);
$values = array($owner, $owner, $owner, $view, $view, $view, $type->id);
if (!$u = get_records_sql_assoc($sql, $values)) {
$u = array();
}
......
......@@ -428,8 +428,6 @@ function group_user_can_leave($group, $userid=null) {
/**
* Removes a user from a group.
*
* Also removes view access given by the user to the group
*
* @param int $groupid ID of group
* @param int $userid ID of user to remove
*/
......@@ -438,19 +436,7 @@ function group_remove_user($groupid, $userid=null, $force=false) {
if (!$force && !group_user_can_leave($groupid, $userid)) {
throw new AccessDeniedException(get_string('usercantleavegroup', 'group'));
}
db_begin();
delete_records('group_member', 'group', $groupid, 'member', $userid);
delete_records_sql(
'DELETE FROM {view_access_group}
WHERE "group" = ?
AND "view" IN (
SELECT v.id
FROM {view} v
WHERE v.owner = ?
)',
array($groupid, $userid)
);
db_commit();
require_once(get_config('docroot') . 'interaction/lib.php');
$interactions = get_column('interaction_instance', 'id', 'group', $groupid);
......
......@@ -1641,12 +1641,12 @@ function can_view_view($view_id, $user_id=null, $usertoken=null, $mnettoken=null
WHERE "view" = ?
UNION
SELECT \'user\' AS type, 2 AS typeorder, ' . db_format_tsfield('startdate') . ', ' . db_format_tsfield('stopdate') . '
FROM {view_access_usr}
FROM {view_access}
WHERE "view" = ? AND usr = ?
UNION
SELECT \'group\' AS type, 3 AS typeorder, ' . db_format_tsfield('startdate') . ', ' . db_format_tsfield('stopdate') . '
FROM
{view_access_group} vg
{view_access} vg
INNER JOIN {group} g ON (vg.group = g.id AND g.deleted = 0)
INNER JOIN {group_member} m ON (g.id = m.group AND (vg.role IS NULL OR vg.role = m.role))
WHERE vg.view = ? AND m.member = ?
......@@ -1680,7 +1680,7 @@ function get_view_from_token($token, $visible=true) {
}
return get_field_sql('
SELECT "view"
FROM {view_access_token}
FROM {view_access}
WHERE token = ? AND visible = ?
AND (startdate IS NULL OR startdate < current_timestamp)
AND (stopdate IS NULL OR stopdate > current_timestamp)
......@@ -1788,7 +1788,7 @@ function get_views($users, $userlooking=null, $limit=5, $type=null) {
' . db_format_tsfield('ctime') . '
FROM
{view} v
INNER JOIN {view_access_usr} a ON v.id=a.view AND a.usr=?
INNER JOIN {view_access} a ON v.id=a.view AND a.usr=?
WHERE
v.owner IN (' . join(',',array_map('db_quote', array_keys($users))) . ')
AND ( v.startdate IS NULL OR v.startdate < ? )
......@@ -1816,7 +1816,7 @@ function get_views($users, $userlooking=null, $limit=5, $type=null) {
' . db_format_tsfield('v.ctime','ctime') . '
FROM
{view} v
INNER JOIN {view_access_group} a ON v.id=a.view
INNER JOIN {view_access} a ON v.id=a.view
INNER JOIN {group_member} m ON m.group=a.group AND m.member=?
INNER JOIN {group} g ON (g.id = a.group AND g.deleted = ?)
WHERE
......
......@@ -1057,7 +1057,7 @@ function delete_user($userid) {
delete_records('usr_institution_request', 'usr', $userid);
delete_records('usr_password_request', 'usr', $userid);
delete_records('usr_watchlist_view', 'usr', $userid);
delete_records('view_access_usr', 'usr', $userid);
delete_records('view_access', 'usr', $userid);
// Remove the user's views & artefacts
$viewids = get_column('view', 'id', 'owner', $userid);
......
......@@ -517,9 +517,6 @@ class View {
db_begin();
ArtefactTypeComment::delete_view_comments($this->id);
delete_records('view_access','view',$this->id);
delete_records('view_access_group','view',$this->id);
delete_records('view_access_usr','view',$this->id);
delete_records('view_access_token', 'view', $this->id);
delete_records('view_autocreate_grouptype', 'view', $this->id);
delete_records('view_tag','view',$this->id);
delete_records('view_visit','view',$this->id);
......@@ -539,32 +536,19 @@ class View {
public function get_access($timeformat=null) {
if (is_mysql()) {
$uid = 'usr';
$gid = '"group"';
}
else {
$uid = 'CAST (usr AS TEXT)';
$gid = 'CAST ("group" AS TEXT)';
}
$data = get_records_sql_array("
SELECT accesstype AS type, NULL AS id, NULL AS role, NULL AS grouptype, startdate, stopdate, allowfeedback, approvefeedback
FROM {view_access}
WHERE \"view\" = ?
UNION
SELECT 'user' AS type, $uid AS id, NULL AS role, NULL AS grouptype, startdate, stopdate, allowfeedback, approvefeedback
FROM {view_access_usr}
WHERE \"view\" = ?
UNION
SELECT 'group', $gid, \"role\", grouptype, startdate, stopdate, allowfeedback, approvefeedback FROM {view_access_group}
INNER JOIN {group} g ON (\"group\" = g.id AND g.deleted = ?)
WHERE \"view\" = ?
UNION
SELECT 'token', token, NULL AS role, NULL AS grouptype, startdate, stopdate, allowfeedback, approvefeedback
FROM {view_access_token}
WHERE \"view\" = ? AND visible = 1
", array($this->id, $this->id, 0, $this->id, $this->id));
SELECT va.*, g.grouptype,
CASE WHEN NOT va.accesstype IS NULL THEN va.accesstype
WHEN NOT va.usr IS NULL THEN 'user'
WHEN NOT va.group IS NULL THEN 'group'
WHEN NOT va.token IS NULL THEN 'token'
ELSE NULL END AS type
FROM {view_access} va
LEFT OUTER JOIN {group} g ON (va.group = g.id AND g.deleted = 0)
WHERE va.view = ? AND va.visible = 1",
array($this->id)
);
if ($data) {
foreach ($data as &$item) {
$item = (array)$item;
......@@ -659,10 +643,7 @@ class View {
// endforeach
//
db_begin();
delete_records('view_access', 'view', $this->get('id'));
delete_records('view_access_usr', 'view', $this->get('id'));
delete_records('view_access_group', 'view', $this->get('id'));
delete_records('view_access_token', 'view', $this->get('id'), 'visible', 1);
delete_records('view_access', 'view', $this->get('id'), 'visible', 1);
$time = db_format_timestamp(time());
// View access
......@@ -676,16 +657,8 @@ class View {
foreach ($accessdata as $item) {
$accessrecord = new StdClass;
$accessrecord->view = $this->get('id');
if (!empty($item['allowfeedback'])) {
$accessrecord->allowfeedback = 1;
} else {
$accessrecord->allowfeedback = 0;
}
if (!empty($item['approvefeedback'])) {
$accessrecord->approvefeedback = 1;
} else {
$accessrecord->approvefeedback = 0;
}
$accessrecord->allowcomments = (bool) $item['allowcomments'];
$accessrecord->approvecomments = (bool) $item['approvecomments'];
if (isset($item['startdate'])) {
$accessrecord->startdate = db_format_timestamp($item['startdate']);
}
......@@ -708,7 +681,7 @@ class View {
case 'user':
$accessrecord->usr = $item['id'];
if (array_search($accessrecord, $accessdata_added) === false) {
insert_record('view_access_usr', $accessrecord);
insert_record('view_access', $accessrecord);
$accessdata_added[] = $accessrecord;
}
break;
......@@ -723,7 +696,7 @@ class View {
$accessrecord->role = $item['role'];
}
if (array_search($accessrecord, $accessdata_added) === false) {
insert_record('view_access_group', $accessrecord);
insert_record('view_access', $accessrecord);
$accessdata_added[] = $accessrecord;
}
......@@ -731,7 +704,7 @@ class View {
case 'token':
$accessrecord->token = $item['id'];
if (array_search($accessrecord, $accessdata_added) === false) {
insert_record('view_access_token', $accessrecord);
insert_record('view_access', $accessrecord);
$accessdata_added[] = $accessrecord;
}
break;
......@@ -2126,21 +2099,13 @@ class View {
WHERE va.view IN (' . $viewidlist . ')
GROUP BY va.view, va.artefact, a.title, a.artefacttype, t.plugin
ORDER BY a.title, va.artefact', '');
$accessgroups = get_records_sql_array('SELECT "view", accesstype, grouptype, "role", id, name, startdate, stopdate
FROM (
SELECT "view", \'group\' AS accesstype, g.grouptype, vg.role, g.id, g.name, startdate, stopdate
FROM {view_access_group} vg
INNER JOIN {group} g ON g.id = vg.group AND g.deleted = 0
UNION SELECT view, \'user\' AS accesstype, NULL AS grouptype, NULL AS role, usr AS id, \'\' AS name, startdate, stopdate
FROM {view_access_usr} vu
UNION SELECT "view", \'secreturl\' AS accesstype, NULL AS grouptype, NULL AS role, 0 AS id, \'\' AS name, startdate, stopdate
FROM {view_access_token} vt
UNION SELECT "view", accesstype, NULL AS grouptype, NULL AS role, 0 AS id, \'\' AS name, startdate, stopdate
FROM {view_access} va
) AS a
WHERE "view" in (' . $viewidlist . ')
ORDER BY "view", accesstype, grouptype, "role", name, id
', array());
$accessgroups = get_records_sql_array('
SELECT va.*, g.grouptype, g.name
FROM {view_access} va LEFT OUTER JOIN {group} g ON (g.id = va.group AND g.deleted = 0)
WHERE va.view IN (' . $viewidlist . ')
ORDER BY va.view, va.accesstype, g.grouptype, va.role, g.name, va.group, va.usr',
array()
);
$tags = get_records_select_array('view_tag', '"view" IN (' . $viewidlist . ')');
}
......@@ -2212,15 +2177,21 @@ class View {
}
if ($accessgroups) {
foreach ($accessgroups as $access) {
$data[$index[$access->view]]['accessgroups'][] = array(
'accesstype' => $access->accesstype, // friends, group, loggedin, public, tutorsgroup, user, secreturl
'role' => $access->role,
'roledisplay' => $access->role ? get_string($access->role, 'grouptype.' . $access->grouptype) : null,
'id' => $access->id,
'name' => $access->name,
'startdate' => $access->startdate,
'stopdate' => $access->stopdate
);
if ($access->usr) {
$access->accesstype = 'user';
$access->id = $access->usr;
}
else if ($access->group) {
$access->accesstype = 'group';
$access->id = $access->group;
if ($access->role) {
$access->roledisplay = get_string($access->role, 'grouptype.' . $access->grouptype);
}
}
else if ($access->token) {
$access->accesstype = 'secreturl';
}
$data[$index[$access->view]]['accessgroups'][] = (array) $access;
}
}
if ($tags) {
......@@ -2358,11 +2329,12 @@ class View {
) AS vg ON (vg.groupid = v.group)
LEFT OUTER JOIN {view_access} va ON (
va.view = v.id
AND va.accesstype IS NOT NULL
AND (va.startdate IS NULL OR va.startdate < current_timestamp)
AND (va.stopdate IS NULL OR va.stopdate > current_timestamp)
)
LEFT OUTER JOIN {usr_friend} f ON (usr1 = v.owner AND usr2 = ?)
LEFT OUTER JOIN {view_access_usr} vau ON (
LEFT OUTER JOIN {view_access} vau ON (
vau.view = v.id
AND (vau.startdate IS NULL OR vau.startdate < current_timestamp)
AND (vau.stopdate IS NULL OR vau.stopdate > current_timestamp)
......@@ -2371,7 +2343,7 @@ class View {
LEFT OUTER JOIN (
SELECT
vag.view, vagm.member
FROM {view_access_group} vag
FROM {view_access} vag
INNER JOIN {group_member} vagm ON (vag.group = vagm.group AND (vag.role = vagm.role OR vag.role IS NULL))
WHERE
(vag.startdate IS NULL OR vag.startdate < current_timestamp)
......@@ -2556,7 +2528,7 @@ class View {
}
$from = '
FROM {view} v
INNER JOIN {view_access_group} a ON (a.view = v.id)
INNER JOIN {view_access} a ON (a.view = v.id)
INNER JOIN {group_member} m ON (a.group = m.group AND (a.role = m.role OR a.role IS NULL))
WHERE a.group = ? AND m.member = ? AND (v.group IS NULL OR v.group != ?)';
$ph = array($groupid, $userid, $groupid);
......@@ -2814,17 +2786,17 @@ class View {
// Currently it only makes sense to have one invisible key per view.
// They are only used during view submission, and a view can only be
// submitted to one group or remote host at any one time.
delete_records('view_access_token', 'view', $viewid, 'visible', 0);
delete_records_select('view_access', 'view = ? AND token IS NOT NULL AND visible = 0', array($viewid));
}
$data = new StdClass;
$data->view = $viewid;
$data->visible = (int) $visible;
$data->token = get_random_key(20);
while (record_exists('view_access_token', 'token', $data->token)) {
while (record_exists('view_access', 'token', $data->token)) {
$data->token = get_random_key(20);
}
if (insert_record('view_access_token', $data)) {
if (insert_record('view_access', $data)) {
return $data;
}
return false;
......
Markdown is supported
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