Kaynağa Gözat

Fix draggable/matching question

Julio Montoya 9 yıl önce
ebeveyn
işleme
2663362bde

+ 48 - 23
main/exercice/MatchingDraggable.php

@@ -24,42 +24,51 @@ class MatchingDraggable extends Question
     }
 
     /**
-     * Creates the form to create / edit the answers of the question
+     * function which redefines Question::createAnswersForm
      * @param FormValidator $form
      */
     public function createAnswersForm($form)
     {
-        $defaults = $matches = [];
+        $defaults = array();
         $nb_matches = $nb_options = 2;
+        $matches = array();
 
         $answer = null;
+        $counter = 1;
+
+        if (isset($this->id)) {
+            $answer = new Answer($this->id);
+            $answer->read();
+
+            if (count($answer->nbrAnswers) > 0) {
+                for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
+                    $correct = $answer->isCorrect($i);
+                    if (empty($correct)) {
+                        $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
+                        $counter++;
+                    }
+                }
+            }
+        }
 
         if ($form->isSubmitted()) {
             $nb_matches = $form->getSubmitValue('nb_matches');
             $nb_options = $form->getSubmitValue('nb_options');
-
             if (isset($_POST['lessMatches'])) {
                 $nb_matches--;
             }
-
             if (isset($_POST['moreMatches'])) {
                 $nb_matches++;
             }
-
             if (isset($_POST['lessOptions'])) {
                 $nb_options--;
             }
-
             if (isset($_POST['moreOptions'])) {
                 $nb_options++;
             }
         } else if (!empty($this->id)) {
-            $answer = new Answer($this->id);
-            $answer->read();
-
             if (count($answer->nbrAnswers) > 0) {
                 $nb_matches = $nb_options = 0;
-
                 for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
                     if ($answer->isCorrect($i)) {
                         $nb_matches++;
@@ -80,9 +89,16 @@ class MatchingDraggable extends Question
             $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
         }
 
-        for ($i = 1; $i <= $nb_options; ++$i) {
-            // fill the array with A, B, C.....
-            $matches[$i] = chr(64 + $i);
+        if (empty($matches)) {
+            for ($i = 1; $i <= $nb_options; ++$i) {
+                // fill the array with A, B, C.....
+                $matches[$i] = chr(64 + $i);
+            }
+        } else {
+            for ($i = $counter; $i <= $nb_options; ++$i) {
+                // fill the array with A, B, C.....
+                $matches[$i] = chr(64 + $i);
+            }
         }
 
         $form->addElement('hidden', 'nb_matches', $nb_matches);
@@ -115,20 +131,25 @@ class MatchingDraggable extends Question
                 '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
                 "answer[$i]"
             );
+
             $renderer->setElementTemplate(
                 '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
                 "matches[$i]"
             );
+
             $renderer->setElementTemplate(
                 '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
                 "weighting[$i]"
             );
 
             $form->addHtml('<tr>');
+
             $form->addHtml("<td>$i</td>");
             $form->addText("answer[$i]", null);
+
             $form->addSelect("matches[$i]", null, $matches);
             $form->addText("weighting[$i]", null, true, ['value' => 10]);
+
             $form->addHtml('</tr>');
         }
 
@@ -177,11 +198,10 @@ class MatchingDraggable extends Question
         }
 
         $form->addHtml('</table>');
-
+        $group = array();
         global $text;
 
         // setting the save button here and not in the question class.php
-        $group = [];
         $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
         $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
         $group[] = $form->addButtonSave($text, 'submitQuestion', true);
@@ -190,15 +210,17 @@ class MatchingDraggable extends Question
 
         if (!empty($this->id)) {
             $form->setDefaults($defaults);
-        } elseif ($this->isContent == 1) {
-            $form->setDefaults($defaults);
+        } else {
+            if ($this->isContent == 1) {
+                $form->setDefaults($defaults);
+            }
         }
 
         $form->setConstants(
-            [
+            array(
                 'nb_matches' => $nb_matches,
                 'nb_options' => $nb_options
-            ]
+            )
         );
     }
 
@@ -225,16 +247,19 @@ class MatchingDraggable extends Question
         // Insert the answers
         for ($i = 1; $i <= $nb_matches; ++$i) {
             $position++;
-
             $answer = $form->getSubmitValue("answer[$i]");
             $matches = $form->getSubmitValue("matches[$i]");
             $weighting = $form->getSubmitValue("weighting[$i]");
-
             $this->weighting += $weighting;
 
-            $objAnswer->createAnswer($answer, $matches, '', $weighting, $position);
+            $objAnswer->createAnswer(
+                $answer,
+                $matches,
+                '',
+                $weighting,
+                $position
+            );
         }
-
         $objAnswer->save();
         $this->save();
     }

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

@@ -617,12 +617,12 @@ class Answer
                         $correct = $answerList[$correct];
                     }*/
 
-                    $sql = "UPDATE $answerTable
+                    /*$sql = "UPDATE $answerTable
                         SET correct = $autoId
                         WHERE
                             id_auto = $autoId
                         ";
-                    Database::query($sql);
+                    Database::query($sql);*/
                 }
             }
         }

+ 15 - 13
main/exercice/exercise.class.php

@@ -2254,7 +2254,10 @@ class Exercise
             error_log('$answerType: '.$answerType);
         }
 
