institutions.php 13.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/**
 * This program is part of Mahara
 *
 *  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 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage admin
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */
define('INTERNAL', 1);
27
define('INSTITUTIONALADMIN', 1);
28
define('MENUITEM', 'configusers/institutions');
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
35
36
37
38
require_once('pieforms/pieform.php');
$smarty = smarty();

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

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
global $USER;
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='
                 . key($USER->get('institutions')));
    }
}

58
59
if ($institution || $add) {

60
61
62
63
64
    $authinstances = auth_get_auth_instances_for_institution($institution);
    if (false == $authinstances) {
        $authinstances = array();
    }

65
    if ($delete) {
66
        function delete_validate(Pieform $form, $values) {
67
            if (get_field('usr_institution', 'COUNT(*)', 'institution', $values['i'])) {
68
                // TODO: exception is of the wrong type
69
70
71
72
                throw new Exception('Attempt to delete an institution that has members');
            }
        }

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

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

            db_begin();
            delete_records('institution_locked_profile_field', 'name', $values['i']);
82
            delete_records('usr_institution_request', 'institution', $values['i']);
83
84
85
86
            delete_records('institution', 'name', $values['i']);
            db_commit();

            $SESSION->add_ok_msg(get_string('institutiondeletedsuccessfully', 'admin'));
87
            redirect('/admin/users/institutions.php');
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        }
        $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'))
                )
            )
        );
        $smarty->assign('delete_form', pieform($form));
107
        $smarty->display('admin/users/institutions.tpl');
108
109
110
        exit;
    }

111
112
113
    $instancearray = array();
    $instancestring = '';
    $c = count($authinstances);
114
    $inuse = '';
115

116
117
    if (!$add) {
        $data = get_record('institution', 'name', $institution);
118
        $lockedprofilefields = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', $institution);
119
120
        
        // TODO: Find a better way to work around Smarty's minimal looping logic
121
122
123
124
125
126
        if (!empty($authinstances)) {
            foreach($authinstances as $key => $val) {
                $authinstances[$key]->index = $key;
                $authinstances[$key]->total = $c;
                $instancearray[] = $val->id;
            }
127

128
129
            $instancestring = implode(',',$instancearray);
            $inuserecords = array();
130
131
            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
132
                    $inuserecords[] = $record->authinstance;
133
                }
134
135
            }
            $inuse = implode(',',$inuserecords);
136
        }
137
        $authtypes = auth_get_available_auth_types($institution);
138
139
140
141
142
    }
    else {
        $data = new StdClass;
        $data->displayname = '';
        $data->registerallowed = 1;
143
144
        $data->theme = 'default';
        $data->defaultmembershipperiod = null;
145
146
        $lockedprofilefields = array();
        $smarty->assign('add', true);
147
148

        $authtypes = auth_get_available_auth_types();
149
    }
150
    $themeoptions = get_themes();
151
152
    
    safe_require('artefact', 'internal');
153
154
    $elements = array(
        'name' => array(
155
            'type' => 'text',
156
            'title' => get_string('institutionname', 'admin'),
157
158
159
            'rules' => array(
                'required'  => true,
                'maxlength' => 255,
160
                'regex'     => '/^[a-zA-Z]+$/'
161
            ),
Penny Leach's avatar
Penny Leach committed
162
163
            'ignore' => !$add,
            'help'   => true,
164
165
166
167
168
169
        ),
        'add' => array(
            'type'   => 'hidden',
            'value'  => true,
            'ignore' => !$add
        ),
170
171
172
        'inuse' => array(
            'type'   => 'hidden',
            'value'  => $inuse,
173
174
            'id'     => 'inuse',
            'ignore' => $add
175
        ),
176
177
178
179
180
181
182
183
184
185
186
187
        '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
188
189
            ),
            'help'   => true,
190
191
192
193
194
195
196
        ),
        'authplugin' => array(
            'type'    => 'authlist',
            'title'   => get_string('authplugin', 'admin'),
            'options' => $authinstances,
            'authtypes' => $authtypes,
            'instancearray' => $instancearray,
197
            'instancestring' => $instancestring,
198
199
200
201
            'institution' => $institution,
            'help'   => true,
            'ignore' => count($authtypes) == 0
        ),
202
203
204
205
        'registerallowed' => array(
            'type'         => 'checkbox',
            'title'        => get_string('registrationallowed', 'admin'),
            'description'  => get_string('registrationalloweddescription', 'admin'),
206
            'defaultvalue' => $data->registerallowed,
Penny Leach's avatar
Penny Leach committed
207
            'help'   => true,
208
        ),
209
210
211
212
    );

    if ($data->name != 'mahara') {
        $elements['defaultmembershipperiod'] = array(
213
            'type'         => 'expiry',
214
215
216
            'title'        => get_string('defaultmembershipperiod', 'admin'),
            'description'  => get_string('defaultmembershipperioddescription', 'admin'),
            'defaultvalue' => $data->defaultmembershipperiod,
Penny Leach's avatar
Penny Leach committed
217
            'help'   => true,
218
219
        );
        $elements['theme'] = array(
220
221
222
223
224
225
226
            'type'         => 'select',
            'title'        => get_string('theme','admin'),
            'description'  => get_string('sitethemedescription','admin'),
            'defaultvalue' => $data->theme,
            'collapseifoneoption' => true,
            'options'      => $themeoptions,
            'help'         => true,
227
        );
228
229
230
231
232
233
234
235
236
        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,
                'rules'        => array('regex' => '/^\d*$/'),
            );
        }
