fixed bug 3606, thanks Ilshat Fattakhov; merged from MOODLE_15_STABLE
[moodle.git] / mod / assignment / type / online / assignment.class.php
CommitLineData
f77cfb73 1<?php // $Id$
2
3/**
4 * Extend the base assignment class for assignments where you upload a single file
5 *
6 */
7class assignment_online extends assignment_base {
8
9 function assignment_online($cmid=0) {
10 parent::assignment_base($cmid);
11
12 }
13
f77cfb73 14 function view() {
15
16 global $USER;
17
18 $editmode = ($this->isopen() && !empty($_GET['edit']));
19
20 if ($editmode) {
21 $this->view_header(get_string('editmysubmission', 'assignment'));
22 } else {
23 $this->view_header();
24 }
25
26 $this->view_intro();
27
28 $this->view_dates();
29
30 if ($data = data_submitted()) { // No incoming data?
31 if ($this->update_submission($data)) {
3e7bf58a 32 //redirect to get updated submission date and word count
33 redirect('view.php?id='.$this->cm->id.'&saved=1');
f77cfb73 34 }
3e7bf58a 35 } else if (!empty($_GET['saved'])) {
36 notify(get_string('submissionsaved', 'assignment'));
37 }
f77cfb73 38
39 print_simple_box_start('center', '70%', '', '', 'generalbox', 'online');
40 $submission = $this->get_submission();
41 if ($editmode) {
42 $this->view_edit_form($submission);
43 } else {
44 if ($submission) {
45 echo format_text($submission->data1, $submission->data2);
46 } else {
d8199f1d 47 echo '<center>'.get_string('emptysubmission', 'assignment').'</center>';
f77cfb73 48 }
49 if ($this->isopen()) {
50 print_single_button('view.php', array('id'=>$this->cm->id,'edit'=>'1'),
51 get_string('editmysubmission', 'assignment'));
52 }
53 }
54 print_simple_box_end();
55
56 if ($editmode and $this->usehtmleditor) {
57 use_html_editor(); // MUst be at the end of the page
58 }
59
60 $this->view_feedback();
61
62 $this->view_footer();
63 }
64
3e7bf58a 65 /*
66 * Display the assignment dates
67 */
68 function view_dates() {
69 global $USER;
70
71 if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
72 return;
73 }
74
75 print_simple_box_start('center', '', '', '', 'generalbox', 'dates');
76 echo '<table>';
77 if ($this->assignment->timeavailable) {
78 echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>';
79 echo ' <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>';
80 }
81 if ($this->assignment->timedue) {
82 echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>';
83 echo ' <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>';
84 }
85 $submission = $this->get_submission($USER->id);
86 if ($submission) {
87 echo '<tr><td class="c0">'.get_string('lastedited').':</td>';
88 echo ' <td class="c1">'.userdate($submission->timemodified);
89 echo ' ('.get_string('numwords', '', count_words(format_text($submission->data1, $submission->data2))).')</td></tr>';
90 }
91 echo '</table>';
92 print_simple_box_end();
93 }
94
f77cfb73 95 function view_edit_form($submission = NULL) {
96 global $CFG;
97
98 $defaulttext = $submission ? $submission->data1 : '';
99 $defaultformat = $submission ? $submission->data2 : $this->defaultformat;
100
088ec317 101 echo '<form name="theform" action="view.php" method="post">'; // do this so URLs look good
f77cfb73 102
103 echo '<table cellspacing="0" class="editbox" align="center">';
104 echo '<tr><td align="right">';
105 helpbutton('reading', get_string('helpreading'), 'moodle', true, true);
106 echo '<br />';
107 helpbutton('writing', get_string('helpwriting'), 'moodle', true, true);
108 echo '<br />';
109 if ($this->usehtmleditor) {
110 helpbutton('richtext', get_string('helprichtext'), 'moodle', true, true);
111 } else {
112 emoticonhelpbutton('theform', 'text');
113 }
114 echo '<br />';
115 echo '</td></tr>';
116 echo '<tr><td align="center">';
117 print_textarea($this->usehtmleditor, 20, 60, 630, 400, 'text', $defaulttext);
118 if (!$this->usehtmleditor) {
119 echo '<div align="right" class="format">';
120 print_string('formattexttype');
121 echo ':&nbsp;';
122 choose_from_menu(format_text_menu(), 'format', $defaultformat, '');
123 helpbutton('textformat', get_string('helpformatting'));
124 echo '</div>';
125 } else {
126 echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
127 }
128 echo '</td></tr>';
129 echo '<tr><td align="center">';
130 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
131 echo '<input type="submit" value="'.get_string('savechanges').'" />';
132 echo '<input type="reset" value="'.get_string('revert').'" />';
133 echo '</td></tr></table>';
134
135 echo '</form>';
136
137 }
138
139 function update_submission($data) {
140 global $CFG, $USER;
141
142 $submission = $this->get_submission($USER->id, true);
143
0cbc1f11 144 $update = NULL;
145 $update->id = $submission->id;
146 $update->data1 = $data->text;
147 $update->format = $data->format;
148 $update->timemodified = time();
f77cfb73 149
0cbc1f11 150 return update_record('assignment_submissions', $update);
f77cfb73 151 }
152
153
154 /*
155 * Display and process the submissions
156 */
157 function process_feedback() {
158
159 global $USER;
160
161 if (!$feedback = data_submitted()) { // No incoming data?
162 return false;
163 }
164
165 if (!empty($feedback->cancel)) { // User hit cancel button
166 return false;
167 }
168
169 $newsubmission = $this->get_submission($feedback->userid, true); // Get or make one
170
171 $newsubmission->grade = $feedback->grade;
172 $newsubmission->comment = $feedback->comment;
173 $newsubmission->format = $feedback->format;
174 $newsubmission->teacher = $USER->id;
175 $newsubmission->mailed = 0; // Make sure mail goes out (again, even)
176 $newsubmission->timemarked = time();
177
178 if (! update_record('assignment_submissions', $newsubmission)) {
179 return false;
180 }
181
182 add_to_log($this->course->id, 'assignment', 'update grades',
183 'submissions.php?id='.$this->assignment->id.'&user='.$feedback->userid, $feedback->userid, $this->cm->id);
184
185 return $newsubmission;
186
187 }
188
189 function print_user_files($userid, $return=false) {
190 global $CFG;
191
192 if (!$submission = $this->get_submission($userid)) {
193 return '';
194 }
195 $output = '<div class="files">'.
196 '<img align="middle" src="'.$CFG->pixpath.'/f/html.gif" height="16" width="16" alt="html" />'.
197 link_to_popup_window ('/mod/assignment/type/online/file.php?id='.$this->cm->id.'&amp;userid='.
3e7bf58a 198 $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags(format_text($submission->data1,$submission->data2))), 15), 450, 580,
f77cfb73 199 get_string('submission', 'assignment'), 'none', true).
200 '</div>';
201
202 if ($return) {
203 return $output;
204 }
205 echo $output;
206 }
207
01e2fdfe 208 function preprocess_submission(&$submission) {
209 if ($this->assignment->var1 && empty($submission->comment)) { // comment inline
47263937 210 if ($this->usehtmleditor) {
211 // Convert to html, clean & copy student data to teacher
212 $submission->comment = format_text($submission->data1, $submission->data2);
213 $submission->format = FORMAT_HTML;
214 } else {
215 // Copy student data to teacher
216 $submission->comment = $submission->data1;
217 $submission->format = $submission->data2;
218 }
01e2fdfe 219 }
220 }
221
f77cfb73 222}
223
224?>