f77cfb73 |
1 | <?php // $Id$ |
6cb1b93e |
2 | require_once($CFG->libdir.'/formslib.php'); |
f77cfb73 |
3 | |
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 | |
10 | function assignment_online($cmid=0) { |
11 | parent::assignment_base($cmid); |
f77cfb73 |
12 | } |
13 | |
f77cfb73 |
14 | function view() { |
15 | |
16 | global $USER; |
6cb1b93e |
17 | |
18 | $edit = optional_param('edit', 0, PARAM_BOOL); |
19 | $saved = optional_param('saved', 0, PARAM_BOOL); |
20 | |
21 | $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); |
0468976c |
22 | require_capability('mod/assignment:view', $context); |
6cb1b93e |
23 | |
61240489 |
24 | $submission = $this->get_submission(); |
1648afb2 |
25 | |
2eac7ec1 |
26 | //Guest can not submit nor edit an assignment (bug: 4604) |
be07f21b |
27 | if (!has_capability('mod/assignment:submit', $context)) { |
2eac7ec1 |
28 | $editable = null; |
6cb1b93e |
29 | } else { |
2eac7ec1 |
30 | $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked); |
31 | } |
6cb1b93e |
32 | $editmode = ($editable and $edit); |
f77cfb73 |
33 | |
34 | if ($editmode) { |
f7de5478 |
35 | //guest can not edit or submit assignment |
be07f21b |
36 | if (!has_capability('mod/assignment:submit', $context)) { |
37 | error(get_string('guestnosubmit', 'assignment')); |
f7de5478 |
38 | } |
f77cfb73 |
39 | } |
40 | |
6cb1b93e |
41 | /// prepare form and process submitted data |
1d284fbd |
42 | $mform = new mod_assignment_online_edit_form(); |
f77cfb73 |
43 | |
6cb1b93e |
44 | $defaults = new object(); |
45 | $defaults->id = $this->cm->id; |
46 | if (!empty($submission)) { |
47 | if ($this->usehtmleditor) { |
48 | $options = new object(); |
49 | $options->smiley = false; |
50 | $options->filter = false; |
f77cfb73 |
51 | |
6cb1b93e |
52 | $defaults->text = format_text($submission->data1, $submission->data2, $options); |
53 | $defaults->format = FORMAT_HTML; |
54 | } else { |
55 | $defaults->text = $submission->data1; |
56 | $defaults->format = $submission->data2; |
57 | } |
58 | } |
59 | $mform->set_defaults($defaults); |
60 | |
5571b5f4 |
61 | if ($mform->is_cancelled()) { |
62 | redirect('view.php?id='.$this->cm->id); |
63 | } |
64 | |
6cb1b93e |
65 | if ($data = $mform->data_submitted()) { // No incoming data? |
f0a99707 |
66 | if ($editable && $this->update_submission($data)) { |
ca4fb814 |
67 | //TODO fix log actions - needs db upgrade |
418db328 |
68 | $submission = $this->get_submission(); |
6cb1b93e |
69 | add_to_log($this->course->id, 'assignment', 'upload', |
9bf660b3 |
70 | 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); |
71 | $this->email_teachers($submission); |
3e7bf58a |
72 | //redirect to get updated submission date and word count |
73 | redirect('view.php?id='.$this->cm->id.'&saved=1'); |
f0a99707 |
74 | } else { |
75 | // TODO: add better error message |
76 | notify(get_string("error")); //submitting not allowed! |
f77cfb73 |
77 | } |
6cb1b93e |
78 | } |
79 | |
80 | /// print header, etc. and display form if needed |
81 | if ($editmode) { |
82 | $this->view_header(get_string('editmysubmission', 'assignment')); |
83 | } else { |
84 | $this->view_header(); |
85 | } |
86 | |
87 | $this->view_intro(); |
88 | |
89 | $this->view_dates(); |
90 | |
91 | if ($saved) { |
92 | notify(get_string('submissionsaved', 'assignment'), 'notifysuccess'); |
3e7bf58a |
93 | } |
f77cfb73 |
94 | |
61240489 |
95 | if (has_capability('mod/assignment:submit', $context)) { |
32776fef |
96 | print_simple_box_start('center', '70%', '', 0, 'generalbox', 'online'); |
61240489 |
97 | if ($editmode) { |
6cb1b93e |
98 | $mform->display(); |
61240489 |
99 | } else { |
100 | if ($submission) { |
101 | echo format_text($submission->data1, $submission->data2); |
102 | } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604 |
d9cb14b8 |
103 | echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>'; |
61240489 |
104 | } else if ($this->isopen()){ //fix for #4206 |
d9cb14b8 |
105 | echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>'; |
61240489 |
106 | } |
107 | if ($editable) { |
6cb1b93e |
108 | print_single_button('view.php', array('id'=>$this->cm->id, 'edit'=>'1'), |
61240489 |
109 | get_string('editmysubmission', 'assignment')); |
110 | } |
111 | } |
112 | print_simple_box_end(); |
6cb1b93e |
113 | |
61240489 |
114 | } |
f77cfb73 |
115 | |
116 | $this->view_feedback(); |
117 | |
118 | $this->view_footer(); |
119 | } |
120 | |
3e7bf58a |
121 | /* |
122 | * Display the assignment dates |
123 | */ |
124 | function view_dates() { |
1884f2a6 |
125 | global $USER, $CFG; |
3e7bf58a |
126 | |
127 | if (!$this->assignment->timeavailable && !$this->assignment->timedue) { |
128 | return; |
129 | } |
130 | |
32776fef |
131 | print_simple_box_start('center', '', '', 0, 'generalbox', 'dates'); |
3e7bf58a |
132 | echo '<table>'; |
133 | if ($this->assignment->timeavailable) { |
134 | echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>'; |
135 | echo ' <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>'; |
136 | } |
137 | if ($this->assignment->timedue) { |
138 | echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>'; |
139 | echo ' <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>'; |
140 | } |
141 | $submission = $this->get_submission($USER->id); |
142 | if ($submission) { |
143 | echo '<tr><td class="c0">'.get_string('lastedited').':</td>'; |
144 | echo ' <td class="c1">'.userdate($submission->timemodified); |
1884f2a6 |
145 | /// Decide what to count |
146 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { |
147 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
148 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { |
149 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
150 | } |
3e7bf58a |
151 | } |
152 | echo '</table>'; |
153 | print_simple_box_end(); |
154 | } |
155 | |
f77cfb73 |
156 | function update_submission($data) { |
157 | global $CFG, $USER; |
158 | |
159 | $submission = $this->get_submission($USER->id, true); |
160 | |
6cb1b93e |
161 | $update = new object(); |
162 | $update->id = $submission->id; |
163 | $update->data1 = $data->text; |
164 | $update->data2 = $data->format; |
0cbc1f11 |
165 | $update->timemodified = time(); |
f77cfb73 |
166 | |
0cbc1f11 |
167 | return update_record('assignment_submissions', $update); |
f77cfb73 |
168 | } |
169 | |
170 | |
9bf660b3 |
171 | function print_student_answer($userid, $return=false){ |
172 | global $CFG; |
173 | if (!$submission = $this->get_submission($userid)) { |
174 | return ''; |
175 | } |
176 | $output = '<div class="files">'. |
0d905d9f |
177 | '<img align="middle" src="'.$CFG->pixpath.'/f/html.gif" class="icon" alt="html" />'. |
9bf660b3 |
178 | link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&userid='. |
6cb1b93e |
179 | $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580, |
9bf660b3 |
180 | get_string('submission', 'assignment'), 'none', true). |
181 | '</div>'; |
182 | return $output; |
183 | } |
6cb1b93e |
184 | |
f77cfb73 |
185 | function print_user_files($userid, $return=false) { |
186 | global $CFG; |
6cb1b93e |
187 | |
f77cfb73 |
188 | if (!$submission = $this->get_submission($userid)) { |
189 | return ''; |
190 | } |
6cb1b93e |
191 | |
f77cfb73 |
192 | $output = '<div class="files">'. |
193 | '<img align="middle" src="'.$CFG->pixpath.'/f/html.gif" height="16" width="16" alt="html" />'. |
194 | link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&userid='. |
6cb1b93e |
195 | $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580, |
f77cfb73 |
196 | get_string('submission', 'assignment'), 'none', true). |
197 | '</div>'; |
198 | |
9bf660b3 |
199 | ///Stolen code from file.php |
6cb1b93e |
200 | |
c9977d05 |
201 | print_simple_box_start('center', '', '', 0, 'generalbox', 'wordcount'); |
9bf660b3 |
202 | echo '<table>'; |
203 | //if ($assignment->timedue) { |
204 | // echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>'; |
205 | // echo ' <td class="c1">'.userdate($assignment->timedue).'</td></tr>'; |
206 | //} |
207 | echo '<tr>';//<td class="c0">'.get_string('lastedited').':</td>'; |
208 | echo ' <td class="c1">';//.userdate($submission->timemodified); |
1884f2a6 |
209 | /// Decide what to count |
210 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { |
211 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
212 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { |
213 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
214 | } |
9bf660b3 |
215 | echo '</table>'; |
216 | print_simple_box_end(); |
217 | print_simple_box(format_text($submission->data1, $submission->data2), 'center', '100%'); |
6cb1b93e |
218 | |
9bf660b3 |
219 | ///End of stolen code from file.php |
6cb1b93e |
220 | |
f77cfb73 |
221 | if ($return) { |
9bf660b3 |
222 | //return $output; |
f77cfb73 |
223 | } |
9bf660b3 |
224 | //echo $output; |
f77cfb73 |
225 | } |
226 | |
01e2fdfe |
227 | function preprocess_submission(&$submission) { |
ea6432fe |
228 | if ($this->assignment->var1 && empty($submission->submissioncomment)) { // comment inline |
47263937 |
229 | if ($this->usehtmleditor) { |
230 | // Convert to html, clean & copy student data to teacher |
ea6432fe |
231 | $submission->submissioncomment = format_text($submission->data1, $submission->data2); |
6cb1b93e |
232 | $submission->format = FORMAT_HTML; |
47263937 |
233 | } else { |
234 | // Copy student data to teacher |
ea6432fe |
235 | $submission->submissioncomment = $submission->data1; |
6cb1b93e |
236 | $submission->format = $submission->data2; |
47263937 |
237 | } |
01e2fdfe |
238 | } |
239 | } |
240 | |
f77cfb73 |
241 | } |
242 | |
f07b9627 |
243 | class mod_assignment_online_edit_form extends moodleform { |
6cb1b93e |
244 | function definition() { |
245 | $mform =& $this->_form; |
246 | |
247 | // visible elements |
6117edc7 |
248 | $mform->addElement('htmleditor', 'text', get_string('submission', 'assignment'), array('cols'=>85, 'rows'=>30)); |
249 | $mform->setType('text', PARAM_RAW); // to be cleaned before display |
18a77361 |
250 | $mform->setHelpButton('text', array('reading', 'writing', 'richtext'), false, 'editorhelpbutton'); |
6cb1b93e |
251 | |
252 | $mform->addElement('format', 'format', get_string('format')); |
253 | $mform->setHelpButton('format', array('textformat', get_string('helpformatting'))); |
254 | |
6117edc7 |
255 | // hidden params |
6cb1b93e |
256 | $mform->addElement('hidden', 'id', 0); |
257 | $mform->setType('id', PARAM_INT); |
258 | |
259 | // buttons |
a23f0aaf |
260 | $this->add_action_buttons(); |
6cb1b93e |
261 | } |
262 | } |
263 | |
f77cfb73 |
264 | ?> |