Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This file contains the definition for the library class for onlinetext submission plugin
19
 *
20
 * This class provides all the functionality for the new assign module.
21
 *
22
 * @package assignsubmission_onlinetext
23
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
24
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
use core_external\external_single_structure;
28
use core_external\external_value;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
// File area for online text submission assignment.
32
define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
33
 
34
/**
35
 * library class for onlinetext submission plugin extending submission plugin base class
36
 *
37
 * @package assignsubmission_onlinetext
38
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class assign_submission_onlinetext extends assign_submission_plugin {
42
 
43
    /**
44
     * Get the name of the online text submission plugin
45
     * @return string
46
     */
47
    public function get_name() {
48
        return get_string('onlinetext', 'assignsubmission_onlinetext');
49
    }
50
 
51
 
52
    /**
53
     * Get onlinetext submission information from the database
54
     *
55
     * @param  int $submissionid
56
     * @return mixed
57
     */
58
    private function get_onlinetext_submission($submissionid) {
59
        global $DB;
60
 
61
        return $DB->get_record('assignsubmission_onlinetext', array('submission'=>$submissionid));
62
    }
63
 
64
    /**
65
     * Remove a submission.
66
     *
67
     * @param stdClass $submission The submission
68
     * @return boolean
69
     */
70
    public function remove(stdClass $submission) {
71
        global $DB;
72
 
73
        $submissionid = $submission ? $submission->id : 0;
74
        if ($submissionid) {
75
            $DB->delete_records('assignsubmission_onlinetext', array('submission' => $submissionid));
76
        }
77
        return true;
78
    }
79
 
80
    /**
81
     * Get the settings for onlinetext submission plugin
82
     *
83
     * @param MoodleQuickForm $mform The form to add elements to
84
     * @return void
85
     */
86
    public function get_settings(MoodleQuickForm $mform) {
87
        global $CFG, $COURSE;
88
 
89
        $defaultwordlimit = $this->get_config('wordlimit') == 0 ? '' : $this->get_config('wordlimit');
90
        $defaultwordlimitenabled = $this->get_config('wordlimitenabled');
91
 
92
        $options = array('size' => '6', 'maxlength' => '6');
93
        $name = get_string('wordlimit', 'assignsubmission_onlinetext');
94
 
95
        // Create a text box that can be enabled/disabled for onlinetext word limit.
96
        $wordlimitgrp = array();
97
        $wordlimitgrp[] = $mform->createElement('text', 'assignsubmission_onlinetext_wordlimit', '', $options);
98
        $wordlimitgrp[] = $mform->createElement('checkbox', 'assignsubmission_onlinetext_wordlimit_enabled',
99
                '', get_string('enable'));
100
        $mform->addGroup($wordlimitgrp, 'assignsubmission_onlinetext_wordlimit_group', $name, ' ', false);
101
        $mform->addHelpButton('assignsubmission_onlinetext_wordlimit_group',
102
                              'wordlimit',
103
                              'assignsubmission_onlinetext');
104
        $mform->disabledIf('assignsubmission_onlinetext_wordlimit',
105
                           'assignsubmission_onlinetext_wordlimit_enabled',
106
                           'notchecked');
107
        $mform->hideIf('assignsubmission_onlinetext_wordlimit',
108
                       'assignsubmission_onlinetext_enabled',
109
                       'notchecked');
110
 
111
        // Add numeric rule to text field.
112
        $wordlimitgrprules = array();
113
        $wordlimitgrprules['assignsubmission_onlinetext_wordlimit'][] = array(null, 'numeric', null, 'client');
114
        $mform->addGroupRule('assignsubmission_onlinetext_wordlimit_group', $wordlimitgrprules);
115
 
116
        // Rest of group setup.
117
        $mform->setDefault('assignsubmission_onlinetext_wordlimit', $defaultwordlimit);
118
        $mform->setDefault('assignsubmission_onlinetext_wordlimit_enabled', $defaultwordlimitenabled);
119
        $mform->setType('assignsubmission_onlinetext_wordlimit', PARAM_INT);
120
        $mform->hideIf('assignsubmission_onlinetext_wordlimit_group',
121
                       'assignsubmission_onlinetext_enabled',
122
                       'notchecked');
123
    }
124
 
