, Ghent University: cleanup, refactoring and rewriting large parts of the code * @version $Id: reporting.php 12263 2007-05-03 13:34:40Z elixir_julian $ * * @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']) { $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 ''.get_lang('DetailedReportByQuestion').'
'.get_lang('DetailedReportByQuestionDetail').'

'; echo ''.get_lang('DetailedReportByUser').'
'.get_lang('DetailedReportByUserDetail').'.

'; echo ''.get_lang('ComparativeReport').'
'.get_lang('ComparativeReportDetail').'.

'; echo ''.get_lang('CompleteReport').'
'.get_lang('CompleteReportDetail').'

'; } // 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 , Ghent University * @version February 2007 */ function check_parameters() { $error = false; // $_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; $people_filled = survey_manager::get_people_who_filled_survey($_GET['survey_id']); if (isset($_GET['user']) AND !in_array($_GET['user'], $people_filled)) { $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) { Display::display_header(); 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 , 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 , Ghent University * @version February 2007 */ function display_user_report() { global $people_filled; // 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 " "; echo get_lang('SelectUserWhoFilledSurvey').'
'; echo ''; // 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; } // 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']]; } $display = new $question['type']; $display->render_question($question, $second_parameter); // echo '
';
	//		print_r($answers[$question['question_id']]);
		//	echo '
'; } } } /** * 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 , 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 '
'; for($i=1; $i<=($survey_data['number_of_questions']); $i++ ) { if ($offset <> $i-1) { echo ''.$i.''; } else { echo $i; } if ($i < $survey_data['number_of_questions']) { echo ' | '; } } echo '
'; // 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 ' << '.get_lang('PreviousQuestion').' '; } else { echo '<<'.get_lang('PreviousQuestion').' '; } echo ' | '; if ($_GET['question'] < ($survey_data['number_of_questions']-1)) { echo ''.get_lang('NextQuestion').'>> '; } else { echo get_lang('NextQuestion'). '>>'; } echo '
'; echo $question['survey_question']; echo '
'; 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'].'
'; } } 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; } // 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; } // displaying the table: headers echo ''; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; // displaying the table: the content foreach ($options as $key=>$value) { $absolute_number = $data[$value['question_option_id']]['total']; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; } // displaying the table: footer (totals) echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo '
 '.get_lang('AbsoluteTotal').''.get_lang('Percentage').''.get_lang('VisualRepresentation').'
'.$value['option_text'].''.$absolute_number.''.round($absolute_number/$number_of_answers*100, 2).' %
 
'.get_lang('Total').''.$number_of_answers.'  
'; } if (isset($_GET['viewoption'])) { echo get_lang('PeopleWhoAnswered').': '.$options[$_GET['viewoption']]['option_text'].'
'; 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 ''.$row['user'].'
'; } } } 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 '
';
	print_r($data);
	echo '
'; */ // displaying the table: headers echo ''; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; // 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 ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; } } // displaying the table: footer (totals) echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo '
 '.get_lang('Score').''.get_lang('AbsoluteTotal').''.get_lang('Percentage').''.get_lang('VisualRepresentation').'
'.$value['option_text'].''.$i.''.$absolute_number.''.round($absolute_number/$number_of_answers*100, 2).' %
 
'.get_lang('Total').' '.$number_of_answers.'  
'; } /** * This functions displays the complete reporting * * @return html code * * @todo open questions are not in the complete report yet. * * @author Patrick Cool , 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 '
'; // the export button echo ''; // the table echo ''; // getting the number of options per question echo ' '; echo ' '; $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 ' 0) { echo ' colspan="'.$row['number_of_options'].'"'; } echo '>'; echo ''; echo ''; } } } echo ' '; // getting all the questions and options echo ' '; echo ' '; // 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 ' '; $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id']; } } } echo ' '; // 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); $answers_of_user=array(); } $answers_of_user[$row['question_id']][$row['option_id']] = $row; $old_user = $row['user']; } display_complete_report_row($possible_answers, $answers_of_user, $old_user); // this is to display the last user echo '
'; if ($_POST['submit_question_filter'] OR $_POST['export_report']) { echo ' '; } echo ' '; echo '
 '; echo $row['option_text']; echo '
'; echo '
'; } /** * 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 , Ghent University * @version February 2007 */ function display_complete_report_row($possible_answers, $answers_of_user, $user) { echo ''; echo ' '.$user.''; // the user column foreach ($possible_answers as $question_id=>$possible_option) { foreach ($possible_option as $option_id=>$value) { echo ''; 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 ''; } } echo ''; } /** * 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 , 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'])."' 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') { for ($ii = 0; $ii < $row['number_of_options']; $ii ++) { $return .= 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__); 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') { $return .= $row['option_text'].';'; $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id']; } } } $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"; $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(); } $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 , Ghent University * @version February 2007 */ function export_complete_report_row($possible_answers, $answers_of_user, $user) { $return = $user.';'; // the user column foreach ($possible_answers as $question_id=>$possible_option) { foreach ($possible_option as $option_id=>$value) { if (!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 , 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 '
'; // survey_id echo ''; echo ''; // X axis echo get_lang('SelectXAxis').': '; echo '

'; // Y axis echo get_lang('SelectYAxis').': '; echo '

'; echo ''; echo '
'; // 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 ''; // the header echo ' '; for ($ii=0; $ii<=count($question_x['answers']); $ii++) { if ($ii == 0) { echo ' '; } else { if ($question_x['type']=='score') { for($x=1; $x<=$question_x['maximum_score']; $x++) { echo ' '; } $x=''; } else { echo ' '; } } } echo ' '; // the main part for ($ij=0; $ij'; 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 ' '; break; } else { echo ' '; } } } else { if ($ii == 0) { echo ' '; } else { echo ' '; } } } echo ' '; } } // The Y axis is NOT a score question type so the number of rows = the number of options else { echo ' '; 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 ' '; break; } else { echo ' '; } } } else { if ($ii == 0) { echo ' '; } else { echo ' '; } } } echo ' '; } } echo '
 '.$question_x['answers'][($ii-1)].'
'.$x.'
'.$question_x['answers'][($ii-1)].'
'.$question_y['answers'][($ij)].' '.$y.''; echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y); echo ''.$question_y['answers'][($ij)].' '.$y.''; echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y); echo '
'.$question_y['answers'][($ij)].''; echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0); echo ''.$question_y['answers'][($ij)].''; echo comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]); echo '
'; } } /** * 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 , 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 , 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; } ?>