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