backend.php 4.74 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This file is part of Moodle - http://moodle.org/
//
// 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/>.
16

Lückemeyer's avatar
Lückemeyer committed
17
18
19
20
21
22
/**
 * backend webservice contact utility class
 *
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @copyright Gero Lueckemeyer and student project teams
Lückemeyer's avatar
Lückemeyer committed
23
 */
24
25
26
class DtaBackendUtils {

    /**
Lückemeyer's avatar
Lückemeyer committed
27
     * Returns the base url of the backend webservice as configured in the administration settings.
28
29
     * @return string backend host base url
     */
30
    private static function getbackendbaseurl(): string {
31
        $backendaddress = get_config(assign_submission_dta::COMPONENT_NAME, "backendHost");
32

33
        if (empty($backendaddress)) {
34
35
36
            \core\notification::error(get_string("backendHost_not_set", assign_submission_dta::COMPONENT_NAME));
        }

37
        return $backendaddress;
38
39
40
    }

    /**
Lückemeyer's avatar
Lückemeyer committed
41
     * Sends the configuration textfile uploaded by prof to the backend.
42
     *
43
44
     * @param stdClass $assignment assignment this test-config belongs to
     * @param stdClass $file uploaded test-config
45
46
     * @return bool true if no error occurred
     */
47
    public static function sendtestconfigtobackend(stdClass $assignment, $file): bool {
48
        $backendaddress = self::getbackendbaseurl();
49
        if (empty($backendaddress)) {
50
51
52
            return true;
        }

53
        // Set endpoint for test upload.
54
        $url = $backendaddress . "/v1/unittest";
55

56
        // Prepare params.
57
        $params = [
58
            "unitTestFile" => $file,
59
60
            "assignmentId" => $assignment->get_instance()->id,
        ];
61

62
        // If request returned null, return false to indicate failure.
63
64
65
66
67
68
69
70
        if (is_null(self::post($url, $params))) {
            return false;
        } else {
            return true;
        }
    }

    /**
Lückemeyer's avatar
Lückemeyer committed
71
     * Sends sumbission config or archive to backend to be tested.
72
     *
73
74
     * @param stdClass $assignment assignment this submission is done for
     * @param stdClass $file submission config file or archive with submission
75
76
     * @return string json string with testresults or null on error
     */
77
    public static function sendsubmissiontobackend(stdClass $assignment, $file): ?string {
78
        $backendaddress = self::getbackendbaseurl();
79
        if (empty($backendaddress)) {
80
81
82
            return true;
        }

83
        // Set endpoint for test upload.
84
        $url = $backendaddress . "/v1/task";
85

86
        // Prepare params.
Lückemeyer's avatar
Lückemeyer committed
87
        $params = [
88
            "taskFile" => $file,
Lückemeyer's avatar
Lückemeyer committed
89
90
            "assignmentId" => $assignment->get_instance()->id,
        ];
91
92
93
94
95

        return self::post($url, $params);
    }

    /**
Lückemeyer's avatar
Lückemeyer committed
96
     * Posts the given params to the given url and returns the response as a string.
97
98
99
100
101
102
103
104
105
106
     * @param string $url full url to request to
     * @param array $params parameters for http-request
     *
     * @return string received body on success or null on error
     */
    private static function post($url, $params): ?string {
        if (!isset($url) || !isset($params)) {
            return false;
        }

107
        $options = ["CURLOPT_RETURNTRANSFER" => true];
108
109
110
111

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

112
        // Check state of request, if response code is a 2xx return the answer.
113
114
115
116
117
        $info = $curl->get_info();
        if ($info["http_code"] >= 200 && $info["http_code"] < 300) {
            return $response;
        }

118
119
120
        // Something went wrong, return null and give an error message.
        debugging(assign_submission_dta::COMPONENT_NAME . ": Post file to server was not successful: http_code=" .
        $info["http_code"]);
121
122
123
124
125
126
127
128

        if ($info['http_code'] >= 400 && $info['http_code'] < 500) {
            \core\notification::error(get_string("http_client_error_msg", assign_submission_dta::COMPONENT_NAME));
            return null;
        } else if ($info['http_code'] >= 500 && $info['http_code'] < 600) {
            \core\notification::error(get_string("http_server_error_msg", assign_submission_dta::COMPONENT_NAME));
            return null;
        } else {
129
130
            \core\notification::error(get_string("http_unknown_error_msg", assign_submission_dta::COMPONENT_NAME) .
            $info["http_code"] . $response);
131
132
133
134
135
            return null;
        }
    }

}