-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
231 lines (189 loc) · 8.37 KB
/
import.php
File metadata and controls
231 lines (189 loc) · 8.37 KB
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
29
30
31
32
33
34
35
36
37
38
39
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/importexportfields.php');
require_once($CFG->libdir.'/formslib.php');
define('STATE_WAITSTART', 0);
define('STATE_INQUOTES', 1);
define('STATE_ESCAPE', 2);
define('STATE_NORMAL', 3);
$id = required_param('id', PARAM_INT); // course module id
if (! $cm = get_coursemodule_from_id('checkskill', $id)) {
print_error(get_string('error_cmid', 'checkskill')); // 'Course Module ID was incorrect'
}
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$checkskill = $DB->get_record('checkskill', array('id' => $cm->instance), '*', MUST_EXIST);
$url = new moodle_url('/mod/checkskill/import.php', array('id' => $cm->id));
$PAGE->set_url($url);
require_login($course, true, $cm);
if ($CFG->version < 2011120100) {
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
} else {
$context = context_module::instance($cm->id);
}
if (!has_capability('mod/checkskill:edit', $context)) {
print_error(get_string('error_import_items', 'checkskill')); // 'You do not have permission to import items to this checkskill');
}
$returl = new moodle_url('/mod/checkskill/edit.php', array('id' => $cm->id));
class checkskill_import_form extends moodleform {
function definition() {
$mform = $this->_form;
$mform->addElement('hidden', 'id', 0);
$mform->setType('id', PARAM_INT);
$mform->addElement('header', 'formheading', get_string('import', 'checkskill'));
$mform->addElement('filepicker', 'importfile', get_string('importfile', 'checkskill'), null, array('accepted_types'=>array('*.csv')));
$this->add_action_buttons(true, get_string('import', 'checkskill'));
}
}
function cleanrow($separator, $row) {
// Convert and $separator inside quotes into [!SEPARATOR!] (to skip it during the 'explode')
/*
ITEMS CSV FILE
Separator ','
Item text,Indent,Type (0 - normal; 1 - optional; 2 - heading),Due Time (timestamp),Colour (red; orange; green; purple; black)
Webcam,0,0,2,blue
Choisir une WebCam,1,0,0,black
Installer une Webcam,1,0,0,black
Participer à une Webconférence,1,0,0,black
Organiser et piloter une Webconférence,2,0,0,red
*/
$state = STATE_WAITSTART;
$chars = str_split($row);
$cleanrow = '';
$quotes = '"';
foreach ($chars as $char) {
switch ($state) {
case STATE_WAITSTART:
if ($char == ' ' || $char == ',') { } // Still in STATE_WAITSTART
else if ($char == '"') { $quotes = '"'; $state = STATE_INQUOTES; }
else if ($char == "'") { $quotes = "'"; $state = STATE_INQUOTES; }
else { $state = STATE_NORMAL; }
break;
case STATE_INQUOTES:
if ($char == $quotes) { $state = STATE_NORMAL; } // End of quotes
else if ($char == '\\') { $state = STATE_ESCAPE; continue 2; } // Possible escaped quotes skip (for now)
else if ($char == $separator) { $cleanrow .= '[!SEPARATOR!]'; continue 2; } // Replace $separator and continue loop
break;
case STATE_ESCAPE:
// Retain escape char, unless escaping a quote character
if ($char != $quotes) { $cleanrow .= '\\'; }
$state = STATE_INQUOTES;
break;
default:
if ($char == ',') { $state = STATE_WAITSTART; }
break;
}
$cleanrow .= $char;
}
return $cleanrow;
}
$form = new checkskill_import_form();
$defaults = new stdClass;
$defaults->id = $cm->id;
$form->set_data($defaults);
if ($form->is_cancelled()) {
redirect($returl);
}
$errormsg = '';
if ($data = $form->get_data()) {
$filename = $form->save_temp_file('importfile');
if (!file_exists($filename)) {
$errormsg = get_string('error_file_upload', 'checkskill');// "Something went wrong with the file upload";
} else {
if (is_readable($filename)) {
$filearray = file($filename);
unlink($filename);
/// Check for Macintosh OS line returns (ie file on one line), and fix
if (preg_match("/\r/", $filearray[0]) AND !preg_match("/\n/", $filearray[0])) {
$filearray = explode("\r", $filearray[0]);
}
$skipheading = true;
$ok = true;
$position = $DB->count_records('checkskill_item', array('checkskill' => $checkskill->id, 'userid' => 0)) + 1;
foreach ($filearray as $row) {
if ($skipheading) {
$skipheading = false;
continue;
}
// Separator defined in importexportfields.php (currently ',')
// Split $row into array $item, by $separator, but ignore $separator when it occurs within ""
$row = cleanrow($separator, $row);
$item = explode($separator, $row);
if (count($item) != count($fields)) {
$errormsg = get_string('error_number_columns', 'checkskill', $row);// "Row has incorrect number of columns in it:<br />$row";
$ok = false;
break;
}
$itemfield = reset($item);
$newitem = new stdClass;
$newitem->checkskill = $checkskill->id;
$newitem->position = $position++;
$newitem->userid = 0;
// $fields defined in importexportfields.php
foreach ($fields as $field => $fieldtext) {
$itemfield = trim($itemfield);
if (substr($itemfield, 0, 1) == '"' && substr($itemfield, -1) == '"') {
$itemfield = substr($itemfield, 1, -1);
}
$itemfield = trim($itemfield);
$itemfield = str_replace('[!SEPARATOR!]', $separator, $itemfield);
switch ($field) {
case 'displaytext':
$newitem->displaytext = trim($itemfield);
break;
case 'indent':
$newitem->indent = intval($itemfield);
if ($newitem->indent < 0) {
$newitem->indent = 0;
} else if ($newitem->indent > 10) {
$newitem->indent = 10;
}
break;
case 'itemoptional':
$newitem->itemoptional = intval($itemfield);
if ($newitem->itemoptional < 0 || $newitem->itemoptional > 2) {
$newitem->itemoptional = 0;
}
break;
case 'duetime':
$newitem->duetime = intval($itemfield);
if ($newitem->itemoptional < 0) {
$newitem->itemoptional = 0;
}
break;
case 'colour':
$allowedcolours = array('red', 'orange', 'green', 'purple', 'black');
$itemfield = trim(strtolower($itemfield));
if (!in_array($itemfield, $allowedcolours)) {
$itemfield = 'black';
}
$newitem->colour = $itemfield;
break;
}
$itemfield = next($item);
}
if ($newitem->displaytext) { // Don't insert items without any text in them
if (!$DB->insert_record('checkskill_item', $newitem)) {
$ok = false;
$errormsg = get_string('error_insert_db', 'checkskill'); // 'Unable to insert DB record for item';
break;
}
}
}
if ($ok) {
redirect($returl);
}
} else {
$errormsg = get_string('error_file_upload', 'checkskill'); // "Something went wrong with the file upload";
}
}
}
$strcheckskill = get_string('modulename', 'checkskill');
$pagetitle = strip_tags($course->shortname.': '.$strcheckskill.': '.format_string($checkskill->name, true));
$PAGE->set_title($pagetitle);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
if ($errormsg) {
echo '<p class="error">'.$errormsg.'</p>';
}
$form->display();
echo $OUTPUT->footer();