MDL-15666 - change all the portfolio plugins and callers to use exceptions
[moodle.git] / mod / assignment / type / online / assignment.class.php
1 <?php // $Id$
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 $USER;
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         $mform = new mod_assignment_online_edit_form();
47         $defaults = new object();
48         $defaults->id = $this->cm->id;
49         if (!empty($submission)) {
50             if ($this->usehtmleditor) {
51                 $options = new object();
52                 $options->smiley = false;
53                 $options->filter = false;
55                 $defaults->text   = format_text($submission->data1, $submission->data2, $options);
56                 $defaults->format = FORMAT_HTML;
57             } else {
58                 $defaults->text   = $submission->data1;
59                 $defaults->format = $submission->data2;
60             }
61         }
62         $mform->set_data($defaults);
64         if ($mform->is_cancelled()) {
65             redirect('view.php?id='.$this->cm->id);
66         }
68         if ($data = $mform->get_data()) {      // No incoming data?
69             if ($editable && $this->update_submission($data)) {
70                 //TODO fix log actions - needs db upgrade
71                 $submission = $this->get_submission();
72                 add_to_log($this->course->id, 'assignment', 'upload',
73                         'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
74                 $this->email_teachers($submission);
75                 //redirect to get updated submission date and word count
76                 redirect('view.php?id='.$this->cm->id.'&saved=1');
77             } else {
78                 // TODO: add better error message
79                 notify(get_string("error")); //submitting not allowed!
80             }
81         }
83 /// print header, etc. and display form if needed
84         if ($editmode) {
85             $this->view_header(get_string('editmysubmission', 'assignment'));
86         } else {
87             $this->view_header();
88         }
90         $this->view_intro();
92         $this->view_dates();
94         if ($saved) {
95             notify(get_string('submissionsaved', 'assignment'), 'notifysuccess');
96         }
98         if (has_capability('mod/assignment:submit', $context)) {
99             if ($editmode) {
100                 print_box_start('generalbox', 'online');
101                 $mform->display();
102             } else {
103                 print_box_start('generalbox boxwidthwide boxaligncenter', 'online');
104                 if ($submission && has_capability('mod/assignment:exportownsubmission', $this->context)) {
105                     echo format_text($submission->data1, $submission->data2);
106                     $button = new portfolio_add_button();
107                     $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/lib.php');
108                     $button->render();
109                 } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604
110                     echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>';
111                 } else if ($this->isopen()){    //fix for #4206
112                     echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>';
113                 }
114             }
115             print_box_end();
116             if (!$editmode && $editable) {
117                 echo "<div style='text-align:center'>";
118                 print_single_button('view.php', array('id'=>$this->cm->id,'edit'=>'1'),
119                         get_string('editmysubmission', 'assignment'));
120                 echo "</div>";
121             }
123         }
125         $this->view_feedback();
127         $this->view_footer();
128     }
130     /*
131      * Display the assignment dates
132      */
133     function view_dates() {
134         global $USER, $CFG;
136         if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
137             return;
138         }
140         print_simple_box_start('center', '', '', 0, 'generalbox', 'dates');
141         echo '<table>';
142         if ($this->assignment->timeavailable) {
143             echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>';
144             echo '    <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>';
145         }
146         if ($this->assignment->timedue) {
147             echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>';
148             echo '    <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>';
149         }
150         $submission = $this->get_submission($USER->id);
151         if ($submission) {
152             echo '<tr><td class="c0">'.get_string('lastedited').':</td>';
153             echo '    <td class="c1">'.userdate($submission->timemodified);
154         /// Decide what to count
155             if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
156                 echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>';
157             } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
158                 echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>';
159             }
160         }
161         echo '</table>';
162         print_simple_box_end();
163     }
165     function update_submission($data) {
166         global $CFG, $USER, $DB;
168         $submission = $this->get_submission($USER->id, true);
170         $update = new object();
171         $update->id           = $submission->id;
172         $update->data1        = $data->text;
173         $update->data2        = $data->format;
174         $update->timemodified = time();
176         if (!$DB->update_record('assignment_submissions', $update)) {
177             return false;
178         }
180         $submission = $this->get_submission($USER->id);
181         $this->update_grade($submission);
182         return true;
183     }
186     function print_student_answer($userid, $return=false){
187         global $CFG;
188         if (!$submission = $this->get_submission($userid)) {
189             return '';
190         }
191         $output = '<div class="files">'.
192                   '<img src="'.$CFG->pixpath.'/f/html.gif" class="icon" alt="html" />'.
193                   link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&amp;userid='.
194                   $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580,
195                   get_string('submission', 'assignment'), 'none', true).
196                   '</div>';
197                   return $output;
198     }
200     function print_user_files($userid, $return=false) {
201         global $CFG;
203         if (!$submission = $this->get_submission($userid)) {
204             return '';
205         }
207         $output = '<div class="files">'.
208                   '<img align="middle" src="'.$CFG->pixpath.'/f/html.gif" height="16" width="16" alt="html" />'.
209                   link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&amp;userid='.
210                   $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580,
211                   get_string('submission', 'assignment'), 'none', true).
212                   '</div>';
214         ///Stolen code from file.php
216         print_simple_box_start('center', '', '', 0, 'generalbox', 'wordcount');
217     /// Decide what to count
218         if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) {
219             echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')';
220         } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) {
221             echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')';
222         }
223         print_simple_box_end();
224         print_simple_box(format_text($submission->data1, $submission->data2), 'center', '100%');
226         ///End of stolen code from file.php
228         if ($return) {
229             //return $output;
230         }
231         //echo $output;
232     }
234     function preprocess_submission(&$submission) {
235         if ($this->assignment->var1 && empty($submission->submissioncomment)) {  // comment inline
236             if ($this->usehtmleditor) {
237                 // Convert to html, clean & copy student data to teacher
238                 $submission->submissioncomment = format_text($submission->data1, $submission->data2);
239                 $submission->format = FORMAT_HTML;
240             } else {
241                 // Copy student data to teacher
242                 $submission->submissioncomment = $submission->data1;
243                 $submission->format = $submission->data2;
244             }
245         }
246     }
248     function setup_elements(&$mform) {
249         global $CFG, $COURSE;
251         $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
253         $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions);
254         $mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment'));
255         $mform->setDefault('resubmit', 0);
257         $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
258         $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
259         $mform->setDefault('emailteachers', 0);
261         $mform->addElement('select', 'var1', get_string("commentinline", "assignment"), $ynoptions);
262         $mform->setHelpButton('var1', array('commentinline', get_string('commentinline', 'assignment'), 'assignment'));
263         $mform->setDefault('var1', 0);
265     }
267     function portfolio_exportable() {
268         return true;
269     }
271     function portfolio_get_sha1($userid=0) {
272         $submission = $this->get_submission($userid);
273         return sha1(format_text($submission->data1, $submission->data2));
274     }
276     function portfolio_prepare_package($exporter, $userid=0) {
277         $submission = $this->get_submission($userid);
278         $exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html');
279     }
281     function portfolio_supported_formats() {
282         return array(PORTFOLIO_FORMAT_HTML);
283     }
286 class mod_assignment_online_edit_form extends moodleform {
287     function definition() {
288         $mform =& $this->_form;
290         // visible elements
291         $mform->addElement('htmleditor', 'text', get_string('submission', 'assignment'), array('cols'=>60, 'rows'=>30));
292         $mform->setType('text', PARAM_RAW); // to be cleaned before display
293         $mform->setHelpButton('text', array('reading', 'writing', 'richtext'), false, 'editorhelpbutton');
294         $mform->addRule('text', get_string('required'), 'required', null, 'client');
296         $mform->addElement('format', 'format', get_string('format'));
297         $mform->setHelpButton('format', array('textformat', get_string('helpformatting')));
299         // hidden params
300         $mform->addElement('hidden', 'id', 0);
301         $mform->setType('id', PARAM_INT);
303         // buttons
304         $this->add_action_buttons();
305     }
308 ?>