DtaResult.php 8.58 KiB
<?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/>.
/**
 * entity classes for DTA submission plugin result summary and test results
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @copyright Gero Lueckemeyer and student project teams
defined('MOODLE_INTERNAL') || die();
/**
 * entity class for DTA submission plugin result
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @copyright Gero Lueckemeyer and student project teams
class DtaResult {
    /**
     * Broadly used in logic, parametrized for easier change.
    const COMPONENT_NAME = "assignsubmission_dta";
    /**
     * @var $packagename Package name of the test.
    public $packagename;
    /**
     * @var $classname Unit name of the test.
    public $classname;
    /**
     * @var $name Name of the test.
    public $name;
    /**
     * @var $state State is defined like below
     *  0 UNKNOWN
     *  1 SUCCESS
     *  2 FAILURE
     *  3 COMPILATIONERROR
    public $state;
    /**
     * @var $failuretype Type of test failure if applicable, "" otherwise.
    public $failuretype;
    /**
* @var $failurereason Reason of test failure if applicable, "" otherwise. */ public $failurereason; /** * @var $stacktrace Stack trace of test failure if applicable, "" otherwise. */ public $stacktrace; /** * @var $columnnumber Column number of compile failure if applicable, "" otherwise. */ public $columnnumber; /** * @var $linenumber Line number of compile failure if applicable, "" otherwise. */ public $linenumber; /** * @var $position Position of compile failure if applicable, "" otherwise. */ public $position; /** * Returns the name of a state with the given number of display. * @param int $state number of the state * @return string name of state as defined */ public static function getstatename(int $state): string { if ($state == 1) { return get_string("tests_successful", self::COMPONENT_NAME); } else if ($state == 2) { return get_string("failures", self::COMPONENT_NAME); } else if ($state == 3) { return get_string("compilation_errors", self::COMPONENT_NAME); } else { return get_string("unknown_state", self::COMPONENT_NAME); } } } /** * entity class for DTA submission plugin result * * @package assignsubmission_dta * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @copyright Gero Lueckemeyer and student project teams */ class DtaResultSummary { /** * @var $timestamp Result timestamp for chronological ordering and deletion of previous results. */ public $timestamp; /** * @var $globalstacktrace Global stack trace if applicable, "" otherwise. */ public $globalstacktrace; /** * @var $successfultestcompetencies Successfully tested competencies according to tests and weights, "" otherwise. */ public $successfultestcompetencies; /** * @var overalltestcompetencies Overall tested competencies according to tests and weights, "" otherwise. */ public $overalltestcompetencies; /** * @var results List of detail results. */
public $results; /** * Decodes the JSON result summary returned by the backend service call into the plugin PHP data structure. * @param string $jsonstring jsonString containing DtaResultSummary * @return DtaResultSummary the result summary */ public static function decodejson(string $jsonstring): DtaResultSummary { $response = json_decode($jsonstring); $summary = new DtaResultSummary(); $summary->timestamp = $response->timestamp; $summary->globalstacktrace = $response->globalstacktrace; $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile; $summary->overalltestcompetencies = $response->overallTestCompetencyProfile; $summary->results = self::decodejsonresultarray($response->results); return $summary; } public static function decodeJsonRecommendation(string $jsonstring): array { // Decode the JSON string into a PHP object $response = json_decode($jsonstring); error_log("decodeJsonRecommendation"); error_log(print_r($response, true)); // Initialize an empty array to store recommendations $recommendations = []; // Loop through the recommendations in the response if (!empty($response->recommendations)) { foreach ($response->recommendations as $recommendation) { // For each recommendation, create an associative array with the properties $recommendations[] = [ 'topic' => $recommendation->topic ?? null, 'url' => $recommendation->exerciseName ?? null, 'exercise_name' => $recommendation->url ?? null, 'difficulty' => $recommendation->difficulty ?? null, 'score' => $recommendation->score ?? null ]; } } error_log(print_r($recommendations,true)); // Return the array of recommendations return $recommendations; } /** * Decodes the array of JSON detail results returned by the backend service call into the plugin PHP data structure. * @param array $jsonarray decoded json array of results array * @return array of DtaResult */ private static function decodejsonresultarray($jsonarray): array { $ret = []; foreach ($jsonarray as $entry) { $value = new DtaResult(); $value->packagename = $entry->packageName; $value->classname = $entry->className; $value->name = $entry->name; $value->state = $entry->state; $value->failuretype = $entry->failureType; $value->failurereason = $entry->failureReason; $value->stacktrace = $entry->stacktrace;
$value->columnnumber = $entry->columnNumber; $value->linenumber = $entry->lineNumber; $value->position = $entry->position; $ret[] = $value; } return $ret; } /** * Returns the number of detail results attached to the summary. * @return int count of occurences */ public function resultcount(): int { return count($this->results); } /** * Returns the number of detail results with the given state attached to the summary. * @param int $state state ordinal number * @return int count of occurences provided state has */ public function stateoccurencecount(int $state): int { $num = 0; foreach ($this->results as $r) { if ($r->state == $state) { $num++; } } return $num; } /** * Returns the number of detail results with compilation errors attached to the summary. * @return int count of occurences */ public function compilationerrorcount(): int { return $this->stateoccurencecount(3); } /** * Returns the number of detail results with test failures attached to the summary. * @return int count of occurences */ public function failedcount(): int { return $this->stateoccurencecount(2); } /** * Returns the number of detail results with successful tests attached to the summary. * @return int count of occurences */ public function successfulcount(): int { return $this->stateoccurencecount(1); } /** * Returns the number of detail results with an unknown result - mostly due to compile errors - attached to the summary. * @return int count of occurences */ public function unknowncount(): int { return $this->stateoccurencecount(0); } }