123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335 |
- <?php
- /*
- DOKEOS - elearning and course management software
- For a full list of contributors, see documentation/credits.html
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- See "documentation/licence.html" more details.
- Contact:
- Dokeos
- Rue du Corbeau, 108
- B-1030 Brussels - Belgium
- */
- /**
- * @package dokeos.survey
- * @author unknown, the initial survey that did not make it in 1.8 because of bad code
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
- * @version $Id: reporting.php 14636 2008-03-17 22:24:08Z yannoo $
- *
- * @todo The question has to be more clearly indicated (same style as when filling the survey)
- */
- // name of the language file that needs to be included
- $language_file = 'survey';
- // including the global dokeos file
- require ('../inc/global.inc.php');
- // export
- /**
- * @todo use export_table_csv($data, $filename = 'export')
- */
- if ($_POST['export_report'])
- {
- if($_POST['export_xls'])
- {
- $data = export_complete_report();
- $filename = 'fileexport.csv';
- echo $data;
- exit;
- }
- else
- {
- $data = export_complete_report();
- $filename = 'fileexport.csv';
- header('Content-type: application/octet-stream');
- header('Content-Type: application/force-download');
- header('Content-length: '.$len);
- if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
- {
- header('Content-Disposition: filename= '.$filename);
- }
- else
- {
- header('Content-Disposition: attachment; filename= '.$filename);
- }
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
- {
- header('Pragma: ');
- header('Cache-Control: ');
- header('Cache-Control: public'); // IE cannot download from sessions without a cache
- }
- header('Content-Description: '.$filename);
- header('Content-transfer-encoding: binary');
- echo $data;
- exit;
- }
- }
- // including additional libraries
- //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
- require_once('survey.lib.php');
- require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
- // Checking the parameters
- check_parameters();
- /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
- if (!api_is_allowed_to_edit())
- {
- Display :: display_header();
- Display :: display_error_message(get_lang('NotAllowed'), false);
- Display :: display_footer();
- exit;
- }
- // Database table definitions
- $table_survey = Database :: get_course_table(TABLE_SURVEY);
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
- $table_user = Database :: get_main_table(TABLE_MAIN_USER);
- $user_info = Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
- // getting the survey information
- $survey_data = survey_manager::get_survey($_GET['survey_id']);
- $urlname = substr(strip_tags($survey_data['title']), 0, 40);
- if (strlen(strip_tags($survey_data['title'])) > 40)
- {
- $urlname .= '...';
- }
- // breadcrumbs
- $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
- $interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$_GET['survey_id'], 'name' => $urlname);
- if (!$_GET['action'] OR $_GET['action'] == 'overview')
- {
- $tool_name = get_lang('Reporting');
- }
- else
- {
- $interbreadcrumb[] = array ("url" => "reporting.php?survey_id=".$_GET['survey_id'], "name" => get_lang('Reporting'));
- switch ($_GET['action'])
- {
- case 'questionreport':
- $tool_name = get_lang('DetailedReportByQuestion');
- break;
- case 'userreport':
- $tool_name = get_lang('DetailedReportByUser');
- break;
- case 'comparativereport':
- $tool_name = get_lang('ComparativeReport');
- break;
- case 'completereport':
- $tool_name = get_lang('CompleteReport');
- break;
- }
- }
- // Displaying the header
- Display::display_header($tool_name);
- // Action handling
- handle_reporting_actions();
- if (!$_GET['action'] OR $_GET['action'] == 'overview')
- {
- echo '<b><a href="reporting.php?action=questionreport&survey_id='.$_GET['survey_id'].'">'.get_lang('DetailedReportByQuestion').'</a></b> <br />'.get_lang('DetailedReportByQuestionDetail').' <br /><br />';
- echo '<b><a href="reporting.php?action=userreport&survey_id='.$_GET['survey_id'].'">'.get_lang('DetailedReportByUser').'</a></b><br />'.get_lang('DetailedReportByUserDetail').'.<br /><br />';
- echo '<b><a href="reporting.php?action=comparativereport&survey_id='.$_GET['survey_id'].'">'.get_lang('ComparativeReport').'</a></b><br />'.get_lang('ComparativeReportDetail').'.<br /><br />';
- echo '<b><a href="reporting.php?action=completereport&survey_id='.$_GET['survey_id'].'">'.get_lang('CompleteReport').'</a></b><br />'.get_lang('CompleteReportDetail').'<br /><br />';
- }
- // Footer
- Display :: display_footer();
- /**
- * This function checks the parameters that are used in this page
- *
- * @return the header, an error and the footer if any parameter fails, else it returns true
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function check_parameters()
- {
- $error = false;
-
- // getting the survey data
- $survey_data = survey_manager::get_survey($_GET['survey_id']);
- // $_GET['survey_id'] has to be numeric
- if (!is_numeric($_GET['survey_id']))
- {
- $error = get_lang('IllegalSurveyId');
- }
- // $_GET['action']
- $allowed_actions = array('overview', 'questionreport', 'userreport', 'comparativereport', 'completereport');
- if (isset($_GET['action']) AND !in_array($_GET['action'], $allowed_actions))
- {
- $error = get_lang('ActionNotAllowed');
- }
- // user report
- if ($_GET['action'] == 'userreport')
- {
- global $people_filled;
- if ($survey_data['anonymous'] == 0)
- {
- $people_filled_full_data = true;
- }
- else
- {
- $people_filled_full_data = false;
- }
- $people_filled = survey_manager::get_people_who_filled_survey($_GET['survey_id'], $people_filled_full_data);
- if ($survey_data['anonymous'] == 0)
- {
- foreach ($people_filled as $key=>$value)
- {
- $people_filled_userids[]=$value['invited_user'];
- }
- }
- else
- {
- $people_filled_userids = $people_filled;
- }
-
- if (isset($_GET['user']) AND !in_array($_GET['user'], $people_filled_userids))
- {
- $error = get_lang('UnknowUser');
- }
- }
- // question report
- if ($_GET['action'] == 'questionreport')
- {
- if (isset($_GET['question'])AND !is_numeric($_GET['question']))
- {
- $error = get_lang('UnknowQuestion');
- }
- }
- if ($error)
- {
- $tool_name = get_lang('Reporting');
- Display::display_header($tool_name);
- Display::display_error_message(get_lang('Error').': '.$error, false);
- Display::display_footer();
- exit;
- }
- else
- {
- return true;
- }
- }
- /**
- * This function deals with the action handling
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function handle_reporting_actions()
- {
- // getting the number of question
- $temp_questions_data = survey_manager::get_questions($_GET['survey_id']);
- // sorting like they should be displayed and removing the non-answer question types (comment and pagebreak)
- foreach ($temp_questions_data as $key=>$value)
- {
- if ($value['type'] <> 'comment' AND $value['type']<>'pagebreak')
- {
- $questions_data[$value['sort']]=$value;
- }
- }
- // counting the number of questions that are relevant for the reporting
- $survey_data['number_of_questions'] = count($questions_data);
- if ($_GET['action'] == 'questionreport')
- {
- display_question_report($survey_data);
- }
- if ($_GET['action'] == 'userreport')
- {
- display_user_report();
- }
- if ($_GET['action'] == 'comparativereport')
- {
- display_comparative_report();
- }
- if ($_GET['action'] == 'completereport')
- {
- display_complete_report();
- }
- }
- /**
- * This function displays the user report which is basically nothing more than a one-page display of all the questions
- * of the survey that is filled with the answers of the person who filled the survey.
- *
- * @return html code of the one-page survey with the answers of the selected user
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function display_user_report()
- {
- global $people_filled, $survey_data;
-
- // Database table definitions
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
- // step 1: selection of the user
- echo "<script language=\"JavaScript\" type=\"text/JavaScript\">
- <!--
- function jumpMenu(targ,selObj,restore)
- {
- eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
- if (restore) selObj.selectedIndex=0;
- }
- //-->
- </script>
- ";
- echo get_lang('SelectUserWhoFilledSurvey').'<br />';
- echo '<select name="user" onchange="jumpMenu(\'parent\',this,0)">';
- echo '<option value="reporting.php?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'">'.get_lang('SelectUser').'</option>';
-
- foreach ($people_filled as $key=>$person)
- {
- if ($survey_data['anonymous'] == 0)
- {
- $name = $person['firstname'].' '.$person['lastname'];
- $id = $person['user_id'];
- }
- else
- {
- $name = $key+1;
- $id = $person;
- }
- echo '<option value="reporting.php?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'&user='.$id.'" ';
- if ($_GET['user'] == $person)
- {
- echo 'selected="selected"';
- }
- echo '>'.$name.'</option>';
- }
- echo '</select>';
- // step 2: displaying the survey and the answer of the selected users
- if (isset($_GET['user']))
- {
- Display::display_normal_message(get_lang('AllQuestionsOnOnePage'), false);
- // getting all the questions and options
- $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,
- survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
- FROM $table_survey_question survey_question
- LEFT JOIN $table_survey_question_option survey_question_option
- ON survey_question.question_id = survey_question_option.question_id
- WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
- ORDER BY survey_question.sort ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- if($row['type'] <> 'pagebreak')
- {
- $questions[$row['sort']]['question_id'] = $row['question_id'];
- $questions[$row['sort']]['survey_id'] = $row['survey_id'];
- $questions[$row['sort']]['survey_question'] = $row['survey_question'];
- $questions[$row['sort']]['display'] = $row['display'];
- $questions[$row['sort']]['type'] = $row['type'];
- $questions[$row['sort']]['maximum_score'] = $row['max_value'];
- $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
- }
- }
- // getting all the answers of the user
- $sql = "SELECT * FROM $table_survey_answer WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."' AND user = '".Database::escape_string($_GET['user'])."'";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- $answers[$row['question_id']][] = $row['option_id'];
- $all_answers[$row['question_id']][] = $row;
- }
- /*
- echo '<pre>';
- print_r($all_answers);
- echo '</pre>';
- */
-
- // displaying all the questions
- foreach ($questions as $key=>$question)
- {
- // if the question type is a scoring then we have to format the answers differently
- if ($question['type'] == 'score')
- {
- foreach($all_answers[$question['question_id']] as $key=>$answer_array)
- {
- $second_parameter[$answer_array['option_id']] = $answer_array['value'];
- }
- }
- else
- {
- $second_parameter = $answers[$question['question_id']];
- if ($question['type'] == 'open')
- {
- $second_parameter = array();
- $second_parameter[] = $all_answers[$question['question_id']][0]['option_id'];
- }
- }
- $display = new $question['type'];
- $display->render_question($question, $second_parameter);
- // echo '<pre>';
- // print_r($answers[$question['question_id']]);
- // echo '</pre>';
- }
- }
- }
- /**
- * This function displays the report by question.
- * It displays a table with all the options of the question and the number of users who have answered positively on the option.
- * The number of users who answered positive on a given option is expressed in an absolute number, in a percentage of the total
- * and graphically using bars
- * By clicking on the absolute number you get a list with the persons who have answered this.
- * 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
- * answers of that user.
- *
- * @param array $survey_data all the data of the survey
- *
- * @return html code that displays the report by question
- *
- * @todo allow switching between horizontal and vertical.
- * @todo multiple response: percentage are probably not OK
- * @todo the question and option text have to be shortened and should expand when the user clicks on it.
- * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function display_question_report($survey_data)
- {
- // Database table definitions
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
- // determining the offset of the sql statement (the n-th question of the survey)
- if (!isset($_GET['question']))
- {
- $offset = 0;
- }
- else
- {
- $offset = $_GET['question'];
- }
- echo '<div id="question_report_questionnumbers">';
- for($i=1; $i<=($survey_data['number_of_questions']); $i++ )
- {
- if ($offset <> $i-1)
- {
- echo '<a href="reporting.php?action=questionreport&survey_id='.(int)$_GET['survey_id'].'&question='.($i-1).'">'.$i.'</a>';
- }
- else
- {
- echo $i;
- }
- if ($i < $survey_data['number_of_questions'])
- {
- echo ' | ';
- }
- }
- echo '</div>';
- // getting the question information
- $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";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- $question = mysql_fetch_assoc($result);
- // navigate through the questions (next and previous)
- if ($_GET['question'] <> 0)
- {
- echo '<a href="reporting.php?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'&question='.($offset-1).'"> << '.get_lang('PreviousQuestion').'</a> ';
- }
- else
- {
- echo '<<'.get_lang('PreviousQuestion').' ';
- }
- echo ' | ';
- if ($_GET['question'] < ($survey_data['number_of_questions']-1))
- {
- echo '<a href="reporting.php?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'&question='.($offset+1).'">'.get_lang('NextQuestion').'>> </a>';
- }
- else
- {
- echo get_lang('NextQuestion'). '>>';
- }
- echo '<br />';
- echo $question['survey_question'];
- echo '<br />';
- if ($question['type'] == 'score')
- {
- /** @todo this function should return the options as this is needed further in the code */
- $options = display_question_report_score($survey_data, $question, $offset);
- }
- elseif ($question['type'] == 'open')
- {
- /** @todo also get the user who has answered this */
- $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
- AND question_id = '".Database::escape_string($question['question_id'])."'";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- echo $row['option_id'].'<hr noshade="noshade" size="1" />';
- }
- }
- else
- {
- // getting the options
- $sql = "SELECT * FROM $table_survey_question_option
- WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
- AND question_id = '".Database::escape_string($question['question_id'])."'
- ORDER BY sort ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- $options[$row['question_option_id']] = $row;
- }
- //echo '<pre>';
- //print_r($options);
- //echo '<pre>';
- // getting the answers
- $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
- WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
- AND question_id = '".Database::escape_string($question['question_id'])."'
- GROUP BY option_id, value";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- $number_of_answers += $row['total'];
- $data[$row['option_id']] = $row;
- }
- //echo '<pre>';
- //print_r($data);
- //echo '<pre>';
- // displaying the table: headers
- echo '<table>';
- echo ' <tr>';
- echo ' <th> </th>';
- echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
- echo ' <th>'.get_lang('Percentage').'</th>';
- echo ' <th>'.get_lang('VisualRepresentation').'</th>';
- echo ' <tr>';
- // displaying the table: the content
- foreach ($options as $key=>$value)
- {
- $absolute_number = $data[$value['question_option_id']]['total'];
- echo ' <tr>';
- echo ' <td>'.$value['option_text'].'</td>';
- echo ' <td><a href="reporting.php?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'&question='.$offset.'&viewoption='.$value['question_option_id'].'">'.$absolute_number.'</a></td>';
- echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
- echo ' <td>';
- $size = $absolute_number/$number_of_answers*100*2;
- if ($size > 0)
- {
- echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px"> </div>';
- }
- echo ' </td>';
- echo ' </tr>';
- }
- // displaying the table: footer (totals)
- echo ' <tr>';
- echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
- echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
- echo ' <td style="border-top:1px solid black"> </td>';
- echo ' <td style="border-top:1px solid black"> </td>';
- echo ' </tr>';
- echo '</table>';
- }
- if (isset($_GET['viewoption']))
- {
- echo get_lang('PeopleWhoAnswered').': '.$options[$_GET['viewoption']]['option_text'].'<br />';
- if (is_numeric($_GET['value']))
- {
- $sql_restriction = "AND value='".Database::escape_string($_GET['value'])."'";
- }
- $sql = "SELECT user FROM $table_survey_answer WHERE option_id = '".Database::escape_string($_GET['viewoption'])."' $sql_restriction";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- echo '<a href="reporting.php?action=userreport&survey_id='.$_GET['survey_id'].'&user='.$row['user'].'">'.$row['user'].'</a><br />';
- }
- }
- }
- function display_question_report_score($survey_data, $question, $offset)
- {
- // Database table definitions
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
- // getting the options
- $sql = "SELECT * FROM $table_survey_question_option
- WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
- AND question_id = '".Database::escape_string($question['question_id'])."'
- ORDER BY sort ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- $options[$row['question_option_id']] = $row;
- }
- // getting the answers
- $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
- WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
- AND question_id = '".Database::escape_string($question['question_id'])."'
- GROUP BY option_id, value";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- $number_of_answers += $row['total'];
- $data[$row['option_id']][$row['value']] = $row;
- }
- /*
- echo '<pre>';
- print_r($data);
- echo '</pre>';
- */
- // displaying the table: headers
- echo '<table>';
- echo ' <tr>';
- echo ' <th> </th>';
- echo ' <th>'.get_lang('Score').'</th>';
- echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
- echo ' <th>'.get_lang('Percentage').'</th>';
- echo ' <th>'.get_lang('VisualRepresentation').'</th>';
- echo ' <tr>';
- // displaying the table: the content
- foreach ($options as $key=>$value)
- {
- for ($i=1; $i<=$question['max_value']; $i++)
- {
- $absolute_number = $data[$value['question_option_id']][$i]['total'];
- echo ' <tr>';
- echo ' <td>'.$value['option_text'].'</td>';
- echo ' <td>'.$i.'</td>';
- echo ' <td><a href="reporting.php?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'&question='.$offset.'&viewoption='.$value['question_option_id'].'&value='.$i.'">'.$absolute_number.'</a></td>';
- echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
- echo ' <td>';
- $size = ($absolute_number/$number_of_answers*100*2);
- if ($size > 0)
- {
- echo ' <div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px"> </div>';
- }
- echo ' </td>';
- echo ' </tr>';
- }
- }
- // displaying the table: footer (totals)
- echo ' <tr>';
- echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
- echo ' <td style="border-top:1px solid black"> </td>';
- echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
- echo ' <td style="border-top:1px solid black"> </td>';
- echo ' <td style="border-top:1px solid black"> </td>';
- echo ' </tr>';
- echo '</table>';
- }
- /**
- * This functions displays the complete reporting
- *
- * @return html code
- *
- * @todo open questions are not in the complete report yet.
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function display_complete_report()
- {
- // Database table definitions
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
- // the form
- echo '<form id="form1" name="form1" method="post" action="'.api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'">';
- /*// the export button
- echo '<input type="submit" name="export_report" value="'.get_lang('ExportCurrentReport').'" />';*/
-
- echo '<input type="hidden" name="export_report" value="export_report">';
-
- echo '</form>';
-
- echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id'].'">';
-
- echo '<a href="#" onclick="document.form1.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif"> '.get_lang('ExportCurrentReport').'</a>';
-
- // the table
- echo '<table class="data_table" border="1">';
- // getting the number of options per question
- echo ' <tr>';
- echo ' <th>';
- if ($_POST['submit_question_filter'] OR $_POST['export_report'])
- {
- echo ' <input type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'" />';
- }
- echo ' <input type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'" />';
- echo '</th>';
- $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
- FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
- ON questions.question_id = options.question_id
- WHERE questions.question_id = options.question_id
- AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
- GROUP BY questions.question_id";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- // we show the questions if
- // 1. there is no question filter and the export button has not been clicked
- // 2. there is a quesiton filter but the question is selected for display
- if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
- {
- // we do not show comment and pagebreak question types
- if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
- {
- echo ' <th';
- if ($row['number_of_options'] >0)
- {
- echo ' colspan="'.$row['number_of_options'].'"';
- }
- echo '>';
- echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> ';
- echo $row['survey_question'];
- echo '</label>';
- echo '</th>';
- }
- }
- $questions[$row['question_id']] = $row;
- }
- echo ' </tr>';
- // getting all the questions and options
- echo ' <tr>';
- echo ' <th> </th>'; // the user column
- $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
- survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
- FROM $table_survey_question survey_question
- LEFT JOIN $table_survey_question_option survey_question_option
- ON survey_question.question_id = survey_question_option.question_id
- WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
- ORDER BY survey_question.sort ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- // we show the options if
- // 1. there is no question filter and the export button has not been clicked
- // 2. there is a quesiton filter but the question is selected for display
- if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
- {
- // we do not show comment and pagebreak question types
- if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
- {
- echo ' <th>';
- echo $row['option_text'];
- echo '</th>';
- $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
- }
- }
- }
- echo ' </tr>';
- // getting all the answers of the users
- $old_user='';
- $answers_of_user = array();
- $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- if ($old_user <> $row['user'] AND $old_user<>'')
- {
- display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions);
- $answers_of_user=array();
- }
- if ($questions[$row['question_id']]['type']<> 'open')
- {
- $answers_of_user[$row['question_id']][$row['option_id']] = $row;
- }
- else
- {
- $answers_of_user[$row['question_id']][0] = $row;
- }
- $old_user = $row['user'];
- }
- display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions); // this is to display the last user
- echo '</table>';
- echo '</form>';
- }
- /**
- * This function displays a row (= a user and his/her answers) in the table of the complete report.
- *
- * @param array $possible_answers all the possible options
- * @param array $answers_of_user the answers of the user
- * @param string $user the user
- *
- * @todo rename $possible_answers to $possible_options ?
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function display_complete_report_row($possible_answers, $answers_of_user, $user, $questions)
- {
- global $survey_data;
-
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- echo '<tr>';
- if ($survey_data['anonymous'] == 0)
- {
- if(intval($user)!==0)
- {
- $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
- $rs = api_sql_query($sql, __FILE__, __LINE__);
- if($row = mysql_fetch_array($rs, MYSQL_ASSOC))
- {
- $user_displayed = $row['lastname'].' '.$row['firstname'];
- }
- else
- {
- $user_displayed = '-';
- }
- echo ' <th><a href="'.api_get_self().'?action=userreport&survey_id='.$_GET['survey_id'].'&user='.$user.'">'.$user_displayed.'</a></th>'; // the user column
- }
- else
- {
- echo ' <th>'.$user.'</th>'; // the user column
- }
- }
- else
- {
- echo '<th>-</th>';
- }
-
- foreach ($possible_answers as $question_id=>$possible_option)
- {
- if ($questions[$question_id]['type'] == 'open')
- {
- echo '<td align="center">';
- echo $answers_of_user[$question_id]['0']['option_id'];
- echo '</td>';
- }
- else
- {
- foreach ($possible_option as $option_id=>$value)
- {
- echo '<td align="center">';
- if (!empty($answers_of_user[$question_id][$option_id]))
- {
- if ($answers_of_user[$question_id][$option_id]['value']<>0)
- {
- echo $answers_of_user[$question_id][$option_id]['value'];
- }
- else
- {
- echo 'v';
- }
- }
- }
- }
-
- }
- echo '</tr>';
- }
- /**
- * the function is quite similar to display_complete_report and return a html string that can be used in a csv file
- *
- * @todo consider merging this function with display_complete_report
- *
- * @return string $return the content of a csv file
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function export_complete_report()
- {
- // Database table definitions
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
- // the first column
- $return = ';';
- $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
- FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
- ON questions.question_id = options.question_id "
- /*WHERE questions.question_id = options.question_id
- AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'*/
- ." AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
- GROUP BY questions.question_id";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- // we show the questions if
- // 1. there is no question filter and the export button has not been clicked
- // 2. there is a quesiton filter but the question is selected for display
- if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
- {
- // we do not show comment and pagebreak question types
- if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
- {
- if($row['number_of_options'] == 0 && $row['type']=='open')
- {
- $return .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['survey_question']))).';';
- }
- else
- {
- for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
- {
- $return .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['survey_question']))).';';
- }
- }
- }
- }
- }
- $return .= "\n";
- // getting all the questions and options
- $return .= ';';
- $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
- survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
- FROM $table_survey_question survey_question
- LEFT JOIN $table_survey_question_option survey_question_option
- ON survey_question.question_id = survey_question_option.question_id
- WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
- ORDER BY survey_question.sort ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- $possible_answers = array();
- $possible_answers_type = array();
- while ($row = mysql_fetch_assoc($result))
- {
- // we show the options if
- // 1. there is no question filter and the export button has not been clicked
- // 2. there is a quesiton filter but the question is selected for display
- if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
- {
- // we do not show comment and pagebreak question types
- if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
- {
- $return .= html_entity_decode(strip_tags($row['option_text'])).';';
- $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
- $possible_answers_type[$row['question_id']] = $row['type'];
- }
- }
- }
- $return .= "\n";
- // getting all the answers of the users
- $old_user='';
- $answers_of_user = array();
- $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
-
- $open_question_iterator = 1;
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- if ($old_user <> $row['user'] AND $old_user <> '')
- {
- $return .= export_complete_report_row($possible_answers, $answers_of_user, $old_user);
- $answers_of_user=array();
- }
- if($possible_answers_type[$row['question_id']] == 'open')
- {
- $temp_id = 'open'.$open_question_iterator;
- $answers_of_user[$row['question_id']][$temp_id] = $row;
- $open_question_iterator++;
- }
- else
- {
- $answers_of_user[$row['question_id']][$row['option_id']] = $row;
- }
- $old_user = $row['user'];
- }
- $return .= export_complete_report_row($possible_answers, $answers_of_user, $old_user); // this is to display the last user
- return $return;
- }
- /**
- * add a line to the csv file
- *
- * @param array $possible_answers all the possible answers
- * @param array $answers_of_user the answers of the user
- * @param string $user the user
- *
- * @return string $return line of the csv file
- *
- * @todo rename $possible_answers to $possible_options ?
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function export_complete_report_row($possible_answers, $answers_of_user, $user)
- {
- $return = $user.';'; // the user column
- if(is_array($possible_answers))
- {
- foreach ($possible_answers as $question_id=>$possible_option)
- {
- if(is_array($possible_option) && count($possible_option)>0)
- {
- foreach ($possible_option as $option_id=>$value)
- {
- $key = array_keys($answers_of_user[$question_id]);
- if(substr($key[0],0,4)=='open')
- {
- $return .= '"'.str_replace('"','""',html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']))).'"';
- }
- elseif (!empty($answers_of_user[$question_id][$option_id]))
- {
- $return .= 'v';
- }
- $return .= ';';
- }
- }
- }
- }
- $return .= "\n";
- return $return;
- }
- /**
- * This function displays the comparative report which allows you to compare two questions
- * A comparative report creates a table where one question is on the x axis and a second question is on the y axis.
- * In the intersection is the number of people who have answerd positive on both options.
- *
- * @return html code
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function display_comparative_report()
- {
- // allowed question types for comparative report
- $allowed_question_types = array('yesno', 'multiplechoice', 'multipleresponse', 'dropdown', 'percentage', 'score');
- // getting all the questions
- $questions = survey_manager::get_questions($_GET['survey_id']);
- // displaying an information message that only the questions with predefined answers can be used in a comparative report
- Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false);
- // The form for selecting the axis of the table
- 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'].'">';
- // survey_id
- echo '<input type="hidden" name="action" value="'.$_GET['action'].'"/>';
- echo '<input type="hidden" name="survey_id" value="'.(int)$_GET['survey_id'].'"/>';
- // X axis
- echo get_lang('SelectXAxis').': ';
- echo '<select name="xaxis">';
- echo '<option value="">---</option>';
- foreach ($questions as $key=>$question)
- {
- if (in_array($question['type'], $allowed_question_types))
- {
- echo '<option value="'.$question['question_id'].'"';
- if ($_GET['xaxis'] == $question['question_id'])
- {
- echo ' selected="selected"';
- }
- echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
- }
- }
- echo '</select><br /><br />';
- // Y axis
- echo get_lang('SelectYAxis').': ';
- echo '<select name="yaxis">';
- echo '<option value="">---</option>';
- foreach ($questions as $key=>$question)
- {
- if (in_array($question['type'], $allowed_question_types))
- {
- echo '<option value="'.$question['question_id'].'"';
- if ($_GET['yaxis'] == $question['question_id'])
- {
- echo ' selected="selected"';
- }
- echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
- }
- }
- echo '</select><br /><br />';
- echo '<input type="submit" name="Submit" value="Submit" />';
- echo '</form>';
- // getting all the information of the x axis
- if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']))
- {
- $question_x = survey_manager::get_question($_GET['xaxis']);
- }
- // getting all the information of the y axis
- if (isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
- {
- $question_y = survey_manager::get_question($_GET['yaxis']);
- }
- if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']) AND isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
- {
- // getting the answers of the two questions
- $answers_x = get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
- $answers_y = get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
- // displaying the table
- echo '<table border="1" class="data_table">';
- // the header
- echo ' <tr>';
- for ($ii=0; $ii<=count($question_x['answers']); $ii++)
- {
- if ($ii == 0)
- {
- echo ' <th> </th>';
- }
- else
- {
- if ($question_x['type']=='score')
- {
- for($x=1; $x<=$question_x['maximum_score']; $x++)
- {
- echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
- }
- $x='';
- }
- else
- {
- echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
- }
- }
- }
- echo ' </tr>';
- // the main part
- for ($ij=0; $ij<count($question_y['answers']); $ij++)
- {
- // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
- if ($question_y['type'] == 'score')
- {
- for($y=1; $y<=$question_y['maximum_score']; $y++)
- {
- echo ' <tr>';
- for ($ii=0; $ii<=count($question_x['answers']); $ii++)
- {
- if ($question_x['type']=='score')
- {
- for($x=1; $x<=$question_x['maximum_score']; $x++)
- {
- if ($ii == 0)
- {
- echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
- break;
- }
- else
- {
- echo ' <td align="center">';
- echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
- echo '</td>';
- }
- }
- }
- else
- {
- if ($ii == 0)
- {
- echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
- }
- else
- {
- echo ' <td align="center">';
- echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
- echo '</td>';
- }
- }
- }
- echo ' </tr>';
- }
- }
- // The Y axis is NOT a score question type so the number of rows = the number of options
- else
- {
- echo ' <tr>';
- for ($ii=0; $ii<=count($question_x['answers']); $ii++)
- {
- if ($question_x['type']=='score')
- {
- for($x=1; $x<=$question_x['maximum_score']; $x++)
- {
- if ($ii == 0)
- {
- echo ' <th>'.$question_y['answers'][($ij)].'</th>';
- break;
- }
- else
- {
- echo ' <td align="center">';
- echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
- echo '</td>';
- }
- }
- }
- else
- {
- if ($ii == 0)
- {
- echo ' <th>'.$question_y['answers'][($ij)].'</th>';
- }
- else
- {
- echo ' <td align="center">';
- echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
- echo '</td>';
- }
- }
- }
- echo ' </tr>';
- }
- }
- echo '</table>';
- }
- }
- /**
- * get all the answers of a question grouped by user
- *
- * @param integer $survey_id the id of the survey
- * @param integer $question_id the id of the question
- * @return array $return an array countaining all the answers of all the users grouped by user
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function get_answers_of_question_by_user($survey_id, $question_id)
- {
- // Database table definitions
- $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
- $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
- $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
- $sql = "SELECT * FROM $table_survey_answer
- WHERE survey_id='".Database::escape_string($survey_id)."'
- AND question_id='".Database::escape_string($question_id)."'
- ORDER BY USER ASC";
- $result = api_sql_query($sql, __FILE__, __LINE__);
- while ($row = mysql_fetch_assoc($result))
- {
- if ($row['value'] == 0)
- {
- $return[$row['user']][] = $row['option_id'];
- }
- else
- {
- $return[$row['user']][] = $row['option_id'].'*'.$row['value'];
- }
- }
- return $return;
- }
- /**
- * count the number of users who answer positively on both options
- *
- * @param array $answers_x all the answers of the x axis
- * @param array $answers_y all the answers of the y axis
- * @param integer $option_x the x axis value (= the option_id of the first question)
- * @param integer $option_y the y axis value (= the option_id of the second question)
- * @return integer the number of users who have answered positively on both options
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version February 2007
- */
- function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x=0, $value_y=0)
- {
- if ($value_x==0)
- {
- $check_x = $option_x;
- }
- else
- {
- $check_x = $option_x.'*'.$value_x;
- }
- if ($value_y==0)
- {
- $check_y = $option_y;
- }
- else
- {
- $check_y = $option_y.'*'.$value_y;
- }
- $counter = 0;
- foreach ($answers_x as $user => $answers)
- {
- // check if the user has given $option_x as answer
- if (in_array($check_x, $answers))
- {
- // check if the user has given $option_y as an answer
- if (in_array($check_y, $answers_y[$user]))
- {
- $counter++;
- }
- }
- }
- return $counter;
- }
- ?>
|