f77cfb73 |
1 | <?php // $Id$ |
2 | |
3 | /** |
4 | * Extend the base assignment class for assignments where you upload a single file |
5 | * |
6 | */ |
7 | class assignment_online extends assignment_base { |
8 | |
9 | function assignment_online($cmid=0) { |
10 | parent::assignment_base($cmid); |
f77cfb73 |
11 | } |
12 | |
f77cfb73 |
13 | function view() { |
14 | |
15 | global $USER; |
61240489 |
16 | |
17 | $context = get_context_instance(CONTEXT_MODULE,$this->cm->id); |
0468976c |
18 | require_capability('mod/assignment:view', $context); |
bbbf2d40 |
19 | |
61240489 |
20 | $submission = $this->get_submission(); |
1648afb2 |
21 | |
2eac7ec1 |
22 | //Guest can not submit nor edit an assignment (bug: 4604) |
be07f21b |
23 | if (!has_capability('mod/assignment:submit', $context)) { |
2eac7ec1 |
24 | $editable = null; |
25 | } |
26 | else { |
27 | $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked); |
28 | } |
f0a99707 |
29 | $editmode = ($editable && !empty($_GET['edit'])); |
f77cfb73 |
30 | |
31 | if ($editmode) { |
f7de5478 |
32 | //guest can not edit or submit assignment |
be07f21b |
33 | if (!has_capability('mod/assignment:submit', $context)) { |
34 | error(get_string('guestnosubmit', 'assignment')); |
f7de5478 |
35 | } |
f77cfb73 |
36 | $this->view_header(get_string('editmysubmission', 'assignment')); |
37 | } else { |
38 | $this->view_header(); |
39 | } |
40 | |
41 | $this->view_intro(); |
42 | |
43 | $this->view_dates(); |
44 | |
45 | if ($data = data_submitted()) { // No incoming data? |
f0a99707 |
46 | if ($editable && $this->update_submission($data)) { |
ca4fb814 |
47 | //TODO fix log actions - needs db upgrade |
418db328 |
48 | $submission = $this->get_submission(); |
9bf660b3 |
49 | add_to_log($this->course->id, 'assignment', 'upload', |
50 | 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id); |
51 | $this->email_teachers($submission); |
3e7bf58a |
52 | //redirect to get updated submission date and word count |
53 | redirect('view.php?id='.$this->cm->id.'&saved=1'); |
f0a99707 |
54 | } else { |
55 | // TODO: add better error message |
56 | notify(get_string("error")); //submitting not allowed! |
f77cfb73 |
57 | } |
3e7bf58a |
58 | } else if (!empty($_GET['saved'])) { |
59 | notify(get_string('submissionsaved', 'assignment')); |
60 | } |
f77cfb73 |
61 | |
61240489 |
62 | if (has_capability('mod/assignment:submit', $context)) { |
63 | print_simple_box_start('center', '70%', '', '', 'generalbox', 'online'); |
64 | if ($editmode) { |
65 | $this->view_edit_form($submission); |
66 | } else { |
67 | if ($submission) { |
68 | echo format_text($submission->data1, $submission->data2); |
69 | } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604 |
70 | echo '<center>'. get_string('guestnosubmit', 'assignment').'</center>'; |
71 | } else if ($this->isopen()){ //fix for #4206 |
72 | echo '<center>'.get_string('emptysubmission', 'assignment').'</center>'; |
73 | } |
74 | if ($editable) { |
75 | print_single_button('view.php', array('id'=>$this->cm->id,'edit'=>'1'), |
76 | get_string('editmysubmission', 'assignment')); |
77 | } |
78 | } |
79 | print_simple_box_end(); |
80 | |
81 | if ($editmode and $this->usehtmleditor) { |
82 | use_html_editor(); // MUst be at the end of the page |
83 | } |
84 | } |
f77cfb73 |
85 | |
86 | $this->view_feedback(); |
87 | |
88 | $this->view_footer(); |
89 | } |
90 | |
3e7bf58a |
91 | /* |
92 | * Display the assignment dates |
93 | */ |
94 | function view_dates() { |
1884f2a6 |
95 | global $USER, $CFG; |
3e7bf58a |
96 | |
97 | if (!$this->assignment->timeavailable && !$this->assignment->timedue) { |
98 | return; |
99 | } |
100 | |
101 | print_simple_box_start('center', '', '', '', 'generalbox', 'dates'); |
102 | echo '<table>'; |
103 | if ($this->assignment->timeavailable) { |
104 | echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>'; |
105 | echo ' <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>'; |
106 | } |
107 | if ($this->assignment->timedue) { |
108 | echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>'; |
109 | echo ' <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>'; |
110 | } |
111 | $submission = $this->get_submission($USER->id); |
112 | if ($submission) { |
113 | echo '<tr><td class="c0">'.get_string('lastedited').':</td>'; |
114 | echo ' <td class="c1">'.userdate($submission->timemodified); |
1884f2a6 |
115 | /// Decide what to count |
116 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { |
117 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
118 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { |
119 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
120 | } |
3e7bf58a |
121 | } |
122 | echo '</table>'; |
123 | print_simple_box_end(); |
124 | } |
125 | |
f77cfb73 |
126 | function view_edit_form($submission = NULL) { |
127 | global $CFG; |
128 | |
129 | $defaulttext = $submission ? $submission->data1 : ''; |
130 | $defaultformat = $submission ? $submission->data2 : $this->defaultformat; |
131 | |
088ec317 |
132 | echo '<form name="theform" action="view.php" method="post">'; // do this so URLs look good |
f77cfb73 |
133 | |
134 | echo '<table cellspacing="0" class="editbox" align="center">'; |
135 | echo '<tr><td align="right">'; |
136 | helpbutton('reading', get_string('helpreading'), 'moodle', true, true); |
137 | echo '<br />'; |
138 | helpbutton('writing', get_string('helpwriting'), 'moodle', true, true); |
139 | echo '<br />'; |
140 | if ($this->usehtmleditor) { |
141 | helpbutton('richtext', get_string('helprichtext'), 'moodle', true, true); |
142 | } else { |
143 | emoticonhelpbutton('theform', 'text'); |
144 | } |
145 | echo '<br />'; |
146 | echo '</td></tr>'; |
147 | echo '<tr><td align="center">'; |
148 | print_textarea($this->usehtmleditor, 20, 60, 630, 400, 'text', $defaulttext); |
149 | if (!$this->usehtmleditor) { |
150 | echo '<div align="right" class="format">'; |
151 | print_string('formattexttype'); |
152 | echo ': '; |
153 | choose_from_menu(format_text_menu(), 'format', $defaultformat, ''); |
154 | helpbutton('textformat', get_string('helpformatting')); |
155 | echo '</div>'; |
156 | } else { |
157 | echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />'; |
158 | } |
159 | echo '</td></tr>'; |
160 | echo '<tr><td align="center">'; |
161 | echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />'; |
162 | echo '<input type="submit" value="'.get_string('savechanges').'" />'; |
163 | echo '<input type="reset" value="'.get_string('revert').'" />'; |
164 | echo '</td></tr></table>'; |
165 | |
166 | echo '</form>'; |
167 | |
168 | } |
169 | |
170 | function update_submission($data) { |
171 | global $CFG, $USER; |
172 | |
173 | $submission = $this->get_submission($USER->id, true); |
174 | |
0cbc1f11 |
175 | $update = NULL; |
176 | $update->id = $submission->id; |
177 | $update->data1 = $data->text; |
178 | $update->format = $data->format; |
179 | $update->timemodified = time(); |
f77cfb73 |
180 | |
0cbc1f11 |
181 | return update_record('assignment_submissions', $update); |
f77cfb73 |
182 | } |
183 | |
184 | |
9bf660b3 |
185 | function print_student_answer($userid, $return=false){ |
186 | global $CFG; |
187 | if (!$submission = $this->get_submission($userid)) { |
188 | return ''; |
189 | } |
190 | $output = '<div class="files">'. |
191 | '<img align="middle" src="'.$CFG->pixpath.'/f/html.gif" height="16" width="16" alt="html" />'. |
192 | link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&userid='. |
193 | $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580, |
194 | get_string('submission', 'assignment'), 'none', true). |
195 | '</div>'; |
196 | return $output; |
197 | } |
198 | |
f77cfb73 |
199 | function print_user_files($userid, $return=false) { |
200 | global $CFG; |
201 | |
202 | if (!$submission = $this->get_submission($userid)) { |
203 | return ''; |
204 | } |
9bf660b3 |
205 | |
f77cfb73 |
206 | $output = '<div class="files">'. |
207 | '<img align="middle" src="'.$CFG->pixpath.'/f/html.gif" height="16" width="16" alt="html" />'. |
208 | link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&userid='. |
3e7bf58a |
209 | $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580, |
f77cfb73 |
210 | get_string('submission', 'assignment'), 'none', true). |
211 | '</div>'; |
212 | |
9bf660b3 |
213 | ///Stolen code from file.php |
214 | |
c9977d05 |
215 | print_simple_box_start('center', '', '', 0, 'generalbox', 'wordcount'); |
9bf660b3 |
216 | echo '<table>'; |
217 | //if ($assignment->timedue) { |
218 | // echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>'; |
219 | // echo ' <td class="c1">'.userdate($assignment->timedue).'</td></tr>'; |
220 | //} |
221 | echo '<tr>';//<td class="c0">'.get_string('lastedited').':</td>'; |
222 | echo ' <td class="c1">';//.userdate($submission->timemodified); |
1884f2a6 |
223 | /// Decide what to count |
224 | if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_WORDS) { |
225 | echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
226 | } else if ($CFG->assignment_itemstocount == ASSIGNMENT_COUNT_LETTERS) { |
227 | echo ' ('.get_string('numletters', '', count_letters(format_text($submission->data1, $submission->data2))).')</td></tr>'; |
228 | } |
9bf660b3 |
229 | echo '</table>'; |
230 | print_simple_box_end(); |
231 | print_simple_box(format_text($submission->data1, $submission->data2), 'center', '100%'); |
232 | |
233 | ///End of stolen code from file.php |
234 | |
f77cfb73 |
235 | if ($return) { |
9bf660b3 |
236 | //return $output; |
f77cfb73 |
237 | } |
9bf660b3 |
238 | //echo $output; |
f77cfb73 |
239 | } |
240 | |
01e2fdfe |
241 | function preprocess_submission(&$submission) { |
ea6432fe |
242 | if ($this->assignment->var1 && empty($submission->submissioncomment)) { // comment inline |
47263937 |
243 | if ($this->usehtmleditor) { |
244 | // Convert to html, clean & copy student data to teacher |
ea6432fe |
245 | $submission->submissioncomment = format_text($submission->data1, $submission->data2); |
47263937 |
246 | $submission->format = FORMAT_HTML; |
247 | } else { |
248 | // Copy student data to teacher |
ea6432fe |
249 | $submission->submissioncomment = $submission->data1; |
47263937 |
250 | $submission->format = $submission->data2; |
251 | } |
01e2fdfe |
252 | } |
253 | } |
254 | |
f77cfb73 |
255 | } |
256 | |
257 | ?> |