locallib.php 13.2 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

defined('MOODLE_INTERNAL') || die();

19
// Import various entity and application logic files.
20
require_once($CFG->dirroot . '/mod/assign/submission/dta/models/DtaResult.php');
Lückemeyer's avatar
Lückemeyer committed
21
22
23
require_once($CFG->dirroot . '/mod/assign/submission/dta/classes/database.php');
require_once($CFG->dirroot . '/mod/assign/submission/dta/classes/backend.php');
require_once($CFG->dirroot . '/mod/assign/submission/dta/classes/view.php');
24
25
26
27
28
29
30
31
32

/**
 * library class for DTA submission plugin extending assign submission plugin base class
 *
 * @package assignsubmission_dta
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class assign_submission_dta extends assign_submission_plugin {

Lückemeyer's avatar
Lückemeyer committed
33
    /**
Lückemeyer's avatar
Lückemeyer committed
34
35
     * Broadly used in logic, parametrized for easier change.
     */
36
    const COMPONENT_NAME = "assignsubmission_dta";
Lückemeyer's avatar
Lückemeyer committed
37
    /**
Lückemeyer's avatar
Lückemeyer committed
38
39
     * Draft file area for dta tests to be uploaded by the teacher.
     */
40
    const ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST = "tests_draft_dta";
Lückemeyer's avatar
Lückemeyer committed
41
42
43
    /**
     * File area for dta tests to be uploaded by the teacher.
     */
44
    const ASSIGNSUBMISSION_DTA_FILEAREA_TEST = "tests_dta";
Lückemeyer's avatar
Lückemeyer committed
45
46
47
    /**
     * File area for dta submission assignment.
     */
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    const ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION = "submissions_dta";

    /**
     * get plugin name
     * @return string
     */
    public function get_name(): string {
        return get_string("pluginname", self::COMPONENT_NAME);
    }

    /**
     * Get default settings for assignment submission settings
     *
     * @param MoodleQuickForm $mform form to add elements to
     * @return void
     */
    public function get_settings(MoodleQuickForm $mform): void {
65
        // Add draft filemanager to form.
66
67
68
69
70
71
72
73
        $mform->addElement(
            "filemanager",
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
            get_string("submission_settings_label", self::COMPONENT_NAME),
            null,
            $this->get_file_options(true)
        );

74
        // Add help button to added filemanager.
75
        $mform->addHelpButton(
76
            // Form-unique element id to which to add button.
77
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
78
            // Key.
79
            "submission_settings_label",
80
            // Language file to use.
81
82
83
            self::COMPONENT_NAME
        );

84
        // Only show filemanager if plugin is enabled.
85
        $mform->hideIf(
86
            // Form-unique element id to hide.
87
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
88
            // Condition to check.
89
            self::COMPONENT_NAME . '_enabled',
90
            // State to match for hiding.
91
92
93
94
95
96
97
98
99
100
101
            'notchecked'
        );
    }

    /**
     * Allows the plugin to update the defaultvalues passed in to
     * the settings form (needed to set up draft areas for editor
     * and filemanager elements)
     * @param array $defaultvalues
     */
    public function data_preprocessing(&$defaultvalues): void {
102
103
        // Get id of draft area for file manager creation.
        $draftitemid = file_get_submitted_draft_itemid(self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST);
104

105
        // Prepare draft area with created draft filearea.
106
107
108
109
110
111
        file_prepare_draft_area(
            $draftitemid,
            $this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            0,
Lückemeyer's avatar
Lückemeyer committed
112
            ['subdirs' => 0]
113
114
115
116
117
118
119
120
121
122
123
124
125
        );

        $defaultvalues[self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST] = $draftitemid;
    }

    /**
     * Save settings of assignment submission settings
     *
     * @param stdClass $data
     * @return bool
     */
    public function save_settings(stdClass $data): bool {

126
        // If the assignment has no filemanager for our plugin just leave.
127
128
        $draftfilemanagerid = self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST;
        if (!isset($data->$draftfilemanagerid)) {
129
130
131
            return true;
        }

132
        // Store files from draft filearea to final one.
133
        file_save_draft_area_files(
134
            // Form-unique element id of draft filemanager from the edit.
135
            $data->$draftfilemanagerid,
136
            // Id of the assignment in edit.
137
138
139
140
141
142
            $this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            0
        );

143
        // Get files from proper filearea.
144
145
        $fs = get_file_storage();
        $files = $fs->get_area_files(
146
            // Id of the current assignment.
147
148
149
150
151
152
153
154
            $this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            0,
            'id',
            false
        );

155
        // Check if a file was uploaded.
156
157
158
159
160
        if (empty($files)) {
            \core\notification::error(get_string("no_testfile_warning", self::COMPONENT_NAME));
            return true;
        }

161
        // Get the file.
162
163
        $file = reset($files);

164
        // Send file to backend.
165
        return DtaBackendUtils::sendtestconfigtobackend($this->assignment, $file);
166
167
168
169
170
    }

    /**
     * Add elements to submission form
     *
Lückemeyer's avatar
Lückemeyer committed
171
172
173
174
175
     * @param mixed $submissionorgrade stdClass|null submission or grade to show in the form
     * @param MoodleQuickForm $mform form for adding elements
     * @param stdClass $data data for filling the elements
     * @param int $userid current user
     * @return bool form elements added
176
     */
