2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * A form for cohort upload.
20 * @package core_cohort
21 * @copyright 2014 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->libdir
.'/formslib.php');
30 * Cohort upload form class
32 * @package core_cohort
33 * @copyright 2014 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class 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();
47 public function definition() {
48 $mform = $this->_form
;
49 $data = (object)$this->_customdata
;
51 $mform->addElement('hidden', 'returnurl');
52 $mform->setType('returnurl', PARAM_URL
);
54 $mform->addElement('header', 'cohortfileuploadform', get_string('uploadafile'));
56 $filepickeroptions = array();
57 $filepickeroptions['filetypes'] = '*';
58 $filepickeroptions['maxbytes'] = get_max_upload_file_size();
59 $mform->addElement('filepicker', 'cohortfile', get_string('file'), null, $filepickeroptions);
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');
68 $mform->setDefault('delimiter', 'comma');
70 $mform->addHelpButton('delimiter', 'csvdelimiter', 'tool_uploadcourse');
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');
77 $options = $this->get_context_options();
78 $mform->addElement('select', 'contextid', get_string('defaultcontext', 'cohort'), $options);
80 $this->add_cohort_upload_buttons(true);
81 $this->set_data($data);
85 * Add buttons to the form ("Upload cohorts", "Preview", "Cancel")
87 protected function add_cohort_upload_buttons() {
88 $mform = $this->_form
;
90 $buttonarray = array();
92 $submitlabel = get_string('uploadcohorts', 'cohort');
93 $buttonarray[] = $mform->createElement('submit', 'submitbutton', $submitlabel);
95 $previewlabel = get_string('preview', 'cohort');
96 $buttonarray[] = $mform->createElement('submit', 'previewbutton', $previewlabel);
97 $mform->registerNoSubmitButton('previewbutton');
99 $buttonarray[] = $mform->createElement('cancel');
101 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
102 $mform->closeHeaderBefore('buttonar');
106 * Process the uploaded file and allow the submit button only if it doest not have errors.
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;
124 if (!$allowsubmitform) {
125 // Hide submit button.
126 $el = $mform->getElement('buttonar')->getElements()[0];
130 $mform->setExpanded('cohortfileuploadform', false);
136 * Returns the list of contexts where current user can create cohorts.
140 protected function get_context_options() {
142 require_once($CFG->libdir
. '/coursecatlib.php');
143 if ($this->contextoptions
=== null) {
144 $this->contextoptions
= array();
145 $displaylist = coursecat
::make_categories_list('moodle/cohort:manage');
146 // We need to index the options array by context id instead of category id and add option for system context.
147 $syscontext = context_system
::instance();
148 if (has_capability('moodle/cohort:manage', $syscontext)) {
149 $this->contextoptions
[$syscontext->id
] = $syscontext->get_context_name();
151 foreach ($displaylist as $cid => $name) {
152 $context = context_coursecat
::instance($cid);
153 $this->contextoptions
[$context->id
] = $name;
156 return $this->contextoptions
;
159 public function validation($data, $files) {
160 $errors = parent
::validation($data, $files);
161 if (empty($errors)) {
162 if (empty($data['cohortfile']) ||
!($file = $this->get_cohort_file($data['cohortfile']))) {
163 $errors['cohortfile'] = get_string('required');
165 if (!empty($this->processeddata
[0]['errors'])) {
166 // Any value in $errors will notify that validation did not pass. The detailed errors will be shown in preview.
167 $errors['dummy'] = '';
175 * Returns the uploaded file if it is present.
177 * @param int $draftid
178 * @return stored_file|null
180 protected function get_cohort_file($draftid) {
182 // We can not use moodleform::get_file_content() method because we need the content before the form is validated.
186 $fs = get_file_storage();
187 $context = context_user
::instance($USER->id
);
188 if (!$files = $fs->get_area_files($context->id
, 'user', 'draft', $draftid, 'id DESC', false)) {
191 $file = reset($files);
198 * Returns the list of prepared objects to be added as cohorts
200 * @return array of stdClass objects, each can be passed to {@link cohort_add_cohort()}
202 public function get_cohorts_data() {
204 if ($this->processeddata
) {
205 foreach ($this->processeddata
as $idx => $line) {
206 if ($idx && !empty($line['data'])) {
207 $cohorts[] = (object)$line['data'];
215 * Displays the preview of the uploaded file
217 protected function preview_uploaded_cohorts() {
219 if (empty($this->processeddata
)) {
222 foreach ($this->processeddata
[0]['errors'] as $error) {
223 echo $OUTPUT->notification($error);
225 foreach ($this->processeddata
[0]['warnings'] as $warning) {
226 echo $OUTPUT->notification($warning, 'notifymessage');
228 $table = new html_table();
229 $table->id
= 'previewuploadedcohorts';
230 $columns = $this->processeddata
[0]['data'];
231 $columns['contextid'] = get_string('context', 'role');
233 // Add column names to the preview table.
234 $table->head
= array('');
235 foreach ($columns as $key => $value) {
236 $table->head
[] = $value;
238 $table->head
[] = get_string('status');
240 // Add (some) rows to the preview table.
241 $previewdrows = $this->get_previewed_rows();
242 foreach ($previewdrows as $idx) {
243 $line = $this->processeddata
[$idx];
244 $cells = array(new html_table_cell($idx));
245 $context = context
::instance_by_id($line['data']['contextid']);
246 foreach ($columns as $key => $value) {
247 if ($key === 'contextid') {
248 $text = html_writer
::link(new moodle_url('/cohort/index.php', array('contextid' => $context->id
)),
249 $context->get_context_name(false));
251 $text = s($line['data'][$key]);
253 $cells[] = new html_table_cell($text);
256 if ($line['errors']) {
257 $text .= html_writer
::div(join('<br>', $line['errors']), 'notifyproblem');
259 if ($line['warnings']) {
260 $text .= html_writer
::div(join('<br>', $line['warnings']));
262 $cells[] = new html_table_cell($text);
263 $table->data
[] = new html_table_row($cells);
265 if ($notdisplayed = count($this->processeddata
) - count($previewdrows) - 1) {
266 $cell = new html_table_cell(get_string('displayedrows', 'cohort',
267 (object)array('displayed' => count($previewdrows), 'total' => count($this->processeddata
) - 1)));
268 $cell->colspan
= count($columns) +
2;
269 $table->data
[] = new html_table_row(array($cell));
271 echo html_writer
::table($table);
275 * Find up rows to show in preview
277 * Number of previewed rows is limited but rows with errors and warnings have priority.
281 protected function get_previewed_rows() {
283 if (count($this->processeddata
) <= 1) {
285 } else if (count($this->processeddata
) < $previewlimit +
1) {
287 $rows = range(1, count($this->processeddata
) - 1);
289 // First find rows with errors and warnings (no more than 10 of each).
290 $errorrows = $warningrows = array();
291 foreach ($this->processeddata
as $rownum => $line) {
292 if ($rownum && $line['errors']) {
293 $errorrows[] = $rownum;
294 if (count($errorrows) >= $previewlimit) {
297 } else if ($rownum && $line['warnings']) {
298 if (count($warningrows) +
count($errorrows) < $previewlimit) {
299 $warningrows[] = $rownum;
303 // Include as many error rows as possible and top them up with warning rows.
304 $rows = array_merge($errorrows, array_slice($warningrows, 0, $previewlimit - count($errorrows)));
305 // Keep adding good rows until we reach limit.
306 for ($rownum = 1; count($rows) < $previewlimit; $rownum++
) {
307 if (!in_array($rownum, $rows)) {
316 public function display() {
317 // Finalize the form definition if not yet done.
318 if (!$this->_definition_finalized
) {
319 $this->_definition_finalized
= true;
320 $this->definition_after_data();
323 // Difference from the parent display() method is that we want to show preview above the form if applicable.
324 $this->preview_uploaded_cohorts();
326 $this->_form
->display();
330 * @param stored_file $file
331 * @param string $encoding
332 * @param string $delimiter
333 * @param context $defaultcontext
336 protected function process_upload_file($file, $encoding, $delimiter, $defaultcontext) {
338 require_once($CFG->libdir
. '/csvlib.class.php');
341 0 => array('errors' => array(), 'warnings' => array(), 'data' => array())
344 // Read and parse the CSV file using csv library.
345 $content = $file->get_content();
347 $cohorts[0]['errors'][] = new lang_string('csvemptyfile', 'error');
351 $uploadid = csv_import_reader
::get_new_iid('uploadcohort');
352 $cir = new csv_import_reader($uploadid, 'uploadcohort');
353 $readcount = $cir->load_csv_content($content, $encoding, $delimiter);
356 $cohorts[0]['errors'][] = get_string('csvloaderror', 'error', $cir->get_error());
359 $columns = $cir->get_columns();
361 // Check that columns include 'name' and warn about extra columns.
362 $allowedcolumns = array('contextid', 'name', 'idnumber', 'description', 'descriptionformat', 'visible', 'theme');
363 $additionalcolumns = array('context', 'category', 'category_id', 'category_idnumber', 'category_path');
364 $displaycolumns = array();
365 $extracolumns = array();
366 $columnsmapping = array();
367 foreach ($columns as $i => $columnname) {
368 $columnnamelower = preg_replace('/ /', '', core_text
::strtolower($columnname));
369 $columnsmapping[$i] = null;
370 if (in_array($columnnamelower, $allowedcolumns)) {
371 $displaycolumns[$columnnamelower] = $columnname;
372 $columnsmapping[$i] = $columnnamelower;
373 } else if (in_array($columnnamelower, $additionalcolumns)) {
374 $columnsmapping[$i] = $columnnamelower;
376 $extracolumns[] = $columnname;
379 if (!in_array('name', $columnsmapping)) {
380 $cohorts[0]['errors'][] = new lang_string('namecolumnmissing', 'cohort');
384 $cohorts[0]['warnings'][] = new lang_string('csvextracolumns', 'cohort', s(join(', ', $extracolumns)));
387 if (!isset($displaycolumns['contextid'])) {
388 $displaycolumns['contextid'] = 'contextid';
390 $cohorts[0]['data'] = $displaycolumns;
395 $idnumbers = array();
397 $haswarnings = false;
398 while ($row = $cir->next()) {
400 $cohorts[$rownum] = array(
402 'warnings' => array(),
406 foreach ($row as $i => $value) {
407 if ($columnsmapping[$i]) {
408 $hash[$columnsmapping[$i]] = $value;
411 $this->clean_cohort_data($hash);
413 $warnings = $this->resolve_context($hash, $defaultcontext);
414 $cohorts[$rownum]['warnings'] = array_merge($cohorts[$rownum]['warnings'], $warnings);
416 if (!empty($hash['idnumber'])) {
417 if (isset($idnumbers[$hash['idnumber']]) ||
$DB->record_exists('cohort', array('idnumber' => $hash['idnumber']))) {
418 $cohorts[$rownum]['errors'][] = new lang_string('duplicateidnumber', 'cohort');
420 $idnumbers[$hash['idnumber']] = true;
423 if (empty($hash['name'])) {
424 $cohorts[$rownum]['errors'][] = new lang_string('namefieldempty', 'cohort');
427 if (!empty($hash['theme']) && !empty($CFG->allowcohortthemes
)) {
428 $availablethemes = cohort_get_list_of_themes();
429 if (empty($availablethemes[$hash['theme']])) {
430 $cohorts[$rownum]['errors'][] = new lang_string('invalidtheme', 'cohort');
434 $cohorts[$rownum]['data'] = array_intersect_key($hash, $cohorts[0]['data']);
435 $haserrors = $haserrors ||
!empty($cohorts[$rownum]['errors']);
436 $haswarnings = $haswarnings ||
!empty($cohorts[$rownum]['warnings']);
440 $cohorts[0]['errors'][] = new lang_string('csvcontainserrors', 'cohort');
444 $cohorts[0]['warnings'][] = new lang_string('csvcontainswarnings', 'cohort');
451 * Cleans input data about one cohort.
455 protected function clean_cohort_data(&$hash) {
456 foreach ($hash as $key => $value) {
458 case 'contextid': $hash[$key] = clean_param($value, PARAM_INT
); break;
459 case 'name': $hash[$key] = core_text
::substr(clean_param($value, PARAM_TEXT
), 0, 254); break;
460 case 'idnumber': $hash[$key] = core_text
::substr(clean_param($value, PARAM_RAW
), 0, 254); break;
461 case 'description': $hash[$key] = clean_param($value, PARAM_RAW
); break;
462 case 'descriptionformat': $hash[$key] = clean_param($value, PARAM_INT
); break;
464 $tempstr = trim(core_text
::strtolower($value));
465 if ($tempstr === '') {
466 // Empty string is treated as "YES" (the default value for cohort visibility).
469 if ($tempstr === core_text
::strtolower(get_string('no')) ||
$tempstr === 'n') {
470 // Special treatment for 'no' string that is not included in clean_param().
473 $hash[$key] = clean_param($value, PARAM_BOOL
) ?
1 : 0;
477 $hash[$key] = core_text
::substr(clean_param($value, PARAM_TEXT
), 0, 50);
484 * Determines in which context the particular cohort will be created
487 * @param context $defaultcontext
488 * @return array array of warning strings
490 protected function resolve_context(&$hash, $defaultcontext) {
495 if (!empty($hash['contextid'])) {
496 // Contextid was specified, verify we can post there.
497 $contextoptions = $this->get_context_options();
498 if (!isset($contextoptions[$hash['contextid']])) {
499 $warnings[] = new lang_string('contextnotfound', 'cohort', $hash['contextid']);
500 $hash['contextid'] = $defaultcontext->id
;
505 if (!empty($hash['context'])) {
506 $systemcontext = context_system
::instance();
507 if ((core_text
::strtolower(trim($hash['context'])) ===
508 core_text
::strtolower($systemcontext->get_context_name())) ||
509 ('' . $hash['context'] === '' . $systemcontext->id
)) {
510 // User meant system context.
511 $hash['contextid'] = $systemcontext->id
;
512 $contextoptions = $this->get_context_options();
513 if (!isset($contextoptions[$hash['contextid']])) {
514 $warnings[] = new lang_string('contextnotfound', 'cohort', $hash['context']);
515 $hash['contextid'] = $defaultcontext->id
;
518 // Assume it is a category.
519 $hash['category'] = trim($hash['context']);
523 if (!empty($hash['category_path'])) {
524 // We already have array with available categories, look up the value.
525 $contextoptions = $this->get_context_options();
526 if (!$hash['contextid'] = array_search($hash['category_path'], $contextoptions)) {
527 $warnings[] = new lang_string('categorynotfound', 'cohort', s($hash['category_path']));
528 $hash['contextid'] = $defaultcontext->id
;
533 if (!empty($hash['category'])) {
534 // Quick search by category path first.
535 // Do not issue warnings or return here, further we'll try to search by id or idnumber.
536 $contextoptions = $this->get_context_options();
537 if ($hash['contextid'] = array_search($hash['category'], $contextoptions)) {
542 // Now search by category id or category idnumber.
543 if (!empty($hash['category_id'])) {
545 $value = clean_param($hash['category_id'], PARAM_INT
);
546 } else if (!empty($hash['category_idnumber'])) {
548 $value = $hash['category_idnumber'];
549 } else if (!empty($hash['category'])) {
550 $field = is_numeric($hash['category']) ?
'id' : 'idnumber';
551 $value = $hash['category'];
553 // No category field was specified, assume default category.
554 $hash['contextid'] = $defaultcontext->id
;
558 if (empty($this->categoriescache
[$field][$value])) {
559 $record = $DB->get_record_sql("SELECT c.id, ctx.id contextid
560 FROM {context} ctx JOIN {course_categories} c ON ctx.contextlevel = ? AND ctx.instanceid = c.id
561 WHERE c.$field = ?", array(CONTEXT_COURSECAT
, $value));
562 if ($record && ($contextoptions = $this->get_context_options()) && isset($contextoptions[$record->contextid
])) {
563 $contextid = $record->contextid
;
565 $warnings[] = new lang_string('categorynotfound', 'cohort', s($value));
566 $contextid = $defaultcontext->id
;
568 // Next time when we can look up and don't search by this value again.
569 $this->categoriescache
[$field][$value] = $contextid;
571 $hash['contextid'] = $this->categoriescache
[$field][$value];