Browse Source

[svn r14639] General cleanup - Moved all functions into a SurveyUtil class
Added first version of XLS export of survey results (not fully functionnal yet) (see FS#2265)

Yannick Warnier 17 years ago
parent
commit
6f36c7c0a3

+ 5 - 82
main/survey/fillsurvey.php

@@ -125,12 +125,6 @@ $survey_data['survey_id'] = $survey_invitation['survey_id'];
 // storing the answers
 if ($_POST)
 {
-	/*
-	echo '<pre>';
-	print_r($_POST);
-	echo '</pre>';
-	*/
-
 	// getting all the types of the question (because of the special treatment of the score question type
 	$sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'";
 	$result = api_sql_query($sql, __FILE__, __LINE__);
@@ -154,7 +148,7 @@ if ($_POST)
 			// 		   when it is a scoring question then the key of the array is the option_id and the value is the value
 			if (is_array($value))
 			{
-				remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
+				SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
 				foreach ($value as $answer_key => $answer_value)
 				{
 					if ($types[$survey_question_id] == 'score')
@@ -167,7 +161,7 @@ if ($_POST)
 						$option_id = $answer_value;
 						$option_value = '';
 					}
-					store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $option_id, $option_value, $survey_data);
+					SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $option_id, $option_value, $survey_data);
 				}
 			}
 			// all the other question types (open question, multiple choice, percentage, ...)
@@ -192,9 +186,9 @@ if ($_POST)
 
 
 				$survey_question_answer = $value;
-				remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
-				store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
-				//store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
+				SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
+				SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
+				//SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
 			}
 		}
 	}
@@ -337,75 +331,4 @@ echo '</form>';
 
 // Footer
 Display :: display_footer();
-
-
-/**
- * This function stores an answer of a user on a question of a survey
- *
- * @param mixed $user the user id or email of the person who fills the survey
- * @param integer $survey_id the survey id
- * @param integer $question_id the question id
- * @param integer $option_id the option id
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function store_answer($user, $survey_id, $question_id, $option_id, $option_value, $survey_data)
-{
-	global $_course;
-	global $types;
-
-
-
-	// table definition
-	$table_survey_answer 		= Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
-
-	// make the survey anonymous
-	if ($survey_data['anonymous'] == 1)
-	{
-		if (!$_SESSION['surveyuser'])
-		{
-			$user = md5($user.time());
-			$_SESSION['surveyuser'] = $user;
-		}
-		else
-		{
-			$user = $_SESSION['surveyuser'];
-		}
-	}
-
-	$sql = "INSERT INTO $table_survey_answer (user, survey_id, question_id, option_id, value) VALUES (
-			'".Database::escape_string($user)."',
-			'".Database::escape_string($survey_id)."',
-			'".Database::escape_string($question_id)."',
-			'".Database::escape_string($option_id)."',
-			'".Database::escape_string($option_value)."'
-			)";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-}
-
-/**
- * This function removes an (or multiple) answer(s) of a user on a question of a survey
- *
- * @param mixed $user the user id or email of the person who fills the survey
- * @param integer $survey_id the survey id
- * @param integer $question_id the question id
- * @param integer $option_id the option id
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function remove_answer($user, $survey_id, $question_id)
-{
-	global $_course;
-
-	// table definition
-	$table_survey_answer 		= Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
-
-	$sql = "DELETE FROM $table_survey_answer
-			WHERE user = '".Database::escape_string($user)."'
-			AND survey_id = '".Database::escape_string($survey_id)."'
-			AND question_id = '".Database::escape_string($question_id)."'";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-}
 ?>

+ 7 - 7
main/survey/preview.php

@@ -68,7 +68,7 @@ $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$_GET['survey_id'],
 Display :: display_header(get_lang('SurveyPreview'));
 
 // We exit here is the first or last question is a pagebreak (which causes errors)
-check_first_last_question($_GET['survey_id'], false);
+SurveyUtil::check_first_last_question($_GET['survey_id'], false);
 
 // only a course admin is allowed to preview a survey: you are NOT a course admin => error message
 if (!api_is_allowed_to_edit())
@@ -108,7 +108,7 @@ else
 				ORDER BY sort ASC";
 		$result = api_sql_query($sql, __FILE__, __LINE__);
 
-		while ($row = mysql_fetch_assoc($result))
+		while ($row = Database::fetch_array($result))
 		{
 			if($row['type'] == 'pagebreak')
 			{
@@ -128,15 +128,15 @@ else
 					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'])."'
-					AND survey_question.question_id IN (".implode(',',$paged_questions[$_GET['show']]).")
+					AND survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).")
 					ORDER BY survey_question.sort, survey_question_option.sort ASC";
 
 			$result = api_sql_query($sql, __FILE__, __LINE__);
-			$question_counter_max = mysql_num_rows($result);
+			$question_counter_max = Database::num_rows($result);
 			$counter = 0;
 			$limit=0;
 			$questions = array();
-			while ($row = mysql_fetch_assoc($result))
+			while ($row = Database::fetch_array($result))
 			{
 				// if the type is not a pagebreak we store it in the $questions array
 				if($row['type'] <> 'pagebreak')
@@ -161,7 +161,7 @@ else
 	// selecting the maximum number of pages
 	$sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($_GET['survey_id'])."'";
 	$result = api_sql_query($sql, __FILE__, __LINE__);
-	$numberofpages = mysql_num_rows($result) + 1;
+	$numberofpages = Database::num_rows($result) + 1;
 	// Displaying the form with the questions
 	if (isset($_GET['show']))
 	{
@@ -171,7 +171,7 @@ else
 	{
 		$show = 0;
 	}
-	echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.$_GET['survey_id'].'&show='.$show.'">';
+	echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($_GET['survey_id']).'&show='.$show.'">';
 	if(is_array($questions) && count($questions)>0)
 	{
 		foreach ($questions as $key=>$question)

+ 22 - 1202
main/survey/reporting.php

@@ -20,7 +20,7 @@
 *	@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 $
+* 	@version $Id: reporting.php 14639 2008-03-18 05:31:08Z yannoo $
 *
 * 	@todo The question has to be more clearly indicated (same style as when filling the survey)
 */
@@ -30,6 +30,7 @@ $language_file = 'survey';
 
 // including the global dokeos file
 require ('../inc/global.inc.php');
+require_once('survey.lib.php');
 
 // export
 /**
@@ -37,18 +38,18 @@ require ('../inc/global.inc.php');
  */
 if ($_POST['export_report'])
 {
-	if($_POST['export_xls'])
+	switch($_POST['export_format'])
 	{
-			$data = export_complete_report();
-			$filename = 'fileexport.csv';
-
-			echo $data;
+		case 'xls':
+			$filename = 'survey_results_'.$_GET['survey_id'].'.xls';
+			$data = SurveyUtil::export_complete_report_xls($filename);
 			exit;
-	}
-	else
-	{
-			$data = export_complete_report();
-			$filename = 'fileexport.csv';
+			break;
+		case 'csv':
+		default:
+			$data = SurveyUtil::export_complete_report();
+			//$filename = 'fileexport.csv';
+			$filename = 'survey_results_'.$_GET['survey_id'].'.csv';
 
 			header('Content-type: application/octet-stream');
 			header('Content-Type: application/force-download');
@@ -72,21 +73,21 @@ if ($_POST['export_report'])
 
 			echo $data;
 			exit;
+			break;
 	}
 }
 
 // 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();
+SurveyUtil::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_header(get_lang('Survey'));
 	Display :: display_error_message(get_lang('NotAllowed'), false);
 	Display :: display_footer();
 	exit;
@@ -139,1197 +140,16 @@ else
 Display::display_header($tool_name);
 
 // Action handling
