access.php 15.4 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 core
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
35
 *
 */

define('INTERNAL', 1);
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'view');
define('SECTION_PAGE', 'editaccess');

require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require_once('pieforms/pieform/elements/calendar.php');
36
37
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
38
39

$view = new View(param_integer('id'));
40
$group = $view->get('group');
41
$institution = $view->get('institution');
42
View::set_nav($group, $institution);
43
44
$new = param_boolean('new');

45
define('TITLE', $view->get('title') . ': ' . get_string('editaccess', 'view'));
46

47
if (!$USER->can_edit_view($view)) {
48
49
50
    throw new AccessDeniedException();
}

51
$js = '';
52
if (!count_records('block_instance', 'view', $view->get('id'))) {
53
    $confirmmessage = get_string('reallyaddaccesstoemptyview', 'view');
54
    $js .= <<<EOF
55
56
57
58
59
60
61
62
63
64
65
addLoadEvent(function() {
    connect('editaccess_submit', 'onclick', function () {
        var accesslistrows = getElementsByTagAndClassName('tr', null, 'accesslistitems');
        if (accesslistrows.length > 0 && !confirm('{$confirmmessage}')) {
            replaceChildNodes('accesslistitems', []);
        }
    });
});
EOF;
}

66
// @todo need a rule here that prevents stopdate being smaller than startdate
67
68
$form = array(
    'name' => 'editaccess',
69
    'renderer' => 'div',
70
71
    'plugintype' => 'core',
    'pluginname' => 'view',
Richard Mansfield's avatar
Richard Mansfield committed
72
    'viewid' => $view->get('id'),
73
74
75
76
77
78
79
80
81
    'elements' => array(
        'id' => array(
            'type' => 'hidden',
            'value' => $view->get('id'),
        ),
        'new' => array(
            'type' => 'hidden',
            'value' => $new,
        ),
82
83
        'template' => array(
            'type'         => 'checkbox',
84
            'title'        => get_string('allowcopying', 'view'),
85
86
87
            'description'  => get_string('templatedescription', 'view'),
            'defaultvalue' => $view->get('template'),
        ),
88
89
90
91
92
93
94
95
96
    )
);

