/** * Get content of uploaded file. * @param $element name of file upload element * @return mixed false in case of failure, string if ok */ function get_file_content($elname) { if (!$this->is_submitted() or !$this->is_validated()) { return false; } if (!$this->_form->elementExists($elname)) { return false; } if (empty($this->_upload_manager->files[$elname]['clear'])) { return false; } if (empty($this->_upload_manager->files[$elname]['tmp_name'])) { return false; } $data = ""; $file = @fopen($this->_upload_manager->files[$elname]['tmp_name'], "rb"); if ($file) { while (!feof($file)) { $data .= fread($file, 1024); // TODO: do we really have to do this? } fclose($file); return $data; } else { return false; } }
/** * Add question-type specific form fields. * * @param MoodleQuickForm $mform the form being built. */ function definition_inner(&$mform) { global $QTYPES; $this->qtypeobj =& $QTYPES[$this->qtype()]; $label = get_string("sharedwildcards", "qtype_datasetdependent"); $mform->addElement('hidden', 'initialcategory', 1); $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question); $mform->insertElementBefore($mform->createElement('static', 'listcategory', $label, $html2), 'name'); $addfieldsname = 'updatecategory'; $addstring = get_string("updatecategory", "qtype_calculated"); $mform->registerNoSubmitButton($addfieldsname); $mform->insertElementBefore($mform->createElement('submit', $addfieldsname, $addstring), 'listcategory'); $repeated = array(); $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}')); $repeated[] =& $mform->createElement('text', 'answers', get_string('correctanswerformula', 'quiz') . '=', array('size' => 50)); $repeatedoptions['answers']['type'] = PARAM_NOTAGS; $creategrades = get_grade_options(); $gradeoptions = $creategrades->gradeoptions; $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions); $repeatedoptions['fraction']['default'] = 0; $repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated')); $repeatedoptions['tolerance']['type'] = PARAM_NUMBER; $repeatedoptions['tolerance']['default'] = 0.01; $repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types()); $repeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9)); $repeatedoptions['correctanswerlength']['default'] = 2; $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz')); $repeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats); $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'), array('course' => $this->coursefilesid)); $repeatedoptions['feedback']['type'] = PARAM_RAW; if (isset($this->question->options)) { $count = count($this->question->options->answers); } else { $count = 0; } if ($this->question->formoptions->repeatelements) { $repeatsatstart = $count + 1; } else { $repeatsatstart = $count; } $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated')); $repeated = array(); $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}')); $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz')); $mform->setType('unit', PARAM_NOTAGS); $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')); $mform->setType('multiplier', PARAM_NUMBER); if (isset($this->question->options)) { $countunits = count($this->question->options->units); } else { $countunits = 0; } if ($this->question->formoptions->repeatelements) { $repeatsatstart = $countunits + 1; } else { $repeatsatstart = $countunits; } $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}')); if ($mform->elementExists('multiplier[0]')) { $firstunit =& $mform->getElement('multiplier[0]'); $firstunit->freeze(); $firstunit->setValue('1.0'); $firstunit->setPersistantFreeze(true); } //hidden elements $mform->addElement('hidden', 'wizard', 'datasetdefinitions'); $mform->setType('wizard', PARAM_ALPHA); }
/** * Internal method. Validates all old-style uploaded files. */ function _validate_files(&$files) { global $CFG, $COURSE; $files = array(); if (empty($_FILES)) { // we do not need to do any checks because no files were submitted // note: server side rules do not work for files - use custom verification in validate() instead return true; } $errors = array(); $filenames = array(); // now check that we really want each file foreach ($_FILES as $elname => $file) { $required = $this->_form->isElementRequired($elname); if ($file['error'] == 4 and $file['size'] == 0) { if ($required) { $errors[$elname] = get_string('required'); } unset($_FILES[$elname]); continue; } if (!empty($file['error'])) { $errors[$elname] = file_get_upload_error($file['error']); unset($_FILES[$elname]); continue; } if (!is_uploaded_file($file['tmp_name'])) { // TODO: improve error message $errors[$elname] = get_string('error'); unset($_FILES[$elname]); continue; } if (!$this->_form->elementExists($elname) or !$this->_form->getElementType($elname) == 'file') { // hmm, this file was not requested unset($_FILES[$elname]); continue; } /* // TODO: rethink the file scanning if ($CFG->runclamonupload) { if (!clam_scan_moodle_file($_FILES[$elname], $COURSE)) { $errors[$elname] = $_FILES[$elname]['uploadlog']; unset($_FILES[$elname]); continue; } } */ $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE); if ($filename === '') { // TODO: improve error message - wrong chars $errors[$elname] = get_string('error'); unset($_FILES[$elname]); continue; } if (in_array($filename, $filenames)) { // TODO: improve error message - duplicate name $errors[$elname] = get_string('error'); unset($_FILES[$elname]); continue; } $filenames[] = $filename; $_FILES[$elname]['name'] = $filename; $files[$elname] = $_FILES[$elname]['tmp_name']; } // return errors if found if (count($errors) == 0) { return true; } else { $files = array(); return $errors; } }