Эх сурвалжийг харах

[svn r10942] new survey (unfinished)

Patrick Cool 18 жил өмнө
parent
commit
a399342211

+ 219 - 0
main/survey/fillsurvey.php

@@ -0,0 +1,219 @@
+<?php
+/*
+    DOKEOS - elearning and course management software
+
+    For a full list of contributors, see documentation/credits.html
+
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    as published by the Free Software Foundation; either version 2
+    of the License, or (at your option) any later version.
+    See "documentation/licence.html" more details.
+
+    Contact:
+		Dokeos
+		Rue des Palais 44 Paleizenstraat
+		B-1030 Brussels - Belgium
+		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_list.php 10680 2007-01-11 21:26:23Z pcool $
+*
+* 	@todo The ansTarget column is not done
+* 	@todo try to understand the white, blue, ... template stuff.
+* 	@todo use quickforms for the forms
+*/
+
+// name of the language file that needs to be included
+$language_file = 'survey';
+
+// including the global dokeos file
+require ('../inc/global.inc.php');
+
+// including additional libraries
+//require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
+require_once('survey.lib.php');
+
+// Database table definitions
+$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
+$table_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+$table_course 					= Database :: get_main_table(TABLE_MAIN_COURSE);
+$table_user 					= Database :: get_main_table(TABLE_MAIN_USER);
+$table_survey_invitation 		= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+
+// breadcrumbs
+// $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
+
+// Header
+Display :: display_header(get_lang('Survey'));
+
+// debug
+/*
+echo '<pre>';
+print_r($_POST);
+echo '</pre>';
+*/
+
+
+// first we check if the needed parameters are present
+if (!isset($_GET['course']) OR !isset($_GET['invitationcode']))
+{
+	Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'));
+	Display :: display_footer();
+	exit;
+}
+
+// now we check if the invitationcode is valid
+$sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '".mysql_real_escape_string($_GET['invitationcode'])."'";
+$result = api_sql_query($sql, __FILE__, __LINE__);
+if (mysql_num_rows($result) < 1)
+{
+	Display :: display_error_message(get_lang('WrongInvitationCode'));
+	Display :: display_footer();
+	exit;
+}
+$survey_invitation = mysql_fetch_assoc($result);
+
+
+// storing the answers
+if ($_POST)
+{
+	foreach ($_POST as $key=>$value)
+	{
+		if (strstr($key,'question'))
+		{
+			$survey_question_id = str_replace('question', '',$key);
+			if (is_array($value))
+			{
+				foreach ($value as $answer_key => $answer_value)
+				{
+					store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $answer_value);
+				}
+			}
+			else // multipleresponse
+			{
+				$survey_question_answer = $value;
+				store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value);
+			}
+		}
+	}
+}
+
+// survey information
+$survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
+echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
+echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
+
+// displaying the survey introduction
+if (!isset($_GET['show']))
+{
+	echo '<div id="survey_content">'.$survey_data['survey_introduction'].'</div>';
+	$limit = 0;
+}
+
+// displaying the survey thanks message
+if ($_POST['finish_survey'])
+{
+	echo '<div id="survey_content"><strong>'.get_lang('SurveyFinished').'</strong>'.$survey_data['survey_thanks'].'</div>';
+	Display :: display_footer();
+	exit;
+}
+
+if (isset($_GET['show']))
+{
+	// Getting all the questions for this page
+	$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 = '".mysql_real_escape_string($survey_invitation['survey_id'])."'
+			ORDER BY survey_question.sort ASC";
+	if ($_GET['show'])
+	{
+			$sql .= ' LIMIT '.($_GET['show']+1).',1000';
+	}
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+	$question_counter_max = mysql_num_rows($result);
+	$counter = 0;
+	while ($row = mysql_fetch_assoc($result))
+	{
+		// if the type is not a pagebreak we store it in the $questions array
+		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']]['options'][$row['option_sort']] = $row['option_text'];
+		}
+		// if the type is a pagebreak we are finished loading the questions for this page
+		else
+		{
+			$limit = $counter;
+			break;
+		}
+		$counter++;
+	}
+}
+
+// Displaying the form with the questions
+echo '<form id="question" name="question" method="post" action="'.$_SERVER['PHP_SELF'].'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&show='.$limit.'">';
+foreach ($questions as $key=>$question)
+{
+	$display = new $question['type'];
+	$display->render_question($question);
+}
+
+if (($limit AND $limit <> $question_counter_max) OR !$_GET['show'])
+{
+	//echo '<a href="'.$_SERVER['PHP_SELF'].'?survey_id='.$survey_invitation['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
+	echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
+}
+if (!$limit AND $_GET['show'])
+{
+	echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
+}
+echo '</form>';
+
+/*
+echo '<pre>';
+print_r($questions);
+echo '</pre>';
+*/
+
+// Footer
+Display :: display_footer();
+
+
+/**
+ * This function stores an answer on 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 januari 2007
+ */
+function store_answer($user, $survey_id, $question_id, $option_id)
+{
+	// table definition
+	$table_survey_answer 		= Database :: get_course_table(TABLE_SURVEY_ANSWER);
+
+	$sql = "INSERT INTO $table_survey_answer (user, survey_id, question_id, option_id) VALUES (
+			'".mysql_real_escape_string($user)."',
+			'".mysql_real_escape_string($survey_id)."',
+			'".mysql_real_escape_string($question_id)."',
+			'".mysql_real_escape_string($option_id)."'
+			)";
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+}
+?>

+ 161 - 0
main/survey/preview.php

