fillsurvey.php 57 KB

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