PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/_standard/tests/create_question_multianswer.php

https://github.com/harriswong/ATutor
PHP | 174 lines | 127 code | 28 blank | 19 comment | 21 complexity | 27338fe8d0c619ec6db97855ab6adcb9 MD5 | raw file
  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. 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']) || isset($_POST['submit_no'])) {
  20. $msg->addFeedback('CANCELLED');
  21. header('Location: question_db.php');
  22. exit;
  23. } else if ($_POST['submit'] || $_POST['submit_yes']) {
  24. $_POST['required'] = intval($_POST['required']);
  25. $_POST['feedback'] = trim($_POST['feedback']);
  26. $_POST['question'] = trim($_POST['question']);
  27. $_POST['category_id'] = intval($_POST['category_id']);
  28. if ($_POST['question'] == ''){
  29. $msg->addError(array('EMPTY_FIELDS', _AT('question')));
  30. }
  31. if (!$msg->containsErrors()) {
  32. $choice_new = array(); // stores the non-blank choices
  33. $answer_new = array(); // stores the associated "answer" for the choices
  34. for ($i=0; $i<10; $i++) {
  35. /**
  36. * Db defined it to be 255 length, chop strings off it it's less than that
  37. * @harris
  38. */
  39. $_POST['choice'][$i] = validate_length($_POST['choice'][$i], 255);
  40. $_POST['choice'][$i] = $addslashes(trim($_POST['choice'][$i]));
  41. $_POST['answer'][$i] = intval($_POST['answer'][$i]);
  42. if ($_POST['choice'][$i] == '') {
  43. /* an empty option can't be correct */
  44. $_POST['answer'][$i] = 0;
  45. } else {
  46. /* filter out empty choices/ remove gaps */
  47. $choice_new[] = $_POST['choice'][$i];
  48. $answer_new[] = $_POST['answer'][$i];
  49. if ($_POST['answer'][$i] != 0)
  50. $has_answer = TRUE;
  51. }
  52. }
  53. if ($has_answer != TRUE && !$_POST['submit_yes']) {
  54. $hidden_vars['required'] = htmlspecialchars($_POST['required']);
  55. $hidden_vars['feedback'] = htmlspecialchars($_POST['feedback']);
  56. $hidden_vars['question'] = htmlspecialchars($_POST['question']);
  57. $hidden_vars['category_id'] = htmlspecialchars($_POST['category_id']);
  58. for ($i = 0; $i < count($choice_new); $i++) {
  59. $hidden_vars['answer['.$i.']'] = htmlspecialchars($answer_new[$i]);
  60. $hidden_vars['choice['.$i.']'] = htmlspecialchars($choice_new[$i]);
  61. }
  62. $msg->addConfirm('NO_ANSWER', $hidden_vars);
  63. } else {
  64. //add slahes throughout - does that fix it?
  65. $_POST['answer'] = $answer_new;
  66. $_POST['choice'] = $choice_new;
  67. $_POST['answer'] = array_pad($_POST['answer'], 10, 0);
  68. $_POST['choice'] = array_pad($_POST['choice'], 10, '');
  69. $_POST['feedback'] = $addslashes($_POST['feedback']);
  70. $_POST['question'] = $addslashes($_POST['question']);
  71. $sql_params = array( $_POST['category_id'],
  72. $_SESSION['course_id'],
  73. $_POST['feedback'],
  74. $_POST['question'],
  75. $_POST['choice'][0],
  76. $_POST['choice'][1],
  77. $_POST['choice'][2],
  78. $_POST['choice'][3],
  79. $_POST['choice'][4],
  80. $_POST['choice'][5],
  81. $_POST['choice'][6],
  82. $_POST['choice'][7],
  83. $_POST['choice'][8],
  84. $_POST['choice'][9],
  85. $_POST['answer'][0],
  86. $_POST['answer'][1],
  87. $_POST['answer'][2],
  88. $_POST['answer'][3],
  89. $_POST['answer'][4],
  90. $_POST['answer'][5],
  91. $_POST['answer'][6],
  92. $_POST['answer'][7],
  93. $_POST['answer'][8],
  94. $_POST['answer'][9]);
  95. $sql = vsprintf(AT_SQL_QUESTION_MULTIANSWER, $sql_params);
  96. $result = mysql_query($sql, $db);
  97. $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
  98. header('Location: question_db.php');
  99. exit;
  100. }
  101. }
  102. }
  103. $onload = 'document.form.category_id.focus();';
  104. require(AT_INCLUDE_PATH.'header.inc.php');
  105. $msg->printConfirm();
  106. ?>
  107. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
  108. <input type="hidden" name="required" value="1" />
  109. <div class="input-form">
  110. <fieldset class="group_form"><legend class="group_form"><?php echo _AT('test_ma'); ?></legend>
  111. <div class="row">
  112. <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="cats"><?php echo _AT('category'); ?></label><br />
  113. <select name="category_id" id="cats">
  114. <?php print_question_cats($_POST['category_id']); ?>
  115. </select>
  116. </div>
  117. <div class="row">
  118. <label for="optional_feedback"><?php echo _AT('optional_feedback'); ?></label>
  119. <?php print_VE('optional_feedback'); ?>
  120. <textarea id="optional_feedback" cols="50" rows="3" name="feedback"><?php echo htmlspecialchars(stripslashes($_POST['feedback'])); ?></textarea>
  121. </div>
  122. <div class="row">
  123. <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="question"><?php echo _AT('question'); ?></label>
  124. <?php print_VE('question'); ?>
  125. <textarea id="question" cols="50" rows="4" name="question" style="width:90%;"><?php echo htmlspecialchars(stripslashes($_POST['question'])); ?></textarea>
  126. </div>
  127. <?php
  128. for ($i=0; $i<10; $i++) {
  129. ?>
  130. <div class="row">
  131. <?php echo _AT('choice'); ?> <?php echo ($i+1); ?>
  132. <?php print_VE('choice_' . $i); ?>
  133. <br />
  134. <small><input type="checkbox" name="answer[<?php echo $i; ?>]" id="answer_<?php echo $i; ?>" value="1" <?php if($_POST['answer'][$i]) { echo 'checked="checked"';} ?>><label for="answer_<?php echo $i; ?>"><?php echo _AT('correct_answer'); ?></label></small>
  135. <textarea id="choice_<?php echo $i; ?>" cols="50" rows="2" name="choice[<?php echo $i; ?>]"><?php
  136. echo htmlspecialchars(stripslashes($_POST['choice'][$i])); ?></textarea>
  137. </div>
  138. <?php } ?>
  139. <div class="row buttons">
  140. <input type="submit" value="<?php echo _AT('save'); ?>" name="submit" accesskey="s" />
  141. <input type="submit" value="<?php echo _AT('cancel'); ?>" name="cancel" />
  142. </div>
  143. </fieldset>
  144. </div>
  145. </form>
  146. <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>