From 08892d5be51c3ce13ca4897993874ea9efc793bb Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Wed, 29 Nov 2006 13:32:00 +0000 Subject: [PATCH] Fixed a notice. Tidied up format class call a bit. Added provision for categories to be specified within import files - MDL-4163. --- question/export.php | 8 +++- question/format.php | 93 ++++++++++++++++++++++++++++++++++++--------- question/import.php | 19 +++++++-- 3 files changed, 96 insertions(+), 24 deletions(-) diff --git a/question/export.php b/question/export.php index 201a70b93e..33737cbe80 100644 --- a/question/export.php +++ b/question/export.php @@ -92,12 +92,16 @@ $classname = "qformat_$format"; $qformat = new $classname(); - if (! $qformat->exportpreprocess($category, $course)) { // Do anything before that we need to + $qformat->setCategory( $category ); + $qformat->setCourse( $course ); + $qformat->setFilename( $exportfilename ); + + if (! $qformat->exportpreprocess()) { // Do anything before that we need to error( get_string('exporterror','quiz'), "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id"); } - if (! $qformat->exportprocess($exportfilename)) { // Process the export data + if (! $qformat->exportprocess()) { // Process the export data error( get_string('exporterror','quiz'), "$CFG->wwwroot/question/export.php?courseid={$course->id}&category=$category->id"); } diff --git a/question/format.php b/question/format.php index 464c7094f6..535b345d80 100644 --- a/question/format.php +++ b/question/format.php @@ -10,9 +10,12 @@ class qformat_default { - var $displayerrors = true; + // var $displayerrors = true; var $category = NULL; var $course = NULL; + var $filename = ''; + var $matchgrades = 'error'; + var $catfromfile = 0; var $questionids = array(); // functions to indicate import/export functionality @@ -26,25 +29,63 @@ class qformat_default { return false; } -/// Importing functions +// Accessor methods - function importpreprocess($category, $course=NULL ) { - /// Does any pre-processing that may be desired + /** + * set the category + * @param object category the category object + */ + function setCategory( $category ) { + $this->category = $category; + } - $this->category = $category; // Important + /** + * set the course class variable + * @param course object Moodle course variable + */ + function setCourse( $course ) { $this->course = $course; + } - return true; + /** + * set the filename + * @param string filename name of file to import/export + */ + function setFilename( $filename ) { + $this->filename = $filename; + } + + /** + * set matchgrades + * @param string matchgrades error or nearest for grades + */ + function setMatchgrades( $matchgrades ) { + $this->matchgrades = $matchgrades; } /** - * - * @param $matchgrades string 'error' or 'nearest', mismatched grades handling + * set catfromfile + * @param bool catfromfile allow categories embedded in import file */ - function importprocess($filename, $matchgrades='error') { - /// Processes a given file. There's probably little need to change this + function setCatfromfile( $catfromfile ) { + $this->catfromfile = $catfromfile; + } - if (! $lines = $this->readdata($filename)) { +/// Importing functions + + /** + * Perform any required pre-processing + */ + function importpreprocess() { + return true; + } + + /** + * Process the file + * This method should not normally be overidden + */ + function importprocess() { + if (! $lines = $this->readdata($this->filename)) { notify( get_string('cannotread','quiz') ); return false; } @@ -63,16 +104,30 @@ class qformat_default { $count = 0; foreach ($questions as $question) { // Process and store each question + + // check for category modifiers + if ($question->qtype=='category') { + if ($this->catfromfile) { + // find/create category object + $catpath = $question->category; + $newcategory = create_category_path( $catpath, '/', $this->course->id ); + if (!empty($newcategory)) { + $this->category = $newcategory; + } + } + continue; + } + $count++; echo "

$count. ".stripslashes($question->questiontext)."

"; // check for answer grades validity (must match fixed list of grades) - if (!empty($question->fraction)) { + if (!empty($question->fraction) and (is_array($question->fraction))) { $fractions = $question->fraction; $answersvalid = true; // in case they are! foreach ($fractions as $key => $fraction) { - $newfraction = match_grade_options($gradeoptionsfull, $fraction, $matchgrades); + $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades); if ($newfraction===false) { $answersvalid = false; } @@ -182,6 +237,9 @@ class qformat_default { $question->usecase = 0; $question->multiplier = array(); $question->generalfeedback = ''; + $question->correctfeedback = ''; + $question->partiallycorrectfeedback = ''; + $question->incorrectfeedback = ''; return $question; } @@ -250,12 +308,9 @@ class qformat_default { return ".txt"; } - function exportpreprocess($category, $course) { + function exportpreprocess() { /// Does any pre-processing that may be desired - $this->category = $category; // Important - $this->course = $course; // As is this! - return true; } @@ -267,7 +322,7 @@ class qformat_default { return $content; } - function exportprocess($filename) { + function exportprocess() { /// Exports a given category. There's probably little need to change this global $CFG; @@ -315,7 +370,7 @@ class qformat_default { $expout = $this->presave_process( $expout ); // write file - $filepath = $path."/".$filename . $this->export_file_extension(); + $filepath = $path."/".$this->filename . $this->export_file_extension(); if (!$fh=fopen($filepath,"w")) { error( get_string('cannotopen','quiz',$filepath) ); } diff --git a/question/import.php b/question/import.php index 0ec1eaf54a..c8ddd16822 100644 --- a/question/import.php +++ b/question/import.php @@ -17,6 +17,7 @@ $params = new stdClass; $params->choosefile = optional_param('choosefile','',PARAM_PATH); $categoryid = optional_param('category', 0, PARAM_INT); + $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL ); $courseid = optional_param('course', 0, PARAM_INT); $format = optional_param('format','',PARAM_FILE); $params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA); @@ -28,6 +29,8 @@ $txt->editingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz"); $txt->file = get_string('file'); $txt->fileformat = get_string('fileformat','quiz'); + $txt->fromfile = get_string('fromfile','quiz'); + $txt->importcategory = get_string('importcategory','quiz'); $txt->importerror = get_string('importerror','quiz'); $txt->importfilearea = get_string('importfilearea','quiz'); $txt->importfileupload = get_string('importfileupload','quiz'); @@ -153,12 +156,19 @@ $classname = "qformat_$format"; $qformat = new $classname(); - if (! $qformat->importpreprocess($category,$course)) { // Do anything before that we need to + // load data into class + $qformat->setCategory( $category ); + $qformat->setCourse( $course ); + $qformat->setFilename( $importfile ); + $qformat->setMatchgrades( $params->matchgrades ); + $qformat->setCatfromfile( $catfromfile ); + + if (! $qformat->importpreprocess()) { // Do anything before that we need to error( $txt->importerror , "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } - if (! $qformat->importprocess($importfile, $params->matchgrades) ) { // Process the uploaded file + if (! $qformat->importprocess() ) { // Process the uploaded file error( $txt->importerror , "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } @@ -221,7 +231,10 @@ - + -- 2.39.5
category; ?>:id, ""); ?>id, ""); ?> + fromfile; ?> + + importcategory, 'quiz'); ?>