125
    /**
126
     * Save the settings for onlinetext submission plugin
127
     *
128
     * @param stdClass $data
129
     * @return bool
130
     */
131
    public function save_settings(stdClass $data) {
132
        if (empty($data->assignsubmission_onlinetext_wordlimit) || empty($data->assignsubmission_onlinetext_wordlimit_enabled)) {
133
            $wordlimit = 0;
134
            $wordlimitenabled = 0;
135
        } else {
136
            $wordlimit = $data->assignsubmission_onlinetext_wordlimit;
137
            $wordlimitenabled = 1;
138
        }
139
 
140
        $this->set_config('wordlimit', $wordlimit);
141
        $this->set_config('wordlimitenabled', $wordlimitenabled);
142
 
143
        return true;
144
    }
145
 
146
    /**
147
     * Add form elements for settings
148
     *
149
     * @param mixed $submission can be null
150
     * @param MoodleQuickForm $mform
151
     * @param stdClass $data
152
     * @return true if elements were added to the form
153
     */
154
    public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
155
        $elements = array();
156
 
157
        $editoroptions = $this->get_edit_options();
158
        $submissionid = $submission ? $submission->id : 0;
159
 
160
        if (!isset($data->onlinetext)) {
161
            $data->onlinetext = '';
162
        }
163
        if (!isset($data->onlinetextformat)) {
164
            $data->onlinetextformat = editors_get_preferred_format();
165
        }
166
 
167
        if ($submission) {
168
            $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
169
            if ($onlinetextsubmission) {
170
                $data->onlinetext = $onlinetextsubmission->onlinetext;
171
                $data->onlinetextformat = $onlinetextsubmission->onlineformat;
172
            }
173
 
174
        }
175
 
176
        $data = file_prepare_standard_editor($data,
177
                                             'onlinetext',
178
                                             $editoroptions,
179
                                             $this->assignment->get_context(),
180
                                             'assignsubmission_onlinetext',
181
                                             ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
182
                                             $submissionid);
183
        $mform->addElement('editor', 'onlinetext_editor', $this->get_name(), null, $editoroptions);
184
 
185
        return true;
186
    }
187
 
188
    /**
189
     * Editor format options
190
     *
191
     * @return array
192
     */
193
    private function get_edit_options() {
194
        $editoroptions = array(
195
            'noclean' => false,
196
            'maxfiles' => EDITOR_UNLIMITED_FILES,
197
            'maxbytes' => $this->assignment->get_course()->maxbytes,
198
            'context' => $this->assignment->get_context(),
199
            'return_types' => (FILE_INTERNAL | FILE_EXTERNAL | FILE_CONTROLLED_LINK),
200
            'removeorphaneddrafts' => true // Whether or not to remove any draft files which aren't referenced in the text.
201
        );
202
        return $editoroptions;
203
    }
204
 
205
    /**
206
     * Save data to the database and trigger plagiarism plugin,
207
     * if enabled, to scan the uploaded content via events trigger
208
     *
209
     * @param stdClass $submission
210
     * @param stdClass $data
211
     * @return bool
212
     */
