viewacl.php 6.68 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
 *
 * @package    mahara
 * @subpackage form
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
31
32
33
34
 *
 */

/**
 * 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
 */
35
function pieform_element_viewacl(Pieform $form, $element) {
36
    global $USER, $SESSION;
37
38
39

    $strlen = function_exists('mb_strlen') ? 'mb_strlen' : 'strlen';

40
    $smarty = smarty_core();
41
42
43
44
45
46
    $smarty->left_delimiter  = '{{';
    $smarty->right_delimiter = '}}';

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

    // Look for the presets and split them into two groups
47
48
49
50
51
52
53
    $public = false;
    if (get_config('allowpublicviews') && $USER->institution_allows_public_views()) {
        $public = true;
    }
    else if (get_config('allowpublicprofiles') && $element['viewtype'] == 'profile') {
        $public = true;
    }
Clare Lenihan's avatar
Clare Lenihan committed
54
    $presets = array();
55
56
57
    $loggedinindex = 0;
    if ($public) {
        $presets[] = 'public';
58
        $loggedinindex = 1;
Clare Lenihan's avatar
Clare Lenihan committed
59
    }
60
61
62
    $presets[] = 'loggedin';
    if ($form->get_property('userview')) {
        $presets[] = 'friends';
Clare Lenihan's avatar
Clare Lenihan committed
63
    }
64

65
    $accesslist = array();
66
    if ($value) {
67
        foreach ($value as $item) {
68
            if (is_array($item)) {
69
70
71
                if ($item['accesstype'] == 'public') {
                    $item['publicallowed'] = (int)$public;
                }
72
                if (in_array($item['type'], $presets)) {
73
                    $item['name'] = get_string($item['type'], 'view');
74
75
76
77
78
                    $item['preset'] = true;
                }
                else {
                    $item['name'] = pieform_render_viewacl_getvaluebytype($item['type'], $item['id']);
                }
79
                if ($strlen($item['name']) > 30) {
80
81
                    $item['shortname'] = str_shorten_text($item['name'], 30, true);
                }
82
                // only show access that is still current. Expired access will be deleted if the form is saved
83
84
                if (empty($item['stopdate']) || (time() <= strtotime($item['stopdate']))) {
                    $accesslist[] = $item;
85
                }
86
87
88
89
            }
        }
    }
    
90
91
92
93
94
95
96
97
98
99
100
101
    $myinstitutions = array();
    foreach ($USER->get('institutions') as $i) {
        $myinstitutions[] = array(
            'type' => 'institution',
            'id'   => $i->institution,
            'start' => null,
            'end'   => null,
            'name' => hsc($i->displayname),
            'preset' => false
        );
    }

102
103
104
105
106
107
108
    $potentialpresets = $presets;
    foreach ($potentialpresets as &$preset) {
        $preset = array(
            'type' => $preset,
            'id'   => $preset,
            'start' => null,
            'end'   => null,
109
            'name' => get_string($preset, 'view'),
110
111
112
            'preset' => true
        );
    }
113

Richard Mansfield's avatar
Richard Mansfield committed
114
115
116
117
118
119
120
121
    $allgroups = array(
        'type'   => 'allgroups',
        'id'     => 'allgroups',
        'start'  => null,
        'end'    => null,
        'name'   => get_string('allmygroups', 'group'),
        'preset' => true
    );
122
123
    $mygroups = array();
    foreach (group_get_user_groups($USER->get('id')) as $g) {
124
        $group = array(
125
126
127
128
129
130
131
            'type' => 'group',
            'id'   => $g->id,
            'start' => null,
            'end'   => null,
            'name' => $g->name,
            'preset' => false
        );
132
        if ($strlen($g->name) > 30) {
133
            $group['shortname'] = str_shorten_text($g->name, 30, true);
134
        }
135
        $mygroups[] = $group;
136
    }
137
    
138
139
    $faves = array();
    foreach (get_user_favorites($USER->get('id')) as $u) {
140
        $fave = array(
141
142
143
144
145
146
147
            'type'   => 'user',
            'id'     => $u->id,
            'start'  => null,
            'end'    => null,
            'name'   => $u->name,
            'preset' => false
        );
148
        if ($strlen($u->name) > 30) {
149
            $fave['shortname'] = str_shorten_text($u->name, 30, true);
150
        }
151
        $faves[] = $fave;
152
153
    }

154
    $smarty->assign('viewtype', $element['viewtype']);
155
    $smarty->assign('potentialpresets', json_encode($potentialpresets));
156
    $smarty->assign('loggedinindex', $loggedinindex);
157
    $smarty->assign('accesslist', json_encode($accesslist));
Richard Mansfield's avatar
Richard Mansfield committed
158
159
    $smarty->assign('viewid', $form->get_property('viewid'));
    $smarty->assign('formname', $form->get_property('name'));
160
    $smarty->assign('myinstitutions', json_encode($myinstitutions));
161
    $smarty->assign('allowcomments', $element['allowcomments']);
Richard Mansfield's avatar
Richard Mansfield committed
162
    $smarty->assign('allgroups', json_encode($allgroups));
163
    $smarty->assign('mygroups', json_encode($mygroups));
164
    $smarty->assign('faves', json_encode($faves));
165
166
167
168
169
    return $smarty->fetch('form/viewacl.tpl');
}

function pieform_render_viewacl_getvaluebytype($type, $id) {
    switch ($type) {
170
171
172
        case 'loggedin':
            return get_string('loggedin', 'view');
            break;
173
174
175
176
        case 'user':
            $user = get_record('usr', 'id', $id);
            return display_name($user);
            break;
177
178
        case 'group':
            return get_field('group', 'name', 'id', $id);
179
            break;
180
181
182
        case 'institution':
            return get_string('institution', 'admin') . ': ' . get_field('institution', 'displayname', 'name', $id);
            break;
183
    }
184
    return sprintf("%s: %s", ucfirst($type), $id);
185
186
}

Clare Lenihan's avatar
Clare Lenihan committed
187
188
189
190
191
192
193
194
195
196
197
198
199
function pieform_element_viewacl_get_value(Pieform $form, $element) {
    $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'];
    }
200
    return $values;
Clare Lenihan's avatar
Clare Lenihan committed
201
}