dta_result_summary.php 5.67 KB
Newer Older
1
<?php
2
// This file is part of Moodle - http://moodle.org/.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 
// 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/>.

/**
18
 * Entity class for DTA submission plugin result summary.
19
20
21
22
23
24
25
26
27
 *
 * @package    assignsubmission_dta
 * @copyright  2023 Gero Lueckemeyer and student project teams
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace assignsubmission_dta\models;

/**
28
 * Entity class for DTA submission plugin result summary.
29
30
31
32
33
34
35
36
 *
 * @package    assignsubmission_dta
 * @copyright  2023 Gero Lueckemeyer and student project teams
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class dta_result_summary {

    /**
37
     * @var int $timestamp Timestamp for ordering and deletion of previous results.
38
39
40
41
     */
    public $timestamp;

    /**
42
     * @var string $globalstacktrace Global stack trace if applicable, empty otherwise.
43
44
45
46
     */
    public $globalstacktrace;

    /**
47
     * @var string $successfultestcompetencies Successfully tested competencies (tests and weights), or empty string.
48
49
50
51
     */
    public $successfultestcompetencies;

    /**
52
     * @var string $overalltestcompetencies Overall tested competencies (tests and weights), or empty string.
53
54
55
56
57
58
59
60
61
62
63
     */
    public $overalltestcompetencies;

    /**
     * @var array $results List of detail results.
     */
    public $results;

    /**
     * Decodes the JSON result summary returned by the backend service call into the plugin PHP data structure.
     *
64
65
     * @param string $jsonstring JSON string containing DtaResultSummary.
     * @return dta_result_summary The result summary.
66
     */
67
    public static function assignsubmission_dta_decode_json(string $jsonstring): dta_result_summary {
68
69
70
71
72
73
74
75
76
        $response = json_decode($jsonstring);

        $summary = new dta_result_summary();
        $summary->timestamp = $response->timestamp;
        $summary->globalstacktrace = $response->globalstacktrace;

        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile ?? '';
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile ?? '';

77
        $summary->results = self::assignsubmission_dta_decode_json_result_array($response->results);
78
79
80
81
82

        return $summary;
    }

    /**
83
     * Decodes an array of JSON detail results into the plugin PHP data structure.
84
     *
85
86
     * @param array $jsonarray Decoded JSON array of results.
     * @return array Array of dta_result objects.
87
     */
88
    private static function assignsubmission_dta_decode_json_result_array(array $jsonarray): array {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        $ret = [];
        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 ?? '';
            $value->linenumber = $entry->lineNumber ?? '';
            $value->position = $entry->position ?? '';

            $ret[] = $value;
        }
        return $ret;
    }

    /**
     * Returns the number of detail results attached to the summary.
     *
111
     * @return int Count of occurrences.
112
     */
113
    public function assignsubmission_dta_result_count(): int {
114
115
116
117
118
119
        return count($this->results);
    }

    /**
     * Returns the number of detail results with the given state attached to the summary.
     *
120
121
     * @param int $state State ordinal number.
     * @return int Count of occurrences for the provided state.
122
     */
123
    public function assignsubmission_dta_state_occurence_count(int $state): int {
124
125
        $num = 0;
        foreach ($this->results as $r) {
126
            if ($r->state === $state) {
127
128
129
130
131
132
133
134
135
                $num++;
            }
        }
        return $num;
    }

    /**
     * Returns the number of detail results with compilation errors attached to the summary.
     *
136
     * @return int Count of occurrences.
137
     */
138
139
    public function assignsubmission_dta_compilation_error_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(3);
140
141
142
143
144
    }

    /**
     * Returns the number of detail results with test failures attached to the summary.
     *
145
     * @return int Count of occurrences.
146
     */
147
148
    public function assignsubmission_dta_failed_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(2);
149
150
151
152
153
    }

    /**
     * Returns the number of detail results with successful tests attached to the summary.
     *
154
     * @return int Count of occurrences.
155
     */
156
157
    public function assignsubmission_dta_successful_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(1);
158
159
160
161
162
    }

    /**
     * Returns the number of detail results with an unknown result attached to the summary.
     *
163
     * @return int Count of occurrences.
164
     */
165
166
    public function assignsubmission_dta_unknown_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(0);
167
168
    }
}