uploadcsv.php 6.19 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
27
28
<?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);
define('ADMIN', 1);
29
define('MENUITEM', 'configusers');
30
define('SUBMENUITEM', 'uploadcsv');
31
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
32
require_once('pieforms/pieform.php');
33

34
$institutions = get_records_array('institution');
35
36
37
38
foreach ($institutions as $name => $data) {
    $options[$name] = $data->displayname;
}

39
$institutions = get_records_select_array('institution', "registerallowed = 1 AND authplugin = 'internal'");
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
if (count($institutions) > 1) {
    $options = array();
    foreach ($institutions as $institution) {
        $options[$institution->name] = $institution->displayname;
    }
    $institutionelement = array(
        'type' => 'select',
        'title' => get_string('institution'),
        'description' => get_string('institutiondescription'),
        'options' => $options
    );
}
else {
    $institutionelement = array(
        'type' => 'hidden',
        'value' => 'mahara'
    );
}

$form = array(
    'name' => 'uploadcsv',
    'elements' => array(
        'institution' => $institutionelement,
        'file' => array(
            'type' => 'file',
            'title' => get_string('csvfile', 'admin'),
            'description' => get_string('csvfiledescription', 'admin'),
            'rules' => array(
                'required' => true
            )
        ),
        'submit' => array(
            'type' => 'submit',
            'value' => get_string('uploadcsvfile', 'admin')
        )
    )
);

/**
 * The CSV file is parsed here so validation errors can be returned to the
 * user. The data from a successful parsing is stored in the <var>$CVSDATA</var>
 * array so it can be accessed by the submit function
 *
83
84
 * @param Pieform  $form   The form to validate
 * @param array    $values The values submitted
85
 */
86
function uploadcsv_validate(Pieform $form, $values) {
87
88
89
90
91
92
93
94
    global $CSVDATA;

    // Don't even start attempting to parse if there are previous errors
    if ($form->has_errors()) {
        return;
    }

    if ($values['file']['size'] == 0) {
95
        $form->set_error('file', $form->i18n('required'));
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        return;
    }

    require_once('pear/File.php');
    require_once('pear/File/CSV.php');

    $institution = $values['institution'];

    log_debug($values);
    $conf = File_CSV::discoverFormat($values['file']['tmp_name']);
    log_debug($conf);
    $i = 0;
    while ($line = @File_CSV::readQuoted($values['file']['tmp_name'], $conf)) {
        log_debug($line);
        $i++;
        if (count($line) < 3) {
            $form->set_error('file', get_string('uploadcsverrorincorrectfieldcount', 'admin', $i));
            return;
        }
        $username = $line[0];
116
117
        $password = $line[1];
        $email    = $line[2];
118
119
120
121
122
123
124

        safe_require('auth', 'internal', 'lib.php', 'require_once');
        if (!AuthInternal::is_username_valid($username)) {
            $form->set_error('file', get_string('uploadcsverrorinvalidusername', 'admin', $i));
            return;
        }
        if (record_exists('usr', 'username', $username)) {
125
            $form->set_error('file', get_string('uploadcsverroruseralreadyexists', 'admin', $i, $username));
126
127
128
129
130
131
132
133
134
135
136
            return;
        }

        // Note: only checks for valid form are done here, none of the checks
        // like whether the password is too easy. The user is going to have to
        // change their password on first login anyway.
        if (!AuthInternal::is_password_valid($password)) {
            $form->set_error('file', get_string('uploadcsverrorinvalidpassword', 'admin', $i));
            return;
        }

137
138
139
140
141
142
143
144
145
146
147
148
149
        safe_require('artefact', 'internal');
        $fieldcounter = 2;
        foreach (ArtefactTypeProfile::get_mandatory_fields() as $field => $type) {
            $fieldcounter++;
            if (!isset($line[$fieldcounter])) {
                $form->set_error('file', get_string('uploadcsverrormandatoryfieldnotspecified', 'admin', $i, $field));
                return;
            }

            // @todo validate the mandatory fields somehow. In theory, this should
            // just involve calling a method on a class.
        }

150
151
152
153
154
155
156
157
158
        // All OK!
        $CSVDATA[] = $line;
    }
}

/**
 * Add the users to the system. Make sure that they have to change their
 * password on next login also.
 */
159
function uploadcsv_submit(Pieform $form, $values) {
160
161
162
    global $SESSION, $CSVDATA;
    log_info('Inserting users from the CSV file');
    foreach ($CSVDATA as $record) {
163
164
165
166
167
168
169
170
171
        log_debug('adding user ' . $record[0]);
        $user = new StdClass;
        $user->username  = $record[0];
        $user->password  = $record[1];
        $user->institution = $values['institution'];
        $user->passwordchange = 1;
        $id = insert_record('usr', $user, 'id', true);

        $i = 2;
172
        safe_require('artefact', 'internal');
173
174
175
        foreach (ArtefactTypeProfile::get_mandatory_fields() as $field => $type) {
            set_profile_field($id, $field, $record[$i++]);
        }
Nigel McNie's avatar
Nigel McNie committed
176
177

        handle_event('createuser', $user);
178
179
180
    }
    log_info('Inserted ' . count($CSVDATA) . ' records');

181
    $SESSION->add_ok_msg(get_string('uploadcsvusersaddedsuccessfully', 'admin'));
182
    redirect('admin/users/uploadcsv.php');
183
184
185
}

$smarty = smarty();
186
$smarty->assign('uploadcsvform', pieform($form));
187
$smarty->display('admin/users/uploadcsv.tpl');
188
189

?>