213
    public function save(stdClass $submission, stdClass $data) {
214
        global $USER, $DB;
215
 
216
        $editoroptions = $this->get_edit_options();
217
 
218
        $data = file_postupdate_standard_editor($data,
219
                                                'onlinetext',
220
                                                $editoroptions,
221
                                                $this->assignment->get_context(),
222
                                                'assignsubmission_onlinetext',
223
                                                ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
224
                                                $submission->id);
225
 
226
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
227
 
228
        $fs = get_file_storage();
229
 
230
        $files = $fs->get_area_files($this->assignment->get_context()->id,
231
                                     'assignsubmission_onlinetext',
232
                                     ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
233
                                     $submission->id,
234
                                     'id',
235
                                     false);
236
 
237
        // Check word count before submitting anything.
238
        $exceeded = $this->check_word_count(trim($data->onlinetext));
239
        if ($exceeded) {
240
            $this->set_error($exceeded);
241
            return false;
242
        }
243
 
244
        $params = array(
245
            'context' => context_module::instance($this->assignment->get_course_module()->id),
246
            'courseid' => $this->assignment->get_course()->id,
247
            'objectid' => $submission->id,
248
            'other' => array(
249
                'pathnamehashes' => array_keys($files),
250
                'content' => trim($data->onlinetext),
251
                'format' => $data->onlinetext_editor['format']
252
            )
253
        );
254
        if (!empty($submission->userid) && ($submission->userid != $USER->id)) {
255
            $params['relateduserid'] = $submission->userid;
256
        }
257
        if ($this->assignment->is_blind_marking()) {
258
            $params['anonymous'] = 1;
259
        }
260
        $event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
261
        $event->trigger();
262
 
263
        $groupname = null;
264
        $groupid = 0;
265
        // Get the group name as other fields are not transcribed in the logs and this information is important.
266
        if (empty($submission->userid) && !empty($submission->groupid)) {
267
            $groupname = $DB->get_field('groups', 'name', array('id' => $submission->groupid), MUST_EXIST);
268
            $groupid = $submission->groupid;
269
        } else {
270
            $params['relateduserid'] = $submission->userid;
271
        }
272
 
273
        $count = count_words($data->onlinetext);
274
 
275
        // Unset the objectid and other field from params for use in submission events.
276
        unset($params['objectid']);
277
        unset($params['other']);
278
        $params['other'] = array(
279
            'submissionid' => $submission->id,
280
            'submissionattempt' => $submission->attemptnumber,
281
            'submissionstatus' => $submission->status,
282
            'onlinetextwordcount' => $count,
283
            'groupid' => $groupid,
284
            'groupname' => $groupname
285
        );
286
 
287
        if ($onlinetextsubmission) {
288
 
289
            $onlinetextsubmission->onlinetext = $data->onlinetext;
290
            $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
291
            $params['objectid'] = $onlinetextsubmission->id;
292
            $updatestatus = $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
293
            $event = \assignsubmission_onlinetext\event\submission_updated::create($params);
294
            $event->set_assign($this->assignment);
295
            $event->trigger();
296
            return $updatestatus;
297
        } else {
298
 
299
            $onlinetextsubmission = new stdClass();
300
            $onlinetextsubmission->onlinetext = $data->onlinetext;
301
            $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
302
 
303
            $onlinetextsubmission->submission = $submission->id;
304
            $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
305
            $onlinetextsubmission->id = $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
306
            $params['objectid'] = $onlinetextsubmission->id;
307
            $event = \assignsubmission_onlinetext\event\submission_created::create($params);
308
            $event->set_assign($this->assignment);
309
            $event->trigger();
310
            return $onlinetextsubmission->id > 0;
311
        }
312
    }
313
 
314
    /**
315
     * Return a list of the text fields that can be imported/exported by this plugin
316
     *
317
     * @return array An array of field names and descriptions. (name=>description, ...)
318
     */
319
    public function get_editor_fields() {
320
        return array('onlinetext' => get_string('pluginname', 'assignsubmission_onlinetext'));
321
    }
322
 
323
    /**
324
     * Get the saved text content from the editor
325
     *
326
     * @param string $name
327
     * @param int $submissionid
328
     * @return string
329
     */
330
    public function get_editor_text($name, $submissionid) {
331
        if ($name == 'onlinetext') {
332
            $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
333
            if ($onlinetextsubmission) {
334
                return $onlinetextsubmission->onlinetext;
335
            }
336
        }
337
 
338
        return '';
339
    }
340
 
341
    /**
342
     * Get the content format for the editor
343
     *
344
     * @param string $name
345
     * @param int $submissionid
346
     * @return int
347
     */
348
    public function get_editor_format($name, $submissionid) {
349
        if ($name == 'onlinetext') {
350
            $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
351
            if ($onlinetextsubmission) {
352
                return $onlinetextsubmission->onlineformat;
353
            }
354
        }
355
 
356
        return 0;
357
    }
358
 
359
 
360
     /**
361
      * Display onlinetext word count in the submission status table
362
      *
363
      * @param stdClass $submission
364
      * @param bool $showviewlink - If the summary has been truncated set this to true
365
      * @return string
366
      */
