Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * this file contains CSV file suspension processor.
19
 *
20
 * File         csv.php
21
 * Encoding     UTF-8
22
 * @copyright   Sebsoft.nl
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace tool_usersuspension\processor;
27
 
28
/**
29
 * Description of csv
30
 *
31
 * @package     tool_usersuspension
32
 *
33
 * @copyright   Sebsoft.nl
34
 * @author      R.J. van Dongen <rogier@sebsoft.nl>
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class csv {
38
 
39
    /**
40
     * @var int SUSPEND MODE
41
     */
42
    const MODE_SUSPEND = 1;
43
    /**
44
     * @var int UNSUSPEND MODE
45
     */
46
    const MODE_UNSUSPEND = 2;
47
 
48
    /**
49
     * CSV Filename
50
     * @var string
51
     */
52
    protected $file;
53
    /**
54
     * CSV read delimiter
55
     * @var string
56
     */
57
    protected $delimiter = ';';
58
    /**
59
     * CSV read enclosure
60
     * @var string
61
     */
62
    protected $enclosure = '"';
63
    /**
64
     * CSV read escape character
65
     * @var string
66
     */
67
    protected $escape = '\\';
68
    /**
69
     * CSV file handle
70
     * @var string
71
     */
72
    protected $fh;
73
    /**
74
     * Notification handler.
75
     * Should be a callable that takes a single string argument
76
     * @var \callable
77
     */
78
    protected $notifycallback;
79
    /**
80
     * User exclusion list.
81
     * This is a list of userids that are excluded from processing.
82
     * @var array list of userids
83
     */
84
    protected $exclusionlist;
85
    /**
86
     * Set test run.
87
     * @var bool
88
     */
89
    protected $testmode = false;
90
    /**
91
     * Set run mode.
92
     * @var bool
93
     */
94
    protected $mode = 1;
95
 
96
    /**
97
     * Return CSV filename
98
     *
99
     * @return string
100
     */
101
    public function get_file() {
102
        return $this->file;
103
    }
104
 
105
    /**
106
     * Return CSV read delimiter
107
     *
108
     * @return string
109
     */
110
    public function get_delimiter() {
111
        return $this->delimiter;
112
    }
113
 
114
    /**
115
     * Return CSV read enclosure
116
     *
117
     * @return string
118
     */
119
    public function get_enclosure() {
120
        return $this->enclosure;
121
    }
122
 
123
    /**
124
     * Return CSV read escape character
125
     *
126
     * @return string
127
     */
128
    public function get_escape() {
129
        return $this->escape;
130
    }
131
 
132
    /**
133
     * Set CSV filename
134
     *
135
     * @param string $file filename
136
     * @return \tool_usersuspension\processor\csv
137
     */
138
    public function set_file($file) {
139
        $this->file = $file;
140
        return $this;
141
    }
142
 
143
    /**
144
     * Set CSV column delimiter
145
     *
146
     * @param string $delimiter CSV column delimiter
147
     * @return \tool_usersuspension\processor\csv
148
     */
149
    public function set_delimiter($delimiter) {
150
        $this->delimiter = $delimiter;
151
        return $this;
152
    }
153
 
154
    /**
155
     * Set CSV enclosure
156
     *
157
     * @param string $enclosure CSV enclosure
158
     * @return \tool_usersuspension\processor\csv
159
     */
160
    public function set_enclosure($enclosure) {
161
        $this->enclosure = $enclosure;
162
        return $this;
163
    }
164
 
165
    /**
166
     * Set CSV escape character
167
     *
168
     * @param string $escape CSV escape character
169
     * @return \tool_usersuspension\processor\csv
170
     */
171
    public function set_escape($escape) {
172
        $this->escape = $escape;
173
        return $this;
174
    }
175
 
176
    /**
177
     * Set callback function for messages, e.g. 'mtrace'.
178
     * Should take a single string argument denoting the message.
179
     *
180
     * @param \callable $notifycallback
181
     * @return \tool_usersuspension\processor\csv
182
     */
