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

class ViewSubmissionUtils {

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

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

34
        // Fetch data.
35
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);
36
37
        $html = "";

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

44
        // Generate html.
45
46
        $html .= $summary->successfulCount() . "/";
        $html .= ($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0)
47
            ? $summary->resultCount() . " (" . $successrate . "%)"
48
                : "?";
49
        $html .= get_string("tests_successful", self::COMPONENT_NAME) . "<br>";
50
51

        if ($summary->compilationErrorCount() > 0) {
52
            $html .= $summary->compilationErrorCount() . get_string("compilation_errors", self::COMPONENT_NAME) . "<br>";
53
54
55
        }

        if ($summary->unknownCount() > 0) {
56
            $html .= $summary->unknownCount() . get_string("unknown_state", self::COMPONENT_NAME) . "<br>";
57
        }
58

59
        $html .= $summary->successfulTestCompetencyProfile . get_string("success_competencies", self::COMPONENT_NAME) . "<br>";
60
61
62
63
64
65
66

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

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

75
        // Fetch data.
76
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);
77
78
        $html = "";

79
        // Define a few css classes and prepare html attribute arrays to beautify the output.
80
81
        $tableheaderrowattributes = ["class" => "dtaTableHeaderRow"];
        $tablerowattributes = ["class" => "dtaTableRow"];
82
83
84
85
86
        $resultrowattributes = $tablerowattributes;
        $unknownattributes = 'dtaResultUnknown';
        $successattributes = 'dtaResultSuccess';
        $failureattributes = 'dtaResultFailure';
        $compilationerrorattributes = 'dtaResultCompilationError';
87

88
        // Summary table.
89
        $tmp = "";
90
        $tmp .= html_writer::tag("th", get_string("summary", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
91
        $tmp .= html_writer::empty_tag("th", ["class" => "dtaTableHeader"]);
92
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
93
94
95
96
        $header = html_writer::tag("thead", $header);

        $body = "";
        $tmp = "";
97
        $attributes = ["class" => "dtaTableData"];
98
99
        $tmp .= html_writer::tag(
            "td",
100
            get_string("total_items", self::COMPONENT_NAME),
101
102
103
104
105
106
107
            $attributes);

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

108
109
        $resultrowattributes = $tablerowattributes;
        $resultrowattributes['class'] = $resultrowattributes['class'] . " " . $unknownattributes;
110

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

        $tmp = "";
114
        $tmp .= html_writer::tag("td", get_string("tests_successful", self::COMPONENT_NAME), $attributes);
115
116
        $tmp .= html_writer::tag( "td", $summary->successfulCount(), $attributes);

117
        $resultrowattributes = $tablerowattributes;
118
        $successrate = "?";
119
120

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

        $tmp = "";
135
        $tmp .= html_writer::tag("td", get_string("failures", self::COMPONENT_NAME), $attributes);
136
137
        $tmp .= html_writer::tag("td", $summary->failedCount(), $attributes);

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

        $tmp = "";
147
        $tmp .= html_writer::tag("td", get_string("compilation_errors", self::COMPONENT_NAME), $attributes);
148
149
        $tmp .= html_writer::tag("td", $summary->compilationErrorCount(), $attributes);

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

        $tmp = "";
159
        $tmp .= html_writer::tag("td", get_string("unknown_state", self::COMPONENT_NAME), $attributes);
160
161
        $tmp .= html_writer::tag("td", $summary->unknownCount(), $attributes);

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

        $tmp = "";
171
        $tmp .= html_writer::tag("td", html_writer::tag("b", get_string("success_rate", self::COMPONENT_NAME)), $attributes);
172
173
174
175
        $tmp .= html_writer::tag(
            "td",
            html_writer::tag("b", $summary->successfulCount()
                . "/" . (($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0) ? $summary->resultCount()
176
                . " (" . $successrate . "%)"
177
178
179
                    : "?")),
            $attributes);

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

        $body = html_writer::tag("tbody", $body);
195
        $table = html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);
196
197
198

        $html .= $table;

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
        // Add empty div for spacing between summary and compentency table.
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);

        $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);
        $index=1;
        foreach ($overallcompetencies as $comp) {
            // If the competency was actually assessed by the assignment and tests, add a row in the table.
            if($comp!="0") {
                // New copy of base attributes array.
                $resultrowattributes = $tablerowattributes;
                $tmp = "";
                $tmp .= html_writer::tag("td", get_string("comp" . $index, self::COMPONENT_NAME), $resultrowattributes);
                
                $tmp = "";
                $tmp .= html_writer::tag("td", 
                    floatval($showncompetencies[index])/floatval ($comp) . "% 
                    (" . $showncompetencies[index] . " / " . $comp . ")",
                    $resultrowattributes);
                
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
            }
            $index = $index + 1;
        }
        $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.
