Project 'ulrike.pado/asyst-moodle-plugin' was moved to 'knight/asyst-moodle-plugin'. Please update any links and bookmarks that may still have the old path.
Commit bb361ef0 authored by Artem Baranovskyi's avatar Artem Baranovskyi
Browse files

Documentation update. Get API helper added.

parent 2f498d68
Showing with 80 additions and 18 deletions
+80 -18
......@@ -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();
......
<?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';
}
}
......@@ -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';
}
......@@ -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.
![API server failure.png](https://transfer.hft-stuttgart.de/gitlab/ulrike.pado/asyst-moodle-plugin/-/blob/asyst-moodle-plugin/images/API%20server%20%20failure.png)
The stucture of request to ASYST ML Backend:
If ASYST ML microservice is running, the grade will appear at every student's answer.
![Grading%20result.png](https://transfer.hft-stuttgart.de/gitlab/ulrike.pado/asyst-moodle-plugin/-/blob/asyst-moodle-plugin/images/Grading%20result.png)
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
[
{
......
......@@ -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());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment