. use local_asystgrade\api\client; use local_asystgrade\api\http_client; use local_asystgrade\db\QuizQuery; defined('MOODLE_INTERNAL') || die(); /** * A hook function that will process the data and insert the rating value. * The function must be called on the desired page like https://www.moodle.loc/mod/quiz/report.php?id=2&mode=grading&slot=1&qid=1&grade=needsgrading&includeauto=1 * * @return void */ function local_asystgrade_before_footer() { global $PAGE; // Получение параметров из 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) { $quizQuery = new QuizQuery(); $question_attempts = $quizQuery->get_question_attempts($qid, $slot); $referenceAnswer = $quizQuery->get_reference_answer($qid); $data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer); $inputNames = $data['inputNames']; error_log('Data prepared: ' . print_r($data, true)); $apiendpoint = get_config('local_asystgrade', 'apiendpoint'); if (!$apiendpoint) { $apiendpoint = 'http://127.0.0.1:5000/api/autograde'; // Default setting } error_log('APIendpoint: ' . $apiendpoint); try { $httpClient = new http_client(); $apiClient = client::getInstance($apiendpoint, $httpClient); error_log('ApiClient initiated.'); error_log('Sending data to API and getting grade'); $response = $apiClient->send_data($data); $grades = json_decode($response, true); error_log('Grade obtained: ' . print_r($grades, true)); } catch (Exception $e) { error_log('Error sending data to API: ' . $e->getMessage()); return; } error_log('After API call'); pasteGradedMarks($grades, $inputNames); error_log('URL matches /mod/quiz/report.php in page_init'); } } /** * Adds JavasScript scrypt to update marks * * @param mixed $grades * @param mixed $inputNames * @return void */ function pasteGradedMarks(mixed $grades, mixed $inputNames): void { echo generate_script($grades, $inputNames);; } /** * @param QuizQuery $database * @param $question_attempts * @param $referenceAnswer * @return array */ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAnswer): array { $studentAnswers = []; $inputNames = []; foreach ($question_attempts as $question_attempt) { $quizattempt_steps = $database->get_attempt_steps($question_attempt->id); foreach ($quizattempt_steps as $quizattempt_step) { if ($quizattempt_step->state === 'complete') { $studentAnswer = $database->get_student_answer($quizattempt_step->id); $studentAnswers[] = $studentAnswer; $inputNames[] = "q" . $question_attempt->questionusageid . ":" . $question_attempt->slot . "_-mark"; error_log("Student Answer: $studentAnswer, Input Name: " . end($inputNames)); } } $quizattempt_steps->close(); } $question_attempts->close(); return [ 'referenceAnswer' => $referenceAnswer, 'studentAnswers' => $studentAnswers, 'inputNames' => $inputNames ]; } /** * Builds JavasScript scrypt to update marks using DOM manipulations * * @param $grades * @param $inputNames * @return string */ function generate_script($grades, $inputNames) { $script = ""; return $script; } /** * Autoloader registration */ spl_autoload_register(function ($classname) { // Check if the class name starts with our plugin's namespace if (strpos($classname, 'local_asystgrade\\') === 0) { // Transforming the Namespace into the Path $classname = str_replace('local_asystgrade\\', '', $classname); $classname = str_replace('\\', DIRECTORY_SEPARATOR, $classname); $filepath = __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $classname . '.php'; if (file_exists($filepath)) { require_once($filepath); } } });