remove old readme
[atutor.git] / mods / _standard / tests / create_test.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 $page = 'tests';
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17
18 authenticate(AT_PRIV_TESTS);
19
20 $test_type = 'normal';
21
22 if (isset($_POST['cancel'])) {
23         $msg->addFeedback('CANCELLED');
24         header('Location: index.php');
25         exit;
26 } else if (isset($_POST['submit'])) {
27         $missing_fields        = array();
28         $_POST['title']        = $addslashes(trim($_POST['title']));
29         $_POST['description']  = $addslashes(trim($_POST['description']));
30         $_POST['num_questions'] = intval($_POST['num_questions']);
31         $_POST['num_takes']        = intval($_POST['num_takes']);
32         $_POST['content_id']   = intval($_POST['content_id']);
33         $_POST['passpercent']   = intval($_POST['passpercent']);
34         $_POST['passscore']     = intval($_POST['passscore']);
35         $_POST['passfeedback']  = $addslashes(trim($_POST['passfeedback']));
36         $_POST['failfeedback']  = $addslashes(trim($_POST['failfeedback']));
37         $_POST['num_takes']    = intval($_POST['num_takes']);
38         $_POST['anonymous']    = intval($_POST['anonymous']);
39         $_POST['allow_guests'] = $_POST['allow_guests'] ? 1 : 0;
40         $_POST['show_guest_form'] = $_POST['show_guest_form'] ? 1 : 0;
41         $_POST['instructions'] = $addslashes($_POST['instructions']);
42         $_POST['display']          = intval($_POST['display']);
43
44         // currently these options are ignored for tests:
45         $_POST['result_release'] = intval($_POST['result_release']); 
46         $_POST['format']       = intval($_POST['format']);
47         $_POST['order']        = 1;  //intval($_POST['order']);
48         $_POST['difficulty']   = 0;  //intval($_POST['difficulty']);    /* avman */
49             
50         if ($_POST['title'] == '') {
51                 $missing_fields[] = _AT('title');
52         }
53
54         if ($_POST['random'] && !$_POST['num_questions']) {
55                 $missing_fields[] = _AT('num_questions_per_test');
56         }
57
58         if ($_POST['pass_score']==1 && !$_POST['passpercent']) {
59                 $missing_fields[] = _AT('percentage_score');
60         }
61
62         if ($_POST['pass_score']==2 && !$_POST['passscore']) {
63                 $missing_fields[] = _AT('points_score');
64         }
65
66         if ($missing_fields) {
67                 $missing_fields = implode(', ', $missing_fields);
68                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
69         }
70
71         $day_start      = intval($_POST['day_start']);
72         $month_start= intval($_POST['month_start']);
73         $year_start     = intval($_POST['year_start']);
74         $hour_start     = intval($_POST['hour_start']);
75         $min_start      = intval($_POST['min_start']);
76
77         $day_end        = intval($_POST['day_end']);
78         $month_end      = intval($_POST['month_end']);
79         $year_end       = intval($_POST['year_end']);
80         $hour_end       = intval($_POST['hour_end']);
81         $min_end        = intval($_POST['min_end']);
82
83         if (!checkdate($month_start, $day_start, $year_start)) {
84                 $msg->addError('START_DATE_INVALID');
85         }
86
87         if (!checkdate($month_end, $day_end, $year_end)) {
88                 $msg->addError('END_DATE_INVALID');
89         }
90
91         if (mktime($hour_end,   $min_end,   0, $month_end,   $day_end,   $year_end) < 
92                 mktime($hour_start, $min_start, 0, $month_start, $day_start, $year_start)) {
93                         $msg->addError('END_DATE_INVALID');
94         }
95
96         if (!$msg->containsErrors()) {
97                 if (strlen($month_start) == 1){
98                         $month_start = "0$month_start";
99                 }
100                 if (strlen($day_start) == 1){
101                         $day_start = "0$day_start";
102                 }
103                 if (strlen($hour_start) == 1){
104                         $hour_start = "0$hour_start";
105                 }
106                 if (strlen($min_start) == 1){
107                         $min_start = "0$min_start";
108                 }
109
110                 if (strlen($month_end) == 1){
111                         $month_end = "0$month_end";
112                 }
113                 if (strlen($day_end) == 1){
114                         $day_end = "0$day_end";
115                 }
116                 if (strlen($hour_end) == 1){
117                         $hour_end = "0$hour_end";
118                 }
119                 if (strlen($min_end) == 1){
120                         $min_end = "0$min_end";
121                 }
122
123                 $start_date = "$year_start-$month_start-$day_start $hour_start:$min_start:00";
124                 $end_date       = "$year_end-$month_end-$day_end $hour_end:$min_end:00";
125
126                 //If title exceeded database defined length, truncate it.
127                 $_POST['title'] = validate_length($_POST['title'], 100);
128
129                 $sql = "INSERT INTO ".TABLE_PREFIX."tests " .
130                        "(test_id,
131              course_id,
132              title,
133              description,
134              format,
135              start_date,
136              end_date,
137              randomize_order,
138              num_questions,
139              instructions,
140              content_id,
141              passscore,
142              passpercent,
143              passfeedback,
144              failfeedback,
145              result_release,
146              random,
147              difficulty,
148              num_takes,
149              anonymous,
150              out_of,
151              guests,
152              display,
153              show_guest_form)" .
154                        "VALUES 
155                         (NULL, 
156                          $_SESSION[course_id], 
157                          '$_POST[title]', 
158                          '$_POST[description]', 
159                          $_POST[format], 
160                          '$start_date', 
161                          '$end_date', 
162                          $_POST[order], 
163                          $_POST[num_questions], 
164                          '$_POST[instructions]', 
165                          $_POST[content_id], 
166                          $_POST[passscore], 
167                          $_POST[passpercent], 
168                          '$_POST[passfeedback]', 
169                          '$_POST[failfeedback]', 
170                          $_POST[result_release], 
171                          $_POST[random], 
172                          $_POST[difficulty], 
173                          $_POST[num_takes], 
174                          $_POST[anonymous], 
175                          '', 
176                          $_POST[allow_guests], 
177                          $_POST[display],
178                          $_POST[show_guest_form])";
179
180                 $result = mysql_query($sql, $db);
181                 $tid = mysql_insert_id($db);
182                 
183                 if (isset($_POST['groups']) && $tid) {
184                         $sql = "INSERT INTO ".TABLE_PREFIX."tests_groups VALUES ";
185                         foreach ($_POST['groups'] as $group) {
186                                 $group = intval($group);
187                                 $sql .= "($tid, $group),";
188                         }
189                         $sql = substr($sql, 0, -1);
190                         $result = mysql_query($sql, $db);
191                 }
192
193                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
194                 header('Location: index.php');
195                 exit;
196         }
197 }
198
199 if (isset($_POST['num_questions']) && ($_POST['num_questions'] === 0)) {
200         $_POST['num_questions'] = '';
201 }
202
203 $onload = 'document.form.title.focus();';
204
205 require(AT_INCLUDE_PATH.'header.inc.php');
206
207 $msg->printErrors();
208
209 ?>
210 <script language="javascript" type="text/javascript">
211 function disable_texts (name) {
212         if (name == 'both')
213         {
214                 document.form['passpercent'].disabled=true;
215                 document.form['passscore'].disabled=true;
216                 document.form['passpercent'].value=0;
217                 document.form['passscore'].value=0;
218         }
219         else if (name == 'percentage')
220         {
221                 document.form['passpercent'].disabled=true;
222                 document.form['passpercent'].value=0;
223                 document.form['passscore'].disabled=false;
224         }
225         else if (name == 'points')
226         {
227                 document.form['passpercent'].disabled=false;
228                 document.form['passscore'].disabled=true;
229                 document.form['passscore'].value=0;
230         }
231 }
232 </script>
233
234 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
235 <input type="hidden" name="test_type" value="<?php echo $test_type; ?>" />
236 <div class="input-form">
237         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('create_test'); ?></legend>
238         <div class="row">
239                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="title"><?php echo _AT('title'); ?></label><br />
240                 <input type="text" name="title" id="title" size="30" value="<?php echo $_POST['title']; ?>" />
241         </div>
242
243         <div class="row">
244                 <label for="description"><?php echo _AT('test_description'); ?></label><br />
245                 <textarea name="description" cols="35" rows="3" id="description"><?php echo htmlspecialchars($_POST['description']); ?></textarea>
246         </div>
247
248         <div class="row">
249                 <label for="num_t"><?php echo _AT('num_takes_test'); ?></label><br />
250                 <select name="num_takes" id="num_t">
251                         <option value="<?php echo AT_TESTS_TAKE_UNLIMITED; ?>" <?php if ($_POST['num_takes'] == AT_TESTS_TAKE_UNLIMITED) { echo 'selected="selected"'; } ?>><?php echo _AT('unlimited'); ?></option>
252                 
253                         <option value="1"<?php if ($_POST['num_takes'] == 1) { echo ' selected="selected"'; } ?>>1</option>
254                         <option value="2"<?php if ($_POST['num_takes'] == 2) { echo ' selected="selected"'; } ?>>2</option>
255                         <option value="3"<?php if ($_POST['num_takes'] == 3) { echo ' selected="selected"'; } ?>>3</option>
256                         <option value="4"<?php if ($_POST['num_takes'] == 4) { echo ' selected="selected"'; } ?>>4</option>
257                         <option value="5"<?php if ($_POST['num_takes'] == 5) { echo ' selected="selected"'; } ?>>5</option>
258                         <option value="6"<?php if ($_POST['num_takes'] == 6) { echo ' selected="selected"'; } ?>>6</option>
259                         <option value="7"<?php if ($_POST['num_takes'] == 7) { echo ' selected="selected"'; } ?>>7</option>
260                         <option value="8"<?php if ($_POST['num_takes'] == 8) { echo ' selected="selected"'; } ?>>8</option>
261                         <option value="9"<?php if ($_POST['num_takes'] == 9) { echo ' selected="selected"'; } ?>>9</option>
262                         <option value="10"<?php if ($_POST['num_takes'] == 10) { echo ' selected="selected"'; } ?>>10</option>
263                         <option value="15"<?php if ($_POST['num_takes'] == 15) { echo ' selected="selected"'; } ?>>15</option>
264                         <option value="20"<?php if ($_POST['num_takes'] >= 20) { echo ' selected="selected"'; } ?>>20</option>
265                 </select>
266         </div>
267
268         <div class="row">
269                 <?php echo _AT('available_on_my_courses'); ?><br />
270                 <?php 
271                         if ($_POST['format'] == 1) {
272                                 $y = 'checked="checked"';
273                                 $n = '';
274                         } else {
275                                 $y = '';
276                                 $n = 'checked="checked"';
277                         }
278                 ?>
279                 <input type="radio" name="format" id="formatN" value="0" <?php echo $n; ?> /><label for="formatN"><?php echo _AT('no'); ?></label> 
280                 <input type="radio" name="format" id="formatY" value="1" <?php echo $y; ?> /><label for="formatY"><?php echo _AT('yes'); ?></label>
281         </div>
282
283         <div class="row">
284                 <?php echo _AT('anonymous_test'); ?><br />
285                 <?php 
286                         if ($_POST['anonymous'] == 1) {
287                                 $y = 'checked="checked"';
288                                 $n = '';
289                         } else {
290                                 $y = '';
291                                 $n = 'checked="checked"';
292                         }
293                 ?>
294
295                 <input type="radio" name="anonymous" id="anonN" value="0" <?php echo $n; ?> /><label for="anonN"><?php echo _AT('no'); ?></label> 
296                 <input type="radio" name="anonymous" value="1" id="anonY" <?php echo $y; ?> /><label for="anonY"><?php echo _AT('yes'); ?></label>
297         </div>
298
299         <div class="row">
300                 <?php echo _AT('allow_guests'); ?><br />
301                 <?php 
302                         if ($_POST['allow_guests'] == 1) {
303                                 $y = 'checked="checked"';
304                                 $n = '';
305                                 $disable_show_guest_form = '';
306                         } else {
307                                 $y = '';
308                                 $n = 'checked="checked"';
309                                 $disable_show_guest_form = 'disabled="disabled"';
310                         }
311                 ?>
312
313                 <input type="radio" name="allow_guests" id="allow_guestsN" value="0" <?php echo $n; ?> onfocus="document.form.show_guest_form.checked=false; document.form.show_guest_form.disabled=true;" /><label for="allow_guestsN"><?php echo _AT('no'); ?></label> 
314                 <input type="radio" name="allow_guests" value="1" id="allow_guestsY" <?php echo $y; ?> onfocus="document.form.show_guest_form.disabled=false;" /><label for="allow_guestsY"><?php echo _AT('yes'); ?></label>
315     <br />
316                 <?php 
317                         if ($_POST['show_guest_form'] == 1)
318                                 $y = 'checked="checked"';
319                         else
320                                 $y = '';
321                 ?>
322
323                 <input type="checkbox" name="show_guest_form" id="show_guest_form" value="1" <?php echo $y . ' '. $disable_show_guest_form; ?> /><label for="show_guest_form"><?php echo _AT('show_guest_form'); ?></label> 
324         </div>
325
326         <div class="row">
327                 <?php echo _AT('display'); ?><br />
328                 <?php 
329                         if ($_POST['display'] == 1) {
330                                 $y = 'checked="checked"';
331                                 $n = '';
332                         } else {
333                                 $y = '';
334                                 $n = 'checked="checked"';
335                         }
336                 ?>
337
338                 <input type="radio" name="display" id="displayN" value="0" <?php echo $n; ?> /><label for="displayN"><?php echo _AT('all_questions_on_page'); ?></label> 
339                 <input type="radio" name="display" id="displayY" value="1" <?php echo $y; ?> /><label for="displayY"><?php echo _AT('one_question_per_page'); ?></label>
340         </div>
341
342         <div class="row">
343                 <?php echo _AT('pass_score'); ?><br />
344                 <input type="radio" name="pass_score" value="0" id="no" <?php if ($_POST['passpercent'] == 0 && $_POST['passscore'] == 0){echo 'checked="true"';} ?> 
345                  onfocus="disable_texts('both');" />
346
347                 <label for="no" title="<?php echo _AT('pass_score'). ': '. _AT('no_pass_score');  ?>"><?php echo _AT('no_pass_score'); ?></label><br />
348
349                 <input type="radio" name="pass_score" value="1" id="percentage"  <?php if ($_POST['passpercent'] <> 0){echo 'checked="true"';} ?>
350                  onfocus="disable_texts('points');" />
351
352                 <input type="text" name="passpercent" id="passpercent" size="2" value="<?php echo $_POST['passpercent']; ?>" 
353                  <?php if ($_POST['passpercent'] == 0){echo 'disabled="true"';} ?> /> 
354                 <label for="percentage" title="<?php echo _AT('pass_score'). ': '. _AT('percentage_score');  ?>"><?php  echo '% ' . _AT('percentage_score'); ?></label><br />
355
356                 <input type="radio" name="pass_score" value="2" id="points"  <?php if ($_POST['passscore'] <> 0){echo 'checked="true"';} ?>
357                  onfocus="disable_texts('percentage');" />
358
359                 <input type="text" name="passscore" id="passscore" size="2" value="<?php echo $_POST['passscore']; ?>" 
360                  <?php if ($_POST['passscore'] == 0){echo 'disabled="true"';} ?>/> 
361                 <label for="points" title="<?php echo _AT('pass_score'). ': '. _AT('points_score');  ?>"><?php  echo _AT('points_score'); ?></label>
362         </div>
363
364         <div class="row">
365                 <label for="passfeedback"><?php echo _AT('pass_feedback'); ?></label><br />
366                 <textarea name="passfeedback" cols="35" rows="1" id="passfeedback"><?php echo htmlspecialchars($_POST['passfeedback']); ?></textarea>
367         </div>
368
369         <div class="row">
370                 <label for="failfeedback"><?php echo _AT('fail_feedback'); ?></label><br />
371                 <textarea name="failfeedback" cols="35" rows="1" id="failfeedback"><?php echo htmlspecialchars($_POST['failfeedback']); ?></textarea>
372         </div>
373
374         <div class="row">
375                 <?php echo _AT('result_release'); ?><br />
376                 <?php 
377                         if ($_POST['result_release'] == AT_RELEASE_IMMEDIATE) {
378                                 $check_marked = $check_never = '';
379                                 $check_immediate = 'checked="checked"';
380                 
381                         } else if ($_POST['result_release'] == AT_RELEASE_MARKED) {
382                                 $check_immediate = $check_never = '';
383                                 $check_marked = 'checked="checked"';
384
385                         } else if ($_POST['result_release'] == AT_RELEASE_NEVER) {
386                                 $check_immediate = $check_marked = '';
387                                 $check_never = 'checked="checked"';
388                         }
389                 ?>
390                 <input type="radio" name="result_release" id="release1" value="<?php echo AT_RELEASE_IMMEDIATE; ?>" <?php echo $check_immediate; ?> /><label for="release1"><?php echo _AT('release_immediate'); ?></label><br />
391                 <input type="radio" name="result_release" id="release2" value="<?php echo AT_RELEASE_MARKED; ?>" <?php echo $check_marked; ?> /><label for="release2"><?php echo _AT('release_marked'); ?></label><br />
392                 <input type="radio" name="result_release" id="release3" value="<?php echo AT_RELEASE_NEVER; ?>" <?php echo $check_never; ?>/><label for="release3"><?php echo _AT('release_never'); ?></label>
393         </div>
394
395         <div class="row">
396                 <?php echo _AT('randomize_questions'); ?><br />
397                 <?php 
398                         if ($_POST['random'] == 1) {
399                                 $y = 'checked="checked"';
400                                 $n = $disabled = '';
401                         } else {
402                                 $y = '';
403                                 $n = 'checked="checked"';
404                                 $disabled = 'disabled="disabled" ';
405                         }
406                 ?>
407                 <input type="radio" name="random" id="random" value="0" checked="checked" onfocus="document.form.num_questions.disabled=true;" /><label for="random"><?php echo _AT('no'); ?></label>. <input type="radio" name="random" value="1" id="ry" onfocus="document.form.num_questions.disabled=false;" <?php echo $y; ?> /><label for="ry"><?php echo _AT('yes'); ?></label>, <input type="text" name="num_questions" id="num_questions" size="2" value="<?php echo $_POST['num_questions']; ?>" <?php echo $disabled . $n; ?> /> <label for="num_questions"><?php echo _AT('num_questions_per_test'); ?></label>
408         </div>
409
410
411         <div class="row">
412                 <?php echo _AT('start_date');  ?><br />
413                 <?php
414                         if (!isset($_POST['submit'])) {
415                                 $today_day  = date('d');
416                                 $today_mon  = date('m');
417                                 $today_year = date('Y');
418                                 $today_hour = date('H');
419                                 $today_min  = 0;
420                         } else {
421                                 $today_day  = intval($day_start);
422                                 $today_mon  = intval($month_start);
423                                 $today_year = intval($year_start);
424                                 $today_hour = intval($hour_start);
425                                 $today_min  = intval($min_start);
426                         }
427
428                         $name = '_start';
429                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
430
431                 ?>
432         </div>
433
434         <div class="row">
435                 <?php echo _AT('end_date');  ?><br />
436                 <?php
437                         if (!isset($_POST['submit'])) {
438                                 $today_day  = date('d');
439                                 $today_mon  = date('m');
440                                 $today_year = date('Y');
441                                 $today_hour = date('H');
442                                 $today_min  = 0;
443                         } else {
444                                 $today_day  = intval($day_end);
445                                 $today_mon  = intval($month_end);
446                                 $today_year = intval($year_end);
447                                 $today_hour = intval($hour_end);
448                                 $today_min  = intval($min_end);
449                         }
450                                         
451                         $name = '_end';
452                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
453                 ?>
454         </div>
455
456         <div class="row">
457                 <?php echo _AT('limit_to_group'); ?><br />
458                 <?php
459                         //show groups
460                         $sql    = "SELECT * FROM ".TABLE_PREFIX."groups_types WHERE course_id=$_SESSION[course_id] ORDER BY title";
461                         $result = mysql_query($sql, $db);
462                         if (mysql_num_rows($result)) {
463                                 while ($row = mysql_fetch_assoc($result)) {
464                                         echo '<strong>'.$row['title'].'</strong><br />';
465
466                                         $sql    = "SELECT * FROM ".TABLE_PREFIX."groups WHERE type_id=$row[type_id] ORDER BY title";
467                                         $g_result = mysql_query($sql, $db);
468                                         while ($grow = mysql_fetch_assoc($g_result)) {
469                                                 echo '&nbsp;<label><input type="checkbox" value="'.$grow['group_id'].'" name="groups['.$grow['group_id'].']" '; 
470                                                 if (is_array($current_groups) && in_array($grow['group_id'], $current_groups)) {
471                                                         echo 'checked="checked"';
472                                                 }
473                                                 echo '/>'.$grow['title'].'</label><br />';
474                                         }
475                                 }
476                         } else {
477                                 echo _AT('none_found');
478                         }
479                 ?>
480         </div>
481
482         <div class="row">
483                 <label for="inst"><?php echo _AT('instructions'); ?></label><br />
484                 <textarea name="instructions" cols="35" rows="3" id="inst"><?php echo htmlspecialchars($_POST['instructions']); ?></textarea>
485         </div>
486
487         <div class="row buttons">
488                 <input type="submit" value="<?php echo _AT('save'); ?>" name="submit" accesskey="s" />
489                 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
490         </div>
491         </fieldset>
492 </div>
493 </form>
494
495 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>