fillsurvey.php 51 KB

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