view.php 17.3 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
/**
 * utility class for DTA submission plugin result display
 *
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class view_submission_utils {

Lückemeyer's avatar
Lückemeyer committed
25
    /**
Lückemeyer's avatar
Lückemeyer committed
26
27
     * Broadly used in logic, parametrized for easier change.
     */
28
29
    const COMPONENT_NAME = "assignsubmission_dta";

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

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

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

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

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

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

Lückemeyer's avatar
Lückemeyer committed
67
68
        $showncompetencies = explode(";", $summary->successfultestcompetencies);
        $overallcompetencies = explode(";", $summary->overalltestcompetencies);
Lückemeyer's avatar
Lückemeyer committed
69

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

Lückemeyer's avatar
Lückemeyer committed
81
        $html .= get_string("success_competencies", self::COMPONENT_NAME) . "<br />" . $tmp . "<br />";
82
83
84
85
86

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

    /**
Kurzenberger's avatar
Kurzenberger committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
 * Generiert die detaillierte HTML-Ansicht, einschließlich Zusammenfassung, Kompetenzen, Details und Empfehlungen.
 *
 * @param int $assignmentid Assignment-ID
 * @param int $submissionid Submission-ID, für die der Bericht erstellt wird
 * @return string HTML-Code
 */
public static function generatedetailhtml(
    int $assignmentid,
    int $submissionid
): string {

    // HTML-Inhalt initialisieren
    $html = "";

    // Daten abrufen
    $summary = DbUtils::getResultSummaryFromDatabase($assignmentid, $submissionid);

    // CSS-Klassen und HTML-Attributarrays definieren
    $tableheaderrowattributes = ["class" => "dtaTableHeaderRow"];
    $tablerowattributes = ["class" => "dtaTableRow"];
    $attributes = ["class" => "dtaTableData"];
    $unknownattributes = 'dtaResultUnknown';
    $successattributes = 'dtaResultSuccess';
    $failureattributes = 'dtaResultFailure';
    $compilationerrorattributes = 'dtaResultCompilationError';

    // **Zusammenfassungstabelle erstellen**
    // Kopfzeile
    $tmp = "";
    $tmp .= html_writer::tag("th", get_string("summary", 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);

    // Tabellenkörper
    $body = "";

    // Gesamtanzahl
    $tmp = "";
    $tmp .= html_writer::tag("td", get_string("total_items", self::COMPONENT_NAME), $attributes);
    $tmp .= html_writer::tag("td", $summary->resultCount(), $attributes);

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

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

    // Erfolgreiche Tests
    $tmp = "";
    $tmp .= html_writer::tag("td", get_string("tests_successful", self::COMPONENT_NAME), $attributes);
    $tmp .= html_writer::tag("td", $summary->successfulCount(), $attributes);

    $resultrowattributes = $tablerowattributes;
    $successrate = "?";

    if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
        $resultrowattributes['class'] .= " " . $unknownattributes;
    } else {
        $successrate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
        if ($successrate < 50) {
            $resultrowattributes['class'] .= " " . $compilationerrorattributes;
        } else if ($successrate < 75) {
            $resultrowattributes['class'] .= " " . $failureattributes;
        } else {
            $resultrowattributes['class'] .= " " . $successattributes;
        }
    }
    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);

    // Fehlgeschlagene Tests
    $tmp = "";
    $tmp .= html_writer::tag("td", get_string("failures", self::COMPONENT_NAME), $attributes);
    $tmp .= html_writer::tag("td", $summary->failedCount(), $attributes);

    $resultrowattributes = $tablerowattributes;
    if ($summary->failedCount() > 0) {
        $resultrowattributes['class'] .= " " . $failureattributes;
    } else {
        $resultrowattributes['class'] .= " " . $successattributes;
    }
    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);

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

    $resultrowattributes = $tablerowattributes;
    if ($summary->compilationErrorCount() > 0) {
        $resultrowattributes['class'] .= " " . $compilationerrorattributes;
    } else {
        $resultrowattributes['class'] .= " " . $successattributes;
    }
    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);

    // Unbekannter Status
    $tmp = "";
    $tmp .= html_writer::tag("td", get_string("unknown_state", self::COMPONENT_NAME), $attributes);
    $tmp .= html_writer::tag("td", $summary->unknownCount(), $attributes);

    $resultrowattributes = $tablerowattributes;
    if ($summary->unknownCount() > 0) {
        $resultrowattributes['class'] .= " " . $unknownattributes;
    } else {
        $resultrowattributes['class'] .= " " . $successattributes;
    }
    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);

    // Erfolgsrate
    $tmp = "";
    $tmp .= html_writer::tag("td", html_writer::tag("b", get_string("success_rate", self::COMPONENT_NAME)), $attributes);
    $tmp .= html_writer::tag(
        "td",
        html_writer::tag("b", $summary->successfulCount()
            . "/" . (($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0) ? $summary->resultCount()
            . " (" . $successrate . "%)"
                : "?")),
        $attributes);

    $resultrowattributes = $tablerowattributes;
    if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
        $resultrowattributes['class'] .= " " . $unknownattributes;
    } else {
        if ($successrate < 50) {
            $resultrowattributes['class'] .= " " . $compilationerrorattributes;
        } else if ($successrate < 75) {
            $resultrowattributes['class'] .= " " . $failureattributes;
        } else {
            $resultrowattributes['class'] .= " " . $successattributes;
        }
    }
    $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
