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