diff --git a/mod/lesson/format.php b/mod/lesson/format.php index 397640844d1..37c447a139f 100644 --- a/mod/lesson/format.php +++ b/mod/lesson/format.php @@ -57,6 +57,17 @@ function lesson_save_question_options($question, $lesson) { $timenow = time(); $result = new stdClass(); + + // Default answer to avoid code duplication. + $defaultanswer = new stdClass(); + $defaultanswer->lessonid = $question->lessonid; + $defaultanswer->pageid = $question->id; + $defaultanswer->timecreated = $timenow; + $defaultanswer->answerformat = FORMAT_HTML; + $defaultanswer->jumpto = LESSON_THISPAGE; + $defaultanswer->grade = 0; + $defaultanswer->score = 0; + switch ($question->qtype) { case LESSON_PAGE_SHORTANSWER: @@ -66,13 +77,10 @@ function lesson_save_question_options($question, $lesson) { // Insert all the new answers foreach ($question->answer as $key => $dataanswer) { if ($dataanswer != "") { - $answer = new stdClass; - $answer->lessonid = $question->lessonid; - $answer->pageid = $question->id; + $answer = clone($defaultanswer); if ($question->fraction[$key] >=0.5) { $answer->jumpto = LESSON_NEXTPAGE; } - $answer->timecreated = $timenow; $answer->grade = round($question->fraction[$key] * 100); $answer->answer = $dataanswer; $answer->response = $question->feedback[$key]['text']; @@ -103,11 +111,8 @@ function lesson_save_question_options($question, $lesson) { // for each answer store the pair of min and max values even if they are the same foreach ($question->answer as $key => $dataanswer) { if ($dataanswer != "") { - $answer = new stdClass; - $answer->lessonid = $question->lessonid; - $answer->pageid = $question->id; + $answer = clone($defaultanswer); $answer->jumpto = LESSON_NEXTPAGE; - $answer->timecreated = $timenow; $answer->grade = round($question->fraction[$key] * 100); $min = $question->answer[$key] - $question->tolerance[$key]; $max = $question->answer[$key] + $question->tolerance[$key]; @@ -136,10 +141,7 @@ function lesson_save_question_options($question, $lesson) { case LESSON_PAGE_TRUEFALSE: // the truth - $answer = new stdClass(); - $answer->lessonid = $question->lessonid; - $answer->pageid = $question->id; - $answer->timecreated = $timenow; + $answer = clone($defaultanswer); $answer->answer = get_string("true", "quiz"); $answer->grade = $question->correctanswer * 100; if ($answer->grade > 50 ) { @@ -152,10 +154,7 @@ function lesson_save_question_options($question, $lesson) { $DB->insert_record("lesson_answers", $answer); // the lie - $answer = new stdClass; - $answer->lessonid = $question->lessonid; - $answer->pageid = $question->id; - $answer->timecreated = $timenow; + $answer = clone($defaultanswer); $answer->answer = get_string("false", "quiz"); $answer->grade = (1 - (int)$question->correctanswer) * 100; if ($answer->grade > 50 ) { @@ -179,10 +178,7 @@ function lesson_save_question_options($question, $lesson) { // Insert all the new answers foreach ($question->answer as $key => $dataanswer) { if ($dataanswer != "") { - $answer = new stdClass; - $answer->lessonid = $question->lessonid; - $answer->pageid = $question->id; - $answer->timecreated = $timenow; + $answer = clone($defaultanswer); $answer->grade = round($question->fraction[$key] * 100); // changed some defaults /* Original Code @@ -231,12 +227,6 @@ function lesson_save_question_options($question, $lesson) { $subquestions = array(); - $defaultanswer = new stdClass; - $defaultanswer->lessonid = $question->lessonid; - $defaultanswer->pageid = $question->id; - $defaultanswer->timecreated = $timenow; - $defaultanswer->grade = 0; - // The first answer should always be the correct answer $correctanswer = clone($defaultanswer); $correctanswer->answer = get_string('thatsthecorrectanswer', 'lesson');