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 the CSV upload form class
|
|
|
19 |
*
|
|
|
20 |
* File upload.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\forms;
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die;
|
|
|
29 |
|
|
|
30 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* tool_usersuspension\forms\upload
|
|
|
34 |
*
|
|
|
35 |
* @package tool_usersuspension
|
|
|
36 |
*
|
|
|
37 |
* @copyright Sebsoft.nl
|
|
|
38 |
* @author R.J. van Dongen <rogier@sebsoft.nl>
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class upload extends \moodleform {
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* form definition
|
|
|
45 |
*/
|
|
|
46 |
public function definition() {
|
|
|
47 |
global $CFG;
|
|
|
48 |
|
|
|
49 |
$mform = $this->_form;
|
|
|
50 |
|
|
|
51 |
// Example CSV.
|
|
|
52 |
$urldownloadcsv = new \moodle_url($CFG->wwwroot . '/admin/tool/usersuspension/assets/example.csv');
|
|
|
53 |
$link = '<a href="' . $urldownloadcsv
|
|
|
54 |
. '" target="_blank">' . get_string('download-sample-csv', 'tool_usersuspension') . '</a>';
|
|
|
55 |
|
|
|
56 |
$mform->addElement('static', 'uploadfiledesc', '',
|
|
|
57 |
get_string('form:static:uploadfile:desc', 'tool_usersuspension') . $link);
|
|
|
58 |
$maxbytes = 1048576; // 1 MB.
|
|
|
59 |
$mform->addElement('filepicker', 'userfile', get_string('file'), null,
|
|
|
60 |
array('maxbytes' => $maxbytes, 'accepted_types' => array('.csv')));
|
|
|
61 |
|
|
|
62 |
$delims = array(';' => ';', '|' => '|', ',' => ',');
|
|
|
63 |
$select1 = $mform->addElement('select', 'delimiter', get_string('csv:delimiter', 'tool_usersuspension'), $delims);
|
|
|
64 |
$mform->setType('delimiter', PARAM_TEXT);
|
|
|
65 |
$select1->setSelected(';');
|
|
|
66 |
|
|
|
67 |
$enclosures = array('"' => '"', "'" => "'");
|
|
|
68 |
$select2 = $mform->addElement('select', 'enclosure', get_string('csv:enclosure', 'tool_usersuspension'), $enclosures);
|
|
|
69 |
$mform->setType('enclosure', PARAM_TEXT);
|
|
|
70 |
$select2->setSelected('"');
|
|
|
71 |
|
|
|
72 |
$options = array(
|
|
|
73 |
\tool_usersuspension\processor\csv::MODE_SUSPEND => get_string('suspend', 'tool_usersuspension'),
|
|
|
74 |
\tool_usersuspension\processor\csv::MODE_UNSUSPEND => get_string('unsuspend', 'tool_usersuspension'),
|
|
|
75 |
);
|
|
|
76 |
$mform->addElement('select', 'suspendmode', get_string('suspendmode', 'tool_usersuspension'), $options);
|
|
|
77 |
$mform->setType('suspendmode', PARAM_INT);
|
|
|
78 |
$mform->setDefault('suspendmode', \tool_usersuspension\processor\csv::MODE_SUSPEND);
|
|
|
79 |
|
|
|
80 |
$this->add_action_buttons(true, get_string('csv:upload:continue', 'tool_usersuspension'));
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Process the posted form
|
|
|
85 |
*
|
|
|
86 |
* @throws \moodle_exception
|
|
|
87 |
*/
|
|
|
88 |
public function process() {
|
|
|
89 |
global $CFG;
|
|
|
90 |
$data = $this->get_data();
|
|
|
91 |
if ($data === null) {
|
|
|
92 |
return false;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$fn = $this->get_new_filename('userfile');
|
|
|
96 |
$file = $CFG->tempdir . '/' . $fn;
|
|
|
97 |
if (file_exists($file)) {
|
|
|
98 |
@unlink($file);
|
|
|
99 |
}
|
|
|
100 |
$uploadedfile = $this->save_file('userfile', $file);
|
|
|
101 |
if (!$uploadedfile) {
|
|
|
102 |
\tool_usersuspension\util::print_notification(get_string('msg:file:upload:fail',
|
|
|
103 |
'tool_usersuspension'), 'error');
|
|
|
104 |
return;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Process upload.
|
|
|
108 |
$proc = new \tool_usersuspension\processor\csv();
|
|
|
109 |
$proc->set_file($file);
|
|
|
110 |
$proc->set_delimiter($data->delimiter);
|
|
|
111 |
$proc->set_enclosure($data->enclosure);
|
|
|
112 |
$proc->set_notifycallback('mtrace');
|
|
|
113 |
$proc->set_mode($data->suspendmode);
|
|
|
114 |
echo "<pre>";
|
|
|
115 |
$proc->process();
|
|
|
116 |
echo "</pre>";
|
|
|
117 |
// Delete uploaded file.
|
|
|
118 |
unlink($file);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
}
|