database.php 4.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
<?php

class DbUtils {

    // summary database table name
    private const TABLE_SUMMARY = "assignsubmission_dtt_summary";
    // result database table name
    private const TABLE_RESULT = "assignsubmission_dtt_result";

    /**
     * get's 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
    ): DttResultSummary {
        global $DB;

        // fetch data from database
        $summaryDbRecord = $DB->get_record(self::TABLE_SUMMARY, array(
            "assignment_id" => $assignmentId,
            "submission_id" => $submissionId
        ));

        $resultsDbArray = $DB->get_records(self::TABLE_RESULT, array(
            "assignment_id" => $assignmentId,
            "submission_id" => $submissionId
        ));

        // create summary instance
        $summary = new DttResultSummary();
        $summary->timestamp = $summaryDbRecord->timestamp;
        $summary->globalStacktrace = $summaryDbRecord->global_stacktrace;
        $summary->results = array();

        // create result instances and add to array of summary instance
        foreach($resultsDbArray as $rr) {
            $result = new DttResult();
            $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;
    }

    /**
     * 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 DttResultSummary instance to persist
     */
    public static function storeResultSummaryToDatabase(
        int $assignmentId,
        int $submissionId,
        DttResultSummary $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;

        // prepare results to persist to array
        $resultRecordArray = array();
        foreach($summary->results as $r) {
            $record = new stdClass();
            $record->assignment_id = $assignmentId;
            $record->submission_id = $submissionId;
            $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;
            $resultRecordArray[] = $record;
        }

        // if results exist yet, delete old values beforehand
        $submission = $DB->get_record(self::TABLE_SUMMARY, array(
            'assignment_id' => $assignmentId,
            'submission_id' => $submissionId
        ));

        if ($submission) {
            $DB->delete_records(self::TABLE_RESULT, array(
                'assignment_id' => $assignmentId,
                'submission_id' => $submissionId
            ));

            $DB->delete_records(self::TABLE_SUMMARY, array(
                'assignment_id' => $assignmentId,
                'submission_id' => $submissionId
            ));
        }

        // create summary and single result entries
        $DB->insert_record(self::TABLE_SUMMARY, $summaryRecord);
        foreach($resultRecordArray as $rr) {
            $DB->insert_record(self::TABLE_RESULT, $rr);
        }
    }

    /**
     * cleans-up database if plugin get's uninstalled
     */
    public static function uninstallPluginCleanUp(): void {
        global $DB;

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

}