@@ -0,0 +1,161 @@
+<?php
+/*
+    DOKEOS - elearning and course management software
+
+    For a full list of contributors, see documentation/credits.html
+
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    as published by the Free Software Foundation; either version 2
+    of the License, or (at your option) any later version.
+    See "documentation/licence.html" more details.
+
+    Contact:
+		Dokeos
+		Rue des Palais 44 Paleizenstraat
+		B-1030 Brussels - Belgium
+		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_list.php 10680 2007-01-11 21:26:23Z pcool $
+*
+* 	@todo The ansTarget column is not done
+* 	@todo try to understand the white, blue, ... template stuff.
+* 	@todo use quickforms for the forms
+*/
+
+// name of the language file that needs to be included
+$language_file = 'survey';
+
+// including the global dokeos file
+require ('../inc/global.inc.php');
+
+// including additional libraries
+//require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
+require_once('survey.lib.php');
+
+// Database table definitions
+$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
+$table_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+$table_course 					= Database :: get_main_table(TABLE_MAIN_COURSE);
+$table_user 					= Database :: get_main_table(TABLE_MAIN_USER);
+
+// breadcrumbs
+$interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
+
+// Header
+Display :: display_header(get_lang('SurveyPreview'));
+
+// debug
+/*
+echo '<pre>';
+print_r($_POST);
+echo '</pre>';
+*/
+
+// only a course admin is allowed to preview a survey: you are NOT a course admin => error message
+if (!api_is_allowed_to_edit())
+{
+	Display :: display_error_message(get_lang('NotAllowedHere'));
+}
+// only a course admin is allowed to preview a survey: you are a course admin
+else
+{
+	if (!isset($_GET['survey_id']) OR !is_numeric($_GET['survey_id']))
+	{
+		Display :: display_error_message(get_lang('InvallidSurvey'));
+	}
+
+	// survey information
+	$survey_data = survey_manager::get_survey($_GET['survey_id']);
+	echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
+	echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
+
+	// displaying the survey introduction
+	if (!isset($_GET['show']))
+	{
+		echo '<div id="survey_content">'.$survey_data['survey_introduction'].'</div>';
+		$limit = 0;
+	}
+
+	// displaying the survey thanks message
+	if ($_POST['finish_survey'])
+	{
+		echo '<div id="survey_content"><strong>'.get_lang('SurveyFinished').'</strong>'.$survey_data['survey_thanks'].'</div>';
+		Display :: display_footer();
+		exit;
+	}
+
+	if (isset($_GET['show']))
+	{
+		// Getting all the questions for this page
+		$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 = '".mysql_real_escape_string($_GET['survey_id'])."'
+				ORDER BY survey_question.sort ASC";
+		if ($_GET['show'])
+		{
+				$sql .= ' LIMIT '.($_GET['show']+1).',1000';
+		}
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$question_counter_max = mysql_num_rows($result);
+		$counter = 0;
+		while ($row = mysql_fetch_assoc($result))
+		{
+			// if the type is not a pagebreak we store it in the $questions array
+			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']]['options'][$row['option_sort']] = $row['option_text'];
+			}
+			// if the type is a pagebreak we are finished loading the questions for this page
+			else
+			{
+				$limit = $counter;
+				break;
+			}
+			$counter++;
+		}
+	}
+
+	// Displaying the form with the questions
+	echo '<form id="question" name="question" method="post" action="'.$_SERVER['PHP_SELF'].'?survey_id='.$_GET['survey_id'].'&show='.$limit.'">';
+	foreach ($questions as $key=>$question)
+	{
+		$display = new $question['type'];
+		$display->render_question($question);
+	}
+
+	if (($limit AND $limit <> $question_counter_max) OR !$_GET['show'])
+	{
+		//echo '<a href="'.$_SERVER['PHP_SELF'].'?survey_id='.$_GET['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
+		echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
+	}
+	if (!$limit AND $_GET['show'])
+	{
+		echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
+	}
+	echo '</form>';
+
+	/*
+	echo '<pre>';
+	print_r($questions);
+	echo '</pre>';
+	*/
+}
+
+// Footer
+Display :: display_footer();
+?>

+ 1028 - 0
main/survey/survey.lib.php

