survey.download.inc.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. *
  6. * @author Arnaud Ligot <arnaud@cblue.be>
  7. *
  8. * A small peace of code to enable user to access images included into survey
  9. * which are accessible by non authenticated users. This file is included
  10. * by document/download.php
  11. */
  12. function check_download_survey($course, $invitation, $doc_url)
  13. {
  14. // Getting all the course information
  15. $_course = api_get_course_info($course);
  16. $course_id = $_course['real_id'];
  17. // Database table definitions
  18. $table_survey = Database::get_course_table(TABLE_SURVEY);
  19. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  20. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  21. $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  22. // Now we check if the invitationcode is valid
  23. $sql = "SELECT * FROM $table_survey_invitation
  24. WHERE
  25. c_id = $course_id AND
  26. invitation_code = '".Database::escape_string($invitation)."'";
  27. $result = Database::query($sql);
  28. if (Database::num_rows($result) < 1) {
  29. echo Display::return_message(get_lang('WrongInvitationCode'), 'error', false);
  30. Display::display_footer();
  31. exit;
  32. }
  33. $survey_invitation = Database::fetch_assoc($result);
  34. // Now we check if the user already filled the survey
  35. if ($survey_invitation['answered'] == 1) {
  36. echo Display::return_message(get_lang('YouAlreadyFilledThisSurvey'), 'error', false);
  37. Display::display_footer();
  38. exit;
  39. }
  40. // Very basic security check: check if a text field from
  41. // a survey/answer/option contains the name of the document requested
  42. // Fetch survey ID
  43. // If this is the case there will be a language choice
  44. $sql = "SELECT * FROM $table_survey
  45. WHERE
  46. c_id = $course_id AND
  47. code='".Database::escape_string($survey_invitation['survey_code'])."'";
  48. $result = Database::query($sql);
  49. if (Database::num_rows($result) > 1) {
  50. if ($_POST['language']) {
  51. $survey_invitation['survey_id'] = $_POST['language'];
  52. } else {
  53. echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.Security::remove_XSS($_GET['course']).'&invitationcode='.Security::remove_XSS($_GET['invitationcode']).'">';
  54. echo ' <select name="language">';
  55. while ($row = Database::fetch_assoc($result)) {
  56. echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  57. }
  58. echo '</select>';
  59. echo ' <input type="submit" name="Submit" value="'.get_lang('Ok').'" />';
  60. echo '</form>';
  61. Display::display_footer();
  62. exit;
  63. }
  64. } else {
  65. $row = Database::fetch_assoc($result);
  66. $survey_invitation['survey_id'] = $row['survey_id'];
  67. }
  68. $sql = "SELECT count(*)
  69. FROM $table_survey
  70. WHERE
  71. c_id = $course_id AND
  72. survey_id = ".$survey_invitation['survey_id']." AND (
  73. title LIKE '%$doc_url%'
  74. or subtitle LIKE '%$doc_url%'
  75. or intro LIKE '%$doc_url%'
  76. or surveythanks LIKE '%$doc_url%'
  77. )
  78. UNION
  79. SELECT count(*)
  80. FROM $table_survey_question
  81. WHERE
  82. c_id = $course_id AND
  83. survey_id = ".$survey_invitation['survey_id']." AND (
  84. survey_question LIKE '%$doc_url%'
  85. or survey_question_comment LIKE '%$doc_url%'
  86. )
  87. UNION
  88. SELECT count(*)
  89. FROM $table_survey_question_option
  90. WHERE
  91. c_id = $course_id AND
  92. survey_id = ".$survey_invitation['survey_id']." AND (
  93. option_text LIKE '%$doc_url%'
  94. )";
  95. $result = Database::query($sql);
  96. if (Database::num_rows($result) == 0) {
  97. echo Display::return_message(get_lang('WrongInvitationCode'), 'error', false);
  98. Display::display_footer();
  99. exit;
  100. }
  101. return $_course;
  102. }