database.php 9.77 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
/**
 * persistence layer utility class
 *
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @copyright Gero Lueckemeyer and student project teams
Lückemeyer's avatar
Lückemeyer committed
23
 */
24
25
class DbUtils {

Lückemeyer's avatar
Lückemeyer committed
26
27
28
    /**
     * Summary database table name.
     */
29
    private const TABLE_SUMMARY = "assignsubmission_dta_summary";
Lückemeyer's avatar
Lückemeyer committed
30
31
32
    /**
     * Result database table name.
     */
33
34
    private const TABLE_RESULT = "assignsubmission_dta_result";

Kurzenberger's avatar
Kurzenberger committed
35
36
    private const TABLE_RECOMMENDATIONS = "assignsubmission_dta_recommendations";

37
38
39
40
41
    public static function get_recommendations_from_database(int $assignmentid, int $submissionid): array {
        global $DB,$USER;
        $userid = $USER->id;
    
        // Schritt 1: Alle Empfehlungen abrufen
Kurzenberger's avatar
Kurzenberger committed
42
43
44
        $records = $DB->get_records(self::TABLE_RECOMMENDATIONS, [
            'assignment_id' => $assignmentid,
            'submission_id' => $submissionid,
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        ]);
    
        // Schritt 2: Modul-ID für 'assign' abrufen
        $module = $DB->get_record('modules', ['name' => 'assign'], 'id');
        if (!$module) {
            // Fehlerbehandlung, falls das Modul nicht gefunden wird
            return $records;
        }
        $moduleid = $module->id;
    
        // Schritt 3: Überprüfe jeden Datensatz
        foreach ($records as $key => $record) {
            // Hol den Namen der Übung aus dem Datensatz
            $exercisename = $record->exercise_name;
    
            // Suche das Assignment mit diesem Namen
            $assign = $DB->get_record('assign', ['name' => $exercisename], 'id');
    
            if ($assign) {
                // Hole die Kursmodul-ID (coursemoduleid) für dieses Assignment
                $cm = $DB->get_record('course_modules', [
                    'module' => $moduleid,
                    'instance' => $assign->id
                ], 'id');
    
                if ($cm) {
                    // Überprüfe den Abschlussstatus für dieses Kursmodul und den Benutzer
                    $completion = $DB->get_record('course_modules_completion', [
                        'coursemoduleid' => $cm->id,
                        'userid' => $userid
                    ], 'completionstate');
    
                    // Wenn der Abschlussstatus 1 ist, entferne den Datensatz aus $records
                    if ($completion && $completion->completionstate == 1) {
                        unset($records[$key]);
                    }
                }
            }
        }
    
        // Rückgabe der gefilterten Datensätze
Kurzenberger's avatar
Kurzenberger committed
86
87
        return $records;
    }
88
    
Kurzenberger's avatar
Kurzenberger committed
89

90
    /**
91
     * gets summary with all corresponding result entries
92
     *
93
94
     * @param int $assignmentid assignment id to search for
     * @param int $submissionid submission id to search for
Lückemeyer's avatar
Lückemeyer committed
95
     * @return DttResultSummary representing given submission
96
     */
97
    public static function getresultsummaryfromdatabase(
98
99
        int $assignmentid,
        int $submissionid
100
101
102
    ): DtaResultSummary {
        global $DB;

103
        // Fetch data from database.
104
        $summaryrecord = $DB->get_record(self::TABLE_SUMMARY, [
105
            "assignment_id" => $assignmentid,
106
            "submission_id" => $submissionid,
Lückemeyer's avatar
Lückemeyer committed
107
        ]);
108

109
        $resultsarray = $DB->get_records(self::TABLE_RESULT, [
110
            "assignment_id" => $assignmentid,
111
112
            "submission_id" => $submissionid,
        ]);
113

114
        // Create a summary instance.
115
        $summary = new DtaResultSummary();
116
        $summary->timestamp = $summaryrecord->timestamp;
117
118
119
        $summary->globalstacktrace = $summaryrecord->global_stacktrace;
        $summary->successfultestcompetencies = $summaryrecord->successful_competencies;
        $summary->overalltestcompetencies = $summaryrecord->tested_competencies;
120
        $summary->results = [];
121

122
123
        // Create result instances and add to array of summary instance.
        foreach ($resultsarray as $rr) {
124
            $result = new DtaResult();
125
126
            $result->packagename = $rr->package_name;
            $result->classname = $rr->class_name;
127
128
            $result->name = $rr->name;
            $result->state = $rr->state;
129
130
            $result->failuretype = $rr->failure_type;
            $result->failurereason = $rr->failure_reason;
131
            $result->stacktrace = $rr->stacktrace;
132
133
            $result->columnnumber = $rr->column_number;
            $result->linenumber = $rr->line_number;
134
135
136
137
138
139
140
            $result->position = $rr->position;

            $summary->results[] = $result;
        }

        return $summary;
    }
Kurzenberger's avatar
Kurzenberger committed
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
    public static function storeRecommendationstoDatabase(
        int $assignmentid,
        int $submissionid,
        array $recommendations
    ): void {
        global $DB;
        error_log(print_r($recommendations,true));
    
        // Prepare recommendations to persist to array.
        $recommendationrecords = [];
        foreach ($recommendations as $recommendation) {
            $record = new stdClass();
            $record->assignment_id = $assignmentid;
            $record->submission_id = $submissionid;
            $record->topic = $recommendation['topic'];
            $record->url = $recommendation['url'];
            $record->exercise_name = $recommendation['exercise_name'];
            $record->difficulty = $recommendation['difficulty'];
            $record->score = $recommendation['score'];
            $recommendationrecords[] = $record;
        }
    
        error_log("Das sind die Recommendationrecords.");
        error_log(print_r($recommendationrecords,true));
    
        // If recommendations already exist, delete old values beforehand.
        $existingrecords = $DB->get_record('assignsubmission_dta_recommendations', [
            'assignment_id' => $assignmentid,
            'submission_id' => $submissionid,
        ]);
    
        if ($existingrecords) {
            $DB->delete_records('assignsubmission_dta_recommendations', [
                'assignment_id' => $assignmentid,
                'submission_id' => $submissionid,
            ]);
        }
    
        // Create new recommendation entries.
        foreach ($recommendationrecords as $rec) {
            error_log("Insert record");
            error_log(print_r($rec,true));
            $DB->insert_record('assignsubmission_dta_recommendations', $rec);
        }
    }
    
187
188
189
190
191

