database.php 8.35 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    private const TABLE_RECOMMENDATIONS = "assignsubmission_dta_recommendations";

    /**
     * Gets the recommendations for a given submission.
     *
     * @param int $submissionid ID of the submission
     * @return array list of recommendations
     */
    public static function get_recommendations_from_database(int $assignmentid,int $submissionid ): array {
        global $DB;

        // Query the database to get all recommendations for the given submission id.
        $records = $DB->get_records(self::TABLE_RECOMMENDATIONS, [
            'assignment_id' => $assignmentid,
            'submission_id' => $submissionid,
    ]);

        return $records;
    }

55
    /**
56
     * gets summary with all corresponding result entries
57
     *
58
59
     * @param int $assignmentid assignment id to search for
     * @param int $submissionid submission id to search for
Lückemeyer's avatar
Lückemeyer committed
60
     * @return DttResultSummary representing given submission
61
     */
62
    public static function getresultsummaryfromdatabase(
63
64
        int $assignmentid,
        int $submissionid
65
66
67
    ): DtaResultSummary {
        global $DB;

68
        // Fetch data from database.
69
        $summaryrecord = $DB->get_record(self::TABLE_SUMMARY, [
70
            "assignment_id" => $assignmentid,
71
            "submission_id" => $submissionid,
Lückemeyer's avatar
Lückemeyer committed
72
        ]);
73

74
        $resultsarray = $DB->get_records(self::TABLE_RESULT, [
75
            "assignment_id" => $assignmentid,
76
77
            "submission_id" => $submissionid,
        ]);
78

79
        // Create a summary instance.
80
        $summary = new DtaResultSummary();
81
        $summary->timestamp = $summaryrecord->timestamp;
82
83
84
        $summary->globalstacktrace = $summaryrecord->global_stacktrace;
        $summary->successfultestcompetencies = $summaryrecord->successful_competencies;
        $summary->overalltestcompetencies = $summaryrecord->tested_competencies;
85
        $summary->results = [];
86

87
88
        // Create result instances and add to array of summary instance.
        foreach ($resultsarray as $rr) {
89
            $result = new DtaResult();
90
91
            $result->packagename = $rr->package_name;
            $result->classname = $rr->class_name;
92
93
            $result->name = $rr->name;
            $result->state = $rr->state;
94
95
            $result->failuretype = $rr->failure_type;
            $result->failurereason = $rr->failure_reason;
96
            $result->stacktrace = $rr->stacktrace;
97
98
            $result->columnnumber = $rr->column_number;
            $result->linenumber = $rr->line_number;
99
100
101
102
103
104
105
            $result->position = $rr->position;

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

        return $summary;
    }
Kurzenberger's avatar
Kurzenberger committed
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
    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);
        }
    }
    
152
153
154
155
156

    /**
     * save given result summary and single results to database
     * under given assignment and submission id
     *
Lückemeyer's avatar
Lückemeyer committed
157
158
159
     * @param int $assignmentid assigment this is submission is linked to
     * @param int $submissionid submission of this result
     * @param DtaResultSummary $summary instance to persist
160
     */
161
    public static function storeresultsummarytodatabase(
162
163
        int $assignmentid,
        int $submissionid,
164
165
166
167
        DtaResultSummary $summary
    ): void {
        global $DB;

168
169
170
171
172
        // Prepare new database entries.
        $summaryrecord = new stdClass();
        $summaryrecord->assignment_id = $assignmentid;
        $summaryrecord->submission_id = $submissionid;
        $summaryrecord->timestamp = $summary->timestamp;
173
174
175
        $summaryrecord->global_stacktrace = $summary->globalstacktrace;
        $summaryrecord->successful_competencies = $summary->successfultestcompetencies;
        $summaryrecord->tested_competencies = $summary->overalltestcompetencies;
Kurzenberger's avatar
Kurzenberger committed
176
        
177
        // Prepare results to persist to array.
178
        $resultrecords = [];
179
        foreach ($summary->results as $r) {
180
            $record = new stdClass();
181
182
            $record->assignment_id = $assignmentid;
            $record->submission_id = $submissionid;
183
184
            $record->package_name = $r->packagename;
            $record->class_name = $r->classname;
185
186
            $record->name = $r->name;
            $record->state = $r->state;
187
188
            $record->failure_type = $r->failuretype;
            $record->failure_reason = $r->failurereason;
189
            $record->stacktrace = $r->stacktrace;
190
191
            $record->column_number = $r->columnnumber;
            $record->line_number = $r->linenumber;
192
            $record->position = $r->position;
193
            $resultrecords[] = $record;
194
195
        }

196
        // If results already exist, delete old values beforehand.
197
        $submission = $DB->get_record(self::TABLE_SUMMARY, [
198
            'assignment_id' => $assignmentid,
199
200
            'submission_id' => $submissionid,
        ]);
201
202

        if ($submission) {
203
            $DB->delete_records(self::TABLE_RESULT, [
204
                'assignment_id' => $assignmentid,
205
206
                'submission_id' => $submissionid,
            ]);
207

208
            $DB->delete_records(self::TABLE_SUMMARY, [
209
                'assignment_id' => $assignmentid,
210
211
                'submission_id' => $submissionid,
            ]);
212
213
        }

214
215
216
        // Create summary and single result entries.
        $DB->insert_record(self::TABLE_SUMMARY, $summaryrecord);
        foreach ($resultrecords as $rr) {
217
218
219
220
221
222
223
            $DB->insert_record(self::TABLE_RESULT, $rr);
        }
    }

    /**
     * cleans up database if plugin is uninstalled
     */
224
    public static function uninstallplugincleaup(): void {
225
226
227
228
        global $DB;

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

231
232
233
    }

}