made a copy
[atutor.git] / assignments / add_assignment.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008                                      */
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         } else {
121                 //ensure the title does not exceed db length, 60
122                 $title = validate_length($title, 60);
123         }
124
125         // If due date is set and user has selected 'accept late submission until'
126         // then ensure cutoff date is greater or equal to due date.
127         if (($has_due_date == 'true') && ($late_submit == '2')){
128                 if ($cutoffyear < $dueyear){
129                         $msg->addError('CUTOFF_DATE_WRONG');
130                 } else if ($cutoffyear == $dueyear){
131                         if ($cutoffmonth < $duemonth){
132                                 $msg->addError('CUTOFF_DATE_WRONG');
133                         } else if ($cutoffmonth == $duemonth){
134                                 if ($cutoffday < $dueday){
135                                         $msg->addError('CUTOFF_DATE_WRONG');
136                                 } else if ($cutoffday == $dueday){
137                                         if ($cutoffhour < $duehour){
138                                                 $msg->addError('CUTOFF_DATE_WRONG');
139                                         } else if ($cutoffhour == $duehour) {
140                                                 if ($cutoffminute < $dueminute){
141                                                         $msg->addError('CUTOFF_DATE_WRONG');
142                                                 }
143                                         }
144                                 }
145                         }
146                 }
147         }
148
149         if (!$msg->containsErrors()) {
150                 $multi_submit = 0;
151
152                 // create the date strings
153                 $date_due = '0';
154                 $date_cutoff = '0';
155
156                 // note: if due date is NOT set then ignore the late submission date
157                 if ($has_due_date == 'true'){
158                         $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';
159                 }
160
161                 if ($late_submit == '1'){ // never accept late submissions
162                         $date_cutoff = $date_due; // cutoff date will be same as due date
163                 } else if ($late_submit == '2'){ // accept late submissions until date
164                         $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';
165                 }
166
167                 // Are we creating a new assignment or updating an existing assignment?
168                 if ($id == '0'){
169                         // creating a new assignment
170                         $sql = "INSERT INTO ".TABLE_PREFIX."assignments VALUES (NULL, $_SESSION[course_id],
171                                 '$title',
172                                 '$assign_to',
173                                 '$date_due',
174                                 '$date_cutoff',
175                                 '$multi_submit'
176                                 )";
177
178                         $result = mysql_query($sql,$db);
179                         $msg->addFeedback('ASSIGNMENT_ADDED');
180                 } else { // updating an existing assignment
181                         $assign_to = 'assign_to';
182
183                         $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]";
184
185                         $result = mysql_query($sql,$db);
186                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
187                 }
188                 header('Location: index_instructor.php');
189                 exit;
190         }
191 } else { // creating a new assignment
192         $title                  = '';
193         $assign_to              = '0';
194         $multi_submit   = '1';
195         $has_due_date   = 'false';
196         $late_submit    = '0'; // 0 == always, 1 == never, 2 = until (date)
197
198         $dueday         = $today['mday'];
199         $duemonth       = $today['mon'];
200         $dueyear        = $today['year'];
201         $duehour        = '12';
202         $dueminute      = '0';
203
204         $cutoffday              = $today['mday'];
205         $cutoffmonth    = $today['mon'];
206         $cutoffyear             = $today['year'];
207         $cutoffhour             = '12';
208         $cutoffminute   = '0';
209 }
210
211 // ensure the dates are valid
212 if ($dueyear == '0'){
213         // use today's date as default
214         $dueday         = $today['mday'];
215         $duemonth       = $today['mon'];
216         $dueyear        = $today['year'];
217         $duehour        = $today['hours'];
218         $dueminute      = $today['minutes'];
219         // round the minute to the next highest multiple of 5 
220         $dueminute = round($dueminute / '5' ) * '5' + '5';
221         if ($dueminute > '55'){ $dueminute = '55'; }
222 }
223 if ($cutoffyear == '0'){
224         // use today's date as default
225         $cutoffday              = $today['mday'];
226         $cutoffmonth    = $today['mon'];
227         $cutoffyear             = $today['year'];
228         $cutoffhour             = $today['hours'];
229         $cutoffminute   = $today['minutes'];
230         // round the minute to the next highest multiple of 5 
231         $cutoffminute = round($cutoffminute / '5' ) * '5' + '5';
232         if ($cutoffminute > '55'){ $cutoffminute = '55'; }
233 }
234
235 $onload = 'document.form.title.focus();';
236
237 // enable/disable date controls
238 if ($has_due_date == 'false'){ 
239         $onload .= ' disable_dates (true, \'_due\');';
240 }
241
242 if ($late_submit != '2'){
243         $onload .= ' disable_dates (true, \'_cutoff\');';
244 }
245
246 require(AT_INCLUDE_PATH.'header.inc.php');
247 ?>
248
249 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
250 <input type="hidden" name="id" value="<?php echo $id ?>" />
251 <div class="input-form">        
252         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('add_assignment'); ?></legend>
253         <div class="row">
254                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="title"><?php  echo _AT('title'); ?></label><br/>
255                 <input type="text" name="title" size="50" id="title" value="<?php echo htmlspecialchars($title); ?>" />
256         </div>
257
258         <div class="row">
259                 <label for="assignto"><?php  echo _AT('assign_to'); ?></label><br/>
260
261                 <?php // Are we editing an assignment?
262                         if ($id != '0'){
263                                 // editing an existing assignment 
264                                 if ($assign_to == '0'){ 
265                                         echo _AT('all_students'); 
266                                 } else { // name of group goes here
267                                         $sql = "SELECT title FROM ".TABLE_PREFIX."groups_types WHERE type_id=$assign_to AND course_id=$_SESSION[course_id]";
268                                         $result = mysql_query($sql, $db);
269                                         $type_row = mysql_fetch_assoc($result);
270                                         echo $type_row['title'];
271                                 }
272                                 ?>
273                         <?php } else { // creating a new assignment
274                         ?>
275                                 <select name="assign_to" size="5" id="assignto">
276                                         <option value="0" <?php if ($assign_to == '0'){ echo 'selected="selected"'; } ?> label="<?php  echo _AT('all_students'); ?>"><?php  echo _AT('all_students'); ?></option>
277                                         <optgroup label="<?php  echo _AT('specific_groups'); ?>">
278                                                 <?php
279                                                         $sql = "SELECT type_id, title FROM ".TABLE_PREFIX."groups_types WHERE course_id={$_SESSION['course_id']} ORDER BY title";
280                                                         $result = mysql_query($sql, $db);
281                                                         while ($type_row = mysql_fetch_assoc($result)) {
282                                                                 echo '<option value="'.$type_row['type_id'].'" ';
283                                                                 if ($assign_to == $type_row['type_id']) {
284                                                                         echo 'selected="selected"';
285                                                                 }
286                                                                 echo '>'.$type_row['title'].'</option>';
287                                                         }
288                                                 ?>
289                                         </optgroup>
290                                 </select>
291                         <?php } ?>
292         </div>  
293
294         <div class="row">
295                 <?php  echo _AT('due_date'); ?><br />
296                 <input type="radio" name="has_due_date" value="false" id="noduedate" <?php if ($has_due_date == 'false') { echo 'checked="checked"'; } ?> 
297                 onfocus="disable_dates (true, '_due');" />
298                 <label for="noduedate" title="<?php echo _AT('due_date'). ': '. _AT('none');  ?>"><?php echo _AT('none'); ?></label><br />
299
300                 <input type="radio" name="has_due_date" value="true" id="hasduedate" <?php if ($has_due_date == 'true'){echo 'checked="checked"'; } ?> 
301                 onfocus="disable_dates (false, '_due');" />
302                 <label for="hasduedate"  title="<?php echo _AT('due_date') ?>"><?php  echo _AT('date'); ?></label>
303
304                 <?php
305                         $today_day  = $dueday;
306                         $today_mon  = $duemonth;
307                         $today_year = $dueyear;
308                         $today_hour = $duehour;
309                         $today_min  = $dueminute;
310                         
311                         $name = '_due';
312                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
313                 ?>
314         </div>
315
316         <div class="row">
317                 <?php  echo _AT('accept_late_submissions'); ?><br />
318                 <input type="radio" name="late_submit" value="0" id="always"  <?php if ($late_submit == '0'){echo 'checked="checked"';} ?> 
319                 onfocus="disable_dates (true, '_cutoff');" />
320
321                 <label for="always" title="<?php echo _AT('accept_late_submissions'). ': '. _AT('always');  ?>"><?php echo _AT('always'); ?></label><br />
322
323                 <input type="radio" name="late_submit" value="1" id="never"  <?php if ($late_submit == '1'){echo 'checked="checked"';} ?>
324                 onfocus="disable_dates (true, '_cutoff');" />
325
326                 <label for="never" title="<?php echo _AT('accept_late_submissions'). ': '. _AT('never');  ?>"><?php  echo _AT('never'); ?></label><br />
327
328                 <input type="radio" name="late_submit" value="2" id="until"  <?php if ($late_submit == '2'){echo 'checked="checked"';} ?>
329                 onfocus="disable_dates (false, '_cutoff');" />
330
331                 <label for="until" title="<?php echo _AT('accept_late_submissions'). ': '. _AT('until');  ?>"><?php  echo _AT('until'); ?></label>
332
333                 <?php
334                         $today_day  = $cutoffday;
335                         $today_mon  = $cutoffmonth;
336                         $today_year = $cutoffyear;
337                         $today_hour = $cutoffhour;
338                         $today_min  = $cutoffminute;
339                         
340                         $name = '_cutoff';
341                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
342                 ?>
343         </div>
344         <?php
345         /****
346          * not included in the initial release.
347          *
348         <div class="row">
349                 <?php  echo _AT('options'); <br/>
350                 <input type="checkbox" name="multi_submit" id="multisubmit" <?php if ($multi_submit == '1'){ echo 'checked="checked"'; }  />
351                 <label for="multisubmit"><?php  echo _AT('allow_re_submissions'); </label>
352         </div>
353         ***/
354         ?>
355         
356         <div class="row buttons">
357                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
358                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
359         </div>
360         </fieldset>
361 </div>
362
363 </form>
364
365 <script language="javascript" type="text/javascript">
366 function disable_dates (state, name) {
367         document.form['day' + name].disabled=state;
368         document.form['month' + name].disabled=state;
369         document.form['year' + name].disabled=state;
370         document.form['hour' + name].disabled=state;
371         document.form['min' + name].disabled=state;
372 }
373 </script>
374
375 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>