]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8450\rAdded support for including question categories in a quiz export.\rAlso a...
authorthepurpleblob <thepurpleblob>
Wed, 7 Feb 2007 10:29:53 +0000 (10:29 +0000)
committerthepurpleblob <thepurpleblob>
Wed, 7 Feb 2007 10:29:53 +0000 (10:29 +0000)
question/export.php
question/format.php
question/format/gift/format.php
question/format/xhtml/format.php
question/format/xml/format.php

index e53c088a901f28d9052dae2e2ec22e2156a353f9..294e12df0266165e687a6fae0a134e218beaa961 100644 (file)
@@ -2,7 +2,11 @@
 /**
 * Export quiz questions into the given category
 *
+<<<<<<< export.php
 * @version $Id$
+=======
+* @version $Id$
+>>>>>>> 1.22.2.2
 * @author Martin Dougiamas, Howard Miller, and many others.
 *         {@link http://moodle.org}
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
     require_once("../config.php");
     require_once( "editlib.php" );
 
+    // get parameters
     $categoryid = optional_param('category',0, PARAM_INT);
+    $cattofile = optional_param('cattofile',0, PARAM_BOOL);
     $courseid = required_param('courseid',PARAM_INT);
-    $format = optional_param('format','', PARAM_FILE );
     $exportfilename = optional_param('exportfilename','',PARAM_FILE );
+    $format = optional_param('format','', PARAM_FILE );
+
+    
+    // get display strings
+    $txt = new object;
+    $txt->category = get_string('category','quiz');
+    $txt->download = get_string('download','quiz');
+    $txt->downloadextra = get_string('downloadextra','quiz');
+    $txt->exporterror = get_string('exporterror','quiz');
+    $txt->exportname = get_string('exportname','quiz');
+    $txt->exportquestions = get_string('exportquestions', 'quiz');
+    $txt->fileformat = get_string('fileformat','quiz');
+    $txt->exportcategory = get_string('exportcategory','quiz');
+    $txt->modulename = get_string('modulename','quiz');
+    $txt->modulenameplural = get_string('modulenameplural','quiz');
+    $txt->nocategory = get_string('nocategory','quiz');
+    $txt->tofile = get_string('tofile','quiz');
+
 
     if (! $course = get_record("course", "id", $courseid)) {
         error("Course does not exist!");
     }
 
     if (! $categorycourse = get_record("course", "id", $category->course)) {
-        error( get_string('nocategory','quiz') );
+        error( $txt->nocategory );
     }
 
     require_login($course->id, false);
     
+    // check role capability
     $context = get_context_instance(CONTEXT_COURSE, $course->id);
     require_capability('moodle/question:export', $context);
 
     // ensure the files area exists for this course
     make_upload_directory( "$course->id" );
 
-    $strexportquestions = get_string("exportquestions", "quiz");
-    $strquestions = get_string("questions", "quiz");
-
-    $strquizzes = get_string('modulenameplural', 'quiz');
-    $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
-
-    $dirname = get_string("exportfilename","quiz");
-    
-    /// Header:
-
+    /// Header
     if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
         $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
-            ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
+            ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename )
             : "";
-        print_header_simple($strexportquestions, '',
-                 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
-                 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
-                 ' -> '.$strexportquestions,
-                 "", "", true, $strupdatemodule);
+        print_header_simple($txt->exportquestions, '',
+            "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">$txt->modulenameplural</a>".
+            " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
+            ' -> '.$txt->exportquestions,
+            "", "", true, $strupdatemodule);
         $currenttab = 'edit';
         $mode = 'export';
         include($CFG->dirroot.'/mod/quiz/tabs.php');
     } else {
-        print_header_simple($strexportquestions, '', $strexportquestions);
+        print_header_simple($txt->exportquestions, '', $txt->exportquestions);
         // print tabs
         $currenttab = 'export';
         include('tabs.php');
     if (!empty($format)) {   /// Filename
 
         if (!confirm_sesskey()) {
-            echo( 'Sesskey error' );
+            print_error( 'sesskey' );
         }
 
         if (! is_readable("format/$format/format.php")) {
-            error('Format not known ('.clean_text($form->format).')');
-        }
+            error( "Format not known ($format)" );  }
 
-        require("format.php");  // Parent class
+        // load parent class for import/export
+        require("format.php"); 
+        
+        // and then the class for the selected format
         require("format/$format/format.php");
 
         $classname = "qformat_$format";
         $qformat->setCategory( $category );
         $qformat->setCourse( $course );
         $qformat->setFilename( $exportfilename );
+        $qformat->setCattofile( $cattofile );
 
         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");
+            error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
         }
 
         if (! $qformat->exportprocess()) {         // Process the export data
-            error( get_string('exporterror','quiz'),
-                    "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
+            error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
         }
 
         if (! $qformat->exportpostprocess()) {                    // In case anything needs to be done after
-            error( get_string('exporterror','quiz'),
-                    "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
+            error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
         }
         echo "<hr />";
 
         // link to download the finished file
         $file_ext = $qformat->export_file_extension();
-        $download_str = get_string( 'download', 'quiz' );
-        $downloadextra_str = get_string( 'downloadextra','quiz' );
         if ($CFG->slasharguments) {
           $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
         }
         else {
           $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
         }
+<<<<<<< export.php
         echo "<p><div class=\"boxaligncenter\"><a href=\"$efile\">$download_str</a></div></p>";
         echo "<p><div class=\"boxaligncenter\"><font size=\"-1\">$downloadextra_str</font></div></p>";
+=======
+        echo "</p><center><a href=\"$efile\">$txt->download</a></center></p>";
+        echo "</p><center><font size=\"-1\">$txt->downloadextra</font></center></p>";
+>>>>>>> 1.22.2.2
 
         print_continue("edit.php?courseid=$course->id");
         print_footer($course);
         exit;
     }
 
-    /// Print upload form
+    /// Display upload form
 
     // get valid formats to generate dropdown list
-    $fileformatnames = get_import_export_formats( "export" );
+    $fileformatnames = get_import_export_formats( 'export' );
 
     // get filename
     if (empty($exportfilename)) {
         $exportfilename = default_export_filename($course, $category);
     }
 
+<<<<<<< export.php
     print_heading_with_help($strexportquestions, "export", "quiz");
 
     print_simple_box_start("center");
     echo " <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
     echo " <input type=\"submit\" name=\"save\" value=\"".get_string("exportquestions","quiz")."\" />";
     echo "</td></tr>\n";
+=======
+    // DISPLAY MAIN PAGE
+    print_heading_with_help($txt->exportquestions, 'export', 'quiz');
+    print_simple_box_start('center');
+    
+?>    
+>>>>>>> 1.22.2.2
 
+<<<<<<< export.php
     echo "</table>\n";
     echo '</fieldset>';
     echo "</form>\n";
+=======
+    <form enctype="multipart/form-data" method="post" action="export.php">
+        <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
+        <input type="hidden" name="courseid" value="<?php echo $course->id; ?>" />
+            
+            <table cellpadding="5">
+                <tr>
+                    <td align="right"><?php echo $txt->category; ?>:</td>
+                    <td>
+                        <?php 
+                        if (!$showcatmenu) { // category already specified
+                            echo '<strong>'.question_category_coursename($category).'</strong>&nbsp;&nbsp;'; ?>
+                            <input type="hidden" name="category" value="<?php echo $category->id ?>" />
+                            <?php
+                            } else { // no category specified, let user choose
+                                question_category_select_menu($course->id, true, false, $category->id);
+                            } 
+                            echo $txt->tofile; ?>
+                            <input name="cattofile" type="checkbox" />
+                            <?php helpbutton('exportcategory', $txt->exportcategory, 'quiz'); ?>
+                        </td>    
+                    </tr>
+                    <tr>
+                        <td align="right"><?php echo $txt->fileformat; ?>:</td>
+                        <td>
+                            <?php choose_from_menu($fileformatnames, 'format', 'gift', '');
+                            helpbutton('export', $txt->exportquestions, 'quiz'); ?>
+                        </td>
+                    </tr>
+                    <tr>
+                        <td align="right"><?php echo $txt->exportname; ?>:</td>
+                        <td>
+                            <input type="text" size="40" name="exportfilename" value="<?php echo $exportfilename; ?>" />
+                        </td>
+                    </tr>
+                    <tr>
+                        <td align="center" colspan="2">
+                            <input type="submit" name="save" value="<?php echo $txt->exportquestions; ?>" />
+                        </td>
+                    </tr>
+                </table>
+        </form>
+    <?php
+>>>>>>> 1.22.2.2
     print_simple_box_end();
-
     print_footer($course);
+?>
 
-?>
\ No newline at end of file
+<<<<<<< export.php
+?>=======
+>>>>>>> 1.22.2.2
index 86854954a6ac37148885e723190f870789da5a91..d176fa44256c0584c077f6ec1adde40e0a551aa9 100644 (file)
@@ -1,4 +1,3 @@
-
 <?php  // $Id$ 
 
 ////////////////////////////////////////////////////////////////////
 
 class qformat_default {
 
-    // var $displayerrors = true;
+    var $displayerrors = true;
     var $category = NULL;
     var $course = NULL;
     var $filename = '';
     var $matchgrades = 'error';
     var $catfromfile = 0;
+    var $cattofile = 0;
     var $questionids = array();
 
 // functions to indicate import/export functionality
@@ -71,6 +71,14 @@ class qformat_default {
     function setCatfromfile( $catfromfile ) {
         $this->catfromfile = $catfromfile;
     }
+    
+    /**
+     * set cattofile
+     * @param bool cattofile exports categories within export file
+     */
+    function setCattofile( $cattofile ) {
+        $this->cattofile = $cattofile;
+    } 
 
 /// Importing functions
 
