MDL-35373 mod_assign Add form elements for assignsubmission_onlinetext word limit
[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() {
172 $editoroptions = array(
173 'noclean' => false,
174 'maxfiles' => EDITOR_UNLIMITED_FILES,
175 'maxbytes' => $this->assignment->get_course()->maxbytes,
c469b396
MG
176 'context' => $this->assignment->get_context(),
177 'return_types' => FILE_INTERNAL | FILE_EXTERNAL
bbd0e548
DW
178 );
179 return $editoroptions;
180 }
181
e5403f8c
DW
182 /**
183 * Save data to the database and trigger plagiarism plugin,
184 * if enabled, to scan the uploaded content via events trigger
185 *
186 * @param stdClass $submission
187 * @param stdClass $data
188 * @return bool
189 */
190 public function save(stdClass $submission, stdClass $data) {
48e5aacb 191 global $USER, $DB;
bbd0e548
DW
192
193 $editoroptions = $this->get_edit_options();
194
e5403f8c
DW
195 $data = file_postupdate_standard_editor($data,
196 'onlinetext',
197 $editoroptions,
198 $this->assignment->get_context(),
199 'assignsubmission_onlinetext',
200 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
201 $submission->id);
bbd0e548
DW
202
203 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
48e5aacb
KG
204
205 $fs = get_file_storage();
e5403f8c
DW
206
207 $files = $fs->get_area_files($this->assignment->get_context()->id,
208 'assignsubmission_onlinetext',
209 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
210 $submission->id,
211 'id',
212 false);
213
7dceb500
FM
214 $params = array(
215 'context' => context_module::instance($this->assignment->get_course_module()->id),
d7f05ad4 216 'courseid' => $this->assignment->get_course()->id,
7dceb500
FM
217 'objectid' => $submission->id,
218 'other' => array(
219 'pathnamehashes' => array_keys($files),
fb59d535
AG
220 'content' => trim($data->onlinetext),
221 'format' => $data->onlinetext_editor['format']
7dceb500
FM
222 )
223 );
224 $event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
225 $event->trigger();
48e5aacb 226
d7f05ad4
AG
227 $groupname = null;
228 $groupid = 0;
229 // Get the group name as other fields are not transcribed in the logs and this information is important.
230 if (empty($submission->userid) && !empty($submission->groupid)) {
231 $groupname = $DB->get_field('groups', 'name', array('id' => $submission->groupid), '*', MUST_EXIST);
232 $groupid = $submission->groupid;
233 } else {
234 $params['relateduserid'] = $submission->userid;
235 }
236
237 $count = count_words($data->onlinetext);
238
239 // Unset the objectid and other field from params for use in submission events.
240 unset($params['objectid']);
241 unset($params['other']);
242 $params['other'] = array(
243 'submissionid' => $submission->id,
244 'submissionattempt' => $submission->attemptnumber,
245 'submissionstatus' => $submission->status,
246 'onlinetextwordcount' => $count,
247 'groupid' => $groupid,
248 'groupname' => $groupname
249 );
250
bbd0e548
DW
251 if ($onlinetextsubmission) {
252
253 $onlinetextsubmission->onlinetext = $data->onlinetext;
254 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
d7f05ad4
AG
255 $params['objectid'] = $onlinetextsubmission->id;
256 $updatestatus = $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
257 $event = \assignsubmission_onlinetext\event\submission_updated::create($params);
258 $event->trigger();
259 return $updatestatus;
bbd0e548
DW
260 } else {
261
262 $onlinetextsubmission = new stdClass();
263 $onlinetextsubmission->onlinetext = $data->onlinetext;
264 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
265
266 $onlinetextsubmission->submission = $submission->id;
267 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
d7f05ad4
AG
268 $onlinetextsubmission->id = $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
269 $params['objectid'] = $onlinetextsubmission->id;
270 $event = \assignsubmission_onlinetext\event\submission_created::create($params);
271 $event->trigger();
272 return $onlinetextsubmission->id > 0;
bbd0e548 273 }
bbd0e548
DW
274 }
275
df47b77f
DW
276 /**
277 * Return a list of the text fields that can be imported/exported by this plugin
278 *
279 * @return array An array of field names and descriptions. (name=>description, ...)
280 */
281 public function get_editor_fields() {
282 return array('onlinetext' => get_string('pluginname', 'assignsubmission_comments'));
283 }
284
bbd0e548
DW
285 /**
286 * Get the saved text content from the editor
287 *
288 * @param string $name
289 * @param int $submissionid
290 * @return string
291 */
292 public function get_editor_text($name, $submissionid) {
293 if ($name == 'onlinetext') {
294 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
295 if ($onlinetextsubmission) {
296 return $onlinetextsubmission->onlinetext;
297 }
298 }
299
300 return '';
301 }
302
303 /**
304 * Get the content format for the editor
305 *
306 * @param string $name
307 * @param int $submissionid
308 * @return int
309 */
310 public function get_editor_format($name, $submissionid) {
311 if ($name == 'onlinetext') {
312 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
313 if ($onlinetextsubmission) {
314 return $onlinetextsubmission->onlineformat;
315 }
316 }
317
e5403f8c 318 return 0;
bbd0e548
DW
319 }
320
321
322 /**
323 * Display onlinetext word count in the submission status table
324 *
325 * @param stdClass $submission
326 * @param bool $showviewlink - If the summary has been truncated set this to true
327 * @return string
328 */
7ee1548a 329 public function view_summary(stdClass $submission, & $showviewlink) {
48e5aacb 330 global $CFG;
bbd0e548
DW
331
332 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
e5403f8c 333 // Always show the view link.
bbd0e548
DW
334 $showviewlink = true;
335
336 if ($onlinetextsubmission) {
e4288042
DW
337 $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
338 $onlinetextsubmission->submission,
339 $this->get_type(),
340 'onlinetext',
341 'assignsubmission_onlinetext');
342
bbd0e548 343 $shorttext = shorten_text($text, 140);
48e5aacb 344 $plagiarismlinks = '';
e5403f8c 345
48e5aacb
KG
346 if (!empty($CFG->enableplagiarism)) {
347 require_once($CFG->libdir . '/plagiarismlib.php');
e5403f8c 348
48e5aacb 349 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
e5403f8c 350 'content' => trim($text),
48e5aacb
KG
351 'cmid' => $this->assignment->get_course_module()->id,
352 'course' => $this->assignment->get_course()->id,
353 'assignment' => $submission->assignment));
354 }
bbd0e548 355 if ($text != $shorttext) {
e5403f8c
DW
356 $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
357
358 return $shorttext . $plagiarismlinks . $wordcount;
bbd0e548 359 } else {
48e5aacb 360 return $shorttext . $plagiarismlinks;
bbd0e548
DW
361 }
362 }
363 return '';
364 }
365
366 /**
e5403f8c 367 * Produce a list of files suitable for export that represent this submission.
bbd0e548
DW
368 *
369 * @param stdClass $submission - For this is the submission data
2406815b 370 * @param stdClass $user - This is the user record for this submission
bbd0e548
DW
371 * @return array - return an array of files indexed by filename
372 */
2406815b 373 public function get_files(stdClass $submission, stdClass $user) {
bbd0e548 374 global $DB;
e5403f8c 375
bbd0e548
DW
376 $files = array();
377 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
e5403f8c 378
bbd0e548 379 if ($onlinetextsubmission) {
2406815b 380 $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
e5403f8c
DW
381 $formattedtext = format_text($finaltext,
382 $onlinetextsubmission->onlineformat,
383 array('context'=>$this->assignment->get_context()));
384 $submissioncontent = '<html><body>'. $formattedtext . '</body></html>';
385
386 $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
387 $files[$filename] = array($submissioncontent);
bbd0e548
DW
388
389 $fs = get_file_storage();
390
e5403f8c
DW
391 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
392 'assignsubmission_onlinetext',
393 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
394 $submission->id,
395 'timemodified',
396 false);
bbd0e548
DW
397
398 foreach ($fsfiles as $file) {
399 $files[$file->get_filename()] = $file;
400 }
401 }
402
403 return $files;
404 }
405
406 /**
407 * Display the saved text content from the editor in the view table
408 *
409 * @param stdClass $submission
410 * @return string
411 */
412 public function view(stdClass $submission) {
413 $result = '';
414
415 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
416
bbd0e548
DW
417 if ($onlinetextsubmission) {
418
e5403f8c
DW
419 // Render for portfolio API.
420 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
421 $onlinetextsubmission->submission,
422 $this->get_type(),
423 'onlinetext',
424 'assignsubmission_onlinetext');
bbd0e548
DW
425
426 }
427
428 return $result;
429 }
430
e5403f8c 431 /**
bbd0e548
DW
432 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
433 *
434 * @param string $type old assignment subtype
435 * @param int $version old assignment version
436 * @return bool True if upgrade is possible
437 */
438 public function can_upgrade($type, $version) {
439 if ($type == 'online' && $version >= 2011112900) {
440 return true;
441 }
442 return false;
443 }
444
445
446 /**
447 * Upgrade the settings from the old assignment to the new plugin based one
448 *
449 * @param context $oldcontext - the database for the old assignment context
450 * @param stdClass $oldassignment - the database for the old assignment instance
451 * @param string $log record log events here
452 * @return bool Was it a success?
453 */
454 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
e5403f8c 455 // No settings to upgrade.
bbd0e548
DW
456 return true;
457 }
458
459 /**
460 * Upgrade the submission from the old assignment to the new one
461 *
462 * @param context $oldcontext - the database for the old assignment context
463 * @param stdClass $oldassignment The data record for the old assignment
464 * @param stdClass $oldsubmission The data record for the old submission
465 * @param stdClass $submission The data record for the new submission
466 * @param string $log Record upgrade messages in the log
467 * @return bool true or false - false will trigger a rollback
468 */
e5403f8c
DW
469 public function upgrade(context $oldcontext,
470 stdClass $oldassignment,
471 stdClass $oldsubmission,
472 stdClass $submission,
473 & $log) {
bbd0e548
DW
474 global $DB;
475
476 $onlinetextsubmission = new stdClass();
477 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
478 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
479
480 $onlinetextsubmission->submission = $submission->id;
481 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
d82bb44d
DW
482
483 if ($onlinetextsubmission->onlinetext === null) {
484 $onlinetextsubmission->onlinetext = '';
485 }
486
487 if ($onlinetextsubmission->onlineformat === null) {
488 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
489 }
490
bbd0e548
DW
491 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
492 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
493 return false;
494 }
495
e5403f8c 496 // Now copy the area files.
bbd0e548
DW
497 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
498 'mod_assignment',
499 'submission',
500 $oldsubmission->id,
bbd0e548
DW
501 $this->assignment->get_context()->id,
502 'assignsubmission_onlinetext',
8ab7bb9f 503 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
bbd0e548
DW
504 $submission->id);
505 return true;
506 }
507
508 /**
509 * Formatting for log info
510 *
511 * @param stdClass $submission The new submission
512 * @return string
513 */
514 public function format_for_log(stdClass $submission) {
e5403f8c 515 // Format the info for each submission plugin (will be logged).
bbd0e548
DW
516 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
517 $onlinetextloginfo = '';
e5403f8c
DW
518 $onlinetextloginfo .= get_string('numwordsforlog',
519 'assignsubmission_onlinetext',
fb59d535 520 count_words($onlinetextsubmission->onlinetext));
bbd0e548
DW
521
522 return $onlinetextloginfo;
523 }
524
525 /**
526 * The assignment has been deleted - cleanup
527 *
528 * @return bool
529 */
530 public function delete_instance() {
531 global $DB;
e5403f8c
DW
532 $DB->delete_records('assignsubmission_onlinetext',
533 array('assignment'=>$this->assignment->get_instance()->id));
bbd0e548
DW
534
535 return true;
536 }
537
538 /**
539 * No text is set for this plugin
540 *
541 * @param stdClass $submission
542 * @return bool
543 */
544 public function is_empty(stdClass $submission) {
7a9fd6da
DW
545 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
546
547 return empty($onlinetextsubmission->onlinetext);
bbd0e548
DW
548 }
549
550 /**
551 * Get file areas returns a list of areas this plugin stores files
552 * @return array - An array of fileareas (keys) and descriptions (values)
553 */
554 public function get_file_areas() {
8ab7bb9f 555 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
bbd0e548
DW
556 }
557
df211804
DW
558 /**
559 * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
560 * on the last submission.
561 * @param stdClass $sourcesubmission
562 * @param stdClass $destsubmission
563 */
564 public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
565 global $DB;
566
567 // Copy the files across (attached via the text editor).
568 $contextid = $this->assignment->get_context()->id;
569 $fs = get_file_storage();
570 $files = $fs->get_area_files($contextid, 'assignsubmission_onlinetext',
571 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sourcesubmission->id, 'id', false);
572 foreach ($files as $file) {
573 $fieldupdates = array('itemid' => $destsubmission->id);
574 $fs->create_file_from_storedfile($fieldupdates, $file);
575 }
576
577 // Copy the assignsubmission_onlinetext record.
578 $onlinetextsubmission = $this->get_onlinetext_submission($sourcesubmission->id);
579 if ($onlinetextsubmission) {
580 unset($onlinetextsubmission->id);
581 $onlinetextsubmission->submission = $destsubmission->id;
582 $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
583 }
584 return true;
585 }
05a6445a
DW
586
587 /**
588 * Return a description of external params suitable for uploading an onlinetext submission from a webservice.
589 *
590 * @return external_description|null
591 */
592 public function get_external_parameters() {
593 $editorparams = array('text' => new external_value(PARAM_TEXT, 'The text for this submission.'),
594 'format' => new external_value(PARAM_INT, 'The format for this submission'),
595 'itemid' => new external_value(PARAM_INT, 'The draft area id for files attached to the submission'));
596 $editorstructure = new external_single_structure($editorparams);
597 return array('onlinetext_editor' => $editorstructure);
598 }
599
bbd0e548
DW
600}
601
602