PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/surveys/show_survey.php

https://bitbucket.org/danaila_iulian/roc
PHP | 162 lines | 123 code | 37 blank | 2 comment | 36 complexity | ed5347cf9d1af0f7c8289b263b64f118 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. include('wpframe.php');
  3. wpframe_stop_direct_call(__FILE__);
  4. if(!is_single() and isset($GLOBALS['surveys_client_includes_loaded'])) { #If this is in the listing page - and a quiz is already shown, don't show another.
  5. printf(t("Please go to <a href='%s'>%s</a> to view the survey"), get_permalink(), get_the_title());
  6. } else {
  7. global $wpdb;
  8. $question = $wpdb->get_results($wpdb->prepare("SELECT ID,question,allow_user_answer,allow_multiple_answers,user_answer_format FROM {$wpdb->prefix}surveys_question WHERE survey_id=%d ORDER BY ID", $survey_id));
  9. if(isset($_POST['action']) and $_POST['action']) { // Save the survey
  10. if($_POST['result_id']) { //Save the name and the email of the survey taker.
  11. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}surveys_result SET name=%s, email=%s WHERE ID=%d", strip_tags($_POST['survey_taker_name']), strip_tags($_POST['email']), $_POST['result_id']));
  12. e("Thanks for taking the survey. Your details have been saved.");
  13. } else { //Save the survey details.
  14. //$wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}surveys_result (survey_ID, added_on) VALUES(%d, DATE_ADD(NOW(), INTERVAL %f HOUR))", $survey_id, get_option('gmt_offset')));
  15. $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}surveys_result (survey_ID, added_on) VALUES(%d, NOW())", $survey_id));
  16. $result_id = $wpdb->insert_id;
  17. $question_count = 0;
  18. foreach($_POST['question_id'] as $question_id) {
  19. if(!$_POST['answer-' . $question_id]) { //User ignored the question.
  20. $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}surveys_result_answer (result_ID, answer_ID, question_ID) VALUES(%d, %d, %d)",
  21. $result_id, 0, $question_id)); // Add an empty answer row.
  22. } else {
  23. foreach($_POST['answer-' . $question_id] as $answer_id) {
  24. $user_answer = '';
  25. if($answer_id == 'user-answer') { //Custom answer provided by the user.
  26. $answer_id = 0;
  27. $user_answer = $_POST['user-answer-' . $question_id]; //Get the user answer from the text input field.
  28. } elseif(!$answer_id) $answer_id = 0; //Question was ignored.
  29. $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}surveys_result_answer (result_ID, answer_ID, question_ID, user_answer) VALUES(%d, %d, %d, %s)",
  30. $result_id, $answer_id, $question_id, strip_tags($user_answer)));
  31. if(!$question[$question_count]->allow_multiple_answers) break; // If this question don't allow multiple answers, break to the next question. This is basically a security measure. Users will have to edit the page HTML to make this necessary(very unlikely.).
  32. }
  33. }
  34. $question_count++;
  35. }
  36. $email = get_option('surveys_email');
  37. if($email) {
  38. $email_body = printf(t("Hi,\nThere is a new result for the survey at %s...\n"), $_SERVER['REQUEST_URI']);
  39. //Code lifted from show_individual_response.php file
  40. $questions = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}surveys_question WHERE survey_ID=%d", $survey_id));
  41. foreach($questions as $q) {
  42. $email_body .= $q->question . "\n";
  43. $all_answers_for_question = $wpdb->get_results($wpdb->prepare("SELECT A.answer,RA.answer_ID,RA.user_answer
  44. FROM {$wpdb->prefix}surveys_result_answer AS RA
  45. LEFT JOIN {$wpdb->prefix}surveys_answer AS A
  46. ON A.ID=RA.answer_ID WHERE RA.result_ID=%d AND RA.question_ID=%d", $result_id, $q->ID));
  47. $answers = array();
  48. foreach($all_answers_for_question as $one_answer) { // There is a chance that there is multiple answers for this question.
  49. if($one_answer->answer_ID) $answers[] = stripslashes($one_answer->answer);
  50. else $answers[] = stripslashes($one_answer->user_answer); //Custom User answer.
  51. }
  52. $email_body .= t("Answer: ");
  53. if($q->allow_user_answer and $q->user_answer_format == 'checkbox') {
  54. if($answers[0]) $email_body .= 'Yes';
  55. else $email_body .= 'No';
  56. } else {
  57. $email_body .= implode(', ', $answers);
  58. }
  59. $email_body .= "\n\n";
  60. }
  61. mail($email, t("Survey Result"), $email_body);
  62. }
  63. print t("Thanks for taking the survey. You input is very valuable to us.<br />If you want, you can attach your name to your survey answers. If you want the result to be anonymous, just ignore this form.");
  64. ?>
  65. <form action="" method="post" class="survey-form" style="text-align: left;">
  66. <label for="name"><?php e("Name") ?></label> <input type="text" name="survey_taker_name" id="name" value="" /><br />
  67. <label for="email"><?php e("Email") ?></label> <input type="text" name="email" id="email" value=""/><br />
  68. <input type="submit" name="action" id="action-button" value="<?php e("Submit Survey") ?>" />
  69. <input type="hidden" name="result_id" value="<?php echo $result_id ?>" />
  70. </form>
  71. <?php
  72. }
  73. } else { // Show The survey.
  74. if(!isset($GLOBALS['surveys_client_includes_loaded'])) {
  75. ?>
  76. <link type="text/css" rel="stylesheet" href="<?php echo $GLOBALS['wpframe_plugin_folder'] ?>/style.css" />
  77. <script type="text/javascript" src="<?php echo $GLOBALS['wpframe_wordpress'] ?>/wp-includes/js/jquery/jquery.js"></script>
  78. <script type="text/javascript" src="<?php echo $GLOBALS['wpframe_plugin_folder'] ?>/script.js"></script>
  79. <?php
  80. $GLOBALS['surveys_client_includes_loaded'] = true; // Make sure that this code is not loaded more than once.
  81. }
  82. if($question) {
  83. $questions_per_page = get_option('surveys_questions_per_page');
  84. if(!is_numeric($questions_per_page)) $questions_per_page = 0;
  85. ?>
  86. <div class="survey-area <?php if($questions_per_page != 1) echo 'multi-question'; ?>">
  87. <form action="" method="post" class="survey-form" id="survey-<?php echo $survey_id?>">
  88. <?php
  89. $question_count = 1;
  90. foreach ($question as $ques) {
  91. echo "<div class='survey-question' id='question-$question_count'>";
  92. echo "{$ques->question}\n";
  93. echo "<input type='hidden' name='question_id[]' value='{$ques->ID}' />\n<br />";
  94. $all_answers = $wpdb->get_results("SELECT ID,answer FROM {$wpdb->prefix}surveys_answer WHERE question_id={$ques->ID} ORDER BY sort_order");
  95. $type = ($ques->allow_multiple_answers) ? 'checkbox' : 'radio'; //If this is a multi answer question, make it a checkbox. Else, it will be a radio.
  96. if(count($all_answers) == 0 and $ques->allow_user_answer) $type = 'hidden'; //If there are no admin specified answer, and it allows user answer, keep it as selected - user don't have to check anything.
  97. if(count($all_answers) or $ques->user_answer_format == 'textarea' or $ques->user_answer_format == 'text') echo "<br />";
  98. foreach ($all_answers as $ans) {
  99. echo "<input type='$type' name='answer-{$ques->ID}[]' id='answer-id-{$ans->ID}' class='answer' value='{$ans->ID}' />\n";
  100. echo "<label for='answer-id-{$ans->ID}'>" . stripslashes($ans->answer) . "</label>\n";
  101. }
  102. if($ques->allow_user_answer) {
  103. echo "<input type='$type' name='answer-{$ques->ID}[]' id='answer-id-{$ans->ID}' class='answer' value='user-answer' />\n";
  104. if($ques->user_answer_format == 'textarea')
  105. echo "<textarea name='user-answer-{$ques->ID}' rows='5' cols='30' class='user-answer'></textarea>";
  106. elseif($ques->user_answer_format == 'checkbox')
  107. echo "<input type='checkbox' name='user-answer-{$ques->ID}' class='user-answer' value='1' />";
  108. else
  109. echo "<br /><input type='text' name='user-answer-{$ques->ID}' class='user-answer' value='' />";
  110. echo "\n";
  111. }
  112. echo "</div>\n\n";
  113. $question_count++;
  114. }
  115. ?><br />
  116. <input type="button" id="survey-next-question" value="<?php e("Next") ?> &gt;" /><br />
  117. <input type="submit" name="action" id="survey-action-button" value="<?php e("Submit Survey") ?>" />
  118. <input type="hidden" name="survey_id" value="<?php echo $survey_id ?>" />
  119. </form>
  120. <script type="text/javascript">survey_questions_per_page = <?php echo $questions_per_page ?>;</script>
  121. </div>
  122. <?php }
  123. }
  124. }
  125. ?>