-handle_reporting_actions();
+SurveyUtil::handle_reporting_actions();
 
 if (!$_GET['action'] OR $_GET['action'] == 'overview')
 {
-	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 />';
-	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 />';
-	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 />';
-	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 />';
+	$myweb_survey_id = Security::remove_XSS($_GET['survey_id']);
+	echo '<b><a href="reporting.php?action=questionreport&amp;survey_id='.$myweb_survey_id.'">'.get_lang('DetailedReportByQuestion').'</a></b> <br />'.get_lang('DetailedReportByQuestionDetail').' <br /><br />';
+	echo '<b><a href="reporting.php?action=userreport&amp;survey_id='.$myweb_survey_id.'">'.get_lang('DetailedReportByUser').'</a></b><br />'.get_lang('DetailedReportByUserDetail').'.<br /><br />';
+	echo '<b><a href="reporting.php?action=comparativereport&amp;survey_id='.$myweb_survey_id.'">'.get_lang('ComparativeReport').'</a></b><br />'.get_lang('ComparativeReportDetail').'.<br /><br />';
+	echo '<b><a href="reporting.php?action=completereport&amp;survey_id='.$myweb_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'].'&amp;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'].'&amp;survey_id='.$_GET['survey_id'].'&amp;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&amp;survey_id='.(int)$_GET['survey_id'].'&amp;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'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.($offset-1).'"> &lt;&lt; '.get_lang('PreviousQuestion').'</a>  ';
-	}
-	else
-	{
-		echo '&lt;&lt;'.get_lang('PreviousQuestion').' ';
-	}
-	echo ' | ';
-	if ($_GET['question'] < ($survey_data['number_of_questions']-1))
-	{
-		echo '<a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.($offset+1).'">'.get_lang('NextQuestion').'&gt;&gt; </a>';
-	}
-	else
-	{
-		echo get_lang('NextQuestion'). '&gt;&gt;';
-	}
-	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>&nbsp;</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'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.$offset.'&amp;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">&nbsp;</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">&nbsp;</td>';
-		echo '		<td style="border-top:1px solid black">&nbsp;</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>&nbsp;</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'].'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.$offset.'&amp;viewoption='.$value['question_option_id'].'&amp;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">&nbsp;</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">&nbsp;</td>';
-		echo '		<td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
-		echo '		<td style="border-top:1px solid black">&nbsp;</td>';
-		echo '		<td style="border-top:1px solid black">&nbsp;</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">&nbsp;'.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>&nbsp;</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>&nbsp;</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;
-}
-?>
+Display :: display_footer();

+ 1947 - 28
main/survey/survey.lib.php

@@ -1788,43 +1788,1962 @@ class score extends question
 }
 
 /**
- * Checks whether the given survey has a pagebreak question as the first or the last question.
- * If so, break the current process, displaying an error message
- * @param	integer	Survey ID (database ID)
- * @param	boolean	Optional. Whether to continue the current process or exit when breaking condition found. Defaults to true (do not break).
- * @return	void 
+ * This class offers a series of general utility functions for survey querying and display
+ * @package dokeos.survey
  */
