weekly release 2.7dev
[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
61 /**
62 * Add form elements for settings
63 *
64 * @param mixed $submission can be null
65 * @param MoodleQuickForm $mform
66 * @param stdClass $data
67 * @return true if elements were added to the form
68 */
69 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
70 $elements = array();
71
72 $editoroptions = $this->get_edit_options();
73 $submissionid = $submission ? $submission->id : 0;
74
75 if (!isset($data->onlinetext)) {
76 $data->onlinetext = '';
77 }
78 if (!isset($data->onlinetextformat)) {
79 $data->onlinetextformat = editors_get_preferred_format();
80 }
81
82 if ($submission) {
83 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
84 if ($onlinetextsubmission) {
85 $data->onlinetext = $onlinetextsubmission->onlinetext;
86 $data->onlinetextformat = $onlinetextsubmission->onlineformat;
87 }
88
89 }
90
e5403f8c
DW
91 $data = file_prepare_standard_editor($data,
92 'onlinetext',
93 $editoroptions,
94 $this->assignment->get_context(),
95 'assignsubmission_onlinetext',
96 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
97 $submissionid);
3a5c3b3c 98 $mform->addElement('editor', 'onlinetext_editor', $this->get_name(), null, $editoroptions);
7e5be074 99
bbd0e548
DW
100 return true;
101 }
102
103 /**
104 * Editor format options
105 *
106 * @return array
107 */
108 private function get_edit_options() {
109 $editoroptions = array(
110 'noclean' => false,
111 'maxfiles' => EDITOR_UNLIMITED_FILES,
112 'maxbytes' => $this->assignment->get_course()->maxbytes,
c469b396
MG
113 'context' => $this->assignment->get_context(),
114 'return_types' => FILE_INTERNAL | FILE_EXTERNAL
bbd0e548
DW
115 );
116 return $editoroptions;
117 }
118
e5403f8c
DW
119 /**
120 * Save data to the database and trigger plagiarism plugin,
121 * if enabled, to scan the uploaded content via events trigger
122 *
123 * @param stdClass $submission
124 * @param stdClass $data
125 * @return bool
126 */
127 public function save(stdClass $submission, stdClass $data) {
48e5aacb 128 global $USER, $DB;
bbd0e548
DW
129
130 $editoroptions = $this->get_edit_options();
131
e5403f8c
DW
132 $data = file_postupdate_standard_editor($data,
133 'onlinetext',
134 $editoroptions,
135 $this->assignment->get_context(),
136 'assignsubmission_onlinetext',
137 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
138 $submission->id);
bbd0e548
DW
139
140 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
48e5aacb
KG
141
142 $fs = get_file_storage();
e5403f8c
DW
143
144 $files = $fs->get_area_files($this->assignment->get_context()->id,
145 'assignsubmission_onlinetext',
146 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
147 $submission->id,
148 'id',
149 false);
150
7dceb500
FM
151 $params = array(
152 'context' => context_module::instance($this->assignment->get_course_module()->id),
153 'objectid' => $submission->id,
154 'other' => array(
155 'pathnamehashes' => array_keys($files),
fb59d535
AG
156 'content' => trim($data->onlinetext),
157 'format' => $data->onlinetext_editor['format']
7dceb500
FM
158 )
159 );
160 $event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
161 $event->trigger();
48e5aacb 162
bbd0e548
DW
163 if ($onlinetextsubmission) {
164
165 $onlinetextsubmission->onlinetext = $data->onlinetext;
166 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
167
bbd0e548
DW
168 return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
169 } else {
170
171 $onlinetextsubmission = new stdClass();
172 $onlinetextsubmission->onlinetext = $data->onlinetext;
173 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
174
175 $onlinetextsubmission->submission = $submission->id;
176 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
177 return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
178 }
bbd0e548
DW
179 }
180
df47b77f
DW
181 /**
182 * Return a list of the text fields that can be imported/exported by this plugin
183 *
184 * @return array An array of field names and descriptions. (name=>description, ...)
185 */
186 public function get_editor_fields() {
187 return array('onlinetext' => get_string('pluginname', 'assignsubmission_comments'));
188 }
189
bbd0e548
DW
190 /**
191 * Get the saved text content from the editor
192 *
193 * @param string $name
194 * @param int $submissionid
195 * @return string
196 */
197 public function get_editor_text($name, $submissionid) {
198 if ($name == 'onlinetext') {
199 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
200 if ($onlinetextsubmission) {
201 return $onlinetextsubmission->onlinetext;
202 }
203 }
204
205 return '';
206 }
207
208 /**
209 * Get the content format for the editor
210 *
211 * @param string $name
212 * @param int $submissionid
213 * @return int
214 */
215 public function get_editor_format($name, $submissionid) {
216 if ($name == 'onlinetext') {
217 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
218 if ($onlinetextsubmission) {
219 return $onlinetextsubmission->onlineformat;
220 }
221 }
222
e5403f8c 223 return 0;
bbd0e548
DW
224 }
225
226
227 /**
228 * Display onlinetext word count in the submission status table
229 *
230 * @param stdClass $submission
231 * @param bool $showviewlink - If the summary has been truncated set this to true
232 * @return string
233 */
7ee1548a 234 public function view_summary(stdClass $submission, & $showviewlink) {
48e5aacb 235 global $CFG;
bbd0e548
DW
236
237 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
e5403f8c 238 // Always show the view link.
bbd0e548
DW
239 $showviewlink = true;
240
241 if ($onlinetextsubmission) {
e4288042
DW
242 $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
243 $onlinetextsubmission->submission,
244 $this->get_type(),
245 'onlinetext',
246 'assignsubmission_onlinetext');
247
bbd0e548 248 $shorttext = shorten_text($text, 140);
48e5aacb 249 $plagiarismlinks = '';
e5403f8c 250
48e5aacb
KG
251 if (!empty($CFG->enableplagiarism)) {
252 require_once($CFG->libdir . '/plagiarismlib.php');
e5403f8c 253
48e5aacb 254 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
e5403f8c 255 'content' => trim($text),
48e5aacb
KG
256 'cmid' => $this->assignment->get_course_module()->id,
257 'course' => $this->assignment->get_course()->id,
258 'assignment' => $submission->assignment));
259 }
bbd0e548 260 if ($text != $shorttext) {
e5403f8c
DW
261 $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
262
263 return $shorttext . $plagiarismlinks . $wordcount;
bbd0e548 264 } else {
48e5aacb 265 return $shorttext . $plagiarismlinks;
bbd0e548
DW
266 }
267 }
268 return '';
269 }
270
271 /**
e5403f8c 272 * Produce a list of files suitable for export that represent this submission.
bbd0e548
DW
273 *
274 * @param stdClass $submission - For this is the submission data
2406815b 275 * @param stdClass $user - This is the user record for this submission
bbd0e548
DW
276 * @return array - return an array of files indexed by filename
277 */
2406815b 278 public function get_files(stdClass $submission, stdClass $user) {
bbd0e548 279 global $DB;
e5403f8c 280
bbd0e548
DW
281 $files = array();
282 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
e5403f8c 283
bbd0e548 284 if ($onlinetextsubmission) {
2406815b 285 $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
e5403f8c
DW
286 $formattedtext = format_text($finaltext,
287 $onlinetextsubmission->onlineformat,
288 array('context'=>$this->assignment->get_context()));
289 $submissioncontent = '<html><body>'. $formattedtext . '</body></html>';
290
291 $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
292 $files[$filename] = array($submissioncontent);
bbd0e548
DW
293
294 $fs = get_file_storage();
295
e5403f8c
DW
296 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
297 'assignsubmission_onlinetext',
298 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
299 $submission->id,
300 'timemodified',
301 false);
bbd0e548
DW
302
303 foreach ($fsfiles as $file) {
304 $files[$file->get_filename()] = $file;
305 }
306 }
307
308 return $files;
309 }
310
311 /**
312 * Display the saved text content from the editor in the view table
313 *
314 * @param stdClass $submission
315 * @return string
316 */
317 public function view(stdClass $submission) {
318 $result = '';
319
320 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
321
bbd0e548
DW
322 if ($onlinetextsubmission) {
323
e5403f8c
DW
324 // Render for portfolio API.
325 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
326 $onlinetextsubmission->submission,
327 $this->get_type(),
328 'onlinetext',
329 'assignsubmission_onlinetext');
bbd0e548
DW
330
331 }
332
333 return $result;
334 }
335
e5403f8c 336 /**
bbd0e548
DW
337 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
338 *
339 * @param string $type old assignment subtype
340 * @param int $version old assignment version
341 * @return bool True if upgrade is possible
342 */
343 public function can_upgrade($type, $version) {
344 if ($type == 'online' && $version >= 2011112900) {
345 return true;
346 }
347 return false;
348 }
349
350
351 /**
352 * Upgrade the settings from the old assignment to the new plugin based one
353 *
354 * @param context $oldcontext - the database for the old assignment context
355 * @param stdClass $oldassignment - the database for the old assignment instance
356 * @param string $log record log events here
357 * @return bool Was it a success?
358 */
359 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
e5403f8c 360 // No settings to upgrade.
bbd0e548
DW
361 return true;
362 }
363
364 /**
365 * Upgrade the submission from the old assignment to the new one
366 *
367 * @param context $oldcontext - the database for the old assignment context
368 * @param stdClass $oldassignment The data record for the old assignment
369 * @param stdClass $oldsubmission The data record for the old submission
370 * @param stdClass $submission The data record for the new submission
371 * @param string $log Record upgrade messages in the log
372 * @return bool true or false - false will trigger a rollback
373 */
e5403f8c
DW
374 public function upgrade(context $oldcontext,
375 stdClass $oldassignment,
376 stdClass $oldsubmission,
377 stdClass $submission,
378 & $log) {
bbd0e548
DW
379 global $DB;
380
381 $onlinetextsubmission = new stdClass();
382 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
383 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
384
385 $onlinetextsubmission->submission = $submission->id;
386 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
d82bb44d
DW
387
388 if ($onlinetextsubmission->onlinetext === null) {
389 $onlinetextsubmission->onlinetext = '';
390 }
391
392 if ($onlinetextsubmission->onlineformat === null) {
393 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
394 }
395
bbd0e548
DW
396 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
397 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
398 return false;
399 }
400
e5403f8c 401 // Now copy the area files.
bbd0e548
DW
402 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
403 'mod_assignment',
404 'submission',
405 $oldsubmission->id,
bbd0e548
DW
406 $this->assignment->get_context()->id,
407 'assignsubmission_onlinetext',
8ab7bb9f 408 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
bbd0e548
DW
409 $submission->id);
410 return true;
411 }
412
413 /**
414 * Formatting for log info
415 *
416 * @param stdClass $submission The new submission
417 * @return string
418 */
419 public function format_for_log(stdClass $submission) {
e5403f8c 420 // Format the info for each submission plugin (will be logged).
bbd0e548
DW
421 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
422 $onlinetextloginfo = '';
e5403f8c
DW
423 $onlinetextloginfo .= get_string('numwordsforlog',
424 'assignsubmission_onlinetext',
fb59d535 425 count_words($onlinetextsubmission->onlinetext));
bbd0e548
DW
426
427 return $onlinetextloginfo;
428 }
429
430 /**
431 * The assignment has been deleted - cleanup
432 *
433 * @return bool
434 */
435 public function delete_instance() {
436 global $DB;
e5403f8c
DW
437 $DB->delete_records('assignsubmission_onlinetext',
438 array('assignment'=>$this->assignment->get_instance()->id));
bbd0e548
DW
439
440 return true;
441 }
442
443 /**
444 * No text is set for this plugin
445 *
446 * @param stdClass $submission
447 * @return bool
448 */
449 public function is_empty(stdClass $submission) {
7a9fd6da
DW
450 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
451
452 return empty($onlinetextsubmission->onlinetext);
bbd0e548
DW
453 }
454
455 /**
456 * Get file areas returns a list of areas this plugin stores files
457 * @return array - An array of fileareas (keys) and descriptions (values)
458 */
459 public function get_file_areas() {
8ab7bb9f 460 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
bbd0e548
DW
461 }
462
df211804
DW
463 /**
464 * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
465 * on the last submission.
466 * @param stdClass $sourcesubmission
467 * @param stdClass $destsubmission
468 */
469 public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
470 global $DB;
471
472 // Copy the files across (attached via the text editor).
473 $contextid = $this->assignment->get_context()->id;
474 $fs = get_file_storage();
475 $files = $fs->get_area_files($contextid, 'assignsubmission_onlinetext',
476 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sourcesubmission->id, 'id', false);
477 foreach ($files as $file) {
478 $fieldupdates = array('itemid' => $destsubmission->id);
479 $fs->create_file_from_storedfile($fieldupdates, $file);
480 }
481
482 // Copy the assignsubmission_onlinetext record.
483 $onlinetextsubmission = $this->get_onlinetext_submission($sourcesubmission->id);
484 if ($onlinetextsubmission) {
485 unset($onlinetextsubmission->id);
486 $onlinetextsubmission->submission = $destsubmission->id;
487 $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
488 }
489 return true;
490 }
05a6445a
DW
491
492 /**
493 * Return a description of external params suitable for uploading an onlinetext submission from a webservice.
494 *
495 * @return external_description|null
496 */
497 public function get_external_parameters() {
498 $editorparams = array('text' => new external_value(PARAM_TEXT, 'The text for this submission.'),
499 'format' => new external_value(PARAM_INT, 'The format for this submission'),
500 'itemid' => new external_value(PARAM_INT, 'The draft area id for files attached to the submission'));
501 $editorstructure = new external_single_structure($editorparams);
502 return array('onlinetext_editor' => $editorstructure);
503 }
504
bbd0e548
DW
505}
506
507