-        if ($answerType == FREE_ANSWER || $answerType == ORAL_EXPRESSION || $answerType == CALCULATED_ANSWER) {
+        if ($answerType == FREE_ANSWER ||
+            $answerType == ORAL_EXPRESSION ||
+            $answerType == CALCULATED_ANSWER
+        ) {
             $nbrAnswers = 1;
         }
 
@@ -2283,7 +2286,8 @@ class Exercise
         $user_answer = '';
 
         // Get answer list for matching
-        $sql = "SELECT id_auto, id, answer FROM $table_ans
+        $sql = "SELECT id_auto, id, answer
+                FROM $table_ans
                 WHERE c_id = $course_id AND question_id = $questionId";
         $res_answer = Database::query($sql);
 
@@ -2387,9 +2391,9 @@ class Exercise
                 case MULTIPLE_ANSWER: //2
                     if ($from_database) {
                         $choice = array();
-                        $queryans = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
-                                     WHERE exe_id = '".$exeId."' AND question_id= '".$questionId."'";
-                        $resultans = Database::query($queryans);
+                        $sql = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
+                                WHERE exe_id = '".$exeId."' AND question_id= '".$questionId."'";
+                        $resultans = Database::query($sql);
                         while ($row = Database::fetch_array($resultans)) {
                             $ind = $row['answer'];
                             $choice[$ind] = 1;
@@ -2792,7 +2796,8 @@ class Exercise
                             $res_user_answer = Database::query($sql);
 
                             if (Database::num_rows($res_user_answer) > 0) {
-                                $s_user_answer = Database::result($res_user_answer, 0, 0); //  rich - good looking
+                                //  rich - good looking
+                                $s_user_answer = Database::result($res_user_answer, 0, 0);
                             } else {
                                 $s_user_answer = 0;
                             }
@@ -2845,10 +2850,11 @@ class Exercise
                         break(2); // break the switch and the "for" condition
                     } else {
                         if ($answerCorrect) {
-                            if ($answerCorrect == $choice[$answerAutoId]) {
+                            if (isset($choice[$answerAutoId]) &&
+                                $answerCorrect == $choice[$answerAutoId]
+                            ) {
                                 $questionScore += $answerWeighting;
                                 $totalScore += $answerWeighting;
-
                                 $user_answer = Display::span($answerMatching[$choice[$answerAutoId]]);
                             } else {
                                 if (isset($answerMatching[$choice[$answerAutoId]])) {
@@ -2862,7 +2868,7 @@ class Exercise
                         }
                         break;
                     }
-                case HOT_SPOT :
+                case HOT_SPOT:
                     if ($from_database) {
                         $TBL_TRACK_HOTSPOT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
                         $sql = "SELECT hotspot_correct
@@ -2944,12 +2950,8 @@ class Exercise
                     break;
             } // end switch Answertype
 
-            global $origin;
-
             if ($show_result) {
-
                 if ($debug) error_log('show result '.$show_result);
-
                 if ($from == 'exercise_result') {
                     if ($debug) error_log('Showing questions $from '.$from);
                     //display answers (if not matching type, or if the answer is correct)

+ 3 - 2
main/exercice/matching.class.php

@@ -5,7 +5,8 @@
  *  Class Matching
  *  Matching questions type class
  *
- *	This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
+ *  This class allows to instantiate an object of
+ *  type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER)
  *	extending the class question
  *
  *	@author Eric Marguin
@@ -254,6 +255,7 @@ class Matching extends Question
             $matches = $form->getSubmitValue('matches['.$i.']');
             $weighting = $form->getSubmitValue('weighting['.$i.']');
             $this->weighting += $weighting;
+
             $objAnswer->createAnswer(
                 $answer,
                 $matches,
@@ -262,7 +264,6 @@ class Matching extends Question
                 $position
             );
         }
-
         $objAnswer->save();
         $this->save();
     }

+ 1 - 1
main/exercice/question.class.php

@@ -847,7 +847,7 @@ abstract class Question
             // creates a new question
             $sql = "SELECT max(position)
                     FROM $TBL_QUESTIONS as question,
-                        $TBL_EXERCISE_QUESTION as test_question
+                    $TBL_EXERCISE_QUESTION as test_question
                     WHERE
                         question.id = test_question.question_id AND
                         test_question.exercice_id = " . intval($exerciseId) . " AND

+ 0 - 33
main/exercice/testheaderpage.php

@@ -1,33 +0,0 @@
-<?php
-/* For licensing terms, see /license.txt */
-
-/**
-*	Code library for HotPotatoes integration.
-*	@package chamilo.exercise
-* 	@author Istvan Mandak
-*/
-require '../inc/global.inc.php';
-
-require_once(api_get_path(SYS_CODE_PATH).'exercice/hotpotatoes.lib.php');
-$documentPath= api_get_path(SYS_COURSE_PATH).$_course['path']."/document";
-$my_file = Security::remove_XSS($_GET['file']);
-$my_file=str_replace(array('../','\\..','\\0','..\\'),array('','','',''),urldecode($my_file));
-$title = GetQuizName($my_file,$documentPath);
-if ($title =='') {
-	$title = basename($my_file);
-}
-$nameTools = $title;
-$noPHP_SELF=true;
-if (isset($_SESSION['gradebook'])){
-	$gradebook=	$_SESSION['gradebook'];
-}
-
-if (!empty($gradebook) && $gradebook=='view') {
-	$interbreadcrumb[]= array (
-		'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
-		'name' => get_lang('ToolGradebook')
-	);
-}
-$interbreadcrumb[]= array ("url"=>"./exercise.php", "name"=> get_lang('Exercises'));
-Display::display_header($nameTools,"Exercise");
-echo "<a name='TOP'></a>";