An error occurred while loading the file. Please try again.
-
Kurzenberger authored812cd760
<?php
// 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/>.
/**
* 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
*/
class DbUtils {
/**
* Summary database table name.
*/
private const TABLE_SUMMARY = "assignsubmission_dta_summary";
/**
* Result database table name.
*/
private const TABLE_RESULT = "assignsubmission_dta_result";
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;
}
/**
* gets summary with all corresponding result entries
*
* @param int $assignmentid assignment id to search for
* @param int $submissionid submission id to search for
* @return DttResultSummary representing given submission
*/
public static function getresultsummaryfromdatabase(
int $assignmentid,
int $submissionid
): DtaResultSummary {
global $DB;
// Fetch data from database.
$summaryrecord = $DB->get_record(self::TABLE_SUMMARY, [
"assignment_id" => $assignmentid,
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
"submission_id" => $submissionid,
]);
$resultsarray = $DB->get_records(self::TABLE_RESULT, [
"assignment_id" => $assignmentid,
"submission_id" => $submissionid,
]);
// Create a summary instance.
$summary = new DtaResultSummary();
$summary->timestamp = $summaryrecord->timestamp;
$summary->globalstacktrace = $summaryrecord->global_stacktrace;
$summary->successfultestcompetencies = $summaryrecord->successful_competencies;
$summary->overalltestcompetencies = $summaryrecord->tested_competencies;
$summary->results = [];
// Create result instances and add to array of summary instance.
foreach ($resultsarray as $rr) {
$result = new DtaResult();
$result->packagename = $rr->package_name;
$result->classname = $rr->class_name;
$result->name = $rr->name;
$result->state = $rr->state;
$result->failuretype = $rr->failure_type;
$result->failurereason = $rr->failure_reason;
$result->stacktrace = $rr->stacktrace;
$result->columnnumber = $rr->column_number;
$result->linenumber = $rr->line_number;
$result->position = $rr->position;
$summary->results[] = $result;
}
return $summary;
}
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,
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
]);
}
// 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);
}
}
/**
* save given result summary and single results to database
* under given assignment and submission id
*
* @param int $assignmentid assigment this is submission is linked to
* @param int $submissionid submission of this result
* @param DtaResultSummary $summary instance to persist
*/
public static function storeresultsummarytodatabase(
int $assignmentid,
int $submissionid,
DtaResultSummary $summary
): void {
global $DB;
// Prepare new database entries.
$summaryrecord = new stdClass();
$summaryrecord->assignment_id = $assignmentid;
$summaryrecord->submission_id = $submissionid;
$summaryrecord->timestamp = $summary->timestamp;
$summaryrecord->global_stacktrace = $summary->globalstacktrace;
$summaryrecord->successful_competencies = $summary->successfultestcompetencies;
$summaryrecord->tested_competencies = $summary->overalltestcompetencies;
// Prepare results to persist to array.
$resultrecords = [];
foreach ($summary->results as $r) {
$record = new stdClass();
$record->assignment_id = $assignmentid;
$record->submission_id = $submissionid;
$record->package_name = $r->packagename;
$record->class_name = $r->classname;
$record->name = $r->name;
$record->state = $r->state;
$record->failure_type = $r->failuretype;
$record->failure_reason = $r->failurereason;
$record->stacktrace = $r->stacktrace;
$record->column_number = $r->columnnumber;
$record->line_number = $r->linenumber;
$record->position = $r->position;
$resultrecords[] = $record;
}
// If results already exist, delete old values beforehand.
$submission = $DB->get_record(self::TABLE_SUMMARY, [
'assignment_id' => $assignmentid,
'submission_id' => $submissionid,
]);
if ($submission) {
$DB->delete_records(self::TABLE_RESULT, [
'assignment_id' => $assignmentid,
'submission_id' => $submissionid,
]);
$DB->delete_records(self::TABLE_SUMMARY, [
'assignment_id' => $assignmentid,
'submission_id' => $submissionid,
]);
}
// Create summary and single result entries.
$DB->insert_record(self::TABLE_SUMMARY, $summaryrecord);
foreach ($resultrecords as $rr) {
$DB->insert_record(self::TABLE_RESULT, $rr);
}
}
/**
* cleans up database if plugin is uninstalled
*/
public static function uninstallplugincleaup(): void {
global $DB;
$DB->delete_records(self::TABLE_RESULT, null);
$DB->delete_records(self::TABLE_SUMMARY, null);
$DB->delete_records(self::TABLE_RECOMMENDATIONS, null);
}
}