fillsurvey.php 58 KB

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