if ($institution) {
    if ($institution == 'mahara') {
        $form['elements']['copynewuser'] = array(
            'type'         => 'checkbox',
            'title'        => get_string('copyfornewusers', 'view'),
            'description'  => get_string('copyfornewusersdescription', 'view'),
97
            'defaultvalue' => $view->get('template') && $view->get('copynewuser'),
98
99
100
101
102
103
104
105
106
        );
        $form['elements']['copyfornewgroups'] = array(
            'type'         => 'html',
            'value'        => '<label>' . get_string('copyfornewgroups', 'view') . '</label>',
        );
        $form['elements']['copyfornewgroupsdescription'] = array(
            'type'         => 'html',
            'value'        => '<div class="description">' . get_string('copyfornewgroupsdescription', 'view') . '</div>',
        );
107
        $copyoptions = array('copynewuser', 'copyfornewgroups', 'copyfornewgroupsdescription');
108
        $needsaccess = array('copynewuser');
109
110
111
        $createfor = $view->get_autocreate_grouptypes();
        foreach (group_get_grouptypes() as $grouptype) {
            safe_require('grouptype', $grouptype);
112
            $jointypestrings = array();
113
            foreach (call_static_method('GroupType' . $grouptype, 'allowed_join_types', true) as $jointype) {
114
115
                $jointypestrings[] = get_string('membershiptype.'.$jointype, 'group');
            }
116
117
            $form['elements']['copyfornewgroups_'.$grouptype] = array(
                'type'         => 'checkbox',
118
                'title'        => get_string('name', 'grouptype.' . $grouptype) . ' (' . join(', ', $jointypestrings) . ')',
119
                'defaultvalue' => $view->get('template') && in_array($grouptype, $createfor),
120
            );
121
            $copyoptions[] = 'copyfornewgroups_'.$grouptype;
122
            $needsaccess[] = 'copyfornewgroups_'.$grouptype;
123
124
125
126
127
128
129
        }
    }
    else {
        $form['elements']['copynewuser'] = array(
            'type'         => 'checkbox',
            'title'        => get_string('copyfornewmembers', 'view'),
            'description'  => get_string('copyfornewmembersdescription', 'view', get_field('institution', 'displayname', 'name', $institution)),
130
            'defaultvalue' => $view->get('template') && $view->get('copynewuser'),
131
        );
132
        $copyoptions = array('copynewuser');
133
        $needsaccess = array('copynewuser');
134
    }
135
136
137
    $copyoptionstr = json_encode($copyoptions);
    $needsaccessstr = json_encode($needsaccess);
    $js .= <<<EOF
138
function update_copy_options() {
139
140
    if ($('editaccess_template').checked) {
        forEach({$copyoptionstr}, function (id) {
141
            removeElementClass($('editaccess_'+id+'_container'), 'hidden');
142
143
144
145
        });
    }
    else {
        forEach({$copyoptionstr}, function (id) {
146
            addElementClass($('editaccess_'+id+'_container'), 'hidden');
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        });
        forEach({$needsaccessstr}, function (id) {
            $('editaccess_'+id).checked = false;
        });
        update_loggedin_access();
    }
}
function update_loggedin_access() {
    if (some({$needsaccessstr}, function (id) { return $('editaccess_'+id).checked; })) {
        ensure_loggedin_access();
    }
    else {
        relax_loggedin_access();
    }
161
162
163
164
}
addLoadEvent(function() {
    update_copy_options();
    connect('editaccess_template', 'onchange', update_copy_options);
165
166
167
    forEach({$needsaccessstr}, function (id) {
        connect('editaccess_'+id, 'onchange', update_loggedin_access);
    });
168
169
});
EOF;
170
171
} else {
    $js .= "function update_loggedin_access() {}\n";
172
173
174
175
}

$form['elements']['accesslist'] = array(
    'type'         => 'viewacl',
176
    'defaultvalue' => isset($view) ? $view->get_access(get_string('strftimedatetimeshort')) : null
177
178
179
180
181
182
183
184
185
);

$form['elements']['overrides'] = array(
    'type' => 'fieldset',
    'legend' => get_string('overridingstartstopdate', 'view'),
    'elements' => array(
        'description' => array(
            'type' => 'html',
            'value' => get_string('overridingstartstopdatesdescription', 'view'),
186
        ),
187
188
189
        'startdate'        => array(
            'type'         => 'calendar',
            'title'        => get_string('startdate','view'),
190
            'description'  => get_string('datetimeformatguide'),
191
192
193
            'defaultvalue' => isset($view) ? strtotime($view->get('startdate')) : null,
            'caloptions'   => array(
                'showsTime'      => true,
194
                'ifFormat'       => get_string('strftimedatetimeshort'),
195
            ),
196
            'help'         => true,
197
        ),
198
199
200
        'stopdate'  => array(
            'type'         => 'calendar',
            'title'        => get_string('stopdate','view'),
201
            'description'  => get_string('datetimeformatguide'),
202
203
204
            'defaultvalue' => isset($view) ? strtotime($view->get('stopdate')) : null,
            'caloptions'   => array(
                'showsTime'      => true,
205
                'ifFormat'       => get_string('strftimedatetimeshort'),
206
207
            ),
            'help'         => true,
208
        ),
209
210
211
212
213
214
215
216
217
    ),
);

