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