MDL-36908 Assignment: Additional fixes for preventing empty submissions
[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) {
229 $text = format_text($onlinetextsubmission->onlinetext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context()));
230 $shorttext = shorten_text($text, 140);
48e5aacb
KG
231 $plagiarismlinks = '';
232 if (!empty($CFG->enableplagiarism)) {
233 require_once($CFG->libdir . '/plagiarismlib.php');
234 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
235 'content' => trim(format_text($onlinetextsubmission->onlinetext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context()))),
236 'cmid' => $this->assignment->get_course_module()->id,
237 'course' => $this->assignment->get_course()->id,
238 'assignment' => $submission->assignment));
239 }
bbd0e548 240 if ($text != $shorttext) {
48e5aacb 241 return $shorttext . $plagiarismlinks . get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
bbd0e548 242 } else {
48e5aacb 243 return $shorttext . $plagiarismlinks;
bbd0e548
DW
244 }
245 }
246 return '';
247 }
248
249 /**
250 * Produce a list of files suitable for export that represent this submission
251 *
252 * @param stdClass $submission - For this is the submission data
253 * @return array - return an array of files indexed by filename
254 */
255 public function get_files(stdClass $submission) {
256 global $DB;
257 $files = array();
258 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
259 if ($onlinetextsubmission) {
260 $user = $DB->get_record("user", array("id"=>$submission->userid),'id,username,firstname,lastname', MUST_EXIST);
261
b473171a
DW
262 if (!$this->assignment->is_blind_marking()) {
263 $filename = str_replace('_', '', fullname($user)) . '_' .
df47b77f 264 $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
b473171a
DW
265 $this->get_name() . '_';
266 $prefix = clean_filename($filename);
267 } else {
268 $filename = get_string('participant', 'assign') . '_' .
df47b77f 269 $this->assignment->get_uniqueid_for_user($submission->userid) . '_' .
b473171a
DW
270 $this->get_name() . '_';
271 $prefix = clean_filename($filename);
272 }
273
bbd0e548
DW
274 $finaltext = str_replace('@@PLUGINFILE@@/', $prefix, $onlinetextsubmission->onlinetext);
275 $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>"; //fetched from database
276
277 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
278
279 $fs = get_file_storage();
280
8ab7bb9f 281 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
bbd0e548
DW
282
283 foreach ($fsfiles as $file) {
284 $files[$file->get_filename()] = $file;
285 }
286 }
287
288 return $files;
289 }
290
291 /**
292 * Display the saved text content from the editor in the view table
293 *
294 * @param stdClass $submission
295 * @return string
296 */
297 public function view(stdClass $submission) {
298 $result = '';
299
300 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
301
302
303 if ($onlinetextsubmission) {
304
305 // render for portfolio API
8ab7bb9f 306 $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
bbd0e548
DW
307
308 }
309
310 return $result;
311 }
312
313 /**
314 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
315 *
316 * @param string $type old assignment subtype
317 * @param int $version old assignment version
318 * @return bool True if upgrade is possible
319 */
320 public function can_upgrade($type, $version) {
321 if ($type == 'online' && $version >= 2011112900) {
322 return true;
323 }
324 return false;
325 }
326
327
328 /**
329 * Upgrade the settings from the old assignment to the new plugin based one
330 *
331 * @param context $oldcontext - the database for the old assignment context
332 * @param stdClass $oldassignment - the database for the old assignment instance
333 * @param string $log record log events here
334 * @return bool Was it a success?
335 */
336 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
337 // first upgrade settings (nothing to do)
338 return true;
339 }
340
341 /**
342 * Upgrade the submission from the old assignment to the new one
343 *
344 * @param context $oldcontext - the database for the old assignment context
345 * @param stdClass $oldassignment The data record for the old assignment
346 * @param stdClass $oldsubmission The data record for the old submission
347 * @param stdClass $submission The data record for the new submission
348 * @param string $log Record upgrade messages in the log
349 * @return bool true or false - false will trigger a rollback
350 */
351 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
352 global $DB;
353
354 $onlinetextsubmission = new stdClass();
355 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
356 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
357
358 $onlinetextsubmission->submission = $submission->id;
359 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
d82bb44d
DW
360
361 if ($onlinetextsubmission->onlinetext === null) {
362 $onlinetextsubmission->onlinetext = '';
363 }
364
365 if ($onlinetextsubmission->onlineformat === null) {
366 $onlinetextsubmission->onlineformat = editors_get_preferred_format();
367 }
368
bbd0e548
DW
369 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
370 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
371 return false;
372 }
373
374 // now copy the area files
375 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
376 'mod_assignment',
377 'submission',
378 $oldsubmission->id,
379 // New file area
380 $this->assignment->get_context()->id,
381 'assignsubmission_onlinetext',
8ab7bb9f 382 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
bbd0e548
DW
383 $submission->id);
384 return true;
385 }
386
387 /**
388 * Formatting for log info
389 *
390 * @param stdClass $submission The new submission
391 * @return string
392 */
393 public function format_for_log(stdClass $submission) {
394 // format the info for each submission plugin add_to_log
395 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
396 $onlinetextloginfo = '';
397 $text = format_text($onlinetextsubmission->onlinetext,
398 $onlinetextsubmission->onlineformat,
399 array('context'=>$this->assignment->get_context()));
400 $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
401
402 return $onlinetextloginfo;
403 }
404
405 /**
406 * The assignment has been deleted - cleanup
407 *
408 * @return bool
409 */
410 public function delete_instance() {
411 global $DB;
412 // will throw exception on failure
413 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
414
415 return true;
416 }
417
418 /**
419 * No text is set for this plugin
420 *
421 * @param stdClass $submission
422 * @return bool
423 */
424 public function is_empty(stdClass $submission) {
7a9fd6da
DW
425 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
426
427 return empty($onlinetextsubmission->onlinetext);
bbd0e548
DW
428 }
429
430 /**
431 * Get file areas returns a list of areas this plugin stores files
432 * @return array - An array of fileareas (keys) and descriptions (values)
433 */
434 public function get_file_areas() {
8ab7bb9f 435 return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
bbd0e548
DW
436 }
437
438}
439
440