From 8254083dc99b94929dd0188935152a6060841c72 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 23 Jun 2011 10:12:18 +0800 Subject: [PATCH 1/2] MDL-27964 lesson - Old file picker is replaced with new filepicker, currently all imported questions will be saved in /temp/questionimport/ where questionbank files are saved. --- mod/lesson/import.php | 12 ++++--- mod/lesson/import_form.php | 65 +++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/mod/lesson/import.php b/mod/lesson/import.php index 6d2921f2529..0b00e20a2d1 100644 --- a/mod/lesson/import.php +++ b/mod/lesson/import.php @@ -66,9 +66,13 @@ if ($data = $mform->get_data()) { require_sesskey(); - if (!$importfile = $mform->get_importfile_name()) { - print_error('uploadproblem', 'moodle'); - } + $realfilename = $mform->get_new_filename('questionfile'); + //TODO: Leave all imported questions in Questionimport for now. + $importfile = "{$CFG->dataroot}/temp/questionimport/{$realfilename}"; + make_upload_directory('temp/questionimport'); + if (!$result = $mform->save_file('questionfile', $importfile, true)) { + throw new moodle_exception('uploadproblem'); + } $formatclass = 'qformat_'.$data->format; $formatclassfile = $CFG->dirroot.'/question/format/'.$data->format.'/format.php'; @@ -102,4 +106,4 @@ if ($data = $mform->get_data()) { $mform->display(); } -echo $OUTPUT->footer(); +echo $OUTPUT->footer(); \ No newline at end of file diff --git a/mod/lesson/import_form.php b/mod/lesson/import_form.php index 4bd93d47965..79210b48dfd 100644 --- a/mod/lesson/import_form.php +++ b/mod/lesson/import_form.php @@ -32,8 +32,8 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class lesson_import_form extends moodleform { - public function definition() { + global $COURSE; $mform = $this->_form; @@ -48,30 +48,57 @@ class lesson_import_form extends moodleform { $mform->setType('format', 'text'); $mform->addRule('format', null, 'required'); - $mform->addElement('file', 'newfile', get_string('upload'), array('size'=>'50')); - $mform->addRule('newfile', null, 'required'); - - $this->add_action_buttons(null, get_string("uploadthisfile")); + //Using filemanager as filepicker + $mform->addElement('filepicker', 'questionfile', get_string('upload')); + $mform->addRule('questionfile', null, 'required', null, 'client'); + $this->add_action_buttons(null, get_string("import")); } - public function get_importfile_name(){ - if ($this->is_submitted() and $this->is_validated()) { - // return the temporary filename to process - return $_FILES['newfile']['tmp_name']; - }else{ - return NULL; + /** + * Checks that a file has been uploaded, and that it is of a plausible type. + * @param array $data the submitted data. + * @param array $errors the errors so far. + * @return array the updated errors. + */ + protected function validate_uploaded_file($data, $errors) { + global $CFG; + + if (empty($data['questionfile'])) { + $errors['questionfile'] = get_string('required'); + return $errors; } - } - public function get_importfile_realname(){ - if ($this->is_submitted() and $this->is_validated()) { - // return the temporary filename to process - // TODO change this to use the files API properly. - return $_FILES['newfile']['name']; - }else{ - return NULL; + $files = $this->get_draft_files('questionfile'); + if (count($files) < 1) { + $errors['questionfile'] = get_string('required'); + return $errors; } + + $formatfile = $CFG->dirroot.'/question/format/'.$data['format'].'/format.php'; + if (!is_readable($formatfile)) { + throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']); + } + + require_once($formatfile); + + $classname = 'qformat_' . $data['format']; + $qformat = new $classname(); + + $file = reset($files); + if ($file->get_mimetype() != $qformat->mime_type()) { + $a = new stdClass(); + $a->actualtype = $file->get_mimetype(); + $a->expectedtype = $qformat->mime_type(); + $errors['newfile'] = get_string('importwrongfiletype', 'lesson', $a); + } + + return $errors; } + public function validation($data, $files) { + $errors = parent::validation($data, $files); + $errors = $this->validate_uploaded_file($data, $errors); + return $errors; + } } \ No newline at end of file From 27beb68c4b03c76e5af2e98092ca427fe995be72 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 23 Jun 2011 14:24:55 +0800 Subject: [PATCH 2/2] MDL-27964 lesson - Quick fix added for importing question files in lesson module --- mod/lesson/format.php | 74 +++++++++++++++++++++++++++++++------- mod/lesson/import_form.php | 10 +----- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/mod/lesson/format.php b/mod/lesson/format.php index 5a2ea0c8798..230de281834 100644 --- a/mod/lesson/format.php +++ b/mod/lesson/format.php @@ -76,7 +76,8 @@ function lesson_save_question_options($question, $lesson) { $answer->timecreated = $timenow; $answer->grade = $question->fraction[$key] * 100; $answer->answer = $dataanswer; - $answer->response = $question->feedback[$key]; + $answer->response = $question->feedback[$key]['text']; + $answer->responseformat = $question->feedback[$key]['format']; $answer->id = $DB->insert_record("lesson_answers", $answer); $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { @@ -113,7 +114,8 @@ function lesson_save_question_options($question, $lesson) { $max = $question->answer[$key] + $question->tolerance[$key]; $answer->answer = $min.":".$max; // $answer->answer = $question->min[$key].":".$question->max[$key]; original line for min/max - $answer->response = $question->feedback[$key]; + $answer->response = $question->feedback[$key]['text']; + $answer->responseformat = $question->feedback[$key]['format']; $answer->id = $DB->insert_record("lesson_answers", $answer); $answers[] = $answer->id; @@ -139,12 +141,13 @@ function lesson_save_question_options($question, $lesson) { $answer->pageid = $question->id; $answer->timecreated = $timenow; $answer->answer = get_string("true", "quiz"); - $answer->grade = $question->answer * 100; + $answer->grade = $question->correctanswer * 100; if ($answer->grade > 50 ) { $answer->jumpto = LESSON_NEXTPAGE; } if (isset($question->feedbacktrue)) { - $answer->response = $question->feedbacktrue; + $answer->response = $question->feedbacktrue['text']; + $answer->responseformat = $question->feedbacktrue['format']; } $DB->insert_record("lesson_answers", $answer); @@ -154,12 +157,13 @@ function lesson_save_question_options($question, $lesson) { $answer->pageid = $question->id; $answer->timecreated = $timenow; $answer->answer = get_string("false", "quiz"); - $answer->grade = (1 - (int)$question->answer) * 100; + $answer->grade = (1 - (int)$question->correctanswer) * 100; if ($answer->grade > 50 ) { $answer->jumpto = LESSON_NEXTPAGE; } if (isset($question->feedbackfalse)) { - $answer->response = $question->feedbackfalse; + $answer->response = $question->feedbackfalse['text']; + $answer->responseformat = $question->feedbackfalse['format']; } $DB->insert_record("lesson_answers", $answer); @@ -191,8 +195,10 @@ function lesson_save_question_options($question, $lesson) { $answer->score = 1; } // end Replace - $answer->answer = $dataanswer; - $answer->response = $question->feedback[$key]; + $answer->answer = $dataanswer['text']; + $answer->answerformat = $dataanswer['format']; + $answer->response = $question->feedback[$key]['text']; + $answer->responseformat = $question->feedback[$key]['format']; $answer->id = $DB->insert_record("lesson_answers", $answer); // for Sanity checks if ($question->fraction[$key] > 0) { @@ -247,7 +253,8 @@ function lesson_save_question_options($question, $lesson) { $answertext = $question->subanswers[$key]; if (!empty($questiontext) and !empty($answertext)) { $answer = clone($defaultanswer); - $answer->answer = $questiontext; + $answer->answer = $questiontext['text']; + $answer->answerformat = $questiontext['format']; $answer->response = $answertext; if ($i == 0) { // first answer contains the correct answer jump @@ -309,7 +316,9 @@ class qformat_default { return false; } - echo $OUTPUT->notification(get_string('importcount', 'lesson', sizeof($questions))); + //Avoid category as question type + echo $OUTPUT->notification(get_string('importcount', 'lesson', + $this->count_questions($questions)), 'notifysuccess'); $count = 0; @@ -317,6 +326,9 @@ class qformat_default { foreach ($questions as $question) { // Process and store each question switch ($question->qtype) { + //TODO: Bad way to bypass category in data... Quickfix for MDL-27964 + case 'category': + break; // the good ones case SHORTANSWER : case NUMERICAL : @@ -325,7 +337,9 @@ class qformat_default { case MATCH : $count++; - echo "

$count. ".$question->questiontext."

"; + //Show nice formated question in one line. + echo "

$count. ".$this->format_question_text($question)."

"; + $newpage = new stdClass; $newpage->lessonid = $lesson->id; $newpage->qtype = $this->qtypeconvert[$question->qtype]; @@ -415,6 +429,27 @@ class qformat_default { return true; } + /** + * Count all non-category questions in the questions array. + * + * @param array questions An array of question objects. + * @return int The count. + * + */ + protected function count_questions($questions) { + $count = 0; + if (!is_array($questions)) { + return $count; + } + foreach ($questions as $question) { + if (!is_object($question) || !isset($question->qtype) || + ($question->qtype == 'category')) { + continue; + } + $count++; + } + return $count; + } function readdata($filename) { /// Returns complete file with an array, one item per line @@ -484,7 +519,7 @@ class qformat_default { $question = new stdClass(); $question->shuffleanswers = get_config('quiz', 'shuffleanswers'); - $question->defaultgrade = 1; + $question->defaultmark = 1; $question->image = ""; $question->usecase = 0; $question->multiplier = array(); @@ -498,6 +533,11 @@ class qformat_default { $question->qoption = 0; $question->layout = 1; + // this option in case the questiontypes class wants + // to know where the data came from + $question->export_process = true; + $question->import_process = true; + return $question; } @@ -508,6 +548,16 @@ class qformat_default { return true; } + /** + * Convert the question text to plain text, so it can safely be displayed + * during import to let the user see roughly what is going on. + */ + protected function format_question_text($question) { + $formatoptions = new stdClass(); + $formatoptions->noclean = true; + return html_to_text(format_text($question->questiontext, + $question->questiontextformat, $formatoptions), 0, false); + } } diff --git a/mod/lesson/import_form.php b/mod/lesson/import_form.php index 79210b48dfd..765d565bdcb 100644 --- a/mod/lesson/import_form.php +++ b/mod/lesson/import_form.php @@ -32,8 +32,8 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class lesson_import_form extends moodleform { + public function definition() { - global $COURSE; $mform = $this->_form; @@ -85,14 +85,6 @@ class lesson_import_form extends moodleform { $classname = 'qformat_' . $data['format']; $qformat = new $classname(); - $file = reset($files); - if ($file->get_mimetype() != $qformat->mime_type()) { - $a = new stdClass(); - $a->actualtype = $file->get_mimetype(); - $a->expectedtype = $qformat->mime_type(); - $errors['newfile'] = get_string('importwrongfiletype', 'lesson', $a); - } - return $errors; }