Browse Source

Merge branch '1.11.x' of github.com:chamilo/chamilo-lms into 1.11.x

jmontoyaa 8 years ago
parent
commit
fa2e99f04f

+ 11 - 0
main/exercise/exercise.class.php

@@ -8487,6 +8487,17 @@ class Exercise
                         case FILL_IN_BLANKS:
                             $isCorrect = FillBlanks::isCorrect($answer['answer']);
                             break;
+                        case MATCHING:
+                            //no break
+                        case DRAGGABLE:
+                            //no break
+                        case MATCHING_DRAGGABLE:
+                            $isCorrect = Matching::isCorrect(
+                                $answer['position'],
+                                $answer['answer'],
+                                $answer['question_id']
+                            );
+                            break;
                         default:
                             $isCorrect = $objAnswer->isCorrectByAutoId($answer['answer']);
                     }

+ 25 - 0
main/exercise/matching.class.php

@@ -275,4 +275,29 @@ class Matching extends Question
 
         return $header;
     }
+
+    /**
+     * Check if a answer is correct
+     * @param int $position
+     * @param int $answer
+     * @return bool
+     */
+    public static function isCorrect($position, $answer, $questionId)
+    {
+        $em = Database::getManager();
+
+        $count = $em
+            ->createQuery('
+                SELECT COUNT(a) From ChamiloCourseBundle:CQuizAnswer a
+                WHERE a.iid = :position AND a.correct = :answer AND a.questionId = :question
+            ')
+            ->setParameters([
+                'position' => $position,
+                'answer' => $answer,
+                'question' => $questionId
+            ])
+            ->getSingleScalarResult();
+
+        return $count ? true : false;
+    }
 }

+ 69 - 62
main/inc/lib/exercise.lib.php

@@ -830,7 +830,7 @@ class ExerciseLib
                         }
                         break;
                     case DRAGGABLE:
