locallib.php 14.3 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
use assignsubmission_dta\dta_db_utils;
20
use assignsubmission_dta\dta_backend_utils;
21
use assignsubmission_dta\dta_view_submission_utils;
22
23
24
use assignsubmission_dta\models\dta_result;
use assignsubmission_dta\models\dta_result_summary;
use assignsubmission_dta\models\dta_recommendation;
25
26

/**
27
 * Library class for DTA submission plugin extending assign submission plugin base class
28
29
30
31
32
33
 *
 * @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
34
    /**
Lückemeyer's avatar
Lückemeyer committed
35
36
     * Broadly used in logic, parametrized for easier change.
     */
37
    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
    const ASSIGNSUBMISSION_DTA_DRAFT_FILEAREA_TEST = "tests_draft_dta";
Lückemeyer's avatar
Lückemeyer committed
42
    /**
43
     * File area for DTA tests to be uploaded by the teacher.
Lückemeyer's avatar
Lückemeyer committed
44
     */
45
    const ASSIGNSUBMISSION_DTA_FILEAREA_TEST = "tests_dta";
Lückemeyer's avatar
Lückemeyer committed
46
    /**
47
     * File area for DTA submission assignment.
Lückemeyer's avatar
Lückemeyer committed
48
     */
49
50
51
    const ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION = "submissions_dta";

    /**
52
     * Get plugin name
53
54
     * @return string
     */
55
56
    public function assignsubmission_dta_get_name(): string {
        return get_string("pluginname", self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME);
57
58
59
60
61
62
63
64
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * Add elements to submission form
     *
Lückemeyer's avatar
Lückemeyer committed
172
173
174
175
176
     * @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
177
     */
178
    public function get_form_elements_for_user($submissionorgrade, MoodleQuickForm $mform, stdClass $data, $userid): bool {
179
        // Prepare submission filearea.
180
181
182
183
184
        $data = file_prepare_standard_filemanager(
            $data,
            'tasks',
            $this->get_file_options(false),
            $this->assignment->get_context(),
185
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
186
187
188
189
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submissionorgrade ? $submissionorgrade->id : 0
        );

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

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

        return true;
    }

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

    /**
Lückemeyer's avatar
Lückemeyer committed
226
     * Counts the number of files in a filearea
227
     *
228
229
     * @param int $submissionid submission id to check
     * @param string $areaid filearea id to count
Lückemeyer's avatar
Lückemeyer committed
230
     * @return int number of files submitted in the filearea
231
     */
232
    private function assignsubmission_dta_count_files(int $submissionid, $areaid) {
233
234
        $fs = get_file_storage();
        $files = $fs->get_area_files($this->assignment->get_context()->id,
235
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
236
237
            $areaid,
            $submissionid,
238
239
240
241
242
243
            'id',
            false);

        return count($files);
    }

244
245
246
247
248
249
250
251
252
253
254
255
256
    /**
     * 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(),
257
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
258
259
260
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id
        );
261

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

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

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

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

288
        // Send file to backend.
289
        $response = \assignsubmission_dta\dta_backend_utils::assignsubmission_dta_send_submission_to_backend($this->assignment, $submission->id, $file);
290

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

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

299
        // Decode recommendations from response.
300
        $recommendations = dta_recommendation::assignsubmission_dta_decode_json_recommendations($response);
Kurzenberger's avatar
Kurzenberger committed
301

302
        error_log(print_r($recommendations, true));
Kurzenberger's avatar
Kurzenberger committed
303

304
        // Persist new results to database.
305
        dta_db_utils::assignsubmission_dta_store_result_summary_to_database($this->assignment->get_instance()->id, $submission->id, $resultsummary);
Kurzenberger's avatar
Kurzenberger committed
306

307
        // Store the array of recommendations in the database.
308
        dta_db_utils::assignsubmission_dta_store_recommendations_to_database($this->assignment->get_instance()->id, $submission->id, $recommendations);
Kurzenberger's avatar
Kurzenberger committed
309

310
311
        return true;
    }
312
313
314

    /**
     * Display a short summary of the test results of the submission
315
     * This is displayed as default view, with the option to expand
316
317
318
319
320
321
322
323
324
     * 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;

325
        return dta_view_submission_utils::assignsubmission_dta_generate_summary_html(
326
327
328
329
330
331
332
333
334
335
336
337
            $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) {
338
        return dta_view_submission_utils::assignsubmission_dta_generate_detail_html(
339
340
341
342
343
344
            $this->assignment->get_instance()->id,
            $submission->id
        );
    }

    /**
345
     * Generate array of allowed file types to upload.
346
347
348
349
350
351
352
     *
     * @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 {
353
354
355
356
357
358
359
360
361
362
363
        $fileoptions = [
                'subdirs' => 0,
                "maxfiles" => 1,
                'accepted_types' => ($settings
                    ? [".txt"]
                    : [
                        ".txt",
                        ".zip",
                    ]),
                'return_types' => FILE_INTERNAL,
            ];
364
365
366
367
368
369
370
371
        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() {
372
        return [
373
374
            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),
375
        ];
376
377
378
379
380
381
382
383
384
385
    }

    /**
     * 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) {
386
        $result = [];
387
388
        $fs = get_file_storage();
        $files = $fs->get_area_files($this->assignment->get_context()->id,
389
            self::ASSIGNSUBMISSION_DTA_COMPONENT_NAME,
390
391
392
393
394
395
            self::ASSIGNSUBMISSION_DTA_FILEAREA_SUBMISSION,
            $submission->id,
            'timemodified',
            false);

        foreach ($files as $file) {
396
            // Do we return the full folder path or just the file name?
397
398
399
400
401
402
403
404
405
406
            if (isset($submission->exportfullpath) && $submission->exportfullpath == false) {
                $result[$file->get_filename()] = $file;
            } else {
                $result[$file->get_filepath().$file->get_filename()] = $file;
            }
        }
        return $result;
    }

    /**
407
     * The plugin is being uninstalled - cleanup
408
409
410
411
     *
     * @return bool
     */
    public function delete_instance() {
412
        dta_db_utils::assignsubmission_dta_uninstall_plugin_cleaup();
413
414
415
416

        return true;
    }
}