Fixed a notice. Tidied up format class call a bit. Added provision

for categories to be specified within import files - MDL-4163.
This commit is contained in:
thepurpleblob
2006-11-29 13:32:00 +00:00
parent f5c154078a
commit 08892d5be5
3 changed files with 98 additions and 26 deletions
+6 -2
View File
@@ -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");
}
+76 -21
View File
@@ -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;
}
// Accessor methods
/**
* set the category
* @param object category the category object
*/
function setCategory( $category ) {
$this->category = $category;
}
/**
* set the course class variable
* @param course object Moodle course variable
*/
function setCourse( $course ) {
$this->course = $course;
}
/**
* 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;
}
/**
* set catfromfile
* @param bool catfromfile allow categories embedded in import file
*/
function setCatfromfile( $catfromfile ) {
$this->catfromfile = $catfromfile;
}
/// Importing functions
function importpreprocess($category, $course=NULL ) {
/// Does any pre-processing that may be desired
$this->category = $category; // Important
$this->course = $course;
/**
* Perform any required pre-processing
*/
function importpreprocess() {
return true;
}
/**
*
* @param $matchgrades string 'error' or 'nearest', mismatched grades handling
* Process the file
* This method should not normally be overidden
*/
function importprocess($filename, $matchgrades='error') {
/// Processes a given file. There's probably little need to change this
if (! $lines = $this->readdata($filename)) {
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) );
}
+16 -3
View 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');
@@ -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}&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");
}
@@ -221,7 +231,10 @@
<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>