edit.php 17.3 KB
Newer Older
1
2
<?php
/**
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
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
 *
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
 *
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 admin
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
 *
 */

define('INTERNAL', 1);
28
define('INSTITUTIONALADMIN', 1);
29
define('MENUITEM', 'configusers/usersearch');
30
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
31
define('TITLE', get_string('accountsettings', 'admin'));
32
33
34
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
require_once('pieforms/pieform.php');
35
require_once('activity.php');
36
37

$id = param_integer('id');
38
39
$user = new User;
$user->find_by_id($id);
40

41
42
if (!$USER->is_admin_for_user($user)) {
    redirect(get_config('wwwroot').'user/view.php?id='.$id);
43
}
44
45
46


// Site-wide account settings
47
$currentdate = getdate();
Richard Mansfield's avatar
Richard Mansfield committed
48
$elements = array();
49
50
51
52
53
54
55
56
$elements['id'] = array(
    'type'    => 'hidden',
    'rules'   => array('integer' => true),
    'value'   => $id,
);
$elements['password'] = array(
    'type'         => 'text',
    'title'        => get_string('resetpassword','admin'),
57
    'description'  => get_string('resetpassworddescription','admin'),
58
59
60
61
);
$elements['passwordchange'] = array(
    'type'         => 'checkbox',
    'title'        => get_string('forcepasswordchange','admin'),
62
    'description'  => get_string('forcepasswordchangedescription','admin'),
63
64
    'defaultvalue' => $user->passwordchange,
);
65
66
67
68
69
if ($USER->get('admin')) {
    $elements['staff'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('sitestaff','admin'),
        'defaultvalue' => $user->staff,
70
        'help'         => true,
71
72
73
74
75
    );
    $elements['admin'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('siteadmin','admin'),
        'defaultvalue' => $user->admin,
76
        'help'         => true,
77
78
    );
}
79
80
81
82
83
84
85
86
$elements['expiry'] = array(
    'type'         => 'date',
    'title'        => get_string('accountexpiry', 'admin'),
    'description'  => get_string('accountexpirydescription', 'admin'),
    'minyear'      => $currentdate['year'] - 2,
    'maxyear'      => $currentdate['year'] + 20,
    'defaultvalue' => $user->expiry
);
87
$elements['quota'] = array(
88
    'type'         => 'bytes',
89
    'title'        => get_string('filequota','admin'),
90
    'description'  => get_string('filequotadescription','admin'),
91
    'rules'        => array('integer' => true),
92
    'defaultvalue' => $user->quota,
93
);
94
95
96
97
98

$authinstances = auth_get_auth_instances();
if (count($authinstances) > 1) {
    $options = array();

99
100
101
102
103
104
105
106
    // NOTE: This is a little broken at the moment. The "username in the remote 
    // system" setting is only actively used by the XMLRPC authentication 
    // plugin, and thus only makes sense when the user is authenticating in 
    // this manner.
    //
    // We hope to one day make it possible for users to get into accounts via 
    // multiple methods, at which time we can tie the username-in-remote-system 
    // setting to the XMLRPC plugin only, making the UI a bit more consistent
107
    $external = false;
108
    foreach ($authinstances as $authinstance) {
Richard Mansfield's avatar
Richard Mansfield committed
109
        if ($USER->can_edit_institution($authinstance->name)) {
110
            $options[$authinstance->id] = $authinstance->instancename . ' (' . $authinstance->displayname . ')';
111
112
113
            if ($authinstance->authname != 'internal') {
                $external = true;
            }
114
        }
115
116
    }

117
118
    if (isset($options[$user->authinstance])) {
        $elements['authinstance'] = array(
119
120
            'type'         => 'select',
            'title'        => get_string('authenticatedby', 'admin'),
121
            'description'  => get_string('authenticatedbydescription', 'admin'),
122
            'options'      => $options,
123
            'defaultvalue' => $user->authinstance,
124
            'help'         => true,
125
        );
126
127
128
        if ($external) {
            $un = get_field('auth_remote_user', 'remoteusername', 'authinstance', $user->authinstance, 'localusr', $user->id);
            $elements['remoteusername'] = array(
129
130
131
                'type'         => 'text',
                'title'        => get_string('remoteusername', 'admin'),
                'description'  => get_string('remoteusernamedescription', 'admin'),
132
133
134
                'defaultvalue' => $un ? $un : $user->username,
            );
        }
135
    }
136

137
138
}

