view.php 13.5 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
19
20
21

class ViewSubmissionUtils {

    /**
     * generates a short summary html
     *
22
23
     * @param int assignmentid assignment
     * @param int submissionid submission to create a report for
24
25
     * @return string html
     */
26
    public static function generatesummaryhtml(
27
28
        int $assignmentid,
        int $submissionid
29
30
    ): string {

31
        // Fetch data.
32
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);
33
34
        $html = "";

35
        // Calculate success rate, if no unknown result states or compilation errors.
36
        $successrate = "?";
37
        if ($summary->unknownCount() == 0 && $summary->compilationErrorCount() == 0) {
38
            $successrate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
39
40
        }

41
        // Generate html.
42
43
        $html .= $summary->successfulCount() . "/";
        $html .= ($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0)
44
            ? $summary->resultCount() . " (" . $successrate . "%)"
45
46
47
48
49
50
51
52
53
54
                : "?";
        $html .= " tests successful<br>";

        if ($summary->compilationErrorCount() > 0) {
            $html .= $summary->compilationErrorCount() . " compilation error(s)<br>";
        }

        if ($summary->unknownCount() > 0) {
            $html .= $summary->unknownCount() . " test(s) with unknown state<br>";
        }
55
56

        $html .= $summary->successfulTestCompetencyProfile . " successfully tested competency profile<br>";
57
58
59
60
61
62
63

        return html_writer::div($html, "dtaSubmissionSummary");
    }

    /**
     * generates detailed view html
     *
64
65
     * @param int assignmentid assignment
     * @param int submissionid submission to create a report for
66
     */
67
    public static function generatedetailhtml(
68
69
        int $assignmentid,
        int $submissionid
70
71
    ): string {

72
        // Fetch data.
73
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);
74
75
        $html = "";

76
77
78
79
80
81
82
83
        // Define a few css classes and prepare html attribute arrays to beautify the output.
        $tableheaderrowattributes = array("class" => "dtaTableHeaderRow");
        $tablerowattributes = array("class" => "dtaTableRow");
        $resultrowattributes = $tablerowattributes;
        $unknownattributes = 'dtaResultUnknown';
        $successattributes = 'dtaResultSuccess';
        $failureattributes = 'dtaResultFailure';
        $compilationerrorattributes = 'dtaResultCompilationError';
84

85
        // Summary table.
86
87
88
        $tmp = "";
        $tmp .= html_writer::tag("th", "Summary", array("class" => "dtaTableHeader"));
        $tmp .= html_writer::empty_tag("th", array("class" => "dtaTableHeader"));
89
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        $header = html_writer::tag("thead", $header);

        $body = "";
        $tmp = "";
        $attributes = array("class" => "dtaTableData");
        $tmp .= html_writer::tag(
            "td",
            "result items in sum",
            $attributes);

        $tmp .= html_writer::tag(
            "td",
            $summary->resultCount(),
            $attributes);

105
106
        $resultrowattributes = $tablerowattributes;
        $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
107

108
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
109
110
111
112
113

        $tmp = "";
        $tmp .= html_writer::tag("td", "successes", $attributes);
        $tmp .= html_writer::tag( "td", $summary->successfulCount(), $attributes);

114
        $resultrowattributes = $tablerowattributes;
115
        $successrate = "?";
116
117

        if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
118
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
119
        } else {
120
121
            $successrate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
            if ($successrate < 50) {
122
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $compilationerrorattributes;
123
            } else if ($successrate < 75) {
124
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $failureattributes;
125
            } else {
126
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
127
128
            }
        }
129
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
130
131
132
133
134

        $tmp = "";
        $tmp .= html_writer::tag("td", "failures", $attributes);
        $tmp .= html_writer::tag("td", $summary->failedCount(), $attributes);

135
        $resultrowattributes = $tablerowattributes;
136
        if ($summary->failedCount() > 0) {
137
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $failureattributes;
138
        } else {
139
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
140
        }
141
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
142
143
144
145
146

        $tmp = "";
        $tmp .= html_writer::tag("td", "compilation errors", $attributes);
        $tmp .= html_writer::tag("td", $summary->compilationErrorCount(), $attributes);

147
        $resultrowattributes = $tablerowattributes;
148
        if ($summary->compilationErrorCount() > 0) {
149
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $compilationerrorattributes;
150
        } else {
151
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
152
        }
153
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
154
155
156
157
158

        $tmp = "";
        $tmp .= html_writer::tag("td", "unknown state", $attributes);
        $tmp .= html_writer::tag("td", $summary->unknownCount(), $attributes);

