DtaResult.php 6.88 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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/>.
16

17
18
defined('MOODLE_INTERNAL') || die();

Lückemeyer's avatar
Lückemeyer committed
19
20
21
22
23
24
25
/**
 * 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
 */
26
27
class DtaResult {

Lückemeyer's avatar
Lückemeyer committed
28
29
30
    /** 
     * Broadly used in logic, parametrized for easier change.
     */
31
32
    const COMPONENT_NAME = "assignsubmission_dta";

Lückemeyer's avatar
Lückemeyer committed
33
34
35
    /** 
     * Package name of the test.
     */
36
    public $packagename;
Lückemeyer's avatar
Lückemeyer committed
37
38
39
40
    
    /** 
     * Unit name of the test.
     */
41
    public $classname;
Lückemeyer's avatar
Lückemeyer committed
42
43
44
45
    
    /** 
     * Name of the test.
     */
46
47
48
49
50
51
52
53
54
55
56
57
    public $name;

    /**
     * State is defined like below
     *
     *  0 UNKNOWN
     *  1 SUCCESS
     *  2 FAILURE
     *  3 COMPILATIONERROR
     */
    public $state;

Lückemeyer's avatar
Lückemeyer committed
58
59
60
    /** 
     * Type of test failure if applicable, "" otherwise.
     */
61
    public $failuretype;
Lückemeyer's avatar
Lückemeyer committed
62
63
64
65
    
    /** 
     * Reason of test failure if applicable, "" otherwise.
     */    
66
    public $failurereason;
Lückemeyer's avatar
Lückemeyer committed
67
68
69
70
    
    /** 
     * Stack trace of test failure if applicable, "" otherwise.
     */    
71
72
    public $stacktrace;

Lückemeyer's avatar
Lückemeyer committed
73
74
75
    /** 
     * Column number of compile failure if applicable, "" otherwise.
     */    
76
    public $columnnumber;
Lückemeyer's avatar
Lückemeyer committed
77
78
79
    /** 
     * Line number of compile failure if applicable, "" otherwise.
     */    
80
    public $linenumber;
Lückemeyer's avatar
Lückemeyer committed
81
82
83
    /** 
     * Position of compile failure if applicable, "" otherwise.
     */    
84
85
86
    public $position;

    /**
Lückemeyer's avatar
Lückemeyer committed
87
88
     * Returns the name of a state with the given number of display.
     * @return name of state as defined
89
     */
90
    public static function getstatename(int $state): string {
91
        if ($state == 1) {
92
            return get_string("tests_successful", self::COMPONENT_NAME);
93
        } else if ($state == 2) {
94
            return get_string("failures", self::COMPONENT_NAME);
95
        } else if ($state == 3) {
96
            return get_string("compilation_errors", self::COMPONENT_NAME);
97
        } else {
98
            return get_string("unknown_state", self::COMPONENT_NAME);
99
100
101
102
        }
    }
}

Lückemeyer's avatar
Lückemeyer committed
103
104
105
106
107
108
109
/**
 * 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
 */
110
111
class DtaResultSummary {

Lückemeyer's avatar
Lückemeyer committed
112
113
114
    /** 
     * Result timestamp for chronological ordering and deletion of previous results.
     */    
115
    public $timestamp;
Lückemeyer's avatar
Lückemeyer committed
116
117
118
119
    
    /** 
     * Global stack trace if applicable, "" otherwise.
     */    
120
    public $globalstacktrace;
Lückemeyer's avatar
Lückemeyer committed
121
122
123
124
    
    /** 
     * Successfully tested competencies according to tests and weights, "" otherwise.
     */    
125
    public $successfultestcompetencies;
Lückemeyer's avatar
Lückemeyer committed
126
127
128
    /** 
     * Overall tested competencies according to tests and weights, "" otherwise.
     */    
129
    public $overalltestcompetencies;
Lückemeyer's avatar
Lückemeyer committed
130
131
132
    /** 
     * List of detail results.
     */    
133
134
135
    public $results;

    /**
Lückemeyer's avatar
Lückemeyer committed
136
     * Decodes the JSON result summary returned by the backend service call into the plugin PHP data structure.
137
138
139
     * @param string $jsonString jsonString containing DtaResultSummary
     * @return DtaResultSummary
     */
140
    public static function decodejson($jsonstring): DtaResultSummary {
141
        $response = json_decode($jsonstring);
142
143
144

        $summary = new DtaResultSummary();
        $summary->timestamp = $response->timestamp;
145
        $summary->globalstacktrace = $response->globalstacktrace;
146

147
148
        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile;
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile;
149

150
        $summary->results = self::decodejsonresultarray($response->results);
151
152
153
154
155

        return $summary;
    }

    /**
Lückemeyer's avatar
Lückemeyer committed
156
     * Decodes the array of JSON detail results returned by the backend service call into the plugin PHP data structure.
157
158
159
     * @param array $jsonArray decoded json array of results array
     * @return array of DtaResult
     */
160
    private static function decodejsonresultarray($jsonarray): array {
161
        $ret = [];
162
        foreach ($jsonarray as $entry) {
163
            $value = new DtaResult();
164
165
            $value->packagename = $entry->packageName;
            $value->classname = $entry->className;
166
167
            $value->name = $entry->name;

168
169
            $value->state = $entry->state;

170
171
            $value->failuretype = $entry->failureType;
            $value->failurereason = $entry->failureReason;
172
173
            $value->stacktrace = $entry->stacktrace;

174
175
            $value->columnnumber = $entry->columnNumber;
            $value->linenumber = $entry->lineNumber;
176
177
178
179
180
181
182
            $value->position = $entry->position;

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

Lückemeyer's avatar
Lückemeyer committed
183
184
185
186
187

    /**
     * Returns the number of detail results attached to the summary.
     * @return int count of occurences
     */
188
189
190
191
    public function resultcount(): int {
        return count($this->results);
    }

192
    /**
Lückemeyer's avatar
Lückemeyer committed
193
     * Returns the number of detail results with the given state attached to the summary.
194
195
196
     * @param int $state state ordinal number
     * @return int count of occurences provided state has
     */
197
    public function stateoccurencecount(int $state): int {
198
        $num = 0;
199
        foreach ($this->results as $r) {
200
201
202
203
204
205
206
            if ($r->state == $state) {
                $num++;
            }
        }
        return $num;
    }

Lückemeyer's avatar
Lückemeyer committed
207
208
209
210
    /**
     * Returns the number of detail results with compilation errors attached to the summary.
     * @return int count of occurences
     */
211
212
    public function compilationerrorcount(): int {
        return $this->stateoccurencecount(3);
213
214
    }

Lückemeyer's avatar
Lückemeyer committed
215
216
217
218
    /**
     * Returns the number of detail results with test failures attached to the summary.
     * @return int count of occurences
     */
219
220
    public function failedcount(): int {
        return $this->stateoccurencecount(2);
221
222
    }

Lückemeyer's avatar
Lückemeyer committed
223
224
225
226
    /**
     * Returns the number of detail results with successful tests attached to the summary.
     * @return int count of occurences
     */
227
228
    public function successfulcount(): int {
        return $this->stateoccurencecount(1);
229
230
    }

Lückemeyer's avatar
Lückemeyer committed
231
232
233
234
    /**
     * Returns the number of detail results with an unknown result - mostly due to compile errors - attached to the summary.
     * @return int count of occurences
     */
235
236
    public function unknowncount(): int {
        return $this->stateoccurencecount(0);
237
238
239
    }

}