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 * @return array - return an array of files indexed by filename
260 public function get_files(stdClass $submission) {
263 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
264 if ($onlinetextsubmission) {
265 $user = $DB->get_record("user", array("id"=>$submission->userid),'id,username,firstname,lastname', MUST_EXIST);
267 if (!$this->assignment->is_blind_marking()) {
268 $filename = str_replace('_', '', fullname($user)) . '_' .
269 $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
270 $this->get_name() . '_';
271 $prefix = clean_filename($filename);
273 $filename = get_string('participant', 'assign') . '_' .
274 $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
275 $this->get_name() . '_';
276 $prefix = clean_filename($filename);
279 $finaltext = str_replace('@@PLUGINFILE@@/', $prefix, $onlinetextsubmission->onlinetext);
280 $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>"; //fetched from database
282 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
284 $fs = get_file_storage();
286 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
288 foreach ($fsfiles as $file) {
289 $files[$file->get_filename()] = $file;
297 * Display the saved text content from the editor in the view table
299 * @param stdClass $submission
302 public function view(stdClass $submission) {
305 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
308 if ($onlinetextsubmission) {
310 // render for portfolio API
311 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
319 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
321 * @param string $type old assignment subtype
322 * @param int $version old assignment version
323 * @return bool True if upgrade is possible
325 public function can_upgrade($type, $version) {
326 if ($type == 'online' && $version >= 2011112900) {
334 * Upgrade the settings from the old assignment to the new plugin based one
336 * @param context $oldcontext - the database for the old assignment context
337 * @param stdClass $oldassignment - the database for the old assignment instance
338 * @param string $log record log events here
339 * @return bool Was it a success?
341 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
342 // first upgrade settings (nothing to do)
347 * Upgrade the submission from the old assignment to the new one
349 * @param context $oldcontext - the database for the old assignment context
350 * @param stdClass $oldassignment The data record for the old assignment
351 * @param stdClass $oldsubmission The data record for the old submission
352 * @param stdClass $submission The data record for the new submission
353 * @param string $log Record upgrade messages in the log
354 * @return bool true or false - false will trigger a rollback
356 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
359 $onlinetextsubmission = new stdClass();
360 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
361 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
363 $onlinetextsubmission->submission = $submission->id;
364 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
366 if ($onlinetextsubmission->onlinetext === null) {
367 $onlinetextsubmission->onlinetext = '';
370 if ($onlinetextsubmission->onlineformat === null) {
371 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
374 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
375 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
379 // now copy the area files
380 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
385 $this->assignment->get_context()->id,
386 'assignsubmission_onlinetext',
387 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
393 * Formatting for log info
395 * @param stdClass $submission The new submission
398 public function format_for_log(stdClass $submission) {
399 // format the info for each submission plugin add_to_log
400 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
401 $onlinetextloginfo = '';
402 $text = format_text($onlinetextsubmission->onlinetext,
403 $onlinetextsubmission->onlineformat,
404 array('context'=>$this->assignment->get_context()));
405 $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
407 return $onlinetextloginfo;
411 * The assignment has been deleted - cleanup
415 public function delete_instance() {
417 // will throw exception on failure
418 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
424 * No text is set for this plugin
426 * @param stdClass $submission
429 public function is_empty(stdClass $submission) {
430 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
432 return empty($onlinetextsubmission->onlinetext);
436 * Get file areas returns a list of areas this plugin stores files
437 * @return array - An array of fileareas (keys) and descriptions (values)
439 public function get_file_areas() {
440 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());