MDL-15666 - change all the portfolio plugins and callers to use exceptions
[moodle.git] / mod / assignment / type / online / assignment.class.php
CommitLineData
f77cfb73 1<?php // $Id$
6cb1b93e 2require_once($CFG->libdir.'/formslib.php');
67a87e7d 3require_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 */
8class 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
17 global $USER;
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
1d284fbd 45 $mform = new mod_assignment_online_edit_form();
f77cfb73 46
6cb1b93e 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;
f77cfb73 54
6cb1b93e 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 }
beac4717 62 $mform->set_data($defaults);
6cb1b93e 63
5571b5f4 64 if ($mform->is_cancelled()) {
65 redirect('view.php?id='.$this->cm->id);
66 }
67
beac4717 68 if ($data = $mform->get_data()) { // No incoming data?
f0a99707 69 if ($editable && $this->update_submission($data)) {
ca4fb814 70 //TODO fix log actions - needs db upgrade
418db328 71 $submission = $this->get_submission();
6cb1b93e 72 add_to_log($this->course->id, 'assignment', 'upload',
9bf660b3 73 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
74 $this->email_teachers($submission);
3e7bf58a 75 //redirect to get updated submission date and word count
76 redirect('view.php?id='.$this->cm->id.'&saved=1');
f0a99707 77 } else {
78 // TODO: add better error message
79 notify(get_string("error")); //submitting not allowed!
f77cfb73 80 }
6cb1b93e 81 }
82
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 }
89
90 $this->view_intro();
91
92 $this->view_dates();
93
94 if ($saved) {
95 notify(get_string('submissionsaved', 'assignment'), 'notifysuccess');
3e7bf58a 96 }
f77cfb73 97
61240489 98 if (has_capability('mod/assignment:submit', $context)) {
61240489 99 if ($editmode) {
d854aaa6 100 print_box_start('generalbox', 'online');
6cb1b93e 101 $mform->display();
61240489 102 } else {
d854aaa6 103 print_box_start('generalbox boxwidthwide boxaligncenter', 'online');
728e1931 104 if ($submission && has_capability('mod/assignment:exportownsubmission', $this->context)) {
61240489 105 echo format_text($submission->data1, $submission->data2);
0d06b6fd 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();
61240489 109 } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604
d9cb14b8 110 echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>';
61240489 111 } else if ($this->isopen()){ //fix for #4206
d9cb14b8 112 echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>';
61240489 113 }
61240489 114 }
d854aaa6 115 print_box_end();
12dce253 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 }
6cb1b93e 122
61240489 123 }
f77cfb73 124
125 $this->view_feedback();
126
127 $this->view_footer();
128 }
129
3e7bf58a 130 /*
131 * Display the assignment dates
132 */
133 function view_dates() {
1884f2a6 134 global $USER, $CFG;
3e7bf58a 135
136 if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
137 return;
138 }
139
32776fef 140 print_simple_box_start('center', '', '', 0, 'generalbox', 'dates');
3e7bf58a 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);
1884f2a6 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 }
3e7bf58a 160 }
161 echo '</table>';
162 print_simple_box_end();
163 }
164
f77cfb73 165 function update_submission($data) {
74d7d735 166 global $CFG, $USER, $DB;
f77cfb73 167
168 $submission = $this->get_submission($USER->id, true);
169
6cb1b93e 170 $update = new object();
171 $update->id = $submission->id;
172 $update->data1 = $data->text;
173 $update->data2 = $data->format;
0cbc1f11 174 $update->timemodified = time();
f77cfb73 175
74d7d735 176 if (!$DB->update_record('assignment_submissions', $update)) {
12dce253 177 return false;
178 }
179
180 $submission = $this->get_submission($USER->id);
181 $this->update_grade($submission);
182 return true;
f77cfb73 183 }
184
185
9bf660b3 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">'.
d9cf7323 192 '<img src="'.$CFG->pixpath.'/f/html.gif" class="icon" alt="html" />'.
9bf660b3 193 link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&amp;userid='.
6cb1b93e 194 $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580,
9bf660b3 195 get_string('submission', 'assignment'), 'none', true).
196 '</div>';
197 return $output;
198 }
6cb1b93e 199
f77cfb73 200 function print_user_files($userid, $return=false) {
201 global $CFG;
6cb1b93e 202
f77cfb73 203 if (!$submission = $this->get_submission($userid)) {
204 return '';
205 }
6cb1b93e 206
f77cfb73 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='.
6cb1b93e 210 $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580,
f77cfb73 211 get_string('submission', 'assignment'), 'none', true).
212 '</div>';
213
9bf660b3 214 ///Stolen code from file.php
6cb1b93e 215
c9977d05 216 print_simple_box_start('center', '', '', 0, 'generalbox', 'wordcount');
63d69d54 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 }
9bf660b3 223 print_simple_box_end();
224 print_simple_box(format_text($submission->data1, $submission->data2), 'center', '100%');
6cb1b93e 225
9bf660b3 226 ///End of stolen code from file.php
6cb1b93e 227
f77cfb73 228 if ($return) {
9bf660b3 229 //return $output;
f77cfb73 230 }
9bf660b3 231 //echo $output;
f77cfb73 232 }
233
01e2fdfe 234 function preprocess_submission(&$submission) {
ea6432fe 235 if ($this->assignment->var1 && empty($submission->submissioncomment)) { // comment inline
47263937 236 if ($this->usehtmleditor) {
237 // Convert to html, clean & copy student data to teacher
ea6432fe 238 $submission->submissioncomment = format_text($submission->data1, $submission->data2);
6cb1b93e 239 $submission->format = FORMAT_HTML;
47263937 240 } else {
241 // Copy student data to teacher
ea6432fe 242 $submission->submissioncomment = $submission->data1;
6cb1b93e 243 $submission->format = $submission->data2;
47263937 244 }
01e2fdfe 245 }
246 }
247
436cfa9f 248 function setup_elements(&$mform) {
249 global $CFG, $COURSE;
250
251 $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
252
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);
256
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);
260
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);
264
265 }
266
67a87e7d 267 function portfolio_exportable() {
268 return true;
269 }
ffcfd8a7 270
eb32c6ca 271 function portfolio_get_sha1($userid=0) {
272 $submission = $this->get_submission($userid);
ffcfd8a7 273 return sha1(format_text($submission->data1, $submission->data2));
274 }
275
eb32c6ca 276 function portfolio_prepare_package($exporter, $userid=0) {
277 $submission = $this->get_submission($userid);
37f03ea0 278 $exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html');
ffcfd8a7 279 }
ea0de12f 280
281 function portfolio_supported_formats() {
282 return array(PORTFOLIO_FORMAT_HTML);
283 }
f77cfb73 284}
285
f07b9627 286class mod_assignment_online_edit_form extends moodleform {
6cb1b93e 287 function definition() {
288 $mform =& $this->_form;
289
290 // visible elements
9da321f7 291 $mform->addElement('htmleditor', 'text', get_string('submission', 'assignment'), array('cols'=>60, 'rows'=>30));
6117edc7 292 $mform->setType('text', PARAM_RAW); // to be cleaned before display
18a77361 293 $mform->setHelpButton('text', array('reading', 'writing', 'richtext'), false, 'editorhelpbutton');
44d6ecc3 294 $mform->addRule('text', get_string('required'), 'required', null, 'client');
6cb1b93e 295
296 $mform->addElement('format', 'format', get_string('format'));
297 $mform->setHelpButton('format', array('textformat', get_string('helpformatting')));
298
6117edc7 299 // hidden params
6cb1b93e 300 $mform->addElement('hidden', 'id', 0);
301 $mform->setType('id', PARAM_INT);
302
303 // buttons
a23f0aaf 304 $this->add_action_buttons();
6cb1b93e 305 }
306}
307
f77cfb73 308?>