tagging as ATutor 1.5.4-release
[atutor.git] / assignments / add_assignment.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2006                                      */
6 /* Written by Greg Gay, Joel Kronenberg & Chris Ridpath         */
7 /* Adaptive Technology Resource Centre / University of Toronto  */
8 /* http://atutor.ca                                                                                             */
9 /*                                                              */
10 /* This program is free software. You can redistribute it and/or*/
11 /* modify it under the terms of the GNU General Public License  */
12 /* as published by the Free Software Foundation.                                */
13 /****************************************************************/
14 // $Id$
15 define('AT_INCLUDE_PATH', '../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 authenticate(AT_PRIV_ASSIGNMENTS);
18
19 // initial values for controls
20 $id = 0;
21 $today = getdate();
22
23 // Are we editing an existing assignment or creating a new assignment?
24 if (isset ($_GET['id'])){
25         // editing an existing assignment
26         $id = intval($_GET['id']); 
27
28         $sql = "SELECT *, DATE_FORMAT(date_due, '%Y-%m-%d %H:%i:00') AS date_due, DATE_FORMAT(date_cutoff, '%Y-%m-%d %H:%i:00') AS date_cutoff FROM ".TABLE_PREFIX."assignments WHERE course_id=$_SESSION[course_id] AND assignment_id=$id";
29
30         $result = mysql_query($sql,$db);
31         if (!($row = mysql_fetch_assoc($result))) {
32                 // should not happen
33                 $msg->addFeedback('ASSIGNMENT_NOT_FOUND');
34                 header('Location: index_instructor.php');
35                 exit;
36         }
37
38         // get values of existing assignment from database
39         $title                  = $row['title'];
40         $assign_to              = $row['assign_to'];
41         $multi_submit   = $row['multi_submit'];
42
43         $array1                 = explode (' ', $row['date_due'], 2);
44         $array_date_due = explode ('-', $array1[0],3);
45         $array_time_due = explode (':', $array1[1]);
46         $dueyear                = $array_date_due[0];
47         $duemonth               = $array_date_due[1];
48         $dueday                 = $array_date_due[2];
49         $duehour                = $array_time_due[0];
50         $dueminute              = $array_time_due[1];
51
52         if ($dueyear == '0000'){
53                 $has_due_date = 'false';
54         } else {
55                 $has_due_date = 'true';
56         }
57
58         // use date from database
59         $array2 = explode (' ', $row['date_cutoff'], 2);
60         $array_date_cutoff = explode ('-', $array2[0],3);
61         $array_time_cutoff = explode (':', $array2[1]);
62         $cutoffyear             = $array_date_cutoff[0];
63         $cutoffmonth    = $array_date_cutoff[1];
64         $cutoffday              = $array_date_cutoff[2];
65         $cutoffhour             = $array_time_cutoff[0];
66         $cutoffminute   = $array_time_cutoff[1];
67
68         if ($cutoffyear == '0000'){
69                 $late_submit    = '0'; // allow late submissions always
70         } else if ($row['date_cutoff'] == $row['date_due']){
71                 $late_submit    = '1'; // allow late submissions never
72                 // use today's date as default
73                 $cutoffday              = $today['mday'];
74                 $cutoffmonth    = $today['mon'];
75                 $cutoffyear             = $today['year'];
76                 $cutoffhour             = $today['hours'];
77                 $cutoffminute   = $today['minutes'];
78                 // round the minute to the next highest multiple of 5 
79                 $cutoffminute = round($cutoffminute / '5' ) * '5' + '5';
80                 if ($cutoffminute > '55'){ $cutoffminute = '55'; }
81         } else {
82                 $late_submit    = '2'; // allow late submissions until (date)
83         }
84 }
85 else if (isset($_POST['cancel'])) {
86         // cancel, nothing happened
87         $msg->addFeedback('CANCELLED');
88         header('Location: index_instructor.php');
89         exit;
90 }
91 else if (isset($_POST['submit'])) {
92         // user has submitted form to update database
93         $id = intval ($_POST['id']);
94
95         if ($_POST['multi_submit'] == 'on'){
96                 $multi_submit = '1';
97         }
98
99         // get values from form that was just submitted
100         $title                  = $addslashes($_POST['title']);
101         $assign_to              = intval($_POST['assign_to']);
102         $has_due_date   = $addslashes($_POST['has_due_date']);
103         $late_submit    = intval($_POST['late_submit']);
104
105         $dueday                 = intval($_POST['day_due']);
106         $duemonth               = intval($_POST['month_due']);
107         $dueyear                = intval($_POST['year_due']);
108         $duehour                = intval($_POST['hour_due']);
109         $dueminute              = intval($_POST['min_due']);
110
111         $cutoffday              = intval($_POST['day_cutoff']);
112         $cutoffmonth    = intval($_POST['month_cutoff']);
113         $cutoffyear             = intval($_POST['year_cutoff']);
114         $cutoffhour             = intval($_POST['hour_cutoff']);
115         $cutoffminute   = intval($_POST['min_cutoff']);
116
117         // ensure title is not empty
118         if (trim($title) == '') {
119                 $msg->addError(array('EMPTY_FIELDS', _AT('title')));
120         }
121
122         // If due date is set and user has selected 'accept late submission until'
123         // then ensure cutoff date is greater or equal to due date.
124         if (($has_due_date == 'true') && ($late_submit == '2')){
125                 if ($cutoffyear < $dueyear){
126                         $msg->addError('CUTOFF_DATE_WRONG');
127                 } else if ($cutoffyear == $dueyear){
128                         if ($cutoffmonth < $duemonth){
129                                 $msg->addError('CUTOFF_DATE_WRONG');
130                         } else if ($cutoffmonth == $duemonth){
131                                 if ($cutoffday < $dueday){
132                                         $msg->addError('CUTOFF_DATE_WRONG');
133                                 } else if ($cutoffday == $dueday){
134                                         if ($cutoffhour < $duehour){
135                                                 $msg->addError('CUTOFF_DATE_WRONG');
136                                         } else if ($cutoffhour == $duehour) {
137                                                 if ($cutoffminute < $dueminute){
138                                                         $msg->addError('CUTOFF_DATE_WRONG');
139                                                 }
140                                         }
141                                 }
142                         }
143                 }
144         }
145
146         if (!$msg->containsErrors()) {
147                 $multi_submit = 0;
148
149                 // create the date strings
150                 $date_due = '0';
151                 $date_cutoff = '0';
152
153                 // note: if due date is NOT set then ignore the late submission date
154                 if ($has_due_date == 'true'){
155                         $date_due = $dueyear. '-' .str_pad ($duemonth, 2, "0", STR_PAD_LEFT). '-' .str_pad ($dueday, 2, "0", STR_PAD_LEFT). ' '.str_pad ($duehour, 2, "0", STR_PAD_LEFT). ':' .str_pad ($dueminute, 2, "0", STR_PAD_LEFT) . ':00';
156                 }
157
158                 if ($late_submit == '1'){ // never accept late submissions
159                         $date_cutoff = $date_due; // cutoff date will be same as due date
160                 } else if ($late_submit == '2'){ // accept late submissions until date
161                         $date_cutoff = $cutoffyear. '-' .str_pad ($cutoffmonth, 2, "0", STR_PAD_LEFT). '-' .str_pad ($cutoffday, 2, "0", STR_PAD_LEFT). ' '.str_pad ($cutoffhour, 2, "0", STR_PAD_LEFT). ':' .str_pad ($cutoffminute, 2, "0", STR_PAD_LEFT) . ':00';
162                 }
163
164                 // Are we creating a new assignment or updating an existing assignment?
165                 if ($id == '0'){
166                         // creating a new assignment
167                         $sql = "INSERT INTO ".TABLE_PREFIX."assignments VALUES (NULL, $_SESSION[course_id],
168                                 '$title',
169                                 '$assign_to',
170                                 '$date_due',
171                                 '$date_cutoff',
172                                 '$multi_submit'
173                                 )";
174
175                         $result = mysql_query($sql,$db);
176                         $msg->addFeedback('ASSIGNMENT_ADDED');
177                 } else { // updating an existing assignment
178                         $assign_to = 'assign_to';
179
180                         $sql = "UPDATE ".TABLE_PREFIX."assignments SET title='$title', assign_to=$assign_to, date_due='$date_due', date_cutoff='$date_cutoff' WHERE assignment_id='$id' AND course_id=$_SESSION[course_id]";
181
182                         $result = mysql_query($sql,$db);
183                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
184                 }
185                 header('Location: index_instructor.php');
186                 exit;
187         }
188 } else { // creating a new assignment
189         $title                  = '';
190         $assign_to              = '0';
191         $multi_submit   = '1';
192         $has_due_date   = 'false';
193         $late_submit    = '0'; // 0 == always, 1 == never, 2 = until (date)
194
195         $dueday         = $today['mday'];
196         $duemonth       = $today['mon'];
197         $dueyear        = $today['year'];
198         $duehour        = '12';
199         $dueminute      = '0';
200
201         $cutoffday              = $today['mday'];
202         $cutoffmonth    = $today['mon'];
203         $cutoffyear             = $today['year'];
204         $cutoffhour             = '12';
205         $cutoffminute   = '0';
206 }
207
208 // ensure the dates are valid
209 if ($dueyear == '0'){
210         // use today's date as default
211         $dueday         = $today['mday'];
212         $duemonth       = $today['mon'];
213         $dueyear        = $today['year'];
214         $duehour        = $today['hours'];
215         $dueminute      = $today['minutes'];
216         // round the minute to the next highest multiple of 5 
217         $dueminute = round($dueminute / '5' ) * '5' + '5';
218         if ($dueminute > '55'){ $dueminute = '55'; }
219 }
220 if ($cutoffyear == '0'){
221         // use today's date as default
222         $cutoffday              = $today['mday'];
223         $cutoffmonth    = $today['mon'];
224         $cutoffyear             = $today['year'];
225         $cutoffhour             = $today['hours'];
226         $cutoffminute   = $today['minutes'];
227         // round the minute to the next highest multiple of 5 
228         $cutoffminute = round($cutoffminute / '5' ) * '5' + '5';
229         if ($cutoffminute > '55'){ $cutoffminute = '55'; }
230 }
231
232 $onload = 'document.form.title.focus();';
233
234 // enable/disable date controls
235 if ($has_due_date == 'false'){ 
236         $onload .= ' disable_dates (true, \'_due\');';
237 }
238
239 if ($late_submit != '2'){
240         $onload .= ' disable_dates (true, \'_cutoff\');';
241 }
242
243 require(AT_INCLUDE_PATH.'header.inc.php');
244 ?>
245
246 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
247 <input type="hidden" name="id" value="<?php echo $id ?>" />
248 <div class="input-form">        
249
250         <div class="row">
251                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="title"><?php  echo _AT('title'); ?></label><br/>
252                 <input type="text" name="title" size="50" id="title" value="<?php echo htmlspecialchars($title); ?>" />
253         </div>
254
255         <div class="row">
256                 <label for="assignto"><?php  echo _AT('assign_to'); ?></label><br/>
257
258                 <?php // Are we editing an assignment?
259                         if ($id != '0'){
260                                 // editing an existing assignment 
261                                 if ($assign_to == '0'){ 
262                                         echo _AT('all_students'); 
263                                 } else { // name of group goes here
264                                         $sql = "SELECT title FROM ".TABLE_PREFIX."groups_types WHERE type_id=$assign_to AND course_id=$_SESSION[course_id]";
265                                         $result = mysql_query($sql, $db);
266                                         $type_row = mysql_fetch_assoc($result);
267                                         echo $type_row['title'];
268                                 }
269                                 ?>
270                         <?php } else { // creating a new assignment
271                         ?>
272                                 <select name="assign_to" size="5" id="assignto">
273                                         <option value="0" <?php if ($assign_to == '0'){ echo 'selected="selected"'; } ?> label="<?php  echo _AT('all_students'); ?>"><?php  echo _AT('all_students'); ?></option>
274                                         <optgroup label="<?php  echo _AT('specific_groups'); ?>">
275                                                 <?php
276                                                         $sql = "SELECT type_id, title FROM ".TABLE_PREFIX."groups_types WHERE course_id={$_SESSION['course_id']} ORDER BY title";
277                                                         $result = mysql_query($sql, $db);
278                                                         while ($type_row = mysql_fetch_assoc($result)) {
279                                                                 echo '<option value="'.$type_row['type_id'].'" ';
280                                                                 if ($assign_to == $type_row['type_id']) {
281                                                                         echo 'selected="selected"';
282                                                                 }
283                                                                 echo '>'.$type_row['title'].'</option>';
284                                                         }
285                                                 ?>
286                                         </optgroup>
287                                 </select>
288                         <?php } ?>
289         </div>  
290
291         <div class="row">
292                 <?php  echo _AT('due_date'); ?><br />
293                 <input type="radio" name="has_due_date" value="false" id="noduedate" <?php if ($has_due_date == 'false') { echo 'checked="checked"'; } ?> 
294                 onfocus="disable_dates (true, '_due');" />
295                 <label for="noduedate" title="<?php echo _AT('due_date'). ': '. _AT('none');  ?>"><?php echo _AT('none'); ?></label><br />
296
297                 <input type="radio" name="has_due_date" value="true" id="hasduedate" <?php if ($has_due_date == 'true'){echo 'checked="checked"'; } ?> 
298                 onfocus="disable_dates (false, '_due');" />
299                 <label for="hasduedate"  title="<?php echo _AT('due_date') ?>"><?php  echo _AT('date'); ?></label>
300
301                 <?php
302                         $today_day  = $dueday;
303                         $today_mon  = $duemonth;
304                         $today_year = $dueyear;
305                         $today_hour = $duehour;
306                         $today_min  = $dueminute;
307                         
308                         $name = '_due';
309                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
310                 ?>
311         </div>
312
313         <div class="row">
314                 <?php  echo _AT('accept_late_submissions'); ?><br />
315                 <input type="radio" name="late_submit" value="0" id="always"  <?php if ($late_submit == '0'){echo 'checked="checked"';} ?> 
316                 onfocus="disable_dates (true, '_cutoff');" />
317
318                 <label for="always" title="<?php echo _AT('accept_late_submissions'). ': '. _AT('always');  ?>"><?php echo _AT('always'); ?></label><br />
319
320                 <input type="radio" name="late_submit" value="1" id="never"  <?php if ($late_submit == '1'){echo 'checked="checked"';} ?>
321                 onfocus="disable_dates (true, '_cutoff');" />
322
323                 <label for="never" title="<?php echo _AT('accept_late_submissions'). ': '. _AT('never');  ?>"><?php  echo _AT('never'); ?></label><br />
324
325                 <input type="radio" name="late_submit" value="2" id="until"  <?php if ($late_submit == '2'){echo 'checked="checked"';} ?>
326                 onfocus="disable_dates (false, '_cutoff');" />
327
328                 <label for="until" title="<?php echo _AT('accept_late_submissions'). ': '. _AT('until');  ?>"><?php  echo _AT('until'); ?></label>
329
330                 <?php
331                         $today_day  = $cutoffday;
332                         $today_mon  = $cutoffmonth;
333                         $today_year = $cutoffyear;
334                         $today_hour = $cutoffhour;
335                         $today_min  = $cutoffminute;
336                         
337                         $name = '_cutoff';
338                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
339                 ?>
340         </div>
341         <?php
342         /****
343          * not included in the initial release.
344          *
345         <div class="row">
346                 <?php  echo _AT('options'); <br/>
347                 <input type="checkbox" name="multi_submit" id="multisubmit" <?php if ($multi_submit == '1'){ echo 'checked="checked"'; }  />
348                 <label for="multisubmit"><?php  echo _AT('allow_re_submissions'); </label>
349         </div>
350         ***/
351         ?>
352         
353         <div class="row buttons">
354                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
355                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
356         </div>
357 </div>
358 </form>
359
360 <script language="javascript" type="text/javascript">
361 function disable_dates (state, name) {
362         document.form['day' + name].disabled=state;
363         document.form['month' + name].disabled=state;
364         document.form['year' + name].disabled=state;
365         document.form['hour' + name].disabled=state;
366         document.form['min' + name].disabled=state;
367 }
368 </script>
369
370 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>