DtaResult.php 4.24 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();

19
20
class DtaResult {

21
22
23
    // Broadly used in logic, parametrized for easier change.
    const COMPONENT_NAME = "assignsubmission_dta";

24
25
    public $packagename;
    public $classname;
26
27
28
29
30
31
32
33
34
35
36
37
    public $name;

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

38
39
    public $failuretype;
    public $failurereason;
40
41
    public $stacktrace;

42
43
    public $columnnumber;
    public $linenumber;
44
45
46
47
48
    public $position;

    /**
     * @return name of state like defined
     */
49
    public static function getstatename(int $state): string {
50
        if ($state == 1) {
51
            return get_string("tests_successful", self::COMPONENT_NAME);
52
        } else if ($state == 2) {
53
            return get_string("failures", self::COMPONENT_NAME);
54
        } else if ($state == 3) {
55
            return get_string("compilation_errors", self::COMPONENT_NAME);
56
        } else {
57
            return get_string("unknown_state", self::COMPONENT_NAME);
58
59
60
61
62
63
64
        }
    }
}

class DtaResultSummary {

    public $timestamp;
65
66
67
    public $globalstacktrace;
    public $successfultestcompetencies;
    public $overalltestcompetencies;
68
69
70
71
72
73
    public $results;

    /**
     * @param string $jsonString jsonString containing DtaResultSummary
     * @return DtaResultSummary
     */
74
    public static function decodejson($jsonstring): DtaResultSummary {
75
        $response = json_decode($jsonstring);
76
77
78

        $summary = new DtaResultSummary();
        $summary->timestamp = $response->timestamp;
79
        $summary->globalstacktrace = $response->globalstacktrace;
80

81
82
        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile;
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile;
83

84
        $summary->results = self::decodejsonresultarray($response->results);
85
86
87
88
89
90
91
92

        return $summary;
    }

    /**
     * @param array $jsonArray decoded json array of results array
     * @return array of DtaResult
     */
93
    private static function decodejsonresultarray($jsonarray): array {
94
        $ret = [];
95
        foreach ($jsonarray as $entry) {
96
            $value = new DtaResult();
97
98
            $value->packagename = $entry->packageName;
            $value->classname = $entry->className;
99
100
            $value->name = $entry->name;

101
102
            $value->state = $entry->state;

103
104
            $value->failuretype = $entry->failureType;
            $value->failurereason = $entry->failureReason;
105
106
            $value->stacktrace = $entry->stacktrace;

107
108
            $value->columnnumber = $entry->columnNumber;
            $value->linenumber = $entry->lineNumber;
109
110
111
112
113
114
115
            $value->position = $entry->position;

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

116
117
118
119
    public function resultcount(): int {
        return count($this->results);
    }

120
121
122
123
    /**
     * @param int $state state ordinal number
     * @return int count of occurences provided state has
     */
124
    public function stateoccurencecount(int $state): int {
125
        $num = 0;
126
        foreach ($this->results as $r) {
127
128
129
130
131
132
133
            if ($r->state == $state) {
                $num++;
            }
        }
        return $num;
    }

134
135
    public function compilationerrorcount(): int {
        return $this->stateoccurencecount(3);
136
137
    }

138
139
    public function failedcount(): int {
        return $this->stateoccurencecount(2);
140
141
    }

142
143
    public function successfulcount(): int {
        return $this->stateoccurencecount(1);
144
145
    }

146
147
    public function unknowncount(): int {
        return $this->stateoccurencecount(0);
148
149
150
    }

}