database.php 5.66 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
17
18

class DbUtils {

19
    // Summary database table name.
20
    private const TABLE_SUMMARY = "assignsubmission_dta_summary";
21
    // Result database table name.
22
23
24
    private const TABLE_RESULT = "assignsubmission_dta_result";

    /**
25
     * gets summary with all corresponding result entries
26
     *
27
28
     * @param int $assignmentid assignment id to search for
     * @param int $submissionid submission id to search for
29
30
     * @return DttResultSummary representing given submission
     */
31
    public static function getresultsummaryfromdatabase(
32
33
        int $assignmentid,
        int $submissionid
34
35
36
    ): DtaResultSummary {
        global $DB;

37
38
        // Fetch data from database.
        $summaryrecord = $DB->get_record(self::TABLE_SUMMARY, array(
39
40
            "assignment_id" => $assignmentid,
            "submission_id" => $submissionid
41
42
        ));

43
        $resultsarray = $DB->get_records(self::TABLE_RESULT, array(
44
45
            "assignment_id" => $assignmentid,
            "submission_id" => $submissionid
46
47
        ));

48
        // Create a summary instance.
49
        $summary = new DtaResultSummary();
50
        $summary->timestamp = $summaryrecord->timestamp;
51
52
53
        $summary->globalstacktrace = $summaryrecord->global_stacktrace;
        $summary->successfultestcompetencies = $summaryrecord->successful_competencies;
        $summary->overalltestcompetencies = $summaryrecord->tested_competencies;
54
55
        $summary->results = array();

56
57
        // Create result instances and add to array of summary instance.
        foreach ($resultsarray as $rr) {
58
            $result = new DtaResult();
59
60
            $result->packagename = $rr->package_name;
            $result->classname = $rr->class_name;
61
62
            $result->name = $rr->name;
            $result->state = $rr->state;
63
64
            $result->failuretype = $rr->failure_type;
            $result->failurereason = $rr->failure_reason;
65
            $result->stacktrace = $rr->stacktrace;
66
67
            $result->columnnumber = $rr->column_number;
            $result->linenumber = $rr->line_number;
68
69
70
71
72
73
74
75
76
77
78
79
            $result->position = $rr->position;

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

        return $summary;
    }

    /**
     * save given result summary and single results to database
     * under given assignment and submission id
     *
80
81
     * @param int assignmentid assigment this is submission is linked to
     * @param int submissionid submission of this result
82
83
     * @param DttResultSummary instance to persist
     */
84
    public static function storeresultsummarytodatabase(
85
86
        int $assignmentid,
        int $submissionid,
87
88
89
90
        DtaResultSummary $summary
    ): void {
        global $DB;

91
92
93
94
95
        // Prepare new database entries.
        $summaryrecord = new stdClass();
        $summaryrecord->assignment_id = $assignmentid;
        $summaryrecord->submission_id = $submissionid;
        $summaryrecord->timestamp = $summary->timestamp;
96
97
98
99
        $summaryrecord->global_stacktrace = $summary->globalstacktrace;
        $summaryrecord->successful_competencies = $summary->successfultestcompetencies;
        $summaryrecord->tested_competencies = $summary->overalltestcompetencies;
		
100
101
102
        // Prepare results to persist to array.
        $resultrecords = array();
        foreach ($summary->results as $r) {
103
            $record = new stdClass();
104
105
            $record->assignment_id = $assignmentid;
            $record->submission_id = $submissionid;
106
107
            $record->package_name = $r->packagename;
            $record->class_name = $r->classname;
108
109
            $record->name = $r->name;
            $record->state = $r->state;
110
111
            $record->failure_type = $r->failuretype;
            $record->failure_reason = $r->failurereason;
112
            $record->stacktrace = $r->stacktrace;
113
114
            $record->column_number = $r->columnnumber;
            $record->line_number = $r->linenumber;
115
            $record->position = $r->position;
116
            $resultrecords[] = $record;
117
118
        }

119
        // If results already exist, delete old values beforehand.
120
        $submission = $DB->get_record(self::TABLE_SUMMARY, array(
121
122
            'assignment_id' => $assignmentid,
            'submission_id' => $submissionid
123
124
125
126
        ));

        if ($submission) {
            $DB->delete_records(self::TABLE_RESULT, array(
127
128
                'assignment_id' => $assignmentid,
                'submission_id' => $submissionid
129
130
131
            ));

            $DB->delete_records(self::TABLE_SUMMARY, array(
132
133
                'assignment_id' => $assignmentid,
                'submission_id' => $submissionid
134
135
136
            ));
        }

137
138
139
        // Create summary and single result entries.
        $DB->insert_record(self::TABLE_SUMMARY, $summaryrecord);
        foreach ($resultrecords as $rr) {
140
141
142
143
144
145
146
            $DB->insert_record(self::TABLE_RESULT, $rr);
        }
    }

    /**
     * cleans up database if plugin is uninstalled
     */
147
    public static function uninstallplugincleaup(): void {
148
149
150
151
152
153
154
        global $DB;

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

}