institutions.php 26.5 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 admin
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
 *
 */
define('INTERNAL', 1);
28
define('INSTITUTIONALADMIN', 1);
29
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
30
define('TITLE', get_string('institutions', 'admin'));
Penny Leach's avatar
Penny Leach committed
31
32
33
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'institutions');
34
require_once('pieforms/pieform.php');
35
define('MENUITEM', 'manageinstitutions/institutions');
36
37
38

$institution = param_variable('i', '');
$add         = param_boolean('add');
39
40
$edit        = param_boolean('edit');
$delete      = param_boolean('delete');
41

42
43
44
45
$query = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit  = 20;

46
47
48
49
50
51
52
53
54
55
56
if (!$USER->get('admin')) {
    // Institutional admins with only 1 institution go straight to the edit page for that institution
    // They cannot add or delete institutions, or edit an institution they don't administer
    $add = false;
    $delete = false;
    if (!empty($institution) && !$USER->is_institutional_admin($institution)) {
        $institution = '';
        $edit = false;
    }
    if (empty($institution) && count($USER->get('admininstitutions')) == 1) {
        redirect(get_config('wwwroot') . 'admin/users/institutions.php?i='
57
                 . key($USER->get('admininstitutions')));
58
59
60
    }
}

61
62
if ($institution || $add) {

63
64
65
66
67
    $authinstances = auth_get_auth_instances_for_institution($institution);
    if (false == $authinstances) {
        $authinstances = array();
    }

68
    if ($delete) {
69
        function delete_validate(Pieform $form, $values) {
70
            if (get_field('usr_institution', 'COUNT(*)', 'institution', $values['i'])) {
71
                throw new ConfigException('Attempt to delete an institution that has members');
72
73
74
            }
        }

75
        function delete_cancel_submit() {
76
            redirect('/admin/users/institutions.php');
77
78
        }

79
        function delete_submit(Pieform $form, $values) {
80
81
            global $SESSION;

82
            $authinstanceids = get_column('auth_instance', 'id', 'institution', $values['i']);
83
84
            $viewids = get_column('view', 'id', 'institution', $values['i']);
            $artefactids = get_column('artefact', 'id', 'institution', $values['i']);
85

86
            db_begin();
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            if ($viewids) {
                require_once(get_config('libroot') . 'view.php');
                foreach ($viewids as $viewid) {
                    $view = new View($viewid);
                    $view->delete();
                }
            }
            if ($artefactids) {
                foreach ($artefactids as $artefactid) {
                    try {
                        $a = artefact_instance_from_id($artefactid);
                        $a->delete();
                    }
                    catch (ArtefactNotFoundException $e) {
                        // Awesome, it's already gone.
                    }
                }
            }
105
106
107
108
109
            foreach ($authinstanceids as $id) {
                delete_records('auth_instance_config', 'instance', $id);
            }
            delete_records('auth_instance', 'institution', $values['i']);
            delete_records('host', 'institution', $values['i']);
110
            delete_records('institution_locked_profile_field', 'name', $values['i']);
111
            delete_records('usr_institution_request', 'institution', $values['i']);
112
113
114
115
            delete_records('institution', 'name', $values['i']);
            db_commit();

            $SESSION->add_ok_msg(get_string('institutiondeletedsuccessfully', 'admin'));
116
            redirect('/admin/users/institutions.php');
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        }
        $form = array(
            'name' => 'delete',
            'elements' => array(
                'i' => array(
                    'type' => 'hidden',
                    'value' => $institution
                ),
                'delete' => array(
                    'type' => 'hidden',
                    'value' => 1
                ),
                'submit' => array(
                    'type' => 'submitcancel',
                    'value' => array(get_string('yes'), get_string('no'))
                )
            )
        );
135
136
137
        $deleteform = pieform($form);
        $smarty = smarty();
        $smarty->assign('delete_form', $deleteform);
138
        $smarty->assign('institutionname', get_field('institution', 'displayname', 'name', $institution));
139
        $smarty->display('admin/users/institutions.tpl');
140
141
142
        exit;
    }

143
144
145
    $instancearray = array();
    $instancestring = '';
    $c = count($authinstances);
146
    $inuse = '';
147

148
149
    $sitelockedfields = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', 'mahara');

150
151
    if (!$add) {
        $data = get_record('institution', 'name', $institution);
152
        $lockedprofilefields = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', $institution);
153

154
        // TODO: Find a better way to work around Smarty's minimal looping logic
155
156
157
158
        if (!empty($authinstances)) {
            foreach($authinstances as $key => $val) {
                $authinstances[$key]->index = $key;
                $authinstances[$key]->total = $c;
159
                $instancearray[] = (int)$val->id;
160
            }
161

162
163
            $instancestring = implode(',',$instancearray);
            $inuserecords = array();
164
165
            if ($records = get_records_sql_assoc('select authinstance, count(id) from {usr} where authinstance in ('.$instancestring.') group by authinstance', array())) {
                foreach ($records as $record) {
Richard Mansfield's avatar
Richard Mansfield committed
166
                    $inuserecords[] = $record->authinstance;
167
                }
168
169
            }
            $inuse = implode(',',$inuserecords);
170
        }
171
        $authtypes = auth_get_available_auth_types($institution);
172
173
174
175
    }
    else {
        $data = new StdClass;
        $data->displayname = '';
176
        $data->expiry = null;
177
178
179
        if (!get_config('usersuniquebyusername')) {
            $data->registerallowed = 1;
        }
180
        $data->theme = 'sitedefault';
181
        $data->defaultmembershipperiod = null;
182
        $lockedprofilefields = array();
183
184

        $authtypes = auth_get_available_auth_types();
185
    }
186
    $themeoptions = get_institution_themes($institution);
187
    $themeoptions['sitedefault'] = '- ' . get_string('sitedefault', 'admin') . ' (' . $themeoptions[get_config('theme')] . ') -';
188
    uksort($themeoptions, 'theme_sort');
189
190
191

    $sitename = get_config('sitename');

192
    safe_require('artefact', 'internal');
193
194
    $elements = array(
        'name' => array(
195
            'type' => 'text',
196
            'title' => get_string('institutionname', 'admin'),
197
198
199
            'rules' => array(
                'required'  => true,
                'maxlength' => 255,
200
                'regex'     => '/^[a-zA-Z]+$/'
201
            ),
Penny Leach's avatar
Penny Leach committed
202
203
            'ignore' => !$add,
            'help'   => true,
204
205
206
207
208
209
        ),
        'add' => array(
            'type'   => 'hidden',
            'value'  => true,
            'ignore' => !$add
        ),
210
211
212
        'inuse' => array(
            'type'   => 'hidden',
            'value'  => $inuse,
213
214
            'id'     => 'inuse',
            'ignore' => $add
215
        ),
216
217
218
219
220
221
222
223
224
225
226
227
        'i' => array(
            'type'   => 'hidden',
            'value'  => $institution,
            'ignore' => $add
        ),
        'displayname' => array(
            'type' => 'text',
            'title' => get_string('institutiondisplayname', 'admin'),
            'defaultvalue' => $data->displayname,
            'rules' => array(
                'required'  => true,
                'maxlength' => 255
Penny Leach's avatar
Penny Leach committed
228
229
            ),
            'help'   => true,
230
        ),
231
    );
232
    if ($USER->get('admin') && $institution != 'mahara') {
233
       $elements['expiry'] = array(
234
235
            'type'         => 'date',
            'title'        => get_string('institutionexpiry', 'admin'),
236
            'description'  => get_string('institutionexpirydescription', 'admin', hsc($sitename)),
237
238
239
240
            'defaultvalue' => is_null($data->expiry) ? null : strtotime($data->expiry),
            'help'         => true,
            'minyear'      => date('Y') - 2,
            'maxyear'      => date('Y') + 10,
241
242
        );
    }
243
244
    if ($USER->get('admin')) {
        $elements['authplugin'] = array(
245
246
247
248
249
            'type'    => 'authlist',
            'title'   => get_string('authplugin', 'admin'),
            'options' => $authinstances,
            'authtypes' => $authtypes,
            'instancearray' => $instancearray,
250
            'instancestring' => $instancestring,
251
252
            'institution' => $institution,
            'help'   => true,
253
            'ignore' => count($authtypes) == 0 || $institution == ''
254
255
256
        );
    }

257
    if (!$add && empty($authinstances)) {
258
        if ($USER->get('admin')) {
259
            $SESSION->add_error_msg(get_string('adminnoauthpluginforinstitution', 'admin'));
260
261
        }
        else {
262
            $SESSION->add_error_msg(get_string('noauthpluginforinstitution', 'admin'));
263
264
265
        }
    }

266
267
268
269
270
271
272
273
274
    if (!get_config('usersuniquebyusername')) {
        $elements['registerallowed'] = array(
            'type'         => 'checkbox',
            'title'        => get_string('registrationallowed', 'admin'),
            'description'  => get_string('registrationalloweddescription2', 'admin'),
            'defaultvalue' => $data->registerallowed,
            'help'   => true,
        );
    }
275

276
    if (empty($data->name) || $data->name != 'mahara') {
277
        $elements['defaultmembershipperiod'] = array(
278
            'type'         => 'expiry',
279
280
281
            'title'        => get_string('defaultmembershipperiod', 'admin'),
            'description'  => get_string('defaultmembershipperioddescription', 'admin'),
            'defaultvalue' => $data->defaultmembershipperiod,
Penny Leach's avatar
Penny Leach committed
282
            'help'   => true,
283
284
        );
        $elements['theme'] = array(
285
            'type'         => 'select',
286
            'title'        => get_string('theme'),
287
            'description'  => get_string('sitethemedescription','admin'),
288
            'defaultvalue' => $data->theme ? $data->theme : 'sitedefault',
289
290
291
            'collapseifoneoption' => true,
            'options'      => $themeoptions,
            'help'         => true,
292
        );
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
        if ($USER->get('admin') || get_config_plugin('artefact', 'file', 'institutionaloverride')) {
            $elements['defaultquota'] = array(
               'type'         => 'bytes',
               'title'        => get_string('defaultquota', 'artefact.file'),
               'description'  => get_string('defaultinstitutionquotadescription', 'admin'),
               'defaultvalue' => !empty($data->defaultquota) ? $data->defaultquota : get_config_plugin('artefact', 'file', 'defaultquota'),
            );
        }
        else {
            $elements['defaultquota'] = array(
                'type' => 'text',
                'title' => get_string('defaultquota', 'artefact.file'),
                'value' => display_size(!empty($data->defaultquota) ? $data->defaultquota : get_config_plugin('artefact', 'file', 'defaultquota')),
                'disabled' => true,
            );
        }
309
310
311
312
313
314
        if ($USER->get('admin')) {
            $elements['maxuseraccounts'] = array(
                'type'         => 'text',
                'title'        => get_string('maxuseraccounts','admin'),
                'description'  => get_string('maxuseraccountsdescription','admin'),
                'defaultvalue' => empty($data->maxuseraccounts) ? '' : $data->maxuseraccounts,
315
316
317
318
319
                'rules'        => array(
                    'regex'     => '/^\d*$/',
                    'maxlength' => 8,
                ),
                'size'         => 5,
320
321
            );
        }
322
    }
323

324
    $elements['lockedfields'] = array(
325
        'type' => 'fieldset',
326
        'legend' => get_string('Lockedfields', 'admin'),
327
328
329
        'collapsible' => true,
        'collapsed' => true,
        'elements' => array(),
330
    );
331
332
333
334
335
336
    if ($institution != 'mahara') {
        $elements['lockedfields']['elements']['description'] = array(
            'type' => 'html',
            'value' => get_string('disabledlockedfieldhelp', 'admin', get_field('institution', 'displayname', 'name', 'mahara')),
        );
    }
337
    foreach (ArtefactTypeProfile::get_all_fields() as $field => $type) {
338
        $elements['lockedfields']['elements'][$field] = array(
339
            'type' => 'checkbox',
340
            'title' => get_string($field, 'artefact.internal'),
341
342
            'defaultvalue' => in_array($field, $lockedprofilefields) || ($institution != 'mahara' && in_array($field, $sitelockedfields)),
            'disabled' => $institution != 'mahara' && in_array($field, $sitelockedfields)
343
344
        );
    }
345
    $elements['lockedfieldshelp'] = array(
Evonne Cheung's avatar
Evonne Cheung committed
346
        'value' => '<tr id="lockedfieldshelp"><th colspan="2">'
347
348
349
350
        . get_help_icon('core', 'admin', 'institution', 'lockedfields') 
        . '</th></tr>'
    );

351
352
353
354
355
    $elements['submit'] = array(
        'type' => 'submitcancel',
        'value' => array(get_string('submit'), get_string('cancel'))
    );

356
    $institutionform = pieform(array(
357
        'name'     => 'institution',
358
        'renderer' => 'table',
Penny Leach's avatar
Penny Leach committed
359
360
        'plugintype' => 'core',
        'pluginname' => 'admin',
361
        'elements' => $elements
362
    ));
363
364
365
366

}
else {
    // Get a list of institutions
367
    require_once(get_config('libroot') . 'institution.php');
368
    if (!$USER->get('admin')) { // Filter the list for institutional admins
369
370
        $filter      = $USER->get('admininstitutions');
        $showdefault = false;
371
372
    }
    else {
373
374
        $filter      = false;
        $showdefault = true;
375
    }
376
    $data = build_institutions_html($filter, $showdefault, $query, $limit, $offset, $count);
377
378

    $smarty = smarty(array('lib/pieforms/static/core/pieforms.js', 'paginator'));
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    $smarty->assign('results', $data);
    $smarty->assign('countinstitutions', $count);

    /*search institution form*/
    $searchform = pieform(array(
        'name' => 'search',
        'renderer' => 'oneline',
        'elements' => array(
            'query' => array(
                'type' => 'text',
                'defaultvalue' => $query
            ),
            'submit' => array(
                'type' => 'submit',
                'value' => get_string('search')
            )
        )
    ));
    $smarty->assign('searchform', $searchform);

    $js = <<< EOF
    addLoadEvent(function () {
    p = {$data['pagination_js']}
    connect('search_submit', 'onclick', function (event) {
        replaceChildNodes('messages');
        var params = {'query': $('search_query').value};
        p.sendQuery(params);
        event.stop();
        });
    });
EOF;

    $smarty->assign('INLINEJAVASCRIPT', $js);
412
413
414
415
    $smarty->assign('siteadmin', $USER->get('admin'));
    $smarty->assign('PAGEHEADING', get_string('admininstitutions', 'admin'));
    $smarty->display('admin/users/institutions.tpl');
    exit;
416
417
}

