5ecf1156c13101ef27ec6325bc03a0bbca9f0498
[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', $this->get_name(), null, $editoroptions);
100         return true;
101     }
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,
113            'context' => $this->assignment->get_context(),
114            'return_types' => FILE_INTERNAL | FILE_EXTERNAL
115         );
116         return $editoroptions;
117     }
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) {
128         global $USER, $DB;
130         $editoroptions = $this->get_edit_options();
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);
140         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
142         $fs = get_file_storage();
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);
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),
156                 'content' => trim($data->onlinetext),
157                 'format' => $data->onlinetext_editor['format']
158             )
159         );
160         $event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
161         $event->trigger();
163         if ($onlinetextsubmission) {
165             $onlinetextsubmission->onlinetext = $data->onlinetext;
166             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
168             return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
169         } else {
171             $onlinetextsubmission = new stdClass();
172             $onlinetextsubmission->onlinetext = $data->onlinetext;
173             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
175             $onlinetextsubmission->submission = $submission->id;
176             $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
177             return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
178         }
179     }
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     }
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         }
205         return '';
206     }
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         }
223         return 0;
224     }
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       */
234     public function view_summary(stdClass $submission, & $showviewlink) {
235         global $CFG;
237         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
238         // Always show the view link.
239         $showviewlink = true;
241         if ($onlinetextsubmission) {
242             $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
243                                                              $onlinetextsubmission->submission,
244                                                              $this->get_type(),
245                                                              'onlinetext',
246                                                              'assignsubmission_onlinetext');
248             $shorttext = shorten_text($text, 140);
249             $plagiarismlinks = '';
251             if (!empty($CFG->enableplagiarism)) {
252                 require_once($CFG->libdir . '/plagiarismlib.php');
254                 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
255                     'content' => trim($text),
256                     'cmid' => $this->assignment->get_course_module()->id,
257                     'course' => $this->assignment->get_course()->id,
258                     'assignment' => $submission->assignment));
259             }
260             if ($text != $shorttext) {
261                 $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
263                 return $shorttext . $plagiarismlinks . $wordcount;
264             } else {
265                 return $shorttext . $plagiarismlinks;
266             }
267         }
268         return '';
269     }
271     /**
272      * Produce a list of files suitable for export that represent this submission.
273      *
274      * @param stdClass $submission - For this is the submission data
275      * @param stdClass $user - This is the user record for this submission
276      * @return array - return an array of files indexed by filename
277      */
278     public function get_files(stdClass $submission, stdClass $user) {
279         global $DB;
281         $files = array();
282         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
284         if ($onlinetextsubmission) {
285             $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
286             $formattedtext = format_text($finaltext,
287                                          $onlinetextsubmission->onlineformat,
288                                          array('context'=>$this->assignment->get_context()));
289             $submissioncontent = '<html><body>'. $formattedtext . '</body></html>';
291             $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
292             $files[$filename] = array($submissioncontent);
294             $fs = get_file_storage();
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);
303             foreach ($fsfiles as $file) {
304                 $files[$file->get_filename()] = $file;
305             }
306         }
308         return $files;
309     }
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 = '';
320         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
322         if ($onlinetextsubmission) {
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');
331         }
333         return $result;
334     }
336     /**
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     }
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) {
360         // No settings to upgrade.
361         return true;
362     }
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      */
374     public function upgrade(context $oldcontext,
375                             stdClass $oldassignment,
376                             stdClass $oldsubmission,
377                             stdClass $submission,
378                             & $log) {
379         global $DB;
381         $onlinetextsubmission = new stdClass();
382         $onlinetextsubmission->onlinetext = $oldsubmission->data1;
383         $onlinetextsubmission->onlineformat = $oldsubmission->data2;
385         $onlinetextsubmission->submission = $submission->id;
386         $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
388         if ($onlinetextsubmission->onlinetext === null) {
389             $onlinetextsubmission->onlinetext = '';
390         }
392         if ($onlinetextsubmission->onlineformat === null) {
393             $onlinetextsubmission->onlineformat = editors_get_preferred_format();
394         }
396         if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
397             $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
398             return false;
399         }
401         // Now copy the area files.
402         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
403                                                         'mod_assignment',
404                                                         'submission',
405                                                         $oldsubmission->id,
406                                                         $this->assignment->get_context()->id,
407                                                         'assignsubmission_onlinetext',
408                                                         ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
409                                                         $submission->id);
410         return true;
411     }
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) {
420         // Format the info for each submission plugin (will be logged).
421         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
422         $onlinetextloginfo = '';
423         $onlinetextloginfo .= get_string('numwordsforlog',
424                                          'assignsubmission_onlinetext',
425                                          count_words($onlinetextsubmission->onlinetext));
427         return $onlinetextloginfo;
428     }
430     /**
431      * The assignment has been deleted - cleanup
432      *
433      * @return bool
434      */
435     public function delete_instance() {
436         global $DB;
437         $DB->delete_records('assignsubmission_onlinetext',
438                             array('assignment'=>$this->assignment->get_instance()->id));
440         return true;
441     }
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) {
450         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
452         return empty($onlinetextsubmission->onlinetext);
453     }
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() {
460         return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
461     }
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;
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         }
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     }
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     }