@@ -0,0 +1,1028 @@
+<?php
+/*
+    DOKEOS - elearning and course management software
+
+    For a full list of contributors, see documentation/credits.html
+
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    as published by the Free Software Foundation; either version 2
+    of the License, or (at your option) any later version.
+    See "documentation/licence.html" more details.
+
+    Contact:
+		Dokeos
+		Rue des Palais 44 Paleizenstraat
+		B-1030 Brussels - Belgium
+		Tel. +32 (2) 211 34 56
+*/
+
+/**
+*	@package dokeos.survey
+* 	@author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
+* 	@version $Id: survey.lib.php 10680 2007-01-11 21:26:23Z pcool $
+*
+* 	@todo move this file to inc/lib
+* 	@todo use consistent naming for the functions (save vs store for instance)
+*/
+
+class survey_manager
+{
+
+	/******************************************************************************************************
+											SURVEY FUNCTIONS
+	 *****************************************************************************************************/
+
+	/**
+	 * This function retrieves all the survey information
+	 *
+	 * @param integer $survey_id the id of the survey
+	 * @return array
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo move this function to surveymanager.inc.lib.php
+	 * @todo this is the same function as in create_new_survey.php
+	 */
+	function get_survey($survey_id)
+	{
+		$tbl_survey = Database :: get_course_table(TABLE_SURVEY);
+
+		$sql = "SELECT * FROM $tbl_survey WHERE survey_id='".mysql_real_escape_string($survey_id)."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$return = mysql_fetch_assoc($result);
+
+		// we do this (temporarily) to have the array match the quickform elements immediately
+		// idealiter the fields in the db match the quickform fields
+		$return['survey_code'] 			= $return['code'];
+		$return['survey_title'] 		= $return['title'];
+		$return['survey_subtitle'] 		= $return['subtitle'];
+		$return['survey_language'] 		= $return['lang'];
+		$return['start_date'] 			= $return['avail_from'];
+		$return['end_date'] 			= $return['avail_till'];
+		$return['survey_share'] 		= $return['is_shared'];
+		$return['survey_introduction'] 	= $return['intro'];
+		$return['survey_thanks'] 		= $return['surveythanks'];
+		return $return;
+	}
+
+	/**
+	 * This function stores a survey in the database
+	 *
+	 * @param array $values
+	 * @return array $return the type of return message that has to be displayed and the message in it
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo move this function to surveymanager.inc.lib.php
+	 */
+	function store_survey($values)
+	{
+		global $_user;
+
+		// table defnitions
+		$table_survey 		= Database :: get_course_table(TABLE_SURVEY);
+
+		if (!$values['survey_id'] OR !is_numeric($values['survey_id']))
+		{
+			$sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date) VALUES (
+						'".mysql_real_escape_string($values['survey_code'])."',
+						'".mysql_real_escape_string($values['survey_title'])."',
+						'".mysql_real_escape_string($values['survey_subtitle'])."',
+						'".mysql_real_escape_string($_user['user_id'])."',
+						'".mysql_real_escape_string($values['survey_language'])."',
+						'".mysql_real_escape_string($values['start_date'])."',
+						'".mysql_real_escape_string($values['end_date'])."',
+						'".mysql_real_escape_string($values['survey_share']['survey_share'])."',
+						'".mysql_real_escape_string('template')."',
+						'".mysql_real_escape_string($values['survey_introduction'])."',
+						'".mysql_real_escape_string($values['survey_thanks'])."',
+						'".date()."')";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			$survey_id = mysql_insert_id();
+
+			$return['message'] = get_lang('SurveyCreatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
+			$return['message'] .= '<a href="survey.php?survey_id='.$survey_id.'">'.get_lang('ClickHere').'</a>';
+			$return['type'] = 'confirmation';
+		}
+		else
+		{
+			$sql = "UPDATE $table_survey SET
+							code 			= '".mysql_real_escape_string($values['survey_code'])."',
+							title 			= '".mysql_real_escape_string($values['survey_title'])."',
+							subtitle 		= '".mysql_real_escape_string($values['survey_subtitle'])."',
+							author 			= '".mysql_real_escape_string($_user['user_id'])."',
+							lang 			= '".mysql_real_escape_string($values['survey_language'])."',
+							avail_from 		= '".mysql_real_escape_string($values['start_date'])."',
+							avail_till		= '".mysql_real_escape_string($values['end_date'])."',
+							is_shared		= '".mysql_real_escape_string($values['survey_share']['survey_share'])."',
+							template 		= '".mysql_real_escape_string('template')."',
+							intro			= '".mysql_real_escape_string($values['survey_introduction'])."',
+							surveythanks	= '".mysql_real_escape_string($values['survey_thanks'])."'
+					WHERE survey_id = '".mysql_real_escape_string($values['survey_id'])."'";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+
+			$return['message'] = get_lang('SurveyUpdatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
+			$return['message'] .= '<a href="survey.php?survey_id='.$values['survey_id'].'">'.get_lang('Here').'</a>';
+			$return['message'] .= get_lang('OrReturnToSurveyOverview').'<a href="survey_list.php">'.get_lang('Here').'</a>';
+			$return['type'] = 'confirmation';
+		}
+
+		return $return;
+	}
+
+	/**
+	 * This function deletes a survey (and also all the question in that survey
+	 *
+	 * @param $survey_id the id of the survey that has to be deleted
+	 * @return true
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function delete_survey($survey_id)
+	{
+		// table definitions
+		$table_survey 			= Database :: get_course_table(TABLE_SURVEY);
+
+		// deleting the survey
+		$sql = "DELETE from $table_survey WHERE survey_id='".mysql_real_escape_string($survey_id)."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+
+		// deleting the questions of the survey
+		survey_manager::delete_all_survey_questions($survey_id);
+
+		return true;
+	}
+
+	/******************************************************************************************************
+										SURVEY QUESTION FUNCTIONS
+	 *****************************************************************************************************/
+
+
+	/**
+	 * This function retrieves all the information of a question
+	 *
+	 * @param integer $question_id the id of the question
+	 * @return array
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo write the function
+	 * @todo move to surveymanager.lib.php
+	 * @todo use table constants
+	 */
+	function get_question($question_id)
+	{
+		// table definitions
+		$tbl_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+
+		// getting the information of the question
+		$sql = "SELECT * FROM $tbl_survey_question WHERE question_id='".mysql_real_escape_string($question_id)."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$row = mysql_fetch_assoc($result);
+		$return['survey_id'] 	= $row['survey_id'];
+	    $return['question_id'] 	= $row['question_id'];
+	    $return['type'] 		= $row['type'];
+	    $return['question'] 	= $row['survey_question'];
+	    $return['horizontalvertical'] 	= $row['display'];
+
+	    // getting the information of the question options
+		$sql = "SELECT * FROM $table_survey_question_option WHERE question_id='".mysql_real_escape_string($question_id)."'";
+		echo $sql;
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		while ($row = mysql_fetch_assoc($result))
+		{
+			$return['answers'][] = $row['option_text'];
+		}
+		return $return;
+	}
+
+	/**
+	 * This function saves a question in the database.
+	 * This can be either an update of an existing survey or storing a new survey
+	 *
+	 * @param integer $question_id the id of the question (used to determine weither it is a update or an insert)
+	 * @param integer $survey_id the id of the survey
+	 * @param string $question_title the question itself
+	 * @param string $question_comment the comment of the question (not yet in use)
+	 * @param string $question_display how the options of the questions should be displayed
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo move to surveymanager.lib.php
+	 * @todo return message, type and id (and display the information)
+	 */
+	function save_question($form_content)
+	{
+		// table definitions
+		$table_survey 			= Database :: get_course_table(TABLE_SURVEY);
+		$tbl_survey_question 	= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+
+		// storing a new question
+		if ($form_content['question_id'] == '' OR !is_numeric($form_content['question_id']))
+		{
+			// finding the max sort order of the questions in the given survey
+			$sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question WHERE survey_id='".mysql_real_escape_string($form_content['survey_id'])."'";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			$row = mysql_fetch_assoc($result);
+			$max_sort = $row['max_sort'];
+
+			// adding the question to the survey_question table
+			$sql = "INSERT INTO $tbl_survey_question (survey_id,survey_question,survey_question_comment,type,display, sort) VALUES (
+						'".mysql_real_escape_string($form_content['survey_id'])."',
+						'".mysql_real_escape_string($form_content['question'])."',
+						'".mysql_real_escape_string($form_content['question_comment'])."',
+						'".mysql_real_escape_string($form_content['type'])."',
+						'".mysql_real_escape_string($form_content['horizontalvertical'])."',
+						'".mysql_real_escape_string($max_sort+1)."')";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			$question_id = mysql_insert_id();
+			$form_content['question_id'] = $question_id;
+			$return_message = get_lang('QuestionAdded');
+		}
+		// updating an existing question
+		else
+		{
+			// adding the question to the survey_question table
+			$sql = "UPDATE $tbl_survey_question SET
+						survey_question = '".mysql_real_escape_string($form_content['question'])."',
+						survey_question_comment = '".mysql_real_escape_string($form_content['question_comment'])."',
+						display = '".mysql_real_escape_string($form_content['horizontalvertical'])."'
+						WHERE question_id = '".mysql_real_escape_string($form_content['question_id'])."'";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			$return_message = get_lang('QuestionUpdated');
+		}
+
+		// storing the options of the question
+		survey_manager::save_question_options($form_content);
+		return $return_message;
+	}
+
+	/**
+	 * This functions moves a question of a survey up or down
+	 *
+	 * @param unknown_type $direction
+	 * @param unknown_type $survey_question_id
+	 * @param unknown_type $survey_id
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function move_survey_question($direction, $survey_question_id, $survey_id)
+	{
+		// table definition
+		$table_survey_question 	= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+
+		if ($direction == 'moveup')
+		{
+			$sort = 'DESC';
+		}
+		if ($direction == 'movedown')
+		{
+			$sort = 'ASC';
+		}
+
+		// finding the two questions that needs to be swapped
+		$sql = "SELECT * FROM $table_survey_question WHERE survey_id='".mysql_real_escape_string($survey_id)."' ORDER BY sort $sort";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		while ($row = mysql_fetch_assoc($result))
+		{
+			if ($found == true)
+			{
+				$question_id_two = $row['question_id'];
+				$question_sort_two = $row['sort'];
+				$found = false;
+			}
+			if ($row['question_id'] == $survey_question_id)
+			{
+				$found = true;
+				$question_id_one = $row['question_id'];
+				$question_sort_one = $row['sort'];
+			}
+		}
+
+		$sql1 = "UPDATE $table_survey_question SET sort = '".mysql_real_escape_string($question_sort_two)."' WHERE question_id='".mysql_real_escape_string($question_id_one)."'";
+		$result = api_sql_query($sql1, __FILE__, __LINE__);
+		$sql2 = "UPDATE $table_survey_question SET sort = '".mysql_real_escape_string($question_sort_one)."' WHERE question_id='".mysql_real_escape_string($question_id_two)."'";
+		$result = api_sql_query($sql2, __FILE__, __LINE__);
+	}
+
+
+	/**
+	 * This function deletes all the questions of a given survey
+	 * This function is normally only called when a survey is deleted
+	 *
+	 * @param $survey_id the id of the survey that has to be deleted
+	 * @return true
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function delete_all_survey_questions($survey_id)
+	{
+		// table definitions
+		$table_survey_question 	= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+
+		// deleting the survey questions
+		$sql = "DELETE from $table_survey_question WHERE survey_id='".mysql_real_escape_string($survey_id)."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+
+		// deleting all the options of the questions of the survey
+		survey_manager::delete_all_survey_questions_options($survey_id);
+
+		// deleting all the answers on this survey
+		survey_manager::delete_all_survey_answers($survey_id);
+	}
+
+
+	/******************************************************************************************************
+									SURVEY QUESTION OPTIONS FUNCTIONS
+	 *****************************************************************************************************/
+
+	/**
+	 * This function stores the options of the questions in the table
+	 *
+	 * @param unknown_type $form_content
+	 * @return
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo writing the update statement when editing a question
+	 */
+	function save_question_options($form_content)
+	{
+		// table defintion
+		$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+
+		// we are editing a question so we first have to remove all the existing options from the database
+		if (is_numeric($form_content['question_id']))
+		{
+			$sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".mysql_real_escape_string($form_content['question_id'])."'";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+		}
+
+		$counter = 1;
+		foreach ($form_content['answers'] as $key=>$answer)
+		{
+			$sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES (
+							'".mysql_real_escape_string($form_content['question_id'])."',
+							'".mysql_real_escape_string($form_content['survey_id'])."',
+							'".mysql_real_escape_string($answer)."',
+							'".mysql_real_escape_string($counter)."')";
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			$counter++;
+		}
+	}
+
+
+	/**
+	 * This function deletes all the options of the questions of a given survey
+	 * This function is normally only called when a survey is deleted
+	 *
+	 * @param $survey_id the id of the survey that has to be deleted
+	 * @return true
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function delete_all_survey_questions_options($survey_id)
+	{
+		// table definitions
+		$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+
+		// deleting the options of the survey questions
+		$sql = "DELETE from $table_survey_question_option WHERE survey_id='".mysql_real_escape_string($survey_id)."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		return true;
+	}
+
+
+
+	/******************************************************************************************************
+									SURVEY ANSWERS FUNCTIONS
+	 *****************************************************************************************************/
+
+	/**
+	 * This function deletes all the answers anyone has given on this survey
+	 * This function is normally only called when a survey is deleted
+	 *
+	 * @param $survey_id the id of the survey that has to be deleted
+	 * @return true
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 2007
+	 */
+	function delete_all_survey_answers($survey_id)
+	{
+		return true;
+	}
+}
+
+
+
+
+
+
+
+
+
+
+
+
+class question
+{
+	// the html code of the form
+	var $html;
+
+	/**
+	 * This function does the generic part of any survey question: the question field
+	 *
+	 * @return unknown
+	 */
+	/**
+	 * This function does the generic part of any survey question: the question field
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo the form_text has to become a wysiwyg editor or adding a question_comment field
+	 * @todo consider adding a question_comment form element
+	 */
+	function create_form($form_content)
+	{
+		$this->html = '<form id="question_form" name="question_form" method="post" action="'.$_SERVER['PHP_SELF'].'?action='.$_GET['action'].'&type='.$_GET['type'].'&survey_id='.$_GET['survey_id'].'&question_id='.$_GET['question_id'].'">';
+		$this->html .= '		<input type="hidden" name="survey_id" id="survey_id" value="'.$_GET['survey_id'].'"/>';
+		$this->html .= '		<input type="hidden" name="question_id" id="question_id" value="'.$_GET['question_id'].'"/>';
+		$this->html .= '		<input type="hidden" name="type" id="type" value="'.$_GET['type'].'"/>';
+		$this->html .= '<table>';
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="3"><strong>'.get_lang('Question').'</strong></td>';
+		$this->html .= '	</tr>';
+		$this->html .= '	<tr>';
+		$this->html .= '		<td><label for="question">'.get_lang('Question').'</label></td>';
+		$this->html .= '		<td><input type="text" name="question" id="question" value="'.$form_content['question'].'"/></td>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '	</tr>';
+		/*
+		$this->html .= '	<tr>';
+		$this->html .= '		<td><label for="question_comment">'.get_lang('QuestionComment').'</label></td>';
+		$this->html .= '		<td><input type="text" name="question_comment" id="question_comment" value="'.$form_content['question_comment'].'"/></td>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '	</tr>';
+		*/
+		return $this->html;
+	}
+
+	/**
+	 * This functions displays the form after the html variable has correctly been finished
+	 * (adding a submit button, closing the table and closing the form)
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 */
+	function render_form()
+	{
+		$this->html .= '	<tr>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '		<td>  <input type="submit" name="save_question" value="'.get_lang('SaveQuestion').'" /></td>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '	</tr>';
+		$this->html .= '</table>';
+		$this->html .= '</form>';
+		echo $this->html;
+	}
+
+	/**
+	 * This function handles the actions on a question and its answers
+	 *
+	 * @todo consider using $form_content instead of $_POST
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function handle_action($form_content)
+	{
+		// moving an answer up
+		if ($_POST['move_up'])
+		{
+			foreach ($_POST['move_up'] as $key=>$value)
+			{
+				$id1		= $key;
+				$content1 	= $form_content['answers'][$id1];
+				$id2		= $key-1;
+				$content2	= $form_content['answers'][$id2];
+				$form_content['answers'][$id1] = $content2;
+				$form_content['answers'][$id2] = $content1;
+			}
+		}
+
+		// moving an answer down
+		if ($_POST['move_down'])
+		{
+			foreach ($_POST['move_down'] as $key=>$value)
+			{
+				$id1		= $key;
+				$content1 	= $form_content['answers'][$id1];
+				$id2		= $key+1;
+				$content2	= $form_content['answers'][$id2];
+				$form_content['answers'][$id1] = $content2;
+				$form_content['answers'][$id2] = $content1;
+			}
+		}
+
+		// adding an answer
+		if ($_POST['add_answer'])
+		{
+			$form_content['answers'][]='';
+		}
+
+		// removing an answer
+		if ($_POST['remove_answer'])
+		{
+			$max_answer = count($form_content['answers']);
+			unset($form_content['answers'][$max_answer-1]);
+		}
+
+		// saving a question
+		if ($_POST['save_question'])
+		{
+			$message = survey_manager::save_question($form_content);
+			Display :: display_confirmation_message($message.'<br />'.get_lang('ReturnTo').' <a href="survey.php?survey_id='.$_GET['survey_id'].'">'.get_lang('Survey').'</a>');
+		}
+
+		/**
+		 * This solution is a little bit strange but I could not find a different solution.
+		 */
+		if ($_POST['delete_answer'])
+		{
+			foreach ($_POST['delete_answer'] as $key=>$value)
+			{
+				unset($form_content['answers'][$key]);
+				$deleted = $key;
+			}
+			foreach ($form_content['answers'] as $key=>$value)
+			{
+				if ($key>$deleted)
+				{
+					$form_content['answers'][$key-1] = $form_content['answers'][$key];
+					unset($form_content['answers'][$key]);
+				}
+			}
+		}
+		return $form_content;
+	}
+
+	/**
+	 * This functions adds two buttons. One to add an option, one to remove an option
+	 *
+	 * @param unknown_type $form_content
+	 * @return html code
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function add_remove_buttons($form_content)
+	{
+		if (count($form_content['answers'])<=2)
+		{
+			$remove_answer_attribute = 'disabled="disabled"';
+		}
+
+		$return .= '	<tr>';
+		$return .= '		<td align="right">&nbsp;</td>';
+		$return .= '		<td colspan="2">';
+		$return .= '			<input type="submit" name="remove_answer" value="'.get_lang('RemoveAnswer').'" '.$remove_answer_attribute.' />';
+		$return .= '			<input type="submit" name="add_answer" value="'.get_lang('AddAnswer').'" />';
+		$return .= '		</td>';
+		$return .= '	</tr>';
+		return $return;
+	}
+
+
+	/**
+	 * render the question. In this case this starts with the form tag
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+		$this->html = '<form id="question" name="question" method="post" action="'.$_SERVER['PHP_SELF'].'?survey_id='.$_GET['survey_id'].'">';
+		echo $this->html;
+	}
+}
+
+class yesno extends question
+{
+	/**
+	 * This function creates the form elements for the yesno questions
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+		// Horizontal or vertical
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
+		$this->html .= '	</tr>';
+		$this->html .= '	<tr>';
+		$this->html .= '		<td align="right" valign="top">&nbsp;</td>';
+		$this->html .= '		<td>';
+		$this->html .= '		  <input name="horizontalvertical" type="radio" value="horizontal" ';
+						if ($form_content['horizontalvertical'] == 'horizontal')
+						{
+							$this->html .= 'checked="checked"';
+						}
+		$this->html .= '/>'.get_lang('Horizontal').'</label><br />';
+    	$this->html .= '		  <input name="horizontalvertical" type="radio" value="vertical" ';
+						if ($form_content['horizontalvertical'] == 'vertical')
+						{
+							$this->html .= 'checked="checked"';
+						}
+    	$this->html .= ' />'.get_lang('Vertical').'</label>';
+		$this->html .= '		</td>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '	</tr>';
+		// The answers
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
+		$this->html .= '	</tr>';
+		$this->html .= '	<tr>';
+		$this->html .= '		<td align="right"><label for="answers[0]">1</label></td>';
+		$this->html .= '		<td><input type="text" name="answers[0]" id="answers[0]" value="'.$form_content['answers'][0].'" /></td>';
+		$this->html .= '		<td><input type="image" src="../img/down.gif"  value="move_down[0]" name="move_down[0]"/></td>';
+		$this->html .= '	</tr>';
+		$this->html .= '	<tr>';
+		$this->html .= '		<td align="right"><label for="answers[1]">2</label></td>';
+		$this->html .= '		<td><input type="text" name="answers[1]" id="answers[1]" value="'.$form_content['answers'][1].'" /></td>';
+		$this->html .= '		<td><input type="image" src="../img/up.gif" value="move_up[1]" name="move_up[1]" /></td>';
+		$this->html .= '	</tr>';
+	}
+
+	/**
+	 * Render the yes not question type
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+		foreach ($form_content['options'] as $key=>$value)
+		{
+			$this->html .= '<label><input name="question'.$form_content['question_id'].'" type="radio" value="'.$key.'" />'.$value.'</label>';
+			if ($form_content['display'] == 'vertical')
+			{
+				$this->html .= '<br />';
+			}
+		}
+		echo '<div class="survey_question_wrapper">';
+		echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
+		echo '<div class="survey_question_options">';
+		echo $this->html;
+		echo '</div>';
+	}
+
+}
+
+class multiplechoice extends question
+{
+	/**
+	 * This function creates the form elements for the multiple choice questions
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
+		$this->html .= '	</tr>';
+		// Horizontal or vertical
+		$this->html .= '	<tr>';
+		$this->html .= '		<td align="right" valign="top">&nbsp;</td>';
+		$this->html .= '		<td>';
+		$this->html .= '		  <input name="horizontalvertical" type="radio" value="horizontal" ';
+						if ($form_content['horizontalvertical'] == 'horizontal')
+						{
+							$this->html .= 'checked="checked"';
+						}
+		$this->html .= '/>'.get_lang('Horizontal').'</label><br />';
+    	$this->html .= '		  <input name="horizontalvertical" type="radio" value="vertical" ';
+						if ($form_content['horizontalvertical'] == 'vertical')
+						{
+							$this->html .= 'checked="checked"';
+						}
+    	$this->html .= ' />'.get_lang('Vertical').'</label>';		$this->html .= '		</td>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '	</tr>';
+		// The answers
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
+		$this->html .= '	</tr>';
+		$total_number_of_answers = count($form_content['answers']);
+		foreach ($form_content['answers'] as $key=>$value)
+		{
+			$this->html .= '	<tr>';
+			$this->html .= '		<td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
+			$this->html .= '		<td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
+			$this->html .= '		<td>';
+			if ($key<$total_number_of_answers-1)
+			{
+				$this->html .= '			<input type="image" src="../img/down.gif"  value="move_down['.$key.']" name="move_down['.$key.']"/>';
+			}
+			if ($key>0)
+			{
+				$this->html .= '			<input type="image" src="../img/up.gif"  value="move_up['.$key.']" name="move_up['.$key.']"/>';
+			}
+			if ($total_number_of_answers> 2)
+			{
+				$this->html .= '			<input type="image" src="../img/delete.gif"  value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
+			}
+			$this->html .= ' 		</td>';
+			$this->html .= '	</tr>';
+		}
+		// The buttons for adding or removing
+		$this->html .= parent :: add_remove_buttons($form_content);
+	}
+
+	/**
+	 * render the multiple choice question type
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @todo it would make more sense to consider yesno as a special case of multiplechoice and not the other way around
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+		$question = new yesno();
+		$question->render_question($form_content);
+	}
+}
+
+
+
+class multipleresponse extends question
+{
+	/**
+	 * This function creates the form elements for the multiple response questions
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
+		$this->html .= '	</tr>';
+		// Horizontal or vertical
+		$this->html .= '	<tr>';
+		$this->html .= '		<td align="right" valign="top">&nbsp;</td>';
+		$this->html .= '		<td>';
+		$this->html .= '		  <input name="horizontalvertical" type="radio" value="horizontal" ';
+						if ($form_content['horizontalvertical'] == 'horizontal')
+						{
+							$this->html .= 'checked="checked"';
+						}
+		$this->html .= '/>'.get_lang('Horizontal').'</label><br />';
+    	$this->html .= '		  <input name="horizontalvertical" type="radio" value="vertical" ';
+						if ($form_content['horizontalvertical'] == 'vertical')
+						{
+							$this->html .= 'checked="checked"';
+						}
+    	$this->html .= ' />'.get_lang('Vertical').'</label>';		$this->html .= '		</td>';
+		$this->html .= '		<td>&nbsp;</td>';
+		$this->html .= '	</tr>';
+		// The answers
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
+		$this->html .= '	</tr>';
+		$total_number_of_answers = count($form_content['answers']);
+		foreach ($form_content['answers'] as $key=>$value)
+		{
+			$this->html .= '	<tr>';
+			$this->html .= '		<td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
+			$this->html .= '		<td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
+			$this->html .= '		<td>';
+			if ($key<$total_number_of_answers-1)
+			{
+				$this->html .= '			<input type="image" src="../img/down.gif"  value="move_down['.$key.']" name="move_down['.$key.']"/>';
+			}
+			if ($key>0)
+			{
+				$this->html .= '			<input type="image" src="../img/up.gif"  value="move_up['.$key.']" name="move_up['.$key.']"/>';
+			}
+			if ($total_number_of_answers> 2)
+			{
+				$this->html .= '			<input type="image" src="../img/delete.gif"  value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
+			}
+			$this->html .= ' 		</td>';
+			$this->html .= '	</tr>';
+		}
+		// The buttons for adding or removing
+		$this->html .= parent :: add_remove_buttons($form_content);
+	}
+
+	/**
+	 * Render the multiple response question type
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+		foreach ($form_content['options'] as $key=>$value)
+		{
+			$this->html .= '<label><input name="question'.$form_content['question_id'].'[]" type="checkbox" value="'.$key.'" />'.$value.'</label>';
+			if ($form_content['display'] == 'vertical')
+			{
+				$this->html .= '<br />';
+			}
+		}
+		echo '<div class="survey_question_wrapper">';
+		echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
+		echo '<div class="survey_question_options">';
+		echo $this->html;
+		echo '</div>';
+	}
+}
+
+class dropdown extends question
+{
+	/**
+	 * This function creates the form elements for the dropdown questions
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+		// The answers
+		$this->html .= '	<tr>';
+		$this->html .= '		<td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
+		$this->html .= '	</tr>';
+		$total_number_of_answers = count($form_content['answers']);
+		foreach ($form_content['answers'] as $key=>$value)
+		{
+			$this->html .= '	<tr>';
+			$this->html .= '		<td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
+			$this->html .= '		<td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
+			$this->html .= '		<td>';
+			if ($key<$total_number_of_answers-1)
+			{
+				$this->html .= '			<input type="image" src="../img/down.gif"  value="move_down['.$key.']" name="move_down['.$key.']"/>';
+			}
+			if ($key>0)
+			{
+				$this->html .= '			<input type="image" src="../img/up.gif"  value="move_up['.$key.']" name="move_up['.$key.']"/>';
+			}
+			if ($total_number_of_answers> 2)
+			{
+				$this->html .= '			<input type="image" src="../img/delete.gif"  value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
+			}
+			$this->html .= ' 		</td>';
+			$this->html .= '	</tr>';
+		}
+		// The buttons for adding or removing
+		$this->html .= parent :: add_remove_buttons($form_content);
+	}
+
+	/**
+	 * Render the dropdown question type
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+
+		foreach ($form_content['options'] as $key=>$value)
+		{
+			$this->html .= '<option value="'.$key.'">'.$value.'</option>';
+		}
+		echo '<div class="survey_question_wrapper">';
+		echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
+		echo '<div class="survey_question_options">';
+		echo '<select name="question'.$form_content['question_id'].'" id="select">';
+		echo $this->html;
+		echo '</select>';
+		echo '</div>';
+		/*
+
+
+    <option value="test">test</option>
+
+		*/
+	}
+}
+
+
+class open extends question
+{
+	/**
+	 * This function creates the form elements for the open questions
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @todo add a limit for the number of characters that can be type
+	 * @todo add a checkbox weither the answer is a textarea or a wysiwyg editor
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+	}
+
+	/**
+	 * render the open question type
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+		echo '<div class="survey_question_wrapper">';
+		echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
+		echo '<div class="survey_question_options">';
+		echo '<label for="question'.$form_content['question_id'].'"></label><textarea name="question'.$form_content['question_id'].'" id="textarea"></textarea>';
+		echo '</div>';
+	}
+}
+
+class comment extends question
+{
+	/**
+	 * This function creates the form elements for a comment.
+	 * A comment is nothing more than a block of text that the user can read
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @param array $form_content
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+	}
+
+
+	/**
+	 * Render the comment "question" type
+	 *
+	 * @param unknown_type $form_content
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 */
+	function render_question($form_content)
+	{
+		echo '<div class="survey_question_wrapper">';
+		echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
+	}
+}
+
+class pagebreak extends question
+{
+	/**
+	 * This function creates the form elements for a comment.
+	 * A comment is nothing more than a block of text that the user can read
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version januari 2007
+	 *
+	 * @param array $form_content
+	 */
+	function create_form($form_content)
+	{
+		$this->html = parent::create_form($form_content);
+	}
+}
+
+class percentage extends question
+{
+}
+?>