139
140
141
142
143
$elements['submit'] = array(
    'type'  => 'submit',
    'value' => get_string('savechanges','admin'),
);

Richard Mansfield's avatar
Richard Mansfield committed
144
145
$siteform = pieform(array(
    'name'       => 'edituser_site',
146
147
148
149
150
151
152
    'renderer'   => 'table',
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'elements'   => $elements,
));


Richard Mansfield's avatar
Richard Mansfield committed
153
function edituser_site_submit(Pieform $form, $values) {
154
155
156
157
158
159
    if (!$user = get_record('usr', 'id', $values['id'])) {
        return false;
    }

    if (isset($values['password']) && $values['password'] !== '') {
        $user->password = $values['password'];
160
        $user->salt = '';
161
162
    }
    $user->passwordchange = (int) ($values['passwordchange'] == 'on');
163
    $user->quota = $values['quota'];
164
    $user->expiry = db_format_timestamp($values['expiry']);
165
166
167
168
169

    global $USER;
    if ($USER->get('admin')) {  // Not editable by institutional admins
        $user->staff = (int) ($values['staff'] == 'on');
        $user->admin = (int) ($values['admin'] == 'on');
170
171
172
        if ($user->admin) {
            activity_add_admin_defaults(array($user->id));
        }
173
174
175
176
    }

    // Authinstance can be changed by institutional admins if both the
    // old and new authinstances belong to the admin's institutions
177
178
179
180
    $remotename = get_field('auth_remote_user', 'remoteusername', 'authinstance', $user->authinstance, 'localusr', $user->id);
    if (!$remotename) {
        $remotename = $user->username;
    }
181
182
183
    if (isset($values['authinstance'])
        && ($values['authinstance'] != $user->authinstance
            || (isset($values['remoteusername']) && $values['remoteusername'] != $remotename))) {
184
185
186
187
188
189
190
        $authinst = get_records_select_assoc('auth_instance', 'id = ? OR id = ?', 
                                             array($values['authinstance'], $user->authinstance));
        if ($USER->get('admin') || 
            ($USER->is_institutional_admin($authinst[$values['authinstance']]->institution) &&
             $USER->is_institutional_admin($authinst[$user->authinstance]->institution))) {
            delete_records('auth_remote_user', 'authinstance', $user->authinstance, 'localusr', $user->id);
            if ($authinst[$values['authinstance']]->authname != 'internal') {
191
192
193
194
195
196
                if (isset($values['remoteusername']) && strlen($values['remoteusername']) > 0) {
                    $un = $values['remoteusername'];
                }
                else {
                    $un = $remotename;
                }
197
198
                insert_record('auth_remote_user', (object) array(
                    'authinstance'   => $values['authinstance'],
199
                    'remoteusername' => $un,
200
201
202
203
204
                    'localusr'       => $user->id,
                ));
            }
            $user->authinstance = $values['authinstance'];
        }
205
    }
206
207
208
209
210
211
212

    update_record('usr', $user);

    redirect('/admin/users/edit.php?id='.$user->id);
}


213
214
215
216
217
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Suspension/deletion controls
$suspended = $user->get('suspendedcusr');
if (empty($suspended)) {
    $suspendform = pieform(array(
        'name'       => 'edituser_suspend',
        'plugintype' => 'core',
        'pluginname' => 'admin',
        'elements'   => array(
            'id' => array(
                 'type'    => 'hidden',
                 'value'   => $id,
            ),
            'reason' => array(
                'type'        => 'textarea',
                'rows'        => 5,
                'cols'        => 60,
                'title'       => get_string('reason'),
                'description' => get_string('suspendedreasondescription', 'admin'),
            ),
            'submit' => array(
                'type'  => 'submit',
                'value' => get_string('suspenduser','admin'),
            ),
        )
    ));
}
else {
    $suspendformdef = array(
        'name'       => 'edituser_unsuspend',
        'plugintype' => 'core',
        'pluginname' => 'admin',
        'renderer'   => 'oneline',
        'elements'   => array(
            'id' => array(
                 'type'    => 'hidden',
                 'value'   => $id,
            ),
            'submit' => array(
                'type'  => 'submit',
                'value' => get_string('unsuspenduser','admin'),
            ),
        )
    );

    // Create two forms for unsuspension - one in the suspend message and the 
    // other where the 'suspend' button normally goes. This keeps the HTML IDs 
    // unique
    $suspendform  = pieform($suspendformdef);
    $suspendformdef['name'] = 'edituser_suspend2';
    $suspendformdef['validatecallback'] = 'edituser_unsuspend_validate';
    $suspendformdef['successcallback'] = 'edituser_unsuspend_submit';
    $suspendform2 = pieform($suspendformdef);

    $suspender = display_name(get_record('usr', 'id', $suspended));
}

