weekly on-sync release 2.5dev
[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 /**
48e5aacb 116 * Save data to the database and trigger plagiarism plugin, if enabled, to scan the uploaded content via events trigger
bbd0e548
DW
117 *
118 * @param stdClass $submission
119 * @param stdClass $data
120 * @return bool
121 */
122 public function save(stdClass $submission, stdClass $data) {
48e5aacb 123 global $USER, $DB;
bbd0e548
DW
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);
48e5aacb
KG
130
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;
ddd84e92 140 $eventdata->content = trim(format_text($data->onlinetext, $data->onlinetext_editor['format'], array('context'=>$this->assignment->get_context())));
48e5aacb
KG
141 if ($files) {
142 $eventdata->pathnamehashes = array_keys($files);
143 }
144 events_trigger('assessable_content_uploaded', $eventdata);
145
bbd0e548
DW
146 if ($onlinetextsubmission) {
147
148 $onlinetextsubmission->onlinetext = $data->onlinetext;
149 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
150
151
152 return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
153 } else {
154
155 $onlinetextsubmission = new stdClass();
156 $onlinetextsubmission->onlinetext = $data->onlinetext;
157 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
158
159 $onlinetextsubmission->submission = $submission->id;
160 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
161 return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
162 }
163
164
165 }
166
df47b77f
DW
167 /**
168 * Return a list of the text fields that can be imported/exported by this plugin
169 *
170 * @return array An array of field names and descriptions. (name=>description, ...)
171 */
172 public function get_editor_fields() {
173 return array('onlinetext' => get_string('pluginname', 'assignsubmission_comments'));
174 }
175
bbd0e548
DW
176 /**
177 * Get the saved text content from the editor
178 *
179 * @param string $name
180 * @param int $submissionid
181 * @return string
182 */
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;
188 }
189 }
190
191 return '';
192 }
193
194 /**
195 * Get the content format for the editor
196 *
197 * @param string $name
198 * @param int $submissionid
199 * @return int
200 */
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;
206 }
207 }
208
209
210 return 0;
211 }
212
213
214 /**
215 * Display onlinetext word count in the submission status table
216 *
217 * @param stdClass $submission
218 * @param bool $showviewlink - If the summary has been truncated set this to true
219 * @return string
220 */
7ee1548a 221 public function view_summary(stdClass $submission, & $showviewlink) {
48e5aacb 222 global $CFG;
bbd0e548
DW
223
224 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
225 // always show the view link
226 $showviewlink = true;
227
228 if ($onlinetextsubmission) {
e4288042
DW
229 $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
230 $onlinetextsubmission->submission,
231 $this->get_type(),
232 'onlinetext',
233 'assignsubmission_onlinetext');
234
bbd0e548 235 $shorttext = shorten_text($text, 140);
48e5aacb
KG
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));
244 }
bbd0e548 245 if ($text != $shorttext) {
48e5aacb 246 return $shorttext . $plagiarismlinks . get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
bbd0e548 247 } else {
48e5aacb 248 return $shorttext . $plagiarismlinks;
bbd0e548
DW
249 }
250 }
251 return '';
252 }
253
254 /**
255 * Produce a list of files suitable for export that represent this submission
256 *
257 * @param stdClass $submission - For this is the submission data
258 * @return array - return an array of files indexed by filename
259 */
260 public function get_files(stdClass $submission) {
261 global $DB;
262 $files = array();
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);
266
b473171a
DW
267 if (!$this->assignment->is_blind_marking()) {
268 $filename = str_replace('_', '', fullname($user)) . '_' .
df47b77f 269 $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
b473171a
DW
270 $this->get_name() . '_';
271 $prefix = clean_filename($filename);
272 } else {
273 $filename = get_string('participant', 'assign') . '_' .
df47b77f 274 $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
b473171a
DW
275 $this->get_name() . '_';
276 $prefix = clean_filename($filename);
277 }
278
bbd0e548
DW
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
281
282 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
283
284 $fs = get_file_storage();
285
8ab7bb9f 286 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
bbd0e548
DW
287
288 foreach ($fsfiles as $file) {
289 $files[$file->get_filename()] = $file;
290 }
291 }
292
293 return $files;
294 }
295
296 /**
297 * Display the saved text content from the editor in the view table
298 *
299 * @param stdClass $submission
300 * @return string
301 */
302 public function view(stdClass $submission) {
303 $result = '';
304
305 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
306
307
308 if ($onlinetextsubmission) {
309
310 // render for portfolio API
8ab7bb9f 311 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
bbd0e548
DW
312
313 }
314
315 return $result;
316 }
317
318 /**
319 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
320 *
321 * @param string $type old assignment subtype
322 * @param int $version old assignment version
323 * @return bool True if upgrade is possible
324 */
325 public function can_upgrade($type, $version) {
326 if ($type == 'online' && $version >= 2011112900) {
327 return true;
328 }
329 return false;
330 }
331
332
333 /**
334 * Upgrade the settings from the old assignment to the new plugin based one
335 *
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?
340 */
341 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
342 // first upgrade settings (nothing to do)
343 return true;
344 }
345
346 /**
347 * Upgrade the submission from the old assignment to the new one
348 *
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
355 */
356 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
357 global $DB;
358
359 $onlinetextsubmission = new stdClass();
360 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
361 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
362
363 $onlinetextsubmission->submission = $submission->id;
364 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
d82bb44d
DW
365
366 if ($onlinetextsubmission->onlinetext === null) {
367 $onlinetextsubmission->onlinetext = '';
368 }
369
370 if ($onlinetextsubmission->onlineformat === null) {
371 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
372 }
373
bbd0e548
DW
374 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
375 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
376 return false;
377 }
378
379 // now copy the area files
380 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
381 'mod_assignment',
382 'submission',
383 $oldsubmission->id,
384 // New file area
385 $this->assignment->get_context()->id,
386 'assignsubmission_onlinetext',
8ab7bb9f 387 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
bbd0e548
DW
388 $submission->id);
389 return true;
390 }
391
392 /**
393 * Formatting for log info
394 *
395 * @param stdClass $submission The new submission
396 * @return string
397 */
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));
406
407 return $onlinetextloginfo;
408 }
409
410 /**
411 * The assignment has been deleted - cleanup
412 *
413 * @return bool
414 */
415 public function delete_instance() {
416 global $DB;
417 // will throw exception on failure
418 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
419
420 return true;
421 }
422
423 /**
424 * No text is set for this plugin
425 *
426 * @param stdClass $submission
427 * @return bool
428 */
429 public function is_empty(stdClass $submission) {
7a9fd6da
DW
430 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
431
432 return empty($onlinetextsubmission->onlinetext);
bbd0e548
DW
433 }
434
435 /**
436 * Get file areas returns a list of areas this plugin stores files
437 * @return array - An array of fileareas (keys) and descriptions (values)
438 */
439 public function get_file_areas() {
8ab7bb9f 440 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
bbd0e548
DW
441 }
442
443}
444
445