237
    }
238

239
240
241
242
243
    $elements['lockedfields'] = array(
        'value' => '<tr><th colspan="2">Locked fields ' 
        . get_help_icon('core', 'admin', 'institution', 'lockedfields') 
        . '</th></tr>'
    );
244
245
246
    foreach (ArtefactTypeProfile::get_all_fields() as $field => $type) {
        $elements[$field] = array(
            'type' => 'checkbox',
247
            'title' => get_string($field, 'artefact.internal'),
248
            'defaultvalue' => in_array($field, $lockedprofilefields)
249
250
251
252
253
254
255
        );
    }
    $elements['submit'] = array(
        'type' => 'submitcancel',
        'value' => array(get_string('submit'), get_string('cancel'))
    );

256
    $smarty->assign('instancestring', $instancestring);
257

258
259
    $smarty->assign('institution_form', pieform(array(
        'name'     => 'institution',
260
        'renderer' => 'table',
Penny Leach's avatar
Penny Leach committed
261
262
        'plugintype' => 'core',
        'pluginname' => 'admin',
263
264
265
266
267
268
        'elements' => $elements
    )));

}
else {
    // Get a list of institutions
269
270
271
272
273
274
275
276
    if (!$USER->get('admin')) { // Filter the list for institutional admins
        $where = '
        WHERE i.name IN (' . join(',', array_map('db_quote', $USER->get('admininstitutions'))) . ')';
    }
    else {
        $where = '';
        $smarty->assign('siteadmin', true);
    }
277
    $institutions = get_records_sql_array('SELECT i.name, i.displayname, i.registerallowed, COUNT(u.usr) AS hasmembers
278
        FROM {institution} i
279
        LEFT OUTER JOIN {usr_institution} u ON (u.institution = i.name) ' . $where . '
280
        GROUP BY 1, 2, 3
281
282
283
284
        ORDER BY i.name', array());
    $smarty->assign('institutions', $institutions);
}

285
function institution_submit(Pieform $form, $values) {
286
    global $SESSION, $institution, $add, $instancearray, $USER;
287
288
289
290
291

    db_begin();
    // Update the basic institution record...
    $newinstitution = new StdClass;
    if ($add) {
292
        $institution = $newinstitution->name = strtolower($values['name']);
293
294
295
296
297
    }

    $newinstitution->displayname                  = $values['displayname'];
    $newinstitution->authplugin                   = $values['authplugin'];
    $newinstitution->registerallowed              = ($values['registerallowed']) ? 1 : 0;
298
    $newinstitution->theme                        = empty($values['theme']) ? null : $values['theme'];
299
    $newinstitution->defaultmembershipperiod      = ($values['defaultmembershipperiod']) ? intval($values['defaultmembershipperiod']) : null;
300

301
302
303
304
    if ($USER->get('admin')) {
        $newinstitution->maxuseraccounts          = ($values['maxuseraccounts']) ? intval($values['maxuseraccounts']) : null;
    }

305
306
    $allinstances = array_merge($values['authplugin']['instancearray'], $values['authplugin']['deletearray']);

307
    if (array_diff($allinstances, $instancearray)) {
308
        // TODO wrong exception type
309
310
311
        throw new Exception('Attempt to delete or update another institution\'s auth instance');
    }

312
    if (array_diff($instancearray, $allinstances)) {
313
        // TODO wrong exception type
314
315
316
317
        throw new Exception('One of your instances is unaccounted for in this transaction');
    }

    foreach($values['authplugin']['instancearray'] as $priority => $instanceid) {
318
        if (in_array($instanceid, $values['authplugin']['deletearray'])) {
319
            // Should never happen:
320
            // TODO wrong exception type
321
322
323
324
325
326
327
328
329
330
331
332
333
            throw new Exception('Attempt to update AND delete an auth instance');
        }
        $record = new StdClass;
        $record->priority = $priority;
        $record->id = $instanceid;
        update_record('auth_instance', $record,  array('id' => $instanceid));
    }

    foreach($values['authplugin']['deletearray'] as $instanceid) {
        delete_records('auth_instance_config', 'instance', $instanceid);
        delete_records('auth_instance', 'id', $instanceid);
    }

334
335
336
    if ($add) {
        insert_record('institution', $newinstitution);
    }
337
    else {
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        $where = new StdClass;
        $where->name = $institution;
        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();

354
355
356
357
358
359
360
361
362
    if ($add) {
        $message = 'institutionaddedsuccessfully';
        $nexturl = '/admin/users/institutions.php?i='.urlencode($institution);
    }
    else {
        $message = 'institutionupdatedsuccessfully';
        $nexturl = '/admin/users/institutions.php';
    }

363
    $SESSION->add_ok_msg(get_string($message, 'admin'));
364
    redirect($nexturl);
365
366
367
}

function institution_cancel_submit() {
368
    redirect('/admin/users/institutions.php');
369
370
}

371
$smarty->display('admin/users/institutions.tpl');
372
373

?>