2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
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 /****************************************************************/
16 define('AT_INCLUDE_PATH', '../../../include/');
17 require(AT_INCLUDE_PATH.'vitals.inc.php');
18 require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/test_result_functions.inc.php');
20 authenticate(AT_PRIV_TESTS);
22 $tid = intval($_REQUEST['tid']);
24 if (isset($_POST['cancel'])) {
25 $msg->addFeedback('CANCELLED');
26 header('Location: index.php');
28 } else if (isset($_POST['submit'])) {
29 $missing_fields = array();
30 $_POST['title'] = $addslashes(trim($_POST['title']));
31 $_POST['description'] = $addslashes(trim($_POST['description']));
32 $_POST['format'] = intval($_POST['format']);
33 $_POST['randomize_order'] = intval($_POST['randomize_order']);
34 $_POST['num_questions'] = intval($_POST['num_questions']);
35 $_POST['passpercent'] = intval($_POST['passpercent']);
36 $_POST['passscore'] = intval($_POST['passscore']);
37 $_POST['passfeedback'] = $addslashes(trim($_POST['passfeedback']));
38 $_POST['failfeedback'] = $addslashes(trim($_POST['failfeedback']));
39 $_POST['num_takes'] = intval($_POST['num_takes']);
40 $_POST['anonymous'] = intval($_POST['anonymous']);
41 $_POST['display'] = intval($_POST['display']);
42 $_POST['allow_guests'] = $_POST['allow_guests'] ? 1 : 0;
43 $_POST['show_guest_form'] = $_POST['show_guest_form'] ? 1 : 0;
44 $_POST['instructions'] = $addslashes($_POST['instructions']);
45 $_POST['result_release'] = intval($_POST['result_release']);
47 /* this doesn't actually get used: */
48 $_POST['difficulty'] = intval($_POST['difficulty']);
49 if ($_POST['difficulty'] == '') {
50 $_POST['difficulty'] = 0;
53 $_POST['content_id'] = intval($_POST['content_id']);
54 if ($_POST['content_id'] == '') {
55 $_POST['content_id'] = 0;
59 $_POST['instructions'] = trim($_POST['instructions']);
61 if ($_POST['title'] == '') {
62 $missing_fields[] = _AT('title');
65 if ($_POST['random'] && !$_POST['num_questions']) {
66 $missing_fields[] = _AT('num_questions_per_test');
69 if ($_POST['pass_score']==1 && !$_POST['passpercent']) {
70 $missing_fields[] = _AT('percentage_score');
73 if ($_POST['pass_score']==2 && !$_POST['passscore']) {
74 $missing_fields[] = _AT('points_score');
79 * If test is anonymous and have submissions, then we don't permit changes.
80 * This addresses the following issue: http://www.atutor.ca/atutor/mantis/view.php?id=3268
81 * TODO: Add an extra column in test_results to remember the state of anonymous submissions.
82 * make changes accordingly on line 255 as well.
84 $sql = "SELECT t.test_id, anonymous FROM ".TABLE_PREFIX."tests_results r NATURAL JOIN ".TABLE_PREFIX."tests t WHERE r.test_id = t.test_id AND r.test_id=$tid";
85 $result = mysql_query($sql, $db);
86 if ($row = mysql_fetch_assoc($result)) {
87 //If there are submission(s) for this test, anonymous field will not be altered.
88 $_POST['anonymous'] = $row['anonymous'];
91 if ($missing_fields) {
92 $missing_fields = implode(', ', $missing_fields);
93 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
96 $day_start = intval($_POST['day_start']);
97 $month_start= intval($_POST['month_start']);
98 $year_start = intval($_POST['year_start']);
99 $hour_start = intval($_POST['hour_start']);
100 $min_start = intval($_POST['min_start']);
102 $day_end = intval($_POST['day_end']);
103 $month_end = intval($_POST['month_end']);
104 $year_end = intval($_POST['year_end']);
105 $hour_end = intval($_POST['hour_end']);
106 $min_end = intval($_POST['min_end']);
108 if (!checkdate($month_start, $day_start, $year_start)) {
109 $msg->addError('START_DATE_INVALID');
112 if (!checkdate($month_end, $day_end, $year_end)) {
113 $msg->addError('END_DATE_INVALID');
116 if (mktime($hour_end, $min_end, 0, $month_end, $day_end, $year_end) <
117 mktime($hour_start, $min_start, 0, $month_start, $day_start, $year_start)) {
118 $msg->addError('END_DATE_INVALID');
121 if (strlen($month_start) == 1){
122 $month_start = "0$month_start";
124 if (strlen($day_start) == 1){
125 $day_start = "0$day_start";
127 if (strlen($hour_start) == 1){
128 $hour_start = "0$hour_start";
130 if (strlen($min_start) == 1){
131 $min_start = "0$min_start";
133 if (strlen($month_end) == 1){
134 $month_end = "0$month_end";
136 if (strlen($day_end) == 1){
137 $day_end = "0$day_end";
139 if (strlen($hour_end) == 1){
140 $hour_end = "0$hour_end";
142 if (strlen($min_end) == 1){
143 $min_end = "0$min_end";
146 $start_date = "$year_start-$month_start-$day_start $hour_start:$min_start:00";
147 $end_date = "$year_end-$month_end-$day_end $hour_end:$min_end:00";
149 if (!$msg->containsErrors()) {
150 // just to make sure we own this test:
151 $sql = "SELECT * FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
152 $result = mysql_query($sql, $db);
154 if ($row = mysql_fetch_assoc($result)) {
155 if ($_POST['random']) {
156 $total_weight = get_total_weight($tid, $_POST['num_questions']);
158 $total_weight = get_total_weight($tid);
160 //If title exceeded database defined length, truncate it.
161 $_POST['title'] = validate_length($_POST['title'], 100);
163 $sql = "UPDATE ".TABLE_PREFIX."tests " .
164 "SET title='$_POST[title]',
165 description='$_POST[description]',
166 format=$_POST[format],
167 start_date='$start_date',
168 end_date='$end_date',
169 randomize_order=$_POST[randomize_order],
170 num_questions=$_POST[num_questions],
171 instructions='$_POST[instructions]',
172 content_id=$_POST[content_id],
173 passscore=$_POST[passscore],
174 passpercent=$_POST[passpercent],
175 passfeedback='$_POST[passfeedback]',
176 failfeedback='$_POST[failfeedback]',
177 result_release=$_POST[result_release],
178 random=$_POST[random],
179 difficulty=$_POST[difficulty],
180 num_takes=$_POST[num_takes],
181 anonymous=$_POST[anonymous],
182 guests=$_POST[allow_guests],
183 show_guest_form=$_POST[show_guest_form],
184 out_of=$total_weight,
185 display=$_POST[display]
187 AND course_id=$_SESSION[course_id]";
189 $result = mysql_query($sql, $db);
191 $sql = "DELETE FROM ".TABLE_PREFIX."tests_groups WHERE test_id=$tid";
192 $result = mysql_query($sql, $db);
194 if (isset($_POST['groups'])) {
195 $sql = "INSERT INTO ".TABLE_PREFIX."tests_groups VALUES ";
196 foreach ($_POST['groups'] as $group) {
197 $group = intval($group);
198 $sql .= "($tid, $group),";
200 $sql = substr($sql, 0, -1);
201 $result = mysql_query($sql, $db);
205 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
207 header('Location: index.php');
212 $onload = 'document.form.title.focus();';
214 require(AT_INCLUDE_PATH.'header.inc.php');
216 if (!isset($_POST['submit'])) {
217 $sql = "SELECT *, DATE_FORMAT(start_date, '%Y-%m-%d %H:%i:00') AS start_date, DATE_FORMAT(end_date, '%Y-%m-%d %H:%i:00') AS end_date FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
218 $result = mysql_query($sql, $db);
220 if (!($row = mysql_fetch_assoc($result))){
221 $msg->printErrors('ITEM_NOT_FOUND');
222 require (AT_INCLUDE_PATH.'footer.inc.php');
227 $_POST['allow_guests'] = $row['guests'];
229 $_POST['start_date'] = $start_date;
230 $_POST['end_date'] = $end_date;
237 <script language="javascript" type="text/javascript">
238 function disable_texts (name) {
241 document.form['passpercent'].disabled=true;
242 document.form['passscore'].disabled=true;
243 document.form['passpercent'].value=0;
244 document.form['passscore'].value=0;
246 else if (name == 'percentage')
248 document.form['passpercent'].disabled=true;
249 document.form['passpercent'].value=0;
250 document.form['passscore'].disabled=false;
252 else if (name == 'points')
254 document.form['passpercent'].disabled=false;
255 document.form['passscore'].disabled=true;
256 document.form['passscore'].value=0;
261 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
262 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
263 <input type="hidden" name="randomize_order" value="1" />
264 <input type="hidden" name="instructions" value="" />
265 <input type="hidden" name="difficulty" value="0" />
267 <div class="input-form">
268 <fieldset class="group_form"><legend class="group_form"><?php echo _AT('edit_test'); ?></legend>
270 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="title"><?php echo _AT('title'); ?></label><br />
271 <input type="text" name="title" id="title" size="40" value="<?php echo stripslashes(htmlspecialchars($_POST['title'])); ?>" />
275 <label for="description"><?php echo _AT('test_description'); ?></label><br />
276 <textarea name="description" cols="35" rows="3" id="description"><?php echo htmlspecialchars($_POST['description']); ?></textarea>
280 <label for="num_t"><?php echo _AT('num_takes_test'); ?></label><br />
281 <select name="num_takes" id="num_t">
282 <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>
284 <option value="1"<?php if ($_POST['num_takes'] == 1) { echo ' selected="selected"'; } ?>>1</option>
285 <option value="2"<?php if ($_POST['num_takes'] == 2) { echo ' selected="selected"'; } ?>>2</option>
286 <option value="3"<?php if ($_POST['num_takes'] == 3) { echo ' selected="selected"'; } ?>>3</option>
287 <option value="4"<?php if ($_POST['num_takes'] == 4) { echo ' selected="selected"'; } ?>>4</option>
288 <option value="5"<?php if ($_POST['num_takes'] == 5) { echo ' selected="selected"'; } ?>>5</option>
289 <option value="6"<?php if ($_POST['num_takes'] == 6) { echo ' selected="selected"'; } ?>>6</option>
290 <option value="7"<?php if ($_POST['num_takes'] == 7) { echo ' selected="selected"'; } ?>>7</option>
291 <option value="8"<?php if ($_POST['num_takes'] == 8) { echo ' selected="selected"'; } ?>>8</option>
292 <option value="9"<?php if ($_POST['num_takes'] == 9) { echo ' selected="selected"'; } ?>>9</option>
293 <option value="10"<?php if ($_POST['num_takes'] == 10) { echo ' selected="selected"'; } ?>>10</option>
294 <option value="15"<?php if ($_POST['num_takes'] == 15) { echo ' selected="selected"'; } ?>>15</option>
295 <option value="20"<?php if ($_POST['num_takes'] >= 20) { echo ' selected="selected"'; } ?>>20</option>
300 <?php echo _AT('available_on_my_courses'); ?><br />
302 if ($_POST['format'] == 1) {
303 $y = 'checked="checked"';
307 $n = 'checked="checked"';
310 <input type="radio" name="format" id="formatN" value="0" <?php echo $n; ?> /><label for="formatN"><?php echo _AT('no'); ?></label>
311 <input type="radio" name="format" id="formatY" value="1" <?php echo $y; ?> /><label for="formatY"><?php echo _AT('yes'); ?></label>
315 <?php echo _AT('anonymous_test'); ?><br />
317 if ($_POST['anonymous'] == 1) {
318 $y = 'checked="checked"';
322 $n = 'checked="checked"';
326 // This addresses the following issue: http://www.atutor.ca/atutor/mantis/view.php?id=3268
328 $sql = "SELECT t.test_id, anonymous FROM ".TABLE_PREFIX."tests_results r NATURAL JOIN ".TABLE_PREFIX."tests t WHERE r.test_id = t.test_id AND r.test_id=$tid";
329 $result = mysql_query($sql, $db);
330 $anonymous_disabled = '';
331 if ($row = mysql_fetch_assoc($result)) {
332 //If there are submission(s) for this test, anonymous field will not be altered.
333 $anonymous_disabled = 'disabled';
336 <input type="radio" name="anonymous" id="anonN" value="0" <?php echo $n; ?> <?php echo $anonymous_disabled; ?> /><label for="anonN"><?php echo _AT('no'); ?></label>
337 <input type="radio" name="anonymous" value="1" id="anonY" <?php echo $y; ?> <?php echo $anonymous_disabled; ?> /><label for="anonY"><?php echo _AT('yes'); ?></label>
339 if ($anonymous_disabled != ""){
340 echo '('._AT('disabled').')';
347 <?php echo _AT('allow_guests'); ?><br />
349 if ($_POST['allow_guests'] == 1) {
350 $y = 'checked="checked"';
352 $disable_show_guest_form = '';
355 $n = 'checked="checked"';
356 $disable_show_guest_form = 'disabled="disabled"';
360 <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>
361 <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>
364 if ($_POST['show_guest_form'] == 1)
365 $y = 'checked="checked"';
370 <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>
374 <?php echo _AT('display'); ?><br />
376 if ($_POST['display'] == 1) {
377 $y = 'checked="checked"';
381 $n = 'checked="checked"';
385 <input type="radio" name="display" id="displayN" value="0" <?php echo $n; ?> /><label for="displayN"><?php echo _AT('all_questions_on_page'); ?></label>
386 <input type="radio" name="display" id="displayY" value="1" <?php echo $y; ?> /><label for="displayY"><?php echo _AT('one_question_per_page'); ?></label>
390 <?php echo _AT('pass_score'); ?><br />
391 <input type="radio" name="pass_score" value="0" id="no" <?php if ($_POST['passpercent'] == 0 && $_POST['passscore'] == 0){echo 'checked="true"';} ?>
392 onfocus="disable_texts('both');" />
394 <label for="no" title="<?php echo _AT('pass_score'). ': '. _AT('no_pass_score'); ?>"><?php echo _AT('no_pass_score'); ?></label><br />
396 <input type="radio" name="pass_score" value="1" id="percentage" <?php if ($_POST['passpercent'] <> 0){echo 'checked="true"';} ?>
397 onfocus="disable_texts('points');" />
399 <input type="text" name="passpercent" id="passpercent" size="2" value="<?php echo $_POST['passpercent']; ?>"
400 <?php if ($_POST['passpercent'] == 0){echo 'disabled="true"';} ?> />
401 <label for="percentage" title="<?php echo _AT('pass_score'). ': '. _AT('percentage_score'); ?>"><?php echo '% ' . _AT('percentage_score'); ?></label><br />
403 <input type="radio" name="pass_score" value="2" id="points" <?php if ($_POST['passscore'] <> 0){echo 'checked="true"';} ?>
404 onfocus="disable_texts('percentage');" />
406 <input type="text" name="passscore" id="passscore" size="2" value="<?php echo $_POST['passscore']; ?>"
407 <?php if ($_POST['passscore'] == 0){echo 'disabled="true"';} ?>/>
408 <label for="points" title="<?php echo _AT('pass_score'). ': '. _AT('points_score'); ?>"><?php echo _AT('points_score'); ?></label>
412 <label for="passfeedback"><?php echo _AT('pass_feedback'); ?></label><br />
413 <textarea name="passfeedback" cols="35" rows="1" id="passfeedback"><?php echo htmlspecialchars($_POST['passfeedback']); ?></textarea>
417 <label for="failfeedback"><?php echo _AT('fail_feedback'); ?></label><br />
418 <textarea name="failfeedback" cols="35" rows="1" id="failfeedback"><?php echo htmlspecialchars($_POST['failfeedback']); ?></textarea>
422 <?php echo _AT('result_release'); ?><br />
424 if ($_POST['result_release'] == AT_RELEASE_IMMEDIATE) {
425 $check_marked = $check_never = '';
426 $check_immediate = 'checked="checked"';
428 } else if ($_POST['result_release'] == AT_RELEASE_MARKED) {
429 $check_immediate = $check_never = '';
430 $check_marked = 'checked="checked"';
432 } else if ($_POST['result_release'] == AT_RELEASE_NEVER) {
433 $check_immediate = $check_marked = '';
434 $check_never = 'checked="checked"';
438 <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 />
439 <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 />
440 <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>
444 <?php echo _AT('randomize_questions'); ?><br />
446 if ($_POST['random'] == 1) {
447 $y = 'checked="checked"';
451 $n = 'checked="checked"';
452 $disabled = 'disabled="disabled" ';
455 <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>
459 <?php echo _AT('start_date'); ?><br />
461 $today_day = substr($_POST['start_date'], 8, 2);
462 $today_mon = substr($_POST['start_date'], 5, 2);
463 $today_year = substr($_POST['start_date'], 0, 4);
465 $today_hour = substr($_POST['start_date'], 11, 2);
466 $today_min = substr($_POST['start_date'], 14, 2);
469 require(AT_INCLUDE_PATH.'html/release_date.inc.php');
473 <?php echo _AT('end_date'); ?><br />
475 $today_day = substr($_POST['end_date'], 8, 2);
476 $today_mon = substr($_POST['end_date'], 5, 2);
477 $today_year = substr($_POST['end_date'], 0, 4);
479 $today_hour = substr($_POST['end_date'], 11, 2);
480 $today_min = substr($_POST['end_date'], 14, 2);
483 require(AT_INCLUDE_PATH.'html/release_date.inc.php');
488 <label for="inst"><?php echo _AT('limit_to_group'); ?></label><br />
491 //get groups currently allowed
492 $current_groups = array();
493 $sql = "SELECT group_id FROM ".TABLE_PREFIX."tests_groups WHERE test_id=$tid";
494 $result = mysql_query($sql, $db);
495 while ($row = mysql_fetch_assoc($result)) {
496 $current_groups[] = $row['group_id'];
500 $sql = "SELECT * FROM ".TABLE_PREFIX."groups_types WHERE course_id=$_SESSION[course_id] ORDER BY title";
501 $result = mysql_query($sql, $db);
502 if (mysql_num_rows($result)) {
503 while ($row = mysql_fetch_assoc($result)) {
504 echo '<strong>'.$row['title'].'</strong><br />';
506 $sql = "SELECT * FROM ".TABLE_PREFIX."groups WHERE type_id=$row[type_id] ORDER BY title";
507 $g_result = mysql_query($sql, $db);
508 while ($grow = mysql_fetch_assoc($g_result)) {
509 echo ' <label><input type="checkbox" value="'.$grow['group_id'].'" name="groups['.$grow['group_id'].']" ';
510 if (is_array($current_groups) && in_array($grow['group_id'], $current_groups)) {
511 echo 'checked="checked"';
513 echo '/>'.$grow['title'].'</label><br />';
517 echo _AT('none_found');
523 <label for="inst"><?php echo _AT('instructions'); ?></label><br />
524 <textarea name="instructions" cols="35" rows="3" id="inst"><?php echo htmlspecialchars($_POST['instructions']); ?></textarea>
528 <div class="row buttons">
529 <input type="submit" value="<?php echo _AT('save'); ?>" name="submit" accesskey="s" />
530 <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
536 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>