function edituser_suspend_submit(Pieform $form, $values) {
    global $SESSION;
    suspend_user($values['id'], $values['reason']);
    $SESSION->add_ok_msg(get_string('usersuspended', 'admin'));
    redirect('/admin/users/edit.php?id=' . $values['id']);
}

function edituser_unsuspend_submit(Pieform $form, $values) {
    global $SESSION;
    unsuspend_user($values['id']);
    $SESSION->add_ok_msg(get_string('userunsuspended', 'admin'));
    redirect('/admin/users/edit.php?id=' . $values['id']);
}

$deleteform = pieform(array(
    'name' => 'edituser_delete',
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'renderer' => 'oneline',
    'elements'   => array(
        'id' => array(
            'type' => 'hidden',
            'value' => $id,
        ),
        'submit' => array(
            'type' => 'submit',
            'value' => get_string('deleteuser', 'admin'),
            'confirm' => get_string('confirmdeleteuser', 'admin'),
        ),
    ),
));

301
302
303
304
305
306
307
function edituser_delete_submit(Pieform $form, $values) {
    global $SESSION;
    delete_user($values['id']);
    $SESSION->add_ok_msg(get_string('userdeletedsuccessfully', 'admin'));
    redirect('/admin/users/search.php');
}

Richard Mansfield's avatar
Richard Mansfield committed
308

309
// Institution settings form
Richard Mansfield's avatar
Richard Mansfield committed
310
311
312
313
314
315
316
$elements = array(
    'id' => array(
         'type'    => 'hidden',
         'value'   => $id,
     ),
);

317
318
319
320
321
322
323
324
$allinstitutions = get_records_assoc('institution');
foreach ($user->get('institutions') as $i) {
    $elements[$i->institution.'_settings'] = array(
        'type' => 'fieldset',
        'legend' => $allinstitutions[$i->institution]->displayname,
        'elements' => array(
            $i->institution.'_expiry' => array(
                'type'         => 'date',
325
326
                'title'        => get_string('membershipexpiry', 'admin'),
                'description'  => get_string('membershipexpirydescription', 'admin'),
327
328
329
330
331
332
                'minyear'      => $currentdate['year'],
                'maxyear'      => $currentdate['year'] + 20,
                'defaultvalue' => $i->expiry
            ),
            $i->institution.'_studentid' => array(
                'type'         => 'text',
333
334
                'title'        => get_string('studentid', 'admin'),
                'description'  => get_string('institutionstudentiddescription', 'admin'),
335
336
                'defaultvalue' => $i->studentid,
            ),
337
            $i->institution.'_staff' => array(
338
339
340
                'type'         => 'checkbox',
                'title'        => get_string('institutionstaff','admin'),
                'defaultvalue' => $i->staff,
341
            ),
342
343
344
            $i->institution.'_admin' => array(
                'type'         => 'checkbox',
                'title'        => get_string('institutionadmin','admin'),
345
                'description'  => get_string('institutionadmindescription','admin'),
346
347
348
349
350
351
                'defaultvalue' => $i->admin,
            ),
            $i->institution.'_submit' => array(
                'type'  => 'submit',
                'value' => get_string('update'),
            ),
352
353
354
355
356
            $i->institution.'_remove' => array(
                'type'  => 'submit',
                'value' => get_string('removeuserfrominstitution', 'admin'),
                'confirm' => get_string('confirmremoveuserfrominstitution', 'admin'),
            ),
357
        ),
Richard Mansfield's avatar
Richard Mansfield committed
358
359
    );
}
360

361
362
363
364
365
366
367
368
369
370
// Only site admins can add institutions; institutional admins must invite
if ($USER->get('admin') 
    && (get_config('usersallowedmultipleinstitutions') || count($user->institutions) == 0)) {
    $options = array();
    foreach ($allinstitutions as $i) {
        if (!$user->in_institution($i->name) && $i->name != 'mahara') {
            $options[$i->name] = $i->displayname;
        }
    }
    if (!empty($options)) {
371
372
373
374
        $elements['addinstitutionheader'] = array(
            'type'  => 'markup',
            'value' => '<tr><td colspan="2"><h4>' . get_string('addusertoinstitution', 'admin') . '</h4></td></tr>',
        );
375
376
        $elements['addinstitution'] = array(
            'type'         => 'select',
377
            'title'        => get_string('institution'),
378
379
380
381
            'options'      => $options,
        );
        $elements['add'] = array(
            'type'  => 'submit',
382
            'value' => get_string('addusertoinstitution', 'admin'),
383
384
385
386
        );
    }
}

