dta_backend_utils.php 5.67 KB
Newer Older
1
<?php
2
// This file is part of Moodle - http://moodle.org/.
3
//
4
5
6
7
// 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.
8
//
9
10
11
12
// 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.
13
//
14
15
16
17
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
18
 * This file contains the backend webservice contact functionality for the DTA plugin.
19
 *
20
21
22
 * @package   assignsubmission_dta
 * @copyright 2023 Your Name
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
24
 */

25
namespace assignsubmission_dta;
26
27

/**
28
 * Backend webservice contact utility class.
29
 *
30
31
32
 * @package   assignsubmission_dta
 * @copyright 2023 Your Name
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
34
35
36
37
38
 */
class dta_backend_utils {

    /**
     * Component name for the plugin.
     */
39
    public const ASSIGNSUBMISSION_DTA_COMPONENT_NAME = 'assignsubmission_dta';
40
41
42

    /**
     * Returns the base URL of the backend webservice as configured in the administration settings.
43
44
     *
     * @return string Backend host base URL.
45
     */
46
    private static function assignsubmission_dta_get_backend_baseurl(): string {
47
48
49
50
        $backendaddress = get_config(
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
            'backendHost'
        );
51
52

        if (empty($backendaddress)) {
53
54
55
            \core\notification::error(
                get_string('backendHost_not_set', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)
            );
56
57
58
59
60
61
62
63
        }

        return $backendaddress;
    }

    /**
     * Sends the configuration text file uploaded by the teacher to the backend.
     *
64
65
66
     * @param \assign $assignment Assignment this test-config belongs to.
     * @param \stored_file $file Uploaded test-config.
     * @return bool True if no error occurred.
67
     */
68
69
    public static function assignsubmission_dta_send_testconfig_to_backend($assignment, $file): bool {
        $backendaddress = self::assignsubmission_dta_get_backend_baseurl();
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        if (empty($backendaddress)) {
            return true;
        }

        // Set endpoint for test upload.
        $url = $backendaddress . '/v1/unittest';

        // Prepare params.
        $params = [
            'unitTestFile' => $file,
            'assignmentId' => $assignment->get_instance()->id,
        ];

        // If request returned null, return false to indicate failure.
84
        if (is_null(self::assignsubmission_dta_post($url, $params))) {
85
86
87
88
89
90
91
92
93
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sends submission config or archive to backend to be tested.
     *
94
95
96
97
     * @param \assign $assignment Assignment for the submission.
     * @param int $submissionid Submission ID of the current file.
     * @param \stored_file $file Submission config file or archive with submission.
     * @return string|null JSON string with test results or null on error.
98
     */
99
100
101
102
103
    public static function assignsubmission_dta_send_submission_to_backend(
        $assignment,
        int $submissionid,
        $file
    ): ?string {
104
        $backendaddress = self::assignsubmission_dta_get_backend_baseurl();
105
106
107
108
109
110
111
112
113
114
115
116
117
        if (empty($backendaddress)) {
            return null;
        }

        // Set endpoint for submission upload.
        $url = $backendaddress . '/v1/task/' . $submissionid;

        // Prepare params.
        $params = [
            'taskFile' => $file,
            'assignmentId' => $assignment->get_instance()->id,
        ];

118
        return self::assignsubmission_dta_post($url, $params);
119
120
121
122
123
    }

    /**
     * Posts the given params to the given URL and returns the response as a string.
     *
124
125
126
     * @param string $url Full URL to request.
     * @param array $params Parameters for HTTP request.
     * @return string|null Received body on success or null on error.
127
     */
128
    private static function assignsubmission_dta_post(string $url, array $params): ?string {
129
130
131
132
133
134
135
136
137
        if (!isset($url) || !isset($params)) {
            return null;
        }

        $options = ['CURLOPT_RETURNTRANSFER' => true];

        $curl = new \curl();
        $response = $curl->post($url, $params, $options);

138
        // Check state of request, if response code is 2xx, return the answer.
139
140
141
142
143
        $info = $curl->get_info();
        if ($info['http_code'] >= 200 && $info['http_code'] < 300) {
            return $response;
        }

144
145
146
147
148
        // Something went wrong, return null and display an error message.
        $msg = self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME
            . ': Post file to server was not successful. HTTP code='
            . $info['http_code'];
        debugging($msg);
149
150

        if ($info['http_code'] >= 400 && $info['http_code'] < 500) {
151
152
153
            \core\notification::error(
                get_string('http_client_error_msg', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)
            );
154
155
            return null;
        } else if ($info['http_code'] >= 500 && $info['http_code'] < 600) {
156
157
158
            \core\notification::error(
                get_string('http_server_error_msg', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)
            );
159
160
            return null;
        } else {
161
162
163
            $unknownmsg = get_string('http_unknown_error_msg', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)
                . $info['http_code'] . ' ' . $response;
            \core\notification::error($unknownmsg);
164
165
166
167
            return null;
        }
    }
}