367
    public function view_summary(stdClass $submission, & $showviewlink) {
368
        global $CFG;
369
 
370
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
371
        // Always show the view link.
372
        $showviewlink = true;
373
 
374
        if ($onlinetextsubmission) {
375
            // This contains the shortened version of the text plus an optional 'Export to portfolio' button.
376
            $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
377
                                                             $onlinetextsubmission->submission,
378
                                                             $this->get_type(),
379
                                                             'onlinetext',
380
                                                             'assignsubmission_onlinetext', true);
381
 
382
            // The actual submission text.
383
            $onlinetext = trim($onlinetextsubmission->onlinetext);
384
            // The shortened version of the submission text.
385
            $shorttext = shorten_text($onlinetext, 140);
386
 
387
            $plagiarismlinks = '';
388
 
389
            if (!empty($CFG->enableplagiarism)) {
390
                require_once($CFG->libdir . '/plagiarismlib.php');
391
 
392
                $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
393
                    'content' => $onlinetext,
394
                    'cmid' => $this->assignment->get_course_module()->id,
395
                    'course' => $this->assignment->get_course()->id,
396
                    'assignment' => $submission->assignment));
397
            }
398
            // We compare the actual text submission and the shortened version. If they are not equal, we show the word count.
399
            if ($onlinetext != $shorttext) {
400
                $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($onlinetext));
401
 
402
                return $plagiarismlinks . $wordcount . $text;
403
            } else {
404
                return $plagiarismlinks . $text;
405
            }
406
        }
407
        return '';
408
    }
409
 
1441 ariadna 410
 
411
    #[\Override]
412
    public function submission_summary_for_messages(stdClass $submission): array {
413
        global $PAGE;
414
 
415
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
416
        if (!$onlinetextsubmission || !$onlinetextsubmission->onlinetext) {
417
            return ['', ''];
418
        }
419
 
420
        $renderer = $PAGE->get_renderer('mod_assign');
421
        $templatecontext = ['wordcount' => count_words($onlinetextsubmission->onlinetext)];
422
        return [
423
            // Mustache strips off all trailing whitespace, but we want a newline at the end.
424
            $renderer->render_from_template(
425
               'assignsubmission_onlinetext/email_summary_text', $templatecontext) . "\n",
426
            $renderer->render_from_template(
427
               'assignsubmission_onlinetext/email_summary_html', $templatecontext),
428
        ];
429
    }
430
 
1 efrain 431
    /**
432
     * Produce a list of files suitable for export that represent this submission.
433
     *
434
     * @param stdClass $submission - For this is the submission data
435
     * @param stdClass $user - This is the user record for this submission
436
     * @return array - return an array of files indexed by filename
437
     */
438
    public function get_files(stdClass $submission, stdClass $user) {
439
        global $DB;
440
 
441
        $files = array();
442
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
443
 
444
        // Note that this check is the same logic as the result from the is_empty function but we do
445
        // not call it directly because we already have the submission record.
446
        if ($onlinetextsubmission && !html_is_blank($onlinetextsubmission->onlinetext)) {
447
            // Do not pass the text through format_text. The result may not be displayed in Moodle and
448
            // may be passed to external services such as document conversion or portfolios.
449
            $formattedtext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
450
            $head = '<head><meta charset="UTF-8"></head>';
451
            $submissioncontent = '<!DOCTYPE html><html>' . $head . '<body>'. $formattedtext . '</body></html>';
452
 
453
            $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
454
            $files[$filename] = array($submissioncontent);
455
 
456
            $fs = get_file_storage();
457
 
458
            $fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
459
                                           'assignsubmission_onlinetext',
460
                                           ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
461
                                           $submission->id,
462
                                           'timemodified',
463
                                           false);
464
 
465
            foreach ($fsfiles as $file) {
466
                $files[$file->get_filename()] = $file;
467
            }
468
        }
469
 
470
        return $files;
471
    }
472
 
473
    /**
474
     * Display the saved text content from the editor in the view table
475
     *
476
     * @param stdClass $submission
477
     * @return string
478
     */