@@ -350,9 +358,15 @@ class qformat_default {
         // results are first written into string (and then to a file)
         // so create/initialize the string here
         $expout = "";
+        
+        // track which category questions are in
+        // if it changes we will record the category change in the output
+        // file if selected. 0 means that it will get printed before the 1st question
+        $trackcategory = 0;
 
         // iterate through questions
         foreach($questions as $question) {
+            
             // do not export hidden questions
             if (!empty($question->hidden)) {
                 continue;
@@ -362,26 +376,42 @@ class qformat_default {
             if ($question->qtype==RANDOM) {
                 continue;
             }
-
-        // export the question displaying message
-        $count++;
-        echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
-        $expout .= $this->writequestion( $question ) . "\n";
+            
+            // check if we need to record category change
+            if ($this->cattofile) {
+                if ($question->category != $trackcategory) {
+                    $trackcategory = $question->category;   
+                    $categoryname = get_category_path( $trackcategory );
+                    
+                    // create 'dummy' question for category export
+                    $dummyquestion = new object;
+                    $dummyquestion->qtype = 'category';
+                    $dummyquestion->category = $categoryname;
+                    $dummyquestion->name = "switch category to $categoryname";
+                    $dummyquestion->id = 0;
+                    $dummyquestion->questiontextformat = '';
+                    $expout .= $this->writequestion( $dummyquestion ) . "\n";
+                }       
+            }    
+
+            // export the question displaying message
+            $count++;
+            echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
+            $expout .= $this->writequestion( $question ) . "\n";
         }
 
         // final pre-process on exported data
         $expout = $this->presave_process( $expout );
-
+       
         // write file
         $filepath = $path."/".$this->filename . $this->export_file_extension();
         if (!$fh=fopen($filepath,"w")) {
             error( get_string('cannotopen','quiz',$filepath) );
         }
-        if (!fwrite($fh, $expout)) {
+        if (!fwrite($fh, $expout, strlen($expout) )) {
             error( get_string('cannotwrite','quiz',$filepath) );
         }
         fclose($fh);
-
         return true;
     }
 
index ae0dfe46d6beea7e468d4e2609bf609a504a56ee..e719ab84c6a3bb8b2cbc98654ee90943ec208828 100755 (executable)
@@ -1,5 +1,5 @@
-
 <?php // $Id$
+//
 ///////////////////////////////////////////////////////////////
 // The GIFT import filter was designed as an easy to use method 
 // for teachers writing questions as a text file. It supports most
@@ -151,8 +151,6 @@ class qformat_gift extends qformat_default {
             return $question;
         }
         
-        
-
         // QUESTION NAME parser
         if (substr($text, 0, 2) == "::") {
             $text = substr($text, 2);
@@ -536,6 +534,10 @@ function writequestion( $question ) {
 
     // output depends on question type
     switch($question->qtype) {
+    case 'category':
+        // not a real question, used to insert category switch
+        $expout .= "\$CATEGORY: $question->category\n";    
+        break;
     case TRUEFALSE:
         $trueanswer = $question->options->answers[$question->options->trueanswer];
         $falseanswer = $question->options->answers[$question->options->falseanswer];
index 77c91751d1a5c374b0a734aa5abf3a01098fa5e7..1a74c21c2fc34b399ef8054e5dc3f188926c3a0f 100755 (executable)
@@ -19,6 +19,11 @@ function writequestion( $question ) {
     // turns question into string
     // question reflects database fields for general question and specific to type
 
+    // if a category switch, just ignore
+    if ($question-qtype=='category') {
+        return '';
+    }
+
     // initial string;
     $expout = "";
     $id = $question->id;
@@ -39,25 +44,25 @@ function writequestion( $question ) {
     // selection depends on question type
     switch($question->qtype) {
     case TRUEFALSE:
-      $st_true = get_string( 'true','quiz' );
-      $st_false = get_string( 'false','quiz' );
-      $expout .= "<ul class=\"truefalse\">\n";
-      $expout .= "  <li><input name=\"quest_$id\" type=\"radio\" value=\"$st_true\" />$st_true</li>\n";
-      $expout .= "  <li><input name=\"quest_$id\" type=\"radio\" value=\"$st_false\" />$st_false</li>\n";
-      $expout .= "</ul>\n";
-      break;
+        $st_true = get_string( 'true','quiz' );
+        $st_false = get_string( 'false','quiz' );
+        $expout .= "<ul class=\"truefalse\">\n";
+        $expout .= "  <li><input name=\"quest_$id\" type=\"radio\" value=\"$st_true\" />$st_true</li>\n";
+        $expout .= "  <li><input name=\"quest_$id\" type=\"radio\" value=\"$st_false\" />$st_false</li>\n";
+        $expout .= "</ul>\n";
+        break;
     case MULTICHOICE:
-      $expout .= "<ul class=\"multichoice\">\n";
-      foreach($question->options->answers as $answer) {
-        $ans_text = $this->repchar( $answer->answer );
-        if ($question->options->single) {
-          $expout .= "  <li><input name=\"quest_$id\" type=\"radio\" value=\"$ans_text\" />$ans_text</li>\n";
-        }
-        else {
-          $expout .= "  <li><input name=\"quest_$id\" type=\"checkbox\" value=\"$ans_text\" />$ans_text</li>\n";
+        $expout .= "<ul class=\"multichoice\">\n";
+        foreach($question->options->answers as $answer) {
+            $ans_text = $this->repchar( $answer->answer );
+            if ($question->options->single) {
+                $expout .= "  <li><input name=\"quest_$id\" type=\"radio\" value=\"$ans_text\" />$ans_text</li>\n";
+            }
+            else {
+                $expout .= "  <li><input name=\"quest_$id\" type=\"checkbox\" value=\"$ans_text\" />$ans_text</li>\n";
+            }
         }
-      }
-      $expout .= "</ul>\n";
+        $expout .= "</ul>\n";
         break;
     case SHORTANSWER:
         $expout .= "<ul class=\"shortanswer\">\n";
index 32952afb6dba9ddbcaa4dfd5f958fdb1a5bbc0e1..cf091ab6439df93838745f659b9b83567f83ebee 100755 (executable)
@@ -1,4 +1,3 @@
-
 <?php // $Id$
 //
 ///////////////////////////////////////////////////////////////
@@ -669,8 +668,16 @@ class qformat_xml extends qformat_default {
         $expout .= "\n\n<!-- question: $question->id  -->\n";
 
         // add opening tag
-        // generates specific header for Cloze type question
-        if ($question->qtype != MULTIANSWER) {
+        // generates specific header for Cloze and category type question
+        if ($question->qtype == 'category') {
+            $expout .= "  <question type=\"category\">\n";
+            $expout .= "    <category>\n";
+            $expout .= "        $question->category\n";
+            $expout .= "    </category>\n";
+            $expout .= "  </question>\n";
+            return $expout;
+        }    
+        elseif ($question->qtype != MULTIANSWER) {
             // for all question types except Close
             $question_type = $this->get_qtype( $question->qtype );
             $name_text = $this->writetext( $question->name );
@@ -712,6 +719,9 @@ class qformat_xml extends qformat_default {
 
         // output depends on question type
         switch($question->qtype) {
+        case 'category':
+            // not a qtype really - dummy used for category switching
+            break;    
         case TRUEFALSE:
             foreach ($question->options->answers as $answer) {
                 $fraction_pc = round( $answer->fraction * 100 );