From 207e72b69b541f5d26f4e16f0c2044028bbb173a Mon Sep 17 00:00:00 2001 From: Kurzenberger <01kuni1bif@hft-stuttgart.de> Date: Mon, 27 Jan 2025 12:54:37 +0100 Subject: [PATCH] fixed the code checker requierments --- dta/classes/dta_backend_utils.php | 6 +- dta/classes/dta_view_submission_utils.php | 251 +++++++++++----------- dta/classes/models/dta_recommendation.php | 15 +- dta/classes/models/dta_result.php | 6 +- dta/classes/models/dta_result_summary.php | 57 +++-- dta/locallib.php | 3 +- 6 files changed, 181 insertions(+), 157 deletions(-) diff --git a/dta/classes/dta_backend_utils.php b/dta/classes/dta_backend_utils.php index 1c7aa8d..001ba79 100644 --- a/dta/classes/dta_backend_utils.php +++ b/dta/classes/dta_backend_utils.php @@ -1,16 +1,16 @@ <?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/>. diff --git a/dta/classes/dta_view_submission_utils.php b/dta/classes/dta_view_submission_utils.php index 1ef5201..0f36bdb 100644 --- a/dta/classes/dta_view_submission_utils.php +++ b/dta/classes/dta_view_submission_utils.php @@ -1,6 +1,6 @@ <?php -// This file is part of Moodle - http://moodle.org/. -// +// 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 @@ -14,6 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. +/** + * This file contains the backend webservice contact functionality for the DTA plugin. + * + * @package assignsubmission_dta + * @copyright 2023 Your Name + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + namespace assignsubmission_dta; use assignsubmission_dta\dta_db_utils; @@ -25,8 +33,8 @@ use assignsubmission_dta\models\dta_recommendation; /** * Utility class for DTA submission plugin result display. * - * @package assignsubmission_dta - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package assignsubmission_dta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class dta_view_submission_utils { @@ -35,105 +43,102 @@ class dta_view_submission_utils { */ public const ASSIGNSUBMISSION_DTA_COMPONENT_NAME = 'assignsubmission_dta'; - /** - * Generates a short summary HTML (like your old plugin). - * - * @param int $assignmentid The assignment ID. - * @param int $submissionid The submission ID to create a report for. - * @return string The HTML summary. - */ -public static function assignsubmission_dta_generate_summary_html( - int $assignmentid, - int $submissionid -): string { - - // 1) Retrieve the summary data from the DB (adjust your DB-utils class as needed). - $summary = dta_db_utils::assignsubmission_dta_get_result_summary_from_database($assignmentid, $submissionid); - - - // 2) Prepare an HTML buffer. - $html = ''; - - // 3) Extract counts from your new method names: - $unknowncount = $summary->assignsubmission_dta_unknown_count(); - $compilecount = $summary->assignsubmission_dta_compilation_error_count(); - $successcount = $summary->assignsubmission_dta_successful_count(); - $failcount = $summary->assignsubmission_dta_failed_count(); - $totalcount = $summary->assignsubmission_dta_result_count(); - - // 4) Compute success rate if no unknown/compile errors and total>0. - $successrate = '?'; - if ($unknowncount === 0 && $compilecount === 0 && $totalcount > 0) { - $successrate = round(($successcount / $totalcount) * 100, 2); - } + /** + * Generates a short summary HTML (like your old plugin). + * + * @param int $assignmentid The assignment ID. + * @param int $submissionid The submission ID to create a report for. + * @return string The HTML summary. + */ + public static function assignsubmission_dta_generate_summary_html( + int $assignmentid, + int $submissionid + ): string { + // 1) Retrieve the summary data from the DB (adjust your DB-utils class as needed). + $summary = dta_db_utils::assignsubmission_dta_get_result_summary_from_database($assignmentid, $submissionid); - // 5) “X/Y (Z%) tests successful†line: - // If either compile errors or unknown exist -> we show "?" - // else X/Y (rate%). - $html .= $successcount . '/'; - if ($compilecount === 0 && $unknowncount === 0) { - $html .= ($totalcount > 0) - ? ($totalcount . ' (' . $successrate . '%)') - : ('0 (' . $successrate . ')'); - } else { - $html .= '?'; - } - $html .= get_string('tests_successful', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) . "<br />"; + // 2) Prepare an HTML buffer. + $html = ''; - // 6) If there are compilation errors, show them: - if ($compilecount > 0) { - $html .= $compilecount - . get_string('compilation_errors', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) - . "<br />"; - } + // 3) Extract counts from your new method names. + $unknowncount = $summary->assignsubmission_dta_unknown_count(); + $compilecount = $summary->assignsubmission_dta_compilation_error_count(); + $successcount = $summary->assignsubmission_dta_successful_count(); + $failcount = $summary->assignsubmission_dta_failed_count(); + $totalcount = $summary->assignsubmission_dta_result_count(); - // 7) If there are unknown results, show them: - if ($unknowncount > 0) { - $html .= $unknowncount - . get_string('unknown_state', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) - . "<br />"; - } + // 4) Compute success rate if no unknown/compile errors and total>0. + $successrate = '?'; + if ($unknowncount === 0 && $compilecount === 0 && $totalcount > 0) { + $successrate = round(($successcount / $totalcount) * 100, 2); + } - if ($failcount > 0) { - $html .= $failcount - . get_string('tests_failed', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) - . "<br />"; - } - - - // 8) Competencies (like your old snippet): - $showncompetencies = explode(';', $summary->successfultestcompetencies); - $overallcompetencies = explode(';', $summary->overalltestcompetencies); - - $tmp = ''; - $size = count($showncompetencies); - for ($i = 0; $i < $size; $i++) { - $shown = $showncompetencies[$i]; - $comp = $overallcompetencies[$i]; - - // If the competency was actually used (non-zero?), show a row. - if ($shown !== '0') { - $shownval = floatval($shown); - $compval = floatval($comp); - - // Guard division by zero: - $pct = 0; - if ($compval > 0) { - $pct = 100.0 * $shownval / $compval; - } + // 5) "X/Y (Z%) tests successful" line: + // If either compile errors or unknown exist -> show "?", else X/Y (rate%). + $html .= $successcount . '/'; + if ($compilecount === 0 && $unknowncount === 0) { + $html .= ($totalcount > 0) + ? ($totalcount . ' (' . $successrate . '%)') + : ('0 (' . $successrate . ')'); + } else { + $html .= '?'; + } + $html .= get_string('tests_successful', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) . "<br />"; - // “compX XX%<br />†- $tmp .= get_string('comp' . $i, self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) - . ' ' . round($pct, 2) . '%<br />'; + // 6) If there are compilation errors, show them. + if ($compilecount > 0) { + $html .= $compilecount + . get_string('compilation_errors', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) + . "<br />"; } - } - $html .= get_string('success_competencies', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) - . "<br />" . $tmp . "<br />"; + // 7) If there are unknown results, show them. + if ($unknowncount > 0) { + $html .= $unknowncount + . get_string('unknown_state', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) + . "<br />"; + } - // 9) Wrap it in a DIV for styling, and return. - return \html_writer::div($html, "dtaSubmissionSummary"); -} + // If there are failed tests, show them. + if ($failcount > 0) { + $html .= $failcount + . get_string('tests_failed', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) + . "<br />"; + } + + // 8) Competencies (like your old snippet). + $showncompetencies = explode(';', $summary->successfultestcompetencies); + $overallcompetencies = explode(';', $summary->overalltestcompetencies); + + $tmp = ''; + $size = count($showncompetencies); + for ($i = 0; $i < $size; $i++) { + $shown = $showncompetencies[$i]; + $comp = $overallcompetencies[$i]; + + // If the competency was actually used (non-zero?), show a row. + if ($shown !== '0') { + $shownval = (float) $shown; + $compval = (float) $comp; + + // Guard division by zero. + $pct = 0; + if ($compval > 0) { + $pct = 100.0 * $shownval / $compval; + } + + // "compX XX%<br />" + $tmp .= get_string('comp' . $i, self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) + . ' ' . round($pct, 2) . '%<br />'; + } + } + + $html .= get_string('success_competencies', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME) + . "<br />" . $tmp . "<br />"; + + // 9) Wrap it in a DIV for styling, and return. + return \html_writer::div($html, 'dtaSubmissionSummary'); + } /** * Generates detailed view HTML. @@ -158,7 +163,7 @@ public static function assignsubmission_dta_generate_summary_html( $html = ''; - // *** Summary Table *** + // Summary table. $tableheaderrowattributes = ['class' => 'dtaTableHeaderRow']; $tablerowattributes = ['class' => 'dtaTableRow']; $resultrowattributes = $tablerowattributes; @@ -188,21 +193,19 @@ public static function assignsubmission_dta_generate_summary_html( $unknowncount = $summary->assignsubmission_dta_unknown_count(); // Total items. - $tmp = ''; - $tmp .= \html_writer::tag( + $tmp = \html_writer::tag( 'td', get_string('total_items', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME), $attributes ); $tmp .= \html_writer::tag('td', $resultcount, $attributes); $resultrowattributes = $tablerowattributes; - // Original code colors this row as unknown by default: + // Original code colors this row as unknown by default. $resultrowattributes['class'] .= ' ' . $unknownattributes; $body .= \html_writer::tag('tr', $tmp, $resultrowattributes); // Tests successful. - $tmp = ''; - $tmp .= \html_writer::tag( + $tmp = \html_writer::tag( 'td', get_string('tests_successful', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME), $attributes @@ -228,8 +231,7 @@ public static function assignsubmission_dta_generate_summary_html( $body .= \html_writer::tag('tr', $tmp, $resultrowattributes); // Failures. - $tmp = ''; - $tmp .= \html_writer::tag( + $tmp = \html_writer::tag( 'td', get_string('failures', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME), $attributes @@ -244,8 +246,7 @@ public static function assignsubmission_dta_generate_summary_html( $body .= \html_writer::tag('tr', $tmp, $resultrowattributes); // Compilation errors. - $tmp = ''; - $tmp .= \html_writer::tag( + $tmp = \html_writer::tag( 'td', get_string('compilation_errors', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME), $attributes @@ -260,8 +261,7 @@ public static function assignsubmission_dta_generate_summary_html( $body .= \html_writer::tag('tr', $tmp, $resultrowattributes); // Unknown state. - $tmp = ''; - $tmp .= \html_writer::tag( + $tmp = \html_writer::tag( 'td', get_string('unknown_state', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME), $attributes @@ -276,8 +276,7 @@ public static function assignsubmission_dta_generate_summary_html( $body .= \html_writer::tag('tr', $tmp, $resultrowattributes); // Success rate row. - $tmp = ''; - $tmp .= \html_writer::tag( + $tmp = \html_writer::tag( 'td', \html_writer::tag('b', get_string('success_rate', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)), $attributes @@ -291,7 +290,6 @@ public static function assignsubmission_dta_generate_summary_html( \html_writer::tag('b', $successfulcount . '/' . $suffix), $attributes ); - $resultrowattributes = $tablerowattributes; if ($compilationcount == 0 && $unknowncount == 0 && $resultcount > 0) { if ($successrate !== '?' && $successrate < 50) { @@ -314,12 +312,13 @@ public static function assignsubmission_dta_generate_summary_html( // Spacing after the summary table. $html .= \html_writer::empty_tag('div', ['class' => 'dtaSpacer']); - // *** Recommendations Table *** + // Recommendations table. if (!empty($recommendations)) { $allowedsortfields = ['topic', 'exercise_name', 'difficulty', 'score']; $allowedsortdirs = ['asc', 'desc']; - $sortby = $_POST['sortby'] ?? 'score'; + // Make sure only one space before ?? + $sortby = $_POST['sortby'] ?? 'score'; $sortdir = $_POST['sortdir'] ?? 'asc'; if (!in_array($sortby, $allowedsortfields)) { @@ -370,7 +369,7 @@ public static function assignsubmission_dta_generate_summary_html( ]); // Hidden inputs. - $hiddeninputs = \html_writer::empty_tag('input', [ + $hiddeninputs = \html_writer::empty_tag('input', [ 'type' => 'hidden', 'name' => 'sortby', 'value' => $columnname, @@ -381,7 +380,7 @@ public static function assignsubmission_dta_generate_summary_html( 'value' => $newsortdir, ]); - $form = \html_writer::start_tag('form', [ + $form = \html_writer::start_tag('form', [ 'method' => 'post', 'style' => 'display:inline', ]); @@ -443,15 +442,14 @@ public static function assignsubmission_dta_generate_summary_html( $html .= \html_writer::empty_tag('div', ['class' => 'dtaSpacer']); } - // *** Competency Assessment Table *** - $body = ''; - $tmp = ''; - $tmp .= \html_writer::tag( + // Competency assessment table. + $body = ''; + $tmp = \html_writer::tag( 'th', get_string('competencies', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME), ['class' => 'dtaTableHeader'] ); - $tmp .= \html_writer::empty_tag('th', ['class' => 'dtaTableHeader']); + $tmp .= \html_writer::empty_tag('th', ['class' => 'dtaTableHeader']); $header = \html_writer::tag('tr', $tmp, $tableheaderrowattributes); $header = \html_writer::tag('thead', $header); @@ -462,12 +460,12 @@ public static function assignsubmission_dta_generate_summary_html( $comp = $overallcompetencies[$index]; $shown = $showncompetencies[$index]; - // If the competency was actually assessed, add a row in the table. + // If the competency was actually assessed, add a row. if ($comp !== '0') { - $compval = floatval($comp); - $shownval = floatval($shown); + $compval = (float) $comp; + $shownval = (float) $shown; - // Guard division by zero: + // Guard division by zero. $pct = 0; if ($compval > 0) { $pct = (100.0 * $shownval / $compval); @@ -499,7 +497,7 @@ public static function assignsubmission_dta_generate_summary_html( // Add empty div for spacing. $html .= \html_writer::empty_tag('div', ['class' => 'dtaSpacer']); - // *** Details Table *** + // Details table. $tmp = ''; $tmp .= \html_writer::tag( 'th', @@ -513,7 +511,7 @@ public static function assignsubmission_dta_generate_summary_html( $body = ''; $spacerrow = null; foreach ($summary->results as $r) { - // Add spacer first if not null. + // Add spacer row before each new entry (after the first). if (!is_null($spacerrow)) { $body .= $spacerrow; } @@ -565,7 +563,7 @@ public static function assignsubmission_dta_generate_summary_html( ); $body .= \html_writer::tag('tr', $tmp, $resultrowattributes); - // If state != 1, show additional info. + // If state != 1 (not successful), show additional info. if ($r->state !== 1) { $tmp = ''; $tmp .= \html_writer::tag( @@ -633,6 +631,7 @@ public static function assignsubmission_dta_generate_summary_html( } if (is_null($spacerrow)) { + // Reuse this spacer row between subsequent items. $spacerrow = \html_writer::empty_tag('tr', ['class' => 'dtaTableSpacer']); } } diff --git a/dta/classes/models/dta_recommendation.php b/dta/classes/models/dta_recommendation.php index 8d78a2c..79afb82 100644 --- a/dta/classes/models/dta_recommendation.php +++ b/dta/classes/models/dta_recommendation.php @@ -1,16 +1,16 @@ <?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/>. @@ -39,9 +39,9 @@ class dta_recommendation { public $topic; /** - * @var string $exerciseName Name of the exercise. + * @var string $exercisename Name of the exercise. */ - public $exerciseName; + public $exercisename; /** * @var string $url URL of the exercise. @@ -73,8 +73,9 @@ class dta_recommendation { foreach ($response->recommendations as $recommendation) { $rec = new dta_recommendation(); $rec->topic = $recommendation->topic ?? null; - // Map correct fields to the renamed variable names: - $rec->exerciseName = $recommendation->url ?? null; + + // Map correct fields to the renamed variable names. + $rec->exercisename = $recommendation->url ?? null; $rec->url = $recommendation->exerciseName ?? null; $rec->difficulty = $recommendation->difficulty ?? null; $rec->score = $recommendation->score ?? null; diff --git a/dta/classes/models/dta_result.php b/dta/classes/models/dta_result.php index 6b2f492..8e93b48 100644 --- a/dta/classes/models/dta_result.php +++ b/dta/classes/models/dta_result.php @@ -1,16 +1,16 @@ <?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/>. diff --git a/dta/classes/models/dta_result_summary.php b/dta/classes/models/dta_result_summary.php index e05288f..8f3633f 100644 --- a/dta/classes/models/dta_result_summary.php +++ b/dta/classes/models/dta_result_summary.php @@ -1,4 +1,27 @@ <?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/>. + +/** + * This file contains the DTA submission plugin result summary entity class. + * + * @package assignsubmission_dta + * @copyright 2023 Your Name + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + namespace assignsubmission_dta\models; /** @@ -7,9 +30,11 @@ namespace assignsubmission_dta\models; * This class holds: * - A timestamp for when the summary was generated. * - An optional global stack trace (in case the entire process failed). - * - A competency profile of how many tests passed for each competency (successfulTestCompetencyProfile). - * - A competency profile of the total coverage for each competency (overallTestCompetencyProfile). - * - An array of DTA result objects that detail individual test results. + * - A competency profile of how many tests passed for each competency. + * - A competency profile of the total coverage for each competency. + * - An array of dta_result objects that detail individual test results. + * + * @package assignsubmission_dta */ class dta_result_summary { @@ -41,9 +66,9 @@ class dta_result_summary { $summary->timestamp = $response->timestamp ?? 0; $summary->globalstacktrace = $response->globalstacktrace ?? ''; - // If your JSON keys are 'successfulTestCompetencyProfile' and 'overallTestCompetencyProfile': + // If your JSON keys are 'successfulTestCompetencyProfile' and 'overallTestCompetencyProfile'. $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile ?? ''; - $summary->overalltestcompetencies = $response->overallTestCompetencyProfile ?? ''; + $summary->overalltestcompetencies = $response->overallTestCompetencyProfile ?? ''; // Decode the "results" array into an array of dta_result objects. if (!empty($response->results) && is_array($response->results)) { @@ -66,16 +91,16 @@ class dta_result_summary { foreach ($jsonarray as $entry) { $value = new dta_result(); - $value->packagename = $entry->packageName ?? ''; - $value->classname = $entry->className ?? ''; - $value->name = $entry->name ?? ''; - $value->state = $entry->state ?? 0; - $value->failuretype = $entry->failureType ?? ''; - $value->failurereason = $entry->failureReason ?? ''; - $value->stacktrace = $entry->stacktrace ?? ''; - $value->columnnumber = $entry->columnNumber ?? 0; - $value->linenumber = $entry->lineNumber ?? 0; - $value->position = $entry->position ?? 0; + $value->packagename = $entry->packageName ?? ''; + $value->classname = $entry->className ?? ''; + $value->name = $entry->name ?? ''; + $value->state = $entry->state ?? 0; + $value->failuretype = $entry->failureType ?? ''; + $value->failurereason = $entry->failureReason ?? ''; + $value->stacktrace = $entry->stacktrace ?? ''; + $value->columnnumber = $entry->columnNumber ?? 0; + $value->linenumber = $entry->lineNumber ?? 0; + $value->position = $entry->position ?? 0; $ret[] = $value; } @@ -151,7 +176,7 @@ class dta_result_summary { /** * Computes the success rate as a percentage of all results (0..100). - * Note: This includes tests that might have compile errors, unknown states, etc. + * Note: This includes tests that might have compile errors or unknown states. * * @return float A floating percentage between 0.0 and 100.0. */ diff --git a/dta/locallib.php b/dta/locallib.php index 546919d..d3638c0 100644 --- a/dta/locallib.php +++ b/dta/locallib.php @@ -306,8 +306,7 @@ class assign_submission_dta extends assign_submission_plugin { $file = reset($files); // Send file to backend (split across lines to avoid exceeding length). - $response = \assignsubmission_dta\dta_backend_utils:: - assignsubmission_dta_send_submission_to_backend( + $response = \assignsubmission_dta\dta_backend_utils::assignsubmission_dta_send_submission_to_backend( $this->assignment, $submission->id, $file -- GitLab