diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index d6a08e25495..a8e98ef964f 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -3676,6 +3676,11 @@ abstract class lesson_page extends lesson_base { const TYPE_QUESTION = 0; const TYPE_STRUCTURE = 1; + /** + * Constant used as a delimiter when parsing multianswer questions + */ + const MULTIANSWER_DELIMITER = '@^#|'; + /** * This method should return the integer used to identify the page type within * the database and throughout code. This maps back to the defines used in 1.x @@ -4097,37 +4102,81 @@ abstract class lesson_page extends lesson_base { $result->feedback .= '
' . get_string("youranswer", "lesson").' :
'; - $studentanswerarray = explode('
', $result->studentanswer); - $responsearr = explode('
', $result->response); - $studentanswerresponse = array_combine($studentanswerarray, $responsearr); // Create a table containing the answers and responses. $table = new html_table(); - foreach ($studentanswerresponse as $answer => $response) { + // Multianswer allowed. + if ($this->properties->qoption) { + $studentanswerarray = explode(self::MULTIANSWER_DELIMITER, $result->studentanswer); + $responsearr = explode(self::MULTIANSWER_DELIMITER, $result->response); + $studentanswerresponse = array_combine($studentanswerarray, $responsearr); + + foreach ($studentanswerresponse as $answer => $response) { + // Add a table row containing the answer. + $studentanswer = $this->format_answer($answer, $context, $result->studentanswerformat); + $table->data[] = array($studentanswer); + // If the response exists, add a table row containing the response. If not, add en empty row. + if (!empty(trim($response))) { + $studentresponse = isset($result->responseformat) ? + $this->format_response($response, $context, $result->responseformat, $options) : $response; + $table->data[] = array(''.get_string("response", "lesson"). + ':
'.$studentresponse); + } else { + $table->data[] = array(''); + } + } + } else { // Add a table row containing the answer. - $studentanswer = format_text($answer, $result->studentanswerformat, - array('context' => $context, 'para' => true)); + $studentanswer = $this->format_answer($result->studentanswer, $context, $result->studentanswerformat); $table->data[] = array($studentanswer); // If the response exists, add a table row containing the response. If not, add en empty row. - if (!empty(trim($response))) { - if (isset($result->responseformat)) { - $convertstudentresponse = file_rewrite_pluginfile_urls($response, 'pluginfile.php', - $context->id, 'mod_lesson', 'page_responses', $result->answerid); - $studentresponse = format_text($convertstudentresponse, $result->responseformat, $options); - } else { - $studentresponse = $response; - } + if (!empty(trim($result->response))) { + $studentresponse = isset($result->responseformat) ? + $this->format_response($result->response, $context, $result->responseformat, + $result->answerid, $options) : $result->response; $table->data[] = array(''.get_string("response", "lesson"). ':
'.$studentresponse); } else { $table->data[] = array(''); } } + $result->feedback .= html_writer::table($table).'
'; } } return $result; } + /** + * Formats the answer + * + * @param string $answer + * @param context $context + * @param int $answerformat + * @return string Returns formatted string + */ + private function format_answer($answer, $context, $answerformat) { + + return format_text($answer, $answerformat, array('context' => $context, 'para' => true)); + } + + /** + * Formats the response + * + * @param string $response + * @param context $context + * @param int $responseformat + * @param int $answerid + * @param stdClass $options + * @return string Returns formatted string + */ + private function format_response($response, $context, $responseformat, $answerid, $options) { + + $convertstudentresponse = file_rewrite_pluginfile_urls($response, 'pluginfile.php', + $context->id, 'mod_lesson', 'page_responses', $answerid); + + return format_text($convertstudentresponse, $responseformat, $options); + } + /** * Returns the string for a jump name * diff --git a/mod/lesson/pagetypes/multichoice.php b/mod/lesson/pagetypes/multichoice.php index 7f51e87b7a1..cb758ff08a9 100644 --- a/mod/lesson/pagetypes/multichoice.php +++ b/mod/lesson/pagetypes/multichoice.php @@ -174,7 +174,7 @@ class lesson_page_type_multichoice extends lesson_page { } } } - $result->studentanswer = implode('
', $studentanswerarray); + $result->studentanswer = implode(self::MULTIANSWER_DELIMITER, $studentanswerarray); $correctpageid = null; $wrongpageid = null; @@ -222,11 +222,11 @@ class lesson_page_type_multichoice extends lesson_page { if ((count($studentanswers) == $ncorrect) and ($nhits == $ncorrect)) { $result->correctanswer = true; - $result->response = implode('
', $responses); + $result->response = implode(self::MULTIANSWER_DELIMITER, $responses); $result->newpageid = $correctpageid; $result->answerid = $correctanswerid; } else { - $result->response = implode('
', $responses); + $result->response = implode(self::MULTIANSWER_DELIMITER, $responses); $result->newpageid = $wrongpageid; $result->answerid = $wronganswerid; }