ajax_controller.php 2.16 KB
Newer Older
Artem Baranovskyi's avatar
Artem Baranovskyi committed
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace local_asystgrade\output;

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

class ajax_controller {

    public static function get_marks(): string | false {
        global $PAGE;

        // Получаем параметры из URL.
        $qid  = optional_param('qid', null, PARAM_INT);
        $slot = optional_param('slot', false, PARAM_INT);

        $quizQuery = new \local_asystgrade\db\quizquery();

        if ($quizQuery->gradesExist($qid, $slot)) {
            return json_encode(['status' => 'error', 'message' => 'Grades already exist in the database.']);
        }
var_dump('AJAX');
        $question_attempts = $quizQuery->get_question_attempts($qid, $slot);
        $referenceAnswer = $quizQuery->get_reference_answer($qid);
        $maxmark = (float)$question_attempts->current()->maxmark;
        $data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer);

        $studentData = $data['studentData'];
        $inputNames = array_column($studentData, 'inputName');
        var_dump('Entering AJAX function...');
        error_log('Before fetching question attempts');

        // API вызов для получения оценок
        try {
            $apiendpoint = get_config('local_asystgrade', 'apiendpoint') ?: 'http://flask:5000/api/autograde';

            $httpClient = new \local_asystgrade\api\http_client();
            $apiClient = \local_asystgrade\api\client::getInstance($apiendpoint, $httpClient);

            $response = $apiClient->send_data([
                'referenceAnswer' => $data['referenceAnswer'],
                'studentAnswers' => array_column($studentData, 'studentAnswer')
            ]);
            $grades = json_decode($response, true);
            error_log('After fetching question attempts');

        } catch (Exception $e) {
            return json_encode(['status' => 'error', 'message' => 'API Error: ' . $e->getMessage()]);
        }

        // Возвращаем JSON с оценками и другими данными
        return json_encode([
            'status' => 'success',
            'grades' => $grades,
            'inputNames' => $inputNames,
            'maxmark' => $maxmark
        ]);
    }
}