question_create.php 3.8 KB

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