159
        $resultrowattributes = $tablerowattributes;
160
        if ($summary->unknownCount() > 0) {
161
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
162
        } else {
163
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
164
        }
165
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
166
167

        $tmp = "";
168
        $tmp .= html_writer::tag("td", html_writer::tag("b", "success rate"), $attributes);
169
170
171
172
        $tmp .= html_writer::tag(
            "td",
            html_writer::tag("b", $summary->successfulCount()
                . "/" . (($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0) ? $summary->resultCount()
173
                . " (" . $successrate . "%)"
174
175
176
                    : "?")),
            $attributes);

177
        $resultrowattributes = $tablerowattributes;
178
        if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
179
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
180
        } else {
181
            if ($successrate < 50) {
182
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $compilationerrorattributes;
183
            } else if ($successrate < 75) {
184
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $failureattributes;
185
            } else {
186
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
187
188
            }
        }
189
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
190
191
192
193
194
195

        $body = html_writer::tag("tbody", $body);
        $table = html_writer::tag("table", $header . $body, array("class" => "dtaTable"));

        $html .= $table;

196
        // Add empty div for spacing between summary and details table.
197
198
        $html .= html_writer::empty_tag("div", array("class" => "dtaSpacer"));

199
        // Details table.
200
201
202
        $tmp = "";
        $tmp .= html_writer::tag("th", "Details", array("class" => "dtaTableHeader"));
        $tmp .= html_writer::empty_tag("th", array("class" => "dtaTableHeader"));
203
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
204
205
206
        $header = html_writer::tag("thead", $header);

        $body = "";
207
        $spacerrow = null;
208
        foreach ($summary->results as $r) {
209
            // Add spacer first if not null.
210
211
            if (!is_null($spacerrow)) {
                $body .= $spacerrow;
212
213
            }

214
215
            // New copy of base attributes array.
            $resultrowattributes = $tablerowattributes;
216

217
            // Check which css class to add for the colored left-border according to resuls state.
218
            if ($r->state == 0) {
219
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultUnknown';
220
            } else if ($r->state == 1) {
221
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultSuccess';
222
            } else if ($r->state == 2) {
223
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultFailure';
224
            } else if ($r->state == 3) {
225
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultCompilationError';
226
227
228
229
230
231
232
            }

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
                "name",
                $attributes);
233

234
235
236
237
            $tmp .= html_writer::tag(
                "td",
                $r->name,
                $attributes);
238
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
239
240
241
242
243
244
245
246
247
248
249

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
                "state",
                $attributes);

            $tmp .= html_writer::tag(
                "td",
                DtaResult::getStateName($r->state),
                $attributes);
250
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
251

252
            // If state is something different than successful, show additional rows.
253
254
255
256
257
258
            if ($r->state != 1) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "failure type",
                    $attributes);
259

260
261
262
263
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureType,
                    $attributes);
264
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
265

266
267
268
269
270
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "failure reason",
                    $attributes);
271

272
273
274
275
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureReason,
                    $attributes);
276
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
277

278
                // Only show line, column and position if they have useful values.
279
280
281
282
283
284
285
286
287
288
289
                if (!is_null($r->lineNumber) && $r->lineNumber > 0) {
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
                        "line number",
                        $attributes);

                    $tmp .= html_writer::tag(
                        "td",
                        $r->lineNumber,
                        $attributes);
290
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
291
292
293
294
295
296
297
298
299
300
301
302
303
                }

                if (!is_null($r->columnNumber) && $r->columnNumber > 0) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "column number",
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
                    $r->columnNumber,
                    $attributes);
304
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
305
306
307
308
309
310
311
312
313
314
315
316
317
                }

                if (!is_null($r->position) && $r->position > 0) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "position",
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
                    $r->position,
                    $attributes);
318
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
319
320
321
322
323
324
325
326
327
328
329
330
                }

                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "stacktrace",
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
                    html_writer::tag("details", $r->stacktrace, array("class" => "dtaStacktraceDetails")),
                    $attributes);
331
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
332
333
            }

334
            // Set spacerrow value if null for next rount separation.
335
336
            if (is_null($spacerrow)) {
                $spacerrow = html_writer::empty_tag("tr", array("class" => "dtaTableSpacer"));
337
338
339
340
            }
        }
        $html .= html_writer::tag("table", $header . $body, array("class" => "dtaTable"));

341
        // Wrap generated html into final div.
342
343
344
345
346
347
        $html = html_writer::div($html, "dtaSubmissionDetails");

        return $html;
    }

}