An error occurred while loading the file. Please try again.
backend.php 3.64 KiB
<?php
class DtaBackendUtils {
    /**
     * @return string backend host base url
     */
    private static function getBackendBaseUrl(): string {
        $backendAddress = get_config(assign_submission_dta::COMPONENT_NAME, "backendHost");
        if (empty($backendAddress)) {
            \core\notification::error(get_string("backendHost_not_set", assign_submission_dta::COMPONENT_NAME));
        return $backendAddress;
    /**
     * 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 {
        $backendAddress = self::getBackendBaseUrl();
        if (empty($backendAddress)) {
            return true;
        // set endpoint for test upload
        $url = $backendAddress . "/v1/unittest";
        // prepare params
        $params = array(
            "unitTestFile" => $file,
            "assignmentId" => $assignment->get_instance()->id
        // if request returned null, return false to indicate failure
        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 {
        $backendAddress = self::getBackendBaseUrl();
        if (empty($backendAddress)) {
            return true;
        // set endpoint for test upload
        $url = $backendAddress . "/v1/task";
        // prepare params
        $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); // check state of request, if response code is a 2xx return the answer $info = $curl->get_info(); if ($info["http_code"] >= 200 && $info["http_code"] < 300) { return $response; } // Something went wrong, return null and give an error msg debugging(assign_submission_dta::COMPONENT_NAME . ": Post file to server was not successful: http_code=" . $info["http_code"]); 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 { \core\notification::error(get_string("http_unknown_error_msg", assign_submission_dta::COMPONENT_NAME) . $info["http_code"] . $response); return null; } } }