瀏覽代碼

[svn r15875] Personality (conditional) Survey added. See FS#2746 (requires testing)

Julio Montoya 16 年之前
父節點
當前提交
5b413d7bce
共有 5 個文件被更改,包括 914 次插入188 次删除
  1. 1 1
      main/inc/lib/surveymanager.lib.php
  2. 639 111
      main/survey/fillsurvey.php
  3. 34 13
      main/survey/question.php
  4. 180 32
      main/survey/survey.lib.php
  5. 60 31
      main/survey/survey.php

+ 1 - 1
main/inc/lib/surveymanager.lib.php

@@ -3,7 +3,7 @@
 =====================================================Gh9Xp5m=========================
 	Dokeos - elearning and course management software
 
-	Copyright (c) 2004 Dokeos S.A.
+	Copyright (c) 2004 Dokeos SPRL
 	Copyright (c) 2003 Ghent University (UGent)
 	Copyright (c) 2001 Universite catholique de Louvain (UCL)
 	Copyright (c) various contributors

+ 639 - 111
main/survey/fillsurvey.php

@@ -23,17 +23,17 @@
 *	@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
+*	@author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification and rewriting large parts of the code
 * 	@version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
 *
 * 	@todo use quickforms for the forms
 * 	@todo check if the user already filled the survey and if this is the case then the answers have to be updated and not stored again.
-* 			alterantively we could not allow people from filling the survey twice.
+* 		  alterantively we could not allow people from filling the survey twice.
 * 	@todo performance could be improved if not the survey_id was stored with the invitation but the survey_code
 */
 
 // name of the language file that needs to be included
 $language_file = 'survey';
-
 // unsetting the course id (because it is in the URL)
 if (!isset($_GET['cidReq']))
 {
@@ -59,7 +59,6 @@ if (!empty($_user))
 	$interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
 }
 
-
 // Header
 Display :: display_header(get_lang('Survey'));
 
@@ -68,6 +67,7 @@ $_course = CourseManager::get_course_information($_GET['course']);
 
 // Database table definitions
 $table_survey 					= Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
+$table_survey_answer			= Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
 $table_survey_question 			= Database :: get_course_table(TABLE_SURVEY_QUESTION, $_course['db_name']);
 $table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $_course['db_name']);
 $table_course 					= Database :: get_main_table(TABLE_MAIN_COURSE);
@@ -105,7 +105,8 @@ if ($survey_invitation['answered'] == 1)
 // If this is the case there will be a language choice
 $sql = "SELECT * FROM $table_survey WHERE code='".Database::escape_string($survey_invitation['survey_code'])."'";
 $result = api_sql_query($sql, __FILE__, __LINE__);
-if (mysql_num_rows($result) > 1)
+  
+if (Database::num_rows($result) > 1)
 {
 	if ($_POST['language'])
 	{
@@ -134,78 +135,124 @@ else
 
 // getting the survey information
 $survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
-$survey_data['survey_id'] = $survey_invitation['survey_id'];
 
+$survey_data['survey_id'] = $survey_invitation['survey_id'];
+//print_r($survey_data);
 // storing the answers
 if ($_POST)
 {
-	// 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__);
-	while ($row = mysql_fetch_assoc($result))
-	{
-		$types[$row['question_id']] = $row['type'];
-	}
-
-
-	// looping through all the post values
-	foreach ($_POST as $key=>$value)
+	if ($survey_data['survey_type']=='0')
 	{
-		// if the post value key contains the string 'question' then it is an answer on a question
-		if (strstr($key,'question'))
+		// 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__);
+		
+		while ($row = mysql_fetch_assoc($result))
 		{
-			// finding the question id by removing 'question'
-			$survey_question_id = str_replace('question', '',$key);
-
-			// if the post value is an array then we have a multiple response question or a scoring question type
-			// remark: when it is a multiple response then the value of the array is the option_id
-			// 		   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))
+			$types[$row['question_id']] = $row['type'];
+		}	
+	
+		// looping through all the post values
+		foreach ($_POST as $key=>$value)
+		{
+			// if the post value key contains the string 'question' then it is an answer on a question
+			if (strstr($key,'question'))
 			{
-				SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
-				foreach ($value as $answer_key => $answer_value)
+				// finding the question id by removing 'question'
+				$survey_question_id = str_replace('question', '',$key);
+	
+				// if the post value is an array then we have a multiple response question or a scoring question type
+				// remark: when it is a multiple response then the value of the array is the option_id
+				// 		   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))
 				{
-					if ($types[$survey_question_id] == 'score')
-					{
-						$option_id = $answer_key;
-						$option_value = $answer_value;
-					}
-					else
+					SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
+					foreach ($value as $answer_key => $answer_value)
 					{
-						$option_id = $answer_value;
-						$option_value = '';
+						if ($types[$survey_question_id] == 'score')
+						{
+							$option_id = $answer_key;
+							$option_value = $answer_value;
+						}
+						else
+						{
+							$option_id = $answer_value;
+							$option_value = '';
+						}
+						SurveyUtil::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, ...)
-			else
-			{
-				if ($types[$survey_question_id] == 'percentage')
-				{
-					$sql = "SELECT * FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
-					$result = api_sql_query($sql, __FILE__, __LINE__);
-					$row = mysql_fetch_assoc($result);
-					$option_value = $row['option_text'];
 				}
+				// all the other question types (open question, multiple choice, percentage, ...)
 				else
 				{
-					$option_value = 0;
-					if($types[$survey_question_id] == 'open')
+					if ($types[$survey_question_id] == 'percentage')
+					{
+						$sql = "SELECT * FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
+						$result = api_sql_query($sql, __FILE__, __LINE__);
+						$row = mysql_fetch_assoc($result);
+						$option_value = $row['option_text'];
+					}
+					else
 					{
-						$option_value = $value;
-						//$value = 0;
+						$option_value = 0;
+						if($types[$survey_question_id] == 'open')
+						{
+							$option_value = $value;
+							//$value = 0;
+						}
 					}
+	
+					$survey_question_answer = $value;
+					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);
 				}
-
-
+			}
+		}
+	}
+	else
+	{
+		// getting all the types of the question (because of the special treatment of the score question type		
+		
+		$shuffle='';
+		/*
+		if ($survey_data['shuffle']=='1')
+		{
+			$shuffle= ' BY RAND() '	;
+		}
+		*/
+				
+		$sql = "SELECT * FROM $table_survey_question
+				WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
+				AND survey_group_pri='0' ORDER BY RAND()
+				";
+		$result = api_sql_query($sql, __FILE__, __LINE__);		
+		while ($row = mysql_fetch_assoc($result))
+		{
+			$types[$row['question_id']] = $row['type'];
+		}		
+		
+		// looping through all the post values
+		foreach ($_POST as $key=>$value)
+		{
+			// if the post value key contains the string 'question' then it is an answer on a question
+			if (strstr($key,'question'))
+			{	
+				// finding the question id by removing 'question'
+				$survey_question_id = str_replace('question', '',$key);				
+				// we select the correct answer and the puntuacion
+				$sql = "SELECT value FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
+				$result = api_sql_query($sql, __FILE__, __LINE__);
+				$row = mysql_fetch_assoc($result);
+				$option_value = $row['value'];			
+				//$option_value = 0;			
 				$survey_question_answer = $value;
 				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);
+				//SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);				
 			}
-		}
-	}
+		}		
+	}	
 }
 
 // displaying the survey title and subtitle (appears on every page)
