Merge branch 'MDL-32870' of git://github.com/jmvedrine/moodle
authorMarina Glancy <marina@moodle.com>
Tue, 7 Oct 2014 09:16:56 +0000 (17:16 +0800)
committerMarina Glancy <marina@moodle.com>
Tue, 7 Oct 2014 09:16:56 +0000 (17:16 +0800)
15 files changed:
mod/lesson/backup/moodle2/backup_lesson_stepslib.php
mod/lesson/backup/moodle2/restore_lesson_activity_task.class.php
mod/lesson/backup/moodle2/restore_lesson_stepslib.php
mod/lesson/editpage.php
mod/lesson/lang/en/lesson.php
mod/lesson/lib.php
mod/lesson/locallib.php
mod/lesson/pagetypes/matching.php
mod/lesson/pagetypes/multichoice.php
mod/lesson/pagetypes/numerical.php
mod/lesson/pagetypes/shortanswer.php
mod/lesson/pagetypes/truefalse.php
mod/lesson/report.php
mod/lesson/tests/behat/questions_images.feature [new file with mode: 0644]
mod/lesson/tests/fixtures/moodle_logo.jpg [new file with mode: 0644]

index 30367de..2ff5ef6 100644 (file)
@@ -190,6 +190,8 @@ class backup_lesson_activity_structure_step extends backup_activity_structure_st
         // Annotate the file areas in user by the lesson module.
         $lesson->annotate_files('mod_lesson', 'mediafile', null);
         $page->annotate_files('mod_lesson', 'page_contents', 'id');
+        $answer->annotate_files('mod_lesson', 'page_answers', 'id');
+        $answer->annotate_files('mod_lesson', 'page_responses', 'id');
 
         // Prepare and return the structure we have just created for the lesson module.
         return $this->prepare_activity_structure($lesson);
index 4b8351c..3dbbc90 100644 (file)
@@ -55,6 +55,7 @@ class restore_lesson_activity_task extends restore_activity_task {
         $contents = array();
 
         $contents[] = new restore_decode_content('lesson_pages', array('contents'), 'lesson_page');
+        $contents[] = new restore_decode_content('lesson_answers', array('answer', 'response'), 'lesson_answer');
 
         return $contents;
     }
index a06adb5..34e8371 100644 (file)
@@ -105,7 +105,7 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
 
         // Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when
         // processing attempts. It will be corrected in after_execute
-        $this->set_mapping('lesson_answer', $data->id, 0);
+        $this->set_mapping('lesson_answer', $data->id, 0, true); // Has related fileareas.
 
         // Answers need to be processed in order, so we store them in an
         // instance variable and insert them in the after_execute stage
