Permalink
Browse files

MDL-24594 qtype_multichoice should allow images in the choices.

This change was massively re-worked by Tim Hunt to update Pierre Pichet's original patch for Moodle 2.0 to work in Moodle 2.1.
  • Loading branch information...
ppichet authored and timhunt committed Jun 16, 2011
1 parent 7a7d34f commit 2a6c5c52ee258b0097895c889cef949d8ab5ce72
@@ -2514,6 +2514,8 @@ protected function define_execution() {
$oldctxid, $this->task->get_userid(), 'question_created', $question->itemid, $newctxid, true);
restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'generalfeedback',
$oldctxid, $this->task->get_userid(), 'question_created', $question->itemid, $newctxid, true);
+ restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'answer',
+ $oldctxid, $this->task->get_userid(), 'question_answer', null, $newctxid, true);
restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'answerfeedback',
$oldctxid, $this->task->get_userid(), 'question_answer', null, $newctxid, true);
restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), 'question', 'hint',
@@ -218,34 +218,28 @@ public function import_headers($question) {
* @param array answer xml tree for single answer
* @return object answer object
*/
- public function import_answer($answer) {
- $fraction = $this->getpath($answer, array('@', 'fraction'), 0);
- $answertext = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
- $answerformat = $this->trans_format($this->getpath($answer,
+ public function import_answer($answer, $withanswerfiles = false) {
+ $ans = new stdClass();
+
+ $ans->answer = array();
+ $ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
+ $ans->answer['format'] = $this->trans_format($this->getpath($answer,
array('@', 'format'), 'moodle_auto_format'));
- $answerfiles = $this->import_files($this->getpath($answer,
- array('#', 'file'), array()));
+ if ($withanswerfiles) {
+ $ans->answer['files'] = $this->import_files($this->getpath($answer,
+ array('#', 'file'), array()));
+ }
- $feedbacktext = $this->getpath($answer,
+ $ans->feedback = array();
+ $ans->feedback['text'] = $this->getpath($answer,
array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
- $feedbackformat = $this->trans_format($this->getpath($answer,
+ $ans->feedback['format'] = $this->trans_format($this->getpath($answer,
array('#', 'feedback', 0, '@', 'format'), 'moodle_auto_format'));
- $feedbackfiles = $this->import_files($this->getpath($answer,
+ $ans->feedback['files'] = $this->import_files($this->getpath($answer,
array('#', 'feedback', 0, '#', 'file'), array()));
- $ans = new stdClass();
-
- $ans->answer = array();
- $ans->answer['text'] = $answertext;
- $ans->answer['format'] = $answerformat;
- $ans->answer['files'] = $answerfiles;
-
- $ans->feedback = array();
- $ans->feedback['text'] = $feedbacktext;
- $ans->feedback['format'] = $feedbackformat;
- $ans->feedback['files'] = $feedbackfiles;
+ $ans->fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
- $ans->fraction = $fraction / 100;
return $ans;
}
@@ -303,19 +297,13 @@ public function import_hint($hintxml) {
return $hint;
}
- $hint->hint = $this->getpath($hintxml,
- array('#', 'text', 0, '#'), '', true);
- $hinttext = array();
- $hinttext['text'] = $this->getpath($hintxml,
+ $hint = new stdClass();
+ $hint->hint['text'] = $this->getpath($hintxml,
array('#', 'text', 0, '#'), '', true);
- $hinttext['format'] = $this->trans_format($this->getpath($hintxml,
+ $hint->hint['format'] = $this->trans_format($this->getpath($hintxml,
array('@', 'format'), 'moodle_auto_format'));
-
- $hinttext['files'] = $this->import_files($this->getpath($hintxml,
+ $hint->hint['files'] = $this->import_files($this->getpath($hintxml,
array('#', 'file'), array(), false));
-
- $hint = new stdClass();
- $hint->hint = $hinttext;
$hint->shownumcorrect = array_key_exists('shownumcorrect', $hintxml['#']);
$hint->clearwrong = array_key_exists('clearwrong', $hintxml['#']);
$hint->options = $this->getpath($hintxml, array('#', 'options', 0, '#'), '', true);
@@ -395,7 +383,7 @@ public function import_multichoice($question) {
$answers = $question['#']['answer'];
$acount = 0;
foreach ($answers as $answer) {
- $ans = $this->import_answer($answer);
+ $ans = $this->import_answer($answer, true);
$qo->answer[$acount] = $ans->answer;
$qo->fraction[$acount] = $ans->fraction;
$qo->feedback[$acount] = $ans->feedback;
@@ -768,7 +756,7 @@ public function import_calculated($question) {
$qo->correctanswerlength = array();
$qo->feedback = array();
foreach ($answers as $answer) {
- $ans = $this->import_answer($answer);
+ $ans = $this->import_answer($answer, true);
// answer outside of <text> is deprecated
if (empty($ans->answer['text'])) {
$ans->answer['text'] = '*';
@@ -1065,6 +1053,8 @@ public function writequestion($question) {
$contextid, 'question', 'generalfeedback', $question->id);
if (!empty($question->options->answers)) {
foreach ($question->options->answers as $answer) {
+ $answer->answerfiles = $fs->get_area_files(
+ $contextid, 'question', 'answer', $answer->id);
$answer->feedbackfiles = $fs->get_area_files(
$contextid, 'question', 'answerfeedback', $answer->id);
}
@@ -1429,6 +1419,7 @@ public function write_answer($answer, $extra = '') {
$output = '';
$output .= " <answer fraction=\"$percent\" {$this->format($answer->answerformat)}>\n";
$output .= $this->writetext($answer->answer, 3);
+ $output .= $this->writefiles($answer->answerfiles);
$output .= " <feedback {$this->format($answer->feedbackformat)}>\n";
$output .= $this->writetext($answer->feedback, 4);
$output .= $this->writefiles($answer->feedbackfiles);
@@ -282,7 +282,7 @@ protected function definition_inner($mform) {
* field holding an array of answers
* @return array of form fields.
*/
- protected function get_per_answer_fields(&$mform, $label, $gradeoptions,
+ protected function get_per_answer_fields($mform, $label, $gradeoptions,
&$repeatedoptions, &$answersoption) {
$repeated = array();
$repeated[] = $mform->createElement('header', 'answerhdr', $label);
@@ -498,14 +498,31 @@ protected function data_preprocessing($question) {
* @param object $question the data being passed to the form.
* @return object $question the modified data.
*/
- protected function data_preprocessing_answers($question) {
+ protected function data_preprocessing_answers($question, $withanswerfiles = false) {
if (empty($question->options->answers)) {
return $question;
}
$key = 0;
foreach ($question->options->answers as $answer) {
- $question->answer[$key] = $answer->answer;
+ if ($withanswerfiles) {
+ // Prepare the feedback editor to display files in draft area
+ $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
+ $question->answer[$key]['text'] = file_prepare_draft_area(
+ $draftitemid, // draftid
+ $this->context->id, // context
+ 'question', // component
+ 'answer', // filarea
+ !empty($answer->id) ? (int) $answer->id : null, // itemid
+ $this->fileoptions, // options
+ $answer->answer // text
+ );
+ $question->answer[$key]['itemid'] = $draftitemid;
+ $question->answer[$key]['format'] = $answer->answerformat;
+ } else {
+ $question->answer[$key] = $answer->answer;
+ }
+
$question->fraction[$key] = 0 + $answer->fraction;
$question->feedback[$key] = array();
@@ -67,6 +67,22 @@ protected function definition_inner($mform) {
$this->add_interactive_settings(true, true);
}
+ protected function get_per_answer_fields($mform, $label, $gradeoptions,
+ &$repeatedoptions, &$answersoption) {
+ $repeated = array();
+ $repeated[] = $mform->createElement('header', 'answerhdr', $label);
+ $repeated[] = $mform->createElement('editor', 'answer',
+ get_string('answer', 'question'), array('rows' => 1), $this->editoroptions);
+ $repeated[] = $mform->createElement('select', 'fraction',
+ get_string('grade'), $gradeoptions);
+ $repeated[] = $mform->createElement('editor', 'feedback',
+ get_string('feedback', 'question'), array('rows' => 1), $this->editoroptions);
+ $repeatedoptions['answer']['type'] = PARAM_RAW;
+ $repeatedoptions['fraction']['default'] = 0;
+ $answersoption = 'answers';
+ return $repeated;
+ }
+
protected function data_preprocessing($question) {
$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_answers($question, true);
@@ -92,7 +108,7 @@ public function validation($data, $files) {
foreach ($answers as $key => $answer) {
//check no of choices
- $trimmedanswer = trim($answer);
+ $trimmedanswer = trim($answer['text']);
if (empty($trimmedanswer)) {
continue;
}
@@ -96,6 +96,10 @@ public function check_file_access($qa, $options, $component, $filearea, $args, $
array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
return $this->check_combined_feedback_file_access($qa, $options, $filearea);
+ } else if ($component == 'question' && $filearea == 'answer') {
+ $answerid = reset($args); // itemid is answer id.
+ return in_array($answerid, $this->order);
+
} else if ($component == 'question' && $filearea == 'answerfeedback') {
$answerid = reset($args); // itemid is answer id.
$response = $this->get_response($qa);
@@ -66,7 +66,7 @@ public function save_question_options($question) {
$maxfraction = -1;
$answers = array();
foreach ($question->answer as $key => $answerdata) {
- if ($answerdata == '') {
+ if (trim($answerdata['text']) == '') {
continue;
}
@@ -80,16 +80,10 @@ public function save_question_options($question) {
$answer->id = $DB->insert_record('question_answers', $answer);
}
- if (is_array($answerdata)) {
- // Doing an import
- $answer->answer = $this->import_or_save_files($answerdata,
- $context, 'question', 'answer', $answer->id);
- $answer->answerformat = $answerdata['format'];
- } else {
- // Saving the form
- $answer->answer = $answerdata;
- $answer->answerformat = FORMAT_HTML;
- }
+ // Doing an import
+ $answer->answer = $this->import_or_save_files($answerdata,
+ $context, 'question', 'answer', $answer->id);
+ $answer->answerformat = $answerdata['format'];
$answer->fraction = $question->fraction[$key];
$answer->feedback = $this->import_or_save_files($question->feedback[$key],
$context, 'question', 'answerfeedback', $answer->id);

0 comments on commit 2a6c5c5

Please sign in to comment.