fillsurvey.php 61 KB

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