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