219

Kurzenberger's avatar
Kurzenberger committed
220
221
222
    // Tabelle zusammenstellen
    $body = html_writer::tag("tbody", $body);
    $table = html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);
223

Kurzenberger's avatar
Kurzenberger committed
224
    $html .= $table;
225

Kurzenberger's avatar
Kurzenberger committed
226
227
    // **Abstand zwischen Tabellen**
    $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);
228

Kurzenberger's avatar
Kurzenberger committed
229
230
231
232
233
234
235
    // **Kompetenzbewertungstabelle erstellen**
    $body = "";
    $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);
236

Kurzenberger's avatar
Kurzenberger committed
237
238
    $showncompetencies = explode(";", $summary->successfultestcompetencies);
    $overallcompetencies = explode(";", $summary->overalltestcompetencies);
239

Kurzenberger's avatar
Kurzenberger committed
240
241
242
243
244
245
246
247
248
249
250
    for ($index = 0, $size = count($overallcompetencies); $index < $size; $index++) {
        $comp = $overallcompetencies[$index];
        $shown = $showncompetencies[$index];
        // Kompetenz wird nur hinzugefügt, wenn sie bewertet wurde
        if ($comp != "0") {
            $resultrowattributes = $tablerowattributes;
            $tmp = "";
            $tmp .= html_writer::tag("td", get_string("comp" . $index, self::COMPONENT_NAME), $attributes);
            $tmp .= html_writer::tag("td", 100 * floatval($shown) / floatval($comp) . "% " .
                "(" . $shown . " / " . $comp . ")", $attributes);
            $tmp .= html_writer::tag("td", get_string("comp_expl" . $index, self::COMPONENT_NAME), $attributes);
251

Kurzenberger's avatar
Kurzenberger committed
252
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
253
        }
Kurzenberger's avatar
Kurzenberger committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    }
    $body = html_writer::tag("tbody", $body);
    $html .= html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);

    // **Abstand zwischen Tabellen**
    $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);

    // **Detailtabelle erstellen**
    $tmp = "";
    $tmp .= html_writer::tag("th", get_string("details", 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);

    $body = "";
    $spacerrow = null;
    foreach ($summary->results as $r) {
        // Abstand zwischen den Ergebnissen
        if (!is_null($spacerrow)) {
            $body .= $spacerrow;
274
275
        }

276
        $resultrowattributes = $tablerowattributes;
277

Kurzenberger's avatar
Kurzenberger committed
278
279
280
281
282
283
284
285
286
        // CSS-Klasse basierend auf dem Status des Ergebnisses hinzufügen
        if ($r->state == 0) {
            $resultrowattributes['class'] .= ' dtaResultUnknown';
        } else if ($r->state == 1) {
            $resultrowattributes['class'] .= ' dtaResultSuccess';
        } else if ($r->state == 2) {
            $resultrowattributes['class'] .= ' dtaResultFailure';
        } else if ($r->state == 3) {
            $resultrowattributes['class'] .= ' dtaResultCompilationError';
287
288
289
        }

        $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
290
291
292
293
294
295
        $tmp .= html_writer::tag("td", 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), $attributes);
        $tmp .= html_writer::tag("td", $r->name, $attributes);
296
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
297

298
        $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
299
300
301
        $tmp .= html_writer::tag("td", get_string("status", self::COMPONENT_NAME), $attributes);
        $tmp .= html_writer::tag("td", DtaResult::getStateName($r->state), $attributes);
        $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
302

Kurzenberger's avatar
Kurzenberger committed
303
304
        // Zusätzliche Informationen für nicht erfolgreiche Zustände
        if ($r->state != 1) {
305
            $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
306
307
            $tmp .= html_writer::tag("td", get_string("failure_type", self::COMPONENT_NAME), $attributes);
            $tmp .= html_writer::tag("td", $r->failureType, $attributes);
308
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
309
310

            $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
311
312
            $tmp .= html_writer::tag("td", get_string("failure_reason", self::COMPONENT_NAME), $attributes);
            $tmp .= html_writer::tag("td", $r->failureReason, $attributes);
313
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
314

Kurzenberger's avatar
Kurzenberger committed
315
316
            // Zeilennummer anzeigen, falls vorhanden
            if (!is_null($r->lineNumber) && $r->lineNumber > 0) {
317
                $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
318
319
                $tmp .= html_writer::tag("td", get_string("line_no", self::COMPONENT_NAME), $attributes);
                $tmp .= html_writer::tag("td", $r->lineNumber, $attributes);
320
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
Kurzenberger's avatar
Kurzenberger committed
321
            }
322

Kurzenberger's avatar
Kurzenberger committed
323
324
            // Spaltennummer anzeigen, falls vorhanden
            if (!is_null($r->columnNumber) && $r->columnNumber > 0) {
325
                $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
326
327
                $tmp .= html_writer::tag("td", get_string("col_no", self::COMPONENT_NAME), $attributes);
                $tmp .= html_writer::tag("td", $r->columnNumber, $attributes);
328
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
Kurzenberger's avatar
Kurzenberger committed
329
            }
330

Kurzenberger's avatar
Kurzenberger committed
331
332
            // Position anzeigen, falls vorhanden
            if (!is_null($r->position) && $r->position > 0) {
333
                $tmp = "";
Kurzenberger's avatar
Kurzenberger committed
334
335
                $tmp .= html_writer::tag("td", get_string("pos", self::COMPONENT_NAME), $attributes);
                $tmp .= html_writer::tag("td", $r->position, $attributes);
336
                $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
337
338
            }

Kurzenberger's avatar
Kurzenberger committed
339
340
341
342
343
            // Stacktrace anzeigen
            $tmp = "";
            $tmp .= html_writer::tag("td", get_string("stacktrace", self::COMPONENT_NAME), $attributes);
            $tmp .= html_writer::tag("td", html_writer::tag("details", $r->stacktrace, ["class" => "dtaStacktraceDetails"]), $attributes);
            $body .= html_writer::tag("tr", $tmp, $resultrowattributes);
344
345
        }

Kurzenberger's avatar
Kurzenberger committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
        // Spacerrow für den nächsten Durchlauf setzen
        if (is_null($spacerrow)) {
            $spacerrow = html_writer::empty_tag("tr", ["class" => "dtaTableSpacer"]);
        }
    }
    $body = html_writer::tag("tbody", $body);
    $html .= html_writer::tag("table", $header . $body, ["class" => "dtaTable"]);

    // **Abstand zwischen Detailtabelle und Empfehlungstabelle**
    $html .= html_writer::empty_tag("div", ["class" => "dtaSpacer"]);

    // **Empfehlungstabelle hinzufügen**
    // Empfehlungen für die Submission abrufen
    $recommendations = DbUtils::get_recommendations_from_database($assignmentid, $submissionid);

    if (!empty($recommendations)) {
        // Überschrift für Empfehlungen
        $html .= html_writer::tag('h3', get_string('recommendations', self::COMPONENT_NAME));

        // Tabellenkopf für Empfehlungen
        $tableheader = "";
        $tableheader .= html_writer::tag("th", get_string("topic", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
        $tableheader .= html_writer::tag("th", get_string("exercise_name", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
        $tableheader .= html_writer::tag("th", get_string("url", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
        $tableheader .= html_writer::tag("th", get_string("difficulty", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);
        $tableheader .= html_writer::tag("th", get_string("score", self::COMPONENT_NAME), ["class" => "dtaTableHeader"]);

        $tableheader = html_writer::tag("tr", $tableheader, ["class" => "dtaTableHeaderRow"]);
        $tableheader = html_writer::tag("thead", $tableheader);

        // Tabellenkörper für Empfehlungen
        $tablebody = "";
        foreach ($recommendations as $recommendation) {
            $tablerow = "";
            $tablerow .= html_writer::tag("td", $recommendation->topic, $attributes);
            $tablerow .= html_writer::tag("td", $recommendation->exercise_name, $attributes);
            $tablerow .= html_writer::tag("td", html_writer::link($recommendation->url, $recommendation->url), $attributes);
            $tablerow .= html_writer::tag("td", $recommendation->difficulty, $attributes);
            $tablerow .= html_writer::tag("td", $recommendation->score, $attributes);

            $tablebody .= html_writer::tag("tr", $tablerow, $tablerowattributes);
        }
        $tablebody = html_writer::tag("tbody", $tablebody);
389

Kurzenberger's avatar
Kurzenberger committed
390
391
        // Empfehlungstabelle zusammenstellen
        $html .= html_writer::tag("table", $tableheader . $tablebody, ["class" => "dtaTable"]);
392
393
    }

Kurzenberger's avatar
Kurzenberger committed
394
395
396
397
398
399
400
    // Abschließendes Div für die gesamte HTML-Ausgabe
    $html = html_writer::div($html, "dtaSubmissionDetails");

    return $html;
}


401
}