view.php 17.6 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

Lückemeyer's avatar
Lückemeyer committed
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * utility class for DTA submission plugin result display
 *
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @copyright Gero Lueckemeyer and student project teams
 */
class view_submission_utils {

    /** 
     * Broadly used in logic, parametrized for easier change.
     */
29
30
    const COMPONENT_NAME = "assignsubmission_dta";

31
32
33
    /**
     * generates a short summary html
     *
34
35
     * @param int assignmentid assignment
     * @param int submissionid submission to create a report for
36
37
     * @return string html
     */
38
    public static function generatesummaryhtml(
39
40
        int $assignmentid,
        int $submissionid
41
42
    ): string {

43
        // Fetch data.
44
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);
45
46
        $html = "";

47
        // Calculate success rate, if no unknown result states or compilation errors.
48
        $successrate = "?";
49
        if ($summary->unknownCount() == 0 && $summary->compilationErrorCount() == 0) {
50
            $successrate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
51
52
        }

53
        // Generate html.
54
55
        $html .= $summary->successfulCount() . "/";
        $html .= ($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0)
56
            ? $summary->resultCount() . " (" . $successrate . "%)"
57
                : "?";
Lückemeyer's avatar
Lückemeyer committed
58
        $html .= get_string("tests_successful", self::COMPONENT_NAME) . "<br />";
59
60

        if ($summary->compilationErrorCount() > 0) {
Lückemeyer's avatar
Lückemeyer committed
61
            $html .= $summary->compilationErrorCount() . get_string("compilation_errors", self::COMPONENT_NAME) . "<br />";
62
63
64
        }

        if ($summary->unknownCount() > 0) {
Lückemeyer's avatar
Lückemeyer committed
65
            $html .= $summary->unknownCount() . get_string("unknown_state", self::COMPONENT_NAME) . "<br />";
66
        }
67

Lückemeyer's avatar
Lückemeyer committed
68
69
70
71
        $showncompetencies = explode(";", $summary->successfultestcompetencies);
        $overallcompetencies = explode(";", $summary->overalltestcompetencies);
        
        $tmp = "";
Lückemeyer's avatar
Lückemeyer committed
72
73
74
        for ($index = 0, $size = count($showncompetencies); $index < $size; $index++) {
            $shown = $showncompetencies[$index];
            $comp = $overallcompetencies[$index];
Lückemeyer's avatar
Lückemeyer committed
75
76
77
78
79
80
            // If the competency was actually assessed by the assignment and tests, add a summary entry.
            if($shown!="0") {
                $tmp .= get_string("comp" . $index, self::COMPONENT_NAME) . " " . 100*floatval($shown)/floatval($comp) . "% " . "<br />";
            }
        }
        
Lückemeyer's avatar
Lückemeyer committed
81
        $html .= get_string("success_competencies", self::COMPONENT_NAME) . "<br />" . $tmp . "<br />";
82
83
84
85
86
87
88

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