177
    public function get_form_elements_for_user($submissionorgrade, MoodleQuickForm $mform, stdClass $data, $userid): bool {
178
        // Prepare submission filearea.
179
180
181
182
183
184
185
186
187
188
        $data = file_prepare_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submissionorgrade ? $submissionorgrade->id : 0
        );

189
        // Add filemanager to form.
190
191
        $mform->addElement(
            'filemanager',
192
            // Form-unique identifier.
193
            'tasks_filemanager',
194
            // Label to show next to the filemanager.
195
            get_string("submission_label", self::COMPONENT_NAME),
196
            // Attributes.
197
            null,
198
            // Options.
199
200
201
            $this->get_file_options(false)
        );

202
        // Add help button.
203
        $mform->addHelpButton(
204
            // Related form item.
205
            "tasks_filemanager",
206
            // Key.
207
            "submission_label",
208
            // Language file.
209
210
211
212
213
214
215
            self::COMPONENT_NAME
        );

        return true;
    }

    /**
Lückemeyer's avatar
Lückemeyer committed
216
     * Determines if a submission file area contains any files.
217
218
219
220
221
     * @param stdClass $submission submission to check
     * @return bool true if file count is zero
     */
    public function is_empty(stdClass $submission): bool {
        return $this->count_files($submission->id, self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION) == 0;
222
    }
223
224

    /**
Lückemeyer's avatar
Lückemeyer committed
225
     * Counts the number of files in a filearea
226
     *
227
228
     * @param int $submissionid submission id to check
     * @param string $areaid filearea id to count
Lückemeyer's avatar
Lückemeyer committed
229
     * @return int number of files submitted in the filearea
230
     */
231
    private function count_files(int $submissionid, $areaid) {
232
233
234
        $fs = get_file_storage();
        $files = $fs->get_area_files($this->assignment->get_context()->id,
            self::COMPONENT_NAME,
235
236
            $areaid,
            $submissionid,
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
            'id',
            false);

        return count($files);
    }

    /**
     * Save data to the database
     *
     * @param stdClass $submission
     * @param stdClass $data
     * @return bool
     */
    public function save(stdClass $submission, stdClass $data) {
        $data = file_postupdate_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id
        );

261
        // If submission is empty leave directly.
262
263
264
265
        if ($this->is_empty($submission)) {
            return true;
        }

266
        // Get submitted files.
267
268
        $fs = get_file_storage();
        $files = $fs->get_area_files(
269
            // Id of current assignment.
270
271
272
273
274
275
276
277
            $this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id,
            'id',
            false
        );

