groupviews.json.php 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype/groupviews
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

/**
 * returns group views in a given group id
 */

define('INTERNAL', 1);
define('JSON', 1);

require(dirname(dirname(dirname(__FILE__))) . '/init.php');

safe_require('blocktype', 'groupviews');
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');

$offset = param_integer('offset', 0);
$groupid = param_integer('group');
27
$editing = param_boolean('editing', false);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

$group_homepage_view = group_get_homepage_view($groupid);
$bi = group_get_homepage_view_groupview_block($groupid);

if (!can_view_view($group_homepage_view)) {
    json_reply(true, get_string('accessdenied', 'error'));
}

$configdata = $bi->get('configdata');
if (!isset($configdata['showgroupviews'])) {
    $configdata['showgroupviews'] = 1;
}
$limit = isset($configdata['count']) ? intval($configdata['count']) : 5;
$limit = ($limit > 0) ? $limit : 5;

43 44 45 46 47 48 49 50 51 52 53 54 55 56
// Sortorder: Group homepage should be first, then sort by sortorder
$sort = array(
        array(
                'column' => "type='grouphomepage'",
                'desc' => true
        )
);
// Find out what order to sort them by (default is titles)
if (!isset($configdata['sortgroupviewsby']) || $configdata['sortgroupviewsby'] == PluginBlocktypeGroupViews::SORTBY_TITLE) {
    $sort[] = array('column' => 'title');
}
else {
    $sort[] = array('column' => 'mtime', 'desc' => true);
}
57 58 59
$groupviews = (array)View::view_search(null, null, (object) array('group' => $groupid),
                                      null, $limit, $offset, true, $sort, null,
                                      false, null, null, null, null, true);
60
foreach ($groupviews['data'] as &$view) {
61 62 63
    if (empty($view['displaytitle'])) {
        $view['displaytitle'] = $view['title']; // add for collections
    }
64
    if (isset($view['template']) && $view['template']) {
65 66
        $collid = !empty($view['collid']) ? $view['collid'] : null;
        $view['form'] = pieform(create_view_form(null, null, $view['id'], $collid, $collid));
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    }
}
if (!empty($configdata['showgroupviews']) && isset($groupviews)) {
    $baseurl = $group_homepage_view->get_url();
    $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'group=' . $groupid;
    $pagination = array(
        'baseurl'    => $baseurl,
        'id'         => 'groupviews_pagination',
        'datatable'  => 'groupviewlist',
        'jsonscript' => 'blocktype/groupviews/groupviews.json.php',
        'resultcounttextsingular' => get_string('view', 'view'),
        'resultcounttextplural'   => get_string('views', 'view'),
    );
    PluginBlocktypeGroupViews::render_items($groupviews, 'blocktype:groupviews:groupviewssection.tpl', $configdata, $pagination);
}

json_reply(false, array('data' => $groupviews));