-function check_first_last_question($survey_id, $continue=true)
-{
-	// table definitions
-	$tbl_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
-	$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+class SurveyUtil {
+	/**
+	 * Checks whether the given survey has a pagebreak question as the first or the last question.
+	 * If so, break the current process, displaying an error message
+	 * @param	integer	Survey ID (database ID)
+	 * @param	boolean	Optional. Whether to continue the current process or exit when breaking condition found. Defaults to true (do not break).
+	 * @return	void 
+	 */
+	function check_first_last_question($survey_id, $continue=true)
+	{
+		// table definitions
+		$tbl_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+		$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+	
+		// getting the information of the question
+		$sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort ASC";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$total = Database::num_rows($result);
+		$counter=1;
+		$error = false;
+		while ($row = Database::fetch_array($result,'ASSOC'))
+		{
+			if ($counter == 1 AND $row['type'] == 'pagebreak')
+			{
+				Display::display_error_message(get_lang('PagebreakNotFirst'), false);
+				$error = true;
+			}
+			if ($counter == $total AND $row['type'] == 'pagebreak')
+			{
+				Display::display_error_message(get_lang('PagebreakNotLast'), false);
+				$error = true;
+			}		
+			$counter++;
+		}
+		
+		if (!$continue AND $error)
+		{
+			Display::display_footer();
+			exit;
+		}
+	}
+	/**
+	 * This function removes an (or multiple) answer(s) of a user on a question of a survey
+	 *
+	 * @param mixed   The user id or email of the person who fills the survey
+	 * @param integer The survey id
+	 * @param integer The question id
+	 * @param integer The option id
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function remove_answer($user, $survey_id, $question_id)
+	{
+		global $_course;
+		// table definition
+		$table_survey_answer 		= Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
+		$sql = "DELETE FROM $table_survey_answer
+				WHERE user = '".Database::escape_string($user)."'
+				AND survey_id = '".Database::escape_string($survey_id)."'
+				AND question_id = '".Database::escape_string($question_id)."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+	}
+	/**
+	 * This function stores an answer of a user on a question of a survey
+	 *
+	 * @param mixed   The user id or email of the person who fills the survey
+	 * @param integer Survey id
+	 * @param integer Question id
+	 * @param integer Option id
+	 * @param string  Option value
+	 * @param array	  Survey data settings
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function store_answer($user, $survey_id, $question_id, $option_id, $option_value, $survey_data)
+	{
+		global $_course;
+		global $types;
+		// table definition
+		$table_survey_answer 		= Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
+	
+		// make the survey anonymous
+		if ($survey_data['anonymous'] == 1)
+		{
+			if (!$_SESSION['surveyuser'])
+			{
+				$user = md5($user.time());
+				$_SESSION['surveyuser'] = $user;
+			}
+			else
+			{
+				$user = $_SESSION['surveyuser'];
+			}
+		}
+		$sql = "INSERT INTO $table_survey_answer (user, survey_id, question_id, option_id, value) VALUES (
+				'".Database::escape_string($user)."',
+				'".Database::escape_string($survey_id)."',
+				'".Database::escape_string($question_id)."',
+				'".Database::escape_string($option_id)."',
+				'".Database::escape_string($option_value)."'
+				)";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+	}
+	/**
+	 * This function checks the parameters that are used in this page
+	 *
+	 * @return 	string 	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
+	 * @return	void
+	 * @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')
+		{
+			SurveyUtil::display_question_report($survey_data);
+		}
+		if ($_GET['action'] == 'userreport')
+		{
+			SurveyUtil::display_user_report();
+		}
+		if ($_GET['action'] == 'comparativereport')
+		{
+			SurveyUtil::display_comparative_report();
+		}
+		if ($_GET['action'] == 'completereport')
+		{
+			SurveyUtil::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 	string	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 - Updated March 2008
+	 */
+	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='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_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='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;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;
+			}
+			// 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);
+			}
+		}
+	}
+	
+	/**
+	 * 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 	All the survey data
+	 * @return 	string	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 - Updated March 2008
+	 */
+	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 = Database::escape_string($_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&amp;survey_id='.(int)$_GET['survey_id'].'&amp;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 = Database::fetch_array($result);
+		
+		// navigate through the questions (next and previous)
+		if ($_GET['question'] <> 0)
+		{
+			echo '<a href="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.Security::remove_XSS($offset-1).'"> &lt;&lt; '.get_lang('PreviousQuestion').'</a>  ';
+		}
+		else
+		{
+			echo '&lt;&lt;'.get_lang('PreviousQuestion').' ';
+		}
+		echo ' | ';
+		if ($_GET['question'] < ($survey_data['number_of_questions']-1))
+		{
+			echo '<a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset+1).'">'.get_lang('NextQuestion').'&gt;&gt; </a>';
+		}
+		else
+		{
+			echo get_lang('NextQuestion'). '&gt;&gt;';
+		}
+		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 = SurveyUtil::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 = Database::fetch_array($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 = Database::fetch_array($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 = Database::fetch_array($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>&nbsp;</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'];
+				if($number_of_answers == 0)
+				{
+					$answers_number = 0;
+				}
+				else
+				{
+					$answers_number = $absolute_number/$number_of_answers*100;
+				}
+				echo '	<tr>';
+				echo '		<td>'.$value['option_text'].'</td>';
+				echo '		<td align="right"><a href="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset).'&amp;viewoption='.$value['question_option_id'].'">'.$absolute_number.'</a></td>';
+				echo '		<td align="right">'.round($answers_number, 2).' %</td>';
+				echo '		<td align="right">';
+				$size = $answers_number*2;
+				if ($size > 0)
+				{
+					echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</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;" align="right"><b>'.($number_of_answers==0?'0':$number_of_answers).'</b></td>';
+			echo '		<td style="border-top:1px solid black;">&nbsp;</td>';
+			echo '		<td style="border-top:1px solid black;">&nbsp;</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 = Database::fetch_array($result))
+			{
+				echo '<a href="reporting.php?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$row['user'].'">'.$row['user'].'</a><br />';
+			}
+		}
+	}
+	/**
+	 * Display score data about a survey question
+	 * @param	array	Question info
+	 * @param	integer	The offset of results shown
+	 * @return	void 	(direct output)
+	 */
+	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 = Database::fetch_array($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 = Database::fetch_array($result))
+		{
+			$number_of_answers += $row['total'];
+			$data[$row['option_id']][$row['value']] = $row;
+		}
+		// displaying the table: headers
+		echo '<table>';
+		echo '	<tr>';
+		echo '		<th>&nbsp;</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='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset).'&amp;viewoption='.$value['question_option_id'].'&amp;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">&nbsp;</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">&nbsp;</td>';
+			echo '		<td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
+			echo '		<td style="border-top:1px solid black">&nbsp;</td>';
+			echo '		<td style="border-top:1px solid black">&nbsp;</td>';
+			echo '	</tr>';
+	
+			echo '</table>';
+	}
+	
+	/**
+	 * This functions displays the complete reporting
+	 * @return	string	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="form1a" name="form1a" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
+		echo '<input type="hidden" name="export_report" value="export_report">';
+		echo '<input type="hidden" name="export_format" value="csv">';
+		echo '</form>';
+		echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
+		echo '<input type="hidden" name="export_report" value="export_report">';
+		echo '<input type="hidden" name="export_format" value="xls">';
+		echo '</form>';
+		echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
+		echo '<a href="#" onclick="document.form1a.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportCurrentReportAsCSV').'</a>';
+		echo '<a href="#" onclick="document.form1b.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportCurrentReportAsXLS').'</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 = Database::fetch_array($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>&nbsp;</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 = Database::fetch_array($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 = Database::fetch_array($result))
+		{
+			if ($old_user <> $row['user'] AND $old_user<>'')
+			{
+				SurveyUtil::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'];
+		}
+		SurveyUtil::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 options
+	 * @param 	array 	User answers
+	 * @param	mixed	User ID or user details string
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version February 2007 - Updated March 2008
+	 */
+	function display_complete_report_row($possible_options, $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 = Database::fetch_array($rs))
+				{
+					$user_displayed = $row['lastname'].' '.$row['firstname'];
+				}
+				else
+				{
+					$user_displayed = '-';
+				}
+				echo '		<th><a href="'.api_get_self().'?action=userreport&survey_id='.Security::remove_XSS($_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_options 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>';
+	}
+	
+	
+	/**
+	 * Quite similar to display_complete_report(), returns an HTML string 
+	 * that can be used in a csv file
+	 * @todo consider merging this function with display_complete_report
+	 * @return	string	The contents 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 "
+				." AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
+				GROUP BY questions.question_id";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		while ($row = Database::fetch_array($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 = Database::fetch_array($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 = Database::fetch_array($result))
+		{
+			if ($old_user <> $row['user'] AND $old_user <> '')
+			{
+				$return .= SurveyUtil::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 .= SurveyUtil::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
+	 * @param	array	User's answers
+	 * @param 	mixed	User ID or user details as string - Used as a string in the result string
+	 * @return	string	One line of the csv file
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version February 2007
+	 */
+	function export_complete_report_row($possible_options, $answers_of_user, $user)
+	{
+		$return = $user.';'; // the user column
+	
+		if(is_array($possible_options))
+		{
+			foreach ($possible_options 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;
+	}
+	/**
+	 * Quite similar to display_complete_report(), returns an HTML string 
+	 * that can be used in a csv file
+	 * @todo consider merging this function with display_complete_report
+	 * @return	string	The contents of a csv file
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version February 2007
+	 */
+	function export_complete_report_xls($filename)
+	{
+		require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
+		$workbook = new Spreadsheet_Excel_Writer();
+		$workbook->send($filename);
+		$worksheet =& $workbook->addWorksheet('Report 1');
+		$line = 0;
+		$column = 0;
+		
+		//$worksheet->write($line, 0, 'Name');
+		
+		// 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
+	
+		$column ++; //skip the first column (row titles)
+		$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 "
+				." AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
+				GROUP BY questions.question_id";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		while ($row = Database::fetch_array($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')
+					{
+							$worksheet->write($line,$column,html_entity_decode(strip_tags($row['survey_question'])));
+							$column ++;					
+					}
+					else
+					{
+						for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
+						{
+							$worksheet->write($line,$column,html_entity_decode(strip_tags($row['survey_question'])));
+							$column ++;
+						}
+					}
+				}
+			}
+		}
+		$line++;
+		$column = 1;
+	
+		// 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.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 = Database::fetch_array($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')
+				{
+					$worksheet->write($line,$column,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'];
+				}
+			}
+		}
+		$line ++;
+		$column = 0;
+	
+		// 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 = Database::fetch_array($result))
+		{
+			if ($old_user <> $row['user'] AND $old_user <> '')
+			{
+				$return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user);
+				foreach($return as $elem)
+				{
+					$worsheet->write($line,$column,$elem);
+					$column++;
+				}
+				$line++;
+				$column=0;
+				$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 = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user); // this is to display the last user
+		foreach($return as $elem)
+		{
+			$worksheet->write($line,$column,$elem);
+			$column++;
+		}
+		$workbook->close();
+		return null;
+	}
+	
+	
+	/**
+	 * Add a line to the csv file
+	 *
+	 * @param	array	Possible answers
+	 * @param	array	User's answers
+	 * @param 	mixed	User ID or user details as string - Used as a string in the result string
+	 * @return	string	One line of the csv file
+	 */
+	function export_complete_report_row_xls($possible_options, $answers_of_user, $user)
+	{
+		$return = array();
+		$return[] = $user; // the user column
+	
+		if(is_array($possible_options))
+		{
+			foreach ($possible_options 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[] = 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;
+	}
+	
+	/**
+	 * 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	string	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='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&xaxis='.Security::remove_XSS($_GET['xaxis']).'&y='.Security::remove_XSS($_GET['yaxis']).'">';
+		// survey_id
+		echo '<input type="hidden" name="action" value="'.Security::remove_XSS($_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 = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
+			$answers_y = SurveyUtil::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>&nbsp;</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 SurveyUtil::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 SurveyUtil::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 SurveyUtil::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 SurveyUtil::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
+	 * @param	integer	Question ID
+	 * @return 	Array	Array containing all answers of all users, grouped by user
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version February 2007 - Updated March 2008
+	 */
+	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 = Database::fetch_array($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	All answers of the x axis
+	 * @param	array	All answers of the y axis
+	 * @param	integer x axis value (= the option_id of the first question)
+	 * @param	integer y axis value (= the option_id of the second question)
+	 * @return	integer Number of users who have answered positively to 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;
+		}
 
-	// getting the information of the question
-	$sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort ASC";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-	$total = Database::num_rows($result);
-	$counter=1;
-	$error = false;
-	while ($row = Database::fetch_array($result,'ASSOC'))
+		$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;
+	}
+	/**
+	 * Get all the information about the invitations of a certain survey
+	 *
+	 * @return	array	Lines of invitation [user, code, date, empty element]
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 *
+	 * @todo use survey_id parameter instead of $_GET
+	 */
+	function get_survey_invitations_data()
+	{
+		// Database table definition
+		$table_survey_invitation 		= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+		$table_user 					= Database :: get_main_table(TABLE_MAIN_USER);
+	
+		$sql = "SELECT
+					survey_invitation.user as col1,
+					survey_invitation.invitation_code as col2,
+					survey_invitation.invitation_date as col3,
+					'' as col4
+					FROM $table_survey_invitation survey_invitation
+			LEFT JOIN $table_user user ON  survey_invitation.user = user.user_id
+			WHERE survey_invitation.survey_id = '".Database::escape_string($_GET['survey_id'])."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		while ($row = Database::fetch_array($res))
+		{
+			$survey_invitation_data[] = $row;
+		}
+		return $survey_invitation_data;
+	}
+	
+	/**
+	 * Get the total number of survey invitations for a given survey (through $_GET['survey_id'])
+	 *
+	 * @return	integer	Total number of survey invitations
+	 *
+	 * @todo use survey_id parameter instead of $_GET
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function get_number_of_survey_invitations()
+	{
+		// Database table definition
+		$table_survey_invitation 		= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+	
+		$sql = "SELECT count(user) AS total FROM $table_survey_invitation WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		$row = mysql_fetch_assoc($res);
+		return $row['total'];
+	}
+	/**
+	 * Save the invitation mail
+	 *
+	 * @param string 	Text of the e-mail
+	 * @param integer	Whether the mail contents are for invite mail (0, default) or reminder mail (1)
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function save_invite_mail($mailtext, $reminder=0)
+	{
+		// Database table definition
+		$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
+	
+		// reminder or not
+		if ($reminder == 0)
+		{
+			$mail_field = 'invite_mail';
+		}
+		else
+		{
+			$mail_field = 'reminder_mail';
+		}
+	
+		$sql = "UPDATE $table_survey SET $mail_field = '".Database::escape_string($mailtext)."' WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+	}
+	
+	/**
+	 * This function saves all the invitations of course users and additional users in the
+	 *
+	 * @param	array	Users array
+	 * @param 	string	Title of the invitation, used as the title of the mail
+	 * @param 	string	Text of the invitation, used as the text of the mail.
+	 * 				 The text has to contain a **link** string or this will automatically be added to the end
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 *
+	 * @todo create the survey link
+	 */
+	function save_invitations($users_array, $invitation_title, $invitation_text, $reminder=0)
+	{
+		global $_user;
+		global $_course;
+		global $_configuration;
+	
+		// getting the survey information
+		$survey_data = survey_manager::get_survey($_GET['survey_id']);
+	
+		// Database table definition
+		$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+		$table_user 				= Database :: get_main_table(TABLE_MAIN_USER);
+	
+		$already_invited = array();
+		$survey_invitations = array();
+		// get the people who are already invited
+		if ($reminder == 1)
+		{
+			$survey_invitations = SurveyUtil::get_invitations($survey_data['survey_code']);
+		}
+		else
+		{
+			$already_invited = SurveyUtil::get_invited_users($survey_data['code']);
+		}
+	
+		$counter = 0;
+		if (is_array($users_array))
+		{
+			foreach ($users_array as $key=>$value)
+			{
+				if(isset($value))
+				{
+					// generating the unique code
+					if(is_array($survey_invitations))
+					{
+						if ($reminder == 1 AND array_key_exists($value,$survey_invitations))
+						{
+							$invitation_code = $survey_invitations[$value]['invitation_code'];
+						}
+						else 
+						{
+							$invitation_code = md5($value.microtime());
+						}
+					}
+					$survey_link = '';
+					$full_invitation_text= '';
+								
+					// storing the invitation (only if the user_id is not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
+					$add_users_array = explode(';',$already_invited['additional_users']);
+					if ((is_numeric($value) AND !in_array($value,$already_invited['course_users'])) OR (!is_numeric($value) AND !in_array($value,$add_users_array)))
+					{
+						if(is_array($survey_invitations))
+						{
+							if (!array_key_exists($value,$survey_invitations))
+							{
+								$sql = "INSERT INTO $table_survey_invitation (user, survey_code, invitation_code, invitation_date) VALUES
+											('".Database::escape_string($value)."','".Database::escape_string($survey_data['code'])."','".Database::escape_string($invitation_code)."','".Database::escape_string(date('Y-m-d H:i:s'))."')";
+								$result = api_sql_query($sql, __FILE__, __LINE__);
+							}
+						}
+						// replacing the **link** part with a valid link for the user
+						$survey_link = $_configuration['root_web'].$_configuration['code_append'].'survey/'.'fillsurvey.php?course='.$_course['sysCode'].'&invitationcode='.$invitation_code;
+						$text_link = '<a href="'.$survey_link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n".get_lang('OrCopyPasteTheFollowingUrl')." <br />\r\n ".$survey_link;
+						
+						$replace_count = 0;
+						$full_invitation_text = str_ireplace('**link**', $text_link ,$invitation_text, $replace_count);
+						if ($replace_count < 1)
+						{
+							$full_invitation_text = $full_invitation_text . "<br />\r\n<br />\r\n".$text_link;
+						}
+						
+		
+						// optionally: finding the e-mail of the course user
+						if (is_numeric($value))
+						{
+							$sql = "SELECT firstname, lastname, email FROM $table_user WHERE user_id='".Database::escape_string($value)."'";
+							$result = api_sql_query($sql, __FILE__, __LINE__);
+							$row = Database::fetch_array($result);
+							$recipient_email = $row['email'];
+							$recipient_name = $row['firstname'].' '.$row['lastname'];
+						}
+						else
+						{
+							/** @todo check if the $value is a valid email	 */
+							$recipient_email = $value;
+						}
+		
+						// sending the mail
+						$sender_name  = $_user['firstName'].' '.$_user['lastName'];
+						$sender_email = $_user['mail'];
+						
+						$replyto = array();
+						if(api_get_setting('survey_email_sender_noreply')=='noreply')
+						{
+							$noreply = api_get_setting('noreply_email_address');
+							if(!empty($noreply))
+							{
+								$replyto['Reply-to'] = $noreply;
+								$sender_name = $noreply;
+								$sender_email = $noreply;
+							}
+						}
+						api_mail_html($recipient_name, $recipient_email, $invitation_title, $full_invitation_text, $sender_name, $sender_email, $replyto);
+						//mail($recipient_email, strip_tags($invitation_title), strip_tags($invitation_text));
+						$counter++;
+					}
+				}
+			}
+		}
+	
+		return $counter;
+	}
+	
+	/**
+	 * This function recalculates the number of users who have been invited and updates the survey table with this value.
+	 *
+	 * @param	string	Survey code
+	 * @return	void
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function update_count_invited($survey_code)
+	{
+		// Database table definition
+		$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+		$table_survey 				= Database :: get_course_table(TABLE_SURVEY);
+	
+		// counting the number of people that are invited
+		$sql = "SELECT count(user) as total FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$row = Database::fetch_array($result);
+		$total_invited = $row['total'];
+	
+		// updating the field in the survey table
+		$sql = "UPDATE $table_survey SET invited = '".Database::escape_string($total_invited)."' WHERE code = '".Database::escape_string($survey_code)."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+	}
+	
+	/**
+	 * This function gets all the invited users for a given survey code.
+	 *
+	 * @param	string	Survey code
+	 * @return 	array	Array containing the course users and additional users (non course users)
+	 *
+	 * @todo consider making $defaults['additional_users'] also an array
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function get_invited_users($survey_code)
+	{
+		// Database table definition
+		$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+	
+		// Selecting all the invitations of this survey
+		$sql = "SELECT user FROM $table_survey_invitation WHERE survey_code='".Database::escape_string($survey_code)."'";
+	
+		$defaults = array();
+		$defaults['course_users'] = array();
+		$defaults['additional_users'] = '';
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		while ($row = Database::fetch_array($result))
+		{
+			if (is_numeric($row['user']))
+			{
+				$defaults['course_users'][] = $row['user'];
+			}
+			else
+			{
+				if (empty($defaults['additional_users']))
+				{
+					$defaults['additional_users'] = $row['user'];
+				}
+				else
+				{
+					$defaults['additional_users'] .= ';'.$row['user'];
+				}
+			}
+		}
+		return $defaults;
+	}
+	/**
+	 * Get all the invitations
+	 *
+	 * @param	string	Survey code
+	 * @return	array	Database rows matching the survey code
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version September 2007
+	 */
+	function get_invitations($survey_code)
+	{
+		// Database table definition
+		$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+	
+		$sql = "SELECT * FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$return = array();
+		while ($row = Database::fetch_array($result))
+		{
+			$return[$row['user']] = $row;
+		}
+		return $return;
+	}
+	/**
+	 * This function displays the form for searching a survey
+	 *
+	 * @return	void	(direct output)
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 *
+	 * @todo use quickforms
+	 * @todo consider moving this to surveymanager.inc.lib.php
+	 */
+	function display_survey_search_form()
+	{
+		echo '<form method="get" action="survey_list.php?search=advanced">';
+		echo '<table>
+				<tr>
+					<td>'.get_lang('Title').'</td>
+					<td><input type="text" name="keyword_title"/></td>
+				</tr>
+				<tr>
+					<td>'.get_lang('Code').'</td>
+					<td><input type="text" name="keyword_code"/></td>
+				</tr>
+				<tr>
+					<td>'.get_lang('Language').'</td>
+					<td>
+						<select name="keyword_language"><option value="%">'.get_lang('All').'</option>';
+		$languages = api_get_languages();
+		foreach ($languages['name'] as $index => $name)
+		{
+			echo '<option value="'.$languages['folder'][$index].'">'.$name.'</option>';
+		}
+		echo '			</select>
+					</td>
+				</tr>
+				<tr>
+					<td>&nbsp;</td>
+					<td><input type="submit" name="do_search" value="'.get_lang('Ok').'"/></td>
+				</tr>
+			</table>
+		</form>';
+	}
+	
+	/**
+	 * This function displays the sortable table with all the surveys
+	 *
+	 * @return	void	(direct output)
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function display_survey_list()
+	{
+		$parameters = array();
+		if ($_GET['do_search'])
+		{
+			$message = get_lang('DisplaySearchResults').'<br />';
+			$message .= '<a href="'.api_get_self().'">'.get_lang('DisplayAll').'</a>';
+			Display::display_normal_message($message, false);
+		}
+	
+		// Create a sortable table with survey-data
+		$table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data',2);
+		$table->set_additional_parameters($parameters);
+		$table->set_header(0, '', false);
+		$table->set_header(1, get_lang('SurveyName'));
+		$table->set_header(2, get_lang('SurveyCode'));
+		$table->set_header(3, get_lang('NumberOfQuestions'));
+		$table->set_header(4, get_lang('Author'));
+		$table->set_header(5, get_lang('Language'));
+		$table->set_header(6, get_lang('Shared'));
+		$table->set_header(7, get_lang('AvailableFrom'));
+		$table->set_header(8, get_lang('AvailableUntill'));
+		$table->set_header(9, get_lang('Invite'));
+		$table->set_header(10, get_lang('Anonymous'));
+		$table->set_header(11, get_lang('Modify'), false);
+		$table->set_column_filter(10, 'anonymous_filter');
+		$table->set_column_filter(11, 'modify_filter');
+		$table->set_form_actions(array ('delete' => get_lang('DeleteSurvey')));
+		$table->display();
+	
+	}
+	
+	/**
+	 * This function changes the modify column of the sortable table
+	 *
+	 * @param integer $survey_id the id of the survey
+	 * @return html code that are the actions that can be performed on any survey
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function modify_filter($survey_id)
+	{
+		global $charset;
+		$survey_id = Security::remove_XSS($survey_id);
+		$return = '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=edit&amp;survey_id='.$survey_id.'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
+		$return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("DeleteSurvey").'?',ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
+		//$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
+		//$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
+		$return .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>';
+		$return .= '<a href="survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('survey_publish.gif', get_lang('Publish')).'</a>';
+		$return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=empty&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("EmptySurvey").'?')).'\')) return false;">'.Display::return_icon('empty.gif', get_lang('EmptySurvey')).'</a>';
+		$return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
+		return $return;
+	}
+	/**
+	 * Returns "yes" when given parameter is one, "no" for any other value
+	 * @param	integer	Whether anonymous or not
+	 * @return	string	"Yes" or "No" in the current language
+	 */
+	function anonymous_filter($anonymous)
+	{
+		if ($anonymous == 1)
+		{
+			return get_lang('Yes');
+		}
+		else
+		{
+			return get_lang('No');
+		}
+	}
+	
+	
+	/**
+	 * This function handles the search restriction for the SQL statements
+	 *
+	 * @return	string	Part of a SQL statement or false on error
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function survey_search_restriction()
 	{
-		if ($counter == 1 AND $row['type'] == 'pagebreak')
+		if ($_GET['do_search'])
+		{
+			if ($_GET['keyword_title']<>'')
+			{
+				$search_term[] = 'title =\''.Database::escape_string($_GET['keyword_title']).'\'';
+			}
+			if ($_GET['keyword_code']<>'')
+			{
+				$search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\'';
+			}
+			if ($_GET['keyword_language']<>'%')
+			{
+				$search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\'';
+			}
+	
+			$search_restriction = implode(' AND ', $search_term);
+			return $search_restriction;
+		}
+		else
 		{
-			Display::display_error_message(get_lang('PagebreakNotFirst'), false);
-			$error = true;
+			return false;
 		}
-		if ($counter == $total AND $row['type'] == 'pagebreak')
+	}
+	/**
+	 * This function calculates the total number of surveys
+	 *
+	 * @return	integer	Total number of surveys
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function get_number_of_surveys()
+	{
+		global $table_survey;
+	
+		$search_restriction = SurveyUtil::survey_search_restriction();
+		if ($search_restriction)
 		{
-			Display::display_error_message(get_lang('PagebreakNotLast'), false);
-			$error = true;
-		}		
-		$counter++;
+			$search_restriction = 'WHERE '.$search_restriction;
+		}
+	
+		$sql = "SELECT count(survey_id) AS total_number_of_items FROM ".$table_survey.' '.$search_restriction;
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		$obj = Database::fetch_object($res);
+		return $obj->total_number_of_items;
+	
 	}
 	
-	if (!$continue AND $error)
+	/**
+	 * This function gets all the survey data that is to be displayed in the sortable table
+	 *
+	 * @param unknown_type $from
+	 * @param unknown_type $number_of_items
+	 * @param unknown_type $column
+	 * @param unknown_type $direction
+	 * @return unknown
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function get_survey_data($from, $number_of_items, $column, $direction)
 	{
-		Display::display_footer();
-		exit;
+		global $table_survey;
+		global $table_user;
+		global $table_survey_question;
+	
+		// searching
+		$search_restriction = SurveyUtil::survey_search_restriction();
+		if ($search_restriction)
+		{
+			$search_restriction = ' AND '.$search_restriction;
+		}
+		$sql = "SELECT
+					survey.survey_id							AS col0,
+	                CONCAT('<a href=\"survey.php?survey_id=',survey.survey_id,'\">',survey.title,'</a>')		AS col1,
+					survey.code									AS col2,
+					count(survey_question.question_id)			AS col3,
+	                CONCAT(user.firstname, ' ', user.lastname)	AS col4,
+	                survey.lang									AS col5,
+	                IF(is_shared<>0,'V','-')	 					AS col6,
+					survey.avail_from							AS col7,
+	                survey.avail_till							AS col8,
+	                CONCAT('<a href=\"survey_invitation.php?view=answered&amp;survey_id=',survey.survey_id,'\">',survey.answered,'</a> / <a href=\"survey_invitation.php?view=invited&amp;survey_id=',survey.survey_id,'\">',survey.invited, '</a>')	AS col9,
+	                survey.anonymous							AS col10,
+	                survey.survey_id							AS col11
+	             FROM $table_survey survey
+				 LEFT JOIN $table_survey_question survey_question ON survey.survey_id = survey_question.survey_id
+	             , $table_user user
+	             WHERE survey.author = user.user_id
+	             $search_restriction
+	             ";
+		$sql .= " GROUP BY survey.survey_id";
+		$sql .= " ORDER BY col$column $direction ";
+		$sql .= " LIMIT $from,$number_of_items";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		$surveys = array ();
+		while ($survey = Database::fetch_array($res))
+		{
+			$surveys[] = $survey;
+		}
+		return $surveys;
 	}
 }
 ?>

+ 2 - 2
main/survey/survey.php

@@ -21,7 +21,7 @@ Tel. +32 (2) 211 34 56
 *	@package dokeos.survey
 * 	@author unknown
 * 	@author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
-* 	@version $Id: survey.php 14636 2008-03-17 22:24:08Z yannoo $
+* 	@version $Id: survey.php 14639 2008-03-18 05:31:08Z yannoo $
 *
 * 	@todo use quickforms for the forms
 */
@@ -97,7 +97,7 @@ if (isset($_GET['message']))
 }
 
 // We exit here is the first or last question is a pagebreak (which causes errors)