    /**
     * generates detailed view html
     *
89
90
     * @param int assignmentid assignment
     * @param int submissionid submission to create a report for
91
     */
92
    public static function generatedetailhtml(
93
94
        int $assignmentid,
        int $submissionid
95
96
    ): string {

97
        // Fetch data.
98
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);
99
100
        $html = "";

101
        // Define a few css classes and prepare html attribute arrays to beautify the output.
102
103
        $tableheaderrowattributes = ["class" => "dtaTableHeaderRow"];
        $tablerowattributes = ["class" => "dtaTableRow"];
104
105
106
107
108
        $resultrowattributes = $tablerowattributes;
        $unknownattributes = 'dtaResultUnknown';
        $successattributes = 'dtaResultSuccess';
        $failureattributes = 'dtaResultFailure';
        $compilationerrorattributes = 'dtaResultCompilationError';
109

110
        // Summary table.
111
        $tmp = "";
112
        $tmp .= html_writer::tag("th", get_string("summary", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
113
        $tmp .= html_writer::empty_tag("th", ["class" => "dtaTableHeader"]);
114
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
115
116
117
118
        $header = html_writer::tag("thead", $header);

        $body = "";
        $tmp = "";
119
        $attributes = ["class" => "dtaTableData"];
120
121
        $tmp .= html_writer::tag(
            "td",
122
            get_string("total_items", self::COMPONENT_NAME),
123
124
125
126
127
128
129
            $attributes);

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

130
131
        $resultrowattributes = $tablerowattributes;
        $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
132

133
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
134
135

        $tmp = "";
136
        $tmp .= html_writer::tag("td", get_string("tests_successful", self::COMPONENT_NAME), $attributes);
137
138
        $tmp .= html_writer::tag( "td", $summary->successfulCount(), $attributes);

139
        $resultrowattributes = $tablerowattributes;
140
        $successrate = "?";
141
142

        if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
143
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
144
        } else {
145
146
            $successrate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
            if ($successrate < 50) {
147
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $compilationerrorattributes;
148
            } else if ($successrate < 75) {
149
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $failureattributes;
150
            } else {
151
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
152
153
            }
        }
154
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
155
156

        $tmp = "";
157
        $tmp .= html_writer::tag("td", get_string("failures", self::COMPONENT_NAME), $attributes);
158
159
        $tmp .= html_writer::tag("td", $summary->failedCount(), $attributes);

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

        $tmp = "";
169
        $tmp .= html_writer::tag("td", get_string("compilation_errors", self::COMPONENT_NAME), $attributes);
170
171
        $tmp .= html_writer::tag("td", $summary->compilationErrorCount(), $attributes);

172
        $resultrowattributes = $tablerowattributes;
173
        if ($summary->compilationErrorCount() > 0) {
174
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $compilationerrorattributes;
175
        } else {
176
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
177
        }
178
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
179
180

        $tmp = "";
181
        $tmp .= html_writer::tag("td", get_string("unknown_state", self::COMPONENT_NAME), $attributes);
182
183
        $tmp .= html_writer::tag("td", $summary->unknownCount(), $attributes);

184
        $resultrowattributes = $tablerowattributes;
185
        if ($summary->unknownCount() > 0) {
186
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
187
        } else {
188
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
189
        }
190
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
191
192

        $tmp = "";
193
        $tmp .= html_writer::tag("td", html_writer::tag("b", get_string("success_rate", self::COMPONENT_NAME)), $attributes);
194
195
196
197
        $tmp .= html_writer::tag(
            "td",
            html_writer::tag("b", $summary->successfulCount()
                . "/" . (($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0) ? $summary->resultCount()
198
                . " (" . $successrate . "%)"
199
200
201
                    : "?")),
            $attributes);

202
        $resultrowattributes = $tablerowattributes;
203
        if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
204
            $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
205
        } else {
206
            if ($successrate < 50) {
207
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $compilationerrorattributes;
208
            } else if ($successrate < 75) {
209
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $failureattributes;
210
            } else {
211
                $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $successattributes;
212
213
            }
        }
214
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
215
216

        $body = html_writer::tag("tbody", $body);
217
        $table = html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);
218
219
220

        $html .= $table;

221
222
        // Add empty div for spacing between summary and compentency table.
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
223
        
Lückemeyer's avatar
Lückemeyer committed
224
        // Competency assessment table.
225
        $body = "";
