weekly release 2.4dev
[moodle.git] / mod / assign / submission / onlinetext / locallib.php
CommitLineData
bbd0e548
DW
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/>.
16
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 */
26
27defined('MOODLE_INTERNAL') || die();
28/**
29 * File area for online text submission assignment
30 */
8ab7bb9f 31define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
bbd0e548
DW
32
33/**
34 * library class for onlinetext submission plugin extending submission plugin base class
35 *
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
39 */
40class assign_submission_onlinetext extends assign_submission_plugin {
41
42 /**
43 * Get the name of the online text submission plugin
44 * @return string
45 */
46 public function get_name() {
47 return get_string('onlinetext', 'assignsubmission_onlinetext');
48 }
49
50
51 /**
52 * Get onlinetext submission information from the database
53 *
54 * @param int $submissionid
55 * @return mixed
56 */
57 private function get_onlinetext_submission($submissionid) {
58 global $DB;
59
60 return $DB->get_record('assignsubmission_onlinetext', array('submission'=>$submissionid));
61 }
62
63 /**
64 * Add form elements for settings
65 *
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
70 */
71 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
72 $elements = array();
73
74 $editoroptions = $this->get_edit_options();
75 $submissionid = $submission ? $submission->id : 0;
76
77 if (!isset($data->onlinetext)) {
78 $data->onlinetext = '';
79 }
80 if (!isset($data->onlinetextformat)) {
81 $data->onlinetextformat = editors_get_preferred_format();
82 }
83
84 if ($submission) {
85 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
86 if ($onlinetextsubmission) {
87 $data->onlinetext = $onlinetextsubmission->onlinetext;
88 $data->onlinetextformat = $onlinetextsubmission->onlineformat;
89 }
90
91 }
92
93
8ab7bb9f 94 $data = file_prepare_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submissionid);
bbd0e548
DW
95 $mform->addElement('editor', 'onlinetext_editor', '', null, $editoroptions);
96 return true;
97 }
98
99 /**
100 * Editor format options
101 *
102 * @return array
103 */
104 private function get_edit_options() {
105 $editoroptions = array(
106 'noclean' => false,
107 'maxfiles' => EDITOR_UNLIMITED_FILES,
108 'maxbytes' => $this->assignment->get_course()->maxbytes,
c469b396
MG
109 'context' => $this->assignment->get_context(),
110 'return_types' => FILE_INTERNAL | FILE_EXTERNAL
bbd0e548
DW
111 );
112 return $editoroptions;
113 }
114
115 /**
116 * Save data to the database
117 *
118 * @param stdClass $submission
119 * @param stdClass $data
120 * @return bool
121 */
122 public function save(stdClass $submission, stdClass $data) {
123 global $DB;
124
125 $editoroptions = $this->get_edit_options();
126
8ab7bb9f 127 $data = file_postupdate_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id);
bbd0e548
DW
128
129 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
130 if ($onlinetextsubmission) {
131
132 $onlinetextsubmission->onlinetext = $data->onlinetext;
133 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
134
135
136 return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
137 } else {
138
139 $onlinetextsubmission = new stdClass();
140 $onlinetextsubmission->onlinetext = $data->onlinetext;
141 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
142
143 $onlinetextsubmission->submission = $submission->id;
144 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
145 return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
146 }
147
148
149 }
150
151 /**
152 * Get the saved text content from the editor
153 *
154 * @param string $name
155 * @param int $submissionid
156 * @return string
157 */
158 public function get_editor_text($name, $submissionid) {
159 if ($name == 'onlinetext') {
160 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
161 if ($onlinetextsubmission) {
162 return $onlinetextsubmission->onlinetext;
163 }
164 }
165
166 return '';
167 }
168
169 /**
170 * Get the content format for the editor
171 *
172 * @param string $name
173 * @param int $submissionid
174 * @return int
175 */
176 public function get_editor_format($name, $submissionid) {
177 if ($name == 'onlinetext') {
178 $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
179 if ($onlinetextsubmission) {
180 return $onlinetextsubmission->onlineformat;
181 }
182 }
183
184
185 return 0;
186 }
187
188
189 /**
190 * Display onlinetext word count in the submission status table
191 *
192 * @param stdClass $submission
193 * @param bool $showviewlink - If the summary has been truncated set this to true
194 * @return string
195 */
7ee1548a 196 public function view_summary(stdClass $submission, & $showviewlink) {
bbd0e548
DW
197
198 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
199 // always show the view link
200 $showviewlink = true;
201
202 if ($onlinetextsubmission) {
203 $text = format_text($onlinetextsubmission->onlinetext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context()));
204 $shorttext = shorten_text($text, 140);
205 if ($text != $shorttext) {
206 return $shorttext . get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
207 } else {
208 return $shorttext;
209 }
210 }
211 return '';
212 }
213
214 /**
215 * Produce a list of files suitable for export that represent this submission
216 *
217 * @param stdClass $submission - For this is the submission data
218 * @return array - return an array of files indexed by filename
219 */
220 public function get_files(stdClass $submission) {
221 global $DB;
222 $files = array();
223 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
224 if ($onlinetextsubmission) {
225 $user = $DB->get_record("user", array("id"=>$submission->userid),'id,username,firstname,lastname', MUST_EXIST);
226
227 $prefix = clean_filename(fullname($user) . "_" .$submission->userid . "_");
228 $finaltext = str_replace('@@PLUGINFILE@@/', $prefix, $onlinetextsubmission->onlinetext);
229 $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>"; //fetched from database
230
231 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
232
233 $fs = get_file_storage();
234
8ab7bb9f 235 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
bbd0e548
DW
236
237 foreach ($fsfiles as $file) {
238 $files[$file->get_filename()] = $file;
239 }
240 }
241
242 return $files;
243 }
244
245 /**
246 * Display the saved text content from the editor in the view table
247 *
248 * @param stdClass $submission
249 * @return string
250 */
251 public function view(stdClass $submission) {
252 $result = '';
253
254 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
255
256
257 if ($onlinetextsubmission) {
258
259 // render for portfolio API
8ab7bb9f 260 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
bbd0e548
DW
261
262 }
263
264 return $result;
265 }
266
267 /**
268 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
269 *
270 * @param string $type old assignment subtype
271 * @param int $version old assignment version
272 * @return bool True if upgrade is possible
273 */
274 public function can_upgrade($type, $version) {
275 if ($type == 'online' && $version >= 2011112900) {
276 return true;
277 }
278 return false;
279 }
280
281
282 /**
283 * Upgrade the settings from the old assignment to the new plugin based one
284 *
285 * @param context $oldcontext - the database for the old assignment context
286 * @param stdClass $oldassignment - the database for the old assignment instance
287 * @param string $log record log events here
288 * @return bool Was it a success?
289 */
290 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
291 // first upgrade settings (nothing to do)
292 return true;
293 }
294
295 /**
296 * Upgrade the submission from the old assignment to the new one
297 *
298 * @param context $oldcontext - the database for the old assignment context
299 * @param stdClass $oldassignment The data record for the old assignment
300 * @param stdClass $oldsubmission The data record for the old submission
301 * @param stdClass $submission The data record for the new submission
302 * @param string $log Record upgrade messages in the log
303 * @return bool true or false - false will trigger a rollback
304 */
305 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
306 global $DB;
307
308 $onlinetextsubmission = new stdClass();
309 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
310 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
311
312 $onlinetextsubmission->submission = $submission->id;
313 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
d82bb44d
DW
314
315 if ($onlinetextsubmission->onlinetext === null) {
316 $onlinetextsubmission->onlinetext = '';
317 }
318
319 if ($onlinetextsubmission->onlineformat === null) {
320 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
321 }
322
bbd0e548
DW
323 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
324 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
325 return false;
326 }
327
328 // now copy the area files
329 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
330 'mod_assignment',
331 'submission',
332 $oldsubmission->id,
333 // New file area
334 $this->assignment->get_context()->id,
335 'assignsubmission_onlinetext',
8ab7bb9f 336 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
bbd0e548
DW
337 $submission->id);
338 return true;
339 }
340
341 /**
342 * Formatting for log info
343 *
344 * @param stdClass $submission The new submission
345 * @return string
346 */
347 public function format_for_log(stdClass $submission) {
348 // format the info for each submission plugin add_to_log
349 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
350 $onlinetextloginfo = '';
351 $text = format_text($onlinetextsubmission->onlinetext,
352 $onlinetextsubmission->onlineformat,
353 array('context'=>$this->assignment->get_context()));
354 $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
355
356 return $onlinetextloginfo;
357 }
358
359 /**
360 * The assignment has been deleted - cleanup
361 *
362 * @return bool
363 */
364 public function delete_instance() {
365 global $DB;
366 // will throw exception on failure
367 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
368
369 return true;
370 }
371
372 /**
373 * No text is set for this plugin
374 *
375 * @param stdClass $submission
376 * @return bool
377 */
378 public function is_empty(stdClass $submission) {
379 return $this->view($submission) == '';
380 }
381
382 /**
383 * Get file areas returns a list of areas this plugin stores files
384 * @return array - An array of fileareas (keys) and descriptions (values)
385 */
386 public function get_file_areas() {
8ab7bb9f 387 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
bbd0e548
DW
388 }
389
390}
391
392