Merge branch 'MDL-37030-master' of git://github.com/damyon/moodle
[moodle.git] / mod / assign / submission / onlinetext / locallib.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
28 // File area for online text submission assignment.
29 define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
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  */
38 class assign_submission_onlinetext extends assign_submission_plugin {
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     }
49     /**
50      * Get onlinetext submission information from the database
51      *
52      * @param  int $submissionid
53      * @return mixed
54      */
55     private function get_onlinetext_submission($submissionid) {
56         global $DB;
58         return $DB->get_record('assignsubmission_onlinetext', array('submission'=>$submissionid));
59     }
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();
72         $editoroptions = $this->get_edit_options();
73         $submissionid = $submission ? $submission->id : 0;
75         if (!isset($data->onlinetext)) {
76             $data->onlinetext = '';
77         }
78         if (!isset($data->onlinetextformat)) {
79             $data->onlinetextformat = editors_get_preferred_format();
80         }
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             }
89         }
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);
98         $mform->addElement('editor', 'onlinetext_editor', '', null, $editoroptions);
99         return true;
100     }
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,
112            'context' => $this->assignment->get_context(),
113            'return_types' => FILE_INTERNAL | FILE_EXTERNAL
114         );
115         return $editoroptions;
116     }
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) {
127         global $USER, $DB;
129         $editoroptions = $this->get_edit_options();
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);
139         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
141         $text = format_text($data->onlinetext,
142                             $data->onlinetext_editor['format'],
143                             array('context'=>$this->assignment->get_context()));
145         $fs = get_file_storage();
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);
154         // Let Moodle know that an assessable content was uploaded (eg for plagiarism detection).
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;
161         $eventdata->content = trim($text);
163         if ($files) {
164             $eventdata->pathnamehashes = array_keys($files);
165         }
166         events_trigger('assessable_content_uploaded', $eventdata);
168         if ($onlinetextsubmission) {
170             $onlinetextsubmission->onlinetext = $data->onlinetext;
171             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
173             return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
174         } else {
176             $onlinetextsubmission = new stdClass();
177             $onlinetextsubmission->onlinetext = $data->onlinetext;
178             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
180             $onlinetextsubmission->submission = $submission->id;
181             $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
182             return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
183         }
184     }
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     }
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         }
210         return '';
211     }
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         }
228         return 0;
229     }
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       */
239     public function view_summary(stdClass $submission, & $showviewlink) {
240         global $CFG;
242         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
243         // Always show the view link.
244         $showviewlink = true;
246         if ($onlinetextsubmission) {
247             $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
248                                                              $onlinetextsubmission->submission,
249                                                              $this->get_type(),
250                                                              'onlinetext',
251                                                              'assignsubmission_onlinetext');
253             $shorttext = shorten_text($text, 140);
254             $plagiarismlinks = '';
256             if (!empty($CFG->enableplagiarism)) {
257                 require_once($CFG->libdir . '/plagiarismlib.php');
259                 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
260                     'content' => trim($text),
261                     'cmid' => $this->assignment->get_course_module()->id,
262                     'course' => $this->assignment->get_course()->id,
263                     'assignment' => $submission->assignment));
264             }
265             if ($text != $shorttext) {
266                 $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
268                 return $shorttext . $plagiarismlinks . $wordcount;
269             } else {
270                 return $shorttext . $plagiarismlinks;
271             }
272         }
273         return '';
274     }
276     /**
277      * Produce a list of files suitable for export that represent this submission.
278      *
279      * @param stdClass $submission - For this is the submission data
280      * @param stdClass $user - This is the user record for this submission
281      * @return array - return an array of files indexed by filename
282      */
283     public function get_files(stdClass $submission, stdClass $user) {
284         global $DB;
286         $files = array();
287         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
289         if ($onlinetextsubmission) {
290             $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
291             $formattedtext = format_text($finaltext,
292                                          $onlinetextsubmission->onlineformat,
293                                          array('context'=>$this->assignment->get_context()));
294             $submissioncontent = '<html><body>'. $formattedtext . '</body></html>';
296             $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
297             $files[$filename] = array($submissioncontent);
299             $fs = get_file_storage();
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);
308             foreach ($fsfiles as $file) {
309                 $files[$file->get_filename()] = $file;
310             }
311         }
313         return $files;
314     }
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 = '';
325         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
327         if ($onlinetextsubmission) {
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');
336         }
338         return $result;
339     }
341     /**
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     }
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) {
365         // No settings to upgrade.
366         return true;
367     }
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      */
379     public function upgrade(context $oldcontext,
380                             stdClass $oldassignment,
381                             stdClass $oldsubmission,
382                             stdClass $submission,
383                             & $log) {
384         global $DB;
386         $onlinetextsubmission = new stdClass();
387         $onlinetextsubmission->onlinetext = $oldsubmission->data1;
388         $onlinetextsubmission->onlineformat = $oldsubmission->data2;
390         $onlinetextsubmission->submission = $submission->id;
391         $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
393         if ($onlinetextsubmission->onlinetext === null) {
394             $onlinetextsubmission->onlinetext = '';
395         }
397         if ($onlinetextsubmission->onlineformat === null) {
398             $onlinetextsubmission->onlineformat = editors_get_preferred_format();
399         }
401         if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
402             $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
403             return false;
404         }
406         // Now copy the area files.
407         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
408                                                         'mod_assignment',
409                                                         'submission',
410                                                         $oldsubmission->id,
411                                                         $this->assignment->get_context()->id,
412                                                         'assignsubmission_onlinetext',
413                                                         ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
414                                                         $submission->id);
415         return true;
416     }
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) {
425         // Format the info for each submission plugin (will be logged).
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()));
431         $onlinetextloginfo .= get_string('numwordsforlog',
432                                          'assignsubmission_onlinetext',
433                                          count_words($text));
435         return $onlinetextloginfo;
436     }
438     /**
439      * The assignment has been deleted - cleanup
440      *
441      * @return bool
442      */
443     public function delete_instance() {
444         global $DB;
445         $DB->delete_records('assignsubmission_onlinetext',
446                             array('assignment'=>$this->assignment->get_instance()->id));
448         return true;
449     }
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) {
458         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
460         return empty($onlinetextsubmission->onlinetext);
461     }
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() {
468         return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
469     }