-check_first_last_question($_GET['survey_id']);
+SurveyUtil::check_first_last_question($_GET['survey_id']);
 
 // Action links
 $survey_actions = get_lang('Survey').': ';

+ 3 - 73
main/survey/survey_invitation.php

@@ -41,7 +41,7 @@ require_once (api_get_path(LIBRARY_PATH)."mail.lib.inc.php");
 /** @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_header(get_lang('Survey'));
 	Display :: display_error_message(get_lang('NotAllowed'), false);
 	Display :: display_footer();
 	exit;
@@ -157,12 +157,6 @@ echo '</table>';
 
 // Footer
 Display :: display_footer();
-
-
-
-
-
-
 /**
  * @todo add the additional parameters
  */
@@ -173,70 +167,6 @@ $table->set_header(0, get_lang('User'));
 $table->set_header(1, get_lang('InvitationCode'));
 $table->set_header(2, get_lang('InvitationDate'));
 $table->set_header(3, get_lang('Answered'));
-$table->set_column_filter(3, 'modify_filter');
+$table->set_column_filter(3, 'SurveyUtil::modify_filter');
 $table->display();
-*/
-
-/**
- * Get all the information about the invitations of a certain survey
- *
- * @return unknown
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- *
- * @todo use survey_id parameter instead of $_GET
- */
-function get_survey_invitations_data()
-{
-	// Database table definition
-	$table_survey_invitation 		= Database :: get_course_table(TABLE_SURVEY_INVITATION);
-	$table_user 					= Database :: get_main_table(TABLE_MAIN_USER);
-
-	$sql = "SELECT
-				survey_invitation.user as col1,
-				survey_invitation.invitation_code as col2,
-				survey_invitation.invitation_date as col3,
-				'' as col4
-				FROM $table_survey_invitation survey_invitation
-		LEFT JOIN $table_user user ON  survey_invitation.user = user.user_id
-		WHERE survey_invitation.survey_id = '".Database::escape_string($_GET['survey_id'])."'";
-	$res = api_sql_query($sql, __FILE__, __LINE__);
-	while ($row = mysql_fetch_array($res))
-	{
-		$survey_invitation_data[] = $row;
-	}
-	return $survey_invitation_data;
-}
-
-/**
- * Get the total number of survey invitations for a given survey (through $_GET['survey_id'])
- *
- * @return unknown
- *
- * @todo use survey_id parameter instead of $_GET
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function get_number_of_survey_invitations()
-{
-	// Database table definition
-	$table_survey_invitation 		= Database :: get_course_table(TABLE_SURVEY_INVITATION);
-
-	$sql = "SELECT count(user) AS total FROM $table_survey_invitation WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'";
-	$res = api_sql_query($sql, __FILE__, __LINE__);
-	$row = mysql_fetch_assoc($res);
-	return $row['total'];
-}
-/**
- * @todo use global array for answered or not
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function modify_filter()
-{
-
-}
-?>
+*/

