Fixed a notice. Tidied up format class call a bit. Added provision
authorthepurpleblob <thepurpleblob>
Wed, 29 Nov 2006 13:32:00 +0000 (13:32 +0000)
committerthepurpleblob <thepurpleblob>
Wed, 29 Nov 2006 13:32:00 +0000 (13:32 +0000)
for categories to be specified within import files - MDL-4163.

question/export.php
question/format.php
question/import.php

index 201a70b..33737cb 100644 (file)
         $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}&amp;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}&amp;category=$category->id");
         }
index 464c709..535b345 100644 (file)
 
 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 "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
 
             // 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) );
         }
index 0ec1eaf..c8ddd16 100644 (file)
@@ -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');
             $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}&amp;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}&amp;category=$category->id");
             }
         <table cellpadding="5">
             <tr>
                 <td align="right"><?php echo $txt->category; ?>:</td>
-                <td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?></td>
+                <td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?>
+                    <?php echo $txt->fromfile; ?>
+                    <input name="catfromfile" type="checkbox" />
+                    <?php helpbutton('importcategory', $txt->importcategory, 'quiz'); ?></td>
             </tr>
 
             <tr>