@@ -216,6 +263,7 @@ echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
 $start_date = mktime(0,0,0,substr($survey_data['start_date'],5,2),substr($survey_data['start_date'],8,2),substr($survey_data['start_date'],0,4));
 $end_date = mktime(0,0,0,substr($survey_data['end_date'],5,2),substr($survey_data['end_date'],8,2),substr($survey_data['end_date'],0,4));
 $cur_date = time();
+
 if($cur_date < $start_date)
 {
 	Display :: display_warning_message(get_lang('SurveyNotAvailableYet'), false);
@@ -238,80 +286,507 @@ if (!isset($_GET['show']))
 
 // displaying the survey thanks message
 if (isset($_POST['finish_survey']))
-{
+{	
 	echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').'</strong> <br />'.$survey_data['survey_thanks'].'</div>';
 	survey_manager::update_survey_answered($survey_data['survey_id'], $survey_invitation['user'], $survey_invitation['survey_code']);
 	Display :: display_footer();
 	exit;
 }
+$shuffle='';
+if ($survey_data['shuffle']==1)
+{
+	$shuffle= ' BY RAND() ';
+}		
 
-if (isset($_GET['show']))
+if ( isset($_GET['show']) || isset($_POST['personality']))
 {
 	// Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
 	// as long as there is no pagebreak fount we keep adding questions to the page
 	$questions_displayed = array();
 	$counter = 0;
-	$sql = "SELECT * FROM $table_survey_question
-			WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
-			ORDER BY sort ASC";
-	$result = api_sql_query($sql, __FILE__, __LINE__);
+	
+	// if is a normal survey
+	if ($survey_data['survey_type']=='0')
+	{		
+		$sql = "SELECT * FROM $table_survey_question
+				WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
+				ORDER BY sort ASC";
+		$result = api_sql_query($sql, __FILE__, __LINE__);		
 
-	while ($row = mysql_fetch_assoc($result))
-	{
-		if($row['type'] == 'pagebreak')
+		while ($row = mysql_fetch_assoc($result))
 		{
-			$counter++;
+			if($row['type'] == 'pagebreak')
+			{
+				$counter++;
+			}
+			else
+			{
+				// ids from question of the current survey
+				$paged_questions[$counter][] = $row['question_id']; 
+			}
 		}
-		else
+		
+		if (key_exists($_GET['show'],$paged_questions))
 		{
-			$paged_questions[$counter][] = $row['question_id'];
+			
+			$sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,					
+					survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value,
+					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($survey_invitation['survey_id'])."'
+					AND survey_question.question_id IN (".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);
+			$counter = 0;
+			$limit=0;
+			$questions = array();
+			
+			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['question_option_id']] = $row['option_text'];
+					$questions[$row['sort']]['maximum_score'] = $row['max_value'];				
+				}
+				// if the type is a pagebreak we are finished loading the questions for this page
+				else
+				{
+					break;
+				}
+				$counter++;
+			}
 		}
 	}
-
-	if (key_exists($_GET['show'],$paged_questions))
-	{
-		$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.max_value,
-						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($survey_invitation['survey_id'])."'
-				AND survey_question.question_id IN (".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);
-		$counter = 0;
-		$limit=0;
-		$questions = array();
-		while ($row = mysql_fetch_assoc($result))
+	else
+	{		
+		$my_survey_id=Database::escape_string($survey_invitation['survey_id']);
+		$current_user = Database::escape_string($survey_invitation['user']);
+				
+		if (isset($_POST['personality']))
 		{
-			// if the type is not a pagebreak we store it in the $questions array
-			if($row['type'] <> 'pagebreak')
+			// I have to calculate the results to get the 3 groups the most near to the personality of the user
+			$paged_group_questions=array();	
+			
+			if ($shuffle=='')
+				$order = 'BY sort ASC ';				
+			else
+				$order = $shuffle;
+				
+			$sql = "SELECT * FROM $table_survey_question
+				 	 WHERE survey_id = '".$my_survey_id."' 
+				 	 AND survey_group_sec1='0' AND survey_group_sec2='0'  
+				 	 ORDER $order ";
+				 	 
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			//echo "<br>";
+			
+			while ($row = mysql_fetch_assoc($result))
+			{			
+				$paged_group_questions[] = $row['question_id'];
+				$paged_group[] = $row['survey_group_pri'];							
+			}			
+			$answer_list=array();
+			//echo "<br>";	print_r($paged_group_questions); print_r($paged_group);	
+			
+			// current user results		
+			$results=array();						
+			$sql = "SELECT survey_group_pri, user, SUM(value) as value
+					FROM $table_survey_answer as survey_answer INNER JOIN $table_survey_question as survey_question 
+					ON  (survey_question.question_id = survey_answer.question_id)
+					WHERE survey_answer.survey_id='".$my_survey_id."' AND
+					survey_answer.user='".$current_user."'
+					GROUP BY survey_group_pri
+					ORDER BY survey_group_pri						
+					";
+														
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			while ($row = Database::fetch_array($result))
+			{		
+				$answer_list['value']=$row['value'];
+				$answer_list['group']=$row['survey_group_pri'];
+				$results[]=$answer_list;
+			}
+			
+			//echo "<br>";print_r($results);	echo "<br>";
+			
+			// total calculations 
+			$totals=array();			
+			$sql = "SELECT SUM(temp.value) as value, temp.survey_group_pri FROM 
+					(SELECT MAX(value) as value,  survey_group_pri, survey_question.question_id 
+					FROM $table_survey_question as survey_question 
+					INNER JOIN $table_survey_question_option as survey_question_option 
+					ON (survey_question.question_id = survey_question_option.question_id)
+					WHERE survey_question.survey_id='".$my_survey_id."'  AND  survey_group_sec1='0' AND survey_group_sec2='0'
+					GROUP BY survey_group_pri, survey_question.question_id) as temp
+					GROUP BY temp.survey_group_pri
+					ORDER BY temp.survey_group_pri";
+					
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			while ($row = Database::fetch_array($result))
+			{		
+				$list['value']=$row['value'];
+				$list['group']=$row['survey_group_pri'];
+				$totals[]=$list;
+			}
+			//print_r($totals);
+			
+			$final_results=array();
+			
+			for ($i=0; $i< count($totals);$i++)
+			{
+				for ($j=0; $j< count($results);$j++)
+				{
+					if ($totals[$i]['group']==$results[$j]['group'])
+					{
+						$group=$totals[$i]['group'];
+						$porcen=($results[$j]['value'] / $totals[$i]['value'] );
+						$final_results[$group]=$porcen;
+					}					
+				}				
+			}
+							
+			// ordering 
+			arsort($final_results);
+			$groups=array_keys($final_results);
+			echo '<pre>';
+			echo 'Group id =>  %';
+			echo "<br>";
+			print_r($final_results);
+											
+			$result=array();
+									
+			foreach ($final_results as $key =>$sub_result)
+			{
+				$result[]=array('group'=>$key, 'value'=>$sub_result);			
+			}
+			
+			$i=0;
+			$group_cant=0;			
+			$equal_count=0;
+			/*
+			//i.e 70% - 70% -70% 70%  $equal_count =3
+			while(1)
+			{
+				if ($result[$i]['value']  == $result[$i+1]['value'])
+				{
+					$equal_count++;
+				}
+				else
+				{
+					break;
+				}
+				$i++;	
+			}
+			echo 'eq'. $equal_count;
+			echo "<br>";
+			if 	($equal_count==0)
 			{
-				$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['question_option_id']] = $row['option_text'];
-				$questions[$row['sort']]['maximum_score'] = $row['max_value'];
+				//i.e 70% 70% -60% 60%  $equal_count = 1 only we get the first 2 options			
+				if ( ($result[0]['value']  == $result[1]['value'])  &&  ($result[2]['value']==$result[3]['value']) )
+				{
+					$group_cant=1;			
+				} 
+				else
+				{	// by default we chose the highest 3
+					$group_cant=2;
+				}		
+			} 
+			elseif ($equal_count==2)
+			{
+				$group_cant=2;
+			}
+			else
+			{
+				$group_cant=-1;
+			}
+			*/
+			
+			//i.e 70% - 70% -70% 70%  $equal_count =3
+			while(1)
+			{
+				if ($result[$i]['value']  == $result[$i+1]['value'])
+				{
+					$equal_count++;
+				}
+				else
+				{
+					break;
+				}
+				$i++;	
+			}
+			
+			if ($equal_count<4)
+			{
+			
+				if 	($equal_count==0 || $equal_count==1 )
+				{	
+					//i.e 70% - 70% -0% - 0% 	-	$equal_count = 0 only we get the first 2 options	
+					if ( ($result[0]['value']  == $result[1]['value'])  &&  ($result[2]['value']==0 )	)					
+					{
+						$group_cant=0;			
+					}
+					//i.e 70% - 70% -60% - 60%  $equal_count = 0 only we get the first 2 options	
+					elseif ( ($result[0]['value']  == $result[1]['value'])  &&  ($result[2]['value']==$result[3]['value']) )
+					{
+						$group_cant=0;				
+					}
+					elseif ( ($result[1]['value']  == $result[2]['value'])  &&  ($result[2]['value']==$result[3]['value']) )
+					{
+						$group_cant=-1;				
+					}
+					else
+					{	// by default we chose the highest 3
+						$group_cant=2;				
+					}		
+				} 
+				else
+				{
+					$group_cant=$equal_count;
+				}				
+				// conditional_status 
+				// 0 no determinado 
+				// 1 determinado 
+				// 2 un solo valor
+				// 3 valores iguales
+								
+				if ($group_cant>0)
+				{				
+					//echo '$equal_count'.$group_cant;
+					// we only get highest 3			
+					$secondary ='';		
+					$combi='';
+					
+					for ($i=0; $i<= $group_cant ;$i++)
+					{			
+						$group1=$groups[$i];
+						$group2=$groups[$i+1];
+						// here we made all the posibilities with the 3 groups
+						if ( $group_cant == 2 && $i==$group_cant) 
+						{	
+							$group2=$groups[0];				
+							$secondary .= " OR ( survey_group_sec1 = '$group1' AND  survey_group_sec2 = '$group2') ";
+							$secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
+							$combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1."<br>";
+						}
+						else
+						{						
+							if ($i!=0 )	
+							{		
+								$secondary .= " OR ( survey_group_sec1 = '$group1' AND  survey_group_sec2 = '$group2') ";
+								$secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
+								$combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1."<br>";
+							}
+							else 
+							{
+								$secondary .= " ( survey_group_sec1 = '$group1' AND  survey_group_sec2 = '$group2') ";
+								$secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
+								$combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1."<br>";
+							}
+						}										 
+					}
+					echo "Pair of Groups <br><br>";
+					echo $combi;
+					
+					// create the new select with the questions								
+					$sql = "SELECT * FROM $table_survey_question
+								 WHERE survey_id = '".$my_survey_id."' 
+							  	 AND ($secondary )
+								 ORDER BY sort ASC";						 				 
+					$result = api_sql_query($sql, __FILE__, __LINE__);		
+					$counter=0;	
+					while ($row = mysql_fetch_assoc($result))
+					{
+						if ($survey_data['one_question_per_page']==0)
+						{
+							$paged_questions[$counter][] = $row['question_id'];
+							$counter++; 
+						}
+						else
+						
+							if($row['type'] == 'pagebreak')
+							{
+								$counter++;
+							}
+							else
+							{
+								// ids from question of the current survey
+								$paged_questions[$counter][] = $row['question_id']; 
+							}					
+					}			
+					
+					if ($shuffle=='') 
+						$shuffle=' BY survey_question.sort, survey_question_option.sort ASC ';	
+							
+					$val=0;
+					
+					
+					if ($survey_data['one_question_per_page']==0)
+					{
+						$val=$_POST['personality'];
+					}			
+								
+					$sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,				
+							survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value,
+							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 = '".$my_survey_id."'
+							AND survey_question.question_id IN (".implode(',',$paged_questions[$val]).")
+							ORDER  $shuffle ";
+								
+					$result = api_sql_query($sql, __FILE__, __LINE__);
+					$question_counter_max = mysql_num_rows($result);
+					$counter = 0;
+					$limit=0;
+					$questions = array();			
+					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['question_option_id']] = $row['option_text'];
+							$questions[$row['sort']]['maximum_score'] = $row['max_value'];
+							// personality params					
+							$questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
+							$questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
+							$questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
+						}
+						// if the type is a pagebreak we are finished loading the questions for this page
+						else
+						{
+							break;
+						}
+						$counter++;
+					}
+				}
+				else
+				{	
+					echo get_lang('SurveyUndetermined');
+				}
 			}
-			// if the type is a pagebreak we are finished loading the questions for this page
 			else
 			{
-				break;
+				echo get_lang('SurveyUndetermined');
 			}
-			$counter++;
+					
 		}
-	}
+		else
+		{			
+			// only the questions from the basic group	
+			//the 50 questions A B C D E F G	
+			$order_sql= $shuffle;
+			if ($shuffle=='') 
+				$order_sql=' BY question_id ';
+						 
+			$sql = "SELECT * FROM $table_survey_question
+						 WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' 
+					  	 AND survey_group_sec1='0' AND survey_group_sec2='0'  
+						 ORDER ".$order_sql." ";
+			//echo "<br>";echo "<br>";
+			$result = api_sql_query($sql, __FILE__, __LINE__);					
+			$counter=0;			
+			while ($row = mysql_fetch_assoc($result))
+			{
+				if ($survey_data['one_question_per_page']==0)
+				{
+					
+					$paged_questions[$counter][] = $row['question_id'];
+					$counter++; 
+				}
+				else 
+				{				
+					if($row['type'] == 'pagebreak')
+					{
+						$counter++;
+					}
+					else
+					{
+						// ids from question of the current survey
+						$paged_questions[$counter][] = $row['question_id']; 
+					}
+				}
+			} 
+		
+			
+			//if (key_exists($_GET['show'],$paged_questions))
+			//{		
+			$order_sql= $shuffle;
+			if ($shuffle=='') 
+				$order_sql=' BY survey_question.sort, survey_question_option.sort ASC ';
+			
+			$val=0;
+			if ($survey_data['one_question_per_page']==0)
+			{
+				$val=$_GET['show'];
+			}
+			
+			$sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,				
+					survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value,
+					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($survey_invitation['survey_id'])."'
+					AND survey_question.question_id IN (".implode(',',$paged_questions[$val]).")  
+					ORDER $order_sql ";	
+	
+			
+			$result = api_sql_query($sql, __FILE__, __LINE__);
+			$question_counter_max = mysql_num_rows($result);
+			$counter = 0;
+			$limit=0;
+			$questions = array();
+			
+			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['question_option_id']] = $row['option_text'];
+					$questions[$row['sort']]['maximum_score'] = $row['max_value'];					
+					// personality params					
+					$questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
+					$questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
+					$questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
+				}
+				// if the type is a pagebreak we are finished loading the questions for this page
+				else
+				{
+					break;
+				}
+				$counter++;
+			}
+		}
+	}	
 }
 
 // selecting the maximum number of pages
 $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_invitation['survey_id'])."'";
 $result = api_sql_query($sql, __FILE__, __LINE__);
