question_create.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Test.
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. // the section (tabs)
  8. $this_section = SECTION_COURSES;
  9. // notice for unauthorized people.
  10. api_protect_course_script(true);
  11. $allow = api_is_allowed_to_edit();
  12. if (!$allow) {
  13. api_not_allowed(true);
  14. }
  15. // breadcrumbs
  16. $interbreadcrumb[] = ["url" => "exercise.php", "name" => get_lang('Tests')];
  17. // Tool name
  18. $nameTools = get_lang('Add this question to the test');
  19. // The form
  20. $form = new FormValidator('add_question', 'post', api_get_self().'?'.api_get_cidreq());
  21. // form title
  22. $form->addElement('header', '', get_lang('Add this question to the test'));
  23. $question_list = Question::getQuestionTypeList();
  24. $question_list_options = [];
  25. foreach ($question_list as $key => $value) {
  26. $question_list_options[$key] = addslashes(get_lang($value[1]));
  27. }
  28. $form->addElement(
  29. 'select',
  30. 'question_type_hidden',
  31. get_lang('Question type'),
  32. $question_list_options,
  33. ['id' => 'question_type_hidden']
  34. );
  35. //session id
  36. $session_id = api_get_session_id();
  37. // the exercises
  38. $tbl_exercises = Database::get_course_table(TABLE_QUIZ_TEST);
  39. $course_id = api_get_course_int_id();
  40. $sql = "SELECT id,title,type,description, results_disabled
  41. FROM $tbl_exercises
  42. WHERE c_id = $course_id AND active<>'-1' AND session_id=".$session_id."
  43. ORDER BY title ASC";
  44. $result = Database::query($sql);
  45. $exercises['-'] = '-'.get_lang('Select exercise').'-';
  46. while ($row = Database :: fetch_array($result)) {
  47. $exercises[$row['id']] = cut($row['title'], EXERCISE_MAX_NAME_SIZE);
  48. }
  49. $form->addElement('select', 'exercise', get_lang('Test'), $exercises);
  50. // generate default content
  51. $form->addElement(
  52. 'checkbox',
  53. 'is_content',
  54. null,
  55. get_lang('Generate default content'),
  56. ['checked' => true]
  57. );
  58. // the submit button
  59. $form->addButtonCreate(get_lang('Create a question'), 'SubmitCreate a question');
  60. // setting the rules
  61. $form->addRule('exercise', get_lang('Required field'), 'required');
  62. $form->addRule('exercise', get_lang('You have to select a test'), 'numeric');
  63. $form->registerRule('validquestiontype', 'callback', 'check_question_type');
  64. $form->addRule('question_type_hidden', get_lang('InvalidQuestion type'), 'validquestiontype');
  65. if ($form->validate()) {
  66. $values = $form->exportValues();
  67. $answer_type = $values['question_type_hidden'];
  68. // check feedback_type from current exercise for type of question delineation
  69. $exercise_id = intval($values['exercise']);
  70. $sql = "SELECT feedback_type FROM $tbl_exercises WHERE c_id = $course_id AND id = '$exercise_id'";
  71. $rs_feedback_type = Database::query($sql);
  72. $row_feedback_type = Database::fetch_row($rs_feedback_type);
  73. $feedback_type = $row_feedback_type[0];
  74. // if question type does not belong to self-evaluation (immediate feedback) it'll send an error
  75. if (($answer_type == HOT_SPOT_DELINEATION && $feedback_type != 1) ||
  76. ($feedback_type == 1 && ($answer_type != HOT_SPOT_DELINEATION && $answer_type != UNIQUE_ANSWER))) {
  77. header('Location: question_create.php?'.api_get_cidreq().'&error=true');
  78. exit;
  79. }
  80. header('Location: admin.php?exerciseId='.$values['exercise'].'&newQuestion=yes&isContent='.$values['is_content'].'&answerType='.$answer_type);
  81. exit;
  82. } else {
  83. // header
  84. Display::display_header($nameTools);
  85. echo '<div class="actions">';
  86. echo '<a href="exercise.php?show=test">'.Display::return_icon('back.png', get_lang('BackToTestsList'), '', ICON_SIZE_MEDIUM).'</a>';
  87. echo '</div>';
  88. // displaying the form
  89. $form->display();
  90. // footer
  91. Display::display_footer();
  92. }
  93. function check_question_type($parameter)
  94. {
  95. $question_list = Question::getQuestionTypeList();
  96. foreach ($question_list as $key => $value) {
  97. $valid_question_types[] = $key;
  98. }
  99. if (in_array($parameter, $valid_question_types)) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. }