fixed recent regression + simplified code a bit
[moodle.git] / mod / assignment / type / online / assignment.class.php
1 <?php
2 require_once($CFG->libdir.'/formslib.php');
3 require_once($CFG->libdir . '/portfoliolib.php');
4 require_once($CFG->dirroot . '/mod/assignment/lib.php');
5 /**
6  * Extend the base assignment class for assignments where you upload a single file
7  *
8  */
9 class assignment_online extends assignment_base {
11     function assignment_online($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
12         parent::assignment_base($cmid, $assignment, $cm, $course);
13         $this->type = 'online';
14     }
16     function view() {
17         global $OUTPUT, $CFG, $USER, $PAGE;
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($USER->id, false);
27         //Guest can not submit nor edit an assignment (bug: 4604)
28         if (!has_capability('mod/assignment:submit', $context)) {
29             $editable = false;
30         } else {
31             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
32         }
33         $editmode = ($editable and $edit);
35         if ($editmode) {
36             // prepare form and process submitted data
37             $editoroptions = array('noclean'=>false, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$this->course->maxbytes);
39             $data = new object();
40             $data->id         = $this->cm->id;
41             $data->edit       = 1;
42             if ($submission) {
43                 $data->sid        = $submission->id;
44                 $data->text       = $submission->data1;
45                 $data->textformat = $submission->data2;
46             } else {
47                 $data->sid        = NULL;
48                 $data->text       = '';
49                 $data->textformat = NULL;
50             }
52             $data = file_prepare_standard_editor($data, 'text', $editoroptions, $this->context, 'assignment_online_submission', $data->sid);
54             $mform = new mod_assignment_online_edit_form(null, array($data, $editoroptions));
56             if ($mform->is_cancelled()) {
57                 redirect($PAGE->url);
58             }
60             if ($data = $mform->get_data()) {
61                 $submission = $this->get_submission($USER->id, true); //create the submission if needed & its id
63                 $data = file_postupdate_standard_editor($data, 'text', $editoroptions, $this->context, 'assignment_online_submission', $submission->id);
65                 $submission = $this->update_submission($data);
67                 //TODO fix log actions - needs db upgrade
68                 add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
69                 $this->email_teachers($submission);
71                 //redirect to get updated submission date and word count
72                 redirect(new moodle_url($PAGE->url, array('saved'=>1)));
73             }
74         }
76         add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
78 /// print header, etc. and display form if needed
79         if ($editmode) {
80             $this->view_header(get_string('editmysubmission', 'assignment'));
81         } else {
82             $this->view_header();
83         }
85         $this->view_intro();
87         $this->view_dates();
89         if ($saved) {
90             echo $OUTPUT->notification(get_string('submissionsaved', 'assignment'), 'notifysuccess');
91         }
93         if (has_capability('mod/assignment:submit', $context)) {
94             if ($editmode) {
95                 echo $OUTPUT->box_start('generalbox', 'onlineenter');
96                 $mform->display();
97             } else {
98                 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter', 'online');
99                 if ($submission && has_capability('mod/assignment:exportownsubmission', $this->context)) {
100                     $text = file_rewrite_pluginfile_urls($submission->data1, 'pluginfile.php', $this->context->id, 'assignment_online_submission', $submission->id);
101                     echo format_text($text, $submission->data2);
102                     if ($CFG->enableportfolios) {
103                         require_once($CFG->libdir . '/portfoliolib.php');
104                         $button = new portfolio_add_button();
105                         $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php');
106                         $fs = get_file_storage();
107                         if ($files = $fs->get_area_files($this->context->id, 'assignment_online_submission', $submission->id, "timemodified", false)) {
108                             $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
109                         } else {
110                             $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
111                         }
112                         $button->render();
113                     }
114                 } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604
115                     echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>';
116                 } else if ($this->isopen()){    //fix for #4206
117                     echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>';
118                 }
119             }
120             echo $OUTPUT->box_end();
121             if (!$editmode && $editable) {
122                 if (!empty($submission)) {
123                     $submitbutton = "editmysubmission";
124                 } else {
125                     $submitbutton = "addsubmission";
126                 }
127                 echo "<div style='text-align:center'>";
128                 echo $OUTPUT->single_button(new moodle_url('view.php', array('id'=>$this->cm->id, 'edit'=>'1')), get_string($submitbutton, 'assignment'));
129                 echo "</div>";
130             }
132         }
134         $this->view_feedback();
136         $this->view_footer();
137     }
139     /*
140      * Display the assignment dates
141      */
142     function view_dates() {
143         global $USER, $CFG, $OUTPUT;
145         if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
146             return;
147         }
149         echo $OUTPUT->box_start('generalbox boxaligncenter', 'dates');
150         echo '<table>';
151         if ($this->assignment->timeavailable) {
152             echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>';
153             echo '    <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>';
154         }
155         if ($this->assignment->timedue) {
156             echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>';
157             echo '    <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>';
158         }
159         $submission = $this->get_submission($USER->id);
160         if ($submission) {
161             echo '<tr><td class="c0">'.get_string('lastedited').':</td>';
162             echo '    <td class="c1">'.userdate($submission->timemodified);
163         /// Decide what to count
164             if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
165                 echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>';
166             } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
167                 echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>';
168             }
169         }
170         echo '</table>';
171         echo $OUTPUT->box_end();
172     }
174     function update_submission($data) {
175         global $CFG, $USER, $DB;
177         $submission = $this->get_submission($USER->id, true);
179         $update = new object();
180         $update->id           = $submission->id;
181         $update->data1        = $data->text;
182         $update->data2        = $data->textformat;
183         $update->timemodified = time();
185         $DB->update_record('assignment_submissions', $update);
187         $submission = $this->get_submission($USER->id);
188         $this->update_grade($submission);
189         return $submission;
190     }
193     function print_student_answer($userid, $return=false){
194         global $OUTPUT;
195         if (!$submission = $this->get_submission($userid)) {
196             return '';
197         }
199         $link = new moodle_url("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}");
200         $action = new popup_action('click', $link, 'file'.$userid, array('height' => 450, 'width' => 580));
201         $popup = $OUTPUT->action_link($link, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), $action, array('title'=>get_string('submission', 'assignment')));
203         $output = '<div class="files">'.
204                   '<img src="'.$OUTPUT->pix_url('f/html') . '" class="icon" alt="html" />'.
205                   $popup .
206                   '</div>';
207                   return $output;
208     }
210     function print_user_files($userid, $return=false) {
211         global $OUTPUT, $CFG;
213         if (!$submission = $this->get_submission($userid)) {
214             return '';
215         }
217         $link = new moodle_url("/mod/assignment/type/online/file.php?id={$this->cm->id}&userid={$submission->userid}");
218         $action = new popup_action('click', $link, 'file'.$userid, array('height' => 450, 'width' => 580));
219         $popup = $OUTPUT->action_link($link, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), $action, array('title'=>get_string('submission', 'assignment')));
221         $output = '<div class="files">'.
222                   '<img align="middle" src="'.$OUTPUT->pix_url('f/html') . '" height="16" width="16" alt="html" />'.
223                   $popup .
224                   '</div>';
226         $wordcount = '<p id="wordcount">';
227     /// Decide what to count
228         if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
229             $wordcount .= ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')';
230         } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
231             $wordcount .= ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')';
232         }
233         $wordcount .= '</p>';
235         $text = file_rewrite_pluginfile_urls($submission->data1, 'pluginfile.php', $this->context->id, 'assignment_online_submission', $submission->id);
236         return $wordcount . format_text($text, $submission->data2);
239         }
241     function preprocess_submission(&$submission) {
242         if ($this->assignment->var1 && empty($submission->submissioncomment)) {  // comment inline
243             if ($this->usehtmleditor) {
244                 // Convert to html, clean & copy student data to teacher
245                 $submission->submissioncomment = format_text($submission->data1, $submission->data2);
246                 $submission->format = FORMAT_HTML;
247             } else {
248                 // Copy student data to teacher
249                 $submission->submissioncomment = $submission->data1;
250                 $submission->format = $submission->data2;
251             }
252         }
253     }
255     function setup_elements(&$mform) {
256         global $CFG, $COURSE;
258         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
260         $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
261         $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
262         $mform->setDefault('resubmit', 0);
264         $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
265         $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
266         $mform->setDefault('emailteachers', 0);
268         $mform->addElement('select', 'var1', get_string('commentinline', 'assignment'), $ynoptions);
269         $mform->addHelpButton('var1', 'commentinline', 'assignment');
270         $mform->setDefault('var1', 0);
272     }
274     function portfolio_exportable() {
275         return true;
276     }
278     function portfolio_load_data($caller) {
279         $submission = $this->get_submission();
280         $fs = get_file_storage();
281         if ($files = $fs->get_area_files($this->context->id, 'assignment_online_submission', $submission->id, "timemodified", false)) {
282             $caller->set('multifiles', $files);
283         }
284     }
286     function portfolio_get_sha1($caller) {
287         $submission = $this->get_submission();
288         $textsha1 = sha1(format_text($submission->data1, $submission->data2));
289         $filesha1 = '';
290         try {
291             $filesha1 = $caller->get_sha1_file();
292         } catch (portfolio_caller_exception $e) {} // no files
293         return sha1($textsha1 . $filesha1);
294     }
296     function portfolio_prepare_package($exporter, $user) {
297         $submission = $this->get_submission($user->id);
298         $html = format_text($submission->data1, $submission->data2);
299         $html = portfolio_rewrite_pluginfile_urls($html, $this->context->id, 'assignment_online_submission', $submission->id, $exporter->get('format'));
300         if (in_array($exporter->get('formatclass'), array(PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_RICHHTML))) {
301             if ($files = $exporter->get('caller')->get('multifiles')) {
302                 foreach ($files as $f) {
303                     $exporter->copy_existing_file($file);
304                 }
305             }
306             return $exporter->write_new_file($html, 'assignment.html', !empty($files));
307         } else if ($exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
308             $leapwriter = $exporter->get('format')->leap2a_writer();
309             $entry = new portfolio_format_leap2a_entry('assignmentonline' . $this->assignment->id, $this->assignment->name, 'resource', $html);
310             $entry->add_category('web', 'resource_type');
311             $entry->published = $submission->timecreated;
312             $entry->updated = $submission->timemodified;
313             $entry->author = $user;
314             $leapwriter->add_entry($entry);
315             if ($files = $exporter->get('caller')->get('multifiles')) {
316                 foreach ($files as $f) {
317                     $exporter->copy_existing_file($f);
318                     $entry->add_attachment($f);
319                 }
320             }
321             $exporter->write_new_file($leapwriter->to_xml(), $exporter->get('format')->manifest_name(), true);
322         } else {
323             debugging('invalid format class: ' . $exporter->get('formatclass'));
324         }
325     }
327     function extend_settings_navigation($node) {
328         global $PAGE, $CFG, $USER;
330         // get users submission if there is one
331         $submission = $this->get_submission();
332         if (has_capability('mod/assignment:submit', $PAGE->cm->context)) {
333             $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
334         } else {
335             $editable = false;
336         }
338         // If the user has submitted something add a bit more stuff
339         if ($submission) {
340             // Add a view link to the settings nav
341             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$PAGE->cm->id));
342             $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
344             if (!empty($submission->timemodified)) {
345                 $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
346                 $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
347                 $submittednode->add_class('note');
348                 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
349                     $submittednode->add_class('early');
350                 } else {
351                     $submittednode->add_class('late');
352                 }
353             }
354         }
356         if (!$submission || $editable) {
357             // If this assignment is editable once submitted add an edit link to the settings nav
358             $link = new moodle_url('/mod/assignment/view.php', array('id'=>$PAGE->cm->id, 'edit'=>1, 'sesskey'=>sesskey()));
359             $node->add(get_string('editmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
360         }
361     }
363     public function send_file($filearea, $args) {
364         global $USER;
365         require_capability('mod/assignment:view', $this->context);
367         $fullpath = $this->context->id.$filearea.implode('/', $args);
369         $fs = get_file_storage();
370         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
371             send_file_not_found();
372         }
374         if (($USER->id != $file->get_userid()) && !has_capability('mod/assignment:grade', $this->context)) {
375             send_file_not_found();
376         }
378         session_get_instance()->write_close(); // unlock session during fileserving
379         send_stored_file($file, 60*60, 0, true);
380     }
382     /**
383      * creates a zip of all assignment submissions and sends a zip to the browser
384      */
385     public function download_submissions() {
386         global $CFG, $DB;
387         require_once($CFG->libdir.'/filelib.php');
389         $submissions = $this->get_submissions('','');
390         if (empty($submissions)) {
391             error("there are no submissions to download");
392         }
393         $filesforzipping = array();
394         $tempdir = assignment_create_temp_dir($CFG->dataroot."/temp/", "assignment".$this->assignment->id); //location for temp files.
395         //online assignment can use html
396         $filextn=".html";
398         $groupmode = groupmode($this->course,$this->cm);
399         $groupid = 0;   // All users
400         $groupname = '';
401         if($groupmode) {
402             $group = get_current_group($this->course->id, true);
403             $groupid = $group->id;
404             $groupname = $group->name.'-';
405         }
406         $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
407         foreach ($submissions as $submission) {
408             $a_userid = $submission->userid; //get userid
409             if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
410                 $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
411                 $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
412                 $submissioncontent = "<html><body>". $submission->data1. "</body></html>";      //fetched from database
413                 //get file name.html
414                 $fileforzipname =  $a_user->username . "_" . clean_filename($this->assignment->name) . $filextn;
415                 $fd = fopen($tempdir . $fileforzipname,'wb');   //create if not exist, write binary
416                 fwrite( $fd, $submissioncontent);
417                 fclose( $fd );
418                 $filesforzipping[$fileforzipname] = $tempdir.$fileforzipname;
419             }
420         }      //end of foreach
421         if ($zipfile = assignment_pack_files($filesforzipping)) {
422             remove_dir($tempdir); //remove old tempdir with individual files.
423             send_temp_file($zipfile, $filename); //send file and delete after sending.
424         }
425     }
428 class mod_assignment_online_edit_form extends moodleform {
429     function definition() {
430         $mform = $this->_form;
432         list($data, $editoroptions) = $this->_customdata;
434         // visible elements
435         $mform->addElement('editor', 'text_editor', get_string('submission', 'assignment'), null, $editoroptions);
436         $mform->setType('text_editor', PARAM_RAW); // to be cleaned before display
437         $mform->addRule('text_editor', get_string('required'), 'required', null, 'client');
439         // hidden params
440         $mform->addElement('hidden', 'id');
441         $mform->setType('id', PARAM_INT);
443         $mform->addElement('hidden', 'edit');
444         $mform->setType('edit', PARAM_INT);
446         // buttons
447         $this->add_action_buttons();
449         $this->set_data($data);
450     }