Projekt

Allgemein

Profil

Fehler #1199 » dynamic_exercises.js

For correcting Javascript function in website - Avishan Sharafi, 26.03.2025 22:44

 
1
window.addEventListener("keypress", function(event){ 
2

    
3
    if (event.key === "Enter") {
4
        event.preventDefault();
5
        const activeTextarea = document.activeElement;
6
        var control_button = $($(activeTextarea).siblings('.control_button')[0]);
7
        control_button.click();
8
    }
9
});   
10

    
11
$(document).on('click', ".control_button", function(e) {
12
            //Wird angeklickt, wenn eine Aufgabe gecheckt werden soll
13
            
14
            //Wir nehmen an, die Aufgabe war richtig und suchen nach Fehlern
15
            var correct = true;
16
            
17
            //Umgebendes Aufgabendiv finden
18
            var exercise = $($(this).parents('.aufgabe')[0]);
19
            
20
            //Eingabefelder checken
21
            exercise.find("input[type='text']").each(function(index){
22
                try {
23
                    var local_correct = true;
24
                    //console.log("------ " + $(this).val() + " " + $(this).next().text())
25
                    //console.log("result: " + isEqual($(this).val(),$(this).next().text()))
26
                    //console.log("result kommutativ: " + isCommutativeIdentical($(this).val(),$(this).next().text())
27
                    if ($(this).hasClass("formel")) {
28
                        //console.log(convertMathInput($(this).val()).mathjs);
29
                        //console.log(convertMathInput($(this).next().text()).mathjs);
30
                        if ($(this).parent().hasClass("formelGruppe")){
31
                            //console.log($(this).parent().find("input").index($(this)));
32
                            //console.log($(this).parent().find("input").size());
33

    
34
                            
35
                            //Wenn das letzte Element erreicht wurde, werten wir aus
36
                            if ($(this).parent().find("input").index($(this)) == $(this).parent().find("input").length-1) {
37
                                var possible_answers = new Array();
38
                                var answer_nr = 0;
39
                                
40
                                //Mögliche Antworten auslesen
41
                                $(this).parent().find("input").each(function(){
42
                                    possible_answers[answer_nr] = new Array();
43
                                    next_answer = $(this).next();
44
                                    i = 0;
45
                                    //p-divs ignorieren
46
                                    while (next_answer.hasClass("p")) {
47
                                        next_answer = next_answer.next();
48
                                        i++;
49
                                        if (i > 100) break;
50
                                    }
51
                                
52
                                    while(next_answer.hasClass("answer") || next_answer.hasClass("help_icon")) {
53
                                        if (!next_answer.hasClass("help_icon")) {
54
                                            possible_answers[answer_nr].push(next_answer.text());
55
                                        }
56
                                        
57
                                        //nächste Antwort suchen und p-divs ignorieren
58
                                        next_answer = next_answer.next();
59
                                        while (next_answer.hasClass("p")) {
60
                                            next_answer = next_answer.next();
61
                                            
62
                                            i++;
63
                                            if (i > 100) break;
64
                                        }
65
                                    }
66
                                    answer_nr++;
67
                                });
68
                                
69
                                $(this).parent().find("input").each(function(){
70
                                    var correct_solution_found = false;
71
                                    for (var j=0; j<possible_answers.length; j++) {
72
                                        for (var k=0; k<possible_answers[j].length; k++) {
73
                                            //console.log("" + $(this).val() + " == " + possible_answers[j][k] + ": " + isCommutativeIdentical($(this).val(),possible_answers[j][k]))
74
                                            if (isCommutativeIdentical($(this).val(),possible_answers[j][k])) {
75
                                                correct_solution_found = true;
76
                                                possible_answers.splice(j, 1);
77
                                                break;
78
                                            }
79
                                        }
80
                                        if (correct_solution_found) {
81
                                            break;
82
                                        }
83
                                    }
84
                                    
85
                                    if (!correct_solution_found) {
86
                                        correct = false;
87
                                        local_correct = false;
88
                                        $(this).addClass("wrong");
89
                                        $(this).removeClass("right_answer");
90
                                    } else {
91
                                        local_correct = true;
92
                                        $(this).removeClass("wrong");
93
                                        $(this).addClass("right_answer");
94
                                    }
95
                                });
96
                                
97
                                //console.log(possible_answers);
98
                            }
99
                        } else {//Wir werten die Eingabe aus und schauen nach, ob es mehrere Lösungsmöglichkeiten für das eine Feld gibt
100
                            var correct_solution_found = false;
101
                            next_answer = $(this).next();
102
                            i = 0;
103
                            //p-divs ignorieren
104
                            while (next_answer.hasClass("p")) {
105
                                next_answer = next_answer.next();
106
                                i++;
107
                                if (i > 100) break;
108
                            }
109
                            
110
                            while(next_answer.hasClass("answer") || next_answer.hasClass("help_icon")) {//Solange es noch antworten gibt
111
                                //console.log($(this).val() + "  " + next_answer.text());
112
                                if (!next_answer.hasClass("help_icon")) {
113
                                    //console.log("" + $(this).val() + " == " + next_answer.text());
114
                                    if (isCommutativeIdentical($(this).val(),next_answer.text())) {
115
                                        correct_solution_found = true;
116
                                        break;
117
                                    }
118
                                }
119
                                
120
                                //nächste Antwort suchen und p-divs ignorieren
121
                                next_answer = next_answer.next();
122
                                while (next_answer.hasClass("p")) {
123
                                    next_answer = next_answer.next();
124
                                    
125
                                    i++;
126
                                    if (i > 100) break;
127
                                }
128
                                
129
                                i++;
130
                                if (i > 100) break;
131
                            }
132
                            if (!correct_solution_found) {
133
                                correct = false;
134
                                local_correct = false;
135
                            }
136
                        }
137
                    } else if ($(this).hasClass("rechnung") && !isCalculatedCorrectly($(this).val(), $(this).next().text(), $(this).next().next().text())) {
138
                        correct = false;
139
                        local_correct = false;
140
                    }  else if ($(this).hasClass("antwortEingabe") && $(this).val().toLowerCase() != $(this).next().text().toLowerCase()) {
141
                        correct = false;
142
                        local_correct = false;
143
                    } 
144
                    
145
                    //Falsche Eingabefelder rot färben, richtige grün
146
                    if (!local_correct) {
147
                        $(this).addClass("wrong");
148
                        $(this).removeClass("right_answer");
149
                    } else {
150
                        $(this).removeClass("wrong");
151
                        $(this).addClass("right_answer");
152
                    }
153
                } catch (e) {
154
                    console.log("Auswertungsfehler abgefangen");
155
                    console.log(e);
156
                    //alert("exception");
157
                    $(this).focus();
158
                    $(this).tooltipster('content', "Diese Eingabe war leider ung" + unescape("%FC") + "ltig.<br/>(Zwischen Buchstabenvariablen muss<br/> das implizite Malzeichen als * eingegeben werden).");
159
                    exercise.find("input[type='text']").each(function(index){
160
                        $(this).removeClass("wrong");
161
                        $(this).removeClass("right_answer");
162
                    });
163
                    correct = false;
164
                }
165
            });
166
            
167
   
168
            
169
            
170
            //Dropdowns checken
171
            exercise.find("select").each(function(index){
172
                var local_correct = true;
173
                if ($(this).children("option:selected").attr("correct") != "1") {
174
                    correct = false;
175
                    local_correct = false;
176
                }
177
                 if (!local_correct) {
178
                    $(this).addClass("wrong");
179
                    $(this).removeClass("right_answer");
180
                } else {
181
                    $(this).removeClass("wrong");
182
                    $(this).addClass("right_answer");
183
                }
184
            });
185
            
186
            
187
            //Checkboxen checken
188
            exercise.find(".multiplechoice_aufgabe").each(function(index){
189
                var local_correct = true;
190
                //var correct_answers = $(this).attr("answer").split("-");
191
                $(this).find("input").each(function(index){
192
                    if ($(this).is(':checked') && $(this).attr("correct") != "1") {
193
                        correct = false;
194
                        local_correct = false;
195
                    }
196
                    if (!$(this).is(':checked') && $(this).attr("correct") != "0") {
197
                        correct = false;
198
                        local_correct = false;
199
                    }
200
                });
201
                if (!local_correct) {
202
                    $(this).addClass("wrong");
203
                    $(this).removeClass("right_answer");
204
                } else {
205
                    $(this).removeClass("wrong");
206
                    $(this).addClass("right_answer");
207
                }
208
            });
209
            
210
            
211
            //Radio-Buttons checken
212
            exercise.find(".singlechoice_aufgabe").each(function(index){
213
                var local_correct = true;
214
                //var correct_answers = $(this).attr("answer").split("-");
215
                var one_correct = false;
216
                $(this).find("input").each(function(index){
217
                    if ($(this).is(':checked') && $(this).attr("correct") == "1") {
218
                        one_correct = true;
219
                    }
220
                });
221
                if (!one_correct) {
222
                    correct = false;
223
                    local_correct = false;
224
                }
225
                /*
226
                 if (!correct) {
227
                    $(this).addClass("wrong");
228
                    $(this).removeClass("right_answer");
229
                } else {
230
                    $(this).removeClass("wrong");
231
                    $(this).addClass("right_answer");
232
                }*/
233
                if (!local_correct) {
234
                    $(this).addClass("wrong");
235
                    $(this).removeClass("right_answer");
236
                } else {
237
                    $(this).removeClass("wrong");
238
                    $(this).addClass("right_answer");
239
                }
240
            });
241
            
242

    
243
            if (correct) {
244
                $(this).next().children(":nth-child(2)").hide();
245
                $(this).next().children(":nth-child(1)").show();
246
                continue_video_of(exercise);
247
            } else {
248
                $(this).next().children(":nth-child(1)").hide();
249
                $(this).next().children(":nth-child(2)").show();
250
            }
251
            
252
            
253
        });