418
function institution_validate(Pieform $form, $values) {
419
    if (!empty($values['name']) && !$form->get_error('name') && record_exists('institution', 'name', $values['name'])) {
420
421
        $form->set_error('name', get_string('institutionnamealreadytaken', 'admin'));
    }
422
423
424
    if (get_config_plugin('artefact', 'file', 'maxquotaenabled') && get_config_plugin('artefact', 'file', 'maxquota') < $values['defaultquota']) {
        $form->set_error('defaultquota', get_string('maxquotatoolow', 'artefact.file'));
    }
425
426
}

427
function institution_submit(Pieform $form, $values) {
428
    global $SESSION, $institution, $add, $instancearray, $USER, $authinstances;
429
430
431
432
433

    db_begin();
    // Update the basic institution record...
    $newinstitution = new StdClass;
    if ($add) {
434
        $institution = $newinstitution->name = strtolower($values['name']);
435
436
437
    }

    $newinstitution->displayname                  = $values['displayname'];
438
    $newinstitution->authplugin                   = empty($values['authplugin']) ? null : $values['authplugin'];
439
440
441
442
443
444
445
446
447
    if (get_config('usersuniquebyusername')) {
        // Registering absolutely not allowed when this setting is on, it's a 
        // security risk. See the documentation for the usersuniquebyusername 
        // setting for more information
        $newinstitution->registerallowed = 0;
    }
    else {
        $newinstitution->registerallowed              = ($values['registerallowed']) ? 1 : 0;
    }
448
    $newinstitution->theme                        = (empty($values['theme']) || $values['theme'] == 'sitedefault') ? null : $values['theme'];
449
450
451
452

    if ($USER->get('admin') || get_config_plugin('artefact', 'file', 'institutionaloverride')) {
        $newinstitution->defaultquota = $values['defaultquota'];
    }
453
454
455
456
    if ($institution != 'mahara') {
        $newinstitution->defaultmembershipperiod  = ($values['defaultmembershipperiod']) ? intval($values['defaultmembershipperiod']) : null;
        if ($USER->get('admin')) {
            $newinstitution->maxuseraccounts      = ($values['maxuseraccounts']) ? intval($values['maxuseraccounts']) : null;
457
            $newinstitution->expiry               = db_format_timestamp($values['expiry']);
458
        }
459
460
    }

461
462
    if (!empty($values['authplugin'])) {
        $allinstances = array_merge($values['authplugin']['instancearray'], $values['authplugin']['deletearray']);
463

464
        if (array_diff($allinstances, $instancearray)) {
465
            throw new ConfigException('Attempt to delete or update another institution\'s auth instance');
466
        }
467

468
        if (array_diff($instancearray, $allinstances)) {
469
            throw new ConfigException('One of your instances is unaccounted for in this transaction');
470
471
        }

472
473
474
        foreach($values['authplugin']['instancearray'] as $priority => $instanceid) {
            if (in_array($instanceid, $values['authplugin']['deletearray'])) {
                // Should never happen:
475
                throw new SystemException('Attempt to update AND delete an auth instance');
476
477
478
479
480
481
482
483
            }
            $record = new StdClass;
            $record->priority = $priority;
            $record->id = $instanceid;
            update_record('auth_instance', $record,  array('id' => $instanceid));
        }

        foreach($values['authplugin']['deletearray'] as $instanceid) {
484
485
486
487
            // If this authinstance is the only xmlrpc authinstance that references a host, delete the host record.
            $hostwwwroot = null;
            foreach ($authinstances as $ai) {
                if ($ai->id == $instanceid && $ai->authname == 'xmlrpc') {
Francois Marier's avatar
Francois Marier committed
488
489
                    $hostwwwroot = get_field_sql("SELECT \"value\" FROM {auth_instance_config} WHERE \"instance\" = ? AND field = 'wwwroot'", array($instanceid));
                    if ($hostwwwroot && count_records_select('auth_instance_config', "field = 'wwwroot' AND \"value\" = ?", array($hostwwwroot)) == 1) {
490
491
492
493
494
495
496
497
498
                        // Unfortunately, it's possible that this host record could belong to a different institution,
                        // so specify the institution here.
                        delete_records('host', 'wwwroot', $hostwwwroot, 'institution', $institution);
                        // We really need to fix this, either by removing the institution from the host table, or refusing to allow the
                        // institution to be changed in the host record when another institution's authinstance is still pointing at it.
                    }
                    break;
                }
            }
499
500
501
502
            delete_records('auth_remote_user', 'authinstance', $instanceid);
            delete_records('auth_instance_config', 'instance', $instanceid);
            delete_records('auth_instance', 'id', $instanceid);
        }
503
504
    }

505
506
    if ($add) {
        insert_record('institution', $newinstitution);
507
508
509
510
511
512
513
514
515
516
517
        // If registration has been turned on, then we automatically insert an 
        // internal authentication authinstance
        if ($newinstitution->registerallowed) {
            $authinstance = (object)array(
                'instancename' => 'internal',
                'priority'     => 0,
                'institution'  => $newinstitution->name,
                'authname'     => 'internal',
            );
            insert_record('auth_instance', $authinstance);
        }
518
    }
519
    else {
520
521
        $where = new StdClass;
        $where->name = $institution;
522
        $oldtheme = get_field('institution', 'theme', 'name', $institution);
523
524
525
526
527
528
529
530
531
532
533
534
535
536
        update_record('institution', $newinstitution, $where);
    }

    delete_records('institution_locked_profile_field', 'name', $institution);
    foreach (ArtefactTypeProfile::get_all_fields() as $field => $type) {
        if ($values[$field]) {
            $profilefield = new StdClass;
            $profilefield->name         = $institution;
            $profilefield->profilefield = $field;
            insert_record('institution_locked_profile_field', $profilefield);
        }
    }
    db_commit();

537
    if ($add) {
538
539
540
541
542
543
        if ($newinstitution->registerallowed) {
            // If registration is not allowed, then an authinstance will not 
            // have been created, and thus cause the institution page to add 
            // its own error message on the next page load
            $SESSION->add_ok_msg(get_string('institutionaddedsuccessfully2', 'admin'));
        }
544
545
546
        $nexturl = '/admin/users/institutions.php?i='.urlencode($institution);
    }
    else {
547
        $message = get_string('institutionupdatedsuccessfully', 'admin');
548
        if (isset($values['theme']) && $oldtheme != $values['theme']
549
            && (!empty($oldtheme) || $values['theme'] != 'sitedefault')) {
550
            $USER->update_theme();
551
552
            $message .= '  ' . get_string('usersseenewthemeonlogin', 'admin');
        }
553
        $SESSION->add_ok_msg($message);
554
555
556
557
        $nexturl = '/admin/users/institutions.php';
    }

    redirect($nexturl);
558
559
560
}

