lib.php 4.11 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
<?php

/**
 * @package     local_asystgrade
 * @author      Artem Baranovskyi
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

23
use local_asystgrade\db\quizquery;
24
25
26
27
28
29
30
31
32
33
34
35

defined('MOODLE_INTERNAL') || die();

/**
 * A hook function that will process the data and insert the rating value.
 * The function must be called on the desired page like https://www.moodle.loc/mod/quiz/report.php?id=2&mode=grading&slot=1&qid=1&grade=needsgrading&includeauto=1
 *
 * @return void
 */

function local_asystgrade_before_footer()
{
36
    global $PAGE;
37
    // Obtaining parameters from URL
38
    $qid = optional_param('qid', null, PARAM_INT);
39
40
41
    $slot = optional_param('slot', false, PARAM_INT);

    if ($PAGE->url->compare(new moodle_url('/mod/quiz/report.php'), URL_MATCH_BASE) && $slot) {
42
        $quizQuery = new quizquery();
43

44
45
46
47
48
        if ($quizQuery->gradesExist($qid, $slot)) {
            error_log('Grades already exist in the database.');
            return;
        }

49
50
        $question_attempts = $quizQuery->get_question_attempts($qid, $slot);
        $referenceAnswer = $quizQuery->get_reference_answer($qid);
Artem Baranovskyi's avatar
Artem Baranovskyi committed
51
        $maxmark = (float)$question_attempts->current()->maxmark;
52
        $data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer);
53
54
55

        foreach (array_keys($data['studentData']) as $studentId) {
            if ($quizQuery->gradesExist($qid, $studentId)) {
56
57
58
                return;
            }
        }
59
60
61

        $studentData = $data['studentData'];
        $inputNames = array_column($studentData, 'inputName');
62

63
64
65
66
67
68
69
70
71
72
73
74
        $js_data = [
                'apiendpoint' => 'http://flask:5000/api/autograde',
                'formNames'   => $inputNames,
                'maxmark'     => $maxmark,
                'request'     => [
                        'referenceAnswer' => $data['referenceAnswer'],
                        'studentAnswers'  => array_column($studentData, 'studentAnswer')
                ]
        ];

        $PAGE->requires->js(new moodle_url('/local/asystgrade/js/grade.js', ['v' => time()]));
        $PAGE->requires->js_init_call('M.local_asystgrade.init', [$js_data]);
75
76
77
78
    }
}

/**
Artem Baranovskyi's avatar
Artem Baranovskyi committed
79
80
 * Processes question attempts and answers to prepare for API a data to estimate answers
 *
81
 * @param quizquery $database
82
83
84
85
 * @param $question_attempts
 * @param $referenceAnswer
 * @return array
 */
86
function prepare_api_data(quizquery $database, $question_attempts, $referenceAnswer): array
87
{
88
    $studentData = [];
89
90
91
92
93
94
95

    foreach ($question_attempts as $question_attempt) {
        $quizattempt_steps = $database->get_attempt_steps($question_attempt->id);

        foreach ($quizattempt_steps as $quizattempt_step) {
            if ($quizattempt_step->state === 'complete') {
                $studentAnswer = $database->get_student_answer($quizattempt_step->id);
96
97
98
99
100
101
102
103
104
105
                $studentId = $quizattempt_step->userid;
                $inputName = "q" . $question_attempt->questionusageid . ":" . $question_attempt->slot . "_-mark";

                // Adding data to an associative array
                $studentData[$studentId] = [
                    'studentAnswer' => $studentAnswer,
                    'inputName' => $inputName // identifying name for mark input field updating
                ];

                error_log("Student ID: $studentId, Student Answer: $studentAnswer, Input Name: $inputName");
106
107
            }
        }
108

109
        $quizattempt_steps->close();
110
    }
111
112
113
114
115

    $question_attempts->close();

    return [
        'referenceAnswer' => $referenceAnswer,
116
        'studentData' => $studentData
117
    ];
118
}