fillsurvey.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  14. ==============================================================================
  15. */
  16. /**
  17. * @package dokeos.survey
  18. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  20. * @author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification and rewriting large parts of the code
  21. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  22. *
  23. * @todo use quickforms for the forms
  24. * @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.
  25. * alterantively we could not allow people from filling the survey twice.
  26. * @todo performance could be improved if not the survey_id was stored with the invitation but the survey_code
  27. */
  28. // name of the language file that needs to be included
  29. $language_file = 'survey';
  30. // unsetting the course id (because it is in the URL)
  31. if (!isset($_GET['cidReq']))
  32. {
  33. $cidReset = true;
  34. }
  35. else
  36. {
  37. $_cid = $_GET['cidReq'];
  38. }
  39. // including the global dokeos file
  40. require ('../inc/global.inc.php');
  41. // including additional libraries
  42. //require_once (api_get_path(LIBRARY_PATH)."survey.lib.php");
  43. require_once('survey.lib.php');
  44. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  45. // breadcrumbs
  46. if (!empty($_user))
  47. {
  48. $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  49. }
  50. // Header
  51. Display :: display_header(get_lang('Survey'));
  52. // getting all the course information
  53. $_course = CourseManager::get_course_information($_GET['course']);
  54. // Database table definitions
  55. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  56. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  57. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION, $_course['db_name']);
  58. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $_course['db_name']);
  59. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  60. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  61. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']);
  62. // first we check if the needed parameters are present
  63. if (!isset($_GET['course']) OR !isset($_GET['invitationcode']))
  64. {
  65. Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'), false);
  66. Display :: display_footer();
  67. exit;
  68. }
  69. // now we check if the invitationcode is valid
  70. $sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '".Database::escape_string($_GET['invitationcode'])."'";
  71. $result = api_sql_query($sql, __FILE__, __LINE__);
  72. if (mysql_num_rows($result) < 1)
  73. {
  74. Display :: display_error_message(get_lang('WrongInvitationCode'), false);
  75. Display :: display_footer();
  76. exit;
  77. }
  78. $survey_invitation = mysql_fetch_assoc($result);
  79. // now we check if the user already filled the survey
  80. if ($survey_invitation['answered'] == 1)
  81. {
  82. Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false);
  83. Display :: display_footer();
  84. exit;
  85. }
  86. // checking if there is another survey with this code.
  87. // If this is the case there will be a language choice
  88. $sql = "SELECT * FROM $table_survey WHERE code='".Database::escape_string($survey_invitation['survey_code'])."'";
  89. $result = api_sql_query($sql, __FILE__, __LINE__);
  90. if (Database::num_rows($result) > 1)
  91. {
  92. if ($_POST['language'])
  93. {
  94. $survey_invitation['survey_id'] = $_POST['language'];
  95. }
  96. else
  97. {
  98. echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&cidReq='.$_GET['cidReq'].'">';
  99. echo ' <select name="language">';
  100. while ($row=mysql_fetch_assoc($result))
  101. {
  102. echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  103. }
  104. echo '</select>';
  105. echo ' <input type="submit" name="Submit" value="'.get_lang('Ok').'" />';
  106. echo '</form>';
  107. display::display_footer();
  108. exit;
  109. }
  110. }
  111. else
  112. {
  113. $row=mysql_fetch_assoc($result);
  114. $survey_invitation['survey_id'] = $row['survey_id'];
  115. }
  116. // getting the survey information
  117. $survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
  118. $survey_data['survey_id'] = $survey_invitation['survey_id'];
  119. //print_r($survey_data);
  120. // storing the answers
  121. if ($_POST)
  122. {
  123. if ($survey_data['survey_type']=='0')
  124. {
  125. // getting all the types of the question (because of the special treatment of the score question type
  126. $sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'";
  127. $result = api_sql_query($sql, __FILE__, __LINE__);
  128. while ($row = mysql_fetch_assoc($result))
  129. {
  130. $types[$row['question_id']] = $row['type'];
  131. }
  132. // looping through all the post values
  133. foreach ($_POST as $key=>$value)
  134. {
  135. // if the post value key contains the string 'question' then it is an answer on a question
  136. if (strstr($key,'question'))
  137. {
  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. {
  145. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  146. foreach ($value as $answer_key => $answer_value)
  147. {
  148. if ($types[$survey_question_id] == 'score')
  149. {
  150. $option_id = $answer_key;
  151. $option_value = $answer_value;
  152. }
  153. else
  154. {
  155. $option_id = $answer_value;
  156. $option_value = '';
  157. }
  158. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $option_id, $option_value, $survey_data);
  159. }
  160. }
  161. // all the other question types (open question, multiple choice, percentage, ...)
  162. else
  163. {
  164. if ($types[$survey_question_id] == 'percentage')
  165. {
  166. $sql = "SELECT * FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
  167. $result = api_sql_query($sql, __FILE__, __LINE__);
  168. $row = mysql_fetch_assoc($result);
  169. $option_value = $row['option_text'];
  170. }
  171. else
  172. {
  173. $option_value = 0;
  174. if($types[$survey_question_id] == 'open')
  175. {
  176. $option_value = $value;
  177. //$value = 0;
  178. }
  179. }
  180. $survey_question_answer = $value;
  181. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  182. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  183. //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  184. }
  185. }
  186. }
  187. }
  188. else
  189. {
  190. // getting all the types of the question (because of the special treatment of the score question type
  191. $shuffle='';
  192. /*
  193. if ($survey_data['shuffle']=='1')
  194. {
  195. $shuffle= ' BY RAND() ' ;
  196. }
  197. */
  198. $sql = "SELECT * FROM $table_survey_question
  199. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  200. AND survey_group_pri='0' ORDER BY RAND()
  201. ";
  202. $result = api_sql_query($sql, __FILE__, __LINE__);
  203. while ($row = mysql_fetch_assoc($result))
  204. {
  205. $types[$row['question_id']] = $row['type'];
  206. }
  207. // looping through all the post values
  208. foreach ($_POST as $key=>$value)
  209. {
  210. // if the post value key contains the string 'question' then it is an answer on a question
  211. if (strstr($key,'question'))
  212. {
  213. // finding the question id by removing 'question'
  214. $survey_question_id = str_replace('question', '',$key);
  215. // we select the correct answer and the puntuacion
  216. $sql = "SELECT value FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
  217. $result = api_sql_query($sql, __FILE__, __LINE__);
  218. $row = mysql_fetch_assoc($result);
  219. $option_value = $row['value'];
  220. //$option_value = 0;
  221. $survey_question_answer = $value;
  222. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  223. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  224. //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  225. }
  226. }
  227. }
  228. }
  229. // displaying the survey title and subtitle (appears on every page)
  230. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  231. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  232. // checking time availability
  233. $start_date = mktime(0,0,0,substr($survey_data['start_date'],5,2),substr($survey_data['start_date'],8,2),substr($survey_data['start_date'],0,4));
  234. $end_date = mktime(0,0,0,substr($survey_data['end_date'],5,2),substr($survey_data['end_date'],8,2),substr($survey_data['end_date'],0,4));
  235. $cur_date = time();
  236. if($cur_date < $start_date)
  237. {
  238. Display :: display_warning_message(get_lang('SurveyNotAvailableYet'), false);
  239. Display :: display_footer();
  240. exit;
  241. }
  242. if($cur_date > $end_date)
  243. {
  244. Display :: display_warning_message(get_lang('SurveyNotAvailableAnymore'), false);
  245. Display :: display_footer();
  246. exit;
  247. }
  248. // displaying the survey introduction
  249. if (!isset($_GET['show']))
  250. {
  251. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  252. $limit = 0;
  253. }
  254. // displaying the survey thanks message
  255. if (isset($_POST['finish_survey']))
  256. {
  257. echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').'</strong> <br />'.$survey_data['survey_thanks'].'</div>';
  258. survey_manager::update_survey_answered($survey_data['survey_id'], $survey_invitation['user'], $survey_invitation['survey_code']);
  259. Display :: display_footer();
  260. exit;
  261. }
  262. $shuffle='';
  263. if ($survey_data['shuffle']==1)
  264. {
  265. $shuffle= ' BY RAND() ';
  266. }
  267. if ( isset($_GET['show']) || isset($_POST['personality']))
  268. {
  269. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  270. // as long as there is no pagebreak fount we keep adding questions to the page
  271. $questions_displayed = array();
  272. $counter = 0;
  273. // if is a normal survey
  274. if ($survey_data['survey_type']=='0')
  275. {
  276. $sql = "SELECT * FROM $table_survey_question
  277. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  278. ORDER BY sort ASC";
  279. $result = api_sql_query($sql, __FILE__, __LINE__);
  280. while ($row = mysql_fetch_assoc($result))
  281. {
  282. if($row['type'] == 'pagebreak')
  283. {
  284. $counter++;
  285. }
  286. else
  287. {
  288. // ids from question of the current survey
  289. $paged_questions[$counter][] = $row['question_id'];
  290. }
  291. }
  292. if (key_exists($_GET['show'],$paged_questions))
  293. {
  294. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  295. 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,
  296. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  297. FROM $table_survey_question survey_question
  298. LEFT JOIN $table_survey_question_option survey_question_option
  299. ON survey_question.question_id = survey_question_option.question_id
  300. WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  301. AND survey_question.question_id IN (".implode(',',$paged_questions[$_GET['show']]).")
  302. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  303. $result = api_sql_query($sql, __FILE__, __LINE__);
  304. $question_counter_max = mysql_num_rows($result);
  305. $counter = 0;
  306. $limit=0;
  307. $questions = array();
  308. while ($row = mysql_fetch_assoc($result))
  309. {
  310. // if the type is not a pagebreak we store it in the $questions array
  311. if($row['type'] <> 'pagebreak')
  312. {
  313. $questions[$row['sort']]['question_id'] = $row['question_id'];
  314. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  315. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  316. $questions[$row['sort']]['display'] = $row['display'];
  317. $questions[$row['sort']]['type'] = $row['type'];
  318. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  319. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  320. }
  321. // if the type is a pagebreak we are finished loading the questions for this page
  322. else
  323. {
  324. break;
  325. }
  326. $counter++;
  327. }
  328. }
  329. }
  330. else
  331. {
  332. $my_survey_id=Database::escape_string($survey_invitation['survey_id']);
  333. $current_user = Database::escape_string($survey_invitation['user']);
  334. if (isset($_POST['personality']))
  335. {
  336. // I have to calculate the results to get the 3 groups the most near to the personality of the user
  337. $paged_group_questions=array();
  338. if ($shuffle=='')
  339. $order = 'BY sort ASC ';
  340. else
  341. $order = $shuffle;
  342. $sql = "SELECT * FROM $table_survey_question
  343. WHERE survey_id = '".$my_survey_id."'
  344. AND survey_group_sec1='0' AND survey_group_sec2='0'
  345. ORDER $order ";
  346. $result = api_sql_query($sql, __FILE__, __LINE__);
  347. //echo "<br>";
  348. while ($row = mysql_fetch_assoc($result))
  349. {
  350. $paged_group_questions[] = $row['question_id'];
  351. $paged_group[] = $row['survey_group_pri'];
  352. }
  353. $answer_list=array();
  354. //echo "<br>"; print_r($paged_group_questions); print_r($paged_group);
  355. // current user results
  356. $results=array();
  357. $sql = "SELECT survey_group_pri, user, SUM(value) as value
  358. FROM $table_survey_answer as survey_answer INNER JOIN $table_survey_question as survey_question
  359. ON (survey_question.question_id = survey_answer.question_id)
  360. WHERE survey_answer.survey_id='".$my_survey_id."' AND
  361. survey_answer.user='".$current_user."'
  362. GROUP BY survey_group_pri
  363. ORDER BY survey_group_pri
  364. ";
  365. $result = api_sql_query($sql, __FILE__, __LINE__);
  366. while ($row = Database::fetch_array($result))
  367. {
  368. $answer_list['value']=$row['value'];
  369. $answer_list['group']=$row['survey_group_pri'];
  370. $results[]=$answer_list;
  371. }
  372. //echo "<br>";print_r($results); echo "<br>";
  373. // total calculations
  374. $totals=array();
  375. $sql = "SELECT SUM(temp.value) as value, temp.survey_group_pri FROM
  376. (SELECT MAX(value) as value, survey_group_pri, survey_question.question_id
  377. FROM $table_survey_question as survey_question
  378. INNER JOIN $table_survey_question_option as survey_question_option
  379. ON (survey_question.question_id = survey_question_option.question_id)
  380. WHERE survey_question.survey_id='".$my_survey_id."' AND survey_group_sec1='0' AND survey_group_sec2='0'
  381. GROUP BY survey_group_pri, survey_question.question_id) as temp
  382. GROUP BY temp.survey_group_pri
  383. ORDER BY temp.survey_group_pri";
  384. $result = api_sql_query($sql, __FILE__, __LINE__);
  385. while ($row = Database::fetch_array($result))
  386. {
  387. $list['value']=$row['value'];
  388. $list['group']=$row['survey_group_pri'];
  389. $totals[]=$list;
  390. }
  391. //print_r($totals);
  392. $final_results=array();
  393. for ($i=0; $i< count($totals);$i++)
  394. {
  395. for ($j=0; $j< count($results);$j++)
  396. {
  397. if ($totals[$i]['group']==$results[$j]['group'])
  398. {
  399. $group=$totals[$i]['group'];
  400. $porcen=($results[$j]['value'] / $totals[$i]['value'] );
  401. $final_results[$group]=$porcen;
  402. }
  403. }
  404. }
  405. // ordering
  406. arsort($final_results);
  407. $groups=array_keys($final_results);
  408. echo '<pre>';
  409. echo 'Group id => %';
  410. echo "<br>";
  411. print_r($final_results);
  412. $result=array();
  413. foreach ($final_results as $key =>$sub_result)
  414. {
  415. $result[]=array('group'=>$key, 'value'=>$sub_result);
  416. }
  417. $i=0;
  418. $group_cant=0;
  419. $equal_count=0;
  420. /*
  421. //i.e 70% - 70% -70% 70% $equal_count =3
  422. while(1)
  423. {
  424. if ($result[$i]['value'] == $result[$i+1]['value'])
  425. {
  426. $equal_count++;
  427. }
  428. else
  429. {
  430. break;
  431. }
  432. $i++;
  433. }
  434. echo 'eq'. $equal_count;
  435. echo "<br>";
  436. if ($equal_count==0)
  437. {
  438. //i.e 70% 70% -60% 60% $equal_count = 1 only we get the first 2 options
  439. if ( ($result[0]['value'] == $result[1]['value']) && ($result[2]['value']==$result[3]['value']) )
  440. {
  441. $group_cant=1;
  442. }
  443. else
  444. { // by default we chose the highest 3
  445. $group_cant=2;
  446. }
  447. }
  448. elseif ($equal_count==2)
  449. {
  450. $group_cant=2;
  451. }
  452. else
  453. {
  454. $group_cant=-1;
  455. }
  456. */
  457. //i.e 70% - 70% -70% 70% $equal_count =3
  458. while(1)
  459. {
  460. if ($result[$i]['value'] == $result[$i+1]['value'])
  461. {
  462. $equal_count++;
  463. }
  464. else
  465. {
  466. break;
  467. }
  468. $i++;
  469. }
  470. if ($equal_count<4)
  471. {
  472. if ($equal_count==0 || $equal_count==1 )
  473. {
  474. //i.e 70% - 70% -0% - 0% - $equal_count = 0 only we get the first 2 options
  475. if ( ($result[0]['value'] == $result[1]['value']) && ($result[2]['value']==0 ) )
  476. {
  477. $group_cant=0;
  478. }
  479. //i.e 70% - 70% -60% - 60% $equal_count = 0 only we get the first 2 options
  480. elseif ( ($result[0]['value'] == $result[1]['value']) && ($result[2]['value']==$result[3]['value']) )
  481. {
  482. $group_cant=0;
  483. }
  484. elseif ( ($result[1]['value'] == $result[2]['value']) && ($result[2]['value']==$result[3]['value']) )
  485. {
  486. $group_cant=-1;
  487. }
  488. else
  489. { // by default we chose the highest 3
  490. $group_cant=2;
  491. }
  492. }
  493. else
  494. {
  495. $group_cant=$equal_count;
  496. }
  497. // conditional_status
  498. // 0 no determinado
  499. // 1 determinado
  500. // 2 un solo valor
  501. // 3 valores iguales
  502. if ($group_cant>0)
  503. {
  504. //echo '$equal_count'.$group_cant;
  505. // we only get highest 3
  506. $secondary ='';
  507. $combi='';
  508. for ($i=0; $i<= $group_cant ;$i++)
  509. {
  510. $group1=$groups[$i];
  511. $group2=$groups[$i+1];
  512. // here we made all the posibilities with the 3 groups
  513. if ( $group_cant == 2 && $i==$group_cant)
  514. {
  515. $group2=$groups[0];
  516. $secondary .= " OR ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  517. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  518. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1."<br>";
  519. }
  520. else
  521. {
  522. if ($i!=0 )
  523. {
  524. $secondary .= " OR ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  525. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  526. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1."<br>";
  527. }
  528. else
  529. {
  530. $secondary .= " ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  531. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  532. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1."<br>";
  533. }
  534. }
  535. }
  536. echo "Pair of Groups <br><br>";
  537. echo $combi;
  538. // create the new select with the questions
  539. $sql = "SELECT * FROM $table_survey_question
  540. WHERE survey_id = '".$my_survey_id."'
  541. AND ($secondary )
  542. ORDER BY sort ASC";
  543. $result = api_sql_query($sql, __FILE__, __LINE__);
  544. $counter=0;
  545. while ($row = mysql_fetch_assoc($result))
  546. {
  547. if ($survey_data['one_question_per_page']==0)
  548. {
  549. $paged_questions[$counter][] = $row['question_id'];
  550. $counter++;
  551. }
  552. else
  553. if($row['type'] == 'pagebreak')
  554. {
  555. $counter++;
  556. }
  557. else
  558. {
  559. // ids from question of the current survey
  560. $paged_questions[$counter][] = $row['question_id'];
  561. }
  562. }
  563. if ($shuffle=='')
  564. $shuffle=' BY survey_question.sort, survey_question_option.sort ASC ';
  565. $val=0;
  566. if ($survey_data['one_question_per_page']==0)
  567. {
  568. $val=$_POST['personality'];
  569. }
  570. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  571. 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,
  572. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  573. FROM $table_survey_question survey_question
  574. LEFT JOIN $table_survey_question_option survey_question_option
  575. ON survey_question.question_id = survey_question_option.question_id
  576. WHERE survey_question.survey_id = '".$my_survey_id."'
  577. AND survey_question.question_id IN (".implode(',',$paged_questions[$val]).")
  578. ORDER $shuffle ";
  579. $result = api_sql_query($sql, __FILE__, __LINE__);
  580. $question_counter_max = mysql_num_rows($result);
  581. $counter = 0;
  582. $limit=0;
  583. $questions = array();
  584. while ($row = mysql_fetch_assoc($result))
  585. {
  586. // if the type is not a pagebreak we store it in the $questions array
  587. if($row['type'] <> 'pagebreak')
  588. {
  589. $questions[$row['sort']]['question_id'] = $row['question_id'];
  590. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  591. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  592. $questions[$row['sort']]['display'] = $row['display'];
  593. $questions[$row['sort']]['type'] = $row['type'];
  594. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  595. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  596. // personality params
  597. $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
  598. $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
  599. $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
  600. }
  601. // if the type is a pagebreak we are finished loading the questions for this page
  602. else
  603. {
  604. break;
  605. }
  606. $counter++;
  607. }
  608. }
  609. else
  610. {
  611. echo get_lang('SurveyUndetermined');
  612. }
  613. }
  614. else
  615. {
  616. echo get_lang('SurveyUndetermined');
  617. }
  618. }
  619. else
  620. {
  621. // only the questions from the basic group
  622. //the 50 questions A B C D E F G
  623. $order_sql= $shuffle;
  624. if ($shuffle=='')
  625. $order_sql=' BY question_id ';
  626. $sql = "SELECT * FROM $table_survey_question
  627. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  628. AND survey_group_sec1='0' AND survey_group_sec2='0'
  629. ORDER ".$order_sql." ";
  630. //echo "<br>";echo "<br>";
  631. $result = api_sql_query($sql, __FILE__, __LINE__);
  632. $counter=0;
  633. while ($row = mysql_fetch_assoc($result))
  634. {
  635. if ($survey_data['one_question_per_page']==0)
  636. {
  637. $paged_questions[$counter][] = $row['question_id'];
  638. $counter++;
  639. }
  640. else
  641. {
  642. if($row['type'] == 'pagebreak')
  643. {
  644. $counter++;
  645. }
  646. else
  647. {
  648. // ids from question of the current survey
  649. $paged_questions[$counter][] = $row['question_id'];
  650. }
  651. }
  652. }
  653. //if (key_exists($_GET['show'],$paged_questions))
  654. //{
  655. $order_sql= $shuffle;
  656. if ($shuffle=='')
  657. $order_sql=' BY survey_question.sort, survey_question_option.sort ASC ';
  658. $val=0;
  659. if ($survey_data['one_question_per_page']==0)
  660. {
  661. $val=$_GET['show'];
  662. }
  663. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  664. 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,
  665. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  666. FROM $table_survey_question survey_question
  667. LEFT JOIN $table_survey_question_option survey_question_option
  668. ON survey_question.question_id = survey_question_option.question_id
  669. WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  670. AND survey_question.question_id IN (".implode(',',$paged_questions[$val]).")
  671. ORDER $order_sql ";
  672. $result = api_sql_query($sql, __FILE__, __LINE__);
  673. $question_counter_max = mysql_num_rows($result);
  674. $counter = 0;
  675. $limit=0;
  676. $questions = array();
  677. while ($row = mysql_fetch_assoc($result))
  678. {
  679. // if the type is not a pagebreak we store it in the $questions array
  680. if($row['type'] <> 'pagebreak')
  681. {
  682. $questions[$row['sort']]['question_id'] = $row['question_id'];
  683. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  684. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  685. $questions[$row['sort']]['display'] = $row['display'];
  686. $questions[$row['sort']]['type'] = $row['type'];
  687. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  688. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  689. // personality params
  690. $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
  691. $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
  692. $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
  693. }
  694. // if the type is a pagebreak we are finished loading the questions for this page
  695. else
  696. {
  697. break;
  698. }
  699. $counter++;
  700. }
  701. }
  702. }
  703. }
  704. // selecting the maximum number of pages
  705. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_invitation['survey_id'])."'";
  706. $result = api_sql_query($sql, __FILE__, __LINE__);
  707. $numberofpages = Database::num_rows($result) + 1;
  708. // Displaying the form with the questions
  709. if (isset($_GET['show']))
  710. {
  711. $show = (int)$_GET['show'] + 1;
  712. }
  713. else
  714. {
  715. $show = 0;
  716. }
  717. // Displaying the form with the questions
  718. if (isset($_POST['personality']))
  719. {
  720. $personality = (int)$_POST['personality'] + 1;
  721. }
  722. else
  723. {
  724. $personality= 0;
  725. }
  726. // Displaying the form with the questions
  727. $g_c = (isset($_GET['course'])?Security::remove_XSS($_GET['course']):'');
  728. $g_ic = (isset($_GET['invitationcode'])?Security::remove_XSS($_GET['invitationcode']):'');
  729. $g_cr = (isset($_GET['cidReq'])?Security::remove_XSS($_GET['cidReq']):'');
  730. $p_l = (isset($_POST['language'])?Security::remove_XSS($_POST['language']):'');
  731. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?course='.$g_c.'&invitationcode='.$g_ic.'&show='.$show.'&cidReq='.$g_cr.'">';
  732. echo '<input type="hidden" name="language" value="'.$p_l.'" />';
  733. if(isset($questions) && is_array($questions))
  734. {
  735. foreach ($questions as $key=>$question)
  736. {
  737. $display = new $question['type'];
  738. $display->render_question($question);
  739. }
  740. }
  741. if ($survey_data['survey_type']==0)
  742. {
  743. if (($show < $numberofpages) || !$_GET['show'])
  744. {
  745. //echo '<a href="'.api_get_self().'?survey_id='.$survey_invitation['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
  746. echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  747. }
  748. if ($show >= $numberofpages && $_GET['show'])
  749. {
  750. echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  751. }
  752. }
  753. else
  754. {
  755. $numberofpages=count($paged_questions);
  756. //echo $show.' / '.$numberofpages;echo "<br />";
  757. if (($show < $numberofpages) || !$_GET['show'])
  758. {
  759. echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  760. if ($survey_data['one_question_per_page']==1 && $show!=0)
  761. {
  762. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  763. }
  764. }
  765. if ($show >= $numberofpages && $_GET['show'] )
  766. {
  767. if ($survey_data['one_question_per_page']==0)
  768. {
  769. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  770. }
  771. $numberofpages=count($paged_questions);
  772. echo $personality.' / '.$numberofpages;
  773. echo "<br />";
  774. if ($personality > $numberofpages -1 )
  775. {
  776. echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  777. }
  778. else
  779. {
  780. echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  781. }
  782. }
  783. }
  784. echo '</form>';
  785. // Footer
  786. Display :: display_footer();
  787. ?>