reporting.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  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. * @version $Id: reporting.php 13041 2007-09-17 09:52:01Z elixir_inter $
  21. *
  22. * @todo The question has to be more clearly indicated (same style as when filling the survey)
  23. */
  24. // name of the language file that needs to be included
  25. $language_file = 'survey';
  26. // including the global dokeos file
  27. require ('../inc/global.inc.php');
  28. // export
  29. /**
  30. * @todo use export_table_csv($data, $filename = 'export')
  31. */
  32. if ($_POST['export_report'])
  33. {
  34. $data = export_complete_report();
  35. $filename = 'fileexport.csv';
  36. header('Content-type: application/octet-stream');
  37. header('Content-Type: application/force-download');
  38. header('Content-length: '.$len);
  39. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
  40. {
  41. header('Content-Disposition: filename= '.$filename);
  42. }
  43. else
  44. {
  45. header('Content-Disposition: attachment; filename= '.$filename);
  46. }
  47. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  48. {
  49. header('Pragma: ');
  50. header('Cache-Control: ');
  51. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  52. }
  53. header('Content-Description: '.$filename);
  54. header('Content-transfer-encoding: binary');
  55. echo $data;
  56. exit;
  57. }
  58. // including additional libraries
  59. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  60. require_once('survey.lib.php');
  61. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  62. // Checking the parameters
  63. check_parameters();
  64. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  65. if (!api_is_allowed_to_edit())
  66. {
  67. Display :: display_header();
  68. Display :: display_error_message(get_lang('NotAllowed'), false);
  69. Display :: display_footer();
  70. exit;
  71. }
  72. // Database table definitions
  73. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  74. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  75. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  76. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  77. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  78. $user_info = Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
  79. // getting the survey information
  80. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  81. $urlname = substr(strip_tags($survey_data['title']), 0, 40);
  82. if (strlen(strip_tags($survey_data['title'])) > 40)
  83. {
  84. $urlname .= '...';
  85. }
  86. // breadcrumbs
  87. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  88. $interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$_GET['survey_id'], 'name' => $urlname);
  89. if (!$_GET['action'] OR $_GET['action'] == 'overview')
  90. {
  91. $tool_name = get_lang('Reporting');
  92. }
  93. else
  94. {
  95. $interbreadcrumb[] = array ("url" => "reporting.php?survey_id=".$_GET['survey_id'], "name" => get_lang('Reporting'));
  96. switch ($_GET['action'])
  97. {
  98. case 'questionreport':
  99. $tool_name = get_lang('DetailedReportByQuestion');
  100. break;
  101. case 'userreport':
  102. $tool_name = get_lang('DetailedReportByUser');
  103. break;
  104. case 'comparativereport':
  105. $tool_name = get_lang('ComparativeReport');
  106. break;
  107. case 'completereport':
  108. $tool_name = get_lang('CompleteReport');
  109. break;
  110. }
  111. }
  112. // Displaying the header
  113. Display::display_header($tool_name);
  114. // Action handling
  115. handle_reporting_actions();
  116. if (!$_GET['action'] OR $_GET['action'] == 'overview')
  117. {
  118. echo '<b><a href="reporting.php?action=questionreport&amp;survey_id='.$_GET['survey_id'].'">'.get_lang('DetailedReportByQuestion').'</a></b> <br />'.get_lang('DetailedReportByQuestionDetail').' <br /><br />';
  119. echo '<b><a href="reporting.php?action=userreport&amp;survey_id='.$_GET['survey_id'].'">'.get_lang('DetailedReportByUser').'</a></b><br />'.get_lang('DetailedReportByUserDetail').'.<br /><br />';
  120. echo '<b><a href="reporting.php?action=comparativereport&amp;survey_id='.$_GET['survey_id'].'">'.get_lang('ComparativeReport').'</a></b><br />'.get_lang('ComparativeReportDetail').'.<br /><br />';
  121. echo '<b><a href="reporting.php?action=completereport&amp;survey_id='.$_GET['survey_id'].'">'.get_lang('CompleteReport').'</a></b><br />'.get_lang('CompleteReportDetail').'<br /><br />';
  122. }
  123. // Footer
  124. Display :: display_footer();
  125. /**
  126. * This function checks the parameters that are used in this page
  127. *
  128. * @return the header, an error and the footer if any parameter fails, else it returns true
  129. *
  130. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  131. * @version February 2007
  132. */
  133. function check_parameters()
  134. {
  135. $error = false;
  136. // getting the survey data
  137. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  138. // $_GET['survey_id'] has to be numeric
  139. if (!is_numeric($_GET['survey_id']))
  140. {
  141. $error = get_lang('IllegalSurveyId');
  142. }
  143. // $_GET['action']
  144. $allowed_actions = array('overview', 'questionreport', 'userreport', 'comparativereport', 'completereport');
  145. if (isset($_GET['action']) AND !in_array($_GET['action'], $allowed_actions))
  146. {
  147. $error = get_lang('ActionNotAllowed');
  148. }
  149. // user report
  150. if ($_GET['action'] == 'userreport')
  151. {
  152. global $people_filled;
  153. if ($survey_data['anonymous'] == 0)
  154. {
  155. $people_filled_full_data = true;
  156. }
  157. else
  158. {
  159. $people_filled_full_data = false;
  160. }
  161. $people_filled = survey_manager::get_people_who_filled_survey($_GET['survey_id'], $people_filled_full_data);
  162. if ($survey_data['anonymous'] == 0)
  163. {
  164. foreach ($people_filled as $key=>$value)
  165. {
  166. $people_filled_userids[]=$value['user_id'];
  167. }
  168. }
  169. else
  170. {
  171. $people_filled_userids = $people_filled;
  172. }
  173. if (isset($_GET['user']) AND !in_array($_GET['user'], $people_filled_userids))
  174. {
  175. $error = get_lang('UnknowUser');
  176. }
  177. }
  178. // question report
  179. if ($_GET['action'] == 'questionreport')
  180. {
  181. if (isset($_GET['question'])AND !is_numeric($_GET['question']))
  182. {
  183. $error = get_lang('UnknowQuestion');
  184. }
  185. }
  186. if ($error)
  187. {
  188. Display::display_header();
  189. Display::display_error_message(get_lang('Error').': '.$error, false);
  190. Display::display_footer();
  191. exit;
  192. }
  193. else
  194. {
  195. return true;
  196. }
  197. }
  198. /**
  199. * This function deals with the action handling
  200. *
  201. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  202. * @version February 2007
  203. */
  204. function handle_reporting_actions()
  205. {
  206. // getting the number of question
  207. $temp_questions_data = survey_manager::get_questions($_GET['survey_id']);
  208. // sorting like they should be displayed and removing the non-answer question types (comment and pagebreak)
  209. foreach ($temp_questions_data as $key=>$value)
  210. {
  211. if ($value['type'] <> 'comment' AND $value['type']<>'pagebreak')
  212. {
  213. $questions_data[$value['sort']]=$value;
  214. }
  215. }
  216. // counting the number of questions that are relevant for the reporting
  217. $survey_data['number_of_questions'] = count($questions_data);
  218. if ($_GET['action'] == 'questionreport')
  219. {
  220. display_question_report($survey_data);
  221. }
  222. if ($_GET['action'] == 'userreport')
  223. {
  224. display_user_report();
  225. }
  226. if ($_GET['action'] == 'comparativereport')
  227. {
  228. display_comparative_report();
  229. }
  230. if ($_GET['action'] == 'completereport')
  231. {
  232. display_complete_report();
  233. }
  234. }
  235. /**
  236. * This function displays the user report which is basically nothing more than a one-page display of all the questions
  237. * of the survey that is filled with the answers of the person who filled the survey.
  238. *
  239. * @return html code of the one-page survey with the answers of the selected user
  240. *
  241. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  242. * @version February 2007
  243. */
  244. function display_user_report()
  245. {
  246. global $people_filled, $survey_data;
  247. // Database table definitions
  248. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  249. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  250. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  251. // step 1: selection of the user
  252. echo "<script language=\"JavaScript\" type=\"text/JavaScript\">
  253. <!--
  254. function jumpMenu(targ,selObj,restore)
  255. {
  256. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  257. if (restore) selObj.selectedIndex=0;
  258. }
  259. //-->
  260. </script>
  261. ";
  262. echo get_lang('SelectUserWhoFilledSurvey').'<br />';
  263. echo '<select name="user" onchange="jumpMenu(\'parent\',this,0)">';
  264. echo '<option value="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'">'.get_lang('SelectUser').'</option>';
  265. foreach ($people_filled as $key=>$person)
  266. {
  267. if ($survey_data['anonymous'] == 0)
  268. {
  269. $name = $person['firstname'].' '.$person['lastname'];
  270. $id = $person['user_id'];
  271. }
  272. else
  273. {
  274. $name = $person;
  275. $id = $person;
  276. }
  277. echo '<option value="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'&amp;user='.$id.'" ';
  278. if ($_GET['user'] == $person)
  279. {
  280. echo 'selected="selected"';
  281. }
  282. echo '>'.$name.'</option>';
  283. }
  284. echo '</select>';
  285. // step 2: displaying the survey and the answer of the selected users
  286. if (isset($_GET['user']))
  287. {
  288. Display::display_normal_message(get_lang('AllQuestionsOnOnePage'), false);
  289. // getting all the questions and options
  290. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.max_value, survey_question.sort, survey_question.type,
  291. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  292. FROM $table_survey_question survey_question
  293. LEFT JOIN $table_survey_question_option survey_question_option
  294. ON survey_question.question_id = survey_question_option.question_id
  295. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  296. ORDER BY survey_question.sort ASC";
  297. $result = api_sql_query($sql, __FILE__, __LINE__);
  298. while ($row = mysql_fetch_assoc($result))
  299. {
  300. if($row['type'] <> 'pagebreak')
  301. {
  302. $questions[$row['sort']]['question_id'] = $row['question_id'];
  303. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  304. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  305. $questions[$row['sort']]['display'] = $row['display'];
  306. $questions[$row['sort']]['type'] = $row['type'];
  307. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  308. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  309. }
  310. }
  311. // getting all the answers of the user
  312. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."' AND user = '".Database::escape_string($_GET['user'])."'";
  313. $result = api_sql_query($sql, __FILE__, __LINE__);
  314. while ($row = mysql_fetch_assoc($result))
  315. {
  316. $answers[$row['question_id']][] = $row['option_id'];
  317. $all_answers[$row['question_id']][] = $row;
  318. }
  319. /*
  320. echo '<pre>';
  321. print_r($all_answers);
  322. echo '</pre>';
  323. */
  324. // displaying all the questions
  325. foreach ($questions as $key=>$question)
  326. {
  327. // if the question type is a scoring then we have to format the answers differently
  328. if ($question['type'] == 'score')
  329. {
  330. foreach($all_answers[$question['question_id']] as $key=>$answer_array)
  331. {
  332. $second_parameter[$answer_array['option_id']] = $answer_array['value'];
  333. }
  334. }
  335. else
  336. {
  337. $second_parameter = $answers[$question['question_id']];
  338. if ($question['type'] == 'open')
  339. {
  340. $second_parameter = array();
  341. $second_parameter[] = $all_answers[$question['question_id']][0]['option_id'];
  342. }
  343. }
  344. $display = new $question['type'];
  345. $display->render_question($question, $second_parameter);
  346. // echo '<pre>';
  347. // print_r($answers[$question['question_id']]);
  348. // echo '</pre>';
  349. }
  350. }
  351. }
  352. /**
  353. * This function displays the report by question.
  354. * It displays a table with all the options of the question and the number of users who have answered positively on the option.
  355. * The number of users who answered positive on a given option is expressed in an absolute number, in a percentage of the total
  356. * and graphically using bars
  357. * By clicking on the absolute number you get a list with the persons who have answered this.
  358. * You can then click on the name of the person and you will then go to the report by user where you see all the
  359. * answers of that user.
  360. *
  361. * @param array $survey_data all the data of the survey
  362. *
  363. * @return html code that displays the report by question
  364. *
  365. * @todo allow switching between horizontal and vertical.
  366. * @todo multiple response: percentage are probably not OK
  367. * @todo the question and option text have to be shortened and should expand when the user clicks on it.
  368. * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before
  369. *
  370. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  371. * @version February 2007
  372. */
  373. function display_question_report($survey_data)
  374. {
  375. // Database table definitions
  376. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  377. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  378. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  379. // determining the offset of the sql statement (the n-th question of the survey)
  380. if (!isset($_GET['question']))
  381. {
  382. $offset = 0;
  383. }
  384. else
  385. {
  386. $offset = $_GET['question'];
  387. }
  388. echo '<div id="question_report_questionnumbers">';
  389. for($i=1; $i<=($survey_data['number_of_questions']); $i++ )
  390. {
  391. if ($offset <> $i-1)
  392. {
  393. echo '<a href="reporting.php?action=questionreport&amp;survey_id='.(int)$_GET['survey_id'].'&amp;question='.($i-1).'">'.$i.'</a>';
  394. }
  395. else
  396. {
  397. echo $i;
  398. }
  399. if ($i < $survey_data['number_of_questions'])
  400. {
  401. echo ' | ';
  402. }
  403. }
  404. echo '</div>';
  405. // getting the question information
  406. $sql = "SELECT * FROM $table_survey_question WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' AND type<>'pagebreak' AND type<>'comment' ORDER BY sort ASC LIMIT ".$offset.",1";
  407. $result = api_sql_query($sql, __FILE__, __LINE__);
  408. $question = mysql_fetch_assoc($result);
  409. // navigate through the questions (next and previous)
  410. if ($_GET['question'] <> 0)
  411. {
  412. echo '<a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.($offset-1).'"> &lt;&lt; '.get_lang('PreviousQuestion').'</a> ';
  413. }
  414. else
  415. {
  416. echo '&lt;&lt;'.get_lang('PreviousQuestion').' ';
  417. }
  418. echo ' | ';
  419. if ($_GET['question'] < ($survey_data['number_of_questions']-1))
  420. {
  421. echo '<a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.($offset+1).'">'.get_lang('NextQuestion').'&gt;&gt; </a>';
  422. }
  423. else
  424. {
  425. echo get_lang('NextQuestion'). '&gt;&gt;';
  426. }
  427. echo '<br />';
  428. echo $question['survey_question'];
  429. echo '<br />';
  430. if ($question['type'] == 'score')
  431. {
  432. /** @todo this function should return the options as this is needed further in the code */
  433. $options = display_question_report_score($survey_data, $question, $offset);
  434. }
  435. elseif ($question['type'] == 'open')
  436. {
  437. /** @todo also get the user who has answered this */
  438. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  439. AND question_id = '".Database::escape_string($question['question_id'])."'";
  440. $result = api_sql_query($sql, __FILE__, __LINE__);
  441. while ($row = mysql_fetch_assoc($result))
  442. {
  443. echo $row['option_id'].'<hr noshade="noshade" size="1" />';
  444. }
  445. }
  446. else
  447. {
  448. // getting the options
  449. $sql = "SELECT * FROM $table_survey_question_option
  450. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  451. AND question_id = '".Database::escape_string($question['question_id'])."'
  452. ORDER BY sort ASC";
  453. $result = api_sql_query($sql, __FILE__, __LINE__);
  454. while ($row = mysql_fetch_assoc($result))
  455. {
  456. $options[$row['question_option_id']] = $row;
  457. }
  458. //echo '<pre>';
  459. //print_r($options);
  460. //echo '<pre>';
  461. // getting the answers
  462. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  463. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  464. AND question_id = '".Database::escape_string($question['question_id'])."'
  465. GROUP BY option_id, value";
  466. $result = api_sql_query($sql, __FILE__, __LINE__);
  467. while ($row = mysql_fetch_assoc($result))
  468. {
  469. $number_of_answers += $row['total'];
  470. $data[$row['option_id']] = $row;
  471. }
  472. //echo '<pre>';
  473. //print_r($data);
  474. //echo '<pre>';
  475. // displaying the table: headers
  476. echo '<table>';
  477. echo ' <tr>';
  478. echo ' <th>&nbsp;</th>';
  479. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  480. echo ' <th>'.get_lang('Percentage').'</th>';
  481. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  482. echo ' <tr>';
  483. // displaying the table: the content
  484. foreach ($options as $key=>$value)
  485. {
  486. $absolute_number = $data[$value['question_option_id']]['total'];
  487. echo ' <tr>';
  488. echo ' <td>'.$value['option_text'].'</td>';
  489. echo ' <td><a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.$offset.'&amp;viewoption='.$value['question_option_id'].'">'.$absolute_number.'</a></td>';
  490. echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
  491. echo ' <td>';
  492. $size = $absolute_number/$number_of_answers*100*2;
  493. if ($size > 0)
  494. {
  495. echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  496. }
  497. echo ' </td>';
  498. echo ' </tr>';
  499. }
  500. // displaying the table: footer (totals)
  501. echo ' <tr>';
  502. echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
  503. echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
  504. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  505. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  506. echo ' </tr>';
  507. echo '</table>';
  508. }
  509. if (isset($_GET['viewoption']))
  510. {
  511. echo get_lang('PeopleWhoAnswered').': '.$options[$_GET['viewoption']]['option_text'].'<br />';
  512. if (is_numeric($_GET['value']))
  513. {
  514. $sql_restriction = "AND value='".Database::escape_string($_GET['value'])."'";
  515. }
  516. $sql = "SELECT user FROM $table_survey_answer WHERE option_id = '".Database::escape_string($_GET['viewoption'])."' $sql_restriction";
  517. $result = api_sql_query($sql, __FILE__, __LINE__);
  518. while ($row = mysql_fetch_assoc($result))
  519. {
  520. echo '<a href="reporting.php?action=userreport&survey_id='.$_GET['survey_id'].'&user='.$row['user'].'">'.$row['user'].'</a><br />';
  521. }
  522. }
  523. }
  524. function display_question_report_score($survey_data, $question, $offset)
  525. {
  526. // Database table definitions
  527. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  528. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  529. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  530. // getting the options
  531. $sql = "SELECT * FROM $table_survey_question_option
  532. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  533. AND question_id = '".Database::escape_string($question['question_id'])."'
  534. ORDER BY sort ASC";
  535. $result = api_sql_query($sql, __FILE__, __LINE__);
  536. while ($row = mysql_fetch_assoc($result))
  537. {
  538. $options[$row['question_option_id']] = $row;
  539. }
  540. // getting the answers
  541. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  542. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  543. AND question_id = '".Database::escape_string($question['question_id'])."'
  544. GROUP BY option_id, value";
  545. $result = api_sql_query($sql, __FILE__, __LINE__);
  546. while ($row = mysql_fetch_assoc($result))
  547. {
  548. $number_of_answers += $row['total'];
  549. $data[$row['option_id']][$row['value']] = $row;
  550. }
  551. /*
  552. echo '<pre>';
  553. print_r($data);
  554. echo '</pre>';
  555. */
  556. // displaying the table: headers
  557. echo '<table>';
  558. echo ' <tr>';
  559. echo ' <th>&nbsp;</th>';
  560. echo ' <th>'.get_lang('Score').'</th>';
  561. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  562. echo ' <th>'.get_lang('Percentage').'</th>';
  563. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  564. echo ' <tr>';
  565. // displaying the table: the content
  566. foreach ($options as $key=>$value)
  567. {
  568. for ($i=1; $i<=$question['max_value']; $i++)
  569. {
  570. $absolute_number = $data[$value['question_option_id']][$i]['total'];
  571. echo ' <tr>';
  572. echo ' <td>'.$value['option_text'].'</td>';
  573. echo ' <td>'.$i.'</td>';
  574. echo ' <td><a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.$offset.'&amp;viewoption='.$value['question_option_id'].'&amp;value='.$i.'">'.$absolute_number.'</a></td>';
  575. echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
  576. echo ' <td>';
  577. $size = ($absolute_number/$number_of_answers*100*2);
  578. if ($size > 0)
  579. {
  580. echo ' <div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  581. }
  582. echo ' </td>';
  583. echo ' </tr>';
  584. }
  585. }
  586. // displaying the table: footer (totals)
  587. echo ' <tr>';
  588. echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
  589. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  590. echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
  591. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  592. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  593. echo ' </tr>';
  594. echo '</table>';
  595. }
  596. /**
  597. * This functions displays the complete reporting
  598. *
  599. * @return html code
  600. *
  601. * @todo open questions are not in the complete report yet.
  602. *
  603. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  604. * @version February 2007
  605. */
  606. function display_complete_report()
  607. {
  608. // Database table definitions
  609. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  610. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  611. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  612. // the form
  613. echo '<form id="form1" name="form1" method="post" action="'.api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'">';
  614. // the export button
  615. echo '<input type="submit" name="export_report" value="'.get_lang('ExportCurrentReport').'" />';
  616. // the table
  617. echo '<table class="data_table" border="1">';
  618. // getting the number of options per question
  619. echo ' <tr>';
  620. echo ' <th>';
  621. if ($_POST['submit_question_filter'] OR $_POST['export_report'])
  622. {
  623. echo ' <input type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'" />';
  624. }
  625. echo ' <input type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'" />';
  626. echo '</th>';
  627. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  628. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  629. ON questions.question_id = options.question_id
  630. /*WHERE questions.question_id = options.question_id*/
  631. AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  632. GROUP BY questions.question_id";
  633. $result = api_sql_query($sql, __FILE__, __LINE__);
  634. while ($row = mysql_fetch_assoc($result))
  635. {
  636. // we show the questions if
  637. // 1. there is no question filter and the export button has not been clicked
  638. // 2. there is a quesiton filter but the question is selected for display
  639. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  640. {
  641. // we do not show comment and pagebreak question types
  642. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  643. {
  644. echo ' <th';
  645. if ($row['number_of_options'] >0)
  646. {
  647. echo ' colspan="'.$row['number_of_options'].'"';
  648. }
  649. echo '>';
  650. echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> ';
  651. echo $row['survey_question'];
  652. echo '</label>';
  653. echo '</th>';
  654. }
  655. }
  656. $questions[$row['question_id']] = $row;
  657. }
  658. echo ' </tr>';
  659. // getting all the questions and options
  660. echo ' <tr>';
  661. echo ' <th>&nbsp;</th>'; // the user column
  662. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  663. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  664. FROM $table_survey_question survey_question
  665. LEFT JOIN $table_survey_question_option survey_question_option
  666. ON survey_question.question_id = survey_question_option.question_id
  667. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  668. ORDER BY survey_question.sort ASC";
  669. $result = api_sql_query($sql, __FILE__, __LINE__);
  670. while ($row = mysql_fetch_assoc($result))
  671. {
  672. // we show the options if
  673. // 1. there is no question filter and the export button has not been clicked
  674. // 2. there is a quesiton filter but the question is selected for display
  675. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  676. {
  677. // we do not show comment and pagebreak question types
  678. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  679. {
  680. echo ' <th>';
  681. echo $row['option_text'];
  682. echo '</th>';
  683. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  684. }
  685. }
  686. }
  687. echo ' </tr>';
  688. // getting all the answers of the users
  689. $old_user='';
  690. $answers_of_user = array();
  691. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  692. $result = api_sql_query($sql, __FILE__, __LINE__);
  693. while ($row = mysql_fetch_assoc($result))
  694. {
  695. if ($old_user <> $row['user'] AND $old_user<>'')
  696. {
  697. display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions);
  698. $answers_of_user=array();
  699. }
  700. if ($questions[$row['question_id']]['type']<> 'open')
  701. {
  702. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  703. }
  704. else
  705. {
  706. $answers_of_user[$row['question_id']][0] = $row;
  707. }
  708. $old_user = $row['user'];
  709. }
  710. display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions); // this is to display the last user
  711. echo '</table>';
  712. echo '</form>';
  713. }
  714. /**
  715. * This function displays a row (= a user and his/her answers) in the table of the complete report.
  716. *
  717. * @param array $possible_answers all the possible options
  718. * @param array $answers_of_user the answers of the user
  719. * @param string $user the user
  720. *
  721. * @todo rename $possible_answers to $possible_options ?
  722. *
  723. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  724. * @version February 2007
  725. */
  726. function display_complete_report_row($possible_answers, $answers_of_user, $user, $questions)
  727. {
  728. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  729. echo '<tr>';
  730. if(intval($user)!==0)
  731. {
  732. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  733. $rs = api_sql_query($sql, __FILE__, __LINE__);
  734. if($row = mysql_fetch_array($rs, MYSQL_ASSOC))
  735. {
  736. $user_displayed = $row['lastname'].' '.$row['firstname'];
  737. }
  738. else
  739. {
  740. $user_displayed = '-';
  741. }
  742. echo ' <th><a href="'.api_get_self().'?action=userreport&survey_id='.$_GET['survey_id'].'&user='.$user.'">'.$user_displayed.'</a></th>'; // the user column
  743. }
  744. else
  745. {
  746. echo ' <th>'.$user.'</th>'; // the user column
  747. }
  748. foreach ($possible_answers as $question_id=>$possible_option)
  749. {
  750. if ($questions[$question_id]['type'] == 'open')
  751. {
  752. echo '<td align="center">';
  753. echo $answers_of_user[$question_id]['0']['option_id'];
  754. echo '</td>';
  755. }
  756. else
  757. {
  758. foreach ($possible_option as $option_id=>$value)
  759. {
  760. echo '<td align="center">';
  761. if (!empty($answers_of_user[$question_id][$option_id]))
  762. {
  763. if ($answers_of_user[$question_id][$option_id]['value']<>0)
  764. {
  765. echo $answers_of_user[$question_id][$option_id]['value'];
  766. }
  767. else
  768. {
  769. echo 'v';
  770. }
  771. }
  772. }
  773. }
  774. }
  775. echo '</tr>';
  776. }
  777. /**
  778. * the function is quite similar to display_complete_report and return a html string that can be used in a csv file
  779. *
  780. * @todo consider merging this function with display_complete_report
  781. *
  782. * @return string $return the content of a csv file
  783. *
  784. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  785. * @version February 2007
  786. */
  787. function export_complete_report()
  788. {
  789. // Database table definitions
  790. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  791. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  792. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  793. // the first column
  794. $return = ';';
  795. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  796. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  797. ON questions.question_id = options.question_id
  798. /*WHERE questions.question_id = options.question_id*/
  799. AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  800. GROUP BY questions.question_id";
  801. $result = api_sql_query($sql, __FILE__, __LINE__);
  802. while ($row = mysql_fetch_assoc($result))
  803. {
  804. // we show the questions if
  805. // 1. there is no question filter and the export button has not been clicked
  806. // 2. there is a quesiton filter but the question is selected for display
  807. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  808. {
  809. // we do not show comment and pagebreak question types
  810. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  811. {
  812. for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
  813. {
  814. $return .= html_entity_decode(strip_tags($row['survey_question'])).';';
  815. }
  816. }
  817. }
  818. }
  819. $return .= "\n";
  820. // getting all the questions and options
  821. $return .= ';';
  822. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  823. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  824. FROM $table_survey_question survey_question
  825. LEFT JOIN $table_survey_question_option survey_question_option
  826. ON survey_question.question_id = survey_question_option.question_id
  827. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  828. ORDER BY survey_question.sort ASC";
  829. $result = api_sql_query($sql, __FILE__, __LINE__);
  830. while ($row = mysql_fetch_assoc($result))
  831. {
  832. // we show the options if
  833. // 1. there is no question filter and the export button has not been clicked
  834. // 2. there is a quesiton filter but the question is selected for display
  835. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  836. {
  837. // we do not show comment and pagebreak question types
  838. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  839. {
  840. $return .= $row['option_text'].';';
  841. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  842. }
  843. }
  844. }
  845. $return .= "\n";
  846. // getting all the answers of the users
  847. $old_user='';
  848. $answers_of_user = array();
  849. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  850. $result = api_sql_query($sql, __FILE__, __LINE__);
  851. while ($row = mysql_fetch_assoc($result))
  852. {
  853. if ($old_user <> $row['user'] AND $old_user<>'')
  854. {
  855. $return .= export_complete_report_row($possible_answers, $answers_of_user, $old_user);
  856. $answers_of_user=array();
  857. }
  858. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  859. $old_user = $row['user'];
  860. }
  861. $return .= export_complete_report_row($possible_answers, $answers_of_user, $old_user); // this is to display the last user
  862. return $return;
  863. }
  864. /**
  865. * add a line to the csv file
  866. *
  867. * @param array $possible_answers all the possible answers
  868. * @param array $answers_of_user the answers of the user
  869. * @param string $user the user
  870. *
  871. * @return string $return line of the csv file
  872. *
  873. * @todo rename $possible_answers to $possible_options ?
  874. *
  875. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  876. * @version February 2007
  877. */
  878. function export_complete_report_row($possible_answers, $answers_of_user, $user)
  879. {
  880. $return = $user.';'; // the user column
  881. foreach ($possible_answers as $question_id=>$possible_option)
  882. {
  883. foreach ($possible_option as $option_id=>$value)
  884. {
  885. if (!empty($answers_of_user[$question_id][$option_id]))
  886. {
  887. $return .= 'v';
  888. }
  889. $return .= ';';
  890. }
  891. }
  892. $return .= "\n";
  893. return $return;
  894. }
  895. /**
  896. * This function displays the comparative report which allows you to compare two questions
  897. * A comparative report creates a table where one question is on the x axis and a second question is on the y axis.
  898. * In the intersection is the number of people who have answerd positive on both options.
  899. *
  900. * @return html code
  901. *
  902. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  903. * @version February 2007
  904. */
  905. function display_comparative_report()
  906. {
  907. // allowed question types for comparative report
  908. $allowed_question_types = array('yesno', 'multiplechoice', 'multipleresponse', 'dropdown', 'percentage', 'score');
  909. // getting all the questions
  910. $questions = survey_manager::get_questions($_GET['survey_id']);
  911. // displaying an information message that only the questions with predefined answers can be used in a comparative report
  912. Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false);
  913. // The form for selecting the axis of the table
  914. echo '<form id="form1" name="form1" method="get" action="'.api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'&xaxis='.$_GET['xaxis'].'&y='.$_GET['yaxis'].'">';
  915. // survey_id
  916. echo '<input type="hidden" name="action" value="'.$_GET['action'].'"/>';
  917. echo '<input type="hidden" name="survey_id" value="'.(int)$_GET['survey_id'].'"/>';
  918. // X axis
  919. echo get_lang('SelectXAxis').': ';
  920. echo '<select name="xaxis">';
  921. echo '<option value="">---</option>';
  922. foreach ($questions as $key=>$question)
  923. {
  924. if (in_array($question['type'], $allowed_question_types))
  925. {
  926. echo '<option value="'.$question['question_id'].'"';
  927. if ($_GET['xaxis'] == $question['question_id'])
  928. {
  929. echo ' selected="selected"';
  930. }
  931. echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
  932. }
  933. }
  934. echo '</select><br /><br />';
  935. // Y axis
  936. echo get_lang('SelectYAxis').': ';
  937. echo '<select name="yaxis">';
  938. echo '<option value="">---</option>';
  939. foreach ($questions as $key=>$question)
  940. {
  941. if (in_array($question['type'], $allowed_question_types))
  942. {
  943. echo '<option value="'.$question['question_id'].'"';
  944. if ($_GET['yaxis'] == $question['question_id'])
  945. {
  946. echo ' selected="selected"';
  947. }
  948. echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
  949. }
  950. }
  951. echo '</select><br /><br />';
  952. echo '<input type="submit" name="Submit" value="Submit" />';
  953. echo '</form>';
  954. // getting all the information of the x axis
  955. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']))
  956. {
  957. $question_x = survey_manager::get_question($_GET['xaxis']);
  958. }
  959. // getting all the information of the y axis
  960. if (isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  961. {
  962. $question_y = survey_manager::get_question($_GET['yaxis']);
  963. }
  964. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']) AND isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  965. {
  966. // getting the answers of the two questions
  967. $answers_x = get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
  968. $answers_y = get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
  969. // displaying the table
  970. echo '<table border="1" class="data_table">';
  971. // the header
  972. echo ' <tr>';
  973. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  974. {
  975. if ($ii == 0)
  976. {
  977. echo ' <th>&nbsp;</th>';
  978. }
  979. else
  980. {
  981. if ($question_x['type']=='score')
  982. {
  983. for($x=1; $x<=$question_x['maximum_score']; $x++)
  984. {
  985. echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
  986. }
  987. $x='';
  988. }
  989. else
  990. {
  991. echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
  992. }
  993. }
  994. }
  995. echo ' </tr>';
  996. // the main part
  997. for ($ij=0; $ij<count($question_y['answers']); $ij++)
  998. {
  999. // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
  1000. if ($question_y['type'] == 'score')
  1001. {
  1002. for($y=1; $y<=$question_y['maximum_score']; $y++)
  1003. {
  1004. echo ' <tr>';
  1005. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  1006. {
  1007. if ($question_x['type']=='score')
  1008. {
  1009. for($x=1; $x<=$question_x['maximum_score']; $x++)
  1010. {
  1011. if ($ii == 0)
  1012. {
  1013. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  1014. break;
  1015. }
  1016. else
  1017. {
  1018. echo ' <td align="center">';
  1019. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
  1020. echo '</td>';
  1021. }
  1022. }
  1023. }
  1024. else
  1025. {
  1026. if ($ii == 0)
  1027. {
  1028. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  1029. }
  1030. else
  1031. {
  1032. echo ' <td align="center">';
  1033. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
  1034. echo '</td>';
  1035. }
  1036. }
  1037. }
  1038. echo ' </tr>';
  1039. }
  1040. }
  1041. // The Y axis is NOT a score question type so the number of rows = the number of options
  1042. else
  1043. {
  1044. echo ' <tr>';
  1045. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  1046. {
  1047. if ($question_x['type']=='score')
  1048. {
  1049. for($x=1; $x<=$question_x['maximum_score']; $x++)
  1050. {
  1051. if ($ii == 0)
  1052. {
  1053. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  1054. break;
  1055. }
  1056. else
  1057. {
  1058. echo ' <td align="center">';
  1059. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
  1060. echo '</td>';
  1061. }
  1062. }
  1063. }
  1064. else
  1065. {
  1066. if ($ii == 0)
  1067. {
  1068. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  1069. }
  1070. else
  1071. {
  1072. echo ' <td align="center">';
  1073. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
  1074. echo '</td>';
  1075. }
  1076. }
  1077. }
  1078. echo ' </tr>';
  1079. }
  1080. }
  1081. echo '</table>';
  1082. }
  1083. }
  1084. /**
  1085. * get all the answers of a question grouped by user
  1086. *
  1087. * @param integer $survey_id the id of the survey
  1088. * @param integer $question_id the id of the question
  1089. * @return array $return an array countaining all the answers of all the users grouped by user
  1090. *
  1091. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1092. * @version February 2007
  1093. */
  1094. function get_answers_of_question_by_user($survey_id, $question_id)
  1095. {
  1096. // Database table definitions
  1097. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1098. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1099. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1100. $sql = "SELECT * FROM $table_survey_answer
  1101. WHERE survey_id='".Database::escape_string($survey_id)."'
  1102. AND question_id='".Database::escape_string($question_id)."'
  1103. ORDER BY USER ASC";
  1104. $result = api_sql_query($sql, __FILE__, __LINE__);
  1105. while ($row = mysql_fetch_assoc($result))
  1106. {
  1107. if ($row['value'] == 0)
  1108. {
  1109. $return[$row['user']][] = $row['option_id'];
  1110. }
  1111. else
  1112. {
  1113. $return[$row['user']][] = $row['option_id'].'*'.$row['value'];
  1114. }
  1115. }
  1116. return $return;
  1117. }
  1118. /**
  1119. * count the number of users who answer positively on both options
  1120. *
  1121. * @param array $answers_x all the answers of the x axis
  1122. * @param array $answers_y all the answers of the y axis
  1123. * @param integer $option_x the x axis value (= the option_id of the first question)
  1124. * @param integer $option_y the y axis value (= the option_id of the second question)
  1125. * @return integer the number of users who have answered positively on both options
  1126. *
  1127. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1128. * @version February 2007
  1129. */
  1130. function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x=0, $value_y=0)
  1131. {
  1132. if ($value_x==0)
  1133. {
  1134. $check_x = $option_x;
  1135. }
  1136. else
  1137. {
  1138. $check_x = $option_x.'*'.$value_x;
  1139. }
  1140. if ($value_y==0)
  1141. {
  1142. $check_y = $option_y;
  1143. }
  1144. else
  1145. {
  1146. $check_y = $option_y.'*'.$value_y;
  1147. }
  1148. $counter = 0;
  1149. foreach ($answers_x as $user => $answers)
  1150. {
  1151. // check if the user has given $option_x as answer
  1152. if (in_array($check_x, $answers))
  1153. {
  1154. // check if the user has given $option_y as an answer
  1155. if (in_array($check_y, $answers_y[$user]))
  1156. {
  1157. $counter++;
  1158. }
  1159. }
  1160. }
  1161. return $counter;
  1162. }
  1163. ?>