function institution_cancel_submit() {
561
    redirect('/admin/users/institutions.php');
562
563
}

564
565
566
567
568
569
570
571
572
573
574
575
576
577
if ($institution && $institution != 'mahara') {
    $_institution = get_record('institution', 'name', $institution);
    $suspended = $_institution->suspended;
    if ($USER->get('admin')) {
        function institution_suspend_submit(Pieform $form, $values) {
            global $SESSION, $USER;
            if (!$USER->get('admin')) {
                $SESSION->add_error_msg(get_string('errorwhilesuspending', 'admin'));
            }
            else {
                set_field('institution', 'suspended', 1, 'name', $values['i']);
                $SESSION->add_ok_msg(get_string('institutionsuspended', 'admin'));
            }
            redirect('/admin/users/institutions.php?i=' . $values['i']);
578
        }
579
580
581
582
583
584
585
586
587
588
589

        function institution_unsuspend_submit(Pieform $form, $values) {
            global $SESSION, $USER;
            if (!$USER->get('admin')) {
                $SESSION->add_error_msg(get_string('errorwhileunsuspending', 'admin'));
            }
            else {
                set_field('institution', 'suspended', 0, 'name', $values['i']);
                $SESSION->add_ok_msg(get_string('institutionunsuspended', 'admin'));
            }
            redirect('/admin/users/institutions.php?i=' . $values['i']);
590
        }
591

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
        // Suspension controls
        if (empty($suspended)) {
            $suspendformdef = array(
                'name'       => 'institution_suspend',
                'plugintype' => 'core',
                'pluginname' => 'admin',
                'elements'   => array(
                    'i' => array(
                        'type'    => 'hidden',
                        'value'   => $institution,
                    ),
                    'submit' => array(
                        'type'        => 'submit',
                        'value'       => get_string('suspendinstitution','admin'),
                        'description' => get_string('suspendinstitutiondescription','admin'),
                    ),
                )
            );

            $suspendform  = pieform($suspendformdef);
612
613
        }
        else {
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
            $suspendformdef = array(
                'name'       => 'institution_unsuspend',
                'plugintype' => 'core',
                'pluginname' => 'admin',
                'elements'   => array(
                    'i' => array(
                        'type'    => 'hidden',
                        'value'   => $institution,
                    ),
                    'submit' => array(
                        'type'        => 'submit',
                        'value'       => get_string('unsuspendinstitution','admin'),
                        'description' => get_string('unsuspendinstitutiondescription','admin'),
                    ),
                )
            );
            $suspendform  = pieform($suspendformdef);

            // Create a second forms for unsuspension to go in the suspend message.
            // This keeps the HTML IDs unique
            $suspendformdef['name'] = 'institution_unsuspend_top';
            $suspendformdef['renderer'] = 'oneline';
            $suspendformdef['successcallback'] = 'institution_unsuspend_submit';
            $suspendform_top = pieform($suspendformdef);
        }
639
640
641
    }
}

642
643
644
645
function search_submit(Pieform $form, $values) {
    redirect('/admin/users/institutions.php' . (!empty($values['query']) ? '?query=' . urlencode($values['query']) : ''));
}

646
647
648
649
$smarty = smarty();
$smarty->assign('institution_form', $institutionform);
$smarty->assign('instancestring', $instancestring);
$smarty->assign('add', $add);
650
651
652
653
654
655

if (isset($suspended)) {
    if ($suspended) {
        $smarty->assign('suspended', get_string('suspendedinstitutionmessage', 'admin'));
    }
    if (isset($suspendform)) {
656
657
658
        $smarty->assign('suspendform', $suspendform);
        if (isset($suspendform_top)) {
            $smarty->assign('suspendform_top', $suspendform_top);
659
        }
660
661
662
    }
}

663
$smarty->assign('PAGEHEADING', get_string('admininstitutions', 'admin'));
664
$smarty->display('admin/users/institutions.tpl');
665

666
667
668
669
670
671
672
673
674
function theme_sort($a, $b) {
    if ($a == 'sitedefault') {
        return -1;
    }
    if ($b == 'sitedefault') {
        return 1;
    }
    return $a > $b;
}