"dta/classes/view.php" did not exist on "e69d8ecd01c835732269ab60b470264e8c8c45ab"
backend.php 4.34 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
17
18
19
20
21
22

class DtaBackendUtils {

    /**
     * @return string backend host base url
     */
    private static function getBackendBaseUrl(): string {
23
        $backendaddress = get_config(assign_submission_dta::COMPONENT_NAME, "backendHost");
24

25
        if (empty($backendaddress)) {
26
27
28
            \core\notification::error(get_string("backendHost_not_set", assign_submission_dta::COMPONENT_NAME));
        }

29
        return $backendaddress;
30
31
32
33
34
35
36
37
38
39
40
    }

    /**
     * Sends the configuration textfile uploaded by prof to the backend
     *
     * @param $assignment assignment this test-config belongs to
     * @param $file uploaded test-config
     *
     * @return bool true if no error occurred
     */
    public static function sendTestConfigToBackend($assignment, $file): bool {
41
42
        $backendaddress = self::getBackendBaseUrl();
        if (empty($backendaddress)) {
43
44
45
            return true;
        }

46
        // Set endpoint for test upload.
47
        $url = $backendaddress . "/v1/unittest";
48

49
        // Prepare params.
50
51
52
53
54
        $params = array(
            "unitTestFile" => $file,
            "assignmentId" => $assignment->get_instance()->id
        );

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

    /**
     * Sends sumbission config or archive to backend to be tested
     *
     * @param $assignment assignment this submission is done for
     * @param $file submission config file or archive with submission
     *
     * @return string json string with testresults or null on error
     */
    public static function sendSubmissionToBackend($assignment, $file): ?string {
72
73
        $backendaddress = self::getBackendBaseUrl();
        if (empty($backendaddress)) {
74
75
76
            return true;
        }

77
        // Set endpoint for test upload.
78
        $url = $backendaddress . "/v1/task";
79

80
        // Prepare params.
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        $params = array(
            "taskFile" => $file,
            "assignmentId" => $assignment->get_instance()->id
        );

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

    /**
     * @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;
        }

        $options = array(
            "CURLOPT_RETURNTRANSFER" => true
        );

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

107
        // Check state of request, if response code is a 2xx return the answer.
108
109
110
111
112
        $info = $curl->get_info();
        if ($info["http_code"] >= 200 && $info["http_code"] < 300) {
            return $response;
        }

113
114
115
        // 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"]);
116
117
118
119
120
121
122
123

        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 {
124
125
            \core\notification::error(get_string("http_unknown_error_msg", assign_submission_dta::COMPONENT_NAME) .
            $info["http_code"] . $response);
126
127
128
129
130
            return null;
        }
    }

}