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

Lückemeyer's avatar
Lückemeyer committed
59
        $html .= get_string("success_competencies", self::COMPONENT_NAME) . $summary->successfulTestCompetencyProfile . "<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
        // 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);
Lückemeyer's avatar
Lückemeyer committed
210
        
211
212
213
214
215
216
217
218
219
220
221
        $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", 
Lückemeyer's avatar
Lückemeyer committed
222
223
                    floatval($showncompetencies[$index])/floatval ($comp) . "% 
                    (" . $showncompetencies[$index] . " / " . $comp . ")",
224
225
226
227
228
229
230
231
232
233
                    $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.
234
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
235

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $html;
    }

}