~openteachermaintainers/openteacher/3.x

« back to all changes in this revision

Viewing changes to modules/org/openteacher/logic/wordsString/javaScript/checker/checker.js

  • Committer: Marten de Vries
  • Date: 2017-06-28 18:05:48 UTC
  • Revision ID: git-v1:b4c406307aa345c58b9904b76580f15c5bff2a4e
Move JS into npm modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
        Copyright 2012-2013, Marten de Vries
3
 
 
4
 
        This file is part of OpenTeacher.
5
 
 
6
 
        OpenTeacher is free software: you can redistribute it and/or modify
7
 
        it under the terms of the GNU General Public License as published by
8
 
        the Free Software Foundation, either version 3 of the License, or
9
 
        (at your option) any later version.
10
 
 
11
 
        OpenTeacher is distributed in the hope that it will be useful,
12
 
        but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
        GNU General Public License for more details.
15
 
 
16
 
        You should have received a copy of the GNU General Public License
17
 
        along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>.
18
 
*/
19
 
 
20
 
var check = (function () {
21
 
        "use strict";
22
 
        var calculateDifference, arrayValuesEqual,
23
 
                checkSingleCompulsoryAnswerGiven,
24
 
                checkMultipleCompulsoryAnswersGiven;
25
 
 
26
 
        calculateDifference = function (a1, a2) {
27
 
                return a1.filter(function (item) {
28
 
                        return a2.indexOf(item) === -1;
29
 
                });
30
 
        };
31
 
 
32
 
        arrayValuesEqual = function (a1, a2) {
33
 
                var i, index;
34
 
 
35
 
                //copy, it's going to be modified
36
 
                a1 = a1.slice();
37
 
 
38
 
                for (i = 0; i < a2.length; i += 1) {
39
 
                        index = a1.indexOf(a2[i]);
40
 
                        if (index === -1) {
41
 
                                return false;
42
 
                        }
43
 
                        a1.splice(index, 1);
44
 
                }
45
 
                return a1.length === 0;
46
 
        };
47
 
 
48
 
        checkSingleCompulsoryAnswerGiven = function (givenAnswer, word) {
49
 
                var result, difference, compulsoryAnswer, oldDifference, i;
50
 
 
51
 
                result = {"result": "right"};
52
 
                difference = givenAnswer[0];
53
 
                for (i = 0; i < word.answers.length; i += 1) {
54
 
                        compulsoryAnswer = word.answers[i];
55
 
 
56
 
                        oldDifference = difference;
57
 
                        difference = calculateDifference(difference, compulsoryAnswer);
58
 
                        if (arrayValuesEqual(oldDifference, difference)) {
59
 
                                result = {"result": "wrong"};
60
 
                                break;
61
 
                        }
62
 
                }
63
 
                if (result.result === "right" && difference.length !== 0) {
64
 
                        result = {"result": "wrong"};
65
 
                }
66
 
                return result;
67
 
        };
68
 
 
69
 
        checkMultipleCompulsoryAnswersGiven = function (givenAnswer, word) {
70
 
                var result, compulsoryAnswerCount, compulsoryGivenAnswer, difference, i, j, compulsoryAnswer;
71
 
 
72
 
                result = {"result": "wrong"};
73
 
                compulsoryAnswerCount = 0;
74
 
 
75
 
                for (i = 0; i < givenAnswer.length; i += 1) {
76
 
                        compulsoryGivenAnswer = givenAnswer[i];
77
 
 
78
 
                        for (j = 0; j < word.answers.length; j += 1) {
79
 
                                compulsoryAnswer = word.answers[j];
80
 
 
81
 
                                difference = calculateDifference(compulsoryGivenAnswer, compulsoryAnswer);
82
 
                                if (difference.length === 0) {
83
 
                                        compulsoryAnswerCount += 1;
84
 
                                }
85
 
                        }
86
 
                }
87
 
                if (compulsoryAnswerCount === word.answers.length) {
88
 
                        result = {"result": "right"};
89
 
                }
90
 
                return result;
91
 
        };
92
 
 
93
 
        return function (givenAnswer, word) {
94
 
                var result;
95
 
 
96
 
                if (givenAnswer.length === 1) {
97
 
                        result = checkSingleCompulsoryAnswerGiven(givenAnswer, word);
98
 
                } else {
99
 
                        result = checkMultipleCompulsoryAnswersGiven(givenAnswer, word);
100
 
                }
101
 
 
102
 
                result.itemId = word.id;
103
 
                return result;
104
 
        };
105
 
}());