database.php 6.08 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

Lückemeyer's avatar
Lückemeyer committed
25
26
27
28
29
30
/**
 * 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
31
 */
32
33
class DbUtils {

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

    /**
44
     * gets summary with all corresponding result entries
45
     *
46
47
     * @param int $assignmentid assignment id to search for
     * @param int $submissionid submission id to search for
48
     * @return DtaResultSummary representing given submission
49
     */
50
    public static function getresultsummaryfromdatabase(
51
52
        int $assignmentid,
        int $submissionid
53
54
55
    ): DtaResultSummary {
        global $DB;

56
        // Fetch data from database.
57
        $summaryrecord = $DB->get_record(self::TABLE_SUMMARY, [
58
            "assignment_id" => $assignmentid,
59
            "submission_id" => $submissionid,
Lückemeyer's avatar
Lückemeyer committed
60
        ]);
61

62
        $resultsarray = $DB->get_records(self::TABLE_RESULT, [
63
            "assignment_id" => $assignmentid,
64
65
            "submission_id" => $submissionid,
        ]);
66

67
        // Create a summary instance.
68
        $summary = new DtaResultSummary();
69
        $summary->timestamp = $summaryrecord->timestamp;
70
71
72
        $summary->globalstacktrace = $summaryrecord->global_stacktrace;
        $summary->successfultestcompetencies = $summaryrecord->successful_competencies;
        $summary->overalltestcompetencies = $summaryrecord->tested_competencies;
73
        $summary->results = [];
74

75
76
        // Create result instances and add to array of summary instance.
        foreach ($resultsarray as $rr) {
77
            $result = new DtaResult();
78
79
            $result->packagename = $rr->package_name;
            $result->classname = $rr->class_name;
80
81
            $result->name = $rr->name;
            $result->state = $rr->state;
82
83
            $result->failuretype = $rr->failure_type;
            $result->failurereason = $rr->failure_reason;
84
            $result->stacktrace = $rr->stacktrace;
85
86
            $result->columnnumber = $rr->column_number;
            $result->linenumber = $rr->line_number;
87
88
89
90
91
92
93
94
95
96
97
98
            $result->position = $rr->position;

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

        return $summary;
    }

    /**
     * save given result summary and single results to database
     * under given assignment and submission id
     *
99
100
101
     * @param int $assignmentid assigment this is submission is linked to
     * @param int $submissionid submission of this result
     * @param DtaResultSummary $summary instance to persist
102
     */
103
    public static function storeresultsummarytodatabase(
104
105
        int $assignmentid,
        int $submissionid,
106
107
108
109
        DtaResultSummary $summary
    ): void {
        global $DB;

110
111
112
113
114
        // Prepare new database entries.
        $summaryrecord = new stdClass();
        $summaryrecord->assignment_id = $assignmentid;
        $summaryrecord->submission_id = $submissionid;
        $summaryrecord->timestamp = $summary->timestamp;
115
116
117
        $summaryrecord->global_stacktrace = $summary->globalstacktrace;
        $summaryrecord->successful_competencies = $summary->successfultestcompetencies;
        $summaryrecord->tested_competencies = $summary->overalltestcompetencies;
118

119
        // Prepare results to persist to array.
120
        $resultrecords = [];
121
        foreach ($summary->results as $r) {
122
            $record = new stdClass();
123
124
            $record->assignment_id = $assignmentid;
            $record->submission_id = $submissionid;
125
126
            $record->package_name = $r->packagename;
            $record->class_name = $r->classname;
127
128
            $record->name = $r->name;
            $record->state = $r->state;
129
130
            $record->failure_type = $r->failuretype;
            $record->failure_reason = $r->failurereason;
131
            $record->stacktrace = $r->stacktrace;
132
133
            $record->column_number = $r->columnnumber;
            $record->line_number = $r->linenumber;
134
            $record->position = $r->position;
135
            $resultrecords[] = $record;
136
137
        }

138
        // If results already exist, delete old values beforehand.
139
        $submission = $DB->get_record(self::TABLE_SUMMARY, [
140
            'assignment_id' => $assignmentid,
141
142
            'submission_id' => $submissionid,
        ]);
143
144

        if ($submission) {
145
            $DB->delete_records(self::TABLE_RESULT, [
146
                'assignment_id' => $assignmentid,
147
148
                'submission_id' => $submissionid,
            ]);
149

150
            $DB->delete_records(self::TABLE_SUMMARY, [
151
                'assignment_id' => $assignmentid,
152
153
                'submission_id' => $submissionid,
            ]);
154
155
        }

156
157
158
        // Create summary and single result entries.
        $DB->insert_record(self::TABLE_SUMMARY, $summaryrecord);
        foreach ($resultrecords as $rr) {
159
160
161
162
163
164
165
            $DB->insert_record(self::TABLE_RESULT, $rr);
        }
    }

    /**
     * cleans up database if plugin is uninstalled
     */
166
    public static function uninstallplugincleaup(): void {
167
168
169
170
171
172
173
        global $DB;

        $DB->delete_records(self::TABLE_RESULT, null);
        $DB->delete_records(self::TABLE_SUMMARY, null);
    }

}