f77cfb73 |
1 | <?php // $Id$ |
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 |
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 |
3a003ead |
79 | echo $OUTPUT->notification(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) { |
3a003ead |
95 | echo $OUTPUT->notification(get_string('submissionsaved', 'assignment'), 'notifysuccess'); |
3e7bf58a |
96 | } |
f77cfb73 |
97 | |
61240489 |
98 | if (has_capability('mod/assignment:submit', $context)) { |
61240489 |
99 | if ($editmode) { |
01e0da58 |
100 | echo $OUTPUT->box_start('generalbox', 'online'); |
6cb1b93e |
101 | $mform->display(); |
61240489 |
102 | } else { |
01e0da58 |
103 | echo $OUTPUT->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 | } |
01e0da58 |
115 | echo $OUTPUT->box_end(); |
12dce253 |
116 | if (!$editmode && $editable) { |
117 | echo "<div style='text-align:center'>"; |
75d47fa9 |
118 | echo $OUTPUT->button(html_form::make_button('view.php', array('id'=>$this->cm->id,'edit'=>'1'), |
119 | get_string('editmysubmission', 'assignment'))); |
12dce253 |
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() { |
3a003ead |
134 | global $USER, $CFG, $OUTPUT; |
3e7bf58a |
135 | |
136 | if (!$this->assignment->timeavailable && !$this->assignment->timedue) { |
137 | return; |
138 | } |
139 | |
3a003ead |
140 | echo $OUTPUT->box_start('generalbox boxaligncenter', '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>'; |
3a003ead |
162 | echo $OUTPUT->box_end(); |
3e7bf58a |
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 | |
9d749339 |
176 | $DB->update_record('assignment_submissions', $update); |
12dce253 |
177 | |
178 | $submission = $this->get_submission($USER->id); |
179 | $this->update_grade($submission); |
180 | return true; |
f77cfb73 |
181 | } |
182 | |
183 | |
9bf660b3 |
184 | function print_student_answer($userid, $return=false){ |
f2a1963c |
185 | global $OUTPUT; |
9bf660b3 |
186 | if (!$submission = $this->get_submission($userid)) { |
187 | return ''; |
188 | } |
a9573c08 |
189 | |
190 | $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)); |
191 | $link->add_action(new popup_action('click', $link->url, 'file'.$userid, array('height' => 450, 'width' => 580))); |
192 | $link->title = get_string('submission', 'assignment'); |
193 | $popup = $OUTPUT->link($link); |
194 | |
9bf660b3 |
195 | $output = '<div class="files">'. |
f2a1963c |
196 | '<img src="'.$OUTPUT->old_icon_url('f/html') . '" class="icon" alt="html" />'. |
a9573c08 |
197 | $popup . |
9bf660b3 |
198 | '</div>'; |
199 | return $output; |
200 | } |
6cb1b93e |
201 | |
f77cfb73 |
202 | function print_user_files($userid, $return=false) { |
f2a1963c |
203 | global $OUTPUT; |
6cb1b93e |
204 | |
f77cfb73 |
205 | if (!$submission = $this->get_submission($userid)) { |
206 | return ''; |
207 | } |
a9573c08 |
208 | |
209 | $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)); |
210 | $link->add_action(new popup_action('click', $link->url, 'file'.$userid, array('height' => 450, 'width' => 580))); |
211 | $link->title = get_string('submission', 'assignment'); |
212 | $popup = $OUTPUT->link($link); |
6cb1b93e |
213 | |
f77cfb73 |
214 | $output = '<div class="files">'. |
f2a1963c |
215 | '<img align="middle" src="'.$OUTPUT->old_icon_url('f/html') . '" height="16" width="16" alt="html" />'. |
a9573c08 |
216 | $popup . |
f77cfb73 |
217 | '</div>'; |
218 | |
9bf660b3 |
219 | ///Stolen code from file.php |
6cb1b93e |
220 | |
3a003ead |
221 | echo $OUTPUT->box_start('generalbox boxaligncenter', 'wordcount'); |
63d69d54 |
222 | /// Decide what to count |
223 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { |
224 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')'; |
225 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { |
226 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')'; |
227 | } |
3a003ead |
228 | echo $OUTPUT->box_end(); |
229 | echo $OUTPUT->box(format_text($submission->data1, $submission->data2), 'generalbox boxaligncenter boxwidthwide'); |
6cb1b93e |
230 | |
9bf660b3 |
231 | ///End of stolen code from file.php |
6cb1b93e |
232 | |
f77cfb73 |
233 | if ($return) { |
9bf660b3 |
234 | //return $output; |
f77cfb73 |
235 | } |
9bf660b3 |
236 | //echo $output; |
f77cfb73 |
237 | } |
238 | |
01e2fdfe |
239 | function preprocess_submission(&$submission) { |
ea6432fe |
240 | if ($this->assignment->var1 && empty($submission->submissioncomment)) { // comment inline |
47263937 |
241 | if ($this->usehtmleditor) { |
242 | // Convert to html, clean & copy student data to teacher |
ea6432fe |
243 | $submission->submissioncomment = format_text($submission->data1, $submission->data2); |
6cb1b93e |
244 | $submission->format = FORMAT_HTML; |
47263937 |
245 | } else { |
246 | // Copy student data to teacher |
ea6432fe |
247 | $submission->submissioncomment = $submission->data1; |
6cb1b93e |
248 | $submission->format = $submission->data2; |
47263937 |
249 | } |
01e2fdfe |
250 | } |
251 | } |
252 | |
436cfa9f |
253 | function setup_elements(&$mform) { |
254 | global $CFG, $COURSE; |
255 | |
256 | $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes')); |
257 | |
258 | $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions); |
259 | $mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment')); |
260 | $mform->setDefault('resubmit', 0); |
261 | |
262 | $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions); |
263 | $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment')); |
264 | $mform->setDefault('emailteachers', 0); |
265 | |
266 | $mform->addElement('select', 'var1', get_string("commentinline", "assignment"), $ynoptions); |
267 | $mform->setHelpButton('var1', array('commentinline', get_string('commentinline', 'assignment'), 'assignment')); |
268 | $mform->setDefault('var1', 0); |
269 | |
270 | } |
271 | |
67a87e7d |
272 | function portfolio_exportable() { |
273 | return true; |
274 | } |
ffcfd8a7 |
275 | |
eb32c6ca |
276 | function portfolio_get_sha1($userid=0) { |
277 | $submission = $this->get_submission($userid); |
ffcfd8a7 |
278 | return sha1(format_text($submission->data1, $submission->data2)); |
279 | } |
280 | |
eb32c6ca |
281 | function portfolio_prepare_package($exporter, $userid=0) { |
282 | $submission = $this->get_submission($userid); |
6be1dcae |
283 | $exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html', false); |
ffcfd8a7 |
284 | } |
ea0de12f |
285 | |
286 | function portfolio_supported_formats() { |
6be1dcae |
287 | return array(PORTFOLIO_FORMAT_PLAINHTML); |
ea0de12f |
288 | } |
f77cfb73 |
289 | } |
290 | |
f07b9627 |
291 | class mod_assignment_online_edit_form extends moodleform { |
6cb1b93e |
292 | function definition() { |
293 | $mform =& $this->_form; |
294 | |
295 | // visible elements |
9da321f7 |
296 | $mform->addElement('htmleditor', 'text', get_string('submission', 'assignment'), array('cols'=>60, 'rows'=>30)); |
6117edc7 |
297 | $mform->setType('text', PARAM_RAW); // to be cleaned before display |
16851b22 |
298 | $mform->setHelpButton('text', array('reading', 'writing', 'richtext2'), false, 'editorhelpbutton'); |
44d6ecc3 |
299 | $mform->addRule('text', get_string('required'), 'required', null, 'client'); |
6cb1b93e |
300 | |
301 | $mform->addElement('format', 'format', get_string('format')); |
302 | $mform->setHelpButton('format', array('textformat', get_string('helpformatting'))); |
303 | |
6117edc7 |
304 | // hidden params |
6cb1b93e |
305 | $mform->addElement('hidden', 'id', 0); |
306 | $mform->setType('id', PARAM_INT); |
307 | |
308 | // buttons |
a23f0aaf |
309 | $this->add_action_buttons(); |
6cb1b93e |
310 | } |
311 | } |
312 | |
f77cfb73 |
313 | ?> |