MDL-31270 mod_assign: introducing the assignment module
[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 */
31define('ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT', 'submissions_onlinetext');
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
94 $data = file_prepare_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT, $submissionid);
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,
109 'context' => $this->assignment->get_context()
110 );
111 return $editoroptions;
112 }
113
114 /**
115 * Save data to the database
116 *
117 * @param stdClass $submission
118 * @param stdClass $data
119 * @return bool
120 */
121 public function save(stdClass $submission, stdClass $data) {
122 global $DB;
123
124 $editoroptions = $this->get_edit_options();
125
126 $data = file_postupdate_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT, $submission->id);
127
128 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
129 if ($onlinetextsubmission) {
130
131 $onlinetextsubmission->onlinetext = $data->onlinetext;
132 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
133
134
135 return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
136 } else {
137
138 $onlinetextsubmission = new stdClass();
139 $onlinetextsubmission->onlinetext = $data->onlinetext;
140 $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
141
142 $onlinetextsubmission->submission = $submission->id;
143 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
144 return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
145 }
146
147
148 }
149
150 /**
151 * Get the saved text content from the editor
152 *
153 * @param string $name
154 * @param int $submissionid
155 * @return string
156 */
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;
162 }
163 }
164
165 return '';
166 }
167
168 /**
169 * Get the content format for the editor
170 *
171 * @param string $name
172 * @param int $submissionid
173 * @return int
174 */
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;
180 }
181 }
182
183
184 return 0;
185 }
186
187
188 /**
189 * Display onlinetext word count in the submission status table
190 *
191 * @param stdClass $submission
192 * @param bool $showviewlink - If the summary has been truncated set this to true
193 * @return string
194 */
195 public function view_summary(stdClass $submission, $showviewlink) {
196
197 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
198 // always show the view link
199 $showviewlink = true;
200
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));
206 } else {
207 return $shorttext;
208 }
209 }
210 return '';
211 }
212
213 /**
214 * Produce a list of files suitable for export that represent this submission
215 *
216 * @param stdClass $submission - For this is the submission data
217 * @return array - return an array of files indexed by filename
218 */
219 public function get_files(stdClass $submission) {
220 global $DB;
221 $files = array();
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);
225
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
229
230 $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
231
232 $fs = get_file_storage();
233
234 $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT, $submission->id, "timemodified", false);
235
236 foreach ($fsfiles as $file) {
237 $files[$file->get_filename()] = $file;
238 }
239 }
240
241 return $files;
242 }
243
244 /**
245 * Display the saved text content from the editor in the view table
246 *
247 * @param stdClass $submission
248 * @return string
249 */
250 public function view(stdClass $submission) {
251 $result = '';
252
253 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
254
255
256 if ($onlinetextsubmission) {
257
258 // render for portfolio API
259 $result .= $this->assignment->render_editor_content(ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
260
261 }
262
263 return $result;
264 }
265
266 /**
267 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
268 *
269 * @param string $type old assignment subtype
270 * @param int $version old assignment version
271 * @return bool True if upgrade is possible
272 */
273 public function can_upgrade($type, $version) {
274 if ($type == 'online' && $version >= 2011112900) {
275 return true;
276 }
277 return false;
278 }
279
280
281 /**
282 * Upgrade the settings from the old assignment to the new plugin based one
283 *
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?
288 */
289 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
290 // first upgrade settings (nothing to do)
291 return true;
292 }
293
294 /**
295 * Upgrade the submission from the old assignment to the new one
296 *
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
303 */
304 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
305 global $DB;
306
307 $onlinetextsubmission = new stdClass();
308 $onlinetextsubmission->onlinetext = $oldsubmission->data1;
309 $onlinetextsubmission->onlineformat = $oldsubmission->data2;
310
311 $onlinetextsubmission->submission = $submission->id;
312 $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
313 if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
314 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
315 return false;
316 }
317
318 // now copy the area files
319 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
320 'mod_assignment',
321 'submission',
322 $oldsubmission->id,
323 // New file area
324 $this->assignment->get_context()->id,
325 'assignsubmission_onlinetext',
326 ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT,
327 $submission->id);
328 return true;
329 }
330
331 /**
332 * Formatting for log info
333 *
334 * @param stdClass $submission The new submission
335 * @return string
336 */
337 public function format_for_log(stdClass $submission) {
338 // format the info for each submission plugin add_to_log
339 $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
340 $onlinetextloginfo = '';
341 $text = format_text($onlinetextsubmission->onlinetext,
342 $onlinetextsubmission->onlineformat,
343 array('context'=>$this->assignment->get_context()));
344 $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
345
346 return $onlinetextloginfo;
347 }
348
349 /**
350 * The assignment has been deleted - cleanup
351 *
352 * @return bool
353 */
354 public function delete_instance() {
355 global $DB;
356 // will throw exception on failure
357 $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
358
359 return true;
360 }
361
362 /**
363 * No text is set for this plugin
364 *
365 * @param stdClass $submission
366 * @return bool
367 */
368 public function is_empty(stdClass $submission) {
369 return $this->view($submission) == '';
370 }
371
372 /**
373 * Get file areas returns a list of areas this plugin stores files
374 * @return array - An array of fileareas (keys) and descriptions (values)
375 */
376 public function get_file_areas() {
377 return array(ASSIGN_FILEAREA_SUBMISSION_ONLINETEXT=>$this->get_name());
378 }
379
380}
381
382