@@ -186,7 +186,7 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
         ksort($this->answers);
         foreach ($this->answers as $answer) {
             $newitemid = $DB->insert_record('lesson_answers', $answer);
-            $this->set_mapping('lesson_answer', $answer->id, $newitemid);
+            $this->set_mapping('lesson_answer', $answer->id, $newitemid, true);
 
             // Update the lesson attempts to use the newly created answerid
             $DB->set_field('lesson_attempts', 'answerid', $newitemid, array(
@@ -199,6 +199,8 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
         $this->add_related_files('mod_lesson', 'mediafile', null);
         // Add lesson page files, by lesson_page itemname
         $this->add_related_files('mod_lesson', 'page_contents', 'lesson_page');
+        $this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
+        $this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
 
         // Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
         $rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
index e61107c..786ab33 100644 (file)
@@ -85,6 +85,31 @@ if ($edit) {
     $data->id = $cm->id;
     $editoroptions['context'] = $context;
     $data = file_prepare_standard_editor($data, 'contents', $editoroptions, $context, 'mod_lesson', 'page_contents',  $editpage->id);
+
+    $answerscount = 0;
+    $answers = $editpage->get_answers();
+    foreach ($answers as $answer) {
+        $answereditor = 'answer_editor['.$answerscount.']';
+        if (is_array($data->$answereditor)) {
+            $answerdata = $data->$answereditor;
+            $answerdraftid = file_get_submitted_draft_itemid($answereditor);
+            $answertext = file_prepare_draft_area($answerdraftid, $PAGE->cm->context->id,
+                    'mod_lesson', 'page_answers', $answer->id, $editoroptions, $answerdata['text']);
+            $data->$answereditor = array('text' => $answertext, 'format' => $answerdata['format'], 'itemid' => $answerdraftid);
+        }
+
+        $responseeditor = 'response_editor['.$answerscount.']';
+        if (is_array($data->$responseeditor)) {
+            $responsedata = $data->$responseeditor;
+            $responsedraftid = file_get_submitted_draft_itemid($responseeditor);
+            $responsetext = file_prepare_draft_area($responsedraftid, $PAGE->cm->context->id,
+                    'mod_lesson', 'page_responses', $answer->id, $editoroptions, $responsedata['text']);
+            $data->$responseeditor = array('text' => $responsetext, 'format' => $responsedata['format'],
+                    'itemid' => $responsedraftid);
+        }
+        $answerscount++;
+    }
+
     $mform->set_data($data);
     $PAGE->navbar->add(get_string('edit'), new moodle_url('/mod/lesson/edit.php', array('id'=>$id)));
     $PAGE->navbar->add(get_string('editingquestionpage', 'lesson', get_string($mform->qtypestring, 'lesson')));
index 81fd75c..594554f 100644 (file)
@@ -336,7 +336,9 @@ $string['page'] = 'Page: {$a}';
 $string['page-mod-lesson-x'] = 'Any lesson page';
 $string['page-mod-lesson-view'] = 'View or preview lesson page';
 $string['page-mod-lesson-edit'] = 'Edit lesson page';
+$string['pageanswers'] = 'Page answers';
 $string['pagecontents'] = 'Page contents';
+$string['pageresponses'] = 'Page responses';
 $string['pages'] = 'Pages';
 $string['pagetitle'] = 'Page title';
 $string['password'] = 'Password';
index 8e4c01b..fe2923d 100644 (file)
@@ -877,6 +877,13 @@ function lesson_pluginfile($course, $cm, $context, $filearea, $args, $forcedownl
         }
         $fullpath = "/$context->id/mod_lesson/$filearea/$pageid/".implode('/', $args);
 
+    } else if ($filearea === 'page_answers' || $filearea === 'page_responses') {
+        $itemid = (int)array_shift($args);
+        if (!$pageanswers = $DB->get_record('lesson_answers', array('id' => $itemid))) {
+            return false;
+        }
+        $fullpath = "/$context->id/mod_lesson/$filearea/$itemid/".implode('/', $args);
+
     } else if ($filearea === 'mediafile') {
         if (count($args) > 1) {
             // Remove the itemid when it appears to be part of the arguments. If there is only one argument
@@ -909,6 +916,8 @@ function lesson_get_file_areas() {
     $areas = array();
     $areas['page_contents'] = get_string('pagecontents', 'mod_lesson');
     $areas['mediafile'] = get_string('mediafile', 'mod_lesson');
+    $areas['page_answers'] = get_string('pageanswers', 'mod_lesson');
+    $areas['page_responses'] = get_string('pageresponses', 'mod_lesson');
     return $areas;
 }
 
index 9517df2..a2e37cc 100644 (file)
@@ -790,8 +790,19 @@ abstract class lesson_add_page_form_base extends moodleform {
         if ($label === null) {
             $label = get_string('answer', 'lesson');
         }
-        $this->_form->addElement('editor', 'answer_editor['.$count.']', $label, array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
-        $this->_form->setDefault('answer_editor['.$count.']', array('text'=>'', 'format'=>FORMAT_MOODLE));
+
+        if ($this->qtype != 'multichoice' && $this->qtype != 'matching') {
+            $this->_form->addElement('editor', 'answer_editor['.$count.']', $label,
+                    array('rows' => '4', 'columns' => '80'), array('noclean' => true));
+            $this->_form->setDefault('answer_editor['.$count.']', array('text' => '', 'format' => FORMAT_MOODLE));
+        } else {
+            $this->_form->addElement('editor', 'answer_editor['.$count.']', $label,
+                    array('rows' => '4', 'columns' => '80'),
+                    array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
+            $this->_form->setType('answer_editor['.$count.']', PARAM_RAW);
+            $this->_form->setDefault('answer_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
+        }
+
         if ($required) {
             $this->_form->addRule('answer_editor['.$count.']', get_string('required'), 'required', null, 'client');
         }
@@ -808,8 +819,12 @@ abstract class lesson_add_page_form_base extends moodleform {
         if ($label === null) {
             $label = get_string('response', 'lesson');
         }
-        $this->_form->addElement('editor', 'response_editor['.$count.']', $label, array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
-        $this->_form->setDefault('response_editor['.$count.']', array('text'=>'', 'format'=>FORMAT_MOODLE));
+        $this->_form->addElement('editor', 'response_editor['.$count.']', $label,
+                 array('rows' => '4', 'columns' => '80'),
+                 array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
+        $this->_form->setType('response_editor['.$count.']', PARAM_RAW);
+        $this->_form->setDefault('response_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
+
         if ($required) {
             $this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
         }
@@ -1861,6 +1876,8 @@ abstract class lesson_page extends lesson_base {
         $context = context_module::instance($cm->id);
         $fs = get_file_storage();
         $fs->delete_area_files($context->id, 'mod_lesson', 'page_contents', $this->properties->id);
+        $fs->delete_area_files($context->id, 'mod_lesson', 'page_answers', $this->properties->id);
+        $fs->delete_area_files($context->id, 'mod_lesson', 'page_responses', $this->properties->id);
 
         // repair the hole in the linkage
         if (!$this->properties->prevpageid && !$this->properties->nextpageid) {
@@ -2064,6 +2081,9 @@ abstract class lesson_page extends lesson_base {
                     $options->noclean = true;
                     $options->para = true;
                     $options->overflowdiv = true;
+                    $result->response = file_rewrite_pluginfile_urls($result->response, 'pluginfile.php', $context->id,
+                            'mod_lesson', 'page_responses', $result->answerid);
+
                     $result->feedback = $OUTPUT->box(format_text($this->get_contents(), $this->properties->contentsformat, $options), 'generalbox boxaligncenter');
                     $result->feedback .= '<div class="correctanswer generalbox"><em>'.get_string("youranswer", "lesson").'</em> : '.$result->studentanswer; // already in clean html
                     $result->feedback .= $OUTPUT->box($result->response, $class); // already conerted to HTML
@@ -2146,6 +2166,54 @@ abstract class lesson_page extends lesson_base {
         return true;
     }
 
+    /**
+     * save editor answers files and update answer record
+     *
+     * @param object $context
+     * @param int $maxbytes
+     * @param object $answer
+     * @param object $answereditor
+     * @param object $responseeditor
+     */
+    public function save_answers_files($context, $maxbytes, &$answer, $answereditor = '', $responseeditor = '') {
+        global $DB;
+        if (isset($answereditor['itemid'])) {
+            $answer->answer = file_save_draft_area_files($answereditor['itemid'],
+                    $context->id, 'mod_lesson', 'page_answers', $answer->id,
+                    array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $maxbytes),
+                    $answer->answer, null);
+            $DB->set_field('lesson_answers', 'answer', $answer->answer, array('id' => $answer->id));
+        }
+        if (isset($responseeditor['itemid'])) {
+            $answer->response = file_save_draft_area_files($responseeditor['itemid'],
+                    $context->id, 'mod_lesson', 'page_responses', $answer->id,
+                    array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $maxbytes),
+                    $answer->response, null);
+            $DB->set_field('lesson_answers', 'response', $answer->response, array('id' => $answer->id));
+        }
+    }
+
+    /**
+     * Rewrite urls in response and optionality answer of a question answer
+     *
+     * @param object $answer
+     * @param bool $rewriteanswer must rewrite answer
+     * @return object answer with rewritten urls
+     */
+    public static function rewrite_answers_urls($answer, $rewriteanswer = true) {
+        global $PAGE;
+
+        $context = context_module::instance($PAGE->cm->id);
+        if ($rewriteanswer) {
+            $answer->answer = file_rewrite_pluginfile_urls($answer->answer, 'pluginfile.php', $context->id,
+                    'mod_lesson', 'page_answers', $answer->id);
+        }
+        $answer->response = file_rewrite_pluginfile_urls($answer->response, 'pluginfile.php', $context->id,
+                'mod_lesson', 'page_responses', $answer->id);
+
+        return $answer;
+    }
+
     /**
      * Updates a lesson page and its answers within the database
      *
@@ -2201,6 +2269,10 @@ abstract class lesson_page extends lesson_base {
                     $DB->update_record("lesson_answers", $this->answers[$i]->properties());
                 }
 
+                // Save files in answers and responses.
+                $this->save_answers_files($context, $maxbytes, $this->answers[$i],
+                        $properties->answer_editor[$i], $properties->response_editor[$i]);
+
             } else if (isset($this->answers[$i]->id)) {
                 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
                 unset($this->answers[$i]);
@@ -2260,13 +2332,16 @@ abstract class lesson_page extends lesson_base {
      * @return array
      */
     public function create_answers($properties) {
-        global $DB;
+        global $DB, $PAGE;
         // now add the answers
         $newanswer = new stdClass;
         $newanswer->lessonid = $this->lesson->id;
         $newanswer->pageid = $this->properties->id;
         $newanswer->timecreated = $this->properties->timecreated;
 
+        $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
+        $context = context_module::instance($cm->id);
+
         $answers = array();
 
         for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
@@ -2289,6 +2364,13 @@ abstract class lesson_page extends lesson_base {
                     $answer->score = $properties->score[$i];
                 }
                 $answer->id = $DB->insert_record("lesson_answers", $answer);
+                if (isset($properties->response_editor[$i])) {
+                    $this->save_answers_files($context, $PAGE->course->maxbytes, $answer,
+                            $properties->answer_editor[$i], $properties->response_editor[$i]);
+                } else {
+                    $this->save_answers_files($context, $PAGE->course->maxbytes, $answer,
+                            $properties->answer_editor[$i]);
+                }
                 $answers[$answer->id] = new lesson_page_answer($answer);
             } else {
                 break;
index ea0f0f4..7c28403 100644 (file)
@@ -96,13 +96,16 @@ class lesson_page_type_matching extends lesson_page {
     }
 
     public function create_answers($properties) {
-        global $DB;
+        global $DB, $PAGE;
         // now add the answers
         $newanswer = new stdClass;
         $newanswer->lessonid = $this->lesson->id;
         $newanswer->pageid = $this->properties->id;
         $newanswer->timecreated = $this->properties->timecreated;
 
+        $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
+        $context = context_module::instance($cm->id);
+
         $answers = array();
 
         // need to add two to offset correct response and wrong response
@@ -127,6 +130,8 @@ class lesson_page_type_matching extends lesson_page {
 
             if (isset($answer->answer) && $answer->answer != '') {
                 $answer->id = $DB->insert_record("lesson_answers", $answer);
+                $this->save_answers_files($context, $PAGE->course->maxbytes,
+                        $answer, $properties->answer_editor[$i]);
                 $answers[$answer->id] = new lesson_page_answer($answer);
             } else if ($i < 2) {
                 $answer->id = $DB->insert_record("lesson_answers", $answer);
@@ -159,6 +164,9 @@ class lesson_page_type_matching extends lesson_page {
 
         $response = $data->response;
         $getanswers = $this->get_answers();
+        foreach ($getanswers as $key => $answer) {
+            $getanswers[$key] = parent::rewrite_answers_urls($answer);
+        }
 
         $correct = array_shift($getanswers);
         $wrong   = array_shift($getanswers);
@@ -220,6 +228,7 @@ class lesson_page_type_matching extends lesson_page {
         $n = 0;
 
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer);
             if ($n < 2) {
                 if ($answer->answer != null) {
                     $cells = array();
@@ -333,6 +342,9 @@ class lesson_page_type_matching extends lesson_page {
                 } else {
                     $DB->update_record("lesson_answers", $this->answers[$i]->properties());
                 }
+                // Save files in answers and responses.
+                $this->save_answers_files($context, $maxbytes, $this->answers[$i],
+                        $properties->answer_editor[$i], $properties->response_editor[$i]);
             } else if ($i < 2) {
                 if (!isset($this->answers[$i]->id)) {
                     $this->answers[$i]->id =  $DB->insert_record("lesson_answers", $this->answers[$i]);
@@ -340,6 +352,9 @@ class lesson_page_type_matching extends lesson_page {
                     $DB->update_record("lesson_answers", $this->answers[$i]->properties());
                 }
 
+                // Save files in answers and responses.
+                $this->save_answers_files( $context, $maxbytes, $this->answers[$i],
+                        $properties->answer_editor[$i], $properties->response_editor[$i]);
             } else if (isset($this->answers[$i]->id)) {
                 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
                 unset($this->answers[$i]);
@@ -460,12 +475,21 @@ class lesson_add_page_form_matching extends lesson_add_page_form_base {
     public function custom_definition() {
 
         $this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
-        $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
+        $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'),
+                array('rows' => '4', 'columns' => '80'),
+                array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
+        $this->_form->setType('answer_editor[0]', PARAM_RAW);
+        $this->_form->setDefault('answer_editor[0]', array('text' => '', 'format' => FORMAT_HTML));
         $this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
         $this->add_score(0, get_string("correctanswerscore", "lesson"), 1);
 
         $this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
-        $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'), array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
+        $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'),
+                array('rows' => '4', 'columns' => '80'),
+                array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
+        $this->_form->setType('answer_editor[1]', PARAM_RAW);
+        $this->_form->setDefault('answer_editor[1]', array('text' => '', 'format' => FORMAT_HTML));
+
         $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
         $this->add_score(1, get_string("wronganswerscore", "lesson"), 0);
 
@@ -488,7 +512,7 @@ class lesson_add_page_form_matching extends lesson_add_page_form_base {
 class lesson_display_answer_form_matching extends moodleform {
 
     public function definition() {
-        global $USER, $OUTPUT;
+        global $USER, $OUTPUT, $PAGE;
         $mform = $this->_form;
         $answers = $this->_customdata['answers'];
         $useranswers = $this->_customdata['useranswers'];
@@ -529,6 +553,9 @@ class lesson_display_answer_form_matching extends moodleform {
                     // Temporary fixed until MDL-38885 gets integrated
                     $mform->setType('response', PARAM_TEXT);
                 }
+                $context = context_module::instance($PAGE->cm->id);
+                $answer->answer = file_rewrite_pluginfile_urls($answer->answer, 'pluginfile.php', $context->id,
+                        'mod_lesson', 'page_answers', $answer->id);
                 $mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
                 $mform->setType($responseid, PARAM_TEXT);
                 if ($hasattempt) {
index 8d8409f..58e9ff0 100644 (file)
@@ -77,6 +77,8 @@ class lesson_page_type_multichoice extends lesson_page {
         foreach ($answers as $key=>$answer) {
             if ($answer->answer === '') {
                 unset($answers[$key]);
+            } else {
+                $answers[$key] = parent::rewrite_answers_urls($answer);
             }
         }
         return $answers;
@@ -246,6 +248,7 @@ class lesson_page_type_multichoice extends lesson_page {
             if (!$answer = $DB->get_record("lesson_answers", array("id" => $result->answerid))) {
                 print_error("Continue: answer record not found");
             }
+            $answer = parent::rewrite_answers_urls($answer);
             if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
                 $result->correctanswer = true;
             }
@@ -278,6 +281,7 @@ class lesson_page_type_multichoice extends lesson_page {
         $options->para = false;
         $i = 1;
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer);
             $cells = array();
             if ($this->lesson->custom && $answer->score > 0) {
                 // if the score is > 0, then it is correct
index 0285965..6a1b00b 100644 (file)
@@ -118,6 +118,7 @@ class lesson_page_type_numerical extends lesson_page {
         $options->para = false;
         $i = 1;
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer, false);
             $cells = array();
             if ($this->lesson->custom && $answer->score > 0) {
                 // if the score is > 0, then it is correct
index db6e79e..3b5c9a2 100644 (file)
@@ -76,6 +76,7 @@ class lesson_page_type_shortanswer extends lesson_page {
         $i=0;
         $answers = $this->get_answers();
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer, false);
             $i++;
             // Applying PARAM_TEXT as it is applied to the answer submitted by the user.
             $expectedanswer  = clean_param($answer->answer, PARAM_TEXT);
@@ -155,9 +156,9 @@ class lesson_page_type_shortanswer extends lesson_page {
             }
             if ($ismatch) {
                 $result->newpageid = $answer->jumpto;
-                if (trim(strip_tags($answer->response))) {
-                    $result->response = $answer->response;
-                }
+                $options = new stdClass();
+                $options->para = false;
+                $result->response = format_text($answer->response, $answer->responseformat, $options);
                 $result->answerid = $answer->id;
                 break; // quit answer analysis immediately after a match has been found
             }
@@ -182,6 +183,7 @@ class lesson_page_type_shortanswer extends lesson_page {
         $options->para = false;
         $i = 1;
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer, false);
             $cells = array();
             if ($this->lesson->custom && $answer->score > 0) {
                 // if the score is > 0, then it is correct
@@ -239,10 +241,13 @@ class lesson_page_type_shortanswer extends lesson_page {
     }
 
     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
+        global $PAGE;
+
         $answers = $this->get_answers();
         $formattextdefoptions = new stdClass;
         $formattextdefoptions->para = false;  //I'll use it widely in this page
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer, false);
             if ($useranswer == null && $i == 0) {
                 // I have the $i == 0 because it is easier to blast through it all at once.
                 if (isset($pagestats[$this->properties->id])) {
index 7a8a0f4..fcfa4bd 100644 (file)
@@ -50,6 +50,9 @@ class lesson_page_type_truefalse extends lesson_page {
     public function display($renderer, $attempt) {
         global $USER, $CFG, $PAGE;
         $answers = $this->get_answers();
+        foreach ($answers as $key => $answer) {
+            $answers[$key] = parent::rewrite_answers_urls($answer);
+        }
         shuffle($answers);
 
         $params = array('answers'=>$answers, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents(), 'attempt'=>$attempt);
@@ -81,6 +84,7 @@ class lesson_page_type_truefalse extends lesson_page {
         }
         $result->answerid = $data->answerid;
         $answer = $DB->get_record("lesson_answers", array("id" => $result->answerid), '*', MUST_EXIST);
+        $answer = parent::rewrite_answers_urls($answer);
         if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
             $result->correctanswer = true;
         }
@@ -104,6 +108,7 @@ class lesson_page_type_truefalse extends lesson_page {
         $options->para = false;
         $i = 1;
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer);
             $cells = array();
             if ($this->lesson->custom && $answer->score > 0) {
                 // if the score is > 0, then it is correct
@@ -193,6 +198,9 @@ class lesson_page_type_truefalse extends lesson_page {
                 } else {
                     $DB->update_record("lesson_answers", $this->answers[$i]->properties());
                 }
+                // Save files in answers and responses.
+                $this->save_answers_files($context, $maxbytes, $this->answers[$i],
+                        $properties->answer_editor[$i], $properties->response_editor[$i]);
             } else if (isset($this->answers[$i]->id)) {
                 $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
                 unset($this->answers[$i]);
@@ -238,6 +246,7 @@ class lesson_page_type_truefalse extends lesson_page {
         $formattextdefoptions->para = false;
         $formattextdefoptions->noclean = true;
         foreach ($answers as $answer) {
+            $answer = parent::rewrite_answers_urls($answer);
             if ($this->properties->qoption) {
                 if ($useranswer == null) {
                     $userresponse = array();
index 41e03d9..80ecc39 100644 (file)
@@ -584,7 +584,8 @@ if ($action === 'delete') {
                 $table->data[] = $modified;
             }
             if (isset($page->answerdata->response)) {
-                $table->data[] = array($fontstart.get_string("response", "lesson").": <br />".$fontend.$fontstart2.format_text($page->answerdata->response,$page->answerdata->responseformat,$formattextdefoptions).$fontend2, " ");
+                $table->data[] = array($fontstart.get_string("response", "lesson").": <br />".$fontend
+                        .$fontstart2.$page->answerdata->response.$fontend2, " ");
             }
             $table->data[] = array($page->answerdata->score, " ");
         } else {
diff --git a/mod/lesson/tests/behat/questions_images.feature b/mod/lesson/tests/behat/questions_images.feature
new file mode 100644 (file)
index 0000000..8ce6003
--- /dev/null
@@ -0,0 +1,97 @@
+@mod @mod_lesson
+Feature: In a lesson activity, teacher can add embedded images in questions answers and responses
+  As a teacher
+  I need to add questions with images in answers and responses
+
+  @javascript @_file_upload
+  Scenario: questions with images in answers and responses
+    Given the following "users" exist:
+      | username | firstname | lastname | email |
+      | teacher1 | Teacher | 1 | teacher1@asd.com |
+      | student1 | Student | 1 | student1@asd.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1 | 0 |
+    And the following "course enrolments" exist:
+      | user | course | role |
+      | teacher1 | C1 | editingteacher |
+      | student1 | C1 | student |
+    And I log in as "teacher1"
+    And I navigate to "My private files" node in "My profile"
+    And I upload "mod/lesson/tests/fixtures/moodle_logo.jpg" file to "Files" filemanager
+    And I click on "Save changes" "button"
+    When I am on homepage
+    And I follow "Course 1"
+    And I turn editing mode on
+    And I add a "Lesson" to section "1" and I fill the form with:
+      | Name | Test lesson name |
+    And I follow "Test lesson name"
+    And I follow "Add a question page"
+    And I set the field "Select a question type" to "Multichoice"
+    And I press "Add a question page"
+    And I set the following fields to these values:
+      | Page title | Multichoice question |
+      | Page contents | What animal is an amphibian? |
+      | id_answer_editor_0 | Frog |
+      | id_response_editor_0 | Correct answer |
+      | id_jumpto_0 | Next page |
+      | id_score_0 | 1 |
+      | id_answer_editor_1 | Cat |
+      | id_response_editor_1 | Incorrect answer |
+      | id_jumpto_1 | This page |
+      | id_score_1 | 0 |
+      | id_answer_editor_2 | Dog |
+      | id_response_editor_2 | Incorrect answer |
+      | id_jumpto_2 | This page |
+      | id_score_2 | 0 |
+    And I click on "Image" "button" in the "#fitem_id_answer_editor_2" "css_element"
+    And I click on "Browse repositories..." "button"
+    And I click on "Private files" "link"
+    And I click on "moodle_logo.jpg" "link"
+    And I click on "Select this file" "button"
+    And I set the field "Describe this image for someone who cannot see it" to "It's the logo"
+    And I click on "Save image" "button"
+    And I press "Save page"
+    And I set the field "qtype" to "Question"
+    And I set the field "Select a question type" to "True/false"
+    And I press "Add a question page"
+    And I set the following fields to these values:
+      | Page title | Next question |
+      | Page contents | Paper is made from trees. |
+      | id_answer_editor_0 | True |
+      | id_response_editor_0 | Correct |
+      | id_jumpto_0 | Next page |
+      | id_answer_editor_1 | False |
+      | id_response_editor_1 | Wrong |
+      | id_jumpto_1 | This page |
+    And I click on "Image" "button" in the "#fitem_id_response_editor_0" "css_element"
+    And I click on "Browse repositories..." "button"
+    And I click on "Private files" "link"
+    And I click on "moodle_logo.jpg" "link"
+    And I click on "Select this file" "button"
+    And I set the field "Describe this image for someone who cannot see it" to "It's the logo"
+    And I click on "Save image" "button"
+    And I press "Save page"
+    And I log out
+    And I log in as "student1"
+    And I follow "Course 1"
+    When I follow "Test lesson name"
+    Then I should see "What animal is an amphibian?"
+    And "//*[contains(@class, 'answeroption')]//img[contains(@src, 'pluginfile.php')]" "xpath_element" should exist
+    And "//*[contains(@class, 'answeroption')]//img[contains(@src, 'moodle_logo.jpg')]" "xpath_element" should exist
+    And I set the following fields to these values:
+      | Cat | 1 |
+    And I press "Submit"
+    And I should see "Incorrect answer"
+    And I press "Continue"
+    And I should see "Paper is made from trees."
+    And I set the following fields to these values:
+      | True | 1 |
+    And I press "Submit"
+    And I should see "Correct"
+    And I should not see "Wrong"
+    And "//img[contains(@src, 'pluginfile.php')]" "xpath_element" should exist in the ".correctanswer" "css_element"
+    And "//img[contains(@src, 'moodle_logo.jpg')]" "xpath_element" should exist in the ".correctanswer" "css_element"
+    And I press "Continue"
+    And I should see "Congratulations - end of lesson reached"
+    And I should see "Your score is 1 (out of 2)."
diff --git a/mod/lesson/tests/fixtures/moodle_logo.jpg b/mod/lesson/tests/fixtures/moodle_logo.jpg
new file mode 100644 (file)
index 0000000..f2d5365
Binary files /dev/null and b/mod/lesson/tests/fixtures/moodle_logo.jpg differ