-                        if ($answerCorrect != 0) {
+                        if ($answerCorrect) {
                             $parsed_answer = $answer;
                             /*$lines_count = '';
                             $data = $objAnswerTmp->getAnswerByAutoId($numAnswer);
@@ -847,24 +847,31 @@ class ExerciseLib
                                     'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option"
                                 ]
                             );
-                            $selectedValue = 0;
+
                             $draggableSelectOptions = [];
+                            $selectedValue = 0;
+                            $selectedIndex = 0;
 
-                            foreach ($select_items as $key => $val) {
-                                if ($debug_mark_answer) {
-                                    if ($val['id'] == $answerCorrect) {
-                                        $selectedValue = $val['id'];
+                            if ($user_choice) {
+                                foreach ($user_choice as $chosen) {
+                                    if ($answerCorrect != $chosen['answer']) {
+                                        continue;
                                     }
+
+                                    $selectedValue = $chosen['answer'];
                                 }
+                            }
 
-                                if (
-                                    isset($user_choice[$matching_correct_answer]) &&
-                                    $val['id'] == $user_choice[$matching_correct_answer]['answer']
-                                ) {
-                                    $selectedValue = $val['id'];
+                            foreach ($select_items as $key => $select_item) {
+                                $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
+                            }
+
+                            foreach ($draggableSelectOptions as $value => $text) {
+                                if ($value == $selectedValue) {
+                                    break;
                                 }
 
-                                $draggableSelectOptions[$val['id']] = $val['letter'];
+                                $selectedIndex++;
                             }
 
                             $s .= Display::select(
@@ -873,23 +880,22 @@ class ExerciseLib
                                 $selectedValue,
                                 [
                                     'id' => "window_{$windowId}_select",
-                                    'class' => 'select_option',
-                                    'style' => 'display: none;'
+                                    'class' => 'select_option hidden',
                                 ],
                                 false
                             );
 
-                            if (!empty($answerCorrect) && !empty($selectedValue)) {
-                                $s .= <<<JAVASCRIPT
-                                <script>
-                                    $(function() {
-                                        DraggableAnswer.deleteItem(
-                                            $('#{$questionId}_{$selectedValue}'),
-                                            $('#drop_$windowId')
-                                        );
-                                    });
-                                </script>
-JAVASCRIPT;
+                            if ($selectedValue && $selectedIndex) {
+                                $s .= "
+                                    <script>
+                                        $(function() {
+                                            DraggableAnswer.deleteItem(
+                                                $('#{$questionId}_$lines_count'),
+                                                $('#drop_{$questionId}_{$selectedIndex}')
+                                            );
+                                        });
+                                    </script>
+                                ";
                             }
 
                             if (isset($select_items[$lines_count])) {
@@ -900,7 +906,7 @@ JAVASCRIPT;
                                     ) . $select_items[$lines_count]['answer'],
                                     [
                                         'id' => "window_{$windowId}_answer",
-                                        'style' => 'display: none;'
+                                        'class' => 'hidden'
                                     ]
                                 );
                             } else {
@@ -940,35 +946,36 @@ JAVASCRIPT;
                                 </td>
                                 <td width="10%">
 HTML;
-                            $selectedValue = 0;
-                            $selectedPosition = 0;
-                            $questionOptions = [];
 
-                            $iTempt = 0;
+                            $draggableSelectOptions = [];
+                            $selectedValue = 0;
+                            $selectedIndex = 0;
 
-                            foreach ($select_items as $key => $val) {
-                                if ($debug_mark_answer) {
-                                    if ($val['id'] == $answerCorrect) {
-                                        $selectedValue = $val['id'];
-                                        $selectedPosition = $iTempt;
+                            if ($user_choice) {
+                                foreach ($user_choice as $chosen) {
+                                    if ($answerCorrect != $chosen['answer']) {
+                                        continue;
                                     }
+
+                                    $selectedValue = $chosen['answer'];
                                 }
+                            }
 
-                                if (
-                                    isset($user_choice[$matching_correct_answer]) &&
-                                    $val['id'] == $user_choice[$matching_correct_answer]['answer']
-                                ) {
-                                    $selectedValue = $val['id'];
-                                    $selectedPosition = $iTempt;
+                            foreach ($select_items as $key => $select_item) {
+                                $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
+                            }
+
+                            foreach ($draggableSelectOptions as $value => $text) {
+                                if ($value == $selectedValue) {
+                                    break;
                                 }
 
-                                $questionOptions[$val['id']] = $val['letter'];
-                                $iTempt++;
+                                $selectedIndex++;
                             }
 
                             $s .= Display::select(
                                 "choice[$questionId][$numAnswer]",
-                                $questionOptions,
+                                $draggableSelectOptions,
                                 $selectedValue,
                                 [
                                     'id' => "window_{$windowId}_select",
@@ -980,25 +987,25 @@ HTML;
                             if (!empty($answerCorrect) && !empty($selectedValue)) {
                                 // Show connect if is not freeze (question preview)
                                 if (!$freeze) {
-                                    $s .= <<<JAVASCRIPT
-                                <script>
-                                    $(document).on('ready', function () {
-                                        jsPlumb.ready(function() {
-                                            jsPlumb.connect({
-                                                source: 'window_$windowId',
-                                                target: 'window_{$questionId}_{$selectedPosition}_answer',
-                                                endpoint: ['Blank', {radius: 15}],
-                                                anchors: ['RightMiddle', 'LeftMiddle'],
-                                                paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
-                                                connector: [
-                                                    MatchingDraggable.connectorType,
-                                                    {curvines: MatchingDraggable.curviness}
-                                                ]
+                                    $s .= "
+                                        <script>
+                                            $(document).on('ready', function () {
+                                                jsPlumb.ready(function() {
+                                                    jsPlumb.connect({
+                                                        source: 'window_$windowId',
+                                                        target: 'window_{$questionId}_{$selectedIndex}_answer',
+                                                        endpoint: ['Blank', {radius: 15}],
+                                                        anchors: ['RightMiddle', 'LeftMiddle'],
+                                                        paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
+                                                        connector: [
+                                                            MatchingDraggable.connectorType,
+                                                            {curvines: MatchingDraggable.curviness}
+                                                        ]
+                                                    });
+                                                });
                                             });
-                                        });
-                                    });
-                                </script>
-JAVASCRIPT;
+                                        </script>
+                                    ";
                                 }
                             }
 

+ 6 - 10
main/template/default/exercise/submit.js.tpl

@@ -11,8 +11,6 @@ var DraggableAnswer = {
         item.fadeOut(function () {
             var $list = $('<ul>').addClass('gallery ui-helper-reset').appendTo(insertHere);
 
-            item.find('a.btn').remove();
-
             var droppedId = item.attr('id'),
                 dropedOnId = insertHere.attr('id'),
                 originSelectId = 'window_' + droppedId + '_select',
@@ -20,9 +18,11 @@ var DraggableAnswer = {
 
             $('#' + originSelectId + ' option')
                 .filter(function (index) {
-                    return index === parseInt(value);
+                    var position = insertHere.prop('id').split('_')[2];
+
+                    return index === parseInt(position);
                 })
-                .attr("selected", true);
+                .prop("selected", true);
 
             item.appendTo($list).fadeIn();
         });
@@ -30,11 +30,6 @@ var DraggableAnswer = {
     recycleItem: function (item) {
         item.fadeOut(function () {
             item
-                .find('a.btn')
-                .remove()
-                .end()
-                .find("img")
-                .end()
                 .appendTo(DraggableAnswer.gallery)
                 .fadeIn();
         });
@@ -42,7 +37,8 @@ var DraggableAnswer = {
         var droppedId = item.attr('id'),
             originSelectId = 'window_' + droppedId + '_select';
 
-        $('#' + originSelectId + ' option:first').attr('selected', 'selected');
+        $('#' + originSelectId + ' option').prop('selected', false);
+        $('#' + originSelectId + ' option:first').prop('selected', true);
     },
     init: function (gallery, trash) {
         this.gallery = gallery;