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 .= 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
        
212
        for ($index=0, $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
                // New copy of base attributes array.
                $resultrowattributes = $tablerowattributes;
                $tmp = "";
220
221
222
                $tmp .= html_writer::tag("td", get_string("comp" . $index, self::COMPONENT_NAME), $resultrowattributes);                
                $tmp .= html_writer::tag("td", floatval($shown)/floatval ($comp) . "% " .
                    "(" . $shown . " / " . $comp . ")", $resultrowattributes);
223
224
                
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
225
            }
226
227
228
229
230
        }
        $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.
231
        $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
232

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

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

248
249
            // New copy of base attributes array.
            $resultrowattributes = $tablerowattributes;
250

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $html;
    }

}