+ 6 - 250
main/survey/survey_invite.php

@@ -44,7 +44,7 @@ require_once (api_get_path(LIBRARY_PATH)."mail.lib.inc.php");
 /** @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_header(get_lang('Survey'));
 	Display :: display_error_message(get_lang('NotAllowed'), false);
 	Display :: display_footer();
 	exit;
@@ -139,9 +139,9 @@ if ($form->validate())
 {
 	$values = $form->exportValues();
 	// save the invitation mail
-	save_invite_mail($values['mail_text'], $values['resend_to_all']);
+	SurveyUtil::save_invite_mail($values['mail_text'], $values['resend_to_all']);
 	// saving the invitations for the course users
-	$count_course_users = save_invitations($values['course_users'], $values['mail_title'], $values['mail_text'], $values['resend_to_all']);
+	$count_course_users = SurveyUtil::save_invitations($values['course_users'], $values['mail_title'], $values['mail_text'], $values['resend_to_all']);
 	// saving the invitations for the additional users
 	$values['additional_users'] = $values['additional_users'].';'; 	// this is for the case when you enter only one email
 	$temp = str_replace(',',';',$values['additional_users']);		// this is to allow , and ; as email separators
@@ -150,16 +150,16 @@ if ($form->validate())
 	{
 		$additional_users[$i] = trim($additional_users[$i]);
 	}
-	$counter_additional_users = save_invitations($additional_users, $values['mail_title'], $values['mail_text'], $values['resend_to_all']);
+	$counter_additional_users = SurveyUtil::save_invitations($additional_users, $values['mail_title'], $values['mail_text'], $values['resend_to_all']);
 	// updating the invited field in the survey table
-	update_count_invited($survey_data['code']);
+	SurveyUtil::update_count_invited($survey_data['code']);
 	$total_count = $count_course_users + $counter_additional_users;
 	Display :: display_confirmation_message($total_count.' '.get_lang('InvitationsSend'), false);
 }
 else
 {
 	// getting the invited users
-	$defaults = get_invited_users($survey_data['code']);
+	$defaults = SurveyUtil::get_invited_users($survey_data['code']);
 	// getting the survey mail text
 	if (!empty($survey_data['reminder_mail']))
 	{
@@ -179,249 +179,5 @@ Display :: display_footer();
 
 
 
-/**
- * Save the invitation mail
- *
- * @param string 	Text of the e-mail
- * @param integer	Whether the mail contents are for invite mail (0, default) or reminder mail (1)
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function save_invite_mail($mailtext, $reminder=0)
-{
-	// Database table definition
-	$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
-
-	// reminder or not
-	if ($reminder == 0)
-	{
-		$mail_field = 'invite_mail';
-	}
-	else
-	{
-		$mail_field = 'reminder_mail';
-	}
-
-	$sql = "UPDATE $table_survey SET $mail_field = '".Database::escape_string($mailtext)."' WHERE survey_id = '".$_GET['survey_id']."'";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-}
 
-/**
- * This function saves all the invitations of course users and additional users in the
- *
- * @param unknown_type $users_array
- * @param string $invitation_title the title of the invitation is used as the title of the mail
- * @param string $invitation_text the text of the invitation is used as the text of the mail.
- * 				 The $invitation_text has to contain a **link** string or this will automatically be added to the end
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- *
- * @todo create the survey link
- */
-function save_invitations($users_array, $invitation_title, $invitation_text, $reminder=0)
-{
-	global $_user;
-	global $_course;
-	global $_configuration;
-
-	// getting the survey information
-	$survey_data = survey_manager::get_survey($_GET['survey_id']);
-
-	// Database table definition
-	$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
-	$table_user 				= Database :: get_main_table(TABLE_MAIN_USER);
-
-	$already_invited = array();
-	$survey_invitations = array();
-	// get the people who are already invited
-	if ($reminder == 1)
-	{
-		$survey_invitations = get_invitations($survey_data['survey_code']);
-	}
-	else
-	{
-		$already_invited = get_invited_users($survey_data['code']);
-	}
-
-	$counter = 0;
-	if (is_array($users_array))
-	{
-		foreach ($users_array as $key=>$value)
-		{
-			if(isset($value))
-			{
-				// generating the unique code
-				if(is_array($survey_invitations))
-				{
-					if ($reminder == 1 AND array_key_exists($value,$survey_invitations))
-					{
-						$invitation_code = $survey_invitations[$value]['invitation_code'];
-					}
-					else 
-					{
-						$invitation_code = md5($value.microtime());
-					}
-				}
-				$survey_link = '';
-				$full_invitation_text= '';
-							
-				// storing the invitation (only if the user_id is not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
-				$add_users_array = explode(';',$already_invited['additional_users']);
-				if ((is_numeric($value) AND !in_array($value,$already_invited['course_users'])) OR (!is_numeric($value) AND !in_array($value,$add_users_array)))
-				{
-					if(is_array($survey_invitations))
-					{
-						if (!array_key_exists($value,$survey_invitations))
-						{
-							$sql = "INSERT INTO $table_survey_invitation (user, survey_code, invitation_code, invitation_date) VALUES
-										('".Database::escape_string($value)."','".Database::escape_string($survey_data['code'])."','".Database::escape_string($invitation_code)."','".Database::escape_string(date('Y-m-d H:i:s'))."')";
-							$result = api_sql_query($sql, __FILE__, __LINE__);
-						}
-					}
-					// replacing the **link** part with a valid link for the user
-					$survey_link = $_configuration['root_web'].$_configuration['code_append'].'survey/'.'fillsurvey.php?course='.$_course['sysCode'].'&invitationcode='.$invitation_code;
-					$text_link = '<a href="'.$survey_link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n".get_lang('OrCopyPasteTheFollowingUrl')." <br />\r\n ".$survey_link;
-					
-					$replace_count = 0;
-					$full_invitation_text = str_ireplace('**link**', $text_link ,$invitation_text, $replace_count);
-					if ($replace_count < 1)
-					{
-						$full_invitation_text = $full_invitation_text . "<br />\r\n<br />\r\n".$text_link;
-					}
-					
-	
-					// optionally: finding the e-mail of the course user
-					if (is_numeric($value))
-					{
-						$sql = "SELECT firstname, lastname, email FROM $table_user WHERE user_id='".Database::escape_string($value)."'";
-						$result = api_sql_query($sql, __FILE__, __LINE__);
-						$row = mysql_fetch_assoc($result);
-						$recipient_email = $row['email'];
-						$recipient_name = $row['firstname'].' '.$row['lastname'];
-					}
-					else
-					{
-						/** @todo check if the $value is a valid email	 */
-						$recipient_email = $value;
-					}
-	
-					// sending the mail
-					$sender_name  = $_user['firstName'].' '.$_user['lastName'];
-					$sender_email = $_user['mail'];
-					
-					$replyto = array();
-					if(api_get_setting('survey_email_sender_noreply')=='noreply')
-					{
-						$noreply = api_get_setting('noreply_email_address');
-						if(!empty($noreply))
-						{
-							$replyto['Reply-to'] = $noreply;
-							$sender_name = $noreply;
-							$sender_email = $noreply;
-						}
-					}
-					api_mail_html($recipient_name, $recipient_email, $invitation_title, $full_invitation_text, $sender_name, $sender_email, $replyto);
-					//mail($recipient_email, strip_tags($invitation_title), strip_tags($invitation_text));
-					$counter++;
-				}
-			}
-		}
-	}
-
-	return $counter;
-}
-
-/**
- * This function recalculates the number of users who have been invited and updates the survey table with this value.
- *
- * @param unknown_type $survey_id
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function update_count_invited($survey_code)
-{
-	// Database table definition
-	$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
-	$table_survey 				= Database :: get_course_table(TABLE_SURVEY);
-
-	// counting the number of people that are invited
-	$sql = "SELECT count(user) as total FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-	$row = mysql_fetch_assoc($result);
-	$total_invited = $row['total'];
-
-	// updating the field in the survey table
-	$sql = "UPDATE $table_survey SET invited = '".Database::escape_string($total_invited)."' WHERE code = '".Database::escape_string($survey_code)."'";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-}
-
-/**
- * This function gets all the invited users for a given survey code.
- *
- * @param unknown_type $survey_id
- * @return array containing the course users and additional users (non course users)
- *
- * @todo consider making $defaults['additional_users'] also an array
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function get_invited_users($survey_code)
-{
-	// Database table definition
-	$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
-
-	// Selecting all the invitations of this survey
-	$sql = "SELECT user FROM $table_survey_invitation WHERE survey_code='".Database::escape_string($survey_code)."'";
-
-	$defaults = array();
-	$defaults['course_users'] = array();
-	$defaults['additional_users'] = '';
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-	while ($row = mysql_fetch_assoc($result))
-	{
-		if (is_numeric($row['user']))
-		{
-			$defaults['course_users'][] = $row['user'];
-		}
-		else
-		{
-			if (empty($defaults['additional_users']))
-			{
-				$defaults['additional_users'] = $row['user'];
-			}
-			else
-			{
-				$defaults['additional_users'] .= ';'.$row['user'];
-			}
-		}
-	}
-	return $defaults;
-}
-/**
- * get all the invitations
- *
- * @param unknown_type $survey_code
- * @return array 
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version September 2007
- */
-function get_invitations($survey_code)
-{
-	// Database table definition
-	$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
-
-	$sql = "SELECT * FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
-	$return = array();
-	while ($row = mysql_fetch_assoc($result))
-	{
-		$return[$row['user']] = $row;
-	}
-	return $return;
-}
 ?>