$form['elements']['submit'] = array(
    'type'  => !empty($new) ? 'cancelbackcreate' : 'submitcancel',
    'value' => !empty($new) 
        ? array(get_string('cancel'), get_string('back','view'), get_string('save'))
        : array(get_string('save'), get_string('cancel')),
    'confirm' => !empty($new) ? array(get_string('confirmcancelcreatingview', 'view'), null, null) : null,
218
219
);

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
if (!function_exists('strptime')) {
    // Windows doesn't have this, use an inferior version
    function strptime($date, $format) {
        $result = array(
            'tm_sec'  => 0, 'tm_min'  => 0, 'tm_hour' => 0, 'tm_mday'  => 1,
            'tm_mon'  => 0, 'tm_year' => 0, 'tm_wday' => 0, 'tm_yday'  => 0,
        );
        $formats = array(
            '%Y' => array('len' => 4, 'key' => 'tm_year'),
            '%m' => array('len' => 2, 'key' => 'tm_mon'),
            '%d' => array('len' => 2, 'key' => 'tm_mday'),
            '%H' => array('len' => 2, 'key' => 'tm_hour'),
            '%M' => array('len' => 2, 'key' => 'tm_min'),
        );
        while ($format) {
            $start = substr($format, 0, 2);
            switch ($start) {
            case '%Y': case '%m': case '%d': case '%H': case '%M':
                $result[$formats[$start]['key']] = substr($date, 0, $formats[$start]['len']);
                $format = substr($format, 2);
                $date = substr($date, $formats[$start]['len']);
            default:
                $format = substr($format, 1);
                $date = substr($date, 1);
            }
        }
        if ($result['tm_mon'] < 1 || $result['tm_mon'] > 12
            || $result['tm_mday'] < 1 || $result['tm_mday'] > 31
            || $result['tm_hour'] < 0 || $result['tm_hour'] > 23
            || $result['tm_min'] < 0 || $result['tm_min'] > 59) {
            return false;
        }
        return $result;
    }
}

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Converts parsed time array to unix timestamp.
 * @param array // date parsed using strptime()
 * @return int  // Unix timestamp
 */
function ptimetotime($ptime) {
    return mktime(
        $ptime['tm_hour'],
        $ptime['tm_min'],
        $ptime['tm_sec'],
        1,
        $ptime['tm_yday'] + 1,
        $ptime['tm_year'] + 1900
    );
}

272
function editaccess_validate(Pieform $form, $values) {
273
    global $SESSION, $institution, $group;
274
    if ($institution && $values['copynewuser'] && !$values['template']) {
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        $form->set_error('copynewuser', get_string('viewscopiedfornewusersmustbecopyable', 'view'));
    }
    $createforgroup = false;
    if ($institution == 'mahara') {
        foreach (group_get_grouptypes() as $grouptype) {
            if ($values['copyfornewgroups_'.$grouptype]) {
                $createforgroup = true;
                break;
            }
        }
        if ($createforgroup && !$values['template']) {
            $form->set_error('copyfornewgroups', get_string('viewscopiedfornewgroupsmustbecopyable', 'view'));
        }
    }
289
290
291
    if ($values['startdate'] && $values['stopdate'] && $values['startdate'] > $values['stopdate']) {
        $form->set_error('startdate', get_string('startdatemustbebeforestopdate', 'view'));
    }
292
293
    $loggedinaccess = false;
    if ($values['accesslist']) {
294
        $dateformat = get_string('strftimedatetimeshort');
295
        foreach ($values['accesslist'] as &$item) {
296
            if (empty($item['startdate'])) {
297
298
                $item['startdate'] = null;
            }
299
            else if (!$item['startdate'] = strptime($item['startdate'], $dateformat)) {
300
301
                $SESSION->add_error_msg(get_string('unrecogniseddateformat', 'view'));
                $form->set_error('accesslist', '');
302
303
                break;
            }
304
            if (empty($item['stopdate'])) {
305
306
                $item['stopdate'] = null;
            }
307
            else if (!$item['stopdate'] = strptime($item['stopdate'], $dateformat)) {
308
309
                $SESSION->add_error_msg(get_string('unrecogniseddateformat', 'view'));
                $form->set_error('accesslist', '');
310
311
                break;
            }
312
313
314
            if ($item['type'] == 'loggedin' && !$item['startdate'] && !$item['stopdate']) {
                $loggedinaccess = true;
            }
315
316
317
318
319
320
            $now = strptime(date('Y/m/d H:i'), $dateformat);
            if ($item['stopdate'] && ptimetotime($now) > ptimetotime($item['stopdate'])) {
                $SESSION->add_error_msg(get_string('stopdatecannotbeinpast', 'view'));
                $form->set_error('accesslist', '');
                break;
            }
321
            if ($item['startdate'] && $item['stopdate'] && ptimetotime($item['startdate']) > ptimetotime($item['stopdate'])) {
322
323
                $SESSION->add_error_msg(get_string('startdatemustbebeforestopdate', 'view'));
                $form->set_error('accesslist', '');
324
                break;
325
            }
326
327
        }
    }
328

329
    // Must have logged in user access for copy new user/group settings.
330
    if (($createforgroup || ($institution && $values['copynewuser'])) && !$loggedinaccess) {
331
332
        $SESSION->add_error_msg(get_string('copynewusergroupneedsloggedinaccess', 'view'));
        $form->set_error('accesslist', '');
333
    }
334
335
}