Richard Mansfield's avatar
Richard Mansfield committed
387
388
389
390
391
392
393
394
395
$institutionform = pieform(array(
    'name'       => 'edituser_institution',
    'renderer'   => 'table',
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'elements'   => $elements,
));

function edituser_institution_submit(Pieform $form, $values) {
396
397
    $user = new User;
    if (!$user->find_by_id($values['id'])) {
Richard Mansfield's avatar
Richard Mansfield committed
398
399
        return false;
    }
400
    $userinstitutions = $user->get('institutions');
Richard Mansfield's avatar
Richard Mansfield committed
401

402
    global $USER;
403
    foreach ($userinstitutions as $i) {
Richard Mansfield's avatar
Richard Mansfield committed
404
        if ($USER->can_edit_institution($i->institution)) {
405
406
407
408
            if (isset($values[$i->institution.'_submit'])) {
                $newuser = (object) array(
                    'usr'         => $user->id,
                    'institution' => $i->institution,
409
                    'ctime'       => db_format_timestamp($i->ctime),
410
                    'studentid'   => $values[$i->institution . '_studentid'],
411
                    'staff'       => (int) ($values[$i->institution . '_staff'] == 'on'),
412
413
414
415
416
417
418
419
                    'admin'       => (int) ($values[$i->institution . '_admin'] == 'on'),
                );
                if ($values[$i->institution . '_expiry']) {
                    $newuser->expiry = db_format_timestamp($values[$i->institution . '_expiry']);
                }
                db_begin();
                delete_records('usr_institution', 'usr', $user->id, 'institution', $i->institution);
                insert_record('usr_institution', $newuser);
420
421
422
                if ($newuser->admin) {
                    activity_add_admin_defaults(array($user->id));
                }
423
424
425
426
                handle_event('updateuser', $user->id);
                db_commit();
                break;
            } else if (isset($values[$i->institution.'_remove'])) {
427
428
429
430
431
                if ($user->id == $USER->id) {
                    $USER->leave_institution($i->institution);
                } else {
                    $user->leave_institution($i->institution);
                }
432
433
434
435
                // Institutional admins can no longer access this page
                // if they remove the user from the institution, so
                // send them back to user search.
                if (!$USER->get('admin')) {
436
437
438
                    if (!$USER->is_institutional_admin()) {
                        redirect(get_config('wwwroot'));
                    }
439
440
441
442
443
                    redirect('/admin/users/search.php');
                }
                break;
            }
        }
444
445
    }

446
447
    if (isset($values['add']) && $USER->get('admin')
        && (empty($userinstitutions) || get_config('usersallowedmultipleinstitutions'))) {
448
        $user->join_institution($values['addinstitution']);
Richard Mansfield's avatar
Richard Mansfield committed
449
450
451
452
    }

    redirect('/admin/users/edit.php?id='.$user->id);
}
453
454
455

$smarty = smarty();
$smarty->assign('user', $user);
456
457
458
459
$smarty->assign('suspended', $suspended);
if ($suspended) {
    $smarty->assign('suspendedby', get_string('suspendedby', 'admin', $suspender));
}
460
$smarty->assign('suspendform', $suspendform);
461
462
463
464
if (isset($suspendform2)) {
    $smarty->assign('suspendform2', $suspendform2);
}
$smarty->assign('deleteform', $deleteform);
Richard Mansfield's avatar
Richard Mansfield committed
465
$smarty->assign('siteform', $siteform);
466
$smarty->assign('institutions', count($allinstitutions) > 1);
Richard Mansfield's avatar
Richard Mansfield committed
467
$smarty->assign('institutionform', $institutionform);
Richard Mansfield's avatar
Richard Mansfield committed
468
469
470
471
472
473
474

if ($id != $USER->get('id') && is_null($USER->get('parentuser'))) {
    $loginas = get_string('loginasuser', 'admin', $user->username);
} else {
    $loginas = null;
}
$smarty->assign('loginas', $loginas);
475
476
477
$smarty->display('admin/users/edit.tpl');

?>