database.php 5.85 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
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
 */ 
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
35
    private const TABLE_RESULT = "assignsubmission_dta_result";

    /**
36
     * gets summary with all corresponding result entries
37
     *
38
39
     * @param int $assignmentid assignment id to search for
     * @param int $submissionid submission id to search for
40
41
     * @return DttResultSummary representing given submission
     */
42
    public static function getresultsummaryfromdatabase(
43
44
        int $assignmentid,
        int $submissionid
45
46
47
    ): DtaResultSummary {
        global $DB;

48
        // Fetch data from database.
49
        $summaryrecord = $DB->get_record(self::TABLE_SUMMARY, [
50
            "assignment_id" => $assignmentid,
51
            "submission_id" => $submissionid,
Lückemeyer's avatar
Lückemeyer committed
52
        ]);
53

54
        $resultsarray = $DB->get_records(self::TABLE_RESULT, [
55
            "assignment_id" => $assignmentid,
56
57
            "submission_id" => $submissionid,
        ]);
58

59
        // Create a summary instance.
60
        $summary = new DtaResultSummary();
61
        $summary->timestamp = $summaryrecord->timestamp;
62
63
64
        $summary->globalstacktrace = $summaryrecord->global_stacktrace;
        $summary->successfultestcompetencies = $summaryrecord->successful_competencies;
        $summary->overalltestcompetencies = $summaryrecord->tested_competencies;
65
        $summary->results = [];
66

67
68
        // Create result instances and add to array of summary instance.
        foreach ($resultsarray as $rr) {
69
            $result = new DtaResult();
70
71
            $result->packagename = $rr->package_name;
            $result->classname = $rr->class_name;
72
73
            $result->name = $rr->name;
            $result->state = $rr->state;
74
75
            $result->failuretype = $rr->failure_type;
            $result->failurereason = $rr->failure_reason;
76
            $result->stacktrace = $rr->stacktrace;
77
78
            $result->columnnumber = $rr->column_number;
            $result->linenumber = $rr->line_number;
79
80
81
82
83
84
85
86
87
88
89
90
            $result->position = $rr->position;

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

        return $summary;
    }

    /**
     * save given result summary and single results to database
     * under given assignment and submission id
     *
Lückemeyer's avatar
Lückemeyer committed
91
92
93
     * @param $assignmentid assigment this is submission is linked to
     * @param $submissionid submission of this result
     * @param $summary instance to persist
94
     */
95
    public static function storeresultsummarytodatabase(
96
97
        int $assignmentid,
        int $submissionid,
98
99
100
101
        DtaResultSummary $summary
    ): void {
        global $DB;

102
103
104
105
106
        // Prepare new database entries.
        $summaryrecord = new stdClass();
        $summaryrecord->assignment_id = $assignmentid;
        $summaryrecord->submission_id = $submissionid;
        $summaryrecord->timestamp = $summary->timestamp;
107
108
109
        $summaryrecord->global_stacktrace = $summary->globalstacktrace;
        $summaryrecord->successful_competencies = $summary->successfultestcompetencies;
        $summaryrecord->tested_competencies = $summary->overalltestcompetencies;
110

111
        // Prepare results to persist to array.
112
        $resultrecords = [];
113
        foreach ($summary->results as $r) {
114
            $record = new stdClass();
115
116
            $record->assignment_id = $assignmentid;
            $record->submission_id = $submissionid;
117
118
            $record->package_name = $r->packagename;
            $record->class_name = $r->classname;
119
120
            $record->name = $r->name;
            $record->state = $r->state;
121
122
            $record->failure_type = $r->failuretype;
            $record->failure_reason = $r->failurereason;
123
            $record->stacktrace = $r->stacktrace;
124
125
            $record->column_number = $r->columnnumber;
            $record->line_number = $r->linenumber;
126
            $record->position = $r->position;
127
            $resultrecords[] = $record;
128
129
        }

130
        // If results already exist, delete old values beforehand.
131
        $submission = $DB->get_record(self::TABLE_SUMMARY, [
132
            'assignment_id' => $assignmentid,
133
134
            'submission_id' => $submissionid,
        ]);
135
136

        if ($submission) {
137
            $DB->delete_records(self::TABLE_RESULT, [
138
                'assignment_id' => $assignmentid,
139
140
                'submission_id' => $submissionid,
            ]);
141

142
            $DB->delete_records(self::TABLE_SUMMARY, [
143
                'assignment_id' => $assignmentid,
144
145
                'submission_id' => $submissionid,
            ]);
146
147
        }

148
149
150
        // Create summary and single result entries.
        $DB->insert_record(self::TABLE_SUMMARY, $summaryrecord);
        foreach ($resultrecords as $rr) {
151
152
153
154
155
156
157
            $DB->insert_record(self::TABLE_RESULT, $rr);
        }
    }

    /**
     * cleans up database if plugin is uninstalled
     */
158
    public static function uninstallplugincleaup(): void {
159
160
161
162
163
164
165
        global $DB;

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

}