view.php 16.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
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 .= get_string("success_competencies", self::COMPONENT_NAME) . $summary->successfultestcompetencies . "<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
        // Add empty div for spacing between summary and compentency table.
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
201
202
        
        $body = "";
203
204
205
206
207
208
209
210
        $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
211
        
Lückemeyer's avatar
Lückemeyer committed
212
        for ($index=1, $size=count($overallcompetencies); $index<=$size; $index++) {
213
214
            $comp=$overallcompetencies[$index];
            $shown=$showncompetencies[$index];
215
            // If the competency was actually assessed by the assignment and tests, add a row in the table.
216
//            if($comp!="0") {
217
218
219
220
221
222
223
                // 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", 
224
225
                    floatval($shown)/floatval ($comp) . "% 
                    (" . $shown . " / " . $comp . ")",
226
227
228
                    $resultrowattributes);
                
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
229
//            }
230
231
232
233
234
        }
        $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.
235
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
236

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $html;
    }

}