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