MDL-42919 Assign: Remove no-label form hacks
[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         $text = format_text($data->onlinetext,
143                             $data->onlinetext_editor['format'],
144                             array('context'=>$this->assignment->get_context()));
146         $fs = get_file_storage();
148         $files = $fs->get_area_files($this->assignment->get_context()->id,
149                                      'assignsubmission_onlinetext',
150                                      ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
151                                      $submission->id,
152                                      'id',
153                                      false);
155         $params = array(
156             'context' => context_module::instance($this->assignment->get_course_module()->id),
157             'objectid' => $submission->id,
158             'other' => array(
159                 'pathnamehashes' => array_keys($files),
160                 'content' => trim($text)
161             )
162         );
163         $event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
164         $event->trigger();
166         if ($onlinetextsubmission) {
168             $onlinetextsubmission->onlinetext = $data->onlinetext;
169             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
171             return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
172         } else {
174             $onlinetextsubmission = new stdClass();
175             $onlinetextsubmission->onlinetext = $data->onlinetext;
176             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
178             $onlinetextsubmission->submission = $submission->id;
179             $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
180             return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
181         }
182     }
184     /**
185      * Return a list of the text fields that can be imported/exported by this plugin
186      *
187      * @return array An array of field names and descriptions. (name=>description, ...)
188      */
189     public function get_editor_fields() {
190         return array('onlinetext' => get_string('pluginname', 'assignsubmission_comments'));
191     }
193     /**
194      * Get the saved text content from the editor
195      *
196      * @param string $name
197      * @param int $submissionid
198      * @return string
199      */
200     public function get_editor_text($name, $submissionid) {
201         if ($name == 'onlinetext') {
202             $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
203             if ($onlinetextsubmission) {
204                 return $onlinetextsubmission->onlinetext;
205             }
206         }
208         return '';
209     }
211     /**
212      * Get the content format for the editor
213      *
214      * @param string $name
215      * @param int $submissionid
216      * @return int
217      */
218     public function get_editor_format($name, $submissionid) {
219         if ($name == 'onlinetext') {
220             $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
221             if ($onlinetextsubmission) {
222                 return $onlinetextsubmission->onlineformat;
223             }
224         }
226         return 0;
227     }
230      /**
231       * Display onlinetext word count in the submission status table
232       *
233       * @param stdClass $submission
234       * @param bool $showviewlink - If the summary has been truncated set this to true
235       * @return string
236       */
237     public function view_summary(stdClass $submission, & $showviewlink) {
238         global $CFG;
240         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
241         // Always show the view link.
242         $showviewlink = true;
244         if ($onlinetextsubmission) {
245             $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
246                                                              $onlinetextsubmission->submission,
247                                                              $this->get_type(),
248                                                              'onlinetext',
249                                                              'assignsubmission_onlinetext');
251             $shorttext = shorten_text($text, 140);
252             $plagiarismlinks = '';
254             if (!empty($CFG->enableplagiarism)) {
255                 require_once($CFG->libdir . '/plagiarismlib.php');
257                 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
258                     'content' => trim($text),
259                     'cmid' => $this->assignment->get_course_module()->id,
260                     'course' => $this->assignment->get_course()->id,
261                     'assignment' => $submission->assignment));
262             }
263             if ($text != $shorttext) {
264                 $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
266                 return $shorttext . $plagiarismlinks . $wordcount;
267             } else {
268                 return $shorttext . $plagiarismlinks;
269             }
270         }
271         return '';
272     }
274     /**
275      * Produce a list of files suitable for export that represent this submission.
276      *
277      * @param stdClass $submission - For this is the submission data
278      * @param stdClass $user - This is the user record for this submission
279      * @return array - return an array of files indexed by filename
280      */
281     public function get_files(stdClass $submission, stdClass $user) {
282         global $DB;
284         $files = array();
285         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
287         if ($onlinetextsubmission) {
288             $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
289             $formattedtext = format_text($finaltext,
290                                          $onlinetextsubmission->onlineformat,
291                                          array('context'=>$this->assignment->get_context()));
292             $submissioncontent = '<html><body>'. $formattedtext . '</body></html>';
294             $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
295             $files[$filename] = array($submissioncontent);
297             $fs = get_file_storage();
299             $fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
300                                            'assignsubmission_onlinetext',
301                                            ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
302                                            $submission->id,
303                                            'timemodified',
304                                            false);
306             foreach ($fsfiles as $file) {
307                 $files[$file->get_filename()] = $file;
308             }
309         }
311         return $files;
312     }
314     /**
315      * Display the saved text content from the editor in the view table
316      *
317      * @param stdClass $submission
318      * @return string
319      */
320     public function view(stdClass $submission) {
321         $result = '';
323         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
325         if ($onlinetextsubmission) {
327             // Render for portfolio API.
328             $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
329                                                                 $onlinetextsubmission->submission,
330                                                                 $this->get_type(),
331                                                                 'onlinetext',
332                                                                 'assignsubmission_onlinetext');
334         }
336         return $result;
337     }
339     /**
340      * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
341      *
342      * @param string $type old assignment subtype
343      * @param int $version old assignment version
344      * @return bool True if upgrade is possible
345      */
346     public function can_upgrade($type, $version) {
347         if ($type == 'online' && $version >= 2011112900) {
348             return true;
349         }
350         return false;
351     }
354     /**
355      * Upgrade the settings from the old assignment to the new plugin based one
356      *
357      * @param context $oldcontext - the database for the old assignment context
358      * @param stdClass $oldassignment - the database for the old assignment instance
359      * @param string $log record log events here
360      * @return bool Was it a success?
361      */
362     public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
363         // No settings to upgrade.
364         return true;
365     }
367     /**
368      * Upgrade the submission from the old assignment to the new one
369      *
370      * @param context $oldcontext - the database for the old assignment context
371      * @param stdClass $oldassignment The data record for the old assignment
372      * @param stdClass $oldsubmission The data record for the old submission
373      * @param stdClass $submission The data record for the new submission
374      * @param string $log Record upgrade messages in the log
375      * @return bool true or false - false will trigger a rollback
376      */
377     public function upgrade(context $oldcontext,
378                             stdClass $oldassignment,
379                             stdClass $oldsubmission,
380                             stdClass $submission,
381                             & $log) {
382         global $DB;
384         $onlinetextsubmission = new stdClass();
385         $onlinetextsubmission->onlinetext = $oldsubmission->data1;
386         $onlinetextsubmission->onlineformat = $oldsubmission->data2;
388         $onlinetextsubmission->submission = $submission->id;
389         $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
391         if ($onlinetextsubmission->onlinetext === null) {
392             $onlinetextsubmission->onlinetext = '';
393         }
395         if ($onlinetextsubmission->onlineformat === null) {
396             $onlinetextsubmission->onlineformat = editors_get_preferred_format();
397         }
399         if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
400             $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
401             return false;
402         }
404         // Now copy the area files.
405         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
406                                                         'mod_assignment',
407                                                         'submission',
408                                                         $oldsubmission->id,
409                                                         $this->assignment->get_context()->id,
410                                                         'assignsubmission_onlinetext',
411                                                         ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
412                                                         $submission->id);
413         return true;
414     }
416     /**
417      * Formatting for log info
418      *
419      * @param stdClass $submission The new submission
420      * @return string
421      */
422     public function format_for_log(stdClass $submission) {
423         // Format the info for each submission plugin (will be logged).
424         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
425         $onlinetextloginfo = '';
426         $text = format_text($onlinetextsubmission->onlinetext,
427                             $onlinetextsubmission->onlineformat,
428                             array('context'=>$this->assignment->get_context()));
429         $onlinetextloginfo .= get_string('numwordsforlog',
430                                          'assignsubmission_onlinetext',
431                                          count_words($text));
433         return $onlinetextloginfo;
434     }
436     /**
437      * The assignment has been deleted - cleanup
438      *
439      * @return bool
440      */
441     public function delete_instance() {
442         global $DB;
443         $DB->delete_records('assignsubmission_onlinetext',
444                             array('assignment'=>$this->assignment->get_instance()->id));
446         return true;
447     }
449     /**
450      * No text is set for this plugin
451      *
452      * @param stdClass $submission
453      * @return bool
454      */
455     public function is_empty(stdClass $submission) {
456         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
458         return empty($onlinetextsubmission->onlinetext);
459     }
461     /**
462      * Get file areas returns a list of areas this plugin stores files
463      * @return array - An array of fileareas (keys) and descriptions (values)
464      */
465     public function get_file_areas() {
466         return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
467     }
469     /**
470      * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
471      * on the last submission.
472      * @param stdClass $sourcesubmission
473      * @param stdClass $destsubmission
474      */
475     public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
476         global $DB;
478         // Copy the files across (attached via the text editor).
479         $contextid = $this->assignment->get_context()->id;
480         $fs = get_file_storage();
481         $files = $fs->get_area_files($contextid, 'assignsubmission_onlinetext',
482                                      ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sourcesubmission->id, 'id', false);
483         foreach ($files as $file) {
484             $fieldupdates = array('itemid' => $destsubmission->id);
485             $fs->create_file_from_storedfile($fieldupdates, $file);
486         }
488         // Copy the assignsubmission_onlinetext record.
489         $onlinetextsubmission = $this->get_onlinetext_submission($sourcesubmission->id);
490         if ($onlinetextsubmission) {
491             unset($onlinetextsubmission->id);
492             $onlinetextsubmission->submission = $destsubmission->id;
493             $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
494         }
495         return true;
496     }
498     /**
499      * Return a description of external params suitable for uploading an onlinetext submission from a webservice.
500      *
501      * @return external_description|null
502      */
503     public function get_external_parameters() {
504         $editorparams = array('text' => new external_value(PARAM_TEXT, 'The text for this submission.'),
505                               'format' => new external_value(PARAM_INT, 'The format for this submission'),
506                               'itemid' => new external_value(PARAM_INT, 'The draft area id for files attached to the submission'));
507         $editorstructure = new external_single_structure($editorparams);
508         return array('onlinetext_editor' => $editorstructure);
509     }