MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / cohort / upload_form.php
CommitLineData
5076b961
MG
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 * A form for cohort upload.
19 *
20 * @package core_cohort
21 * @copyright 2014 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->libdir.'/formslib.php');
28
29/**
30 * Cohort upload form class
31 *
32 * @package core_cohort
33 * @copyright 2014 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class cohort_upload_form extends moodleform {
37 /** @var array new cohorts that need to be created */
38 public $processeddata = null;
39 /** @var array cached list of available contexts */
40 protected $contextoptions = null;
41 /** @var array temporary cache for retrieved categories */
42 protected $categoriescache = array();
43
44 /**
45 * Form definition
46 */
47 public function definition() {
48 $mform = $this->_form;
49 $data = (object)$this->_customdata;
50
51 $mform->addElement('hidden', 'returnurl');
52 $mform->setType('returnurl', PARAM_URL);
53
54 $mform->addElement('header', 'cohortfileuploadform', get_string('uploadafile'));
55
56 $filepickeroptions = array();
57 $filepickeroptions['filetypes'] = '*';
58 $filepickeroptions['maxbytes'] = get_max_upload_file_size();
59 $mform->addElement('filepicker', 'cohortfile', get_string('file'), null, $filepickeroptions);
60
61 $choices = csv_import_reader::get_delimiter_list();
62 $mform->addElement('select', 'delimiter', get_string('csvdelimiter', 'tool_uploadcourse'), $choices);
63 if (array_key_exists('cfg', $choices)) {
64 $mform->setDefault('delimiter', 'cfg');
65 } else if (get_string('listsep', 'langconfig') == ';') {
66 $mform->setDefault('delimiter', 'semicolon');
67 } else {
68 $mform->setDefault('delimiter', 'comma');
69 }
70 $mform->addHelpButton('delimiter', 'csvdelimiter', 'tool_uploadcourse');
71
72 $choices = core_text::get_encodings();
73 $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploadcourse'), $choices);
74 $mform->setDefault('encoding', 'UTF-8');
75 $mform->addHelpButton('encoding', 'encoding', 'tool_uploadcourse');
76
77 $options = $this->get_context_options();
78 $mform->addElement('select', 'contextid', get_string('defaultcontext', 'cohort'), $options);
79
80 $this->add_cohort_upload_buttons(true);
81 $this->set_data($data);
82 }
83
84 /**
85 * Add buttons to the form ("Upload cohorts", "Preview", "Cancel")
86 */
87 protected function add_cohort_upload_buttons() {
88 $mform = $this->_form;
89
90 $buttonarray = array();
91
92 $submitlabel = get_string('uploadcohorts', 'cohort');
93 $buttonarray[] = $mform->createElement('submit', 'submitbutton', $submitlabel);
94
95 $previewlabel = get_string('preview', 'cohort');
96 $buttonarray[] = $mform->createElement('submit', 'previewbutton', $previewlabel);
97 $mform->registerNoSubmitButton('previewbutton');
98
99 $buttonarray[] = $mform->createElement('cancel');
100
101 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
102 $mform->closeHeaderBefore('buttonar');
103 }
104
105 /**
106 * Process the uploaded file and allow the submit button only if it doest not have errors.
107 */
108 public function definition_after_data() {
109 $mform = $this->_form;
110 $cohortfile = $mform->getElementValue('cohortfile');
111 $allowsubmitform = false;
112 if ($cohortfile && ($file = $this->get_cohort_file($cohortfile))) {
113 // File was uploaded. Parse it.
114 $encoding = $mform->getElementValue('encoding')[0];
115 $delimiter = $mform->getElementValue('delimiter')[0];
116 $contextid = $mform->getElementValue('contextid')[0];
117 if (!empty($contextid) && ($context = context::instance_by_id($contextid, IGNORE_MISSING))) {
118 $this->processeddata = $this->process_upload_file($file, $encoding, $delimiter, $context);
119 if ($this->processeddata && count($this->processeddata) > 1 && !$this->processeddata[0]['errors']) {
120 $allowsubmitform = true;
121 }
122 }
123 }
124 if (!$allowsubmitform) {
125 // Hide submit button.
126 $el = $mform->getElement('buttonar')->getElements()[0];
127 $el->setValue('');
128 $el->freeze();
129 } else {
130 $mform->setExpanded('cohortfileuploadform', false);
131 }
132
133 }
134
135 /**
136 * Returns the list of contexts where current user can create cohorts.
137 *
138 * @return array
139 */
140 protected function get_context_options() {
5076b961
MG
141 if ($this->contextoptions === null) {
142 $this->contextoptions = array();
442f12f8 143 $displaylist = core_course_category::make_categories_list('moodle/cohort:manage');
5076b961
MG
144 // We need to index the options array by context id instead of category id and add option for system context.
145 $syscontext = context_system::instance();
146 if (has_capability('moodle/cohort:manage', $syscontext)) {
147 $this->contextoptions[$syscontext->id] = $syscontext->get_context_name();
148 }
149 foreach ($displaylist as $cid => $name) {
150 $context = context_coursecat::instance($cid);
151 $this->contextoptions[$context->id] = $name;
152 }
153 }
154 return $this->contextoptions;
155 }
156
157 public function validation($data, $files) {
158 $errors = parent::validation($data, $files);
159 if (empty($errors)) {
160 if (empty($data['cohortfile']) || !($file = $this->get_cohort_file($data['cohortfile']))) {
161 $errors['cohortfile'] = get_string('required');
162 } else {
163 if (!empty($this->processeddata[0]['errors'])) {
164 // Any value in $errors will notify that validation did not pass. The detailed errors will be shown in preview.
165 $errors['dummy'] = '';
166 }
167 }
168 }
169 return $errors;
170 }
171
172 /**
173 * Returns the uploaded file if it is present.
174 *
175 * @param int $draftid
176 * @return stored_file|null
177 */
178 protected function get_cohort_file($draftid) {
179 global $USER;
180 // We can not use moodleform::get_file_content() method because we need the content before the form is validated.
181 if (!$draftid) {
182 return null;
183 }
184 $fs = get_file_storage();
185 $context = context_user::instance($USER->id);
186 if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
187 return null;
188 }
189 $file = reset($files);
190
191 return $file;
192
193 }
194
195 /**
196 * Returns the list of prepared objects to be added as cohorts
197 *
198 * @return array of stdClass objects, each can be passed to {@link cohort_add_cohort()}
199 */
200 public function get_cohorts_data() {
201 $cohorts = array();
202 if ($this->processeddata) {
203 foreach ($this->processeddata as $idx => $line) {
204 if ($idx && !empty($line['data'])) {
205 $cohorts[] = (object)$line['data'];
206 }
207 }
208 }
209 return $cohorts;
210 }
211
212 /**
213 * Displays the preview of the uploaded file
214 */
215 protected function preview_uploaded_cohorts() {
216 global $OUTPUT;
217 if (empty($this->processeddata)) {
218 return;
219 }
220 foreach ($this->processeddata[0]['errors'] as $error) {
221 echo $OUTPUT->notification($error);
222 }
223 foreach ($this->processeddata[0]['warnings'] as $warning) {
224 echo $OUTPUT->notification($warning, 'notifymessage');
225 }
226 $table = new html_table();
227 $table->id = 'previewuploadedcohorts';
228 $columns = $this->processeddata[0]['data'];
229 $columns['contextid'] = get_string('context', 'role');
230
231 // Add column names to the preview table.
232 $table->head = array('');
233 foreach ($columns as $key => $value) {
234 $table->head[] = $value;
235 }
236 $table->head[] = get_string('status');
237
238 // Add (some) rows to the preview table.
239 $previewdrows = $this->get_previewed_rows();
240 foreach ($previewdrows as $idx) {
241 $line = $this->processeddata[$idx];
242 $cells = array(new html_table_cell($idx));
243 $context = context::instance_by_id($line['data']['contextid']);
244 foreach ($columns as $key => $value) {
245 if ($key === 'contextid') {
246 $text = html_writer::link(new moodle_url('/cohort/index.php', array('contextid' => $context->id)),
247 $context->get_context_name(false));
248 } else {
249 $text = s($line['data'][$key]);
250 }
251 $cells[] = new html_table_cell($text);
252 }
253 $text = '';
254 if ($line['errors']) {
255 $text .= html_writer::div(join('<br>', $line['errors']), 'notifyproblem');
256 }
257 if ($line['warnings']) {
258 $text .= html_writer::div(join('<br>', $line['warnings']));
259 }
260 $cells[] = new html_table_cell($text);
261 $table->data[] = new html_table_row($cells);
262 }
263 if ($notdisplayed = count($this->processeddata) - count($previewdrows) - 1) {
264 $cell = new html_table_cell(get_string('displayedrows', 'cohort',
265 (object)array('displayed' => count($previewdrows), 'total' => count($this->processeddata) - 1)));
266 $cell->colspan = count($columns) + 2;
267 $table->data[] = new html_table_row(array($cell));
268 }
269 echo html_writer::table($table);
270 }
271
272 /**
273 * Find up rows to show in preview
274 *
275 * Number of previewed rows is limited but rows with errors and warnings have priority.
276 *
277 * @return array
278 */
279 protected function get_previewed_rows() {
280 $previewlimit = 10;
281 if (count($this->processeddata) <= 1) {
282 $rows = array();
283 } else if (count($this->processeddata) < $previewlimit + 1) {
284 // Return all rows.
285 $rows = range(1, count($this->processeddata) - 1);
286 } else {
287 // First find rows with errors and warnings (no more than 10 of each).
288 $errorrows = $warningrows = array();
289 foreach ($this->processeddata as $rownum => $line) {
290 if ($rownum && $line['errors']) {
291 $errorrows[] = $rownum;
292 if (count($errorrows) >= $previewlimit) {
293 return $errorrows;
294 }
295 } else if ($rownum && $line['warnings']) {
296 if (count($warningrows) + count($errorrows) < $previewlimit) {
297 $warningrows[] = $rownum;
298 }
299 }
300 }
301 // Include as many error rows as possible and top them up with warning rows.
302 $rows = array_merge($errorrows, array_slice($warningrows, 0, $previewlimit - count($errorrows)));
303 // Keep adding good rows until we reach limit.
304 for ($rownum = 1; count($rows) < $previewlimit; $rownum++) {
305 if (!in_array($rownum, $rows)) {
306 $rows[] = $rownum;
307 }
308 }
309 asort($rows);
310 }
311 return $rows;
312 }
313
314 public function display() {
315 // Finalize the form definition if not yet done.
316 if (!$this->_definition_finalized) {
317 $this->_definition_finalized = true;
318 $this->definition_after_data();
319 }
320
321 // Difference from the parent display() method is that we want to show preview above the form if applicable.
322 $this->preview_uploaded_cohorts();
323
324 $this->_form->display();
325 }
326
327 /**
328 * @param stored_file $file
329 * @param string $encoding
330 * @param string $delimiter
331 * @param context $defaultcontext
332 * @return array
333 */
334 protected function process_upload_file($file, $encoding, $delimiter, $defaultcontext) {
335 global $CFG, $DB;
336 require_once($CFG->libdir . '/csvlib.class.php');
337
338 $cohorts = array(
339 0 => array('errors' => array(), 'warnings' => array(), 'data' => array())
340 );
341
342 // Read and parse the CSV file using csv library.
343 $content = $file->get_content();
344 if (!$content) {
345 $cohorts[0]['errors'][] = new lang_string('csvemptyfile', 'error');
346 return $cohorts;
347 }
348
349 $uploadid = csv_import_reader::get_new_iid('uploadcohort');
350 $cir = new csv_import_reader($uploadid, 'uploadcohort');
351 $readcount = $cir->load_csv_content($content, $encoding, $delimiter);
352 unset($content);
353 if (!$readcount) {
354 $cohorts[0]['errors'][] = get_string('csvloaderror', 'error', $cir->get_error());
355 return $cohorts;
356 }
357 $columns = $cir->get_columns();
358
359 // Check that columns include 'name' and warn about extra columns.
88cb8b78 360 $allowedcolumns = array('contextid', 'name', 'idnumber', 'description', 'descriptionformat', 'visible', 'theme');
5076b961
MG
361 $additionalcolumns = array('context', 'category', 'category_id', 'category_idnumber', 'category_path');
362 $displaycolumns = array();
363 $extracolumns = array();
364 $columnsmapping = array();
365 foreach ($columns as $i => $columnname) {
366 $columnnamelower = preg_replace('/ /', '', core_text::strtolower($columnname));
367 $columnsmapping[$i] = null;
368 if (in_array($columnnamelower, $allowedcolumns)) {
369 $displaycolumns[$columnnamelower] = $columnname;
370 $columnsmapping[$i] = $columnnamelower;
371 } else if (in_array($columnnamelower, $additionalcolumns)) {
372 $columnsmapping[$i] = $columnnamelower;
373 } else {
374 $extracolumns[] = $columnname;
375 }
376 }
377 if (!in_array('name', $columnsmapping)) {
378 $cohorts[0]['errors'][] = new lang_string('namecolumnmissing', 'cohort');
379 return $cohorts;
380 }
381 if ($extracolumns) {
382 $cohorts[0]['warnings'][] = new lang_string('csvextracolumns', 'cohort', s(join(', ', $extracolumns)));
383 }
384
385 if (!isset($displaycolumns['contextid'])) {
386 $displaycolumns['contextid'] = 'contextid';
387 }
388 $cohorts[0]['data'] = $displaycolumns;
389
390 // Parse data rows.
391 $cir->init();
392 $rownum = 0;
393 $idnumbers = array();
394 $haserrors = false;
395 $haswarnings = false;
396 while ($row = $cir->next()) {
397 $rownum++;
398 $cohorts[$rownum] = array(
399 'errors' => array(),
400 'warnings' => array(),
401 'data' => array(),
402 );
403 $hash = array();
404 foreach ($row as $i => $value) {
405 if ($columnsmapping[$i]) {
406 $hash[$columnsmapping[$i]] = $value;
407 }
408 }
409 $this->clean_cohort_data($hash);
410
411 $warnings = $this->resolve_context($hash, $defaultcontext);
412 $cohorts[$rownum]['warnings'] = array_merge($cohorts[$rownum]['warnings'], $warnings);
413
414 if (!empty($hash['idnumber'])) {
415 if (isset($idnumbers[$hash['idnumber']]) || $DB->record_exists('cohort', array('idnumber' => $hash['idnumber']))) {
416 $cohorts[$rownum]['errors'][] = new lang_string('duplicateidnumber', 'cohort');
417 }
418 $idnumbers[$hash['idnumber']] = true;
419 }
420
421 if (empty($hash['name'])) {
422 $cohorts[$rownum]['errors'][] = new lang_string('namefieldempty', 'cohort');
423 }
424
88cb8b78
SA
425 if (!empty($hash['theme']) && !empty($CFG->allowcohortthemes)) {
426 $availablethemes = cohort_get_list_of_themes();
427 if (empty($availablethemes[$hash['theme']])) {
428 $cohorts[$rownum]['errors'][] = new lang_string('invalidtheme', 'cohort');
429 }
430 }
431
5076b961
MG
432 $cohorts[$rownum]['data'] = array_intersect_key($hash, $cohorts[0]['data']);
433 $haserrors = $haserrors || !empty($cohorts[$rownum]['errors']);
434 $haswarnings = $haswarnings || !empty($cohorts[$rownum]['warnings']);
435 }
436
437 if ($haserrors) {
438 $cohorts[0]['errors'][] = new lang_string('csvcontainserrors', 'cohort');
439 }
440
441 if ($haswarnings) {
442 $cohorts[0]['warnings'][] = new lang_string('csvcontainswarnings', 'cohort');
443 }
444
445 return $cohorts;
446 }
447
448 /**
449 * Cleans input data about one cohort.
450 *
451 * @param array $hash
452 */
453 protected function clean_cohort_data(&$hash) {
454 foreach ($hash as $key => $value) {
455 switch ($key) {
456 case 'contextid': $hash[$key] = clean_param($value, PARAM_INT); break;
457 case 'name': $hash[$key] = core_text::substr(clean_param($value, PARAM_TEXT), 0, 254); break;
458 case 'idnumber': $hash[$key] = core_text::substr(clean_param($value, PARAM_RAW), 0, 254); break;
459 case 'description': $hash[$key] = clean_param($value, PARAM_RAW); break;
460 case 'descriptionformat': $hash[$key] = clean_param($value, PARAM_INT); break;
fbb250f8
MG
461 case 'visible':
462 $tempstr = trim(core_text::strtolower($value));
463 if ($tempstr === '') {
464 // Empty string is treated as "YES" (the default value for cohort visibility).
465 $hash[$key] = 1;
466 } else {
467 if ($tempstr === core_text::strtolower(get_string('no')) || $tempstr === 'n') {
468 // Special treatment for 'no' string that is not included in clean_param().
469 $value = 0;
470 }
471 $hash[$key] = clean_param($value, PARAM_BOOL) ? 1 : 0;
472 }
473 break;
88cb8b78
SA
474 case 'theme':
475 $hash[$key] = core_text::substr(clean_param($value, PARAM_TEXT), 0, 50);
476 break;
5076b961
MG
477 }
478 }
479 }
480
481 /**
482 * Determines in which context the particular cohort will be created
483 *
484 * @param array $hash
485 * @param context $defaultcontext
486 * @return array array of warning strings
487 */
488 protected function resolve_context(&$hash, $defaultcontext) {
489 global $DB;
490
491 $warnings = array();
492
493 if (!empty($hash['contextid'])) {
494 // Contextid was specified, verify we can post there.
495 $contextoptions = $this->get_context_options();
496 if (!isset($contextoptions[$hash['contextid']])) {
497 $warnings[] = new lang_string('contextnotfound', 'cohort', $hash['contextid']);
498 $hash['contextid'] = $defaultcontext->id;
499 }
500 return $warnings;
501 }
502
503 if (!empty($hash['context'])) {
504 $systemcontext = context_system::instance();
505 if ((core_text::strtolower(trim($hash['context'])) ===
506 core_text::strtolower($systemcontext->get_context_name())) ||
507 ('' . $hash['context'] === '' . $systemcontext->id)) {
508 // User meant system context.
509 $hash['contextid'] = $systemcontext->id;
510 $contextoptions = $this->get_context_options();
511 if (!isset($contextoptions[$hash['contextid']])) {
512 $warnings[] = new lang_string('contextnotfound', 'cohort', $hash['context']);
513 $hash['contextid'] = $defaultcontext->id;
514 }
515 } else {
516 // Assume it is a category.
517 $hash['category'] = trim($hash['context']);
518 }
519 }
520
521 if (!empty($hash['category_path'])) {
522 // We already have array with available categories, look up the value.
523 $contextoptions = $this->get_context_options();
524 if (!$hash['contextid'] = array_search($hash['category_path'], $contextoptions)) {
525 $warnings[] = new lang_string('categorynotfound', 'cohort', s($hash['category_path']));
526 $hash['contextid'] = $defaultcontext->id;
527 }
528 return $warnings;
529 }
530
531 if (!empty($hash['category'])) {
532 // Quick search by category path first.
533 // Do not issue warnings or return here, further we'll try to search by id or idnumber.
534 $contextoptions = $this->get_context_options();
535 if ($hash['contextid'] = array_search($hash['category'], $contextoptions)) {
536 return $warnings;
537 }
538 }
539
540 // Now search by category id or category idnumber.
541 if (!empty($hash['category_id'])) {
542 $field = 'id';
543 $value = clean_param($hash['category_id'], PARAM_INT);
544 } else if (!empty($hash['category_idnumber'])) {
545 $field = 'idnumber';
546 $value = $hash['category_idnumber'];
547 } else if (!empty($hash['category'])) {
548 $field = is_numeric($hash['category']) ? 'id' : 'idnumber';
549 $value = $hash['category'];
550 } else {
551 // No category field was specified, assume default category.
552 $hash['contextid'] = $defaultcontext->id;
553 return $warnings;
554 }
555
556 if (empty($this->categoriescache[$field][$value])) {
557 $record = $DB->get_record_sql("SELECT c.id, ctx.id contextid
558 FROM {context} ctx JOIN {course_categories} c ON ctx.contextlevel = ? AND ctx.instanceid = c.id
559 WHERE c.$field = ?", array(CONTEXT_COURSECAT, $value));
560 if ($record && ($contextoptions = $this->get_context_options()) && isset($contextoptions[$record->contextid])) {
561 $contextid = $record->contextid;
562 } else {
563 $warnings[] = new lang_string('categorynotfound', 'cohort', s($value));
564 $contextid = $defaultcontext->id;
565 }
566 // Next time when we can look up and don't search by this value again.
567 $this->categoriescache[$field][$value] = $contextid;
568 }
569 $hash['contextid'] = $this->categoriescache[$field][$value];
570
571 return $warnings;
572 }
573}