survey.ajax.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../global.inc.php';
  4. $action = isset($_GET['a']) ? $_GET['a'] : null;
  5. $current_user_id = api_get_user_id();
  6. $courseId = api_get_course_int_id();
  7. $surveyId = isset($_REQUEST['survey_id']) ? $_REQUEST['survey_id'] : null;
  8. $questionId = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
  9. switch ($action) {
  10. case 'load_question_options':
  11. if (!api_is_allowed_to_edit()) {
  12. exit;
  13. }
  14. $question = SurveyManager::get_question($questionId);
  15. if (!empty($question)) {
  16. foreach ($question['answers'] as $index => $answer) {
  17. echo Display::input(
  18. 'radio',
  19. 'option['.$questionId.']',
  20. $question['answersid'][$index],
  21. ['class' => 'question_option']
  22. );
  23. echo $answer;
  24. echo '<br />';
  25. }
  26. }
  27. echo '
  28. <script>
  29. $(function() {
  30. $(".question_option").on("click", function() {
  31. $("#question_form_option_id").attr("value", $(this).val());
  32. });
  33. });
  34. </script>
  35. ';
  36. break;
  37. case 'save_question':
  38. if (api_is_anonymous()) {
  39. echo '';
  40. break;
  41. }
  42. $status = isset($_GET['status']) ? (int) $_GET['status'] : null;
  43. $userId = api_get_user_id();
  44. $surveyData = SurveyManager::get_survey($surveyId);
  45. if (empty($surveyData)) {
  46. exit;
  47. }
  48. SurveyUtil::remove_answer(
  49. $userId,
  50. $surveyId,
  51. $questionId,
  52. $courseId
  53. );
  54. SurveyUtil::store_answer(
  55. $userId,
  56. $surveyId,
  57. $questionId,
  58. 1,
  59. $status,
  60. $surveyData
  61. );
  62. break;
  63. }
  64. exit;