views.json.php 6.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/**
 *
 * @package    mahara
 * @subpackage core
 * @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.
 *
 */

define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require_once('collection.php');
17
require_once('view.php');
18 19 20 21 22 23 24 25 26 27 28 29 30 31

$id = param_integer('id');
$direction = param_variable('direction','');

$collection = new Collection($id);

if (!$USER->can_edit_collection($collection)) {
    json_reply('local', get_string('accessdenied', 'error'));
}

$owner = $collection->get('owner');
$groupid = $collection->get('group');
$institutionname = $collection->get('institution');
$views = $collection->views();
32
$message = $messagestatus = null;
33 34 35 36
if (!empty($direction)) {
    parse_str($direction, $direction_array);
    $viewids = array();
    // get all the id's of the existing views attached to collection - if any
37
    $firstviewaccess = array();
38 39 40 41
    if (!empty($views['views'])) {
        foreach ($views['views'] as $v) {
            $viewids[] = $v->view;
        }
42 43
        $firstview = new View($viewids[0]);
        $firstviewaccess = $firstview->get_access();
44 45 46 47 48 49 50
    }
    // now check if there are any new views to add to the collection
    // items dragged from the 'add to collection' list. (currently handles only one at a time)
    $diff = array_diff($direction_array['row'], $viewids);
    if (!empty($diff)) {
        // turn it into an array understood by $collection->add_views()
        $addviews = array();
51
        $newviewid = false;
52
        foreach ($diff as $v) {
53
            $newviewid = $v;
54 55 56 57 58
            // We need to check that the id's are allowed to be added to the collection
            // by checking if the user can edit the view.
            $view = new View($v);
            $viewowner = $view->get('owner');
            $viewgroup = $view->get('group');
59
            $viewaccess = $view->get_access();
60 61 62 63 64 65 66 67 68 69 70 71
            $viewinstitution = $view->get('institution');
            if ((!$USER->can_edit_view($view)) ||
                (!empty($viewowner) && $viewowner != $collection->get('owner')) ||
                (!empty($viewgroup) && $viewgroup != $collection->get('group')) ||
                (!empty($viewinstitution) && $viewinstitution != $collection->get('institution'))
               ) {
                continue;
            }
            $addviews['view_' . $v] = true;
        }
        if (!empty($addviews)) {
            $collection->add_views($addviews);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
            // New view permissions
            $collectiondifferent = false;
            $different = false;
            $differentarray = array();
            if (!empty($firstviewaccess) && empty($viewaccess)) {
                // adding the collection access rules to the added pages
                $different = true;
                $differentarray[] = $newviewid;
            }
            else if (!empty($firstviewaccess)) {
                $merged = combine_arrays($firstviewaccess, $viewaccess);
                if ($merged != $firstviewaccess) {
                    // adding the new access rules to both collection and added pages
                    $different = true;
                    $collectiondifferent = true;
                    $differentarray[] = $newviewid;
                }
                else if ($merged != $viewaccess) {
                    // adding the collection access rules to the added pages
                    $different = true;
                    $differentarray[] = $newviewid;
                }
            }
            else if (empty($firstviewaccess) && !empty($viewaccess)) {
                // adding the page's access rules to the collection pages
                $different = true;
                $collectiondifferent = true;
            }
            if ($collectiondifferent) {
                $differentarray = array_merge($differentarray, $viewids);
            }
103
            if ($different && !empty($differentarray)) {
104 105 106 107 108 109
                $alertstr = get_string('viewsaddedaccesschanged', 'collection');
                foreach ($differentarray as $viewid) {
                    $changedview = new View($viewid);
                    $alertstr .= " " . json_encode($changedview->get('title')) . ",";
                }
                $alertstr = substr($alertstr, 0, -1) . '.';
110 111 112 113 114 115
                $message = get_string('viewsaddedtocollection1', 'collection', 1) . ' ' . $alertstr;
                $messagestatus = 'warning';
            }
            else {
                $message = get_string('viewsaddedtocollection1', 'collection', 1);
                $messagestatus = 'ok';
116
            }
117 118 119 120 121 122 123 124 125 126 127 128
        }
    }
    $collection->set_viewdisplayorder(null, $direction_array['row']);
}

// We need to call the collection again to get the updated view list
$collection = new Collection($id);
$views = $collection->get('views');

if ($views) {
    foreach ($views['views'] as &$v) {
        $v->remove = pieform(array(
129
            'renderer' => 'div',
Liam Sharpe's avatar
Liam Sharpe committed
130
            'class' => 'form-as-button pull-right',
131 132 133 134 135 136 137 138
            'name' => 'removeview_' . $v->view,
            'successcallback' => 'removeview_submit',
            'elements' => array(
                'view' => array(
                    'type' => 'hidden',
                    'value' => $v->view,
                ),
                'submit' => array(
Liam Sharpe's avatar
Liam Sharpe committed
139 140
                    'type' => 'button',
                    'usebuttontag' => true,
141
                    'class' => 'btn-link btn-sm',
142
                    'confirm' => get_string('viewconfirmremove', 'collection'),
143
                    'value' => '<span class="icon icon-times icon-lg text-danger" role="presentation" aria-hidden="true"><span class="sr-only">' . get_string('remove') . '</span></span>'                ),
144 145 146 147 148 149 150 151 152 153 154 155
            ),
        ));
    }
}


$smarty = smarty_core();
$smarty->assign_by_ref('views', $views);
$smarty->assign('displayurl', get_config('wwwroot') . 'collection/views.php?id=' . $id);
$html = $smarty->fetch('collection/views.json.tpl');

json_reply(false, array(
156
    'message' => $message,
157
    'messagestatus' => $messagestatus,
158 159
    'html' => $html,
));