183
    public function set_notifycallback($notifycallback) {
184
        if (is_callable($notifycallback)) {
185
            $this->notifycallback = $notifycallback;
186
        }
187
        return $this;
188
    }
189
 
190
    /**
191
     * Do we suspend at all (have a test run)?
192
     * @return bool
193
     */
194
    public function get_testmode() {
195
        return $this->testmode;
196
    }
197
 
198
    /**
199
     * Set no-suspend mode (test mode)
200
     *
201
     * @param bool $testmode
202
     * @return $this
203
     */
204
    public function set_testmode($testmode = true) {
205
        $this->testmode = $testmode;
206
        return $this;
207
    }
208
 
209
    /**
210
     * Get run mode
211
     * @return int
212
     */
213
    public function get_mode() {
214
        return $this->mode;
215
    }
216
 
217
    /**
218
     * Get run mode
219
     * @param int $mode
220
     * @return $this
221
     */
222
    public function set_mode($mode) {
223
        $this->mode = $mode;
224
        return $this;
225
    }
226
 
227
    /**
228
     * Create a new instance of a CSV processor
229
     */
230
    public function __construct() {
231
        $this->fh = null;
232
    }
233
 
234
    /**
235
     * Pass on a message to the notifycallback, if applicable
236
     *
237
     * @param string $msg
238
     */
239
    protected function notify($msg) {
240
        if (isset($this->notifycallback) && $this->notifycallback !== null) {
241
            call_user_func($this->notifycallback, $msg);
242
        }
243
    }
244
 
245
    /**
246
     * Process the CSV file
247
     *
248
     * @return void
249
     */
250
    public function process() {
251
        // Load exclusions.
252
        $this->notify(get_string('notify:load-exclude-list', 'tool_usersuspension'));
253
        $this->exclusionlist = \tool_usersuspension\util::get_user_exclusion_list();
254
        $this->notify(get_string('notify:load-file', 'tool_usersuspension', $this->file));
255
        $this->fh = fopen($this->file, 'r+b');
256
        if ($this->fh === false) {
257
            $this->notify(get_string('notify:load-file-fail', 'tool_usersuspension', $this->file));
258
            return;
259
        }
260
        while (true && !feof($this->fh)) {
261
            $line = fgetcsv($this->fh, 0, $this->delimiter, $this->enclosure, $this->escape);
262
            if ($line !== null && $line !== false) {
263
                if ($line[0] != 'type') {
264
                    $this->p_process_line($line);
265
                }
266
            }
267
        }
268
        fclose($this->fh);
269
    }
270
 
271
    /**
272
     * Process a single CSV line
273
     *
274
     * @param array $line array representing the read CSV line
275
     * @return bool true if successfully processed, false otherwise
276
     */
277
    protected function p_process_line($line) {
278
        if (count($line) === 1) {
279
            // Assumes email.
280
            $line = array('email', $line[0]);
281
        }
282
        // Continue normal processing. Note that we CLEAN the params.
283
        $rs = false;
284
        $type = clean_param(trim($line[0]), PARAM_ALPHA);
285
        switch ($type) {
286
            case 'email':
287
                $email = clean_param(trim($line[1]), PARAM_EMAIL);
288
                if ($this->mode == static::MODE_SUSPEND) {
289
                    $rs = $this->p_suspend_user(array('email' => $email));
290
                } else {
291
                    $rs = $this->p_unsuspend_user(array('email' => $email));
292
                }
293
                break;
294
            case 'idnumber':
295
                $idnumber = clean_param(trim($line[1]), PARAM_NOTAGS);
296
                if ($this->mode == static::MODE_SUSPEND) {
297
                    $rs = $this->p_suspend_user(array('idnumber' => $idnumber));
298
                } else {
299
                    $rs = $this->p_unsuspend_user(array('idnumber' => $idnumber));
300
                }
301
                break;
302
            case 'username':
303
                $username = clean_param(trim($line[1]), PARAM_USERNAME);
304
                if ($this->mode == static::MODE_SUSPEND) {
305
                    $rs = $this->p_suspend_user(array('username' => $username));
306
                } else {
307
                    $rs = $this->p_unsuspend_user(array('username' => $username));
308
                }
309
                break;
310
            default:
311
                $this->notify(get_string('notify:unknown-suspend-type', 'tool_usersuspension', $type));
312
                break;
313
        }
314
        return $rs;
315
    }