+ 181 - 0
main/survey/survey_invitation.php

@@ -0,0 +1,181 @@
+<?php
+/*
+    DOKEOS - elearning and course management software
+
+    For a full list of contributors, see documentation/credits.html
+
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    as published by the Free Software Foundation; either version 2
+    of the License, or (at your option) any later version.
+    See "documentation/licence.html" more details.
+
+    Contact:
+		Dokeos
+		Rue des Palais 44 Paleizenstraat
+		B-1030 Brussels - Belgium
+		Tel. +32 (2) 211 34 56
+*/
+
+/**
+*	@package dokeos.survey
+* 	@author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
+* 	@version $Id: survey_invite.php 10680 2007-01-11 21:26:23Z pcool $
+*
+* 	@todo the answered column
+*/
+
+// name of the language file that needs to be included
+$language_file = 'survey';
+
+// including the global dokeos file
+require ('../inc/global.inc.php');
+
+// 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");
+require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
+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_error_message(get_lang('NotAllowedHere'));
+	Display :: display_footer();
+	exit;
+}
+
+// Database table definitions
+$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
+$table_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+$table_course 					= Database :: get_main_table(TABLE_MAIN_COURSE);
+$table_user 					= Database :: get_main_table(TABLE_MAIN_USER);
+$table_survey_invitation 		= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+
+// breadcrumbs
+$interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
+$interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$_GET['survey_id'], 'name' => get_lang('Survey'));
+$tool_name = get_lang('SurveyInvitations');
+
+// Displaying the header
+Display::display_header($tool_name);
+
+// Displaying the survey information
+$survey_data = survey_manager::get_survey($_GET['survey_id']);
+echo '<a href="survey.php?survey_id='.$survey_data['survey_id'].'">'.$survey_data['title'].'</a><br />';
+echo $survey_data['subtitle'];
+
+// table header
+echo '<table class="data_table">';
+echo '	<tr>';
+echo '		<th>'.get_lang('User').'</th>';
+echo '		<th>'.get_lang('InvitationCode').'</th>';
+echo '		<th>'.get_lang('InvitationDate').'</th>';
+echo '		<th>'.get_lang('Answered').'</th>';
+echo '	</tr>';
+
+$sql = "SELECT survey_invitation.*, user.firstname, user.lastname, user.email FROM $table_survey_invitation survey_invitation
+			LEFT JOIN $table_user user ON  survey_invitation.user = user.user_id
+			WHERE survey_invitation.survey_id = '".mysql_real_escape_string($_GET['survey_id'])."'";
+$res = api_sql_query($sql, __FILE__, __LINE__);
+while ($row = mysql_fetch_assoc($res))
+{
+	echo '<tr>';
+	if (is_numeric($row['user']))
+	{
+		echo '			<td><a href="../user/userInfo.php?editMainUserInfo='.$row['user'].'">'.$row['firstname'].' '.$row['lastname'].'</a></td>';
+	}
+	else
+	{
+			echo '	<td>'.$row['user'].'</td>';
+	}
+	echo '	<td>'.$row['invitation_code'].'</td>';
+	echo '	<td>'.$row['invitation_date'].'</td>';
+	echo '	<td>todo (- or link: view answers)</td>';
+	echo '</tr>';
+
+}
+// closing the table
+echo '</table>';
+
+/**
+ * @todo add the additional parameters
+ */
+$table = new SortableTable('survey_invitations', 'get_number_of_survey_invitations', 'get_survey_invitations_data',2);
+$table->set_additional_parameters($parameters);
+$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->display();
+
+// Footer
+Display :: display_footer();
+
+/**
+ * Get all the information about the invitations of a certain survey
+ *
+ * @return unknown
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+ * @version januari 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 = '".mysql_real_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 januari 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='".mysql_real_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 januari 2007
+ */
+function modify_filter()
+{
+
+}
+?>

