2
Copyright 2012-2013, Marten de Vries
4
This file is part of OpenTeacher.
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.
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.
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/>.
20
var check = (function () {
22
var calculateDifference, arrayValuesEqual,
23
checkSingleCompulsoryAnswerGiven,
24
checkMultipleCompulsoryAnswersGiven;
26
calculateDifference = function (a1, a2) {
27
return a1.filter(function (item) {
28
return a2.indexOf(item) === -1;
32
arrayValuesEqual = function (a1, a2) {
35
//copy, it's going to be modified
38
for (i = 0; i < a2.length; i += 1) {
39
index = a1.indexOf(a2[i]);
45
return a1.length === 0;
48
checkSingleCompulsoryAnswerGiven = function (givenAnswer, word) {
49
var result, difference, compulsoryAnswer, oldDifference, i;
51
result = {"result": "right"};
52
difference = givenAnswer[0];
53
for (i = 0; i < word.answers.length; i += 1) {
54
compulsoryAnswer = word.answers[i];
56
oldDifference = difference;
57
difference = calculateDifference(difference, compulsoryAnswer);
58
if (arrayValuesEqual(oldDifference, difference)) {
59
result = {"result": "wrong"};
63
if (result.result === "right" && difference.length !== 0) {
64
result = {"result": "wrong"};
69
checkMultipleCompulsoryAnswersGiven = function (givenAnswer, word) {
70
var result, compulsoryAnswerCount, compulsoryGivenAnswer, difference, i, j, compulsoryAnswer;
72
result = {"result": "wrong"};
73
compulsoryAnswerCount = 0;
75
for (i = 0; i < givenAnswer.length; i += 1) {
76
compulsoryGivenAnswer = givenAnswer[i];
78
for (j = 0; j < word.answers.length; j += 1) {
79
compulsoryAnswer = word.answers[j];
81
difference = calculateDifference(compulsoryGivenAnswer, compulsoryAnswer);
82
if (difference.length === 0) {
83
compulsoryAnswerCount += 1;
87
if (compulsoryAnswerCount === word.answers.length) {
88
result = {"result": "right"};
93
return function (givenAnswer, word) {
96
if (givenAnswer.length === 1) {
97
result = checkSingleCompulsoryAnswerGiven(givenAnswer, word);
99
result = checkMultipleCompulsoryAnswersGiven(givenAnswer, word);
102
result.itemId = word.id;