233
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
234

235
        // Details table.
236
        $tmp = "";
237
        $tmp .= html_writer::tag("th", get_string("details", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
238
        $tmp .= html_writer::empty_tag("th", ["class" => "dtaTableHeader"]);
239
        $header = html_writer::tag("tr", $tmp, $tableheaderrowattributes);
240
241
242
        $header = html_writer::tag("thead", $header);

        $body = "";
243
        $spacerrow = null;
244
        foreach ($summary->results as $r) {
245
            // Add spacer first if not null.
246
247
            if (!is_null($spacerrow)) {
                $body .= $spacerrow;
248
249
            }

250
251
            // New copy of base attributes array.
            $resultrowattributes = $tablerowattributes;
252

253
            // Check which css class to add for the colored left-border according to resuls state.
254
            if ($r->state == 0) {
255
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultUnknown';
256
            } else if ($r->state == 1) {
257
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultSuccess';
258
            } else if ($r->state == 2) {
259
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultFailure';
260
            } else if ($r->state == 3) {
261
                $resultrowattributes['class'] = $resultrowattributes['class'] . ' dtaResultCompilationError';
262
263
264
265
266
            }

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
                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),
288
                $attributes);
289

290
291
292
293
            $tmp .= html_writer::tag(
                "td",
                $r->name,
                $attributes);
294
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
295
296
297
298

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
299
                get_string("status", self::COMPONENT_NAME),
300
301
302
303
304
305
                $attributes);

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

308
            // If state is something different than successful, show additional rows.
309
310
311
312
            if ($r->state != 1) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
313
                    get_string("failure_type", self::COMPONENT_NAME),
314
                    $attributes);
315

316
317
318
319
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureType,
                    $attributes);
320
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
321

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

328
329
330
331
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureReason,
                    $attributes);
332
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
333

334
                // Only show line, column and position if they have useful values.
335
336
337
338
                if (!is_null($r->lineNumber) && $r->lineNumber > 0) {
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
Lückemeyer's avatar
Lückemeyer committed
339
                        get_string("line_no", self::COMPONENT_NAME),
340
341
342
343
344
345
                        $attributes);

                    $tmp .= html_writer::tag(
                        "td",
                        $r->lineNumber,
                        $attributes);
346
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
347
348
349
                }

                if (!is_null($r->columnNumber) && $r->columnNumber > 0) {
350
351
352
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
353
                        get_string("col_no", self::COMPONENT_NAME),
354
                        $attributes);
355

356
357
358
359
360
                    $tmp .= html_writer::tag(
                        "td",
                        $r->columnNumber,
                        $attributes);
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
361
362
363
                }

                if (!is_null($r->position) && $r->position > 0) {
364
365
366
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
367
                        get_string("pos", self::COMPONENT_NAME),
368
                        $attributes);
369

370
371
372
373
374
                    $tmp .= html_writer::tag(
                        "td",
                        $r->position,
                        $attributes);
                    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
375
376
377
378
379
                }

                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
380
                    get_string("stacktrace", self::COMPONENT_NAME),
381
382
383
384
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
385
                    html_writer::tag("details", $r->stacktrace, ["class" => "dtaStacktraceDetails"]),
386
                    $attributes);
387
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
388
389
            }

390
            // Set spacerrow value if null for next round separation.
391
            if (is_null($spacerrow)) {
392
                $spacerrow = html_writer::empty_tag("tr", ["class" => "dtaTableSpacer"]);
393
394
            }
        }
395
        $html .= html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);
396

397
        // Wrap generated html into final div.
398
399
400
401
402
403
        $html = html_writer::div($html, "dtaSubmissionDetails");

        return $html;
    }

}