question_create.php 3.8 KB

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