Commit 6763b52a authored by Francois Marier's avatar Francois Marier Committed by Gerrit Code Review
Browse files

Merge changes I7b123f20,I760eec99,Ib0352e62,I43e4f752,I78086fb3

* changes:
  Add "shared with" option to shared pages (part of bug #857793)
  Add sorting options to Shared Pages (part of bug #857793)
  Use http_build_query rather than urlencode on Shared pages
  Use view_search function to fetch data for Shared pages
  Add sorting by owner name in view_search
parents dc58ab00 1e51bab6
......@@ -36,19 +36,53 @@ $tag = param_variable('tag', null);
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$queryparams = array();
if (!empty($tag)) {
$querystring = '?tag=' . urlencode($tag);
$queryparams['tag'] = $tag;
$query = null;
}
else if (!empty($query)) {
$queryparams['query'] = $query;
}
$sortoptions = array(
'lastchanged',
'mtime',
'ownername',
'title',
);
if (!in_array($sort = param_alpha('sort', 'lastchanged'), $sortoptions)) {
$sort = 'lastchanged';
}
if ($sort !== 'lastchanged') {
$queryparams['sort'] = $sort;
}
$sortdir = ($sort == 'lastchanged' || $sort == 'mtime') ? 'desc' : 'asc';
$shareoptions = array(
'user',
'friend',
'group',
'institution',
'loggedin',
'public',
);
$share = param_variable('share', array());
if (is_array($share)) {
$share = $queryparams['share'] = array_intersect($share, $shareoptions);
}
else {
$querystring = empty($query) ? '' : ('?query=' . urlencode($query));
$share = null;
}
$data = View::shared_to_user($query, $tag, $limit, $offset);
$data = View::shared_to_user($query, $tag, $limit, $offset, $sort, $sortdir, $share);
$pagination = build_pagination(array(
'id' => 'sharedviews_pagination',
'url' => get_config('wwwroot') . 'view/sharedviews.php' . $querystring,
'url' => get_config('wwwroot') . 'view/sharedviews.php' . (empty($queryparams) ? '' : ('?' . http_build_query($queryparams))),
'jsonscript' => '/json/sharedviews.php',
'datatable' => 'sharedviewlist',
'count' => $data->count,
......
......@@ -632,6 +632,7 @@ $string['results'] = 'results';
$string['Results'] = 'Results';
$string['noresultsfound'] = 'No results found';
$string['users'] = 'Users';
$string['searchwithin'] = 'Search within';
// artefact
$string['artefact'] = 'artefact';
......@@ -958,6 +959,9 @@ $string['nusers'] = array(
'%s users',
);
$string['hidden'] = 'hidden';
$string['lastupdate'] = 'Last update';
$string['lastupdateorcomment'] = 'Last update or comment';
$string['Title'] = 'Title';
// import related strings (maybe separated later)
$string['importedfrom'] = 'Imported from %s';
......
......@@ -347,3 +347,5 @@ $string['sharedviews'] = 'Shared pages';
$string['titleanddescription'] = 'Title, description, tags';
$string['tagsonly'] = 'Tags only';
$string['sharedviewsdescription'] = 'This page lists the most recently modified or commented on pages that have been shared with you. They may have been shared with you directly, shared with friends of the owner, or shared with one of your groups.';
$string['sharedwith'] = 'Shared with';
$string['Me'] = 'Me';
......@@ -3032,6 +3032,14 @@ class View {
';
$orderby .= 'GREATEST(lastcomment, v.mtime)';
}
else if ($item['column'] == 'ownername') {
// Join on usr, group, and institution and order by name
$from .= 'LEFT OUTER JOIN {usr} su ON su.id = v.owner
LEFT OUTER JOIN {group} sg ON sg.id = v.group
LEFT OUTER JOIN {institution} si ON si.name = v.institution
';
$orderby .= "COALESCE(sg.name, si.displayname, CASE WHEN su.preferredname IS NOT NULL AND su.preferredname != '' THEN su.preferredname ELSE su.firstname || ' ' || su.lastname END)";
}
else {
$orderby .= 'v.' . $item['column'];
}
......@@ -3067,87 +3075,43 @@ class View {
}
return (object) array(
'ids' => array_keys($viewdata),
'data' => array_values($viewdata),
'count' => $count,
);
}
/**
* Get views that have been actively shared with a user, either
* shared with the user individually, shared as a friend of the
* view owner, or shared with one of the user's groups.
* Get views that have been shared with a user using the given
* access types.
*
* @param string $query Search string for title/description
* @param string $tag Return only views with this tag
* @param integer $limit
* @param integer $offset
* @param string $sort Either 'lastchanged', 'ownername', or a column of the view table
* @param string $sortdir Ascending/descending
* @param array $accesstypes Types of view access
*
*/
public static function shared_to_user($query=null, $tag=null, $limit=null, $offset=0) {
global $USER;
$viewerid = $USER->get('id');
$values = array();
$select = "
SELECT va.view
FROM {view_access} va
INNER JOIN {view} v ON va.view = v.id";
if ($tag) { // Filter by the tag
$select .= "
INNER JOIN {view_tag} vt ON (vt.view = va.view AND vt.tag = ?)";
$values[] = $tag;
}
elseif ($query) { // Include matches on the title, description or tag
$select .= "
LEFT JOIN {view_tag} vt ON (vt.view = va.view AND vt.tag = ?)";
$values[] = $query;
}
$select .= "
LEFT JOIN {usr_friend} f1 ON (v.owner = f1.usr1 AND f1.usr2 = ?)
LEFT JOIN {usr_friend} f2 ON (v.owner = f2.usr2 AND f2.usr1 = ?)
LEFT JOIN {group_member} gm ON (va.group = gm.group AND (va.role IS NULL OR va.role = gm.role) AND gm.member = ?)
LEFT JOIN {usr_institution} ui ON (va.institution = ui.institution AND ui.usr = ?)";
array_push($values, $viewerid, $viewerid, $viewerid, $viewerid);
$where = "
WHERE
(v.owner IS NULL OR (v.owner > 0 AND v.owner != ?))
AND v.type NOT IN ('profile', 'dashboard', 'grouphomepage')
AND (v.startdate IS NULL OR v.startdate < current_timestamp)
AND (v.stopdate IS NULL OR v.stopdate > current_timestamp)
AND (va.startdate IS NULL OR va.startdate < current_timestamp)
AND (va.stopdate IS NULL OR va.stopdate > current_timestamp)
AND (va.usr = ?
OR (va.accesstype = 'friends' AND (f1.usr2 IS NOT NULL OR f2.usr1 IS NOT NULL))
OR gm.member IS NOT NULL
OR ui.institution IS NOT NULL
)";
array_push($values, $viewerid, $viewerid);
if ($query) {
$like = db_ilike();
$where .= "
AND (v.title $like '%' || ? || '%' OR v.description $like '%' || ? || '%' OR vt.tag = ?)";
array_push($values, $query, $query, $query);
}
public static function shared_to_user($query=null, $tag=null, $limit=null, $offset=0, $sort='lastchanged', $sortdir='desc',
$accesstypes=null) {
$innerselect = $select . $where;
$sort = array(
array(
'column' => $sort,
'desc' => $sortdir == 'desc',
)
);
$result = (object) array(
'data' => array(),
'count' => count_records_sql('SELECT COUNT(*) FROM {view} WHERE id IN (' . $innerselect . ')', $values),
$result = self::view_search(
$query, null, null, null, $limit, $offset, true, $sort, array('portfolio'), false, $accesstypes, $tag
);
if (!$result->count) {
return $result;
}
// Order by last comment.
if (is_postgres()) {
$lastcomments = '
SELECT DISTINCT ON (l.onview) l.onview, a.mtime, a.author, a.authorname, a.id, a.description
......@@ -3167,27 +3131,31 @@ class View {
GROUP BY onview';
}
$viewdata = get_records_sql_assoc('
// Get additional data: number of comments, last commenter
$commentdata = get_records_sql_assoc('
SELECT
v.id, v.title, v.description, v.owner, v.ownerformat, v.group, v.institution, v.mtime, v.ctime,
last.mtime AS lastcommenttime, last.author AS commentauthor, last.authorname AS commentauthorname,
v.id, last.mtime AS lastcommenttime, last.author AS commentauthor, last.authorname AS commentauthorname,
last.description AS commenttext, last.id AS commentid,
COUNT(c.artefact) AS commentcount
FROM {view} v
LEFT JOIN {artefact_comment_comment} c ON (c.onview = v.id AND c.deletedby IS NULL AND c.private = 0)
LEFT JOIN (' . $lastcomments . '
) last ON last.onview = v.id
WHERE v.id IN (' . $innerselect . ')
WHERE v.id IN (' . join(',', array_fill(0, count($result->data), '?')) . ')
GROUP BY
v.id, v.title, v.description, v.owner, v.ownerformat, v.group, v.institution, v.mtime, v.ctime,
last.mtime, last.author, last.authorname, last.description, last.id
ORDER BY GREATEST(last.mtime, v.mtime) DESC, v.title, v.id',
$values, $offset, $limit
v.id, last.mtime, last.author, last.authorname, last.description, last.id',
$result->ids
);
View::get_extra_view_info($viewdata, false);
$fields = array('lastcommenttime', 'commentauthor', 'commentauthorname', 'commenttext', 'commentid', 'commentcount');
$result->data =& $viewdata;
foreach ($result->data as &$v) {
if (isset($commentdata[$v['id']])) {
foreach ($fields as $f) {
$v[$f] = $commentdata[$v['id']]->$f;
}
}
}
return $result;
}
......
......@@ -38,6 +38,8 @@ $tag = param_variable('tag', null);
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$queryparams = array();
$searchoptions = array(
'titleanddescription' => get_string('titleanddescription', 'view'),
'tagsonly' => get_string('tagsonly', 'view'),
......@@ -45,41 +47,90 @@ $searchoptions = array(
if (!empty($tag)) {
$searchtype = 'tagsonly';
$searchdefault = $tag;
$querystring = '?tag=' . urlencode($tag);
$queryparams['tag'] = $tag;
$query = null;
}
else {
$searchtype = 'titleanddescription';
$searchdefault = $query;
$querystring = empty($query) ? '' : ('?query=' . urlencode($query));
if (!empty($query)) {
$queryparams['query'] = $query;
}
}
$sortoptions = array(
'lastchanged' => get_string('lastupdateorcomment'),
'mtime' => get_string('lastupdate'),
'ownername' => get_string('Owner', 'view'),
'title' => get_string('Title'),
);
if (!in_array($sort = param_alpha('sort', 'lastchanged'), array_keys($sortoptions))) {
$sort = 'lastchanged';
}
if ($sort !== 'lastchanged') {
$queryparams['sort'] = $sort;
}
$sortdir = ($sort == 'lastchanged' || $sort == 'mtime') ? 'desc' : 'asc';
$share = $queryparams['share'] = $sharedefault = array('user', 'friend', 'group');
$shareoptions = array(
'user' => get_string('Me', 'view'),
'friend' => get_string('friends', 'view'),
'group' => get_string('mygroups'),
);
if ($USER->get('institutions')) {
$shareoptions['institution'] = get_string('myinstitutions', 'group');
}
$shareoptions['loggedin'] = get_string('loggedin', 'view');
if (get_config('allowpublicviews')) {
$shareoptions['public'] = get_string('public', 'view');
}
foreach ($shareoptions as $k => &$v) {
$v = array('title' => $v, 'value' => $k, 'defaultvalue' => in_array($k, $sharedefault));
}
$searchform = pieform(array(
'name' => 'search',
'renderer' => 'oneline',
'dieaftersubmit' => false,
'elements' => array(
'query' => array(
'type' => 'text',
'title' => get_string('search') . ': ',
'title' => get_string('Query') . ': ',
'defaultvalue' => $searchdefault,
),
'type' => array(
'type' => 'select',
'title' => get_string('searchwithin') . ': ',
'options' => $searchoptions,
'defaultvalue' => $searchtype,
),
'submit' => array(
'sort' => array(
'type' => 'select',
'title' => get_string('sortresultsby') . ' ',
'options' => $sortoptions,
'defaultvalue' => $sort,
),
'share' => array(
'type' => 'checkboxes',
'title' => get_string('sharedwith', 'view') . ': ',
'elements' => $shareoptions,
'labelwidth' => 0,
),
'search' => array(
'type' => 'submit',
'value' => get_string('search')
)
),
)
));
$data = View::shared_to_user($query, $tag, $limit, $offset);
$data = View::shared_to_user($query, $tag, $limit, $offset, $sort, $sortdir, $share);
$pagination = build_pagination(array(
'id' => 'sharedviews_pagination',
'url' => get_config('wwwroot') . 'view/sharedviews.php' . $querystring,
'url' => get_config('wwwroot') . 'view/sharedviews.php' . (empty($queryparams) ? '' : ('?' . http_build_query($queryparams))),
'jsonscript' => '/json/sharedviews.php',
'datatable' => 'sharedviewlist',
'count' => $data->count,
......@@ -97,11 +148,27 @@ $smarty->display('view/sharedviews.tpl');
exit;
function search_submit(Pieform $form, $values) {
$goto = '/view/sharedviews.php';
// Convert (query,type) parameters from form to (query,tag)
global $queryparams, $tag, $query, $share;
if (isset($queryparams['query'])) {
unset($queryparams['query']);
$query = null;
}
if (isset($queryparams['tag'])) {
unset($queryparams['tag']);
$tag = null;
}
if (!empty($values['query'])) {
$querystring = $values['type'] == 'tagsonly' ? '?tag=' : '?query=';
$querystring .= urlencode($values['query']);
$goto .= $querystring;
if ($values['type'] == 'tagsonly') {
$queryparams['tag'] = $tag = $values['query'];
}
else {
$queryparams['query'] = $query = $values['query'];
}
}
redirect($goto);
$share = $queryparams['share'] = param_variable('share', array());
}
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