PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/mods/_standard/tests/create_question_likert.php

https://github.com/harriswong/ATutor
PHP | 203 lines | 159 code | 29 blank | 15 comment | 30 complexity | 70149f30261df3991221b6a0d375e5af MD5 | raw file
  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. define('AT_INCLUDE_PATH', '../../../include/');
  14. require(AT_INCLUDE_PATH.'vitals.inc.php');
  15. require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/likert_presets.inc.php');
  16. require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/test_question_queries.inc.php');
  17. authenticate(AT_PRIV_TESTS);
  18. require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/test_result_functions.inc.php');
  19. if (isset($_POST['cancel'])) {
  20. $msg->addFeedback('CANCELLED');
  21. header('Location: question_db.php');
  22. exit;
  23. } else if (isset($_POST['submit'])) {
  24. $_POST['required'] = intval($_POST['required']);
  25. $_POST['question'] = trim($_POST['question']);
  26. $_POST['category_id'] = intval($_POST['category_id']);
  27. $empty_fields = array();
  28. if ($_POST['question'] == ''){
  29. $empty_fields[] = _AT('question');
  30. }
  31. if ($_POST['choice'][0] == '') {
  32. $empty_fields[] = _AT('choice').' 1';
  33. }
  34. if ($_POST['choice'][1] == '') {
  35. $empty_fields[] = _AT('choice').' 2';
  36. }
  37. if (!empty($empty_fields)) {
  38. $msg->addError(array('EMPTY_FIELDS', implode(', ', $empty_fields)));
  39. }
  40. if (!$msg->containsErrors()) {
  41. $_POST['feedback'] = '';
  42. $_POST['question'] = $addslashes($_POST['question']);
  43. for ($i=0; $i<10; $i++) {
  44. $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
  45. $_POST['answer'][$i] = intval($_POST['answer'][$i]);
  46. if ($_POST['choice'][$i] == '') {
  47. /* an empty option can't be correct */
  48. $_POST['answer'][$i] = 0;
  49. }
  50. }
  51. $sql_params = array( $_POST['category_id'],
  52. $_SESSION['course_id'],
  53. $_POST['feedback'],
  54. $_POST['question'],
  55. $_POST['choice'][0],
  56. $_POST['choice'][1],
  57. $_POST['choice'][2],
  58. $_POST['choice'][3],
  59. $_POST['choice'][4],
  60. $_POST['choice'][5],
  61. $_POST['choice'][6],
  62. $_POST['choice'][7],
  63. $_POST['choice'][8],
  64. $_POST['choice'][9],
  65. $_POST['answer'][0],
  66. $_POST['answer'][1],
  67. $_POST['answer'][2],
  68. $_POST['answer'][3],
  69. $_POST['answer'][4],
  70. $_POST['answer'][5],
  71. $_POST['answer'][6],
  72. $_POST['answer'][7],
  73. $_POST['answer'][8],
  74. $_POST['answer'][9]);
  75. $sql = vsprintf(AT_SQL_QUESTION_LIKERT, $sql_params);
  76. $result = mysql_query($sql, $db);
  77. $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
  78. header('Location: question_db.php');
  79. exit;
  80. }
  81. } else if (isset($_POST['preset'])) {
  82. // load preset
  83. $_POST['preset_num'] = intval($_POST['preset_num']);
  84. if (isset($_likert_preset[$_POST['preset_num']])) {
  85. $_POST['choice'] = $_likert_preset[$_POST['preset_num']];
  86. } else if ($_POST['preset_num']) {
  87. $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE question_id=$_POST[preset_num] AND course_id=$_SESSION[course_id]";
  88. $result = mysql_query($sql, $db);
  89. if ($row = mysql_fetch_assoc($result)){
  90. for ($i=0; $i<10; $i++) {
  91. $_POST['choice'][$i] = $row['choice_' . $i];
  92. }
  93. }
  94. }
  95. }
  96. $onload = 'document.form.category_id.focus();';
  97. require(AT_INCLUDE_PATH.'header.inc.php');
  98. ?>
  99. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
  100. <input type="hidden" name="required" value="1" />
  101. <div class="input-form">
  102. <fieldset class="group_form"><legend class="group_form"><?php echo _AT('preset_scales'); ?></legend>
  103. <div class="row">
  104. <select name="preset_num">
  105. <optgroup label="<?php echo _AT('presets'); ?>">
  106. <?php
  107. //presets
  108. foreach ($_likert_preset as $val=>$preset) {
  109. echo '<option value="'.$val.'">'.$preset[0].' - '.$preset[count($preset)-1].'</option>';
  110. }
  111. echo '</optgroup>';
  112. //previously used
  113. $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions WHERE course_id=$_SESSION[course_id] AND type=4";
  114. $result = mysql_query($sql, $db);
  115. if ($row = mysql_fetch_assoc($result)) {
  116. echo '<optgroup label="'. _AT('prev_used').'">';
  117. $used_choices = array();
  118. do {
  119. $choices = array_slice($row, 9, 10);
  120. if (in_array($choices, $used_choices)) {
  121. continue;
  122. }
  123. $used_choices[] = $choices;
  124. for ($i=0; $i<=10; $i++) {
  125. if ($row['choice_'.$i] == '') {
  126. $i--;
  127. break;
  128. }
  129. }
  130. echo '<option value="'.$row['question_id'].'">'.$row['choice_0'].' - '.$row['choice_'.$i].'</option>';
  131. } while ($row = mysql_fetch_assoc($result));
  132. echo '</optgroup>';
  133. }
  134. ?>
  135. </select>
  136. </div>
  137. <div class="row buttons">
  138. <input type="submit" name="preset" value="<?php echo _AT('set_preset'); ?>" class="button" />
  139. </div>
  140. </fieldset>
  141. </div>
  142. <br />
  143. <div class="input-form">
  144. <fieldset class="group_form"><legend class="group_form"><?php echo _AT('test_lk'); ?></legend>
  145. <div class="row">
  146. <label for="cats"><?php echo _AT('category'); ?></label><br />
  147. <select name="category_id" id="cats">
  148. <?php print_question_cats($_POST['category_id']); ?>
  149. </select>
  150. </div>
  151. <div class="row">
  152. <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="question"><?php echo _AT('question'); ?></label>
  153. <?php print_VE('question'); ?>
  154. <textarea id="question" cols="50" rows="6" name="question" style="width:90%;"><?php
  155. echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
  156. </div>
  157. <?php for ($i=0; $i<10; $i++) { ?>
  158. <div class="row">
  159. <?php if ($i==0 || $i==1) { ?>
  160. <span class="required" title="<?php echo _AT('required_field'); ?>">*</span>
  161. <?php } ?>
  162. <label for="choice_<?php echo $i; ?>">
  163. <?php echo _AT('choice'); ?> <?php echo ($i+1); ?></label><br />
  164. <input type="text" id="choice_<?php echo $i; ?>" size="40" name="choice[<?php echo $i; ?>]" value="<?php echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?>" />
  165. </div>
  166. <?php } ?>
  167. <div class="row buttons">
  168. <input type="submit" value="<?php echo _AT('save'); ?>" name="submit" accesskey="s" />
  169. <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
  170. </div>
  171. </fieldset>
  172. </div>
  173. </form>
  174. <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>