278
        // Check if a file is uploaded.
279
280
281
282
283
        if (empty($files)) {
            \core\notification::error(get_string("no_submissionfile_warning", self::COMPONENT_NAME));
            return true;
        }

284
        // Get the file.
285
        $file = reset($files);
286
287

        // Send file to backend.
288
        $response = DtaBackendUtils::send_submission_to_backend($this->assignment, $submission->id, $file);
289

290
        // With a null response, return an error.
291
292
293
294
        if (is_null($response)) {
            return false;
        }

295
        // Convert received json to valid class instances.
296
        $resultsummary = DtaResultSummary::decodejson($response);
297

298
        // Persist new results to database.
299
        DbUtils::storeresultsummarytodatabase($this->assignment->get_instance()->id, $submission->id, $resultsummary);
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

        return true;
    }

    /**
     * Display a short summary of the test results of the submission
     * This is diplayed as default view, with the option to expand
     * to the full detailed results.
     *
     * @param stdClass $submission to show
     * @param bool $showviewlink configuration variable to show expand option
     * @return string summary results html
     */
    public function view_summary(stdClass $submission, & $showviewlink) {
        $showviewlink = true;

Lückemeyer's avatar
Lückemeyer committed
316
        return view_submission_utils::generatesummaryhtml(
317
318
319
320
321
322
323
324
325
326
327
328
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    /**
     * Display detailed results
     *
     * @param stdClass $submission the submission the results are shown for.
     * @return string detailed results html
     */
    public function view(stdClass $submission) {
Lückemeyer's avatar
Lückemeyer committed
329
        return view_submission_utils::generatedetailhtml(
330
331
332
333
334
335
336
337
338
339
340
341
342
343
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    /**
     * generate array of allowed filetypes to upload.
     *
     * @param bool $settings switch to define if list for assignment settings
     *      or active submission should be returned
     *
     * @return array
     */
    private function get_file_options(bool $settings): array {
344
345
346
347
348
349
350
351
352
353
354
        $fileoptions = [
                'subdirs' => 0,
                "maxfiles" => 1,
                'accepted_types' => ($settings
                    ? [".txt"]
                    : [
                        ".txt",
                        ".zip",
                    ]),
                'return_types' => FILE_INTERNAL,
            ];
355
356
357
358
359
360
361
362
        return $fileoptions;
    }

    /**
     * Get file areas returns a list of areas this plugin stores files
     * @return array - An array of fileareas (keys) and descriptions (values)
     */
    public function get_file_areas() {
363
        return [
364
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION => get_string("dta_submissions_fa", self::COMPONENT_NAME),
365
366
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST => get_string("dta_tests_fa", self::COMPONENT_NAME),
        ];
367
368
369
370
371
372
373
374
375
376
    }

    /**
     * Produce a list of files suitable for export that represent this feedback or submission
     *
     * @param stdClass $submission The submission
     * @param stdClass $user The user record - unused
     * @return array - return an array of files indexed by filename
     */
    public function get_files(stdClass $submission, stdClass $user) {
377
        $result = [];
378
379
380
381
382
383
384
385
386
        $fs = get_file_storage();
        $files = $fs->get_area_files($this->assignment->get_context()->id,
            self::COMPONENT_NAME,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id,
            'timemodified',
            false);

        foreach ($files as $file) {
387
            // Do we return the full folder path or just the file name?
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
            if (isset($submission->exportfullpath) && $submission->exportfullpath == false) {
                $result[$file->get_filename()] = $file;
            } else {
                $result[$file->get_filepath().$file->get_filename()] = $file;
            }
        }
        return $result;
    }

    /**
     * The plugin is beeing uninstalled - cleanup
     *
     * @return bool
     */
    public function delete_instance() {
403
        DbUtils::uninstallplugincleanup();
404
405
406
407

        return true;
    }
}