dokeos-survey
[ class tree: dokeos-survey ] [ index: dokeos-survey ] [ all elements ]

Source for file fillsurvey.php

Documentation is available at fillsurvey.php

  1. <?php
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2004-2008 Dokeos SPRL
  7.  
  8.     For a full list of contributors, see "credits.txt".
  9.     The full license can be read in "license.txt".
  10.  
  11.     This program is free software; you can redistribute it and/or
  12.     modify it under the terms of the GNU General Public License
  13.     as published by the Free Software Foundation; either version 2
  14.     of the License, or (at your option) any later version.
  15.  
  16.     See the GNU General Public License for more details.
  17.  
  18.     Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  19. ==============================================================================
  20. */
  21.  
  22. /**
  23. *    @package dokeos.survey
  24. *     @author unknown, the initial survey that did not make it in 1.8 because of bad code
  25. *     @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  26. *     @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  27. *
  28. *     @todo use quickforms for the forms
  29. *     @todo check if the user already filled the survey and if this is the case then the answers have to be updated and not stored again.
  30. *              alterantively we could not allow people from filling the survey twice.
  31. *     @todo performance could be improved if not the survey_id was stored with the invitation but the survey_code
  32. */
  33.  
  34. // name of the language file that needs to be included
  35. $language_file 'survey';
  36.  
  37. // unsetting the course id (because it is in the URL)
  38. if (!isset($_GET['cidReq']))
  39. {
  40.     $cidReset true;
  41. }
  42. else 
  43. {
  44.     $_cid $_GET['cidReq']
  45. }
  46.  
  47. // including the global dokeos file
  48. require ('../inc/global.inc.php');
  49.  
  50. // including additional libraries
  51. //require_once (api_get_path(LIBRARY_PATH)."survey.lib.php");
  52. require_once('survey.lib.php');
  53. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  54.  
  55.  
  56. // breadcrumbs
  57. if (!empty($_user))
  58. {
  59.     $interbreadcrumb[array ("url" => 'survey_list.php''name' => get_lang('SurveyList'));
  60. }
  61.  
  62.  
  63. // Header
  64.  
  65. // getting all the course information
  66. $_course CourseManager::get_course_information($_GET['course']);
  67.  
  68. // Database table definitions
  69. $table_survey                     Database :: get_course_table(TABLE_SURVEY$_course['db_name']);
  70. $table_survey_question             Database :: get_course_table(TABLE_SURVEY_QUESTION$_course['db_name']);
  71. $table_survey_question_option     Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION$_course['db_name']);
  72. $table_course                     Database :: get_main_table(TABLE_MAIN_COURSE);
  73. $table_user                     Database :: get_main_table(TABLE_MAIN_USER);
  74. $table_survey_invitation         Database :: get_course_table(TABLE_SURVEY_INVITATION$_course['db_name']);
  75.  
  76. // first we check if the needed parameters are present
  77. if (!isset($_GET['course']OR !isset($_GET['invitationcode']))
  78. {
  79.     Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste')false);
  80.     Display :: display_footer();
  81.     exit;
  82. }
  83.  
  84. // now we check if the invitationcode is valid
  85. $sql "SELECT * FROM $table_survey_invitation WHERE invitation_code = '".Database::escape_string($_GET['invitationcode'])."'";
  86. $result api_sql_query($sql__FILE____LINE__);
  87. if (mysql_num_rows($result1)
  88. {
  89.     Display :: display_error_message(get_lang('WrongInvitationCode')false);
  90.     Display :: display_footer();
  91.     exit;
  92. }
  93. $survey_invitation mysql_fetch_assoc($result);
  94.  
  95. // now we check if the user already filled the survey
  96. if ($survey_invitation['answered'== 1)
  97. {
  98.     Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey')false);
  99.     Display :: display_footer();
  100.     exit;
  101. }
  102.  
  103. // checking if there is another survey with this code.
  104. // If this is the case there will be a language choice
  105. $sql "SELECT * FROM $table_survey WHERE code='".Database::escape_string($survey_invitation['survey_code'])."'";
  106. $result api_sql_query($sql__FILE____LINE__);
  107. if (mysql_num_rows($result1)
  108. {
  109.     if ($_POST['language'])
  110.     {
  111.         $survey_invitation['survey_id'$_POST['language'];
  112.     }
  113.     else
  114.     {
  115.         echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&cidReq='.$_GET['cidReq'].'">';
  116.         echo '  <select name="language">';
  117.         while ($row=mysql_fetch_assoc($result))
  118.         {
  119.             echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  120.         }
  121.         echo '</select>';
  122.         echo '  <input type="submit" name="Submit" value="'.get_lang('Ok').'" />';
  123.         echo '</form>';
  124.         display::display_footer();
  125.         exit;
  126.     }
  127. }
  128. else
  129. {
  130.     $row=mysql_fetch_assoc($result);
  131.     $survey_invitation['survey_id'$row['survey_id'];
  132. }
  133.  
  134. // getting the survey information
  135. $survey_data survey_manager::get_survey($survey_invitation['survey_id']);
  136. $survey_data['survey_id'$survey_invitation['survey_id'];
  137.  
  138. // storing the answers
  139. if ($_POST)
  140. {
  141.     // getting all the types of the question (because of the special treatment of the score question type
  142.     $sql "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'";
  143.     $result api_sql_query($sql__FILE____LINE__);
  144.     while ($row mysql_fetch_assoc($result))
  145.     {
  146.         $types[$row['question_id']] $row['type'];
  147.     }
  148.  
  149.  
  150.     // looping through all the post values
  151.     foreach ($_POST as $key=>$value)
  152.     {
  153.         // if the post value key contains the string 'question' then it is an answer on a question
  154.         if (strstr($key,'question'))
  155.         {
  156.             // finding the question id by removing 'question'
  157.             $survey_question_id str_replace('question''',$key);
  158.  
  159.             // if the post value is an array then we have a multiple response question or a scoring question type
  160.             // remark: when it is a multiple response then the value of the array is the option_id
  161.             //            when it is a scoring question then the key of the array is the option_id and the value is the value
  162.             if (is_array($value))
  163.             {
  164.                 SurveyUtil::remove_answer($survey_invitation['user']$survey_invitation['survey_id']$survey_question_id);
  165.                 foreach ($value as $answer_key => $answer_value)
  166.                 {
  167.                     if ($types[$survey_question_id== 'score')
  168.                     {
  169.                         $option_id $answer_key;
  170.                         $option_value $answer_value;
  171.                     }
  172.                     else
  173.                     {
  174.                         $option_id $answer_value;
  175.                         $option_value '';
  176.                     }
  177.                     SurveyUtil::store_answer($survey_invitation['user']$survey_invitation['survey_id']$survey_question_id$option_id$option_value$survey_data);
  178.                 }
  179.             }
  180.             // all the other question types (open question, multiple choice, percentage, ...)
  181.             else
  182.             {
  183.                 if ($types[$survey_question_id== 'percentage')
  184.                 {
  185.                     $sql "SELECT * FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
  186.                     $result api_sql_query($sql__FILE____LINE__);
  187.                     $row mysql_fetch_assoc($result);
  188.                     $option_value $row['option_text'];
  189.                 }
  190.                 else
  191.                 {
  192.                     $option_value 0;
  193.                     if($types[$survey_question_id== 'open')
  194.                     {
  195.                         $option_value $value;
  196.                         //$value = 0;
  197.                     }
  198.                 }
  199.  
  200.  
  201.                 $survey_question_answer $value;
  202.                 SurveyUtil::remove_answer($survey_invitation['user']$survey_invitation['survey_id']$survey_question_id);
  203.                 SurveyUtil::store_answer($survey_invitation['user']$survey_invitation['survey_id']$survey_question_id$value$option_value$survey_data);
  204.                 //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  205.             }
  206.         }
  207.     }
  208. }
  209.  
  210. // displaying the survey title and subtitle (appears on every page)
  211. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  212. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  213.  
  214. // checking time availability
  215. $start_date mktime(0,0,0,substr($survey_data['start_date'],5,2),substr($survey_data['start_date'],8,2),substr($survey_data['start_date'],0,4));
  216. $end_date mktime(0,0,0,substr($survey_data['end_date'],5,2),substr($survey_data['end_date'],8,2),substr($survey_data['end_date'],0,4));
  217. $cur_date time();
  218. if($cur_date $start_date)
  219. {
  220.     Display :: display_warning_message(get_lang('SurveyNotAvailableYet')false);
  221.     Display :: display_footer();
  222.     exit;
  223. }
  224. if($cur_date $end_date)
  225. {
  226.     Display :: display_warning_message(get_lang('SurveyNotAvailableAnymore')false);
  227.     Display :: display_footer();
  228.     exit;
  229. }
  230.  
  231. // displaying the survey introduction
  232. if (!isset($_GET['show']))
  233. {
  234.     echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  235.     $limit 0;
  236. }
  237.  
  238. // displaying the survey thanks message
  239. if (isset($_POST['finish_survey']))
  240. {
  241.     echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').'</strong> <br />'.$survey_data['survey_thanks'].'</div>';
  242.     survey_manager::update_survey_answered($survey_data['survey_id']$survey_invitation['user']$survey_invitation['survey_code']);
  243.     Display :: display_footer();
  244.     exit;
  245. }
  246.  
  247. if (isset($_GET['show']))
  248. {
  249.     // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  250.     // as long as there is no pagebreak fount we keep adding questions to the page
  251.     $questions_displayed array();
  252.     $counter 0;
  253.     $sql "SELECT * FROM $table_survey_question
  254.             WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  255.             ORDER BY sort ASC";
  256.     $result api_sql_query($sql__FILE____LINE__);
  257.  
  258.     while ($row mysql_fetch_assoc($result))
  259.     {
  260.         if($row['type'== 'pagebreak')
  261.         {
  262.             $counter++;
  263.         }
  264.         else
  265.         {
  266.             $paged_questions[$counter][$row['question_id'];
  267.         }
  268.     }
  269.  
  270.     if (key_exists($_GET['show'],$paged_questions))
  271.     {
  272.         $sql "SELECT     survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value,
  273.                         survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  274.                 FROM $table_survey_question survey_question
  275.                 LEFT JOIN $table_survey_question_option survey_question_option
  276.                 ON survey_question.question_id = survey_question_option.question_id
  277.                 WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  278.                 AND survey_question.question_id IN (".implode(',',$paged_questions[$_GET['show']]).")
  279.                 ORDER BY survey_question.sort, survey_question_option.sort ASC";
  280.  
  281.         $result api_sql_query($sql__FILE____LINE__);
  282.         $question_counter_max mysql_num_rows($result);
  283.         $counter 0;
  284.         $limit=0;
  285.         $questions array();
  286.         while ($row mysql_fetch_assoc($result))
  287.         {
  288.             // if the type is not a pagebreak we store it in the $questions array
  289.             if($row['type'<> 'pagebreak')
  290.             {
  291.                 $questions[$row['sort']]['question_id'$row['question_id'];
  292.                 $questions[$row['sort']]['survey_id'$row['survey_id'];
  293.                 $questions[$row['sort']]['survey_question'$row['survey_question'];
  294.                 $questions[$row['sort']]['display'$row['display'];
  295.                 $questions[$row['sort']]['type'$row['type'];
  296.                 $questions[$row['sort']]['options'][$row['question_option_id']] $row['option_text'];
  297.                 $questions[$row['sort']]['maximum_score'$row['max_value'];
  298.             }
  299.             // if the type is a pagebreak we are finished loading the questions for this page
  300.             else
  301.             {
  302.                 break;
  303.             }
  304.             $counter++;
  305.         }
  306.     }
  307. }
  308.  
  309. // selecting the maximum number of pages
  310. $sql "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_invitation['survey_id'])."'";
  311. $result api_sql_query($sql__FILE____LINE__);
  312. $numberofpages mysql_num_rows($result1;
  313.     // Displaying the form with the questions
  314. if (isset($_GET['show']))
  315. {
  316.     $show = (int)$_GET['show'1;
  317. }
  318. else
  319. {
  320.     $show 0;
  321. }
  322.  
  323.  
  324. // Displaying the form with the questions
  325. $g_c (isset($_GET['course'])?Security::remove_XSS($_GET['course']):'');
  326. $g_ic (isset($_GET['invitationcode'])?Security::remove_XSS($_GET['invitationcode']):'');
  327. $g_cr (isset($_GET['cidReq'])?Security::remove_XSS($_GET['cidReq']):'');
  328. $p_l (isset($_POST['language'])?Security::remove_XSS($_POST['language']):'');
  329. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?course='.$g_c.'&invitationcode='.$g_ic.'&show='.$show.'&cidReq='.$g_cr.'">';
  330. echo '<input type="hidden" name="language" value="'.$p_l.'" />';
  331. if(isset($questions&& is_array($questions)){
  332.     foreach ($questions as $key=>$question)
  333.     {
  334.         $display new $question['type'];
  335.         $display->render_question($question);
  336.     }
  337. }
  338. if (($show $numberofpagesOR !$_GET['show'])
  339. {
  340.     //echo '<a href="'.api_get_self().'?survey_id='.$survey_invitation['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
  341.     echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  342. }
  343. if ($show >= $numberofpages AND $_GET['show'])
  344. {
  345.     echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  346. }
  347. echo '</form>';
  348.  
  349. // Footer
  350. ?>

Documentation generated on Thu, 12 Jun 2008 13:29:49 -0500 by phpDocumentor 1.4.1