viewacl.php 3.9 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage form
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
28
29
30
31
32
33
 *
 */

/**
 * Provides an element to manage a view ACL
 *
 * @param array    $element The element to render
 * @param Pieform  $form    The form to render the element for
 * @return string           The HTML for the element
 */
34
function pieform_element_viewacl(Pieform $form, $element) {
35
    $smarty = smarty_core();
36
37
38
39
40
41
    $smarty->left_delimiter  = '{{';
    $smarty->right_delimiter = '}}';

    $value = $form->get_value($element);

    // Look for the presets and split them into two groups
Clare Lenihan's avatar
Clare Lenihan committed
42
43
    $presets = array();
    if (get_config('allowpublicviews') == '1') {
Richard Mansfield's avatar
Richard Mansfield committed
44
        $presets = array('public', 'loggedin', 'friends', 'token');
Clare Lenihan's avatar
Clare Lenihan committed
45
46
    }
    else {
Richard Mansfield's avatar
Richard Mansfield committed
47
        $presets = array('loggedin', 'friends');
Clare Lenihan's avatar
Clare Lenihan committed
48
    }
49
    if ($value) {
50
51
52
        foreach ($value as $key => &$item) {
            if (is_array($item)) {
                if (in_array($item['type'], $presets)) {
53
                    $item['name'] = get_string($item['type'], 'view');
54
55
56
57
58
                    $item['preset'] = true;
                }
                else {
                    $item['name'] = pieform_render_viewacl_getvaluebytype($item['type'], $item['id']);
                }
59
60
            }
            else {
61
                unset($value[$key]);
62
63
64
65
66
67
68
69
70
71
72
            }
        }
    }
    
    $potentialpresets = $presets;
    foreach ($potentialpresets as &$preset) {
        $preset = array(
            'type' => $preset,
            'id'   => $preset,
            'start' => null,
            'end'   => null,
73
            'name' => get_string($preset, 'view'),
74
75
76
77
78
79
            'preset' => true
        );
    }
    
    $smarty->assign('potentialpresets', json_encode($potentialpresets));
    $smarty->assign('accesslist', json_encode($value));
Richard Mansfield's avatar
Richard Mansfield committed
80
81
    $smarty->assign('viewid', $form->get_property('viewid'));
    $smarty->assign('formname', $form->get_property('name'));
82
83
84
85
86
87
88
89
90
    return $smarty->fetch('form/viewacl.tpl');
}

function pieform_render_viewacl_getvaluebytype($type, $id) {
    switch ($type) {
        case 'user':
            $user = get_record('usr', 'id', $id);
            return display_name($user);
            break;
91
92
        case 'group':
            return get_field('group', 'name', 'id', $id);
93
            break;
94
95
96
97
    }
    return "$type: $id";
}

Clare Lenihan's avatar
Clare Lenihan committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function pieform_element_viewacl_get_value(Pieform $form, $element) {
    global $USER;
    $values = null;
    $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
    if (isset($element['value'])) {
        $values = $element['value'];
    }
    else if (isset($global[$element['name']])) {
        $value = $global[$element['name']];
        $values = $value;
    }
    else if (isset($element['defaultvalue'])) {
        $values = $element['defaultvalue'];
    }
112
    if (get_config('allowpublicviews') != '1' && $values) {
Clare Lenihan's avatar
Clare Lenihan committed
113
        foreach ($values as $key => $value) {
Richard Mansfield's avatar
Richard Mansfield committed
114
            if ($value['type'] == 'public' || $value['type'] == 'token') {
Clare Lenihan's avatar
Clare Lenihan committed
115
116
117
118
119
120
121
                unset($values[$key]);
            }
        }
    }
    return $values;
}

122
?>