survey.download.inc.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. * @author Arnaud Ligot <arnaud@cblue.be>
  6. * @version $Id: $
  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. require_once 'survey.lib.php';
  14. // Getting all the course information
  15. $_course = CourseManager::get_course_information($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 WHERE c_id = $course_id AND invitation_code = '".Database::escape_string($invitation)."'";
  24. $result = Database::query($sql);
  25. if (Database::num_rows($result) < 1) {
  26. Display :: display_error_message(get_lang('WrongInvitationCode'), false);
  27. Display :: display_footer();
  28. exit;
  29. }
  30. $survey_invitation = Database::fetch_assoc($result);
  31. // Now we check if the user already filled the survey
  32. if ($survey_invitation['answered'] == 1) {
  33. Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false);
  34. Display :: display_footer();
  35. exit;
  36. }
  37. // Very basic security check: check if a text field from a survey/answer/option contains the name of the document requested
  38. // Fetch survey ID
  39. // If this is the case there will be a language choice
  40. $sql = "SELECT * FROM $table_survey WHERE c_id = $course_id AND code='".Database::escape_string($survey_invitation['survey_code'])."'";
  41. $result = Database::query($sql);
  42. if (Database::num_rows($result) > 1) {
  43. if ($_POST['language']) {
  44. $survey_invitation['survey_id'] = $_POST['language'];
  45. } else {
  46. echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.Security::remove_XSS($_GET['course']).'&invitationcode='.Security::remove_XSS($_GET['invitationcode']).'">';
  47. echo ' <select name="language">';
  48. while ($row = Database::fetch_assoc($result)) {
  49. echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  50. }
  51. echo '</select>';
  52. echo ' <input type="submit" name="Submit" value="'.get_lang('Ok').'" />';
  53. echo '</form>';
  54. display::display_footer();
  55. exit;
  56. }
  57. } else {
  58. $row = Database::fetch_assoc($result);
  59. $survey_invitation['survey_id'] = $row['survey_id'];
  60. }
  61. $sql = "SELECT count(*) FROM $table_survey WHERE c_id = $course_id AND survey_id = ".$survey_invitation['survey_id']."
  62. and (
  63. title LIKE '%$doc_url%'
  64. or subtitle LIKE '%$doc_url%'
  65. or intro LIKE '%$doc_url%'
  66. or surveythanks LIKE '%$doc_url%'
  67. )
  68. union select count(*) from $table_survey_question where c_id = $course_id AND survey_id = ".$survey_invitation['survey_id']."
  69. and (
  70. survey_question LIKE '%$doc_url%'
  71. or survey_question_comment LIKE '%$doc_url%'
  72. )
  73. union select count(*) from $table_survey_question_option where c_id = $course_id AND survey_id = ".$survey_invitation['survey_id']."
  74. and (
  75. option_text LIKE '%$doc_url%'
  76. )";
  77. $result = Database::query($sql);
  78. if (Database::num_rows($result) == 0) {
  79. Display :: display_error_message(get_lang('WrongInvitationCode'), false);
  80. Display :: display_footer();
  81. exit;
  82. }
  83. return $_course;
  84. }