+ 10 - 229
main/survey/survey_list.php

@@ -21,7 +21,7 @@
 *	@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: survey_list.php 13922 2007-12-04 23:20:19Z yannoo $
+* 	@version $Id: survey_list.php 14639 2008-03-18 05:31:08Z yannoo $
 *
 * 	@todo use quickforms for the forms
 */
@@ -40,7 +40,7 @@ require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
 /** @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_header(get_lang('Survey'));
 	Display :: display_error_message(get_lang('NotAllowed'), false);
 	Display :: display_footer();
 	exit;
@@ -70,7 +70,7 @@ Display :: display_header($tool_name);
 // Action handling: searching
 if (isset ($_GET['search']) AND $_GET['search'] == 'advanced')
 {
-	display_survey_search_form();
+	SurveyUtil::display_survey_search_form();
 }
 // Action handling: deleting a survey
 if (isset($_GET['action']) AND $_GET['action'] == 'delete' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
@@ -138,244 +138,25 @@ echo '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=add">'.get_
 echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;search=advanced">'.get_lang('Search').'</a>';
 
 // Main content
-display_survey_list();
+SurveyUtil::display_survey_list();
 
 // Footer
 Display :: display_footer();
 
-
-
-/**
- * This function displays the form for searching a survey
- *
- * @return html code
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- *
- * @todo use quickforms
- * @todo consider moving this to surveymanager.inc.lib.php
- */
-function display_survey_search_form()
-{
-	echo '<form method="get" action="survey_list.php?search=advanced">';
-	echo '<table>
-			<tr>
-				<td>'.get_lang('Title').'</td>
-				<td><input type="text" name="keyword_title"/></td>
-			</tr>
-			<tr>
-				<td>'.get_lang('Code').'</td>
-				<td><input type="text" name="keyword_code"/></td>
-			</tr>
-			<tr>
-				<td>'.get_lang('Language').'</td>
-				<td>
-					<select name="keyword_language"><option value="%">'.get_lang('All').'</option>';
-	$languages = api_get_languages();
-	foreach ($languages['name'] as $index => $name)
-	{
-		echo '<option value="'.$languages['folder'][$index].'">'.$name.'</option>';
-	}
-	echo '			</select>
-				</td>
-			</tr>
-			<tr>
-				<td>&nbsp;</td>
-				<td><input type="submit" name="do_search" value="'.get_lang('Ok').'"/></td>
-			</tr>
-		</table>
-	</form>';
-}
-
-/**
- * This function displays the sortable table with all the surveys
- *
- * @return html code
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function display_survey_list()
-{
-	$parameters = array();
-	if ($_GET['do_search'])
-	{
-		$message = get_lang('DisplaySearchResults').'<br />';
-		$message .= '<a href="'.api_get_self().'">'.get_lang('DisplayAll').'</a>';
-		Display::display_normal_message($message, false);
-	}
-
-	// Create a sortable table with survey-data
-	$table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data',2);
-	$table->set_additional_parameters($parameters);
-	$table->set_header(0, '', false);
-	$table->set_header(1, get_lang('SurveyName'));
-	$table->set_header(2, get_lang('SurveyCode'));
-	$table->set_header(3, get_lang('NumberOfQuestions'));
-	$table->set_header(4, get_lang('Author'));
-	$table->set_header(5, get_lang('Language'));
-	$table->set_header(6, get_lang('Shared'));
-	$table->set_header(7, get_lang('AvailableFrom'));
-	$table->set_header(8, get_lang('AvailableUntill'));
-	$table->set_header(9, get_lang('Invite'));
-	$table->set_header(10, get_lang('Anonymous'));
-	$table->set_header(11, get_lang('Modify'), false);
-	$table->set_column_filter(10, 'anonymous_filter');
-	$table->set_column_filter(11, 'modify_filter');
-	$table->set_form_actions(array ('delete' => get_lang('DeleteSurvey')));
-	$table->display();
-
-}
-
-/**
- * This function calculates the total number of surveys
- *
- * @return integer
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
+/* Bypass functions to make direct use from SortableTable possible */
 function get_number_of_surveys()
 {
-	global $table_survey;
-
-	$search_restriction = survey_search_restriction();
-	if ($search_restriction)
-	{
-		$search_restriction = 'WHERE '.$search_restriction;
-	}
-
-	$sql = "SELECT count(survey_id) AS total_number_of_items FROM ".$table_survey.' '.$search_restriction;
-	$res = api_sql_query($sql, __FILE__, __LINE__);
-	$obj = mysql_fetch_object($res);
-	return $obj->total_number_of_items;
-
+	return SurveyUtil::get_number_of_surveys();
 }
