reporting.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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 du Corbeau, 108
  13. B-1030 Brussels - Belgium
  14. */
  15. /**
  16. * @package dokeos.survey
  17. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  18. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  19. * @version $Id: reporting.php 14635 2008-03-17 21:43:24Z yannoo $
  20. *
  21. * @todo The question has to be more clearly indicated (same style as when filling the survey)
  22. */
  23. // name of the language file that needs to be included
  24. $language_file = 'survey';
  25. // including the global dokeos file
  26. require ('../inc/global.inc.php');
  27. // export
  28. /**
  29. * @todo use export_table_csv($data, $filename = 'export')
  30. */
  31. if ($_POST['export_report'])
  32. {
  33. $data = export_complete_report();
  34. $filename = 'fileexport.csv';
  35. header('Content-type: application/octet-stream');
  36. header('Content-Type: application/force-download');
  37. header('Content-length: '.$len);
  38. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
  39. {
  40. header('Content-Disposition: filename= '.$filename);
  41. }
  42. else
  43. {
  44. header('Content-Disposition: attachment; filename= '.$filename);
  45. }
  46. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  47. {
  48. header('Pragma: ');
  49. header('Cache-Control: ');
  50. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  51. }
  52. header('Content-Description: '.$filename);
  53. header('Content-transfer-encoding: binary');
  54. echo $data;
  55. exit;
  56. }
  57. // including additional libraries
  58. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  59. require_once('survey.lib.php');
  60. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  61. // Checking the parameters
  62. check_parameters();
  63. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  64. if (!api_is_allowed_to_edit())
  65. {
  66. Display :: display_header();
  67. Display :: display_error_message(get_lang('NotAllowed'), false);
  68. Display :: display_footer();
  69. exit;
  70. }
  71. // Database table definitions
  72. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  73. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  74. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  75. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  76. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  77. $user_info = Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
  78. // getting the survey information
  79. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  80. $urlname = substr(strip_tags($survey_data['title']), 0, 40);
  81. if (strlen(strip_tags($survey_data['title'])) > 40)
  82. {
  83. $urlname .= '...';
  84. }
  85. // breadcrumbs
  86. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  87. $interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$_GET['survey_id'], 'name' => $urlname);
  88. if (!$_GET['action'] OR $_GET['action'] == 'overview')
  89. {
  90. $tool_name = get_lang('Reporting');
  91. }
  92. else
  93. {
  94. $interbreadcrumb[] = array ("url" => "reporting.php?survey_id=".$_GET['survey_id'], "name" => get_lang('Reporting'));
  95. switch ($_GET['action'])
  96. {
  97. case 'questionreport':
  98. $tool_name = get_lang('DetailedReportByQuestion');
  99. break;
  100. case 'userreport':
  101. $tool_name = get_lang('DetailedReportByUser');
  102. break;
  103. case 'comparativereport':
  104. $tool_name = get_lang('ComparativeReport');
  105. break;
  106. case 'completereport':
  107. $tool_name = get_lang('CompleteReport');
  108. break;
  109. }
  110. }
  111. // Displaying the header
  112. Display::display_header($tool_name);
  113. // Action handling
  114. handle_reporting_actions();
  115. if (!$_GET['action'] OR $_GET['action'] == 'overview')
  116. {
  117. 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 />';
  118. 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 />';
  119. 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 />';
  120. 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 />';
  121. }
  122. // Footer
  123. Display :: display_footer();
  124. /**
  125. * This function checks the parameters that are used in this page
  126. *
  127. * @return the header, an error and the footer if any parameter fails, else it returns true
  128. *
  129. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  130. * @version February 2007
  131. */
  132. function check_parameters()
  133. {
  134. $error = false;
  135. // getting the survey data
  136. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  137. // $_GET['survey_id'] has to be numeric
  138. if (!is_numeric($_GET['survey_id']))
  139. {
  140. $error = get_lang('IllegalSurveyId');
  141. }
  142. // $_GET['action']
  143. $allowed_actions = array('overview', 'questionreport', 'userreport', 'comparativereport', 'completereport');
  144. if (isset($_GET['action']) AND !in_array($_GET['action'], $allowed_actions))
  145. {
  146. $error = get_lang('ActionNotAllowed');
  147. }
  148. // user report
  149. if ($_GET['action'] == 'userreport')
  150. {
  151. global $people_filled;
  152. if ($survey_data['anonymous'] == 0)
  153. {
  154. $people_filled_full_data = true;
  155. }
  156. else
  157. {
  158. $people_filled_full_data = false;
  159. }
  160. $people_filled = survey_manager::get_people_who_filled_survey($_GET['survey_id'], $people_filled_full_data);
  161. if ($survey_data['anonymous'] == 0)
  162. {
  163. foreach ($people_filled as $key=>$value)
  164. {
  165. $people_filled_userids[]=$value['invited_user'];
  166. }
  167. }
  168. else
  169. {
  170. $people_filled_userids = $people_filled;
  171. }
  172. if (isset($_GET['user']) AND !in_array($_GET['user'], $people_filled_userids))
  173. {
  174. $error = get_lang('UnknowUser');
  175. }
  176. }
  177. // question report
  178. if ($_GET['action'] == 'questionreport')
  179. {
  180. if (isset($_GET['question'])AND !is_numeric($_GET['question']))
  181. {
  182. $error = get_lang('UnknowQuestion');
  183. }
  184. }
  185. if ($error)
  186. {
  187. $tool_name = get_lang('Reporting');
  188. Display::display_header($tool_name);
  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 = $key+1;
  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. echo '<input type="hidden" name="export_report" value="export_report">';
  617. echo '</form>';
  618. echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'">';
  619. echo '<a href="#" onclick="document.form1.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportCurrentReport').'</a>';
  620. // the table
  621. echo '<table class="data_table" border="1">';
  622. // getting the number of options per question
  623. echo ' <tr>';
  624. echo ' <th>';
  625. if ($_POST['submit_question_filter'] OR $_POST['export_report'])
  626. {
  627. echo ' <input type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'" />';
  628. }
  629. echo ' <input type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'" />';
  630. echo '</th>';
  631. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  632. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  633. ON questions.question_id = options.question_id
  634. WHERE questions.question_id = options.question_id
  635. AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  636. GROUP BY questions.question_id";
  637. $result = api_sql_query($sql, __FILE__, __LINE__);
  638. while ($row = mysql_fetch_assoc($result))
  639. {
  640. // we show the questions if
  641. // 1. there is no question filter and the export button has not been clicked
  642. // 2. there is a quesiton filter but the question is selected for display
  643. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  644. {
  645. // we do not show comment and pagebreak question types
  646. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  647. {
  648. echo ' <th';
  649. if ($row['number_of_options'] >0)
  650. {
  651. echo ' colspan="'.$row['number_of_options'].'"';
  652. }
  653. echo '>';
  654. echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> ';
  655. echo $row['survey_question'];
  656. echo '</label>';
  657. echo '</th>';
  658. }
  659. }
  660. $questions[$row['question_id']] = $row;
  661. }
  662. echo ' </tr>';
  663. // getting all the questions and options
  664. echo ' <tr>';
  665. echo ' <th>&nbsp;</th>'; // the user column
  666. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  667. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  668. FROM $table_survey_question survey_question
  669. LEFT JOIN $table_survey_question_option survey_question_option
  670. ON survey_question.question_id = survey_question_option.question_id
  671. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  672. ORDER BY survey_question.sort ASC";
  673. $result = api_sql_query($sql, __FILE__, __LINE__);
  674. while ($row = mysql_fetch_assoc($result))
  675. {
  676. // we show the options if
  677. // 1. there is no question filter and the export button has not been clicked
  678. // 2. there is a quesiton filter but the question is selected for display
  679. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  680. {
  681. // we do not show comment and pagebreak question types
  682. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  683. {
  684. echo ' <th>';
  685. echo $row['option_text'];
  686. echo '</th>';
  687. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  688. }
  689. }
  690. }
  691. echo ' </tr>';
  692. // getting all the answers of the users
  693. $old_user='';
  694. $answers_of_user = array();
  695. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  696. $result = api_sql_query($sql, __FILE__, __LINE__);
  697. while ($row = mysql_fetch_assoc($result))
  698. {
  699. if ($old_user <> $row['user'] AND $old_user<>'')
  700. {
  701. display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions);
  702. $answers_of_user=array();
  703. }
  704. if ($questions[$row['question_id']]['type']<> 'open')
  705. {
  706. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  707. }
  708. else
  709. {
  710. $answers_of_user[$row['question_id']][0] = $row;
  711. }
  712. $old_user = $row['user'];
  713. }
  714. display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions); // this is to display the last user
  715. echo '</table>';
  716. echo '</form>';
  717. }
  718. /**
  719. * This function displays a row (= a user and his/her answers) in the table of the complete report.
  720. *
  721. * @param array $possible_answers all the possible options
  722. * @param array $answers_of_user the answers of the user
  723. * @param string $user the user
  724. *
  725. * @todo rename $possible_answers to $possible_options ?
  726. *
  727. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  728. * @version February 2007
  729. */
  730. function display_complete_report_row($possible_answers, $answers_of_user, $user, $questions)
  731. {
  732. global $survey_data;
  733. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  734. echo '<tr>';
  735. if ($survey_data['anonymous'] == 0)
  736. {
  737. if(intval($user)!==0)
  738. {
  739. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  740. $rs = api_sql_query($sql, __FILE__, __LINE__);
  741. if($row = mysql_fetch_array($rs, MYSQL_ASSOC))
  742. {
  743. $user_displayed = $row['lastname'].' '.$row['firstname'];
  744. }
  745. else
  746. {
  747. $user_displayed = '-';
  748. }
  749. echo ' <th><a href="'.api_get_self().'?action=userreport&survey_id='.$_GET['survey_id'].'&user='.$user.'">'.$user_displayed.'</a></th>'; // the user column
  750. }
  751. else
  752. {
  753. echo ' <th>'.$user.'</th>'; // the user column
  754. }
  755. }
  756. else
  757. {
  758. echo '<th>-</th>';
  759. }
  760. foreach ($possible_answers as $question_id=>$possible_option)
  761. {
  762. if ($questions[$question_id]['type'] == 'open')
  763. {
  764. echo '<td align="center">';
  765. echo $answers_of_user[$question_id]['0']['option_id'];
  766. echo '</td>';
  767. }
  768. else
  769. {
  770. foreach ($possible_option as $option_id=>$value)
  771. {
  772. echo '<td align="center">';
  773. if (!empty($answers_of_user[$question_id][$option_id]))
  774. {
  775. if ($answers_of_user[$question_id][$option_id]['value']<>0)
  776. {
  777. echo $answers_of_user[$question_id][$option_id]['value'];
  778. }
  779. else
  780. {
  781. echo 'v';
  782. }
  783. }
  784. }
  785. }
  786. }
  787. echo '</tr>';
  788. }
  789. /**
  790. * the function is quite similar to display_complete_report and return a html string that can be used in a csv file
  791. *
  792. * @todo consider merging this function with display_complete_report
  793. *
  794. * @return string $return the content of a csv file
  795. *
  796. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  797. * @version February 2007
  798. */
  799. function export_complete_report()
  800. {
  801. // Database table definitions
  802. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  803. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  804. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  805. // the first column
  806. $return = ';';
  807. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  808. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  809. ON questions.question_id = options.question_id "
  810. /*WHERE questions.question_id = options.question_id
  811. AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'*/
  812. ." AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  813. GROUP BY questions.question_id";
  814. $result = api_sql_query($sql, __FILE__, __LINE__);
  815. while ($row = mysql_fetch_assoc($result))
  816. {
  817. // we show the questions if
  818. // 1. there is no question filter and the export button has not been clicked
  819. // 2. there is a quesiton filter but the question is selected for display
  820. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  821. {
  822. // we do not show comment and pagebreak question types
  823. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  824. {
  825. if($row['number_of_options'] == 0 && $row['type']=='open')
  826. {
  827. $return .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['survey_question']))).';';
  828. }
  829. else
  830. {
  831. for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
  832. {
  833. $return .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['survey_question']))).';';
  834. }
  835. }
  836. }
  837. }
  838. }
  839. $return .= "\n";
  840. // getting all the questions and options
  841. $return .= ';';
  842. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  843. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  844. FROM $table_survey_question survey_question
  845. LEFT JOIN $table_survey_question_option survey_question_option
  846. ON survey_question.question_id = survey_question_option.question_id
  847. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  848. ORDER BY survey_question.sort ASC";
  849. $result = api_sql_query($sql, __FILE__, __LINE__);
  850. $possible_answers = array();
  851. $possible_answers_type = array();
  852. while ($row = mysql_fetch_assoc($result))
  853. {
  854. // we show the options if
  855. // 1. there is no question filter and the export button has not been clicked
  856. // 2. there is a quesiton filter but the question is selected for display
  857. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  858. {
  859. // we do not show comment and pagebreak question types
  860. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  861. {
  862. $return .= html_entity_decode(strip_tags($row['option_text'])).';';
  863. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  864. $possible_answers_type[$row['question_id']] = $row['type'];
  865. }
  866. }
  867. }
  868. $return .= "\n";
  869. // getting all the answers of the users
  870. $old_user='';
  871. $answers_of_user = array();
  872. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  873. $open_question_iterator = 1;
  874. $result = api_sql_query($sql, __FILE__, __LINE__);
  875. while ($row = mysql_fetch_assoc($result))
  876. {
  877. if ($old_user <> $row['user'] AND $old_user <> '')
  878. {
  879. $return .= export_complete_report_row($possible_answers, $answers_of_user, $old_user);
  880. $answers_of_user=array();
  881. }
  882. if($possible_answers_type[$row['question_id']] == 'open')
  883. {
  884. $temp_id = 'open'.$open_question_iterator;
  885. $answers_of_user[$row['question_id']][$temp_id] = $row;
  886. $open_question_iterator++;
  887. }
  888. else
  889. {
  890. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  891. }
  892. $old_user = $row['user'];
  893. }
  894. $return .= export_complete_report_row($possible_answers, $answers_of_user, $old_user); // this is to display the last user
  895. return $return;
  896. }
  897. /**
  898. * add a line to the csv file
  899. *
  900. * @param array $possible_answers all the possible answers
  901. * @param array $answers_of_user the answers of the user
  902. * @param string $user the user
  903. *
  904. * @return string $return line of the csv file
  905. *
  906. * @todo rename $possible_answers to $possible_options ?
  907. *
  908. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  909. * @version February 2007
  910. */
  911. function export_complete_report_row($possible_answers, $answers_of_user, $user)
  912. {
  913. $return = $user.';'; // the user column
  914. if(is_array($possible_answers))
  915. {
  916. foreach ($possible_answers as $question_id=>$possible_option)
  917. {
  918. if(is_array($possible_option) && count($possible_option)>0)
  919. {
  920. foreach ($possible_option as $option_id=>$value)
  921. {
  922. $key = array_keys($answers_of_user[$question_id]);
  923. if(substr($key[0],0,4)=='open')
  924. {
  925. $return .= '"'.str_replace('"','""',html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']))).'"';
  926. }
  927. elseif (!empty($answers_of_user[$question_id][$option_id]))
  928. {
  929. $return .= 'v';
  930. }
  931. $return .= ';';
  932. }
  933. }
  934. }
  935. }
  936. $return .= "\n";
  937. return $return;
  938. }
  939. /**
  940. * This function displays the comparative report which allows you to compare two questions
  941. * A comparative report creates a table where one question is on the x axis and a second question is on the y axis.
  942. * In the intersection is the number of people who have answerd positive on both options.
  943. *
  944. * @return html code
  945. *
  946. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  947. * @version February 2007
  948. */
  949. function display_comparative_report()
  950. {
  951. // allowed question types for comparative report
  952. $allowed_question_types = array('yesno', 'multiplechoice', 'multipleresponse', 'dropdown', 'percentage', 'score');
  953. // getting all the questions
  954. $questions = survey_manager::get_questions($_GET['survey_id']);
  955. // displaying an information message that only the questions with predefined answers can be used in a comparative report
  956. Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false);
  957. // The form for selecting the axis of the table
  958. 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'].'">';
  959. // survey_id
  960. echo '<input type="hidden" name="action" value="'.$_GET['action'].'"/>';
  961. echo '<input type="hidden" name="survey_id" value="'.(int)$_GET['survey_id'].'"/>';
  962. // X axis
  963. echo get_lang('SelectXAxis').': ';
  964. echo '<select name="xaxis">';
  965. echo '<option value="">---</option>';
  966. foreach ($questions as $key=>$question)
  967. {
  968. if (in_array($question['type'], $allowed_question_types))
  969. {
  970. echo '<option value="'.$question['question_id'].'"';
  971. if ($_GET['xaxis'] == $question['question_id'])
  972. {
  973. echo ' selected="selected"';
  974. }
  975. echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
  976. }
  977. }
  978. echo '</select><br /><br />';
  979. // Y axis
  980. echo get_lang('SelectYAxis').': ';
  981. echo '<select name="yaxis">';
  982. echo '<option value="">---</option>';
  983. foreach ($questions as $key=>$question)
  984. {
  985. if (in_array($question['type'], $allowed_question_types))
  986. {
  987. echo '<option value="'.$question['question_id'].'"';
  988. if ($_GET['yaxis'] == $question['question_id'])
  989. {
  990. echo ' selected="selected"';
  991. }
  992. echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
  993. }
  994. }
  995. echo '</select><br /><br />';
  996. echo '<input type="submit" name="Submit" value="Submit" />';
  997. echo '</form>';
  998. // getting all the information of the x axis
  999. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']))
  1000. {
  1001. $question_x = survey_manager::get_question($_GET['xaxis']);
  1002. }
  1003. // getting all the information of the y axis
  1004. if (isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  1005. {
  1006. $question_y = survey_manager::get_question($_GET['yaxis']);
  1007. }
  1008. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']) AND isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  1009. {
  1010. // getting the answers of the two questions
  1011. $answers_x = get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
  1012. $answers_y = get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
  1013. // displaying the table
  1014. echo '<table border="1" class="data_table">';
  1015. // the header
  1016. echo ' <tr>';
  1017. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  1018. {
  1019. if ($ii == 0)
  1020. {
  1021. echo ' <th>&nbsp;</th>';
  1022. }
  1023. else
  1024. {
  1025. if ($question_x['type']=='score')
  1026. {
  1027. for($x=1; $x<=$question_x['maximum_score']; $x++)
  1028. {
  1029. echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
  1030. }
  1031. $x='';
  1032. }
  1033. else
  1034. {
  1035. echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
  1036. }
  1037. }
  1038. }
  1039. echo ' </tr>';
  1040. // the main part
  1041. for ($ij=0; $ij<count($question_y['answers']); $ij++)
  1042. {
  1043. // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
  1044. if ($question_y['type'] == 'score')
  1045. {
  1046. for($y=1; $y<=$question_y['maximum_score']; $y++)
  1047. {
  1048. echo ' <tr>';
  1049. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  1050. {
  1051. if ($question_x['type']=='score')
  1052. {
  1053. for($x=1; $x<=$question_x['maximum_score']; $x++)
  1054. {
  1055. if ($ii == 0)
  1056. {
  1057. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  1058. break;
  1059. }
  1060. else
  1061. {
  1062. echo ' <td align="center">';
  1063. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
  1064. echo '</td>';
  1065. }
  1066. }
  1067. }
  1068. else
  1069. {
  1070. if ($ii == 0)
  1071. {
  1072. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  1073. }
  1074. else
  1075. {
  1076. echo ' <td align="center">';
  1077. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
  1078. echo '</td>';
  1079. }
  1080. }
  1081. }
  1082. echo ' </tr>';
  1083. }
  1084. }
  1085. // The Y axis is NOT a score question type so the number of rows = the number of options
  1086. else
  1087. {
  1088. echo ' <tr>';
  1089. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  1090. {
  1091. if ($question_x['type']=='score')
  1092. {
  1093. for($x=1; $x<=$question_x['maximum_score']; $x++)
  1094. {
  1095. if ($ii == 0)
  1096. {
  1097. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  1098. break;
  1099. }
  1100. else
  1101. {
  1102. echo ' <td align="center">';
  1103. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
  1104. echo '</td>';
  1105. }
  1106. }
  1107. }
  1108. else
  1109. {
  1110. if ($ii == 0)
  1111. {
  1112. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  1113. }
  1114. else
  1115. {
  1116. echo ' <td align="center">';
  1117. echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
  1118. echo '</td>';
  1119. }
  1120. }
  1121. }
  1122. echo ' </tr>';
  1123. }
  1124. }
  1125. echo '</table>';
  1126. }
  1127. }
  1128. /**
  1129. * get all the answers of a question grouped by user
  1130. *
  1131. * @param integer $survey_id the id of the survey
  1132. * @param integer $question_id the id of the question
  1133. * @return array $return an array countaining all the answers of all the users grouped by user
  1134. *
  1135. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1136. * @version February 2007
  1137. */
  1138. function get_answers_of_question_by_user($survey_id, $question_id)
  1139. {
  1140. // Database table definitions
  1141. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1142. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1143. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1144. $sql = "SELECT * FROM $table_survey_answer
  1145. WHERE survey_id='".Database::escape_string($survey_id)."'
  1146. AND question_id='".Database::escape_string($question_id)."'
  1147. ORDER BY USER ASC";
  1148. $result = api_sql_query($sql, __FILE__, __LINE__);
  1149. while ($row = mysql_fetch_assoc($result))
  1150. {
  1151. if ($row['value'] == 0)
  1152. {
  1153. $return[$row['user']][] = $row['option_id'];
  1154. }
  1155. else
  1156. {
  1157. $return[$row['user']][] = $row['option_id'].'*'.$row['value'];
  1158. }
  1159. }
  1160. return $return;
  1161. }
  1162. /**
  1163. * count the number of users who answer positively on both options
  1164. *
  1165. * @param array $answers_x all the answers of the x axis
  1166. * @param array $answers_y all the answers of the y axis
  1167. * @param integer $option_x the x axis value (= the option_id of the first question)
  1168. * @param integer $option_y the y axis value (= the option_id of the second question)
  1169. * @return integer the number of users who have answered positively on both options
  1170. *
  1171. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1172. * @version February 2007
  1173. */
  1174. function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x=0, $value_y=0)
  1175. {
  1176. if ($value_x==0)
  1177. {
  1178. $check_x = $option_x;
  1179. }
  1180. else
  1181. {
  1182. $check_x = $option_x.'*'.$value_x;
  1183. }
  1184. if ($value_y==0)
  1185. {
  1186. $check_y = $option_y;
  1187. }
  1188. else
  1189. {
  1190. $check_y = $option_y.'*'.$value_y;
  1191. }
  1192. $counter = 0;
  1193. foreach ($answers_x as $user => $answers)
  1194. {
  1195. // check if the user has given $option_x as answer
  1196. if (in_array($check_x, $answers))
  1197. {
  1198. // check if the user has given $option_y as an answer
  1199. if (in_array($check_y, $answers_y[$user]))
  1200. {
  1201. $counter++;
  1202. }
  1203. }
  1204. }
  1205. return $counter;
  1206. }
  1207. ?>