grade.js 1.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
M.local_asystgrade = {
    init: function(Y, js_data) { // YUI Module entry point
        window.gradeData = js_data;

        document.addEventListener('DOMContentLoaded', function() {
            const apiEndpoint = M.cfg.wwwroot + '/local/asystgrade/api.php';
            const gradesDataRequest = window.gradeData.request;

            if (gradesDataRequest) {
                fetch(apiEndpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(gradesDataRequest)
                })
                    .then(response => response.json())
                    .then(data => {
                        updateMarks(data.grades);
                    })
                    .catch(error => console.error('Error:', error));
            }

            function updateMarks(grades) {
                grades.forEach((grade, index) => {
                    console.log(grade, index)
                    const predictedGrade = grade.predicted_grade === 'correct' ? window.gradeData.maxmark : 0;
                    const inputName = window.gradeData.formNames[index];
                    const gradeInput = document.querySelector(`input[name="${inputName}"]`);

                    if (gradeInput) {
                        gradeInput.value = predictedGrade;
                    } else {
                        console.error(`Input field not found for name: ${inputName}`);
                    }
                });
            }
        });
    }
};