db_utils.php 9.98 KB
Newer Older
1
<?php
2
namespace assignsubmission_dta;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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/>.
17

Lückemeyer's avatar
Lückemeyer committed
18
19
20
21
22
23
/**
 * 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
24
 */
25
26
27
28
29
30
31

use assignsubmission_dta\dta_backend_utils;
use assignsubmission_dta\view_submission_utils;
use assignsubmission_dta\models\dta_result;
use assignsubmission_dta\models\dta_result_summary;
use assignsubmission_dta\models\dta_recommendation;
class db_utils {
32

Lückemeyer's avatar
Lückemeyer committed
33
34
35
    /**
     * Summary database table name.
     */
36
    private const TABLE_SUMMARY = "assignsubmission_dta_summary";
Lückemeyer's avatar
Lückemeyer committed
37
38
39
    /**
     * Result database table name.
     */
40
41
    private const TABLE_RESULT = "assignsubmission_dta_result";

Kurzenberger's avatar
Kurzenberger committed
42
43
    private const TABLE_RECOMMENDATIONS = "assignsubmission_dta_recommendations";

44
45
46
47
48
    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
49
50
51
        $records = $DB->get_records(self::TABLE_RECOMMENDATIONS, [
            'assignment_id' => $assignmentid,
            'submission_id' => $submissionid,
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
86
        ]);
    
        // 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]);
87
                        
88
89
90
91
92
93
                    }
                }
            }
        }
    
        // Rückgabe der gefilterten Datensätze
Kurzenberger's avatar
Kurzenberger committed
94
95
        return $records;
    }
96
    
Kurzenberger's avatar
Kurzenberger committed
97

98
    /**
99
     * gets summary with all corresponding result entries
100
     *
101
102
     * @param int $assignmentid assignment id to search for
     * @param int $submissionid submission id to search for
Lückemeyer's avatar
Lückemeyer committed
103
     * @return DttResultSummary representing given submission
104
     */
105
    public static function get_result_summary_from_database(
106
107
        int $assignmentid,
        int $submissionid
108
    ): dta_result_summary {
109
110
        global $DB;

111
        // Fetch data from database.
112
        $summaryrecord = $DB->get_record(self::TABLE_SUMMARY, [
113
            "assignment_id" => $assignmentid,
114
            "submission_id" => $submissionid,
Lückemeyer's avatar
Lückemeyer committed
115
        ]);
116

117
        $resultsarray = $DB->get_records(self::TABLE_RESULT, [
118
            "assignment_id" => $assignmentid,
119
120
            "submission_id" => $submissionid,
        ]);
121

122
        // Create a summary instance.
123
        $summary = new dta_result_summary();
124
        $summary->timestamp = $summaryrecord->timestamp;
125
126
127
        $summary->globalstacktrace = $summaryrecord->global_stacktrace;
        $summary->successfultestcompetencies = $summaryrecord->successful_competencies;
        $summary->overalltestcompetencies = $summaryrecord->tested_competencies;
128
        $summary->results = [];
129

130
131
        // Create result instances and add to array of summary instance.
        foreach ($resultsarray as $rr) {
132
            $result = new dta_result();
133
134
            $result->packagename = $rr->package_name;
            $result->classname = $rr->class_name;
135
136
            $result->name = $rr->name;
            $result->state = $rr->state;
137
138
            $result->failuretype = $rr->failure_type;
            $result->failurereason = $rr->failure_reason;
139
            $result->stacktrace = $rr->stacktrace;
140
141
            $result->columnnumber = $rr->column_number;
            $result->linenumber = $rr->line_number;
142
143
144
145
146
147
148
            $result->position = $rr->position;

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

        return $summary;
    }
149
    public static function store_recommendations_to_database(
Kurzenberger's avatar
Kurzenberger committed
150
151
152
153
154
        int $assignmentid,
        int $submissionid,
        array $recommendations
    ): void {
        global $DB;
155
        error_log(print_r($recommendations, true));
Kurzenberger's avatar
Kurzenberger committed
156
157
    
        // If recommendations already exist, delete old values beforehand.
158
        $existingrecords = $DB->get_records('assignsubmission_dta_recommendations', [
Kurzenberger's avatar
Kurzenberger committed
159
160
161
162
163
164
165
166
167
168
169
170
            'assignment_id' => $assignmentid,
            'submission_id' => $submissionid,
        ]);
    
        if ($existingrecords) {
            $DB->delete_records('assignsubmission_dta_recommendations', [
                'assignment_id' => $assignmentid,
                'submission_id' => $submissionid,
            ]);
        }
    
        // Create new recommendation entries.
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        foreach ($recommendations as $recommendation) {
            // Ensure $recommendation is an instance of DtaRecommendation
            if ($recommendation instanceof dta_recommendation) {
                // Add assignment and submission IDs to the recommendation object
                $recommendation->assignment_id = $assignmentid;
                $recommendation->submission_id = $submissionid;
    
                error_log("Insert record");
                error_log(print_r($recommendation, true));
    
                // Insert the recommendation into the database
                $DB->insert_record('assignsubmission_dta_recommendations', $recommendation);
            } else {
                // Handle the case where $recommendation is not a DtaRecommendation instance
                error_log("Invalid recommendation object");
            }
Kurzenberger's avatar
Kurzenberger committed
187
188
189
        }
    }
    
190
191
192
193
194

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

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

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

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

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

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

    /**
     * cleans up database if plugin is uninstalled
     */
262
    public static function uninstall_plugin_cleaup(): void {
263
264
265
266
        global $DB;

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

269
270
271
    }

}