Commit 4f98e0aa authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add tag filter & search box to my views


Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent b59e15f0
......@@ -36,33 +36,19 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('libroot') . 'view.php');
require_once('pieforms/pieform.php');
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
$title = get_string('siteviews', 'admin');
define('TITLE', $title);
list($searchform, $data, $pagination) = View::views_by_owner(null, 'mahara');
$createviewform = pieform(create_view_form(null, 'mahara'));
$smarty = smarty();
$smarty->assign('PAGEHEADING', TITLE);
$data = View::get_myviews_data($limit, $offset, null, 'mahara');
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'admin/site/views.php',
'count' => $data->count,
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('view', 'view'),
'resultcounttextplural' => get_string('views', 'view')
));
$smarty->assign('views', $data->data);
$smarty->assign('institution', 'mahara');
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('searchform', $searchform);
$smarty->assign('createviewform', $createviewform);
$smarty->display('view/index.tpl');
?>
......@@ -2300,39 +2300,50 @@ class View {
return self::can_remove_viewtype($this->type);
}
public static function get_myviews_data($limit=5, $offset=0, $groupid=null, $institution=null) {
private static function _get_myviews_data($limit=5, $offset=0, $query=null, $tag=null, $groupid=null, $institution=null) {
global $USER;
$userid = $USER->get('id');
$owner = null;
$userid = (!$groupid && !$institution) ? $USER->get('id') : null;
$select = '
SELECT v.id,v.title,v.description,v.type';
$from = '
FROM {view} v';
$where = '
WHERE ' . self::owner_sql((object) array('owner' => $userid, 'group' => $groupid, 'institution' => $institution));
$sort = '
ORDER BY v.title, v.id';
$values = array();
if ($groupid) {
$count = count_records('view', 'group', $groupid);
$viewdata = get_records_sql_array('SELECT v.id,v.title,v.description,v.type
FROM {view} v
WHERE v.group = ?
'. (group_user_access($groupid) != 'admin' ? ' AND v.type != \'grouphomepage\'' : '') . '
ORDER BY v.title, v.id', array($groupid), $offset, $limit);
if ($tag) { // Filter by the tag
$from .= "
INNER JOIN {view_tag} vt ON (vt.view = v.id AND vt.tag = ?)";
$values[] = $tag;
}
else if ($institution) {
$count = count_records('view', 'institution', $institution);
$viewdata = get_records_sql_array('SELECT v.id,v.title,v.description,v.type
FROM {view} v
WHERE v.institution = ?
ORDER BY v.title, v.id', array($institution), $offset, $limit);
elseif ($query) { // Include matches on the title, description or tag
$from .= "
LEFT JOIN {view_tag} vt ON (vt.view = v.id AND vt.tag = ?)";
$like = db_ilike();
$where .= "
AND (v.title $like '%' || ? || '%' OR v.description $like '%' || ? || '%' OR vt.tag = ?)";
array_push($values, $query, $query, $query, $query);
}
else {
$count = count_records_select('view', 'owner = ?', array($userid));
$viewdata = get_records_sql_array('SELECT v.id,v.title,v.description,v.type,v.submittedtime,
g.id AS submitgroupid, g.name AS submitgroupname, h.wwwroot AS submithostwwwroot, h.name AS submithostname
FROM {view} v
if ($groupid && group_user_access($groupid) != 'admin') {
$where .= " AND v.type != 'grouphomepage'";
}
if ($userid) {
$select .= ',v.submittedtime,
g.id AS submitgroupid, g.name AS submitgroupname, h.wwwroot AS submithostwwwroot, h.name AS submithostname';
$from .= '
LEFT OUTER JOIN {group} g ON (v.submittedgroup = g.id AND g.deleted = 0)
LEFT OUTER JOIN {host} h ON (v.submittedhost = h.wwwroot)
WHERE v.owner = ?
ORDER BY v.type = \'portfolio\', v.type, v.title, v.id', array($userid), $offset, $limit);
$owner = $userid;
LEFT OUTER JOIN {host} h ON (v.submittedhost = h.wwwroot)';
$sort = '
ORDER BY v.type = \'portfolio\', v.type, v.title, v.id';
}
$count = count_records_sql('SELECT COUNT(v.id) ' . $from . $where, $values);
$viewdata = get_records_sql_array($select . $from . $where . $sort, $values, $offset, $limit);
$data = array();
if ($viewdata) {
for ($i = 0; $i < count($viewdata); $i++) {
......@@ -2340,7 +2351,6 @@ class View {
$data[$i]['id'] = $viewdata[$i]->id;
$data[$i]['type'] = $viewdata[$i]->type;
$data[$i]['title'] = $viewdata[$i]->title;
$data[$i]['owner'] = $owner;
$data[$i]['removable'] = self::can_remove_viewtype($viewdata[$i]->type);
$data[$i]['description'] = $viewdata[$i]->description;
if (!empty($viewdata[$i]->submitgroupid)) {
......@@ -2382,6 +2392,107 @@ class View {
);
}
public static function get_myviews_url($group=null, $institution=null, $query=null, $tag=null) {
$queryparams = array();
if (!empty($tag)) {
$queryparams[] = 'tag=' . urlencode($tag);
}
else if (!empty($query)) {
$queryparams[] = 'query=' . urlencode($query);
}
if ($group) {
$url = get_config('wwwroot') . 'view/groupviews.php';
$queryparams[] = 'group=' . $group;
}
else if ($institution) {
if ($institution == 'mahara') {
$url = get_config('wwwroot') . 'admin/site/views.php';
}
else {
$url = get_config('wwwroot') . 'view/institutionviews.php';
$queryparams[] = 'institution=' . $institution;
}
}
else {
$url = get_config('wwwroot') . 'view/';
}
if (!empty($queryparams)) {
$url .= '?' . join('&', $queryparams);
}
return $url;
}
public static function views_by_owner($group=null, $institution=null) {
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$query = param_variable('query', null);
$tag = param_variable('tag', null);
$searchoptions = array(
'titleanddescription' => get_string('titleanddescription', 'view'),
'tagsonly' => get_string('tagsonly', 'view'),
);
if (!empty($tag)) {
$searchtype = 'tagsonly';
$searchdefault = $tag;
$query = null;
}
else {
$searchtype = 'titleanddescription';
$searchdefault = $query;
}
$searchform = array(
'name' => 'searchviews',
'renderer' => 'oneline',
'elements' => array(
'query' => array(
'type' => 'text',
'title' => get_string('search') . ': ',
'defaultvalue' => $searchdefault,
),
'type' => array(
'type' => 'select',
'options' => $searchoptions,
'defaultvalue' => $searchtype,
),
'submit' => array(
'type' => 'submit',
'value' => get_string('search')
)
)
);
if ($group) {
$searchform['elements']['group'] = array('type' => 'hidden', 'name' => 'group', 'value' => $group);
}
else if ($institution) {
$searchform['elements']['institution'] = array('type' => 'hidden', 'name' => 'institution', 'value' => $institution);
}
$searchform = pieform($searchform);
$data = self::_get_myviews_data($limit, $offset, $query, $tag, $group, $institution);
$url = self::get_myviews_url($group, $institution, $query, $tag);
$pagination = build_pagination(array(
'url' => $url,
'count' => $data->count,
'limit' => $limit,
'offset' => $offset,
));
return array($searchform, $data, $pagination);
}
public function get_user_views($userid=null) {
if (is_null($userid)) {
global $USER;
......@@ -3699,6 +3810,21 @@ function createview_cancel_submit(Pieform $form, $values) {
redirect(get_config('wwwroot') . 'view/');
}
function searchviews_submit(Pieform $form, $values) {
$tag = $query = null;
if (!empty($values['query'])) {
if ($values['type'] == 'tagsonly') {
$tag = $values['query'];
}
else {
$query = $values['query'];
}
}
$group = isset($values['group']) ? $values['group'] : null;
$institution = isset($values['institution']) ? $values['institution'] : null;
redirect(View::get_myviews_url($group, $institution, $query, $tag));
}
function view_group_submission_form($viewid, $tutorgroupdata, $returnto=null) {
$options = array();
foreach ($tutorgroupdata as $group) {
......
......@@ -15,6 +15,8 @@
</div>
{if $institution} {$institutionselector|safe}{/if}
{$searchform|safe}
{if $views}
<table id="myviews" class="fullwidth listing">
<tbody>
......
......@@ -38,8 +38,6 @@ require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
require_once('pieforms/pieform.php');
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
define('GROUP', param_integer('group'));
$group = group_current_group();
if (!is_logged_in() && !$group->public) {
......@@ -54,11 +52,11 @@ $can_edit = group_user_can_edit_views($group->id);
// page, otherwise just show a list of the views owned by this group that
// are visible to the user.
if ($can_edit) {
$data = View::get_myviews_data($limit, $offset, $group->id);
$createviewform = pieform(create_view_form($group->id));
}
else {
if (!$can_edit) {
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
$data = View::view_search(null, null, (object) array('group' => $group->id), null, $limit, $offset);
// Add a copy view form for all templates in the list
foreach ($data->data as &$v) {
......@@ -66,24 +64,29 @@ else {
$v['copyform'] = pieform(create_view_form(null, null, $v['id']));
}
}
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'view/groupviews.php?group='.$group->id,
'count' => $data->count,
'limit' => $limit,
'offset' => $offset,
));
$smarty = smarty();
$smarty->assign('views', $data->data);
$smarty->assign('pagination', $pagination['html']);
$smarty->display('view/groupviews.tpl');
exit;
}
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'view/groupviews.php?group='.$group->id,
'count' => $data->count,
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('view', 'view'),
'resultcounttextplural' => get_string('views', 'view')
));
list($searchform, $data, $pagination) = View::views_by_owner($group->id);
$createviewform = pieform(create_view_form($group->id));
$smarty = smarty();
$smarty->assign('views', $data->data);
$smarty->assign('pagination', $pagination['html']);
if ($can_edit) {
$smarty->assign('createviewform', $createviewform);
$smarty->display('view/index.tpl');
} else {
$smarty->display('view/groupviews.tpl');
}
$smarty->assign('searchform', $searchform);
$smarty->assign('createviewform', $createviewform);
$smarty->display('view/index.tpl');
......@@ -35,24 +35,9 @@ define('SECTION_PAGE', 'index');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once(get_config('libroot') . 'view.php');
require_once('pieforms/pieform.php');
require_once('group.php');
define('TITLE', get_string('Views', 'view'));
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$data = View::get_myviews_data($limit, $offset);
$userid = $USER->get('id');
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'view/',
'count' => $data->count,
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('view', 'view'),
'resultcounttextplural' => get_string('views', 'view')
));
list($searchform, $data, $pagination) = View::views_by_owner();
$createviewform = pieform(create_view_form());
......@@ -60,7 +45,6 @@ $smarty = smarty();
$smarty->assign('views', $data->data);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('searchform', $searchform);
$smarty->assign('createviewform', $createviewform);
$smarty->display('view/index.tpl');
?>
......@@ -37,9 +37,6 @@ require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'institution.php');
require_once('pieforms/pieform.php');
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);
$institution = param_alpha('institution', false);
if ($institution == 'mahara') {
......@@ -59,16 +56,9 @@ if ($institution === false) {
exit;
}
$data = View::get_myviews_data($limit, $offset, null, $institution);
list($searchform, $data, $pagination) = View::views_by_owner(null, $institution);
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'view/institutionviews.php?institution='.$institution,
'count' => $data->count,
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('view', 'view'),
'resultcounttextplural' => get_string('views', 'view')
));
$createviewform = pieform(create_view_form(null, $institution));
$smarty = smarty();
$smarty->assign('PAGEHEADING', TITLE);
......@@ -77,8 +67,7 @@ $smarty->assign('INLINEJAVASCRIPT', $s['institutionselectorjs']);
$smarty->assign('views', $data->data);
$smarty->assign('institution', $institution);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('createviewform', pieform(create_view_form(null, $institution)));
$smarty->assign('searchform', $searchform);
$smarty->assign('createviewform', $createviewform);
$smarty->display('view/index.tpl');
?>
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