-$numberofpages = mysql_num_rows($result) + 1;
-	// Displaying the form with the questions
+$numberofpages = Database::num_rows($result) + 1;
+
+// Displaying the form with the questions
 if (isset($_GET['show']))
 {
 	$show = (int)$_GET['show'] + 1;
@@ -322,31 +797,84 @@ else
 }
 
 
+// Displaying the form with the questions
+if (isset($_POST['personality']))
+{
+	$personality = (int)$_POST['personality'] + 1;
+}
+else
+{
+	$personality= 0;
+}
+
 // Displaying the form with the questions
 $g_c = (isset($_GET['course'])?Security::remove_XSS($_GET['course']):'');
 $g_ic = (isset($_GET['invitationcode'])?Security::remove_XSS($_GET['invitationcode']):'');
 $g_cr = (isset($_GET['cidReq'])?Security::remove_XSS($_GET['cidReq']):'');
 $p_l = (isset($_POST['language'])?Security::remove_XSS($_POST['language']):'');
+
 echo '<form id="question" name="question" method="post" action="'.api_get_self().'?course='.$g_c.'&invitationcode='.$g_ic.'&show='.$show.'&cidReq='.$g_cr.'">';
 echo '<input type="hidden" name="language" value="'.$p_l.'" />';
-if(isset($questions) && is_array($questions)){
+
+if(isset($questions) && is_array($questions))
+{
 	foreach ($questions as $key=>$question)
 	{
 		$display = new $question['type'];
 		$display->render_question($question);
 	}
 }
-if (($show < $numberofpages) OR !$_GET['show'])
-{
-	//echo '<a href="'.api_get_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 ($survey_data['survey_type']==0)
+{	
+	if (($show < $numberofpages) || !$_GET['show'])
+	{
+		//echo '<a href="'.api_get_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 ($show >= $numberofpages && $_GET['show'])
+	{	
+		echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
+	}
 }
-if ($show >= $numberofpages AND $_GET['show'])
-{
-	echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
+else
+{ 
+	$numberofpages=count($paged_questions);
+	//echo $show.' / '.$numberofpages;echo "<br />";
+	
+	if (($show < $numberofpages) || !$_GET['show'])
+	{
+		echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
+				
+		if ($survey_data['one_question_per_page']==1 && $show!=0)
+		{			
+			echo '<input type="hidden" name="personality" value="'.$personality.'">';
+		}
+	}	
+		
+	if ($show >= $numberofpages && $_GET['show'] )
+	{ 		
+		if ($survey_data['one_question_per_page']==0)
+		{	
+			echo '<input type="hidden" name="personality" value="'.$personality.'">';
+		}					
+		$numberofpages=count($paged_questions);
+		
+		echo $personality.' / '.$numberofpages;
+		echo "<br />"; 
+		
+		if ($personality >  $numberofpages  -1 )
+		{
+			echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
+		}
+		else
+		{
+			echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
+		}			
+	}	
 }
 echo '</form>';
-
 // Footer
 Display :: display_footer();
-?>
+?>

+ 34 - 13
main/survey/question.php

@@ -23,7 +23,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: question.php 15846 2008-07-24 20:29:41Z dperales $
+* 	@version $Id: question.php 15875 2008-07-30 23:21:03Z juliomontoya $
 */
 
 // name of the language file that needs to be included
@@ -63,7 +63,8 @@ if (strlen(strip_tags($survey_data['title'])) > 40)
 	$urlname .= '...';
 }
 
-if($survey_data['survey_type']==1){
+if($survey_data['survey_type']==1)
+{
 	$sql = 'SELECT id FROM '.Database :: get_course_table(TABLE_SURVEY_GROUP).' WHERE survey_id = '.(int)$_GET['survey_id'].' LIMIT 1';
 	$rs = api_sql_query($sql,__FILE__,__LINE__);
 	if(Database::num_rows($rs)===0) {
@@ -87,7 +88,7 @@ if ($_GET['action'] == 'edit')
 }
 
 // the possible question types
-$possible_types = array('yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
+$possible_types = array('personality','yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
 
 // checking if it is a valid type
 if (!in_array($_GET['type'], $possible_types))
@@ -100,10 +101,10 @@ if (!in_array($_GET['type'], $possible_types))
 // displaying the form for adding or editing the question
 if (empty($_POST['save_question']) && in_array($_GET['type'],$possible_types))
 {
+	
 	// Displaying the header
 	Display::display_header($tool_name,'Survey');
 	
-
 	$error_message='';	
 	// Displys message if exists					
 	if (isset($_SESSION['temp_sys_message']))
@@ -113,32 +114,48 @@ if (empty($_POST['save_question']) && in_array($_GET['type'],$possible_types))
 		if ($error_message=='PleaseEnterAQuestion' || $error_message=='PleasFillAllAnswer'|| $error_message=='PleaseChooseACondition'|| $error_message=='ChooseDifferentCategories')
 		{
 			Display::display_error_message(get_lang($error_message), true);			
-		}		
+		} 		
 	}
-	
+	  
 	echo '<img src="../img/'.survey_manager::icon_question($_GET['type']).'" alt="'.get_lang(ucfirst($_GET['type'])).'" title="'.get_lang(ucfirst($_GET['type'])).'" /><br />';
 	echo get_lang(ucfirst($_GET['type']));
 
 	$form = new $_GET['type'];
 
 	// The defaults values for the form
-	$form_content['horizontalvertical'] = 'vertical';
+	$form_content['horizontalvertical'] = 'vertical2';
 	$form_content['answers'] = array('', '');
-		
+	
 	if ($_GET['type'] == 'yesno')
 	{
 		$form_content['answers'][0]=get_lang('Yes');
 		$form_content['answers'][1]=get_lang('No');
 	}
+	
+	if ($_GET['type'] == 'personality')
+	{
+		$form_content['answers'][0]=get_lang('1');
+		$form_content['answers'][1]=get_lang('2');
+		$form_content['answers'][2]=get_lang('3');
+		$form_content['answers'][3]=get_lang('4');
+		$form_content['answers'][4]=get_lang('5');
+		
+		$form_content['values'][0]=0;
+		$form_content['values'][1]=0;
+		$form_content['values'][2]=1;
+		$form_content['values'][3]=2;
+		$form_content['values'][4]=3;	
+	}
+		
 	// We are editing a question
 	if (isset($_GET['question_id']) AND !empty($_GET['question_id']))
 	{
-		$form_content = survey_manager::get_question($_GET['question_id']);
+		echo "aqui".$form_content = survey_manager::get_question($_GET['question_id']);
 	}
 
 	// an action has been performed (for instance adding a possible answer, moving an answer, ...)
 	if ($_POST)
-	{
+	{	
 		$form_content = $_POST;
 		$form_content = $form->handle_action($form_content);
 	}
@@ -147,15 +164,19 @@ if (empty($_POST['save_question']) && in_array($_GET['type'],$possible_types))
 	{						
 		$form_content['question']=$_SESSION['temp_user_message'];
 		$form_content['answers']=$_SESSION['temp_answers'];
+		$form_content['values']=$_SESSION['temp_values'];
+		
 		unset($_SESSION['temp_user_message']);
-		unset($_SESSION['temp_answers']);								
+		unset($_SESSION['temp_answers']);
+		unset($_SESSION['temp_values']);								
 	}
+	
 	$form->create_form($form_content);
 	$form->render_form();	 
 }
 else
-{
-	$form_content = $_POST;
+{	
+	$form_content = $_POST;	
 	$form = new question();
 	$form->handle_action($form_content);
 }

+ 180 - 32
main/survey/survey.lib.php

@@ -23,7 +23,8 @@
 /**
 *	@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 15846 2008-07-24 20:29:41Z dperales $
+	@author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification and rewriting large parts of the code
+* 	@version $Id: survey.lib.php 15875 2008-07-30 23:21:03Z juliomontoya $
 *
 * 	@todo move this file to inc/lib
 * 	@todo use consistent naming for the functions (save vs store for instance)
@@ -128,7 +129,9 @@ class survey_manager
 			
 			$additional['columns'] = '';
 			$additional['values'] = '';
-			if($values['survey_type']==1){
+			
+			if($values['survey_type']==1)
+			{
 				$additional['columns'] = ', survey_type';
 				$additional['values'] .= ",'1'";
 
@@ -142,7 +145,8 @@ class survey_manager
 				$additional['values'] .= ",'".Database::escape_string($values['parent_id'])."'";
 
 				// logic for versioning surveys
-				if(!empty($values['parent_id'])){
+				if(!empty($values['parent_id']))
+				{
 					$additional['columns'] .= ', survey_version';
 					$sql = 'SELECT survey_version FROM '.$table_survey.' WHERE parent_id = '.$values['parent_id'].' ORDER BY survey_version DESC LIMIT 1';
 					$rs = api_sql_query($sql,__FILE__,__LINE__);
@@ -150,9 +154,12 @@ class survey_manager
 						$sql = 'SELECT survey_version FROM '.$table_survey.' WHERE survey_id = '.$values['parent_id'];
 						$rs = api_sql_query($sql,__FILE__,__LINE__);
 						$getversion = Database::fetch_array($rs,ASSOC);
-						if(empty($getversion['survey_version'])){
+						if(empty($getversion['survey_version']))
+						{
 							$additional['values'] .= ",'".++$getversion['survey_version']."'";
-						} else {
+						} 
+						else 
+						{
 							$additional['values'] .= ",'".$getversion['survey_version'].".1'";							 
 						}
 					} else {
@@ -462,11 +469,13 @@ class survey_manager
 	function icon_question($type)
 	{
 		// the possible question types
-		$possible_types = array('yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
+		$possible_types = array('personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
 
 		// the images array
-		$icon_question = array(
+		$icon_question = array
+				(
 				'yesno' 			=> 'yesno.gif',
+				'personality' 			=> 'yesno.gif',
 				'multiplechoice' 	=> 'mcua.gif',
 				'multipleresponse' 	=> 'mcma.gif',
 				'open' 				=> 'open_answer.gif',
@@ -521,10 +530,13 @@ class survey_manager
 	    $return['shared_question_id']	= $row['shared_question_id'];
 	    $return['maximum_score']		= $row['max_value'];
  		
-  		if($row['survey_group_pri']!=0){
+  		if($row['survey_group_pri']!=0)
+  		{
   			$return['assigned'] = $row['survey_group_pri'];
 	 		$return['choose'] = 1;
-   		} else {
+   		} 
+   		else 
+   		{
  	 		$return['assigned1'] = $row['survey_group_sec1'];
   			$return['assigned2'] = $row['survey_group_sec2'];
 	 		$return['choose'] = 2;
@@ -537,6 +549,9 @@ class survey_manager
 		{
 			/** @todo this should be renamed to options instead of answers */
 			$return['answers'][] = $row['option_text'];
+			
+			$return['values'][] = $row['value'];
+			
 			/** @todo this can be done more elegantly (used in reporting) */
 			$return['answersid'][] = $row['question_option_id'];
 		}
@@ -581,7 +596,7 @@ class survey_manager
 		$result = api_sql_query($sql, __FILE__, __LINE__);
 		while ($row = Database::fetch_array($result,'ASSOC'))
 		{
-			$return[$row['question_id']]['answers'][] = $row['option_text'];
+			$return[$row['question_id']]['answers'][] = $row['option_text'];			
 		}
 
 		return $return;
@@ -598,8 +613,8 @@ class survey_manager
 	 */
 	 
 	function save_question($form_content)
-	{	
-	global $survey_data;
+	{			
+		global $survey_data;
 	
 		if (strlen($form_content['question'])>1)
 		{		//checks lenght of the question	
@@ -607,7 +622,8 @@ class survey_manager
 			
 			if ($survey_data['survey_type'] == 1)
 			{
-				if (empty($form_content['choose'])){
+				if (empty($form_content['choose']))
+				{
 					$return_message = 'PleaseChooseACondition';	 
 					return $return_message;	
 				}
@@ -616,8 +632,7 @@ class survey_manager
 				{
 					$return_message = 'ChooseDifferentCategories';	 
 					return $return_message;	
-				}
-				
+				}				
 			}
 					
 			if ($form_content['type'] != 'percentage')
@@ -669,11 +684,14 @@ class survey_manager
 					//some variables defined for survey-test type
 					$additional['column'] = '';
 					$additional['value'] = '';
+					
 					if($_POST['choose']==1)
 					{
 						$additional['column'] = ',survey_group_pri';	
 						$additional['value'] = ",'".Database::escape_string($_POST['assigned'])."'";	
-					} elseif($_POST['choose']==2) {
+					} 
+					elseif($_POST['choose']==2) 
+					{
 						$additional['column'] = ',survey_group_sec1, survey_group_sec2';	
 						$additional['value'] = ",'".Database::escape_string($_POST['assigned1'])."'".",'".Database::escape_string($_POST['assigned2'])."'";	
 					}
@@ -700,9 +718,12 @@ class survey_manager
 				{
 					$additionalsets = '';
 					
-					if($_POST['choose']==1){
+					if($_POST['choose']==1)
+					{
 						$additionalsets = ',survey_group_pri = \''.Database::escape_string($_POST['assigned']).'\', survey_group_sec1 = \'0\', survey_group_sec2 = \'0\' ';	
-					} elseif($_POST['choose']==2) {
+					} 
+					elseif($_POST['choose']==2) 
+					{
 						$additionalsets = ',survey_group_pri = \'0\', survey_group_sec1 = \''.Database::escape_string($_POST['assigned1']).'\', survey_group_sec2 = \''.Database::escape_string($_POST['assigned2']).'\' ';		
 					}
 					
@@ -841,7 +862,7 @@ class survey_manager
 		$sql2 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_one)."' WHERE question_id='".Database::escape_string($question_id_two)."'";
 		$result = api_sql_query($sql2, __FILE__, __LINE__);
 	}
-
+  
 
 	/**
 	 * This function deletes all the questions of a given survey
@@ -942,7 +963,6 @@ class survey_manager
 	 *
 	 * @param array $form_content
 	 * @return
-	 *
 	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
 	 * @version January 2007
 	 *
@@ -974,19 +994,24 @@ class survey_manager
 			$result = api_sql_query($sql, __FILE__, __LINE__);
 		}
 
-		$counter=1;		
+		$counter=1;
 		if(is_array($form_content['answers']))
 		{
-			foreach ($form_content['answers'] as $key=>$answer)
+			
+			//foreach ($form_content['answers'] as $key=>$answer)			{
+			for ($i=0;$i<count($form_content['answers']);$i++)
 			{
-				$sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES (
+				echo $sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, value,sort) VALUES (
 								'".Database::escape_string($form_content['question_id'])."',
 								'".Database::escape_string($form_content['survey_id'])."',
-								'".Database::escape_string($answer)."',
-								'".Database::escape_string($counter)."')";
+								'".Database::escape_string($form_content['answers'][$i])."',
+								'".Database::escape_string($form_content['values'][$i])."',
+								'".Database::escape_string($counter)."')"; 
 				$result = api_sql_query($sql, __FILE__, __LINE__);
 				$counter++;
+				echo "<br>";
 			}
+			
 		}
 	}
 
@@ -1226,10 +1251,11 @@ class question
 		if($survey_data['survey_type']==1)
 		{
 			$table_surve_group = Database::get_course_table(TABLE_SURVEY_GROUP);
-			$sql = 'SELECT id,name FROM '.$table_surve_group.' WHERE survey_id = '.(int)$_GET['survey_id'];
+			$sql = 'SELECT id,name FROM '.$table_surve_group.' WHERE survey_id = '.(int)$_GET['survey_id'].' ORDER BY name';
 			$rs = api_sql_query($sql,__FILE__,__LINE__);
 			
-			while($row = Database::fetch_array($rs,NUM)){
+			while($row = Database::fetch_array($rs,NUM))
+			{
 				$glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>';
 			}	
 			
@@ -1340,11 +1366,11 @@ class question
 		{					
 			$message = survey_manager::save_question($form_content);
 									
-			if ($message == 'QuestionAdded' || $message == 'QuestionUpdated' ) {
+			if ($message == 'QuestionAdded' || $message == 'QuestionUpdated' ) 
+			{
 				
 				$sql='SELECT COUNT(*) FROM '.Database :: get_course_table(TABLE_SURVEY_QUESTION).' WHERE survey_id = '.(int)$_GET['survey_id'];
-				$res = Database :: fetch_array (api_sql_query($sql, __FILE__, __LINE__));
-				
+				$res = Database :: fetch_array (api_sql_query($sql, __FILE__, __LINE__));				
 				
 				if ($config['survey']['debug'])
 				{								
@@ -1358,10 +1384,12 @@ class question
 			}
 			else 
 			{			
-				if ($message == 'PleaseEnterAQuestion' || $message=='PleasFillAllAnswer'|| $message=='PleaseChooseACondition'|| $message=='ChooseDifferentCategories'){							
+				if ($message == 'PleaseEnterAQuestion' || $message=='PleasFillAllAnswer'|| $message=='PleaseChooseACondition'|| $message=='ChooseDifferentCategories')
+				{							
 					$_SESSION['temp_user_message']=$form_content['question'];
 					$_SESSION['temp_sys_message']=$message;
-					$_SESSION['temp_answers']=$form_content['answers'];																						
+					$_SESSION['temp_answers']=$form_content['answers'];
+					$_SESSION['temp_values']=$form_content['values'];																								
 					header('location:question.php?'.api_get_cidreq().'&question_id='.Security::remove_XSS($_GET['question_id']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&action='.Security::remove_XSS($_GET['action']).'&type='.Security::remove_XSS($_GET['type']).'');																  										
 				}
 				
@@ -1619,6 +1647,126 @@ class multiplechoice extends question
 }
 
 
+class personality extends question
+{
+	/**
+	 * This function creates the form elements for the multiple response questions
+	 *
+	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+	 * @version January 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>';
+		$this->html .='		<tr>
+								<td colspan="">&nbsp;</td>
+							</tr>';
+
+		// The options
+		$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']);
+		
+		
+		$question_values=array();
+		
+		// values of question options				 
+		foreach ($form_content['values'] as $key=>$value)
+		{
+			$question_values [] = '<input size="3" type="text" id="values['.$key.']" name="values['.$key.']" value="'.$value.'" />';	
+		}
+		
+		$count=0;
+		
+		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 width="500">'.api_return_html_area('answers['.$key.']', html_entity_decode(stripslashes($form_content['answers'][$key]))).'</td>';
+			$this->html .= '		<td>';
+			
+			if ($total_number_of_answers> 2)
+			{
+				$this->html .=$question_values[$count];				
+			}
+			
+			if ($key<$total_number_of_answers-1)
+			{
+				$this->html .= '		<input type="image" src="../img/down.gif"  value="move_down['.$key.']" name="move_down['.$key.']"/>';
+			}
+			else
+			{
+				$this->html .= '		<img src="../img/spacer.gif" alt="'.get_lang('Empty').'" title="'.get_lang('Empty').'" />';
+			}
+			
+			if ($key>0)
+			{
+				$this->html .= '		<input type="image" src="../img/up.gif"  value="move_up['.$key.']" name="move_up['.$key.']"/>';
+			}
+			else
+			{
+				$this->html .= '		<img src="../img/spacer.gif" alt="'.get_lang('Empty').'" title="'.get_lang('Empty').'" />';
+			}
+			
+			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>';
+			$count++;
+		}
+		
+	
+			
+		
+		// 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 January 2007
+	 */
+	function render_question($form_content, $answers=array())
+	{
+		$question = new yesno();
+		$question->render_question($form_content, $answers);
+	}
+}
+
+
 
 class multipleresponse extends question
 {

+ 60 - 31
main/survey/survey.php

@@ -1,27 +1,30 @@
 <?php
 /*
-DOKEOS - elearning and course management software
+==============================================================================
+	Dokeos - elearning and course management software
 
-For a full list of contributors, see documentation/credits.html
+	Copyright (c) 2004-2008 Dokeos SPRL
 
-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.
+	For a full list of contributors, see "credits.txt".
+	The full license can be read in "license.txt".
 
-Contact:
-Dokeos
-Rue des Palais 44 Paleizenstraat
-B-1030 Brussels - Belgium
-Tel. +32 (2) 211 34 56
+	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 the GNU General Public License for more details.
+
+	Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
+	Mail: info@dokeos.com
+==============================================================================
 */
 
 /**
 *	@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 15846 2008-07-24 20:29:41Z dperales $
+* 	@version $Id: survey.php 15875 2008-07-30 23:21:03Z juliomontoya $
 *
 * 	@todo use quickforms for the forms
 */
@@ -67,14 +70,20 @@ if (strlen(strip_tags($survey_data['title'])) > 40)
 	$tool_name .= '...';
 }
 
-if($is_survey_type_1 && ($_GET['action']=='addgroup')||($_GET['action']=='deletegroup')){
+if($is_survey_type_1 && ($_GET['action']=='addgroup')||($_GET['action']=='deletegroup'))
+{
 	$table_survey_group = Database::get_course_table(TABLE_SURVEY_GROUP);
 	$_POST['name'] = trim($_POST['name']);
-	if(($_GET['action']=='addgroup')){
-		if(!empty($_POST['group_id'])){
+	
+	if(($_GET['action']=='addgroup'))
+	{
+		if(!empty($_POST['group_id']))
+		{
 			api_sql_query('UPDATE '.$table_survey_group.' SET description = \''.Database::escape_string($_POST['description']).'\' WHERE id = \''.Database::escape_string($_POST['group_id']).'\'');
 			$sendmsg = 'GroupUpdatedSuccessfully';
-		} elseif(!empty($_POST['name'])){
+		} 
+		elseif(!empty($_POST['name']))
+		{
 			api_sql_query('INSERT INTO '.$table_survey_group.' (name,description,survey_id) values (\''.Database::escape_string($_POST['name']).'\',\''.Database::escape_string($_POST['description']).'\',\''.Database::escape_string($_GET['survey_id']).'\') ');	
 			$sendmsg = 'GroupCreatedSuccessfully';
 		} else {
@@ -140,18 +149,35 @@ $survey_actions .= '<a href="survey_invite.php?'.api_get_cidreq().'&amp;survey_i
 $survey_actions .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$_GET['survey_id'].'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
 echo '<div style="float:right;">'.$survey_actions.'</div>';
 
-echo '<div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=yesno&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" /><br />'.get_lang('YesNo').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multiplechoice&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcua.gif" /><br />'.get_lang('MultipleChoice').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multipleresponse&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcma.gif" /><br />'.get_lang('MultipleResponse').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=open&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/open_answer.gif" /><br />'.get_lang('Open').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=dropdown&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/dropdown.gif" /><br />'.get_lang('Dropdown').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=percentage&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/percentagequestion.gif" /><br />'.get_lang('Percentage').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=score&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/scorequestion.gif" /><br />'.get_lang('Score').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=comment&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/commentquestion.gif" /><br />'.get_lang('Comment').'</a></div>';
-echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=pagebreak&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/page_end.gif" /><br />'.get_lang('Pagebreak').'</a></div>';
-echo '</div>';
-echo '<div style="clear:both;"></div>';
+//print_r($survey_data);
+
+if ($survey_data['survey_type']==0)
+{	
+	echo '<div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=yesno&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" /><br />'.get_lang('YesNo').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multiplechoice&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcua.gif" /><br />'.get_lang('MultipleChoice').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=multipleresponse&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/mcma.gif" /><br />'.get_lang('MultipleResponse').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=open&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/open_answer.gif" /><br />'.get_lang('Open').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=dropdown&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/dropdown.gif" /><br />'.get_lang('Dropdown').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=percentage&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/percentagequestion.gif" /><br />'.get_lang('Percentage').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=score&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/scorequestion.gif" /><br />'.get_lang('Score').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=comment&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/commentquestion.gif" /><br />'.get_lang('Comment').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=pagebreak&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/page_end.gif" /><br />'.get_lang('Pagebreak').'</a></div>';
+	echo '</div>';
+	echo '<div style="clear:both;"></div>';
+
+}
+else
+{	
+	echo '<div>';
+	//echo '<div style="float:left; text-align:center; margin:5px;"><a href="group.php?'.api_get_cidreq().'&amp;action=add&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" /><br />'.get_lang('Add groups').'</a></div>';
+	echo '<div style="float:left; text-align:center; margin:5px;"><a href="question.php?'.api_get_cidreq().'&amp;action=add&type=personality&amp;survey_id='.$_GET['survey_id'].'"><img src="../img/yesno.gif" /><br />'.get_lang('PersonalityQuestion').'</a></div>';
+	echo '</div>';	 
+	echo '<div style="clear:both;"></div>';	
+}
+
+
+
 
 // Displaying the table header with all the questions
 echo '<table class="data_table">';
@@ -240,7 +266,9 @@ if($is_survey_type_1)
 		echo	'<input type="text" maxlength="150" name="description" value="'.$editedrow['description'].'" size="40">';	
 		echo	'<input type="hidden" name="group_id" value="'.(int)$_GET['gid'].'">';		
 		echo	'<input type="submit" value="'.get_lang('Save').'"'.'<input type="button" value="'.get_lang('Cancel').'" onclick="window.location.href = \'survey.php?survey_id='.(int)$_GET['survey_id'].'\';" />';
-	} else {
+	} 
+	else 
+	{
 		echo	'<input type="text" maxlength="20" name="name" value="" size="10">';
 		echo	'<input type="text" maxlength="250" name="description" value="" size="80">';
 		echo	'<input type="submit" value="'.get_lang('Create').'"';	
@@ -254,7 +282,8 @@ if($is_survey_type_1)
 	echo '		<th width="100">'.get_lang('Modify').'</th>';
 	echo '	</tr>';	
 	
-	$sql = 'SELECT id,name,description FROM '.$table_surve_group.' WHERE survey_id = '.Database::escape_string($_GET['survey_id']);
+	$sql = 'SELECT id,name,description FROM '.$table_surve_group.' WHERE survey_id = '.Database::escape_string($_GET['survey_id']).' ORDER BY name';
+	
 	$rs = api_sql_query($sql,__FILE__,__LINE__);
 	while($row = Database::fetch_array($rs,ASSOC)){
 		$grouplist .= '<tr><td>'.$row['name'].'</td><td>'.$row['description'].'</td><td>'.