fillsurvey.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  7. * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality Test modification and rewriting large parts of the code as well
  8. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  9. *
  10. * @todo use quickforms for the forms
  11. * @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.
  12. * alterantively we could not allow people from filling the survey twice.
  13. * @todo performance could be improved if not the survey_id was stored with the invitation but the survey_code
  14. */
  15. // Language file that needs to be included
  16. $language_file = 'survey';
  17. // Unsetting the course id (because it is in the URL)
  18. if (!isset($_GET['cidReq'])) {
  19. $cidReset = true;
  20. } else {
  21. $_cid = $_GET['cidReq'];
  22. }
  23. // Including the global initialization file
  24. require '../inc/global.inc.php';
  25. // Including additional libraries
  26. //require_once api_get_path(LIBRARY_PATH).'survey.lib.php';
  27. require_once 'survey.lib.php';
  28. // Breadcrumbs
  29. if (!empty($_user)) {
  30. $interbreadcrumb[] = array('url' => 'survey_list.php?cidReq='.Security::remove_XSS($_GET['course']), 'name' => get_lang('SurveyList'));
  31. }
  32. // Header
  33. Display :: display_header(get_lang('ToolSurvey'));
  34. // getting all the course information
  35. $_course = CourseManager::get_course_information($_GET['course']);
  36. // Database table definitions
  37. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  38. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  39. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION, $_course['db_name']);
  40. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $_course['db_name']);
  41. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  42. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  43. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']);
  44. // First we check if the needed parameters are present
  45. if ((!isset($_GET['course']) || !isset($_GET['invitationcode']))&& !isset($_GET['user_id'])) {
  46. Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'), false);
  47. Display :: display_footer();
  48. exit;
  49. }
  50. $invitationcode = $_GET['invitationcode'];
  51. $course_id = api_get_course_int_id();
  52. // Start auto-invitation feature FS#3403 (all-users-can-do-the-survey-URL handling)
  53. if ($invitationcode == 'auto' && isset($_GET['scode'])){
  54. // Not intended for anonymous users
  55. if (!(isset($_user['user_id']) && $_user['user_id']) || api_is_anonymous($_user['user_id'],true)) {
  56. api_not_allowed();
  57. }
  58. $userid = $_user['user_id'];
  59. $scode = Database::escape_string($_GET['scode']); // Survey_code of the survey
  60. $autoInvitationcode = "auto-$userid-$scode"; // New invitation code from userid
  61. // The survey code must exist in this course, or the URL is invalid
  62. $sql = "SELECT * FROM $table_survey WHERE code ='" . $scode . "'";
  63. $result = Database::query($sql);
  64. if (Database :: num_rows($result) > 0) { // Ok
  65. // Check availability
  66. $row = Database :: fetch_array($result, 'ASSOC');
  67. $tempdata = survey_manager :: get_survey($row['survey_id']);
  68. check_time_availability($tempdata); //exit if survey not available anymore
  69. // Check for double invitation records (insert should be done once)
  70. $sql = "SELECT user from $table_survey_invitation WHERE invitation_code = '".Database::escape_string($autoInvitationcode)."'";
  71. $result = Database::query($sql);
  72. if (Database :: num_rows($result) == 0) { // Ok
  73. $sql = "INSERT INTO $table_survey_invitation (c_id, survey_code,user, invitation_code, invitation_date) ";
  74. $sql .= " values ($course_id, \"$scode\", \"$userid\", \"$autoInvitationcode\", now())";
  75. Database::query($sql);
  76. }
  77. // From here we use the new invitationcode auto-userid-surveycode string
  78. $_GET['invitationcode'] = $autoInvitationcode;
  79. $invitationcode = $autoInvitationcode;
  80. }
  81. }
  82. // Now we check if the invitationcode is valid
  83. $sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '" . Database :: escape_string($invitationcode) . "'";
  84. $result = Database::query($sql); // false = suppress errors
  85. if (Database::num_rows($result) < 1) {
  86. Display :: display_error_message(get_lang('WrongInvitationCode'), false);
  87. Display :: display_footer();
  88. exit;
  89. }
  90. $survey_invitation = Database::fetch_array($result, 'ASSOC');
  91. // Now we check if the user already filled the survey
  92. if ($survey_invitation['answered'] == 1 && !isset($_GET['user_id'])) {
  93. Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false);
  94. Display :: display_footer();
  95. exit();
  96. }
  97. // Checking if there is another survey with this code.
  98. // If this is the case there will be a language choice
  99. $sql = "SELECT * FROM $table_survey WHERE code='".Database::escape_string($survey_invitation['survey_code'])."'";
  100. $result = Database::query($sql);
  101. if (Database::num_rows($result) > 1) {
  102. if ($_POST['language']) {
  103. $survey_invitation['survey_id'] = $_POST['language'];
  104. } else {
  105. echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&cidReq='.$_GET['cidReq'].'">';
  106. echo ' <select name="language">';
  107. while ($row=Database::fetch_array($result, 'ASSOC')) {
  108. echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  109. }
  110. echo '</select>';
  111. //echo ' <input type="submit" name="Submit" value="' . get_lang('Ok') . '" class="next" />';
  112. echo '<button type="submit" name="Submit" class="next">'.get_lang('Ok').'</button>';
  113. echo '</form>';
  114. display::display_footer();
  115. exit();
  116. }
  117. } else {
  118. $row=Database::fetch_array($result, 'ASSOC');
  119. $survey_invitation['survey_id'] = $row['survey_id'];
  120. }
  121. // Getting the survey information
  122. $survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
  123. $survey_data['survey_id'] = $survey_invitation['survey_id'];
  124. // Storing the answers
  125. if (count($_POST) > 0) {
  126. if ($survey_data['survey_type'] === '0') {
  127. // Getting all the types of the question (because of the special treatment of the score question type
  128. $sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'";
  129. $result = Database::query($sql);
  130. while ($row = Database::fetch_array($result, 'ASSOC')) {
  131. $types[$row['question_id']] = $row['type'];
  132. }
  133. // Looping through all the post values
  134. foreach ($_POST as $key => & $value) {
  135. // If the post value key contains the string 'question' then it is an answer on a question
  136. if (strpos($key, 'question') !== false) {
  137. // Finding the question id by removing 'question'
  138. $survey_question_id = str_replace('question', '', $key);
  139. // If the post value is an array then we have a multiple response question or a scoring question type
  140. // remark: when it is a multiple response then the value of the array is the option_id
  141. // when it is a scoring question then the key of the array is the option_id and the value is the value
  142. if (is_array($value)) {
  143. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  144. foreach ($value as $answer_key => & $answer_value) {
  145. if ($types[$survey_question_id] == 'score') {
  146. $option_id = $answer_key;
  147. $option_value = $answer_value;
  148. } else {
  149. $option_id = $answer_value;
  150. $option_value = '';
  151. }
  152. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $option_id, $option_value, $survey_data);
  153. }
  154. }
  155. // All the other question types (open question, multiple choice, percentage, ...)
  156. else {
  157. if ($types[$survey_question_id] == 'percentage') {
  158. $sql = "SELECT * FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
  159. $result = Database::query($sql);
  160. $row = Database::fetch_array($result, 'ASSOC');
  161. $option_value = $row['option_text'];
  162. } else {
  163. $option_value = 0;
  164. if ($types[$survey_question_id] == 'open') {
  165. $option_value = $value;
  166. //$value = 0;
  167. }
  168. }
  169. $survey_question_answer = $value;
  170. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  171. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  172. //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  173. }
  174. }
  175. }
  176. } elseif ($survey_data['survey_type'] === '1') { //conditional/personality-test type surveys
  177. // Getting all the types of the question (because of the special treatment of the score question type
  178. $shuffle='';
  179. if ($survey_data['shuffle'] == '1') {
  180. $shuffle= ' ORDER BY RAND() ';
  181. }
  182. $sql = "SELECT * FROM $table_survey_question
  183. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  184. AND survey_group_pri='0' $shuffle";
  185. $result = Database::query($sql);
  186. // There is only one question type for conditional surveys
  187. while ($row = Database::fetch_array($result, 'ASSOC')) {
  188. $types[$row['question_id']] = $row['type'];
  189. }
  190. // Looping through all the post values
  191. foreach ($_POST as $key => & $value) {
  192. // If the post value key contains the string 'question' then it is an answer to a question
  193. if (strpos($key, 'question') !== false) {
  194. // Finding the question id by removing 'question'
  195. $survey_question_id = str_replace('question', '', $key);
  196. // We select the correct answer and the puntuacion
  197. $sql = "SELECT value FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
  198. $result = Database::query($sql);
  199. $row = Database::fetch_array($result, 'ASSOC');
  200. $option_value = $row['value'];
  201. //$option_value = 0;
  202. $survey_question_answer = $value;
  203. // We save the answer after making sure that a possible previous attempt is deleted
  204. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  205. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  206. //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  207. }
  208. }
  209. } else { // In case it's another type than 0 or 1
  210. die(get_lang('ErrorSurveyTypeUnknown'));
  211. }
  212. }
  213. // Displaying the survey title and subtitle (appears on every page)
  214. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  215. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  216. // Checking time availability
  217. check_time_availability($survey_data);
  218. // Displaying the survey introduction
  219. if (!isset($_GET['show'])) {
  220. // The first thing we do is delete the session
  221. unset($_SESSION['paged_questions']);
  222. unset($_SESSION['page_questions_sec']);
  223. $paged_questions_sec=array();
  224. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  225. $limit = 0;
  226. }
  227. $user_id = api_get_user_id();
  228. if ($user_id == 0) {
  229. $user_id = $survey_invitation['user'];
  230. }
  231. $user_data = UserManager :: get_user_info_by_id($user_id);
  232. if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_array($user_data)) {
  233. //echo '<pre>'; print_r($survey_invitation);
  234. $form_fields = explode('@', $survey_data['form_fields']);
  235. $list = array();
  236. foreach ($form_fields as $field) {
  237. $field_value = explode(':', $field);
  238. if ($field_value[1] == 1) {
  239. if ($field_value[0] != '') {
  240. $val = api_substr($field_value[0], 8, api_strlen($field_value[0]));
  241. $list[$val] = 1;
  242. }
  243. }
  244. }
  245. // We use the same form as in auth/profile.php
  246. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  247. $form = new FormValidator('profile', 'post', api_get_self() . "?" . str_replace('&show_form=1', '&show_form=1', $_SERVER['QUERY_STRING']), null,
  248. array('style' => 'width: 75%; float: ' . ($text_dir == 'rtl' ? 'right;' : 'left;'))
  249. );
  250. if (api_is_western_name_order()) {
  251. if ($list['firstname'] == 1 ) {
  252. //FIRST NAME
  253. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  254. if (api_get_setting('profile', 'name') !== 'true') {
  255. $form->freeze(array('firstname'));
  256. }
  257. $form->applyFilter(array('firstname'), 'stripslashes');
  258. $form->applyFilter(array('firstname'), 'trim');
  259. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  260. }
  261. if ($list['lastname'] == 1) {
  262. // LAST NAME
  263. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  264. if (api_get_setting('profile', 'name') !== 'true') {
  265. $form->freeze(array('lastname'));
  266. }
  267. $form->applyFilter(array('lastname'), 'stripslashes');
  268. $form->applyFilter(array('lastname'), 'trim');
  269. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  270. }
  271. } else {
  272. if ($list['lastname'] == 1) {
  273. // LAST NAME
  274. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  275. if (api_get_setting('profile', 'name') !== 'true') {
  276. $form->freeze(array('lastname'));
  277. }
  278. $form->applyFilter(array('lastname'), 'stripslashes');
  279. $form->applyFilter(array('lastname'), 'trim');
  280. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  281. }
  282. if ($list['firstname'] == 1 ) {
  283. //FIRST NAME
  284. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  285. if (api_get_setting('profile', 'name') !== 'true') {
  286. $form->freeze(array('firstname'));
  287. }
  288. $form->applyFilter(array('firstname'), 'stripslashes');
  289. $form->applyFilter(array('firstname'), 'trim');
  290. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  291. }
  292. }
  293. if ($list['official_code'] == 1) {
  294. // OFFICIAL CODE
  295. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  296. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => 40));
  297. if (api_get_setting('profile', 'officialcode') !== 'true') {
  298. $form->freeze('official_code');
  299. }
  300. $form->applyFilter('official_code', 'stripslashes');
  301. $form->applyFilter('official_code', 'trim');
  302. if (api_get_setting('registration', 'officialcode') == 'true' && api_get_setting('profile', 'officialcode') == 'true') {
  303. $form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required');
  304. }
  305. }
  306. }
  307. if ($list['email'] == 1) {
  308. // EMAIL
  309. $form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
  310. if (api_get_setting('profile', 'email') !== 'true') {
  311. $form->freeze('email');
  312. }
  313. $form->applyFilter('email', 'stripslashes');
  314. $form->applyFilter('email', 'trim');
  315. if (api_get_setting('registration', 'email') == 'true') {
  316. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  317. }
  318. $form->addRule('email', get_lang('EmailWrong'), 'email');
  319. }
  320. if ($list['phone'] == 1) {
  321. // PHONE
  322. $form->addElement('text', 'phone', get_lang('phone'), array('size' => 20));
  323. if (api_get_setting('profile', 'phone') !== 'true') {
  324. $form->freeze('phone');
  325. }
  326. $form->applyFilter('phone', 'stripslashes');
  327. $form->applyFilter('phone', 'trim');
  328. if (api_get_setting('profile', 'phone') == 'true') {
  329. $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
  330. }
  331. }
  332. if ($list['language'] == 1) {
  333. // LANGUAGE
  334. $form->addElement('select_language', 'language', get_lang('Language'));
  335. if (api_get_setting('profile', 'language') !== 'true') {
  336. $form->freeze('language');
  337. }
  338. if (api_get_setting('profile', 'language') == 'true') {
  339. $form->addRule('language', get_lang('ThisFieldIsRequired'), 'required');
  340. }
  341. }
  342. // EXTRA FIELDS
  343. $extra = UserManager :: get_extra_fields(0, 50, 5, 'ASC');
  344. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  345. foreach ($extra as $id => $field_details) {
  346. if ($field_details[6] == 0) {
  347. continue;
  348. }
  349. if ($list['extra_' . $field_details[1]] == 1) {
  350. switch ($field_details[2]) {
  351. case USER_FIELD_TYPE_TEXT :
  352. $form->addElement('text', 'extra_' . $field_details[1], $field_details[3], array('size' => 40));
  353. $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
  354. $form->applyFilter('extra_' . $field_details[1], 'trim');
  355. if ($field_details[7] == 0) {
  356. $form->freeze('extra_' . $field_details[1]);
  357. }
  358. break;
  359. case USER_FIELD_TYPE_TEXTAREA :
  360. $form->add_html_editor('extra_' . $field_details[1], $field_details[3], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130'));
  361. $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
  362. $form->applyFilter('extra_' . $field_details[1], 'trim');
  363. if ($field_details[7] == 0) {
  364. $form->freeze('extra_' . $field_details[1]);
  365. }
  366. break;
  367. case USER_FIELD_TYPE_RADIO :
  368. $group = array();
  369. foreach ($field_details[9] as $option_id => $option_details) {
  370. $options[$option_details[1]] = $option_details[2];
  371. $group[] = & HTML_QuickForm :: createElement('radio', 'extra_' . $field_details[1], $option_details[1], $option_details[2] . '<br />', $option_details[1]);
  372. }
  373. $form->addGroup($group, 'extra_' . $field_details[1], $field_details[3], '');
  374. if ($field_details[7] == 0) {
  375. $form->freeze('extra_' . $field_details[1]);
  376. }
  377. break;
  378. case USER_FIELD_TYPE_SELECT :
  379. $options = array();
  380. foreach ($field_details[9] as $option_id => $option_details) {
  381. $options[$option_details[1]] = $option_details[2];
  382. }
  383. $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, '');
  384. if ($field_details[7] == 0) {
  385. $form->freeze('extra_' . $field_details[1]);
  386. }
  387. break;
  388. case USER_FIELD_TYPE_SELECT_MULTIPLE :
  389. $options = array();
  390. foreach ($field_details[9] as $option_id => $option_details) {
  391. $options[$option_details[1]] = $option_details[2];
  392. }
  393. $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, array('multiple' => 'multiple'));
  394. if ($field_details[7] == 0) {
  395. $form->freeze('extra_' . $field_details[1]);
  396. }
  397. break;
  398. case USER_FIELD_TYPE_DATE :
  399. $form->addElement('datepickerdate', 'extra_' . $field_details[1], $field_details[3], array('form_name' => 'profile'));
  400. $form->_elements[$form->_elementIndex['extra_' . $field_details[1]]]->setLocalOption('minYear', 1900);
  401. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  402. $form -> setDefaults($defaults);
  403. if ($field_details[7] == 0) {
  404. $form->freeze('extra_' . $field_details[1]);
  405. }
  406. $form->applyFilter('theme', 'trim');
  407. break;
  408. case USER_FIELD_TYPE_DATETIME :
  409. $form->addElement('datepicker', 'extra_' . $field_details[1], $field_details[3], array('form_name' => 'profile'));
  410. $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  411. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  412. $form -> setDefaults($defaults);
  413. if ($field_details[7] == 0) {
  414. $form->freeze('extra_' . $field_details[1]);
  415. }
  416. $form->applyFilter('theme', 'trim');
  417. break;
  418. case USER_FIELD_TYPE_DOUBLE_SELECT :
  419. foreach ($field_details[9] as $key => $element) {
  420. if ($element[2][0] == '*') {
  421. $values['*'][$element[0]] = str_replace('*', '', $element[2]);
  422. } else {
  423. $values[0][$element[0]] = $element[2];
  424. }
  425. }
  426. $group = '';
  427. $group[] = & HTML_QuickForm :: createElement('select', 'extra_' . $field_details[1], '', $values[0], '');
  428. $group[] = & HTML_QuickForm :: createElement('select', 'extra_' . $field_details[1] . '*', '', $values['*'], '');
  429. $form->addGroup($group, 'extra_' . $field_details[1], $field_details[3], '&nbsp;');
  430. if ($field_details[7] == 0) {
  431. $form->freeze('extra_' . $field_details[1]);
  432. }
  433. // Recoding the selected values for double : if the user has selected certain values, we have to assign them to the correct select form
  434. if (key_exists('extra_' . $field_details[1], $extra_data)) {
  435. // Exploding all the selected values (of both select forms)
  436. $selected_values = explode(';', $extra_data['extra_' . $field_details[1]]);
  437. $extra_data['extra_' . $field_details[1]] = array();
  438. // Looping through the selected values and assigning the selected values to either the first or second select form
  439. foreach ($selected_values as $key => $selected_value) {
  440. if (key_exists($selected_value, $values[0])) {
  441. $extra_data['extra_' . $field_details[1]]['extra_' . $field_details[1]] = $selected_value;
  442. } else {
  443. $extra_data['extra_' . $field_details[1]]['extra_' . $field_details[1] . '*'] = $selected_value;
  444. }
  445. }
  446. }
  447. break;
  448. case USER_FIELD_TYPE_DIVIDER :
  449. $form->addElement('static', $field_details[1], '<br /><strong>' . $field_details[3] . '</strong>');
  450. break;
  451. }
  452. $form->addRule('extra_' . $field_details[1], get_lang('ThisFieldIsRequired'), 'required');
  453. }
  454. }
  455. $form->addElement('style_submit_button', '', get_lang('Next'), array('class' => 'next'));
  456. $user_data = array_merge($user_data, $extra_data);
  457. $form->setDefaults($user_data);
  458. }
  459. if ($survey_data['form_fields'] && $survey_data['anonymous'] == 0 && is_array($user_data) && !isset($_GET['show'])) {
  460. if ($form->validate()) {
  461. $user_data = $form->exportValues();
  462. if (is_array($user_data)) {
  463. if (count($user_data) > 0) {
  464. $extras = array();
  465. // Build SQL query
  466. $sql = "UPDATE $table_user SET";
  467. foreach ($user_data as $key => $value) {
  468. if (substr($key, 0, 6) == 'extra_') { //an extra field
  469. $extras[substr($key, 6)] = $value;
  470. } else {
  471. $sql .= " $key = '" . Database :: escape_string($value) . "',";
  472. }
  473. }
  474. // Remove trailing , from the query we have so far
  475. $sql = rtrim($sql, ',');
  476. $sql .= " WHERE user_id = '" . $user_id . "'";
  477. Database::query($sql);
  478. // Update the extra fields
  479. if (is_array($extras)) {
  480. foreach ($extras as $key => $value) {
  481. $myres = UserManager :: update_extra_field_value($user_id, $key, $value);
  482. }
  483. }
  484. echo '<div id="survey_content" class="survey_content">' . get_lang('InformationUpdated') . ' ' . get_lang('PleaseFillSurvey') . '</div>';
  485. }
  486. }
  487. //$_GET['show_form'] = 0;
  488. //$show_form = 0;
  489. $_GET['show'] = 0;
  490. $show = 0;
  491. // We unset the sessions
  492. unset($_SESSION['paged_questions']);
  493. unset($_SESSION['page_questions_sec']);
  494. $paged_questions_sec = array();
  495. }
  496. //elseif ($_GET['show_form'] == 1) { // Displaying the field
  497. else {
  498. echo '<div id="survey_content" class="survey_content">' . get_lang('UpdateInformation') . '</div>';
  499. // We unset the sessions
  500. unset($_SESSION['paged_questions']);
  501. unset($_SESSION['page_questions_sec']);
  502. $paged_questions_sec = array();
  503. $form->display();
  504. }
  505. }
  506. // Displaying the survey thanks message
  507. if (isset($_POST['finish_survey'])) {
  508. echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').'</strong> <br />'.$survey_data['survey_thanks'].'</div>';
  509. survey_manager::update_survey_answered($survey_data['survey_id'], $survey_invitation['user'], $survey_invitation['survey_code']);
  510. unset($_SESSION['paged_questions']);
  511. unset($_SESSION['page_questions_sec']);
  512. Display :: display_footer();
  513. exit();
  514. }
  515. // Sets the random questions
  516. $shuffle = '';
  517. if ($survey_data['shuffle'] == 1) {
  518. $shuffle = ' BY RAND() ';
  519. }
  520. if (isset($_GET['show']) || isset($_POST['personality'])) {
  521. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  522. // As long as there is no pagebreak fount we keep adding questions to the page
  523. $questions_displayed = array();
  524. $counter = 0;
  525. $paged_questions = array();
  526. // If non-conditional survey
  527. if ($survey_data['survey_type'] === '0') {
  528. if (empty($_SESSION['paged_questions'])) {
  529. $sql = "SELECT * FROM $table_survey_question
  530. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  531. ORDER BY sort ASC";
  532. $result = Database::query($sql);
  533. while ($row = Database::fetch_array($result, 'ASSOC')) {
  534. if ($row['type'] == 'pagebreak') {
  535. $counter++;
  536. } else {
  537. // ids from question of the current survey
  538. $paged_questions[$counter][] = $row['question_id'];
  539. }
  540. }
  541. $_SESSION['paged_questions'] = $paged_questions;
  542. } else {
  543. $paged_questions = $_SESSION['paged_questions'];
  544. }
  545. $course_id = api_get_course_int_id();
  546. if (key_exists($_GET['show'], $paged_questions)) {
  547. if (isset($_GET['user_id'])) {
  548. // Get the user into survey answer table (user or anonymus)
  549. $my_user_id=($survey_data['anonymous']==1) ? $_SESSION['surveyuser'] :api_get_user_id();
  550. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  551. 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,
  552. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  553. FROM $table_survey_question survey_question
  554. LEFT JOIN $table_survey_question_option survey_question_option
  555. ON survey_question.question_id = survey_question_option.question_id
  556. WHERE survey_question.survey_id = '" . Database :: escape_string($survey_invitation['survey_id']) . "'
  557. AND survey_question.question_id NOT IN (SELECT sa.question_id FROM ".$table_survey_answer." sa WHERE sa.user='".$my_user_id."') AND
  558. survey_question_option.c_id = $course_id AND
  559. survey_question.c_id = $course_id
  560. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  561. } else {
  562. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  563. 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,
  564. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  565. FROM $table_survey_question survey_question
  566. LEFT JOIN $table_survey_question_option survey_question_option
  567. ON survey_question.question_id = survey_question_option.question_id
  568. WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' AND
  569. survey_question.question_id IN (".implode(',',$paged_questions[$_GET['show']]).") AND
  570. survey_question_option.c_id = $course_id AND
  571. survey_question.c_id = $course_id
  572. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  573. }
  574. $result = Database::query($sql);
  575. $question_counter_max = Database::num_rows($result);
  576. $counter = 0;
  577. $limit = 0;
  578. $questions = array();
  579. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  580. // If the type is not a pagebreak we store it in the $questions array
  581. if ($row['type'] != 'pagebreak') {
  582. $questions[$row['sort']]['question_id'] = $row['question_id'];
  583. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  584. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  585. $questions[$row['sort']]['display'] = $row['display'];
  586. $questions[$row['sort']]['type'] = $row['type'];
  587. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  588. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  589. }
  590. // If the type is a pagebreak we are finished loading the questions for this page
  591. else {
  592. break;
  593. }
  594. $counter++;
  595. }
  596. }
  597. } elseif ($survey_data['survey_type'] === '1') {
  598. $my_survey_id=Database::escape_string($survey_invitation['survey_id']);
  599. $current_user = Database::escape_string($survey_invitation['user']);
  600. if (isset($_POST['personality'])) {
  601. // Compute the results to get the 3 groups nearest to the user's personality
  602. if ($shuffle == '') {
  603. $order = 'BY sort ASC ';
  604. } else {
  605. $order = $shuffle;
  606. }
  607. $answer_list = array();
  608. // Get current user results
  609. $results = array();
  610. $sql = "SELECT survey_group_pri, user, SUM(value) as value
  611. FROM $table_survey_answer as survey_answer INNER JOIN $table_survey_question as survey_question
  612. ON (survey_question.question_id = survey_answer.question_id)
  613. WHERE survey_answer.survey_id='".$my_survey_id."' AND
  614. survey_answer.user='".$current_user."'
  615. GROUP BY survey_group_pri
  616. ORDER BY survey_group_pri
  617. ";
  618. $result = Database::query($sql);
  619. while ($row = Database :: fetch_array($result)) {
  620. $answer_list['value'] = $row['value'];
  621. $answer_list['group'] = $row['survey_group_pri'];
  622. $results[] = $answer_list;
  623. }
  624. //echo '<br />'; print_r($results); echo '<br />';
  625. // Get the total score for each group of questions
  626. $totals = array();
  627. $sql = "SELECT SUM(temp.value) as value, temp.survey_group_pri FROM
  628. (SELECT MAX(value) as value, survey_group_pri, survey_question.question_id
  629. FROM $table_survey_question as survey_question
  630. INNER JOIN $table_survey_question_option as survey_question_option
  631. ON (survey_question.question_id = survey_question_option.question_id)
  632. WHERE survey_question.survey_id='".$my_survey_id."' AND survey_group_sec1='0' AND survey_group_sec2='0'
  633. GROUP BY survey_group_pri, survey_question.question_id) as temp
  634. GROUP BY temp.survey_group_pri
  635. ORDER BY temp.survey_group_pri";
  636. $result = Database::query($sql);
  637. while ($row = Database::fetch_array($result)) {
  638. $list['value'] = $row['value'];
  639. $list['group'] = $row['survey_group_pri'];
  640. $totals[] = $list;
  641. }
  642. //echo '<pre>'; print_r($totals);
  643. $final_results = array();
  644. // Get a percentage score for each group
  645. for ($i = 0; $i < count($totals); $i++) {
  646. for ($j = 0; $j < count($results); $j++) {
  647. if ($totals[$i]['group'] == $results[$j]['group']) {
  648. $group = $totals[$i]['group'];
  649. $porcen = ($results[$j]['value'] / $totals[$i]['value']);
  650. $final_results[$group] = $porcen;
  651. }
  652. }
  653. }
  654. // Sort the results by score (getting a list of group IDs by score into $groups)
  655. arsort($final_results);
  656. $groups=array_keys($final_results);
  657. /*
  658. echo '<pre>';
  659. echo 'Group id => %';
  660. echo '<br />';
  661. print_r($final_results);
  662. echo '</pre>';
  663. */
  664. $result = array();
  665. $count_result = 0;
  666. foreach ($final_results as $key => & $sub_result) {
  667. $result[] = array('group' => $key, 'value' => $sub_result);
  668. $count_result++;
  669. }
  670. /*
  671. //i.e 70% - 70% -70% 70% $equal_count =3
  672. while (1) {
  673. if ($result[$i]['value'] == $result[$i+1]['value']) {
  674. $equal_count++;
  675. } else {
  676. break;
  677. }
  678. $i++;
  679. }
  680. echo 'eq'. $equal_count;
  681. echo '<br />';
  682. if ($equal_count == 0) {
  683. //i.e 70% 70% -60% 60% $equal_count = 1 only we get the first 2 options
  684. if (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  685. $group_cant = 1;
  686. } else {
  687. // By default we chose the highest 3
  688. $group_cant=2;
  689. }
  690. } elseif ($equal_count == 2) {
  691. $group_cant = 2;
  692. } else {
  693. $group_cant = -1;
  694. }
  695. */
  696. // i.e 70% - 70% -70% 70% $equal_count =3
  697. $i = 0;
  698. $group_cant = 0;
  699. $equal_count = 0;
  700. // This is the case if the user does not select any question
  701. if ($count_result > 0) {
  702. // Count the number of scores equal to the first
  703. while (1) {
  704. if ($result[$i]['value'] == $result[$i + 1]['value']) {
  705. $equal_count++;
  706. } else {
  707. break;
  708. }
  709. $i++;
  710. }
  711. } else {
  712. // We force the exit of the survey undeterminated
  713. $equal_count=10;
  714. }
  715. //echo '<pre>';
  716. //print_r($result);
  717. // If we have only 3 or less equal scores (i.e. 0,1 or 2 equalities), then we can use the three first groups
  718. if ($equal_count < 4) {
  719. // If there is one or less score equalities
  720. if ($equal_count === 0 || $equal_count === 1) {
  721. // i.e 70% - 70% -60% - 60% $equal_count = 1 we only get the first 2 options
  722. if (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  723. $group_cant = 1;
  724. }
  725. // i.e 70% - 70% -0% - 0% - $equal_count = 0 we only get the first 2 options
  726. /*elseif (($result[0]['value'] == $result[1]['value']) && ($result[1]['value'] != $result[2]['value'])) {
  727. $group_cant = 0;
  728. }*/
  729. /*
  730. // i.e 70% - 70% -60% - 60% $equal_count = 0 we only get the first 2 options
  731. elseif (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  732. $group_cant = 0;
  733. }*/
  734. // i.e. 80% - 70% - 70% - 70%
  735. elseif (($result[0]['value'] != $result[1]['value']) && ($result[1]['value'] == $result[2]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  736. $group_cant = 0;
  737. } else {
  738. // i.e. 80% - 70% - 70% - 50
  739. // i.e. 80% - 80% - 70% - 50
  740. // By default we choose the highest 3
  741. $group_cant = 2;
  742. }
  743. } else {
  744. // If there are two score equalities
  745. $group_cant = $equal_count;
  746. }
  747. //@todo Translate these comments.
  748. // conditional_status
  749. // 0 no determinado
  750. // 1 determinado
  751. // 2 un solo valor
  752. // 3 valores iguales
  753. if ($group_cant > 0) {
  754. //echo '$equal_count'.$group_cant;
  755. // We only get highest 3
  756. $secondary = '';
  757. $combi = '';
  758. for ($i = 0; $i <= $group_cant; $i++) {
  759. $group1 = $groups[$i];
  760. $group2 = $groups[$i + 1];
  761. // Here we made all the posibilities with the 3 groups
  762. if ($group_cant == 2 && $i == $group_cant) {
  763. $group2 = $groups[0];
  764. $secondary .= " OR ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  765. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  766. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
  767. } else {
  768. if ($i != 0) {
  769. $secondary .= " OR ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  770. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  771. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
  772. } else {
  773. $secondary .= " ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  774. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  775. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
  776. }
  777. }
  778. }
  779. /*
  780. echo '<pre>';
  781. echo 'Pair of Groups <br /><br />';
  782. echo $combi;
  783. echo '</pre>';
  784. */
  785. // Create the new select with the questions from the secondary phase
  786. if (empty($_SESSION['page_questions_sec']) && !is_array($_SESSION['page_questions_sec']) && count($_SESSION['page_questions_sec'] == 0)) {
  787. $sql = "SELECT * FROM $table_survey_question
  788. WHERE survey_id = '".$my_survey_id."'
  789. AND ($secondary )
  790. ORDER BY sort ASC";
  791. $result = Database::query($sql);
  792. $counter = 0;
  793. while ($row = Database::fetch_array($result, 'ASSOC')) {
  794. if ($survey_data['one_question_per_page'] == 1) {
  795. $paged_questions_sec[$counter][] = $row['question_id'];
  796. $counter++;
  797. } elseif ($row['type'] == 'pagebreak') {
  798. $counter++;
  799. } else {
  800. // ids from question of the current survey
  801. $paged_questions_sec[$counter][] = $row['question_id'];
  802. }
  803. }
  804. $_SESSION['paged_questions_sec'] = $paged_questions_sec;
  805. } else {
  806. $paged_questions_sec = $_SESSION['paged_questions_sec'];
  807. }
  808. //print_r($paged_questions_sec);
  809. $paged_questions = $_SESSION['paged_questions']; // For the sake of pages counting
  810. //$paged_questions = $paged_questions_sec; // For the sake of pages counting coming up at display time...
  811. if ($shuffle == '') {
  812. $shuffle = ' BY survey_question.sort, survey_question_option.sort ASC ';
  813. }
  814. //$val = 0;
  815. //if ($survey_data['one_question_per_page'] == 0) {
  816. $val = (int)$_POST['personality'];
  817. //}
  818. //echo '<pre>'; print_r($paged_questions_sec); echo '</pre>';
  819. if (is_array($paged_questions_sec)) {
  820. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  821. 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,
  822. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  823. FROM $table_survey_question survey_question
  824. LEFT JOIN $table_survey_question_option survey_question_option
  825. ON survey_question.question_id = survey_question_option.question_id
  826. WHERE survey_question.survey_id = '".$my_survey_id."'
  827. AND survey_question.question_id IN (".implode(',',$paged_questions_sec[$val]).")
  828. ORDER $shuffle ";
  829. $result = Database::query($sql);
  830. $question_counter_max = Database::num_rows($result);
  831. $counter = 0;
  832. $limit = 0;
  833. $questions = array();
  834. while ($row = Database::fetch_array($result, 'ASSOC')) {
  835. // If the type is not a pagebreak we store it in the $questions array
  836. if ($row['type'] != 'pagebreak') {
  837. $questions[$row['sort']]['question_id'] = $row['question_id'];
  838. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  839. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  840. $questions[$row['sort']]['display'] = $row['display'];
  841. $questions[$row['sort']]['type'] = $row['type'];
  842. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  843. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  844. // Personality params
  845. $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
  846. $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
  847. $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
  848. }
  849. // If the type is a pagebreak we are finished loading the questions for this page
  850. else {
  851. break;
  852. }
  853. $counter++;
  854. }
  855. } else {
  856. echo get_lang('SurveyUndetermined');
  857. }
  858. } else {
  859. echo get_lang('SurveyUndetermined');
  860. }
  861. } else {
  862. echo get_lang('SurveyUndetermined');
  863. }
  864. } else {
  865. // We need this variable only in the 2nd set of questions when personality is set.
  866. unset($_SESSION['page_questions_sec']);
  867. $paged_questions_sec=array();
  868. // Only the questions from the basic group
  869. // the 50 questions A B C D E F G
  870. $order_sql = $shuffle;
  871. if ($shuffle == '') {
  872. $order_sql=' BY question_id ';
  873. }
  874. if (empty($_SESSION['paged_questions'])) {
  875. $sql = "SELECT * FROM $table_survey_question
  876. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  877. AND survey_group_sec1='0' AND survey_group_sec2='0'
  878. ORDER ".$order_sql." ";
  879. //echo '<br />'; echo '<br />';
  880. $result = Database::query($sql);
  881. $counter = 0;
  882. while ($row = Database::fetch_array($result, 'ASSOC')) {
  883. if ($survey_data['one_question_per_page'] == 1) {
  884. $paged_questions[$counter][] = $row['question_id'];
  885. $counter++;
  886. } else {
  887. if ($row['type'] == 'pagebreak') {
  888. $counter++;
  889. } else {
  890. // ids from question of the current survey
  891. $paged_questions[$counter][] = $row['question_id'];
  892. }
  893. }
  894. }
  895. $_SESSION['paged_questions'] = $paged_questions;
  896. } else {
  897. $paged_questions = $_SESSION['paged_questions'];
  898. }
  899. //print_r($paged_questions);
  900. //print_r($paged_questions);
  901. //if (key_exists($_GET['show'], $paged_questions)) {
  902. $order_sql = $shuffle;
  903. if ($shuffle == '') {
  904. $order_sql=' BY survey_question.sort, survey_question_option.sort ASC ';
  905. }
  906. //$val = 0;
  907. //if ($survey_data['one_question_per_page'] == 0) {
  908. $val = $_GET['show'];
  909. //}
  910. //echo '<pre>'; print_r($paged_questions); echo $val;
  911. $result = null;
  912. if ($val != '') {
  913. $imploded = implode(',', $paged_questions[$val]);
  914. if ($imploded != '') {
  915. // The answers are always in the same order NO shuffle
  916. $order_sql = ' BY survey_question.sort, survey_question_option.sort ASC ';
  917. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  918. 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,
  919. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  920. FROM $table_survey_question survey_question
  921. LEFT JOIN $table_survey_question_option survey_question_option
  922. ON survey_question.question_id = survey_question_option.question_id
  923. WHERE survey_question.survey_id = '" . Database :: escape_string($survey_invitation['survey_id']) . "'
  924. AND survey_question.question_id IN (" .$imploded. ")
  925. ORDER $order_sql ";
  926. $result = Database::query($sql);
  927. $question_counter_max = Database :: num_rows($result);
  928. }
  929. }
  930. if (!is_null($result)) {
  931. $counter = 0;
  932. $limit = 0;
  933. $questions = array();
  934. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  935. // If the type is not a pagebreak we store it in the $questions array
  936. if ($row['type'] != 'pagebreak') {
  937. $questions[$row['sort']]['question_id'] = $row['question_id'];
  938. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  939. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  940. $questions[$row['sort']]['display'] = $row['display'];
  941. $questions[$row['sort']]['type'] = $row['type'];
  942. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  943. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  944. // Personality params
  945. $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
  946. $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
  947. $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
  948. }
  949. // If the type is a pagebreak we are finished loading the questions for this page
  950. else {
  951. break;
  952. }
  953. $counter++;
  954. }
  955. }
  956. }
  957. } else { // In case it's another type than 0 or 1
  958. echo get_lang('ErrorSurveyTypeUnknown');
  959. }
  960. }
  961. // Selecting the maximum number of pages
  962. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_invitation['survey_id'])."'";
  963. $result = Database::query($sql);
  964. $numberofpages = Database::num_rows($result) + 1;
  965. // Displaying the form with the questions
  966. if (isset($_GET['show'])) {
  967. $show = (int)$_GET['show'] + 1;
  968. } else {
  969. $show = 0;
  970. }
  971. // Displaying the form with the questions
  972. if (isset($_POST['personality'])) {
  973. $personality = (int)$_POST['personality'] + 1;
  974. } else {
  975. $personality = 0;
  976. }
  977. // Displaying the form with the questions
  978. $g_c = isset($_GET['course']) ? Security::remove_XSS($_GET['course']) : '';
  979. $g_ic = isset($_GET['invitationcode']) ? Security::remove_XSS($_GET['invitationcode']) : '';
  980. $g_cr = isset($_GET['cidReq']) ? Security::remove_XSS($_GET['cidReq']) : '';
  981. $p_l = isset($_POST['language']) ? Security::remove_XSS($_POST['language']) : '';
  982. $add_parameters = isset($_GET['user_id']) ? 'user_id='.$_GET['user_id'].'&amp;' : '';
  983. echo '<form id="question" name="question" method="post" action="' . api_get_self() .'?'.$add_parameters. 'course=' . $g_c . '&invitationcode=' . $g_ic . '&show=' . $show . '&cidReq=' . $g_cr . '">';
  984. echo '<input type="hidden" name="language" value="'.$p_l.'" />';
  985. if (isset($questions) && is_array($questions)) {
  986. foreach ($questions as $key => & $question) {
  987. $ch_type = 'ch_'.$question['type'];
  988. $display = new $ch_type;
  989. $display->render_question($question);
  990. }
  991. }
  992. if ($survey_data['survey_type'] === '0') {
  993. if ($survey_data['show_form_profile'] == 0) {
  994. // The normal survey as always
  995. if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
  996. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . ' " class="next" />';
  997. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('NextQuestion').'</button>';
  998. }
  999. if ($show >= $numberofpages && $_GET['show']) {
  1000. //echo '<input type="submit" name="finish_survey" value="' . get_lang('FinishSurvey') . '" class="next" />';
  1001. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  1002. }
  1003. } else {
  1004. // The normal survey as always but with the form profile
  1005. if (isset($_GET['show'])) {
  1006. $numberofpages = count($paged_questions);
  1007. if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
  1008. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . '" class="next" />';
  1009. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  1010. }
  1011. if ($show >= $numberofpages && $_GET['show'] ) {
  1012. //echo '<input type="submit" name="finish_survey" value="' . get_lang('FinishSurvey') . '" class="next" />';
  1013. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  1014. }
  1015. }
  1016. }
  1017. } elseif ($survey_data['survey_type'] === '1') { //conditional/personality-test type survey
  1018. if (isset($_GET['show']) || isset($_POST['personality'])) {
  1019. $numberofpages = count($paged_questions);
  1020. //echo '<br />'; echo 'num pages norma:'.$numberofpages; echo '<br />';
  1021. //echo '<pre>'; print_r($paged_questions_sec);
  1022. if (!empty ($paged_questions_sec) && count($paged_questions_sec) > 0) {
  1023. // In case we're in the second phase, also sum the second group questions
  1024. //echo 'pagesec :'.count($paged_questions_sec);
  1025. $numberofpages += count($paged_questions_sec);
  1026. //echo 'pagesec :';
  1027. } else {
  1028. // We need this variable only if personality == 1
  1029. unset($_SESSION['page_questions_sec']);
  1030. $paged_questions_sec = array();
  1031. }
  1032. /*echo '<br />';
  1033. echo 'num pages:'.$numberofpages; echo '<br />';
  1034. echo 'show :'.$show;echo '<br />';
  1035. echo 'personality :'.$personality;
  1036. echo '<br />';
  1037. */
  1038. //echo $show.' / '.$numberofpages.'<br />';
  1039. if ($personality == 0)
  1040. if (($show <= $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
  1041. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . ' " class="next" />';
  1042. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  1043. if ($survey_data['one_question_per_page'] == 0) {
  1044. if ($personality >= 0) {
  1045. echo '<input type="hidden" name="personality" value="' . $personality . '">';
  1046. }
  1047. } else {
  1048. if ($personality > 0) {
  1049. echo '<input type="hidden" name="personality" value="' . $personality . '">';
  1050. }
  1051. }
  1052. if ($numberofpages == $show) {
  1053. echo '<input type="hidden" name="personality" value="' . $personality . '">';
  1054. }
  1055. }
  1056. if ($show > $numberofpages && $_GET['show'] && $personality == 0) {
  1057. echo '<input type="hidden" name="personality" value="' . $personality . '">';
  1058. //$numberofpages = count($paged_questions);
  1059. //echo $numberofpages = count($paged_questions_sec);
  1060. //echo $personality.' / '.$numberofpages;
  1061. //echo '<br />';
  1062. //if ($personality > count($paged_questions_sec) - 1)
  1063. //|| $numberofpages == $show +$personality +1
  1064. //echo $show + $personality;
  1065. //echo $numberofpages;
  1066. } elseif ($personality > 0) {
  1067. if ($survey_data['one_question_per_page'] == 1) {
  1068. if ($show >= $numberofpages) {
  1069. //echo '<input type="submit" name="finish_survey" value="' . get_lang('FinishSurvey') . ' " class="next" />';
  1070. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  1071. } else {
  1072. echo '<input type="hidden" name="personality" value="' . $personality . '">';
  1073. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . '" class="next" />';
  1074. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  1075. }
  1076. } else {
  1077. // if the personality test hidden input was set.
  1078. //echo '<input type="submit" name="finish_survey" value="' . get_lang('FinishSurvey') . ' " class="next" />';
  1079. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  1080. }
  1081. }
  1082. }
  1083. // This is the case when the show_profile_form is true but there are not form_fields
  1084. elseif ($survey_data['form_fields'] == '') {
  1085. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . ' " class="next" />';
  1086. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  1087. } elseif(!is_array($user_data)) {
  1088. // If the user is not registered in the platform we do not show the form to update his information
  1089. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . ' " class="next" />';
  1090. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  1091. }
  1092. }
  1093. echo '</form>';
  1094. // Footer
  1095. Display :: display_footer();
  1096. /**
  1097. * Check whether this survey has ended. If so, display message and exit rhis script
  1098. */
  1099. function check_time_availability($surv_data) {
  1100. $start_date = mktime(0, 0, 0, substr($surv_data['start_date'], 5, 2), substr($surv_data['start_date'], 8, 2), substr($surv_data['start_date'], 0, 4));
  1101. $end_date = mktime(0, 0, 0, substr($surv_data['end_date'], 5, 2), substr($surv_data['end_date'], 8, 2), substr($surv_data['end_date'], 0, 4));
  1102. $cur_date = time();
  1103. if ($cur_date < $start_date) {
  1104. Display :: display_warning_message(get_lang('SurveyNotAvailableYet'), false);
  1105. Display :: display_footer();
  1106. exit;
  1107. }
  1108. if ($cur_date > $end_date) {
  1109. Display :: display_warning_message(get_lang('SurveyNotAvailableAnymore'), false);
  1110. Display :: display_footer();
  1111. exit;
  1112. }
  1113. }