Commit | Line | Data |
---|---|---|
e7521559 | 1 | <?php |
6cb1b93e | 2 | require_once($CFG->libdir.'/formslib.php'); |
67a87e7d | 3 | require_once($CFG->dirroot . '/mod/assignment/lib.php'); |
f77cfb73 | 4 | /** |
5 | * Extend the base assignment class for assignments where you upload a single file | |
6 | * | |
7 | */ | |
8 | class assignment_online extends assignment_base { | |
9 | ||
7bddd4b7 | 10 | function assignment_online($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) { |
11 | parent::assignment_base($cmid, $assignment, $cm, $course); | |
0b5a80a1 | 12 | $this->type = 'online'; |
f77cfb73 | 13 | } |
14 | ||
f77cfb73 | 15 | function view() { |
16 | ||
007772d6 | 17 | global $OUTPUT; |
6cb1b93e | 18 | |
19 | $edit = optional_param('edit', 0, PARAM_BOOL); | |
20 | $saved = optional_param('saved', 0, PARAM_BOOL); | |
21 | ||
22 | $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); | |
0468976c | 23 | require_capability('mod/assignment:view', $context); |
6cb1b93e | 24 | |
61240489 | 25 | $submission = $this->get_submission(); |
1648afb2 | 26 | |
2eac7ec1 | 27 | //Guest can not submit nor edit an assignment (bug: 4604) |
be07f21b | 28 | if (!has_capability('mod/assignment:submit', $context)) { |
2eac7ec1 | 29 | $editable = null; |
6cb1b93e | 30 | } else { |
2eac7ec1 | 31 | $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked); |
32 | } | |
6cb1b93e | 33 | $editmode = ($editable and $edit); |
f77cfb73 | 34 | |
35 | if ($editmode) { | |
f7de5478 | 36 | //guest can not edit or submit assignment |
be07f21b | 37 | if (!has_capability('mod/assignment:submit', $context)) { |
cb59ca28 | 38 | print_error('guestnosubmit', 'assignment'); |
f7de5478 | 39 | } |
f77cfb73 | 40 | } |
41 | ||
cb59ca28 | 42 | add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id); |
43 | ||
6cb1b93e | 44 | /// prepare form and process submitted data |
c561f44c | 45 | $mformdata = new stdClass; |
c561f44c | 46 | $mformdata->context = $this->context; |
9e6b608f | 47 | $mformdata->maxbytes = $this->course->maxbytes; |
007772d6 | 48 | $mformdata->submission = $submission; |
c561f44c | 49 | $mform = new mod_assignment_online_edit_form(null, $mformdata); |
f77cfb73 | 50 | |
6cb1b93e | 51 | $defaults = new object(); |
52 | $defaults->id = $this->cm->id; | |
53 | if (!empty($submission)) { | |
54 | if ($this->usehtmleditor) { | |
55 | $options = new object(); | |
56 | $options->smiley = false; | |
57 | $options->filter = false; | |
f77cfb73 | 58 | |
6cb1b93e | 59 | $defaults->text = format_text($submission->data1, $submission->data2, $options); |
60 | $defaults->format = FORMAT_HTML; | |
61 | } else { | |
62 | $defaults->text = $submission->data1; | |
63 | $defaults->format = $submission->data2; | |
64 | } | |
65 | } | |
beac4717 | 66 | $mform->set_data($defaults); |
6cb1b93e | 67 | |
5571b5f4 | 68 | if ($mform->is_cancelled()) { |
69 | redirect('view.php?id='.$this->cm->id); | |
70 | } | |
71 | ||
beac4717 | 72 | if ($data = $mform->get_data()) { // No incoming data? |
f0a99707 | 73 | if ($editable && $this->update_submission($data)) { |
ca4fb814 | 74 | //TODO fix log actions - needs db upgrade |
418db328 | 75 | $submission = $this->get_submission(); |
6cb1b93e | 76 | add_to_log($this->course->id, 'assignment', 'upload', |
9bf660b3 | 77 | 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); |
78 | $this->email_teachers($submission); | |
3e7bf58a | 79 | //redirect to get updated submission date and word count |
80 | redirect('view.php?id='.$this->cm->id.'&saved=1'); | |
f0a99707 | 81 | } else { |
82 | // TODO: add better error message | |
3a003ead | 83 | echo $OUTPUT->notification(get_string("error")); //submitting not allowed! |
f77cfb73 | 84 | } |
6cb1b93e | 85 | } |
86 | ||
87 | /// print header, etc. and display form if needed | |
88 | if ($editmode) { | |
89 | $this->view_header(get_string('editmysubmission', 'assignment')); | |
90 | } else { | |
91 | $this->view_header(); | |
92 | } | |
93 | ||
94 | $this->view_intro(); | |
95 | ||
96 | $this->view_dates(); | |
97 | ||
98 | if ($saved) { | |
3a003ead | 99 | echo $OUTPUT->notification(get_string('submissionsaved', 'assignment'), 'notifysuccess'); |
3e7bf58a | 100 | } |
f77cfb73 | 101 | |
61240489 | 102 | if (has_capability('mod/assignment:submit', $context)) { |
61240489 | 103 | if ($editmode) { |
01e0da58 | 104 | echo $OUTPUT->box_start('generalbox', 'online'); |
6cb1b93e | 105 | $mform->display(); |
61240489 | 106 | } else { |
01e0da58 | 107 | echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter', 'online'); |
728e1931 | 108 | if ($submission && has_capability('mod/assignment:exportownsubmission', $this->context)) { |
007772d6 | 109 | $text = file_rewrite_pluginfile_urls($submission->data1, 'pluginfile.php', $this->context->id, 'assignment_online_submission', $submission->id); |
c561f44c | 110 | echo format_text($text, $submission->data2); |
0d06b6fd | 111 | $button = new portfolio_add_button(); |
5fb29115 | 112 | $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php'); |
59dd457e | 113 | $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML); //TODO this might have files? |
0d06b6fd | 114 | $button->render(); |
61240489 | 115 | } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604 |
d9cb14b8 | 116 | echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>'; |
61240489 | 117 | } else if ($this->isopen()){ //fix for #4206 |
d9cb14b8 | 118 | echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>'; |
61240489 | 119 | } |
61240489 | 120 | } |
01e0da58 | 121 | echo $OUTPUT->box_end(); |
12dce253 | 122 | if (!$editmode && $editable) { |
123 | echo "<div style='text-align:center'>"; | |
75d47fa9 | 124 | echo $OUTPUT->button(html_form::make_button('view.php', array('id'=>$this->cm->id,'edit'=>'1'), |
125 | get_string('editmysubmission', 'assignment'))); | |
12dce253 | 126 | echo "</div>"; |
127 | } | |
6cb1b93e | 128 | |
61240489 | 129 | } |
f77cfb73 | 130 | |
131 | $this->view_feedback(); | |
132 | ||
133 | $this->view_footer(); | |
134 | } | |
135 | ||
3e7bf58a | 136 | /* |
137 | * Display the assignment dates | |
138 | */ | |
139 | function view_dates() { | |
3a003ead | 140 | global $USER, $CFG, $OUTPUT; |
3e7bf58a | 141 | |
142 | if (!$this->assignment->timeavailable && !$this->assignment->timedue) { | |
143 | return; | |
144 | } | |
145 | ||
3a003ead | 146 | echo $OUTPUT->box_start('generalbox boxaligncenter', 'dates'); |
3e7bf58a | 147 | echo '<table>'; |
148 | if ($this->assignment->timeavailable) { | |
149 | echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>'; | |
150 | echo ' <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>'; | |
151 | } | |
152 | if ($this->assignment->timedue) { | |
153 | echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>'; | |
154 | echo ' <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>'; | |
155 | } | |
156 | $submission = $this->get_submission($USER->id); | |
157 | if ($submission) { | |
158 | echo '<tr><td class="c0">'.get_string('lastedited').':</td>'; | |
159 | echo ' <td class="c1">'.userdate($submission->timemodified); | |
1884f2a6 | 160 | /// Decide what to count |
161 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { | |
162 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>'; | |
163 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { | |
164 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>'; | |
165 | } | |
3e7bf58a | 166 | } |
167 | echo '</table>'; | |
3a003ead | 168 | echo $OUTPUT->box_end(); |
3e7bf58a | 169 | } |
170 | ||
f77cfb73 | 171 | function update_submission($data) { |
74d7d735 | 172 | global $CFG, $USER, $DB; |
f77cfb73 | 173 | |
174 | $submission = $this->get_submission($USER->id, true); | |
175 | ||
6cb1b93e | 176 | $update = new object(); |
177 | $update->id = $submission->id; | |
178 | $update->data1 = $data->text; | |
179 | $update->data2 = $data->format; | |
0cbc1f11 | 180 | $update->timemodified = time(); |
f77cfb73 | 181 | |
9d749339 | 182 | $DB->update_record('assignment_submissions', $update); |
12dce253 | 183 | |
184 | $submission = $this->get_submission($USER->id); | |
185 | $this->update_grade($submission); | |
186 | return true; | |
f77cfb73 | 187 | } |
188 | ||
189 | ||
9bf660b3 | 190 | function print_student_answer($userid, $return=false){ |
f2a1963c | 191 | global $OUTPUT; |
9bf660b3 | 192 | if (!$submission = $this->get_submission($userid)) { |
193 | return ''; | |
194 | } | |
e7521559 | 195 | |
47c96a77 | 196 | $link = html_link::make("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}", shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15)); |
a9573c08 | 197 | $link->add_action(new popup_action('click', $link->url, 'file'.$userid, array('height' => 450, 'width' => 580))); |
198 | $link->title = get_string('submission', 'assignment'); | |
e7521559 PS |
199 | $popup = $OUTPUT->link($link); |
200 | ||
9bf660b3 | 201 | $output = '<div class="files">'. |
b5d0cafc | 202 | '<img src="'.$OUTPUT->pix_url('f/html') . '" class="icon" alt="html" />'. |
a9573c08 | 203 | $popup . |
9bf660b3 | 204 | '</div>'; |
205 | return $output; | |
206 | } | |
6cb1b93e | 207 | |
f77cfb73 | 208 | function print_user_files($userid, $return=false) { |
47c96a77 | 209 | global $OUTPUT, $CFG; |
6cb1b93e | 210 | |
f77cfb73 | 211 | if (!$submission = $this->get_submission($userid)) { |
212 | return ''; | |
213 | } | |
e7521559 | 214 | |
47c96a77 | 215 | $link = html_link::make("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}", shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15)); |
a9573c08 | 216 | $link->add_action(new popup_action('click', $link->url, 'file'.$userid, array('height' => 450, 'width' => 580))); |
217 | $link->title = get_string('submission', 'assignment'); | |
e7521559 | 218 | $popup = $OUTPUT->link($link); |
6cb1b93e | 219 | |
f77cfb73 | 220 | $output = '<div class="files">'. |
b5d0cafc | 221 | '<img align="middle" src="'.$OUTPUT->pix_url('f/html') . '" height="16" width="16" alt="html" />'. |
a9573c08 | 222 | $popup . |
f77cfb73 | 223 | '</div>'; |
224 | ||
9bf660b3 | 225 | ///Stolen code from file.php |
6cb1b93e | 226 | |
3a003ead | 227 | echo $OUTPUT->box_start('generalbox boxaligncenter', 'wordcount'); |
63d69d54 | 228 | /// Decide what to count |
229 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { | |
230 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')'; | |
231 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { | |
232 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')'; | |
233 | } | |
3a003ead | 234 | echo $OUTPUT->box_end(); |
235 | echo $OUTPUT->box(format_text($submission->data1, $submission->data2), 'generalbox boxaligncenter boxwidthwide'); | |
6cb1b93e | 236 | |
9bf660b3 | 237 | ///End of stolen code from file.php |
6cb1b93e | 238 | |
f77cfb73 | 239 | if ($return) { |
9bf660b3 | 240 | //return $output; |
f77cfb73 | 241 | } |
9bf660b3 | 242 | //echo $output; |
f77cfb73 | 243 | } |
244 | ||
01e2fdfe | 245 | function preprocess_submission(&$submission) { |
ea6432fe | 246 | if ($this->assignment->var1 && empty($submission->submissioncomment)) { // comment inline |
47263937 | 247 | if ($this->usehtmleditor) { |
248 | // Convert to html, clean & copy student data to teacher | |
ea6432fe | 249 | $submission->submissioncomment = format_text($submission->data1, $submission->data2); |
6cb1b93e | 250 | $submission->format = FORMAT_HTML; |
47263937 | 251 | } else { |
252 | // Copy student data to teacher | |
ea6432fe | 253 | $submission->submissioncomment = $submission->data1; |
6cb1b93e | 254 | $submission->format = $submission->data2; |
47263937 | 255 | } |
01e2fdfe | 256 | } |
257 | } | |
258 | ||
436cfa9f | 259 | function setup_elements(&$mform) { |
260 | global $CFG, $COURSE; | |
261 | ||
262 | $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes')); | |
263 | ||
264 | $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions); | |
265 | $mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment')); | |
266 | $mform->setDefault('resubmit', 0); | |
267 | ||
268 | $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions); | |
269 | $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment')); | |
270 | $mform->setDefault('emailteachers', 0); | |
271 | ||
272 | $mform->addElement('select', 'var1', get_string("commentinline", "assignment"), $ynoptions); | |
273 | $mform->setHelpButton('var1', array('commentinline', get_string('commentinline', 'assignment'), 'assignment')); | |
274 | $mform->setDefault('var1', 0); | |
275 | ||
276 | } | |
277 | ||
67a87e7d | 278 | function portfolio_exportable() { |
279 | return true; | |
280 | } | |
ffcfd8a7 | 281 | |
eb32c6ca | 282 | function portfolio_get_sha1($userid=0) { |
283 | $submission = $this->get_submission($userid); | |
ffcfd8a7 | 284 | return sha1(format_text($submission->data1, $submission->data2)); |
285 | } | |
286 | ||
eb32c6ca | 287 | function portfolio_prepare_package($exporter, $userid=0) { |
288 | $submission = $this->get_submission($userid); | |
59dd457e PL |
289 | $html = format_text($submission->data1, $submission->data2); |
290 | if ($exporter->get('formatclass') == PORTFOLIO_FORMAT_PLAINHTML) { | |
291 | return $exporter->write_new_file($html, 'assignment.html', false); | |
292 | } else if ($exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) { | |
293 | $leapwriter = $exporter->get('format')->leap2a_writer(); | |
294 | $entry = new portfolio_format_leap2a_entry('assignmentonline' . $this->assignment->id, $this->assignment->name, 'resource', $html); // TODO entry? | |
295 | $entry->add_category('web', 'resource_type'); | |
296 | $leapwriter->add_entry($entry); | |
297 | return $exporter->write_new_file($leapwriter->to_xml(), $exporter->get('format')->manifest_name(), true); | |
298 | //TODO attached files?! | |
299 | } else { | |
300 | die('wtf ;' . $exporter->get('formatclass')); | |
301 | } | |
ea0de12f | 302 | } |
47c96a77 | 303 | |
304 | function extend_settings_navigation($node) { | |
305 | global $PAGE, $CFG, $USER; | |
306 | ||
307 | // get users submission if there is one | |
308 | $submission = $this->get_submission(); | |
309 | if (has_capability('mod/assignment:submit', $PAGE->cm->context)) { | |
310 | $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked); | |
311 | } else { | |
312 | $editable = false; | |
313 | } | |
314 | ||
315 | // If the user has submitted something add a bit more stuff | |
316 | if ($submission) { | |
317 | // Add a view link to the settings nav | |
318 | $link = new moodle_url($CFG->wwwroot.'/mod/assignment/view.php', array('id'=>$PAGE->cm->id)); | |
319 | $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING); | |
320 | ||
321 | if (!empty($submission->timemodified)) { | |
322 | $key = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified)); | |
323 | $node->get($key)->text = preg_replace('#([^,])\s#', '$1 ', $node->get($key)->text); | |
324 | $node->get($key)->add_class('note'); | |
325 | if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) { | |
326 | $node->get($key)->add_class('early'); | |
327 | } else { | |
328 | $node->get($key)->add_class('late'); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | if (!$submission || $editable) { | |
334 | // If this assignment is editable once submitted add an edit link to the settings nav | |
335 | $link = new moodle_url($CFG->wwwroot.'/mod/assignment/view.php', array('id'=>$PAGE->cm->id, 'edit'=>1, 'sesskey'=>sesskey())); | |
336 | $node->add(get_string('editmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING); | |
337 | } | |
338 | } | |
c561f44c SH |
339 | |
340 | public function send_file($filearea, $args) { | |
ff284987 | 341 | global $USER; |
c561f44c SH |
342 | require_capability('mod/assignment:view', $this->context); |
343 | ||
344 | $fullpath = $this->context->id.$filearea.implode('/', $args); | |
345 | ||
ff284987 | 346 | $fs = get_file_storage(); |
c561f44c SH |
347 | if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { |
348 | send_file_not_found(); | |
349 | } | |
350 | ||
351 | if (($USER->id != $file->get_userid()) && !has_capability('mod/assignment:grade', $this->context)) { | |
352 | send_file_not_found(); | |
353 | } | |
354 | ||
355 | session_get_instance()->write_close(); // unlock session during fileserving | |
356 | send_stored_file($file, 60*60, 0, true); | |
357 | } | |
f77cfb73 | 358 | } |
359 | ||
f07b9627 | 360 | class mod_assignment_online_edit_form extends moodleform { |
c561f44c SH |
361 | |
362 | public function set_data($data) { | |
c561f44c SH |
363 | $editoroptions = $this->get_editor_options(); |
364 | if (!isset($data->text)) { | |
365 | $data->text = ''; | |
366 | } | |
367 | if (!isset($data->format)) { | |
368 | $data->textformat = FORMAT_HTML; | |
369 | } else { | |
370 | $data->textformat = $data->format; | |
371 | } | |
007772d6 SH |
372 | |
373 | if (!empty($this->_customdata->submission->id)) { | |
374 | $itemid = $this->_customdata->submission->id; | |
375 | } else { | |
376 | $itemid = null; | |
377 | } | |
378 | ||
379 | $data = file_prepare_standard_editor($data, 'text', $editoroptions, $this->_customdata->context, $editoroptions['filearea'], $itemid); | |
c561f44c SH |
380 | return parent::set_data($data); |
381 | } | |
382 | ||
383 | public function get_data() { | |
c561f44c | 384 | $data = parent::get_data(); |
007772d6 SH |
385 | |
386 | if (!empty($this->_customdata->submission->id)) { | |
387 | $itemid = $this->_customdata->submission->id; | |
388 | } else { | |
389 | $itemid = null; | |
390 | } | |
391 | ||
c561f44c SH |
392 | if ($data) { |
393 | $editoroptions = $this->get_editor_options(); | |
007772d6 | 394 | $data = file_postupdate_standard_editor($data, 'text', $editoroptions, $this->_customdata->context, $editoroptions['filearea'], $itemid); |
c561f44c SH |
395 | $data->format = $data->textformat; |
396 | } | |
397 | return $data; | |
398 | } | |
399 | ||
6cb1b93e | 400 | function definition() { |
401 | $mform =& $this->_form; | |
402 | ||
403 | // visible elements | |
c561f44c SH |
404 | $mform->addElement('editor', 'text_editor', get_string('submission', 'assignment'), null, $this->get_editor_options()); |
405 | $mform->setType('text_editor', PARAM_RAW); // to be cleaned before display | |
406 | $mform->setHelpButton('text_editor', array('reading', 'writing', 'richtext2'), false, 'editorhelpbutton'); | |
407 | $mform->addRule('text_editor', get_string('required'), 'required', null, 'client'); | |
6cb1b93e | 408 | |
6117edc7 | 409 | // hidden params |
6cb1b93e | 410 | $mform->addElement('hidden', 'id', 0); |
411 | $mform->setType('id', PARAM_INT); | |
412 | ||
413 | // buttons | |
a23f0aaf | 414 | $this->add_action_buttons(); |
6cb1b93e | 415 | } |
c561f44c SH |
416 | |
417 | protected function get_editor_options() { | |
c561f44c SH |
418 | $editoroptions = array(); |
419 | $editoroptions['filearea'] = 'assignment_online_submission'; | |
420 | $editoroptions['noclean'] = false; | |
421 | $editoroptions['maxfiles'] = EDITOR_UNLIMITED_FILES; | |
9e6b608f | 422 | $editoroptions['maxbytes'] = $this->_customdata->maxbytes; |
c561f44c SH |
423 | return $editoroptions; |
424 | } | |
6cb1b93e | 425 | } |
426 | ||
e7521559 | 427 |