479
    public function view(stdClass $submission) {
480
        global $CFG;
481
        $result = '';
482
        $plagiarismlinks = '';
483
 
484
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
485
 
486
        if ($onlinetextsubmission) {
487
 
488
            // Render for portfolio API.
489
            $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
490
                                                                $onlinetextsubmission->submission,
491
                                                                $this->get_type(),
492
                                                                'onlinetext',
493
                                                                'assignsubmission_onlinetext');
494
 
495
            if (!empty($CFG->enableplagiarism)) {
496
                require_once($CFG->libdir . '/plagiarismlib.php');
497
 
498
                $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
499
                    'content' => trim($onlinetextsubmission->onlinetext),
500
                    'cmid' => $this->assignment->get_course_module()->id,
501
                    'course' => $this->assignment->get_course()->id,
502
                    'assignment' => $submission->assignment));
503
            }
504
        }
505
 
506
        return $plagiarismlinks . $result;
507
    }
508
 
509
    /**
510
     * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
511
     *
512
     * @param string $type old assignment subtype
513
     * @param int $version old assignment version
514
     * @return bool True if upgrade is possible
515
     */
516
    public function can_upgrade($type, $version) {
517
        if ($type == 'online' && $version >= 2011112900) {
518
            return true;
519
        }
520
        return false;
521
    }
522
 
523
 
524
    /**
525
     * Upgrade the settings from the old assignment to the new plugin based one
526
     *
527
     * @param context $oldcontext - the database for the old assignment context
528
     * @param stdClass $oldassignment - the database for the old assignment instance
529
     * @param string $log record log events here
530
     * @return bool Was it a success?
531
     */
532
    public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
533
        // No settings to upgrade.
534
        return true;
535
    }
536
 
537
    /**
538
     * Upgrade the submission from the old assignment to the new one
539
     *
540
     * @param context $oldcontext - the database for the old assignment context
541
     * @param stdClass $oldassignment The data record for the old assignment
542
     * @param stdClass $oldsubmission The data record for the old submission
543
     * @param stdClass $submission The data record for the new submission
544
     * @param string $log Record upgrade messages in the log
545
     * @return bool true or false - false will trigger a rollback
546
     */
547
    public function upgrade(context $oldcontext,
548
                            stdClass $oldassignment,
549
                            stdClass $oldsubmission,
550
                            stdClass $submission,
551
                            & $log) {
552
        global $DB;
553
 
554
        $onlinetextsubmission = new stdClass();
555
        $onlinetextsubmission->onlinetext = $oldsubmission->data1;
556
        $onlinetextsubmission->onlineformat = $oldsubmission->data2;
557
 
558
        $onlinetextsubmission->submission = $submission->id;
559
        $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
560
 
561
        if ($onlinetextsubmission->onlinetext === null) {
562
            $onlinetextsubmission->onlinetext = '';
563
        }
564
 
565
        if ($onlinetextsubmission->onlineformat === null) {
566
            $onlinetextsubmission->onlineformat = editors_get_preferred_format();
567
        }
568
 
569
        if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
570
            $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
571
            return false;
572
        }
573
 
574
        // Now copy the area files.
575
        $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
576
                                                        'mod_assignment',
577
                                                        'submission',
578
                                                        $oldsubmission->id,
579
                                                        $this->assignment->get_context()->id,
580
                                                        'assignsubmission_onlinetext',
581
                                                        ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
582
                                                        $submission->id);
583
        return true;
584
    }
585
 
586
    /**
587
     * The assignment has been deleted - cleanup
588
     *
589
     * @return bool
590
     */
591
    public function delete_instance() {
592
        global $DB;
593
        $DB->delete_records('assignsubmission_onlinetext',
594
                            array('assignment'=>$this->assignment->get_instance()->id));
595
 
596
        return true;
597
    }
598
 
599
    /**
600
     * No text is set for this plugin
601
     *
602
     * @param stdClass $submission
603
     * @return bool
604
     */
605
    public function is_empty(stdClass $submission) {
606
        $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
607
        $wordcount = 0;
608
        $hasinsertedresources = false;
609
 
610
        if (isset($onlinetextsubmission->onlinetext)) {
611
            $wordcount = count_words(trim($onlinetextsubmission->onlinetext));
612
            // Check if the online text submission contains video, audio or image elements
613
            // that can be ignored and stripped by count_words().
614
            $hasinsertedresources = preg_match('/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>(.*?))/',
615
                    trim($onlinetextsubmission->onlinetext));
616
        }
617
 
618
        return $wordcount == 0 && !$hasinsertedresources;
619
    }
620
 
