locallib.php 14.7 KB
Newer Older
1
<?php
2
// This file is part of Moodle - http://moodle.org/.
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// 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
use assignsubmission_dta\dta_db_utils;
18
use assignsubmission_dta\dta_backend_utils;
19
use assignsubmission_dta\dta_view_submission_utils;
20
21
22
use assignsubmission_dta\models\dta_result;
use assignsubmission_dta\models\dta_result_summary;
use assignsubmission_dta\models\dta_recommendation;
23
24

/**
25
 * Library class for DTA submission plugin extending assign submission plugin base class.
26
 *
27
28
29
 * @package    assignsubmission_dta
 * @copyright  2023 Your Name or Organization
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
31
32
 */
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
37
    public const ASSIGNSUBMISSION_DTA_COMPONENT_NAME = 'assignsubmission_dta';

Lückemeyer's avatar
Lückemeyer committed
38
    /**
39
     * Draft file area for DTA tests to be uploaded by the teacher.
Lückemeyer's avatar
Lückemeyer committed
40
     */
41
42
    public const ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST = 'tests_draft_dta';

Lückemeyer's avatar
Lückemeyer committed
43
    /**
44
     * File area for DTA tests to be uploaded by the teacher.
Lückemeyer's avatar
Lückemeyer committed
45
     */
46
47
    public const ASSIGNSUBMISSION_DTA_FILEAREA_TEST = 'tests_dta';

Lückemeyer's avatar
Lückemeyer committed
48
    /**
49
     * File area for DTA submission assignment.
Lückemeyer's avatar
Lückemeyer committed
50
     */
51
    public const ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION = 'submissions_dta';
52
53

    /**
54
55
     * Get plugin name.
     *
56
57
     * @return string
     */
58
    public function get_name(): string {
59
        return get_string('pluginname', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME);
60
61
62
    }

    /**
63
     * Get default settings for assignment submission settings.
64
     *
65
     * @param MoodleQuickForm $mform Form to add elements to.
66
67
     * @return void
     */
