question_create.php 3.9 KB

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