621
    /**
622
     * Determine if a submission is empty
623
     *
624
     * This is distinct from is_empty in that it is intended to be used to
625
     * determine if a submission made before saving is empty.
626
     *
627
     * @param stdClass $data The submission data
628
     * @return bool
629
     */
630
    public function submission_is_empty(stdClass $data) {
631
        if (!isset($data->onlinetext_editor)) {
632
            return true;
633
        }
634
        $wordcount = 0;
635
        $hasinsertedresources = false;
636
 
637
        if (isset($data->onlinetext_editor['text'])) {
638
            $wordcount = count_words(trim((string)$data->onlinetext_editor['text']));
639
            // Check if the online text submission contains video, audio or image elements
640
            // that can be ignored and stripped by count_words().
641
            $hasinsertedresources = preg_match('/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>(.*?))/',
642
                    trim((string)$data->onlinetext_editor['text']));
643
        }
644
 
645
        return $wordcount == 0 && !$hasinsertedresources;
646
    }
647
 
648
    /**
649
     * Get file areas returns a list of areas this plugin stores files
650
     * @return array - An array of fileareas (keys) and descriptions (values)
651
     */
652
    public function get_file_areas() {
653
        return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
654
    }
655
 
656
    /**
657
     * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
658
     * on the last submission.
659
     * @param stdClass $sourcesubmission
660
     * @param stdClass $destsubmission
661
     */
662
    public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
663
        global $DB;
664
 
665
        // Copy the files across (attached via the text editor).
666
        $contextid = $this->assignment->get_context()->id;
667
        $fs = get_file_storage();
668
        $files = $fs->get_area_files($contextid, 'assignsubmission_onlinetext',
669
                                     ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sourcesubmission->id, 'id', false);
670
        foreach ($files as $file) {
671
            $fieldupdates = array('itemid' => $destsubmission->id);
672
            $fs->create_file_from_storedfile($fieldupdates, $file);
673
        }
674
 
675
        // Copy the assignsubmission_onlinetext record.
676
        $onlinetextsubmission = $this->get_onlinetext_submission($sourcesubmission->id);
677
        if ($onlinetextsubmission) {
678
            unset($onlinetextsubmission->id);
679
            $onlinetextsubmission->submission = $destsubmission->id;
680
            $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
681
        }
682
        return true;
683
    }
684
 
685
    /**
686
     * Return a description of external params suitable for uploading an onlinetext submission from a webservice.
687
     *
688
     * @return \core_external\external_description|null
689
     */
690
    public function get_external_parameters() {
691
        $editorparams = array('text' => new external_value(PARAM_RAW, 'The text for this submission.'),
692
                              'format' => new external_value(PARAM_INT, 'The format for this submission'),
693
                              'itemid' => new external_value(PARAM_INT, 'The draft area id for files attached to the submission'));
694
        $editorstructure = new external_single_structure($editorparams, 'Editor structure', VALUE_OPTIONAL);
695
        return array('onlinetext_editor' => $editorstructure);
696
    }
697
 
698
    /**
699
     * Compare word count of onlinetext submission to word limit, and return result.
700
     *
701
     * @param string $submissiontext Onlinetext submission text from editor
702
     * @return string Error message if limit is enabled and exceeded, otherwise null
703
     */
704
    public function check_word_count($submissiontext) {
705
        global $OUTPUT;
706
 
707
        $wordlimitenabled = $this->get_config('wordlimitenabled');
708
        $wordlimit = $this->get_config('wordlimit');
709
 
710
        if ($wordlimitenabled == 0) {
711
            return null;
712
        }
713
 
714
        // Count words and compare to limit.
715
        $wordcount = count_words($submissiontext);
716
        if ($wordcount <= $wordlimit) {
717
            return null;
718
        } else {
719
            $errormsg = get_string('wordlimitexceeded', 'assignsubmission_onlinetext',
720
                    array('limit' => $wordlimit, 'count' => $wordcount));
721
            return $OUTPUT->error_text($errormsg);
722
        }
723
    }
724
 
725
    /**
726
     * Return the plugin configs for external functions.
727
     *
728
     * @return array the list of settings
729
     * @since Moodle 3.2
730
     */
731
    public function get_config_for_external() {
732
        return (array) $this->get_config();
733
    }
734
}