2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the definition for the library class for onlinetext submission plugin
20 * This class provides all the functionality for the new assign module.
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
27 defined('MOODLE_INTERNAL') || die();
29 * File area for online text submission assignment
31 define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
34 * library class for onlinetext submission plugin extending submission plugin base class
36 * @package assignsubmission_onlinetext
37 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class assign_submission_onlinetext extends assign_submission_plugin {
43 * Get the name of the online text submission plugin
46 public function get_name() {
47 return get_string('onlinetext', 'assignsubmission_onlinetext');
52 * Get onlinetext submission information from the database
54 * @param int $submissionid
57 private function get_onlinetext_submission($submissionid) {
60 return $DB->get_record('assignsubmission_onlinetext', array('submission'=>$submissionid));
64 * Add form elements for settings
66 * @param mixed $submission can be null
67 * @param MoodleQuickForm $mform
68 * @param stdClass $data
69 * @return true if elements were added to the form
71 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
74 $editoroptions = $this->get_edit_options();
75 $submissionid = $submission ? $submission->id : 0;
77 if (!isset($data->onlinetext)) {
78 $data->onlinetext = '';
80 if (!isset($data->onlinetextformat)) {
81 $data->onlinetextformat = editors_get_preferred_format();
85 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
86 if ($onlinetextsubmission) {
87 $data->onlinetext = $onlinetextsubmission->onlinetext;
88 $data->onlinetextformat = $onlinetextsubmission->onlineformat;
94 $data = file_prepare_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submissionid);
95 $mform->addElement('editor', 'onlinetext_editor', '', null, $editoroptions);
100 * Editor format options
104 private function get_edit_options() {
105 $editoroptions = array(
107 'maxfiles' => EDITOR_UNLIMITED_FILES,
108 'maxbytes' => $this->assignment->get_course()->maxbytes,
109 'context' => $this->assignment->get_context(),
110 'return_types' => FILE_INTERNAL | FILE_EXTERNAL
112 return $editoroptions;
116 * Save data to the database and trigger plagiarism plugin, if enabled, to scan the uploaded content via events trigger
118 * @param stdClass $submission
119 * @param stdClass $data
122 public function save(stdClass $submission, stdClass $data) {
125 $editoroptions = $this->get_edit_options();
127 $data = file_postupdate_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id);
129 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
131 $fs = get_file_storage();
132 $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "id", false);
133 // Let Moodle know that an assessable content was uploaded (eg for plagiarism detection)
134 $eventdata = new stdClass();
135 $eventdata->modulename = 'assign';
136 $eventdata->cmid = $this->assignment->get_course_module()->id;
137 $eventdata->itemid = $submission->id;
138 $eventdata->courseid = $this->assignment->get_course()->id;
139 $eventdata->userid = $USER->id;
140 $eventdata->content = trim(format_text($data->onlinetext, $data->onlinetext_editor['format'], array('context'=>$this->assignment->get_context())));
142 $eventdata->pathnamehashes = array_keys($files);
144 events_trigger('assessable_content_uploaded', $eventdata);
146 if ($onlinetextsubmission) {
148 $onlinetextsubmission->onlinetext = $data->onlinetext;
149 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
152 return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
155 $onlinetextsubmission = new stdClass();
156 $onlinetextsubmission->onlinetext = $data->onlinetext;
157 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
159 $onlinetextsubmission->submission = $submission->id;
160 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
161 return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
168 * Return a list of the text fields that can be imported/exported by this plugin
170 * @return array An array of field names and descriptions. (name=>description, ...)
172 public function get_editor_fields() {
173 return array('onlinetext' => get_string('pluginname', 'assignsubmission_comments'));
177 * Get the saved text content from the editor
179 * @param string $name
180 * @param int $submissionid
183 public function get_editor_text($name, $submissionid) {
184 if ($name == 'onlinetext') {
185 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
186 if ($onlinetextsubmission) {
187 return $onlinetextsubmission->onlinetext;
195 * Get the content format for the editor
197 * @param string $name
198 * @param int $submissionid
201 public function get_editor_format($name, $submissionid) {
202 if ($name == 'onlinetext') {
203 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
204 if ($onlinetextsubmission) {
205 return $onlinetextsubmission->onlineformat;
215 * Display onlinetext word count in the submission status table
217 * @param stdClass $submission
218 * @param bool $showviewlink - If the summary has been truncated set this to true
221 public function view_summary(stdClass $submission, & $showviewlink) {
224 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
225 // always show the view link
226 $showviewlink = true;
228 if ($onlinetextsubmission) {
229 $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
230 $onlinetextsubmission->submission,
233 'assignsubmission_onlinetext');
235 $shorttext = shorten_text($text, 140);
236 $plagiarismlinks = '';
237 if (!empty($CFG->enableplagiarism)) {
238 require_once($CFG->libdir . '/plagiarismlib.php');
239 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
240 'content' => trim(format_text($onlinetextsubmission->onlinetext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context()))),
241 'cmid' => $this->assignment->get_course_module()->id,
242 'course' => $this->assignment->get_course()->id,
243 'assignment' => $submission->assignment));
245 if ($text != $shorttext) {
246 return $shorttext . $plagiarismlinks . get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
248 return $shorttext . $plagiarismlinks;
255 * Produce a list of files suitable for export that represent this submission
257 * @param stdClass $submission - For this is the submission data
258 * @param stdClass $user - This is the user record for this submission
259 * @return array - return an array of files indexed by filename
261 public function get_files(stdClass $submission, stdClass $user) {
264 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
265 if ($onlinetextsubmission) {
266 $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
267 $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>";
269 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
271 $fs = get_file_storage();
273 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
275 foreach ($fsfiles as $file) {
276 $files[$file->get_filename()] = $file;
284 * Display the saved text content from the editor in the view table
286 * @param stdClass $submission
289 public function view(stdClass $submission) {
292 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
295 if ($onlinetextsubmission) {
297 // render for portfolio API
298 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
306 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
308 * @param string $type old assignment subtype
309 * @param int $version old assignment version
310 * @return bool True if upgrade is possible
312 public function can_upgrade($type, $version) {
313 if ($type == 'online' && $version >= 2011112900) {
321 * Upgrade the settings from the old assignment to the new plugin based one
323 * @param context $oldcontext - the database for the old assignment context
324 * @param stdClass $oldassignment - the database for the old assignment instance
325 * @param string $log record log events here
326 * @return bool Was it a success?
328 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
329 // first upgrade settings (nothing to do)
334 * Upgrade the submission from the old assignment to the new one
336 * @param context $oldcontext - the database for the old assignment context
337 * @param stdClass $oldassignment The data record for the old assignment
338 * @param stdClass $oldsubmission The data record for the old submission
339 * @param stdClass $submission The data record for the new submission
340 * @param string $log Record upgrade messages in the log
341 * @return bool true or false - false will trigger a rollback
343 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
346 $onlinetextsubmission = new stdClass();
347 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
348 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
350 $onlinetextsubmission->submission = $submission->id;
351 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
353 if ($onlinetextsubmission->onlinetext === null) {
354 $onlinetextsubmission->onlinetext = '';
357 if ($onlinetextsubmission->onlineformat === null) {
358 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
361 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
362 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
366 // now copy the area files
367 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
372 $this->assignment->get_context()->id,
373 'assignsubmission_onlinetext',
374 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
380 * Formatting for log info
382 * @param stdClass $submission The new submission
385 public function format_for_log(stdClass $submission) {
386 // format the info for each submission plugin add_to_log
387 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
388 $onlinetextloginfo = '';
389 $text = format_text($onlinetextsubmission->onlinetext,
390 $onlinetextsubmission->onlineformat,
391 array('context'=>$this->assignment->get_context()));
392 $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
394 return $onlinetextloginfo;
398 * The assignment has been deleted - cleanup
402 public function delete_instance() {
404 // will throw exception on failure
405 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
411 * No text is set for this plugin
413 * @param stdClass $submission
416 public function is_empty(stdClass $submission) {
417 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
419 return empty($onlinetextsubmission->onlinetext);
423 * Get file areas returns a list of areas this plugin stores files
424 * @return array - An array of fileareas (keys) and descriptions (values)
426 public function get_file_areas() {
427 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());