Browse Source

Injecting Exercise object inside the answer

Julio Montoya 11 years ago
parent
commit
bd7165f071

+ 2 - 2
main/exercice/admin.php

@@ -256,11 +256,11 @@ if (!empty($clone_question) && !empty($objExercise->id)) {
     $new_question_obj->addToList($exerciseId);
 
     // This should be moved to the duplicate function
-    $new_answer_obj = new Answer($clone_question);
+    $new_answer_obj = new Answer($clone_question, null, $objExercise);
     $new_answer_obj->read();
     $new_answer_obj->duplicate($new_id);
 
-    //Reloading tne $objExercise obj
+    // Reloading tne $objExercise obj
     $objExercise->read($objExercise->id);
 
     header('Location: admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id);

+ 11 - 12
main/exercice/answer.class.php

@@ -45,10 +45,12 @@ class Answer
     /**
      * Constructor of the class
      *
-     * @author     Olivier Brouckaert
-     * @param     integer    Question ID that answers belong to
+     * @author Olivier Brouckaert
+     * @param  integer   Question ID that answers belong to
+     * @param int course id
+     * @param \Exercise
      */
-    public function Answer($questionId, $course_id = null)
+    public function Answer($questionId, $course_id = null, $exercise = null)
     {
         $this->questionId          = intval($questionId);
         $this->answer              = array();
@@ -72,16 +74,13 @@ class Answer
         $this->course    = $course_info;
         $this->course_id = $course_info['real_id'];
 
-        // fills arrays
-        $objExercise = new Exercise($this->course_id);
-        $exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null;
-
-        if ($exerciseId) {
-            $objExercise->read($exerciseId);
-            if ($objExercise->random_answers == '1') {
-                $this->readOrderedBy('rand()', ''); // randomize answers
+        if (isset($exercise)) {
+            if ($exercise->random_answers == '1') {
+                 // Randomize answers.
+                $this->readOrderedBy('rand()', '');
             } else {
-                $this->read(); // natural order
+                // Normal order
+                $this->read();
             }
         } else {
             $this->read();

+ 2 - 2
main/exercice/answer_admin.inc.php

@@ -55,7 +55,7 @@ if ($modifyIn) {
 
         // construction of the duplicated Answers
 
-        $objAnswer = new Answer($questionId);
+        $objAnswer = new Answer($questionId, null, $objExercise);
     }
     if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_COMBINATION || $answerType == GLOBAL_MULTIPLE_ANSWER) {
         $correct = unserialize($correct);
@@ -510,7 +510,7 @@ if ($modifyAnswers) {
     }
 
     // construction of the Answer object
-    $objAnswer = new Answer($questionId);
+    $objAnswer = new Answer($questionId, null, $objExercise);
 
     Session::write('objAnswer', $objAnswer);
     if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {

+ 2 - 2
main/exercice/draggable.class.php

@@ -33,7 +33,7 @@ class Draggable extends Matching
     }
 
     /**
-     * function which redifines Question::createAnswersForm
+     * Function which redefines Question::createAnswersForm
      * @param the formvalidator instance
      */
     public function createAnswersForm($form)
@@ -60,7 +60,7 @@ class Draggable extends Matching
 
         } else {
             if (!empty($this->id)) {
-                $answer = new Answer($this->id);
+                $answer = new Answer($this->id, api_get_course_int_id());
                 $answer->read();
                 if (count($answer->nbrAnswers) > 0) {
                     $nb_matches = $nb_options = 0;