+ 297 - 0
main/survey/survey_invite.php

@@ -0,0 +1,297 @@
+<?php
+/*
+    DOKEOS - elearning and course management software
+
+    For a full list of contributors, see documentation/credits.html
+
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License
+    as published by the Free Software Foundation; either version 2
+    of the License, or (at your option) any later version.
+    See "documentation/licence.html" more details.
+
+    Contact:
+		Dokeos
+		Rue des Palais 44 Paleizenstraat
+		B-1030 Brussels - Belgium
+		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_invite.php 10680 2007-01-11 21:26:23Z pcool $
+*
+* 	@todo checking if the additional emails are valid (or add a rule for this)
+* 	@todo check if the mailtext contains the **link** part, if not, add the link to the end
+* 	@todo add rules: title and text cannot be empty
+*/
+
+// name of the language file that needs to be included
+$language_file = 'survey';
+
+// including the global dokeos file
+require ('../inc/global.inc.php');
+
+// 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");
+require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
+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_error_message(get_lang('NotAllowedHere'));
+	Display :: display_footer();
+	exit;
+}
+
+// Database table definitions
+$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
+$table_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION);
+$table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+$table_course 					= Database :: get_main_table(TABLE_MAIN_COURSE);
+$table_user 					= Database :: get_main_table(TABLE_MAIN_USER);
+$user_info 						= Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
+
+// breadcrumbs
+$interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
+$interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$_GET['survey_id'], 'name' => get_lang('Survey'));
+$tool_name = get_lang('SurveyPublication');
+
+// Displaying the header
+Display::display_header($tool_name);
+
+// Displaying the survey information
+$survey_data = survey_manager::get_survey($_GET['survey_id']);
+echo '<a href="survey.php?survey_id='.$survey_data['survey_id'].'">'.$survey_data['title'].'</a><br />';
+echo $survey_data['subtitle'];
+
+// building the form for publishing the survey
+$form = new FormValidator('publish_form','post', $_SERVER['PHP_SELF'].'?survey_id='.$_GET['survey_id']);
+// Course users
+$complete_user_list = CourseManager :: get_user_list_from_course_code($_course['id']);
+$possible_users = array ();
+foreach ($complete_user_list as $index => $user)
+{
+	$possible_users[$user['user_id']] = $user['lastname'].' '.$user['firstname'];
+}
+$users = $form->addElement('advmultiselect', 'course_users', get_lang('CourseUsers'), $possible_users);
+$users->setElementTemplate('
+{javascript}
+<table{class}>
+<!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
+<!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
+<tr>
+  <td valign="top">{unselected}</td>
+  <td align="center">{add}<br /><br />{remove}</td>
+  <td valign="top">{selected}</td>
+</tr>
+</table>
+');
+// additional users
+$form->addElement('textarea', 'additional_users', get_lang('AdditonalUsers'), array ('cols' => 50, 'rows' => 2));
+// additional users comment
+$form->addElement('static', null, null, get_lang('AdditonalUsersComment'));
+// the title of the mail
+$form->addElement('text', 'mail_title', get_lang('MailTitle'));
+// the text of the mail
+$form->addElement('html_editor', 'mail_text', get_lang('MailText'));
+// some explanation of the mail
+$form->addElement('static', null, null, get_lang('UseLinkSyntax'));
+// submit button
+$form->addElement('submit', 'submit', get_lang('Ok'));
+if ($form->validate())
+{
+	$values = $form->exportValues();
+	// save the invitation mail
+	save_invite_mail($values['mail_text']);
+	// saving the invitations for the course users
+	$count_course_users = save_invitations($values['course_users'], $values['mail_title'], $values['mail_text']);
+	// 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
+	$additional_users = explode(';',$temp);
+	$counter_additional_users = save_invitations($additional_users, $values['mail_title'], $values['mail_text']);
+	// updating the invited field in the survey table
+	update_count_invited($_GET['survey_id']);
+	$total_count = $count_course_users + $counter_additional_users;
+	Display :: display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
+}
+else
+{
+	$defaults = get_invitations($_GET['survey_id']);
+	$form->setDefaults($defaults);
+	$form->display();
+}
+
+// Footer
+Display :: display_footer();
+
+
+
+/**
+ * Save the invitation mail
+ *
+ * @param unknown_type $mailtext
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+ * @version januari 2007
+ */
+function save_invite_mail($mailtext)
+{
+	// Database table definition
+	$table_survey 					= Database :: get_course_table(TABLE_SURVEY);
+
+	$sql = "UPDATE $table_survey SET invite_mail = '".mysql_real_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)
+{
+	global $_user;
+	global $_course;
+
+	// Database table definition
+	$table_survey_invitation 	= Database :: get_course_table(TABLE_SURVEY_INVITATION);
+	$table_user 				= Database :: get_main_table(TABLE_MAIN_USER);
+
+	// get the people who are already invited
+	$already_invited = get_invitations($_GET['survey_id']);
+
+	$counter = 0;
+	if (is_array($users_array))
+	{
+		foreach ($users_array as $key=>$value)
+		{
+			// generating the unique code
+			$invitation_code = md5($value.microtime());
+
+			// storing the invitation (only if the user_id is not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
+			if ((is_numeric($value) AND !in_array($value,$already_invited['course_users'])) OR (!is_numeric($value) AND !strstr($already_invited['additional_users'], $value)) AND !empty($value))
+			{
+				$sql = "INSERT INTO $table_survey_invitation (user, survey_id, invitation_code, invitation_date) VALUES
+							('".mysql_real_escape_string($value)."','".mysql_real_escape_string($_GET['survey_id'])."','".mysql_real_escape_string($invitation_code)."','".mysql_real_escape_string(date('Y-m-d H:i:s'))."')";
+				echo $sql;
+				$result = api_sql_query($sql, __FILE__, __LINE__);
+
+				// replacing the **link** part with a valid link for the user
+				$survey_link = 'http://fillsurvey.php?course='.$_course['code'].'&invitationcode='.$invitation_code;
+				$invitation_text = str_ireplace('**link**', $survey_link ,$invitation_text, $replace_count);
+				if ($replace_count < 1)
+				{
+					$invitation_text = $invitation_text . $survey_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='".mysql_real_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['email'];
+				//echo $recipient_name.'-'.$recipient_email.'-'.$invitation_title.'-'.$invitation_text.'-'.$sender_name.'-'.$sender_email.'-';
+				//api_mail($recipient_name, $recipient_email, $invitation_title, $invitation_text, $sender_name, $sender_email, '');
+				mail($recipient_email, $invitation_title, $invitation_text);
+				$counter++;
+			}
+		}
+	}
+
+	return $counter;
+}
+
+/**
+ * Enter description here...
+ *
+ * @param unknown_type $survey_id
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+ * @version januari 2007
+ */
+function update_count_invited($survey_id)
+{
+	// 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_id = '".mysql_real_escape_string($survey_id)."'";
+	$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 = '".mysql_real_escape_string($total_invited)."' WHERE survey_id = '".mysql_real_escape_string($survey_id)."'";
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+}
+
+/**
+ * Enter description here...
+ *
+ * @param unknown_type $survey_id
+ * @return unknown
+ *
+ * @todo consider making $defaults['additional_users'] also an array
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+ * @version januari 2007
+ */
+function get_invitations($survey_id)
+{
+	// 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_id='".mysql_real_escape_string($survey_id)."'";
+
+	$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;
+}
+?>