336
function editaccess_cancel_submit() {
337
338
339
340
341
342
343
344
    global $view, $new, $group, $institution;
    if ($new) {
        $view->delete();
    }
    if ($group) {
        redirect('/view/groupviews.php?group='.$group);
    }
    if ($institution) {
345
        redirect('/view/institutionviews.php?institution='.$institution);
346
    }
347
    redirect('/view');
348
349
350
351
}


function editaccess_submit(Pieform $form, $values) {
352
    global $SESSION, $view, $new, $group, $institution;
353
354
355
356
357

    if (param_boolean('back')) {
        redirect('/view/blocks.php?id=' . $view->get('id') . '&new=' . $new);
    }

358
    if ($values['accesslist']) {
359
        $dateformat = get_string('strftimedatetimeshort');
360
        foreach ($values['accesslist'] as &$item) {
361
            if (!empty($item['startdate'])) {
362
                $item['startdate'] = ptimetotime(strptime($item['startdate'], $dateformat));
363
            }
364
            if (!empty($item['stopdate'])) {
365
                $item['stopdate'] = ptimetotime(strptime($item['stopdate'], $dateformat));
366
367
368
            }
        }
    }
369
    $view->set_access($values['accesslist']);
370
371
372

    $view->set('startdate', $values['startdate']);
    $view->set('stopdate', $values['stopdate']);
373
374
    $istemplate = (int) $values['template'];
    $view->set('template', $istemplate);
375
    if (isset($values['copynewuser'])) {
376
        $view->set('copynewuser', (int) ($istemplate && $values['copynewuser']));
377
    }
378
379
380
    if ($institution == 'mahara') {
        $createfor = array();
        foreach (group_get_grouptypes() as $grouptype) {
381
            if ($istemplate && $values['copyfornewgroups_'.$grouptype]) {
382
383
384
385
386
387
                $createfor[] = $grouptype;
            }
        }
        $view->set('copynewgroups', $createfor);
    }

388
389
    $view->commit();

390
391
392
393
394
395
396
    if ($values['new']) {
        $str = get_string('viewcreatedsuccessfully', 'view');
    }
    else {
        $str = get_string('viewaccesseditedsuccessfully', 'view');
    }
    $SESSION->add_ok_msg($str);
397
398
399
    if ($group) {
        redirect('/view/groupviews.php?group='.$group);
    }
400
    if ($institution) {
401
        redirect('/view/institutionviews.php?institution='.$institution);
402
    }
403
    redirect('/view/');
404

405
406
}

407
408
$form = pieform($form);

409
$smarty = smarty(array('tablerenderer'), array(), array('mahara' => array('From', 'To', 'datetimeformatguide')), array('sidebars' => false));
410
$smarty->assign('INLINEJAVASCRIPT', $js);
411
$smarty->assign('PAGEHEADING', hsc(TITLE));
412
$smarty->assign('form', $form);
413
414
415
$smarty->display('view/access.tpl');

?>