<?php // This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>. class DbUtils { // summary database table name private const TABLE_SUMMARY = "assignsubmission_dta_summary"; // result database table name private const TABLE_RESULT = "assignsubmission_dta_result"; /** * get's summary with all corresponding result entries * * @param int $assignmentid assignment id to search for * @param int $submissionid submission id to search for * @return DttResultSummary representing given submission */ public static function getResultSummaryFromDatabase( int $assignmentid, int $submissionid ): DtaResultSummary { global $DB; //Fetch data from database. $summary_record = $DB->get_record(self::TABLE_SUMMARY, array( "assignment_id" => $assignmentid, "submission_id" => $submissionid )); $results_array = $DB->get_records(self::TABLE_RESULT, array( "assignment_id" => $assignmentid, "submission_id" => $submissionid )); //Create a summary instance. $summary = new DtaResultSummary(); $summary->timestamp = $summary_record->timestamp; $summary->globalStacktrace = $summary_record->global_stacktrace; $summary->successfulTestCompetencyProfile = $summary_record->successful_competencies; $summary->overallTestCompetencyProfile = $summary_record->tested_competencies; $summary->results = array(); //Create result instances and add to array of summary instance. foreach($results_array as $rr) { $result = new DtaResult(); $result->packageName = $rr->package_name; $result->className = $rr->class_name; $result->name = $rr->name; $result->state = $rr->state; $result->failureType = $rr->failure_type; $result->failureReason = $rr->failure_reason; $result->stacktrace = $rr->stacktrace; $result->columnNumber = $rr->column_number; $result->lineNumber = $rr->line_number; $result->position = $rr->position; $summary->results[] = $result; } return $summary; } /** * save given result summary and single results to database * under given assignment and submission id * * @param int assignmentid assigment this is submission is linked to * @param int submissionid submission of this result * @param DttResultSummary instance to persist */ public static function storeResultSummaryToDatabase( int $assignmentid, int $submissionid, DtaResultSummary $summary ): void { global $DB; //Prepare new database entries. $summary_record = new stdClass(); $summary_record->assignment_id = $assignmentid; $summary_record->submission_id = $submissionid; $summary_record->successful_competencies = $summary->successfulTestCompetencyProfile; $summary_record->tested_competencies = $summary->overallTestCompetencyProfile; $summary_record->timestamp = $summary->timestamp; $summary_record->global_stacktrace = $summary->globalStacktrace; //Prepare results to persist to array. $result_records = array(); foreach($summary->results as $r) { $record = new stdClass(); $record->assignment_id = $assignmentid; $record->submission_id = $submissionid; $record->package_name = $r->packageName; $record->class_name = $r->className; $record->name = $r->name; $record->state = $r->state; $record->failure_type = $r->failureType; $record->failure_reason = $r->failureReason; $record->stacktrace = $r->stacktrace; $record->column_number = $r->columnNumber; $record->line_number = $r->lineNumber; $record->position = $r->position; $result_records[] = $record; } //If results already exist, delete old values beforehand. $submission = $DB->get_record(self::TABLE_SUMMARY, array( 'assignment_id' => $assignmentid, 'submission_id' => $submissionid )); if ($submission) { $DB->delete_records(self::TABLE_RESULT, array( 'assignment_id' => $assignmentid, 'submission_id' => $submissionid )); $DB->delete_records(self::TABLE_SUMMARY, array( 'assignment_id' => $assignmentid, 'submission_id' => $submissionid )); } //Create summary and single result entries. $DB->insert_record(self::TABLE_SUMMARY, $summary_record); foreach($result_records as $rr) { $DB->insert_record(self::TABLE_RESULT, $rr); } } /** * cleans up database if plugin is uninstalled */ public static function uninstallPluginCleanUp(): void { global $DB; $DB->delete_records(self::TABLE_RESULT, null); $DB->delete_records(self::TABLE_SUMMARY, null); } }