68
    public function get_settings(MoodleQuickForm $mform): void {
69
        // Add draft filemanager to form.
70
        $mform->addElement(
71
            'filemanager',
72
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
73
74
75
76
            get_string(
                'submission_settings_label',
                self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME
            ),
77
78
79
80
            null,
            $this->get_file_options(true)
        );

81
        // Add help button to added filemanager.
82
        $mform->addHelpButton(
83
            // Form-unique element id to which to add button.
84
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST,
85
            // Key.
86
            'submission_settings_label',
87
            // Language file to use.
88
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME
89
90
        );

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

    /**
103
     * Allows the plugin to update the default values passed into
104
     * the settings form (needed to set up draft areas for editor
105
106
107
     * and filemanager elements).
     *
     * @param array $defaultvalues Default values to update.
108
109
     */
    public function data_preprocessing(&$defaultvalues): void {
110
        // Get id of draft area for file manager creation.
111
112
113
        $draftitemid = file_get_submitted_draft_itemid(
            self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST
        );
114

115
        // Prepare draft area with created draft filearea.
116
117
118
        file_prepare_draft_area(
            $draftitemid,
            $this->assignment->get_context()->id,
119
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
120
121
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            0,
Lückemeyer's avatar
Lückemeyer committed
122
            ['subdirs' => 0]
123
124
125
126
127
128
        );

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

    /**
129
     * Save settings of assignment submission settings.
130
     *
131
     * @param stdClass $data Form data.
132
133
     * @return bool
     */
134
    public function save_settings(stdClass $data): bool {
135
        // If the assignment has no filemanager for our plugin, just leave.
136
137
        $draftfilemanagerid = self::ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST;
        if (!isset($data->$draftfilemanagerid)) {
138
139
140
            return true;
        }

141
        // Store files from draft filearea to final one.
142
        file_save_draft_area_files(
143
            // Form-unique element id of draft filemanager from the edit.
144
            $data->$draftfilemanagerid,
145
            // Id of the assignment in edit.
146
            $this->assignment->get_context()->id,
147
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
148
149
150
151
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            0
        );

152
        // Get files from proper filearea.
153
154
        $fs = get_file_storage();
        $files = $fs->get_area_files(
155
            // Id of the current assignment.
156
            $this->assignment->get_context()->id,
157
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
158
159
160
161
162
163
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST,
            0,
            'id',
            false
        );

164
        // Check if a file was uploaded.
165
        if (empty($files)) {
166
167
168
            \core\notification::error(
                get_string('no_testfile_warning', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)
            );
169
170
171
            return true;
        }

172
        // Get the file.
173
174
        $file = reset($files);

175
        // Send file to backend.
176
177
178
179
        return dta_backend_utils::assignsubmission_dta_send_testconfig_to_backend(
            $this->assignment,
            $file
        );
180
181
182
    }

    /**
183
     * Add elements to submission form.
184
     *
185
186
187
188
189
     * @param stdClass|null $submissionorgrade 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 True if form elements added.
190
     */
191
192
193
194
195
196
    public function get_form_elements_for_user(
        $submissionorgrade,
        MoodleQuickForm $mform,
        stdClass $data,
        $userid
    ): bool {
197
        // Prepare submission filearea.
198
199
200
201
202
        $data = file_prepare_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
203
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
204
205
206
207
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submissionorgrade ? $submissionorgrade->id : 0
        );

208
        // Add filemanager to form.
209
210
        $mform->addElement(
            'filemanager',
211
            // Form-unique identifier.
212
            'tasks_filemanager',
213
            // Label to show next to the filemanager.
214
            get_string('submission_label', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME),
215
216
217
218
            null,
            $this->get_file_options(false)
        );

219
        // Add help button.
220
        $mform->addHelpButton(
221
222
            'tasks_filemanager',
            'submission_label',
223
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME
224
225
226
227
228
229
        );

        return true;
    }

    /**
Lückemeyer's avatar
Lückemeyer committed
230
     * Determines if a submission file area contains any files.
231
232
233
     *
     * @param stdClass $submission Submission to check.
     * @return bool True if file count is zero.
234
     */
235
236
    public function is_empty(stdClass $submission): bool {
        return ($this->count_files(
237
238
239
            $submission->id,
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION
        ) === 0);
240
    }
241
242

    /**
243
     * Counts the number of files in a filearea.
244
     *
245
246
247
     * @param int $submissionid Submission id to check.
     * @param string $areaid Filearea id to count.
     * @return int Number of files submitted in the filearea.
248
     */
249
    private function count_files(int $submissionid, $areaid): int {
250
        $fs = get_file_storage();
251
252
        $files = $fs->get_area_files(
            $this->assignment->get_context()->id,
253
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
254
255
            $areaid,
            $submissionid,
256
            'id',
257
258
            false
        );
259
260
261
262

        return count($files);
    }

263
    /**
264
     * Save data to the database.
265
     *
266
267
268
     * @param stdClass $submission Submission object.
     * @param stdClass $data Data from the form.
     * @return bool True if saved successfully.
269
     */
270
    public function save(stdClass $submission, stdClass $data): bool {
271
272
273
274
275
        $data = file_postupdate_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
276
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
277
278
279
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id
        );
280

281
        // If submission is empty, leave directly.
282
        if ($this->is_empty($submission)) {
283
284
            return true;
        }
285

286
287
288
289
        // Get submitted files.
        $fs = get_file_storage();
        $files = $fs->get_area_files(
            $this->assignment->get_context()->id,
290
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
291
292
293
294
295
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id,
            'id',
            false
        );
296

297
298
        // Check if a file is uploaded.
        if (empty($files)) {
299
300
301
            \core\notification::error(
                get_string('no_submissionfile_warning', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME)
            );
302
303
            return true;
        }
304

305
306
        // Get the file.
        $file = reset($files);
307

308
        // Send file to backend (split across lines to avoid exceeding length).
309
        $response = \assignsubmission_dta\dta_backend_utils::assignsubmission_dta_send_submission_to_backend(
310
311
312
313
                $this->assignment,
                $submission->id,
                $file
            );
314

315
316
317
318
        // With a null response, return an error.
        if (is_null($response)) {
            return false;
        }
319

320
        // Convert received JSON to valid class instances.
321
        $resultsummary = dta_result_summary::assignsubmission_dta_decode_json($response);
Kurzenberger's avatar
Kurzenberger committed
322

323
        // Decode recommendations from response.
324
        $recommendations = dta_recommendation::assignsubmission_dta_decode_json_recommendations($response);
Kurzenberger's avatar
Kurzenberger committed
325

326
327
        // Use Moodle debugging instead of error_log/print_r.
        debugging('Recommendations: ' . json_encode($recommendations), DEBUG_DEVELOPER);
Kurzenberger's avatar
Kurzenberger committed
328

329
330
331
332
333
334
        // Persist new results to database (split long lines).
        dta_db_utils::assignsubmission_dta_store_result_summary_to_database(
            $this->assignment->get_instance()->id,
            $submission->id,
            $resultsummary
        );
Kurzenberger's avatar
Kurzenberger committed
335

336
        // Store the array of recommendations in the database.
337
338
339
340
341
        dta_db_utils::assignsubmission_dta_store_recommendations_to_database(
            $this->assignment->get_instance()->id,
            $submission->id,
            $recommendations
        );
Kurzenberger's avatar
Kurzenberger committed
342

343
344
        return true;
    }
