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