226
227
228
229
230
231
232
233
        $tmp = "";
        $tmp .= html_writer::tag("th", get_string("competencies", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
        $tmp .= html_writer::empty_tag("th", ["class" => "dtaTableHeader"]);
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
        $header = html_writer::tag("thead", $header);
        
        $showncompetencies = explode(";", $summary->successfultestcompetencies);
        $overallcompetencies = explode(";", $summary->overalltestcompetencies);
Lückemeyer's avatar
Lückemeyer committed
234
        
Lückemeyer's avatar
Lückemeyer committed
235
236
237
        for ($index = 0, $size = count($overallcompetencies); $index < $size; $index++) {
            $comp = $overallcompetencies[$index];
            $shown = $showncompetencies[$index];
238
            // If the competency was actually assessed by the assignment and tests, add a row in the table.
Lückemeyer's avatar
Lückemeyer committed
239
            if($comp != "0") {
240
241
242
                // New copy of base attributes array.
                $resultrowattributes = $tablerowattributes;
                $tmp = "";
243
                $tmp .= html_writer::tag("td", get_string("comp" . $index, self::COMPONENT_NAME), $resultrowattributes);                
Lückemeyer's avatar
Lückemeyer committed
244
                $tmp .= html_writer::tag("td", 100*floatval($shown)/floatval($comp) . "% " .
245
                    "(" . $shown . " / " . $comp . ")", $resultrowattributes);
Lückemeyer's avatar
Lückemeyer committed
246
                $tmp .= html_writer::tag("td", get_string("comp_expl" . $index, self::COMPONENT_NAME), $resultrowattributes);    
247
248
                
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
249
            }
250
251
252
253
254
        }
        $body = html_writer::tag("tbody", $body);
        $html .= html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);

        // Add empty div for spacing between competency and details table.
255
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
256

257
        // Details table.
258
        $tmp = "";
259
        $tmp .= html_writer::tag("th", get_string("details", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
260
        $tmp .= html_writer::empty_tag("th", ["class" => "dtaTableHeader"]);
261
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
262
263
264
        $header = html_writer::tag("thead", $header);

        $body = "";
265
        $spacerrow = null;
266
        foreach ($summary->results as $r) {
267
            // Add spacer first if not null.
268
269
            if (!is_null($spacerrow)) {
                $body .= $spacerrow;
270
271
            }

272
273
            // New copy of base attributes array.
            $resultrowattributes = $tablerowattributes;
274

275
            // Check which css class to add for the colored left-border according to resuls state.
276
            if ($r->state == 0) {
277
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultUnknown';
278
            } else if ($r->state == 1) {
279
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultSuccess';
280
            } else if ($r->state == 2) {
281
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultFailure';
282
            } else if ($r->state == 3) {
283
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultCompilationError';
284
285
286
287
288
            }

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
                get_string("package_name", self::COMPONENT_NAME),
                $attributes);
                
            $tmp .= html_writer::tag(
                "td",
                $r->packagename,
                $attributes);

            $tmp .= html_writer::tag(
                "td",
                get_string("unit_name", self::COMPONENT_NAME),
                $attributes);
                
            $tmp .= html_writer::tag(
                "td",
                $r->classname,
                $attributes);

            $tmp .= html_writer::tag(
                "td",
                get_string("test_name", self::COMPONENT_NAME),
310
                $attributes);
311

312
313
314
315
            $tmp .= html_writer::tag(
                "td",
                $r->name,
                $attributes);
316
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
317
318
319
320

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
321
                get_string("status", self::COMPONENT_NAME),
322
323
324
325
326
327
                $attributes);

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

330
            // If state is something different than successful, show additional rows.
331
332
333
334
            if ($r->state != 1) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
335
                    get_string("failure_type", self::COMPONENT_NAME),
336
                    $attributes);
337

338
339
340
341
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureType,
                    $attributes);
342
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
343

344
345
346
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
347
                    get_string("failure_reason", self::COMPONENT_NAME),
348
                    $attributes);
349

350
351
352
353
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureReason,
                    $attributes);
354
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
355

356
                // Only show line, column and position if they have useful values.
357
358
359
360
                if (!is_null($r->lineNumber) && $r->lineNumber > 0) {
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
Lückemeyer's avatar
Lückemeyer committed
361
                        get_string("line_no", self::COMPONENT_NAME),
362
363
364
365
366
367
                        $attributes);

                    $tmp .= html_writer::tag(
                        "td",
                        $r->lineNumber,
                        $attributes);
368
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
369
370
371
                }

                if (!is_null($r->columnNumber) && $r->columnNumber > 0) {
372
373
374
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
375
                        get_string("col_no", self::COMPONENT_NAME),
376
                        $attributes);
377

378
379
380
381
382
                    $tmp .= html_writer::tag(
                        "td",
                        $r->columnNumber,
                        $attributes);
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
383
384
385
                }

                if (!is_null($r->position) && $r->position > 0) {
386
387
388
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
389
                        get_string("pos", self::COMPONENT_NAME),
390
                        $attributes);
391

392
393
394
395
396
                    $tmp .= html_writer::tag(
                        "td",
                        $r->position,
                        $attributes);
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
397
398
399
400
401
                }

                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
402
                    get_string("stacktrace", self::COMPONENT_NAME),
403
404
405
406
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
407
                    html_writer::tag("details", $r->stacktrace, ["class" => "dtaStacktraceDetails"]),
408
                    $attributes);
409
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
410
411
            }

412
            // Set spacerrow value if null for next round separation.
413
            if (is_null($spacerrow)) {
414
                $spacerrow = html_writer::empty_tag("tr", ["class" => "dtaTableSpacer"]);
415
416
            }
        }
417
        $html .= html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);
418

419
        // Wrap generated html into final div.
420
421
422
423
424
425
        $html = html_writer::div($html, "dtaSubmissionDetails");

        return $html;
    }

}