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