316
 
317
    /**
318
     * Performs the user suspension.
319
     * The user, when found, is checked against the exclusion list to determine
320
     * if he/she shouldn't be suspended.
321
     *
322
     * @param array $params general parameters to use in the query to lookup a record from the database
323
     * @return bool true if successfully suspended, false otherwise
324
     */
325
    protected function p_suspend_user($params) {
326
        global $CFG, $DB;
327
        $this->notify(__METHOD__ . ": " . key($params) . ' = ' . current($params));
328
        $params['mnethostid'] = $CFG->mnet_localhost_id;
329
        $params['deleted'] = 0;
330
        $params['suspended'] = 0;
331
        $user = $DB->get_record('user', $params);
332
        if (!empty($user)) {
333
            if (in_array($user->id, $this->exclusionlist)) {
334
                $this->notify(get_string('notify:suspend-excluded-user', 'tool_usersuspension', $user));
335
                return;
336
            }
337
            $this->notify(get_string('notify:suspend-user', 'tool_usersuspension', $user));
338
            // Suspend this user.
339
            if ($this->testmode) {
340
                $result = true;
341
                $this->notify(get_string('msg:user:suspend:nosuspendmode', 'tool_usersuspension', $user));
342
                return $result;
343
            }
344
            $result = \tool_usersuspension\util::do_suspend_user($user);
345
            if ($result === true) {
346
                $this->notify(get_string('msg:user:suspend:success', 'tool_usersuspension', $user));
347
            } else {
348
                $this->notify(get_string('msg:user:suspend:failed', 'tool_usersuspension', $user));
349
            }
350
            return $result;
351
        }
352
        $this->notify("\t" . get_string('msg:user:not-found', 'tool_usersuspension'));
353
        return false;
354
    }
355
 
356
    /**
357
     * Performs the user suspension.
358
     * The user, when found, is checked against the exclusion list to determine
359
     * if he/she shouldn't be suspended.
360
     *
361
     * @param array $params general parameters to use in the query to lookup a record from the database
362
     * @return bool true if successfully suspended, false otherwise
363
     */
364
    protected function p_unsuspend_user($params) {
365
        global $CFG, $DB;
366
        $this->notify(__METHOD__ . ": " . key($params) . ' = ' . current($params));
367
        $params['mnethostid'] = $CFG->mnet_localhost_id;
368
        $params['deleted'] = 0;
369
        $params['suspended'] = 1;
370
        $user = $DB->get_record('user', $params);
371
        if (!empty($user)) {
372
            if (in_array($user->id, $this->exclusionlist)) {
373
                $this->notify(get_string('notify:suspend-excluded-user', 'tool_usersuspension', $user));
374
                return;
375
            }
376
            $this->notify(get_string('notify:suspend-user', 'tool_usersuspension', $user));
377
            // Suspend this user.
378
            if ($this->testmode) {
379
                $result = true;
380
                $this->notify(get_string('msg:user:suspend:nosuspendmode', 'tool_usersuspension', $user));
381
                return $result;
382
            }
383
            $result = \tool_usersuspension\util::do_unsuspend_user($user);
384
            if ($result === true) {
385
                $this->notify(get_string('msg:user:unsuspend:success', 'tool_usersuspension', $user));
386
            } else {
387
                $this->notify(get_string('msg:user:unsuspend:failed', 'tool_usersuspension', $user));
388
            }
389
            return $result;
390
        }
391
        $this->notify("\t" . get_string('msg:user:not-found', 'tool_usersuspension'));
392
        return false;
393
    }
394
 
395
}