    /**
     * save given result summary and single results to database
     * under given assignment and submission id
     *
Lückemeyer's avatar
Lückemeyer committed
192
193
194
     * @param int $assignmentid assigment this is submission is linked to
     * @param int $submissionid submission of this result
     * @param DtaResultSummary $summary instance to persist
195
     */
196
    public static function storeresultsummarytodatabase(
197
198
        int $assignmentid,
        int $submissionid,
199
200
201
202
        DtaResultSummary $summary
    ): void {
        global $DB;

203
204
205
206
207
        // Prepare new database entries.
        $summaryrecord = new stdClass();
        $summaryrecord->assignment_id = $assignmentid;
        $summaryrecord->submission_id = $submissionid;
        $summaryrecord->timestamp = $summary->timestamp;
208
209
210
        $summaryrecord->global_stacktrace = $summary->globalstacktrace;
        $summaryrecord->successful_competencies = $summary->successfultestcompetencies;
        $summaryrecord->tested_competencies = $summary->overalltestcompetencies;
Kurzenberger's avatar
Kurzenberger committed
211
        
212
        // Prepare results to persist to array.
213
        $resultrecords = [];
214
        foreach ($summary->results as $r) {
215
            $record = new stdClass();
216
217
            $record->assignment_id = $assignmentid;
            $record->submission_id = $submissionid;
218
219
            $record->package_name = $r->packagename;
            $record->class_name = $r->classname;
220
221
            $record->name = $r->name;
            $record->state = $r->state;
222
223
            $record->failure_type = $r->failuretype;
            $record->failure_reason = $r->failurereason;
224
            $record->stacktrace = $r->stacktrace;
225
226
            $record->column_number = $r->columnnumber;
            $record->line_number = $r->linenumber;
227
            $record->position = $r->position;
228
            $resultrecords[] = $record;
229
230
        }

231
        // If results already exist, delete old values beforehand.
232
        $submission = $DB->get_record(self::TABLE_SUMMARY, [
233
            'assignment_id' => $assignmentid,
234
235
            'submission_id' => $submissionid,
        ]);
236
237

        if ($submission) {
238
            $DB->delete_records(self::TABLE_RESULT, [
239
                'assignment_id' => $assignmentid,
240
241
                'submission_id' => $submissionid,
            ]);
242

243
            $DB->delete_records(self::TABLE_SUMMARY, [
244
                'assignment_id' => $assignmentid,
245
246
                'submission_id' => $submissionid,
            ]);
247
248
        }

249
250
251
        // Create summary and single result entries.
        $DB->insert_record(self::TABLE_SUMMARY, $summaryrecord);
        foreach ($resultrecords as $rr) {
252
253
254
255
256
257
258
            $DB->insert_record(self::TABLE_RESULT, $rr);
        }
    }

    /**
     * cleans up database if plugin is uninstalled
     */
259
    public static function uninstallplugincleaup(): void {
260
261
262
263
        global $DB;

        $DB->delete_records(self::TABLE_RESULT, null);
        $DB->delete_records(self::TABLE_SUMMARY, null);
Kurzenberger's avatar
Kurzenberger committed
264
265
        $DB->delete_records(self::TABLE_RECOMMENDATIONS, null);

266
267
268
    }

}