diff --git a/api.php b/api.php index 6c6a5624c5ef1d0a2c8015c8cc6effaa9aa1030b..8dfb2a23c9ce684ad6461ba704deccd61ef6b212 100755 --- a/api.php +++ b/api.php @@ -31,6 +31,7 @@ require_once('lib.php'); use local_asystgrade\api\client; use local_asystgrade\api\http_client; +use local_asystgrade\utils; try { require_login(); @@ -48,13 +49,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($data) { // Preparing Flask API. try { - $apiendpoint = get_config('local_asystgrade', 'apiendpoint'); - if (!$apiendpoint) { - throw new dml_exception('missingconfig', 'local_asystgrade'); - } + $apiendpoint = utils::get_api_endpoint(); } catch (dml_exception $e) { - debugging('Config error: ' . $e->getMessage()); - $apiendpoint = 'http://127.0.0.1:5001/api/autograde'; // Default value. + debugging('Failed to get API endpoint setting: ' . $e->getMessage()); } $httpclient = new http_client(); diff --git a/classes/utils.php b/classes/utils.php new file mode 100644 index 0000000000000000000000000000000000000000..2371a096548d92bb33ba43ac155d495094c25af7 --- /dev/null +++ b/classes/utils.php @@ -0,0 +1,42 @@ +<?php +// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. + +/** + * The plugin uses the ASYST grading tool <https://transfer.hft-stuttgart.de/gitlab/ulrike.pado/ASYST> + * modified to work as a web endpoint. + * + * @package local_asystgrade + * @copyright 2024 Artem Baranovskyi <artem.baranovsky1980@gmail.com> + * @copyright based on work by 2023 Ulrike Pado <ulrike.pado@hft-stuttgart.de>, + * @copyright Yunus Eryilmaz & Larissa Kirschner <https://link.springer.com/article/10.1007/s40593-023-00383-w> + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_asystgrade; + +/** + * This class contains helper functions for the local_asystgrade plugin. + */ +class utils { + /** + * Gets api endpoint to access ASYST ML Backend + * @return string + * @throws \dml_exception + */ + public static function get_api_endpoint(): string { + return get_config('local_asystgrade', 'apiendpoint') ?? 'http://127.0.0.1:5001/api/autograde'; + } +} diff --git a/lib.php b/lib.php index b93d93531bf5bd5446cfccb0d7cc5e5382c6567b..37f59202bfbbaeb3ec8645dd824c0e237fd0e7dc 100755 --- a/lib.php +++ b/lib.php @@ -29,6 +29,7 @@ use core\exception\moodle_exception; use core\output\notification; +use local_asystgrade\utils; /** * A hook function that will process the data and insert the rating value. @@ -46,10 +47,14 @@ function local_asystgrade_before_footer(): void { $slot = optional_param('slot', false, PARAM_INT); if ($PAGE->url->compare(new moodle_url('/mod/quiz/report.php'), URL_MATCH_BASE) && $slot) { + $apiendpoint = utils::get_api_endpoint(); + $urlcomponents = parse_url($apiendpoint); + $host = $urlcomponents['host']; + $port = $urlcomponents['port']; if (is_flask_backend_running()) { $jsdata = [ - 'apiendpoint' => 'http://127.0.0.1:5001/api/autograde', + 'apiendpoint' => $apiendpoint, 'qid' => $qid, 'slot' => $slot ]; @@ -68,12 +73,14 @@ function local_asystgrade_before_footer(): void { /** * Checks if the Flask backend is running. * - * @param string $host The hostname or IP address. - * @param int $port The port number. * @param int $timeout The timeout in seconds. * @return bool True if the Flask backend is running, false otherwise. */ -function is_flask_backend_running(string $host = '127.0.0.1', int $port = 5001, int $timeout = 3): bool { +function is_flask_backend_running(int $timeout = 3): bool { + $apiendpoint = utils::get_api_endpoint(); + $urlcomponents = parse_url($apiendpoint); + $host = $urlcomponents['host'] ?? '127.0.0.1'; + $port = $urlcomponents['port'] ?? 5001; $connection = @fsockopen($host, $port, $errno, $errstr, $timeout); if (is_resource($connection)) { @@ -85,3 +92,13 @@ function is_flask_backend_running(string $host = '127.0.0.1', int $port = 5001, return false; } } + +/** + * Obtains URL Flask API from plugin settings. + * + * @return string URL API. + * @throws dml_exception + */ +function local_asystgrade_get_api_endpoint(): string { + return get_config('local_asystgrade', 'apiendpoint') ?? 'http://127.0.0.1:5001/api/autograde'; +} diff --git a/readme.md b/readme.md index e8d134af27efc5264885d555f2136f1502eeba53..67e891aab94d3badeb73acd8c931ca3a0b221004 100755 --- a/readme.md +++ b/readme.md @@ -165,8 +165,15 @@ or go to the Moodle folder with cd then enter to ./local/asystgrade/ and run ~~~bash docker-compose up flask -d ~~~ +Asyst ML Backend could be hosted and used not only at local server, but at some remoted services. +In this case it is possible to change an API address from http://127.0.0.1:5001/api/autograde to another. + -The stucture of request to ASYST ML Backend: +If ASYST ML microservice is running, the grade will appear at every student's answer. + + + +The structure of request to ASYST ML Backend: ~~~JSON { "referenceAnswer": "The reference answer", @@ -182,7 +189,7 @@ The stucture of request to ASYST ML Backend: **studentAnswers**: This array contains the answers submitted by students. Each answer is evaluated against the reference answer. -The stucture of responce from ASYST ML Backend: +The structure of response from ASYST ML Backend: ~~~JSON [ { diff --git a/settings.php b/settings.php index 2c18d8db0f89d68ad347c7133e9ecfeeb894edee..69b4192d5f5be583f457fd286497bd0d33953ab8 100755 --- a/settings.php +++ b/settings.php @@ -23,12 +23,9 @@ * @copyright Yunus Eryilmaz & Larissa Kirschner <https://link.springer.com/article/10.1007/s40593-023-00383-w> * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use local_asystgrade\utils; -defined('MOODLE_INTERNAL') || die(); // Security check, only internally call through Moodle allowed. -/** - * Default API endpoint for the AsystGrade ML Backend. - */ -const DEFAULT_API_ENDPOINT = 'http://127.0.0.1:5001/api/autograde'; +defined('MOODLE_INTERNAL') || die(); if ($hassiteconfig) { global $ADMIN; @@ -47,11 +44,13 @@ if ($hassiteconfig) { 'local_asystgrade/apiendpoint', get_string('apiendpoint', 'local_asystgrade'), get_string('apiendpoint_desc', 'local_asystgrade'), - DEFAULT_API_ENDPOINT, + utils::get_api_endpoint(), PARAM_URL )); } catch (coding_exception $e) { // Exception intentionally ignored because the setting might already exist or might not be critical. debugging('Failed to add API endpoint setting: ' . $e->getMessage()); + } catch (dml_exception $e) { + debugging('Failed to get API endpoint setting: ' . $e->getMessage()); } }