254

    
255
function init_input_with_tooltipster (input)  {
256
        input.tooltipster({
257
                //content: input.next().next().children("div"),
258
                content: "",
259
                autoClose: false,
260
                contentAsHTML: true,
261
                //theme: 'tooltipster-light'
262
            });
263
            input.tooltipster('disable');
264
            //console.log("add");
265
            input.focus(function(){
266
                if (input.val()!="") {
267
                    input.tooltipster('enable');
268
                    input.tooltipster('show');
269
                    MathJax.Hub.Queue(["Typeset", MathJax.Hub]); //MathJax nochmal rendern lassen
270
                }
271
            })
272
            
273
            input.blur(function(){
274
                input.tooltipster('disable');
275
                input.tooltipster('hide');
276
            });
277
}
278

    
279
function activate_input_tooltips () {
280
        /***/
281
        $("input.formel").each(function() {
282
            init_input_with_tooltipster($(this));
283
        });
284
        
285
        
286
        $(document).on("keyup", "input.formel", function() {
287
            if (!$(this).hasClass("tooltipstered")) {
288
                init_input_with_tooltipster($(this));
289
            }
290
            //console.log("keyup");
291
            content: $(this).next().next().children("div").text(convertMathInput($(this).val()).latex);
292
            var formula = convertMathInput($(this).val()).latex;
293
            if (formula != "") {
294
                $(this).tooltipster('content', "Interpretation der Formel:<br/><br/>\\(" + convertMathInput(replaceAll("\\.","{,,}", replaceAll(";",",",replaceAll(",","\\.",$(this).val())))).latex + "\\)");
295
                //console.log(convertMathInput(replaceAll("\\.","{,,}", replaceAll(";",",",replaceAll(",","\\.",$(this).val())))).latex);
296
            }
297
            if ($(this).val()=="") {
298
                $(this).tooltipster('disable');
299
                $(this).tooltipster('hide');
300
            } else {
301
                $(this).tooltipster('enable');
302
                $(this).tooltipster('show');
303
                tooltip = $(this)
304
                if (formula != "") {
305
                    MathJax.Hub.Queue(["Typeset", MathJax.Hub], function() {
306
                            tooltip.tooltipster("reposition");//Tooltip neu ausrichten (z.B. für Brüche, mit Potenzen im Nenner)
307
                    }); //MathJax nochmal rendern lassen
308

    
309
                }
310
            }
311
            
312
        });
313
        
314
        $('.formula_helper').tooltipster({
315
                //content: input.next().next().children("div"),
316
                //content: "In diesem Formeleingabefeld k&ouml;nnen Sie mathematische Formeln eingeben. Dazu stehen Ihnen unter Anderem die folgenden Eingabezeichen zur Verf&uuml;gung:<br/><br/>&nbsp; + Addition<br/>&nbsp; - Subtraktion<br/>&nbsp; * Multiplikation <br/>&nbsp; / Division (bzw. Bruchdarstellung)<br/>&nbsp; ^ f&uuml;r Potenzen<br/>&nbsp; sqrt() zur Eingabe von Wurzeln <br/>&nbsp; sqrt(n;a) zur Eingabe der n-ten Wurzeln aus a <br/>&nbsp; ( bzw. ) zur Klammerung von Ausdr&uuml;cken<br/>&nbsp; . bzw. , als Dezimaltrennzeichen<br/><br/>Probieren Sie es ruhig einmal aus! Die Interpretation Ihrer Eingabe als Formel wird Ihnen beim Tippen &uuml;ber dem Eingabefeld eingeblendet.",
317
            content: "In diesem Formeleingabefeld k&ouml;nnen Sie mathematische Formeln eingeben. Dazu stehen Ihnen unter Anderem die folgenden Eingabezeichen zur Verf&uuml;gung:<br/><br/>&nbsp; + Addition<br/>&nbsp; - Subtraktion<br/>&nbsp; * Multiplikation <br/>&nbsp; / Division (bzw. Bruchdarstellung)<br/>&nbsp; ^ f&uuml;r Potenzen - z.B. 2^3<br/>&nbsp; sqrt() zur Eingabe von Wurzeln - z.B. sqrt(3)<br/>&nbsp; ( bzw. ) zur Klammerung von Ausdr&uuml;cken<br/>&nbsp; . bzw. , als Dezimaltrennzeichen<br/>&nbsp; _ zur Darstellung von Indizes, z.B. t_R<br/>abs() oder | | f&uuml;r Betr&auml;ge<br/><br/>Probieren Sie es ruhig einmal aus! Die Interpretation Ihrer Eingabe als Formel wird Ihnen beim Tippen &uuml;ber dem Eingabefeld eingeblendet.",
318
                maxWidth: 350,
319
                contentAsHTML: true,
320
        });
321
        
322
}
323

    
324

    
325

    
326
function end_exercise(exercise) {
327
//     console.log("hallo Welt");
328
//     console.log(exercise);
329
    
330
    //Wir ermitteln das mutmaßliche Video-Aufgaben-Div
331
    if(!exercise.hasClass("zufallsaufgabe")){
332
        videoaufgaben_div = exercise.parent();
333
    }
334
    else {
335
        videoaufgaben_div = exercise.parent().parent();//In Zufallsaufgaben sind wir eine ebene tiever verschachtelt im DOM
336
    }
337

    
338
    /*Dieser Block würde dafür sorgen, dass Videos bei korrekter Antwort automatisch fortgesetzt werden. Das wollen wir gerade aber nicht.
339
    
340
    //Handelt es sich wirklich um eine Videoaufgabe?
341
    if(!videoaufgaben_div.hasClass("videoaufgabe_gesamt")) {
342
        console.log("EXIT, keine Videoaufgabe");
343
        return;
344
    }
345
    
346
    videoaufgaben_div.siblings("video")[0].play();
347
    videoaufgaben_div.hide();
348
    exercise.hide();+
349
    
350
    */
351
    
352
}
353

    
354
function continue_video_of(exercise) {
355
    end_exercise(exercise);
356
}
357

    
358
$(document).on('click', '.skip_exercise', function(e) {
359
    e.preventDefault();
360
    //console.log("Skip exercise");
361
    //$(this).parent().siblings(".aufgabe").hide();
362
    $(this).parent().parent().hide();
363
    $(this).parent().parent().siblings("video")[0].play();
364
});
365

    
366
$("body").bind('scroll', function() {
367
            //console.log("scroll")
368
            $("input.formel").each(function() {
369
                  try {
370
                    $(this).tooltipster('reposition');
371
                  } catch(e) {
372
                      //Wenn das fehlschlägt, ist das Tooltip aus irgend einem Grund noch nicht initialisiert und wir holen das nach
373
                      init_input_with_tooltipster($(this));
374
                  }
375
                });
376
        });
377

    
378
        // Fallback definition to fix Kontrolle button crash
379
function isCommutativeIdentical(input, expected) {
380
    const clean = str => str.replace(/\s+/g, '').replace(/\*\*/g, '^').replace(/=*$/, '');
381

    
382
    const a = clean(input);
383
    const b = clean(expected);
384

    
385
    return a === b;
386
}
(1-1/2)