-
-/**
- * This function gets all the survey data that is to be displayed in the sortable table
- *
- * @param unknown_type $from
- * @param unknown_type $number_of_items
- * @param unknown_type $column
- * @param unknown_type $direction
- * @return unknown
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
 function get_survey_data($from, $number_of_items, $column, $direction)
 {
-	global $table_survey;
-	global $table_user;
-	global $table_survey_question;
-
-	// searching
-	$search_restriction = survey_search_restriction();
-	if ($search_restriction)
-	{
-		$search_restriction = ' AND '.$search_restriction;
-	}
-	$sql = "SELECT
-				survey.survey_id							AS col0,
-                CONCAT('<a href=\"survey.php?survey_id=',survey.survey_id,'\">',survey.title,'</a>')		AS col1,
-				survey.code									AS col2,
-				count(survey_question.question_id)			AS col3,
-                CONCAT(user.firstname, ' ', user.lastname)	AS col4,
-                survey.lang									AS col5,
-                IF(is_shared<>0,'V','-')	 					AS col6,
-				survey.avail_from							AS col7,
-                survey.avail_till							AS col8,
-                CONCAT('<a href=\"survey_invitation.php?view=answered&amp;survey_id=',survey.survey_id,'\">',survey.answered,'</a> / <a href=\"survey_invitation.php?view=invited&amp;survey_id=',survey.survey_id,'\">',survey.invited, '</a>')	AS col9,
-                survey.anonymous							AS col10,
-                survey.survey_id							AS col11
-             FROM $table_survey survey
-			 LEFT JOIN $table_survey_question survey_question ON survey.survey_id = survey_question.survey_id
-             , $table_user user
-             WHERE survey.author = user.user_id
-             $search_restriction
-             ";
-	$sql .= " GROUP BY survey.survey_id";
-	$sql .= " ORDER BY col$column $direction ";
-	$sql .= " LIMIT $from,$number_of_items";
-	$res = api_sql_query($sql, __FILE__, __LINE__);
-	$surveys = array ();
-	while ($survey = mysql_fetch_row($res))
-	{
-		$surveys[] = $survey;
-	}
-	return $surveys;
+	return SurveyUtil::get_survey_data($from, $number_of_items, $column, $direction);
 }
-
-
-/**
- * This function changes the modify column of the sortable table
- *
- * @param integer $survey_id the id of the survey
- * @return html code that are the actions that can be performed on any survey
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
 function modify_filter($survey_id)
 {
-	global $charset;
-	$return = '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=edit&amp;survey_id='.$survey_id.'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
-	$return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("DeleteSurvey").'?',ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
-	//$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
-	//$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
-	$return .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>';
-	$return .= '<a href="survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('survey_publish.gif', get_lang('Publish')).'</a>';
-	$return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=empty&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("EmptySurvey").'?')).'\')) return false;">'.Display::return_icon('empty.gif', get_lang('EmptySurvey')).'</a>';
-	$return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
-	return $return;
+	return SurveyUtil::modify_filter($survey_id);
 }
-
 function anonymous_filter($anonymous)
 {
-	if ($anonymous == 1)
-	{
-		return get_lang('Yes');
-	}
-	else
-	{
-		return get_lang('No');
-	}
-}
-
-
-/**
- * this function handles the search restriction for the SQL statements
- *
- * @return false or a part of a SQL statement
- *
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
- * @version January 2007
- */
-function survey_search_restriction()
-{
-	if ($_GET['do_search'])
-	{
-		if ($_GET['keyword_title']<>'')
-		{
-			$search_term[] = 'title =\''.Database::escape_string($_GET['keyword_title']).'\'';
-		}
-		if ($_GET['keyword_code']<>'')
-		{
-			$search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\'';
-		}
-		if ($_GET['keyword_language']<>'%')
-		{
-			$search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\'';
-		}
-
-		$search_restriction = implode(' AND ', $search_term);
-		return $search_restriction;
-	}
-	else
-	{
-		return false;
-	}
-}
-?>
+	return SurveyUtil::anonymous_filter($anonymous);
+}