. /** * The plugin uses the ASYST grading tool * modified to work as a web endpoint. * * This file contains the library functions for the local_asystgrade plugin. * * @package local_asystgrade * @copyright 2024 Artem Baranovskyi * @copyright based on work by 2023 Ulrike Pado , * @copyright Yunus Eryilmaz & Larissa Kirschner * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ 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. * The function must be called on the desired SAG page like * https://www.moodle.loc/mod/quiz/report.php?id=2&mode=grading&slot=1&qid=1&grade=needsgrading&includeauto=1 * * @return void * @throws coding_exception|moodle_exception */ function local_asystgrade_before_footer(): void { global $PAGE; // Obtaining parameters from URL. $qid = optional_param('qid', null, PARAM_INT); $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' => $apiendpoint, 'qid' => $qid, 'slot' => $slot, ]; $PAGE->requires->js(new moodle_url('/local/asystgrade/js/grade.js', ['v' => time()])); $PAGE->requires->js_init_call('M.local_asystgrade.init', [$jsdata]); } else { \core\notification::add( 'Flask API server is not running. Please check the server status.', notification::NOTIFY_ERROR ); } } } /** * Checks if the Flask backend is running. * * @param int $timeout The timeout in seconds. * @return bool True if the Flask backend is running, false otherwise. */ function is_flask_backend_running(int $timeout = 3): bool { $apiendpoint = utils::get_api_endpoint(); $urlcomponents = parse_url($apiendpoint); $host = $urlcomponents['host']; $port = $urlcomponents['port']; $connection = @fsockopen($host, $port, $errno, $errstr, $timeout); if (is_resource($connection)) { fclose($connection); return true; } else { // Displaying an error message in Moodle. \core\notification::add("Flask backend connection failed: $errstr ($errno)", notification::NOTIFY_ERROR); return false; } }