345
346

    /**
347
     * Display a short summary of the test results of the submission.
348
     *
349
350
351
     * @param stdClass $submission Submission to show.
     * @param bool $showviewlink Whether to show expand option.
     * @return string Summary results HTML.
352
     */
353
    public function view_summary(stdClass $submission, &$showviewlink): string {
354
        $showviewlink = true;
355
        return dta_view_submission_utils::assignsubmission_dta_generate_summary_html(
356
357
358
359
360
361
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    /**
362
     * Display detailed results.
363
     *
364
365
     * @param stdClass $submission The submission for which to show results.
     * @return string Detailed results HTML.
366
     */
367
    public function view(stdClass $submission): string {
368
        return dta_view_submission_utils::assignsubmission_dta_generate_detail_html(
369
370
371
372
373
374
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    /**
375
     * Generate array of allowed file types to upload.
376
     *
377
     * @param bool $settings Whether this is for assignment settings or active submission.
378
379
380
     * @return array
     */
    private function get_file_options(bool $settings): array {
381
        $fileoptions = [
382
383
384
385
386
387
388
389
390
391
392
393
            'subdirs' => 0,
            'maxfiles' => 1,
            'accepted_types' => (
                $settings
                ? ['.txt']
                : [
                    '.txt',
                    '.zip',
                ]
            ),
            'return_types' => FILE_INTERNAL,
        ];
394
395
396
397
        return $fileoptions;
    }

    /**
398
399
400
     * Get file areas returns a list of areas this plugin stores files.
     *
     * @return array An array of fileareas (keys) and descriptions (values).
401
     */
402
    public function get_file_areas(): array {
403
        return [
404
405
406
407
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION =>
                get_string('dta_submissions_fa', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME),
            self::ASSIGNSUBMISSION_DTA_FILEAREA_TEST =>
                get_string('dta_tests_fa', self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME),
408
        ];
409
410
411
    }

    /**
412
     * Produce a list of files suitable for export that represent this feedback or submission.
413
     *
414
415
416
     * @param stdClass $submission The submission object.
     * @param stdClass $user The user record (unused).
     * @return array An array of files indexed by filename.
417
     */
418
    public function get_files(stdClass $submission, stdClass $user): array {
419
        $result = [];
420
        $fs = get_file_storage();
421
422
        $files = $fs->get_area_files(
            $this->assignment->get_context()->id,
423
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
424
425
426
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id,
            'timemodified',
427
428
            false
        );
429

430
        foreach ($files as $fileobj) {
431
            // Do we return the full folder path or just the file name?
432
433
            if (isset($submission->exportfullpath) && $submission->exportfullpath === false) {
                $result[$fileobj->get_filename()] = $fileobj;
434
            } else {
435
                $result[$fileobj->get_filepath() . $fileobj->get_filename()] = $fileobj;
436
437
438
439
440
441
            }
        }
        return $result;
    }

    /**
442
     * The plugin is being uninstalled - cleanup.
443
444
445
     *
     * @return bool
     */
446
    public function delete_instance(): bool {
447
        dta_db_utils::assignsubmission_dta_uninstall_plugin_cleaup();
448
449
450
        return true;
    }
}