5f2af3ac640ad41b72d9e7adeff20434744ae777
[moodle.git] / mod / assignment / type / online / assignment.class.php
1 <?php
2 require_once($CFG->libdir.'/formslib.php');
3 require_once($CFG->dirroot . '/mod/assignment/lib.php');
4 /**
5  * Extend the base assignment class for assignments where you upload a single file
6  *
7  */
8 class assignment_online extends assignment_base {
10     function assignment_online($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
11         parent::assignment_base($cmid, $assignment, $cm, $course);
12         $this->type = 'online';
13     }
15     function view() {
17         global $OUTPUT;
19         $edit  = optional_param('edit', 0, PARAM_BOOL);
20         $saved = optional_param('saved', 0, PARAM_BOOL);
22         $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
23         require_capability('mod/assignment:view', $context);
25         $submission = $this->get_submission();
27         //Guest can not submit nor edit an assignment (bug: 4604)
28         if (!has_capability('mod/assignment:submit', $context)) {
29             $editable = null;
30         } else {
31             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
32         }
33         $editmode = ($editable and $edit);
35         if ($editmode) {
36             //guest can not edit or submit assignment
37             if (!has_capability('mod/assignment:submit', $context)) {
38                 print_error('guestnosubmit', 'assignment');
39             }
40         }
42         add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
44 /// prepare form and process submitted data
45         $mformdata = new stdClass;
46         $mformdata->context = $this->context;
47         $mformdata->maxbytes = $this->course->maxbytes;
48         $mformdata->submission = $submission;
49         $mform = new mod_assignment_online_edit_form(null, $mformdata);
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;
59                 $defaults->text   = format_text($submission->data1, $submission->data2, $options);
60                 $defaults->format = FORMAT_HTML;
61             } else {
62                 $defaults->text   = clean_text($submission->data1, $submission->data2);
63                 $defaults->format = $submission->data2;
64             }
65         }
66         $mform->set_data($defaults);
68         if ($mform->is_cancelled()) {
69             redirect('view.php?id='.$this->cm->id);
70         }
72         if ($data = $mform->get_data()) {      // No incoming data?
73             if ($editable && $this->update_submission($data)) {
74                 //TODO fix log actions - needs db upgrade
75                 $submission = $this->get_submission();
76                 add_to_log($this->course->id, 'assignment', 'upload',
77                         'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
78                 $this->email_teachers($submission);
79                 //redirect to get updated submission date and word count
80                 redirect('view.php?id='.$this->cm->id.'&saved=1');
81             } else {
82                 // TODO: add better error message
83                 echo $OUTPUT->notification(get_string("error")); //submitting not allowed!
84             }
85         }
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         }
94         $this->view_intro();
96         $this->view_dates();
98         if ($saved) {
99             echo $OUTPUT->notification(get_string('submissionsaved', 'assignment'), 'notifysuccess');
100         }
102         if (has_capability('mod/assignment:submit', $context)) {
103             if ($editmode) {
104                 echo $OUTPUT->box_start('generalbox', 'online');
105                 $mform->display();
106             } else {
107                 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter', 'online');
108                 if ($submission && has_capability('mod/assignment:exportownsubmission', $this->context)) {
109                     $text = file_rewrite_pluginfile_urls($submission->data1, 'pluginfile.php', $this->context->id, 'assignment_online_submission', $submission->id);
110                     echo format_text($text, $submission->data2);
111                     $button = new portfolio_add_button();
112                     $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php');
113                     $fs = get_file_storage();
114                     if ($files = $fs->get_area_files($this->context->id, 'assignment_online_submission', $submission->id, "timemodified", false)) {
115                         $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
116                     } else {
117                         $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
118                     }
119                     $button->render();
120                 } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604
121                     echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>';
122                 } else if ($this->isopen()){    //fix for #4206
123                     echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>';
124                 }
125             }
126             echo $OUTPUT->box_end();
127             if (!$editmode && $editable) {
128                 echo "<div style='text-align:center'>";
129                 echo $OUTPUT->single_button(new moodle_url('view.php', array('id'=>$this->cm->id, 'edit'=>'1')), get_string('editmysubmission', 'assignment'));
130                 echo "</div>";
131             }
133         }
135         $this->view_feedback();
137         $this->view_footer();
138     }
140     /*
141      * Display the assignment dates
142      */
143     function view_dates() {
144         global $USER, $CFG, $OUTPUT;
146         if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
147             return;
148         }
150         echo $OUTPUT->box_start('generalbox boxaligncenter', 'dates');
151         echo '<table>';
152         if ($this->assignment->timeavailable) {
153             echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>';
154             echo '    <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>';
155         }
156         if ($this->assignment->timedue) {
157             echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>';
158             echo '    <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>';
159         }
160         $submission = $this->get_submission($USER->id);
161         if ($submission) {
162             echo '<tr><td class="c0">'.get_string('lastedited').':</td>';
163             echo '    <td class="c1">'.userdate($submission->timemodified);
164         /// Decide what to count
165             if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
166                 echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>';
167             } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
168                 echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>';
169             }
170         }
171         echo '</table>';
172         echo $OUTPUT->box_end();
173     }
175     function update_submission($data) {
176         global $CFG, $USER, $DB;
178         $submission = $this->get_submission($USER->id, true);
180         $update = new object();
181         $update->id           = $submission->id;
182         $update->data1        = $data->text;
183         $update->data2        = $data->format;
184         $update->timemodified = time();
186         $DB->update_record('assignment_submissions', $update);
188         $submission = $this->get_submission($USER->id);
189         $this->update_grade($submission);
190         return true;
191     }
194     function print_student_answer($userid, $return=false){
195         global $OUTPUT;
196         if (!$submission = $this->get_submission($userid)) {
197             return '';
198         }
200         $link = new moodle_url("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}");
201         $action = new popup_action('click', $link, 'file'.$userid, array('height' => 450, 'width' => 580));
202         $popup = $OUTPUT->action_link($link, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), $action, array('title'=>get_string('submission', 'assignment')));
204         $output = '<div class="files">'.
205                   '<img src="'.$OUTPUT->pix_url('f/html') . '" class="icon" alt="html" />'.
206                   $popup .
207                   '</div>';
208                   return $output;
209     }
211     function print_user_files($userid, $return=false) {
212         global $OUTPUT, $CFG;
214         if (!$submission = $this->get_submission($userid)) {
215             return '';
216         }
218         $link = new moodle_url("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}");
219         $action = new popup_action('click', $link, 'file'.$userid, array('height' => 450, 'width' => 580));
220         $popup = $OUTPUT->action_link($link, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), $action, array('title'=>get_string('submission', 'assignment')));
222         $output = '<div class="files">'.
223                   '<img align="middle" src="'.$OUTPUT->pix_url('f/html') . '" height="16" width="16" alt="html" />'.
224                   $popup .
225                   '</div>';
227         ///Stolen code from file.php
229         echo $OUTPUT->box_start('generalbox boxaligncenter', 'wordcount');
230     /// Decide what to count
231         if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
232             echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')';
233         } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
234             echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')';
235         }
236         echo $OUTPUT->box_end();
237         echo $OUTPUT->box(format_text($submission->data1, $submission->data2), 'generalbox boxaligncenter boxwidthwide');
239         ///End of stolen code from file.php
241         if ($return) {
242             //return $output;
243         }
244         //echo $output;
245     }
247     function preprocess_submission(&$submission) {
248         if ($this->assignment->var1 && empty($submission->submissioncomment)) {  // comment inline
249             if ($this->usehtmleditor) {
250                 // Convert to html, clean & copy student data to teacher
251                 $submission->submissioncomment = format_text($submission->data1, $submission->data2);
252                 $submission->format = FORMAT_HTML;
253             } else {
254                 // Copy student data to teacher
255                 $submission->submissioncomment = $submission->data1;
256                 $submission->format = $submission->data2;
257             }
258         }
259     }
261     function setup_elements(&$mform) {
262         global $CFG, $COURSE;
264         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
266         $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions);
267         $mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment'));
268         $mform->setDefault('resubmit', 0);
270         $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
271         $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
272         $mform->setDefault('emailteachers', 0);
274         $mform->addElement('select', 'var1', get_string("commentinline", "assignment"), $ynoptions);
275         $mform->setHelpButton('var1', array('commentinline', get_string('commentinline', 'assignment'), 'assignment'));
276         $mform->setDefault('var1', 0);
278     }
280     function portfolio_exportable() {
281         return true;
282     }
284     function portfolio_load_data($caller) {
285         $submission = $this->get_submission();
286         $fs = get_file_storage();
287         if ($files = $fs->get_area_files($this->context->id, 'assignment_online_submission', $submission->id, "timemodified", false)) {
288             $caller->set('multifiles', $files);
289         }
290     }
292     function portfolio_get_sha1($caller) {
293         $submission = $this->get_submission();
294         $textsha1 = sha1(format_text($submission->data1, $submission->data2));
295         $filesha1 = '';
296         try {
297             $filesha1 = $caller->get_sha1_file();
298         } catch (portfolio_caller_exception $e) {} // no files
299         return sha1($textsha1 . $filesha1);
300     }
302     function portfolio_prepare_package($exporter, $user) {
303         $submission = $this->get_submission($user->id);
304         $html = format_text($submission->data1, $submission->data2);
305         $html = portfolio_rewrite_pluginfile_urls($html, $this->context->id, 'assignment_online_submission', $submission->id, $exporter->get('format'));
306         if (in_array($exporter->get('formatclass'), array(PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_RICHHTML))) {
307             if ($files = $exporter->get('caller')->get('multifiles')) {
308                 foreach ($files as $f) {
309                     $exporter->copy_existing_file($file);
310                 }
311             }
312             return $exporter->write_new_file($html, 'assignment.html', !empty($files));
313         } else if ($exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
314             $leapwriter = $exporter->get('format')->leap2a_writer();
315             $entry = new portfolio_format_leap2a_entry('assignmentonline' . $this->assignment->id, $this->assignment->name, 'resource', $html);
316             $entry->add_category('web', 'resource_type');
317             $entry->published = $submission->timecreated;
318             $entry->updated = $submission->timemodified;
319             $entry->author = $user;
320             $leapwriter->add_entry($entry);
321             if ($files = $exporter->get('caller')->get('multifiles')) {
322                 foreach ($files as $f) {
323                     $exporter->copy_existing_file($f);
324                     $entry->add_attachment($f);
325                 }
326             }
327             $exporter->write_new_file($leapwriter->to_xml(), $exporter->get('format')->manifest_name(), true);
328         } else {
329             debugging('invalid format class: ' . $exporter->get('formatclass'));
330         }
331     }
333     function extend_settings_navigation($node) {
334         global $PAGE, $CFG, $USER;
336         // get users submission if there is one
337         $submission = $this->get_submission();
338         if (has_capability('mod/assignment:submit', $PAGE->cm->context)) {
339             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
340         } else {
341             $editable = false;
342         }
344         // If the user has submitted something add a bit more stuff
345         if ($submission) {
346             // Add a view link to the settings nav
347             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$PAGE->cm->id));
348             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
350             if (!empty($submission->timemodified)) {
351                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
352                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
353                 $submittednode->add_class('note');
354                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
355                     $submittednode->add_class('early');
356                 } else {
357                     $submittednode->add_class('late');
358                 }
359             }
360         }
362         if (!$submission || $editable) {
363             // If this assignment is editable once submitted add an edit link to the settings nav
364             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$PAGE->cm->id, 'edit'=>1, 'sesskey'=>sesskey()));
365             $node->add(get_string('editmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
366         }
367     }
369     public function send_file($filearea, $args) {
370         global $USER;
371         require_capability('mod/assignment:view', $this->context);
373         $fullpath = $this->context->id.$filearea.implode('/', $args);
375         $fs = get_file_storage();
376         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
377             send_file_not_found();
378         }
380         if (($USER->id != $file->get_userid()) && !has_capability('mod/assignment:grade', $this->context)) {
381             send_file_not_found();
382         }
384         session_get_instance()->write_close(); // unlock session during fileserving
385         send_stored_file($file, 60*60, 0, true);
386     }
389 class mod_assignment_online_edit_form extends moodleform {
391     public function set_data($data) {
392         $editoroptions = $this->get_editor_options();
393         if (!isset($data->text)) {
394             $data->text = '';
395         }
396         if (!isset($data->format)) {
397             $data->textformat = FORMAT_HTML;
398         } else {
399             $data->textformat = $data->format;
400         }
402         if (!empty($this->_customdata->submission->id)) {
403             $itemid = $this->_customdata->submission->id;
404         } else {
405             $itemid = null;
406         }
408         $data = file_prepare_standard_editor($data, 'text', $editoroptions, $this->_customdata->context, $editoroptions['filearea'], $itemid);
409         return parent::set_data($data);
410     }
412     public function get_data() {
413         $data = parent::get_data();
415         if (!empty($this->_customdata->submission->id)) {
416             $itemid = $this->_customdata->submission->id;
417         } else {
418             $itemid = null;
419         }
421         if ($data) {
422             $editoroptions = $this->get_editor_options();
423             $data = file_postupdate_standard_editor($data, 'text', $editoroptions, $this->_customdata->context, $editoroptions['filearea'], $itemid);
424             $data->format = $data->textformat;
425         }
426         return $data;
427     }
429     function definition() {
430         $mform =& $this->_form;
432         // visible elements
433         $mform->addElement('editor', 'text_editor', get_string('submission', 'assignment'), null, $this->get_editor_options());
434         $mform->setType('text_editor', PARAM_RAW); // to be cleaned before display
435         $mform->setHelpButton('text_editor', array('reading', 'writing', 'richtext2'), false, 'editorhelpbutton');
436         $mform->addRule('text_editor', get_string('required'), 'required', null, 'client');
438         // hidden params
439         $mform->addElement('hidden', 'id', 0);
440         $mform->setType('id', PARAM_INT);
442         // buttons
443         $this->add_action_buttons();
444     }
446     protected function get_editor_options() {
447         $editoroptions = array();
448         $editoroptions['filearea'] = 'assignment_online_submission';
449         $editoroptions['noclean'] = false;
450         $editoroptions['maxfiles'] = EDITOR_UNLIMITED_FILES;
451         $editoroptions['maxbytes'] = $this->_customdata->maxbytes;
452         return $editoroptions;
453     }