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()
111 return $editoroptions;
115 * Save data to the database
117 * @param stdClass $submission
118 * @param stdClass $data
121 public function save(stdClass $submission, stdClass $data) {
124 $editoroptions = $this->get_edit_options();
126 $data = file_postupdate_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id);
128 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
129 if ($onlinetextsubmission) {
131 $onlinetextsubmission->onlinetext = $data->onlinetext;
132 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
135 return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
138 $onlinetextsubmission = new stdClass();
139 $onlinetextsubmission->onlinetext = $data->onlinetext;
140 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
142 $onlinetextsubmission->submission = $submission->id;
143 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
144 return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
151 * Get the saved text content from the editor
153 * @param string $name
154 * @param int $submissionid
157 public function get_editor_text($name, $submissionid) {
158 if ($name == 'onlinetext') {
159 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
160 if ($onlinetextsubmission) {
161 return $onlinetextsubmission->onlinetext;
169 * Get the content format for the editor
171 * @param string $name
172 * @param int $submissionid
175 public function get_editor_format($name, $submissionid) {
176 if ($name == 'onlinetext') {
177 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
178 if ($onlinetextsubmission) {
179 return $onlinetextsubmission->onlineformat;
189 * Display onlinetext word count in the submission status table
191 * @param stdClass $submission
192 * @param bool $showviewlink - If the summary has been truncated set this to true
195 public function view_summary(stdClass $submission, & $showviewlink) {
197 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
198 // always show the view link
199 $showviewlink = true;
201 if ($onlinetextsubmission) {
202 $text = format_text($onlinetextsubmission->onlinetext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context()));
203 $shorttext = shorten_text($text, 140);
204 if ($text != $shorttext) {
205 return $shorttext . get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
214 * Produce a list of files suitable for export that represent this submission
216 * @param stdClass $submission - For this is the submission data
217 * @return array - return an array of files indexed by filename
219 public function get_files(stdClass $submission) {
222 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
223 if ($onlinetextsubmission) {
224 $user = $DB->get_record("user", array("id"=>$submission->userid),'id,username,firstname,lastname', MUST_EXIST);
226 $prefix = clean_filename(fullname($user) . "_" .$submission->userid . "_");
227 $finaltext = str_replace('@@PLUGINFILE@@/', $prefix, $onlinetextsubmission->onlinetext);
228 $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>"; //fetched from database
230 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
232 $fs = get_file_storage();
234 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
236 foreach ($fsfiles as $file) {
237 $files[$file->get_filename()] = $file;
245 * Display the saved text content from the editor in the view table
247 * @param stdClass $submission
250 public function view(stdClass $submission) {
253 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
256 if ($onlinetextsubmission) {
258 // render for portfolio API
259 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
267 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
269 * @param string $type old assignment subtype
270 * @param int $version old assignment version
271 * @return bool True if upgrade is possible
273 public function can_upgrade($type, $version) {
274 if ($type == 'online' && $version >= 2011112900) {
282 * Upgrade the settings from the old assignment to the new plugin based one
284 * @param context $oldcontext - the database for the old assignment context
285 * @param stdClass $oldassignment - the database for the old assignment instance
286 * @param string $log record log events here
287 * @return bool Was it a success?
289 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
290 // first upgrade settings (nothing to do)
295 * Upgrade the submission from the old assignment to the new one
297 * @param context $oldcontext - the database for the old assignment context
298 * @param stdClass $oldassignment The data record for the old assignment
299 * @param stdClass $oldsubmission The data record for the old submission
300 * @param stdClass $submission The data record for the new submission
301 * @param string $log Record upgrade messages in the log
302 * @return bool true or false - false will trigger a rollback
304 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
307 $onlinetextsubmission = new stdClass();
308 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
309 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
311 $onlinetextsubmission->submission = $submission->id;
312 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
314 if ($onlinetextsubmission->onlinetext === null) {
315 $onlinetextsubmission->onlinetext = '';
318 if ($onlinetextsubmission->onlineformat === null) {
319 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
322 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
323 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
327 // now copy the area files
328 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
333 $this->assignment->get_context()->id,
334 'assignsubmission_onlinetext',
335 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
341 * Formatting for log info
343 * @param stdClass $submission The new submission
346 public function format_for_log(stdClass $submission) {
347 // format the info for each submission plugin add_to_log
348 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
349 $onlinetextloginfo = '';
350 $text = format_text($onlinetextsubmission->onlinetext,
351 $onlinetextsubmission->onlineformat,
352 array('context'=>$this->assignment->get_context()));
353 $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
355 return $onlinetextloginfo;
359 * The assignment has been deleted - cleanup
363 public function delete_instance() {
365 // will throw exception on failure
366 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
372 * No text is set for this plugin
374 * @param stdClass $submission
377 public function is_empty(stdClass $submission) {
378 return $this->view($submission) == '';
382 * Get file areas returns a list of areas this plugin stores files
383 * @return array - An array of fileareas (keys) and descriptions (values)
385 public function get_file_areas() {
386 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());