Ver código fonte

[svn r15840] added new type of survey. lang constants are not declared yet.

Daniel Perales 16 anos atrás
pai
commit
24314a58f0

+ 24 - 3
main/survey/create_new_survey.php

@@ -24,7 +24,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 (if not all) of the code
-* 	@version $Id: create_new_survey.php 15616 2008-06-25 10:21:57Z elixir_inter $
+* 	@version $Id: create_new_survey.php 15840 2008-07-23 22:59:44Z dperales $
 *
 * 	@todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter)
 */
@@ -149,7 +149,28 @@ $fck_attribute['Height'] = '200';
 //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
 $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
 $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'));
-$form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'));
+$form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'));
+
+$surveytypes[0] = get_lang('Normal');
+$surveytypes[1] = get_lang('Conditional');
+if ($_GET['action'] == 'add'){
+	$form->addElement('select', 'survey_type', get_lang('SelectType'), $surveytypes);
+	
+	$sql = 'SELECT survey_id,title FROM '.$table_survey.' WHERE type = 1';
+	$rs = api_sql_query($sql,__FILE__,__LINE__);
+	$list_surveys[0] = '';
+	while($row = Database::fetch_array($rs,NUM)){
+		$list_surveys[$row[0]] = $row[1];
+	}
+	
+	if(count($list_surveys)>1)$form->addElement('select', 'parent_id', get_lang('ParentId'), $list_surveys);
+}
+
+if ($survey_data['type']==1 || $_GET['action'] == 'add' ){
+	$form->addElement('checkbox', 'one_question_page', get_lang('OneQuestionPerPage'));
+	$form->addElement('checkbox', 'shuffle', get_lang('ActivateShuffle'));
+}
+
 $form->addElement('submit', 'submit_survey', get_lang('Ok'));
 
 // setting the rules
@@ -217,4 +238,4 @@ else
 
 // Footer
 Display :: display_footer();
-?>
+?>

+ 14 - 2
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 15556 2008-06-11 20:53:01Z juliomontoya $
+* 	@version $Id: question.php 15840 2008-07-23 22:59:44Z dperales $
 */
 
 // name of the language file that needs to be included
@@ -52,6 +52,8 @@ $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);
+
+
 
 // getting the survey information
 $survey_data = survey_manager::get_survey($_GET['survey_id']);
@@ -59,6 +61,16 @@ $urlname = substr(html_entity_decode($survey_data['title'],ENT_QUOTES,$charset),
 if (strlen(strip_tags($survey_data['title'])) > 40)
 {
 	$urlname .= '...';
+}
+
+if($survey_data['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__);
+	$row = Database::fetch_array($rs,NUM);
+	if($row==false) {
+		header('Location: survey.php?survey_id='.(int)$_GET['survey_id'].'&message='.'YouNeedToCreateGroups');
+		exit;	
+	}
 }
 
 // breadcrumbs
@@ -99,7 +111,7 @@ if (empty($_POST['save_question']) && in_array($_GET['type'],$possible_types))
 	{	
 		$error_message=$_SESSION['temp_sys_message'];
 		unset($_SESSION['temp_sys_message']);				
-		if ($error_message=='PleaseEnterAQuestion' || $error_message=='PleasFillAllAnswer')
+		if ($error_message=='PleaseEnterAQuestion' || $error_message=='PleasFillAllAnswer'|| $error_message=='PleaseChooseACondition'|| $error_message=='ChooseDifferentCategories')
 		{
 			Display::display_error_message(get_lang($error_message), true);			
 		}		

+ 214 - 17
main/survey/survey.lib.php

@@ -23,14 +23,13 @@
 /**
 *	@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 15774 2008-07-14 02:46:42Z yannoo $
+* 	@version $Id: survey.lib.php 15840 2008-07-23 22:59:44Z dperales $
 *
 * 	@todo move this file to inc/lib
 * 	@todo use consistent naming for the functions (save vs store for instance)
 */
 $config['survey']['debug'] = false;
 require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
-
 class survey_manager
 {
 
@@ -78,6 +77,11 @@ class survey_manager
 		$return['survey_share'] 		= $return['is_shared'];
 		$return['survey_introduction'] 	= $return['intro'];
 		$return['survey_thanks'] 		= $return['surveythanks'];
+		$return['survey_type'] 		= $return['type'];
+		$return['one_question_page'] 		= $return['one_question_page'];
+		$return['shuffle'] 		= $return['shuffle'];
+		$return['parent_id'] 		= $return['parent_id'];
+		$return['version'] 		= $return['version'];
 		return $return;
 	}
 
@@ -120,7 +124,52 @@ class survey_manager
 			{
 				$values['anonymous']=0;
 			}
-			$sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date, anonymous) VALUES (
+			
+			$additional['columns'] = '';
+			$additional['values'] = '';
+			if($values['survey_type']==1){
+				$additional['columns'] = ', type';
+				$additional['values'] .= ",'1'";
+
+				$additional['columns'] .= ', shuffle';
+				$additional['values'] .= ",'".Database::escape_string($values['shuffle'])."'";
+
+				$additional['columns'] .= ', one_question_page';
+				$additional['values'] .= ",'".Database::escape_string($values['one_question_page'])."'";
+
+				$additional['columns'] .= ', parent_id';
+				$additional['values'] .= ",'".Database::escape_string($values['parent_id'])."'";
+
+				// logic for versioning surveys
+				if(!empty($values['parent_id'])){
+					$additional['columns'] .= ', version';
+					$sql = 'SELECT version FROM '.$table_survey.' WHERE parent_id = '.$values['parent_id'].' ORDER BY version DESC LIMIT 1';
+					$rs = api_sql_query($sql,__FILE__,__LINE__);
+					$row = Database::fetch_array($rs,ASSOC);
+					if($row==false) {
+						$sql = 'SELECT 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['version'])){
+							$additional['values'] .= ",'".++$getversion['version']."'";
+						} else {
+							$additional['values'] .= ",'".$getversion['version'].".1'";							 
+						}
+					} else {
+						if(strpos($row['version'], '.')===false){
+							$additional['values'] .= ",'".($row['version']+1)."'";
+						} else {
+							$getlast= split('\.',$row['version']);
+							$lastversion = array_pop($getlast);
+							$lastversion++;
+							$insertnewversion = implode('.',$getlast).'.'.$lastversion;
+							$additional['values'] .= ",'".$insertnewversion."'";
+						}
+					}
+				}
+			}
+			
+			$sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date, anonymous".$additional['columns'].") VALUES (
 						'".Database::escape_string($values['survey_code'])."',
 						'".Database::escape_string($values['survey_title'])."',
 						'".Database::escape_string($values['survey_subtitle'])."',
@@ -133,10 +182,14 @@ class survey_manager
 						'".Database::escape_string($values['survey_introduction'])."',
 						'".Database::escape_string($values['survey_thanks'])."',
 						'".date('Y-m-d H:i:s')."',
-						'".Database::escape_string($values['anonymous'])."'
+						'".Database::escape_string($values['anonymous'])."'".$additional['values']."
 						)";
 			$result = api_sql_query($sql, __FILE__, __LINE__);
 			$survey_id = Database::insert_id();
+			
+			if($values['survey_type']==1 && !empty($values['parent_id'])){
+				survey_manager::copy_survey($values['parent_id'],$survey_id);
+			}
 
 			//$return['message'] = get_lang('SurveyCreatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
 			//$return['message'] .= '<a href="survey.php?survey_id='.$survey_id.'">'.get_lang('ClickHere').'</a>';
@@ -150,6 +203,10 @@ class survey_manager
 			{
 				$values['anonymous']=0;
 			}
+			
+			$additionalsets = ", shuffle = '".Database::escape_string($values['shuffle'])."'";
+			$additionalsets .= ", one_question_page = '".Database::escape_string($values['one_question_page'])."'";
+			
 			$sql = "UPDATE $table_survey SET
 							code 			= '".Database::escape_string($values['survey_code'])."',
 							title 			= '".Database::escape_string($values['survey_title'])."',
@@ -162,7 +219,7 @@ class survey_manager
 							template 		= '".Database::escape_string('template')."',
 							intro			= '".Database::escape_string($values['survey_introduction'])."',
 							surveythanks	= '".Database::escape_string($values['survey_thanks'])."',
-							anonymous	= '".Database::escape_string($values['anonymous'])."'
+							anonymous	= '".Database::escape_string($values['anonymous'])."'".$additionalsets."
 					WHERE survey_id = '".Database::escape_string($values['survey_id'])."'";
 			$result = api_sql_query($sql, __FILE__, __LINE__);
 
@@ -241,6 +298,7 @@ class survey_manager
 	{
 		// Database table definitions
 		$table_survey 		= Database :: get_course_table(TABLE_SURVEY);
+		$table_survey_group = Database :: get_course_table(TABLE_SURVEY_GROUP);
 		if ($shared)
 		{
 			$table_survey 	= Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
@@ -249,6 +307,10 @@ class survey_manager
 		// deleting the survey
 		$sql = "DELETE from $table_survey WHERE survey_id='".Database::escape_string($survey_id)."'";
 		$res = api_sql_query($sql, __FILE__, __LINE__);
+		
+		// deleting groups of this survey
+		$sql = "DELETE from $table_survey_group WHERE survey_id='".Database::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, $shared);
@@ -256,6 +318,45 @@ class survey_manager
 		return true;
 	}
 	
+	function copy_survey($parent_survey, $new_survey_id)
+	{
+		// Database table definitions
+		$table_survey 		= Database :: get_course_table(TABLE_SURVEY);
+		$table_survey_group = Database :: get_course_table(TABLE_SURVEY_GROUP);
+		$table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
+		$table_survey_options = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
+
+		//get groups
+		$sql = "SELECT * from $table_survey_group WHERE survey_id='".$parent_survey."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);		
+		while($row = Database::fetch_array($res,ASSOC)){
+			$sql1 = 'INSERT INTO '.$table_survey_group.' (name,description,survey_id) VALUES (\''.Database::escape_string($row['name']).'\',\''.Database::escape_string($row['description']).'\',\''.$new_survey_id.'\')';
+			$res1 = api_sql_query($sql1, __FILE__, __LINE__);		
+			$group_id[$row['id']] = Database::insert_id();
+		}
+
+		//get questions
+		$sql = "SELECT * FROM $table_survey_question WHERE survey_id='".$parent_survey."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		while($row = Database::fetch_array($res,ASSOC)){
+			$sql2 = 'INSERT INTO '.$table_survey_question.' (survey_id,survey_question,survey_question_comment,type,display,sort,shared_question_id,max_value,cond_group_basic,cond_group_sec1,cond_group_sec2) VALUES '.
+			'(\''.$new_survey_id.'\',\''.Database::escape_string($row['survey_question']).'\',\''.Database::escape_string($row['survey_comment']).'\',\''.$row['type'].'\',\''.$row['display'].'\',\''.$row['sort'].'\',\''.$row['shared_question_id'].'\',\''.$row['max_value'].
+			'\',\''.$group_id[$row['cond_group_basic']].'\',\''.$group_id[$row['cond_group_sec1']].'\',\''.$group_id[$row['cond_group_sec2']].'\')';
+			$res2 = api_sql_query($sql2, __FILE__, __LINE__);
+			$question_id[$row['question_id']] = Database::insert_id();		
+		}
+		
+		//get questions options
+		$sql = "SELECT * FROM $table_survey_options WHERE  survey_id='".$parent_survey."'";
+		$res = api_sql_query($sql, __FILE__, __LINE__);
+		while($row = Database::fetch_array($res,ASSOC)){
+			$sql3 = 'INSERT INTO '.$table_survey_options.' (question_id,survey_id,option_text,sort) VALUES ('.
+			"'".$question_id[$row['question_id']]."','".$new_survey_id."','".Database::escape_string($row['option_text'])."','".$row['sort']."')";
+			$res3 = api_sql_query($sql3, __FILE__, __LINE__);		
+		}
+		return true;
+	}	
+	
 	/**
 	 * This function duplicates a survey (and also all the question in that survey
 	 *
@@ -417,7 +518,16 @@ class survey_manager
 	    $return['horizontalvertical'] 	= $row['display'];
 	    $return['shared_question_id']	= $row['shared_question_id'];
 	    $return['maximum_score']		= $row['max_value'];
-
+ 		
+  		if($row['cond_group_basic']!=0){
+  			$return['assigned'] = $row['cond_group_basic'];
+	 		$return['choose'] = 1;
+   		} else {
+ 	 		$return['assigned1'] = $row['cond_group_sec1'];
+  			$return['assigned2'] = $row['cond_group_sec2'];
+	 		$return['choose'] = 2;
+  		}
+ 		
 	    // getting the information of the question options
 		$sql = "SELECT * FROM $table_survey_question_option WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort` ";
 		$result = api_sql_query($sql, __FILE__, __LINE__);
@@ -471,6 +581,7 @@ class survey_manager
 		{
 			$return[$row['question_id']]['answers'][] = $row['option_text'];
 		}
+
 		return $return;
 	}
 
@@ -483,11 +594,29 @@ class survey_manager
 	 * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
 	 * @version January 2007
 	 */
+	 
 	function save_question($form_content)
-	{			
+	{	
+	global $survey_data;
+	
 		if (strlen($form_content['question'])>1)
 		{		//checks lenght of the question	
 			$empty_answer=false;	
+			
+			if ($survey_data['survey_type'] == 1)
+			{
+				if (empty($form_content['choose'])){
+					$return_message = 'PleaseChooseACondition';	 
+					return $return_message;	
+				}
+			
+				if (($form_content['choose']==2)&&($form_content['assigned1']==$form_content['assigned2']))
+				{
+					$return_message = 'ChooseDifferentCategories';	 
+					return $return_message;	
+				}
+				
+			}
 					
 			if ($form_content['type'] != 'percentage')
 			{
@@ -534,9 +663,22 @@ class survey_manager
 					$result = api_sql_query($sql, __FILE__, __LINE__);
 					$row = Database::fetch_array($result,'ASSOC');
 					$max_sort = $row['max_sort'];
+					
+					//some variables defined for survey-test type
+					$additional['column'] = '';
+					$additional['value'] = '';
+					if($_POST['choose']==1)
+					{
+						$additional['column'] = ',cond_group_basic';	
+						$additional['value'] = ",'".Database::escape_string($_POST['assigned'])."'";	
+					} elseif($_POST['choose']==2) {
+						$additional['column'] = ',cond_group_sec1, cond_group_sec2';	
+						$additional['value'] = ",'".Database::escape_string($_POST['assigned1'])."'".",'".Database::escape_string($_POST['assigned2'])."'";	
+					}
+
 		
 					// adding the question to the survey_question table
-					$sql = "INSERT INTO $tbl_survey_question (survey_id,survey_question,survey_question_comment,type,display, sort, shared_question_id, max_value) VALUES (
+					$sql = "INSERT INTO $tbl_survey_question (survey_id,survey_question,survey_question_comment,type,display, sort, shared_question_id, max_value".$additional['column'].") VALUES (
 								'".Database::escape_string($form_content['survey_id'])."',
 								'".Database::escape_string($form_content['question'])."',
 								'".Database::escape_string($form_content['question_comment'])."',
@@ -544,7 +686,8 @@ class survey_manager
 								'".Database::escape_string($form_content['horizontalvertical'])."',
 								'".Database::escape_string($max_sort+1)."',
 								'".Database::escape_string($form_content['shared_question_id'])."',
-								'".Database::escape_string($form_content['maximum_score'])."'
+								'".Database::escape_string($form_content['maximum_score'])."'".
+								$additional['value']."
 								)";
 					$result = api_sql_query($sql, __FILE__, __LINE__);
 					$question_id = Database::insert_id();
@@ -554,12 +697,23 @@ class survey_manager
 				// updating an existing question
 				else
 				{
+					$additionalsets = '';
+					
+					if($_POST['choose']==1){
+						$additionalsets = ',cond_group_basic = \''.Database::escape_string($_POST['assigned']).'\', cond_group_sec1 = \'0\', cond_group_sec2 = \'0\' ';	
+					} elseif($_POST['choose']==2) {
+						$additionalsets = ',cond_group_basic = \'0\', cond_group_sec1 = \''.Database::escape_string($_POST['assigned1']).'\', cond_group_sec2 = \''.Database::escape_string($_POST['assigned2']).'\' ';		
+					}
+					
+					$setadditionals = $additional['set'][1].$additional['set'][2].$additional['set'][3];
+
 					// adding the question to the survey_question table
 					$sql = "UPDATE $tbl_survey_question SET
 								survey_question 		= '".Database::escape_string($form_content['question'])."',
 								survey_question_comment = '".Database::escape_string($form_content['question_comment'])."',
 								display 				= '".Database::escape_string($form_content['horizontalvertical'])."',
-								max_value 				= '".Database::escape_string($form_content['maximum_score'])."'
+								max_value 				= '".Database::escape_string($form_content['maximum_score'])."'" .
+								$additionalsets."
 								WHERE question_id 		= '".Database::escape_string($form_content['question_id'])."'";
 					$result = api_sql_query($sql, __FILE__, __LINE__);
 					$return_message = 'QuestionUpdated'; 
@@ -710,7 +864,7 @@ class survey_manager
 		// deleting the survey questions
 		$sql = "DELETE from $table_survey_question WHERE survey_id='".Database::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, $shared);
 
@@ -743,6 +897,7 @@ class survey_manager
 		// deleting the survey questions
 		$sql = "DELETE from $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'";
 		$res = api_sql_query($sql, __FILE__, __LINE__);
+	
 
 		// deleting the options of the question of the survey
 		survey_manager::delete_survey_question_option($survey_id, $question_id, $shared);
@@ -1039,6 +1194,7 @@ class question
 	function create_form($form_content)
 	{
 		global $fck_attribute;
+		global $survey_data;
 		$this->html = '<form id="question_form" name="question_form" method="post" action="'.api_get_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'].'"/>';
@@ -1064,9 +1220,44 @@ class question
 		$this->html .= '	</tr>';
 		*/
 
-		$this->html .='		<tr>
-								<td colspan="">&nbsp;</td>
-							</tr>';
+		$this->html .='	<tr><td colspan="">&nbsp;</td></tr>'; 
+
+		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'];
+			$rs = api_sql_query($sql,__FILE__,__LINE__);
+			
+			while($row = Database::fetch_array($rs,NUM)){
+				$glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>';
+			}	
+			
+			$grouplist = $grouplist1 = $grouplist2 = $glist;
+			
+			if(!empty($form_content['assigned']))
+				$grouplist = str_replace('<option value="'.$form_content['assigned'].'"','<option value="'.$form_content['assigned'].'" selected',$glist);
+			
+			if(!empty($form_content['assigned1']))
+				$grouplist1 = str_replace('<option value="'.$form_content['assigned1'].'"','<option value="'.$form_content['assigned1'].'" selected',$glist);
+				
+			if(!empty($form_content['assigned2']))
+				$grouplist2 = str_replace('<option value="'.$form_content['assigned2'].'"','<option value="'.$form_content['assigned2'].'" selected',$glist);
+			
+			$this->html .='	<tr><td colspan="">	
+			<fieldset style="border:1px solid black"><legend>'.get_lang('Condition').'</legend>
+
+			<b>'.get_lang('Basic').'</b><br />
+			'.'<input type="radio" name="choose" value="1" '.(($form_content['choose']==1)?'checked':'').
+			'><select name="assigned">'.$grouplist.'</select><br />';
+			
+			$this->html .='			
+			<b>'.get_lang('Secondary').'</b><br />
+			'.'<input type="radio" name="choose" value="2" '.(($form_content['choose']==2)?'checked':'').
+			'><select name="assigned1">'.$grouplist1.'</select> '.
+			'<select name="assigned2">'.$grouplist2.'</select>'
+			.'</fieldset><br />';
+		}
+
 		return $this->html;
 	}
 
@@ -1148,7 +1339,12 @@ 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__));
+				
+				
 				if ($config['survey']['debug'])
 				{								
 					Display :: display_header();
@@ -1161,11 +1357,11 @@ class question
 			}
 			else 
 			{			
-				if ($message == 'PleaseEnterAQuestion' || $message=='PleasFillAllAnswer'){							
+				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'];																						
-					header('location:question.php?'.api_get_cidreq().'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&action='.Security::remove_XSS($_GET['action']).'&type='.Security::remove_XSS($_GET['type']).'');																  										
+					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']).'');																  										
 				}
 				
 			}					
@@ -1191,6 +1387,7 @@ class question
 			}
 		}
 		return $form_content;
+		
 	}
 
 	/**

+ 96 - 3
main/survey/survey.php

@@ -21,7 +21,7 @@ Tel. +32 (2) 211 34 56
 *	@package dokeos.survey
 * 	@author unknown
 * 	@author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
-* 	@version $Id: survey.php 15555 2008-06-11 20:39:15Z juliomontoya $
+* 	@version $Id: survey.php 15840 2008-07-23 22:59:44Z dperales $
 *
 * 	@todo use quickforms for the forms
 */
@@ -53,6 +53,7 @@ $table_survey_question_option 	= Database :: get_course_table(TABLE_SURVEY_QUEST
 $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);
+$table_survey_question_group	= Database :: get_main_table(TABLE_SURVEY_QUESTION_GROUP);
 
 // breadcrumbs
 $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
@@ -66,6 +67,30 @@ if (strlen(strip_tags($survey_data['title'])) > 40)
 	$tool_name .= '...';
 }
 
+if(($_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'])){
+			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'])){
+			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 {
+			$sendmsg = 'GroupNeedName';
+		}
+	}	
+		
+	if($_GET['action']=='deletegroup'){
+		api_sql_query('DELETE FROM '.$table_survey_group.' WHERE id = '.Database::escape_string($_GET['gid']).' and survey_id = '.Database::escape_string($_GET['survey_id']));
+		$sendmsg = 'GroupDeletedSuccessfully';	
+	}
+	
+	header('Location:survey.php?survey_id='.(int)$_GET['survey_id'].'&sendmsg='.$sendmsg);
+	exit;
+}
+
 // Displaying the header
 Display::display_header($tool_name,'Survey'); 
 
@@ -94,9 +119,14 @@ if (isset($_GET['message']))
 	if (in_array($_GET['message'], array('QuestionAdded','QuestionUpdated')))
 	{
 		Display::display_confirmation_message(get_lang($_GET['message']), false);
+	}
+	
+	if (in_array($_GET['message'], array('YouNeedToCreateGroups')))
+	{
+		Display::display_warning_message(get_lang($_GET['message']), false);
 	}
 }
-
+if(!empty($survey_data['version'])) echo '<b>'.get_lang('Version').': '.$survey_data['version'].'</b>';
 // We exit here is the first or last question is a pagebreak (which causes errors)
 SurveyUtil::check_first_last_question($_GET['survey_id']);
 
@@ -131,12 +161,13 @@ echo '		<th>'.get_lang('Title').'</th>';
 echo '		<th>'.get_lang('Type').'</th>';
 echo '		<th>'.get_lang('NumberOfOptions').'</th>';
 echo '		<th width="100">'.get_lang('Modify').'</th>';
+if($survey_data['type']==1) echo '<th width="100">'.get_lang('Condition').'</th>';
 echo '	</tr>';
 // Displaying the table contents with all the questions
 $question_counter = 1;
 $sql = "SELECT survey_question.*, count(survey_question_option.question_option_id) as number_of_options
 			FROM $table_survey_question survey_question
-			LEFT JOIN $table_survey_question_option survey_question_option
+			LEFT JOIN $table_survey_question_option survey_question_option 
 			ON survey_question.question_id = survey_question_option.question_id
 			WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
 			GROUP BY survey_question.question_id
@@ -165,16 +196,78 @@ while ($row = mysql_fetch_assoc($result))
 	if ($question_counter > 1)
 	{
 		echo '		<a href="survey.php?'.api_get_cidreq().'&amp;action=moveup&amp;survey_id='.$_GET['survey_id'].'&amp;question_id='.$row['question_id'].'">'.Display::return_icon('up.gif', get_lang('MoveUp')).'</a>';
+	} else {
+		echo ' <img src="../img/up_na.gif"> ';	
 	}
 	if ($question_counter < $question_counter_max)
 	{
 		echo '		<a href="survey.php?'.api_get_cidreq().'&amp;action=movedown&amp;survey_id='.$_GET['survey_id'].'&amp;question_id='.$row['question_id'].'">'.Display::return_icon('down.gif', get_lang('MoveDown')).'</a>';
+	} else {
+		echo ' <img src="../img/down_na.gif"> ';		
 	}
 	echo '	</td>';
 	$question_counter++;
+	
+	if($survey_data['type']==1) echo '<td>'.(($survey_data['cond_group_basic']==0)?get_lang('Basic'):get_lang('Secondary')).'</td>';
+	echo '</tr>'; 
 }
 echo '</table>';
 
+if($survey_data['type']==1)
+{
+	echo '<br /><br /><b>'.get_lang('ManageGroups').'</b><br /><br />';
+	
+	if (in_array($_GET['sendmsg'], array('GroupUpdatedSuccessfully','GroupDeletedSuccessfully','GroupCreatedSuccessfully'))){
+		echo Display::display_confirmation_message(get_lang($_GET['sendmsg']), false);
+	}
+	
+	if (in_array($_GET['sendmsg'], array('GroupNeedName'))){
+		echo Display::display_warning_message(get_lang($_GET['sendmsg']), false);
+	}
+	
+	echo '<table border="0"><tr><td width="100">'.get_lang('Name').'</td><td>'.get_lang('Description').'</td></tr></table>';
+	
+	$table_surve_group = Database::get_course_table(TABLE_SURVEY_GROUP);
+
+	echo '<form action="survey.php?action=addgroup&survey_id='.(int)$_GET['survey_id'].'" method="post">';
+	if($_GET['action']=='editgroup')
+	{	
+		$sql = 'SELECT name,description FROM '.$table_surve_group.' WHERE id = '.(int)$_GET['gid'].' AND survey_id = '.Database::escape_string($_GET['survey_id']).' limit 1';
+		$rs = api_sql_query($sql,__FILE__,__LINE__);
+		$editedrow = Database::fetch_array($rs,ASSOC);
+	
+		echo	'<input type="text" maxlength="20" name="name" value="'.$editedrow['name'].'" size="10" disabled>';
+		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 {
+		echo	'<input type="text" maxlength="20" name="name" value="" size="10">';
+		echo	'<input type="text" maxlength="150" name="description" value="" size="40">';
+		echo	'<input type="submit" value="'.get_lang('Create').'"';	
+	}
+	echo	'</form><br />';
+
+	echo '<table class="data_table">';
+	echo '	<tr class="row_odd">';
+	echo '		<th width="200">'.get_lang('Name').'</th>';
+	echo '		<th>'.get_lang('Description').'</th>';
+	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']);
+	$rs = api_sql_query($sql,__FILE__,__LINE__);
+	while($row = Database::fetch_array($rs,NUM)){
+		$grouplist .= '<tr><td>'.$row[1].'</td><td>'.$row[2].'</td><td>'.
+		'<a href="survey.php?survey_id='.(int)$_GET['survey_id'].'&gid='.$row[0].'&action=editgroup">'.
+		Display::return_icon('edit.gif', get_lang('Edit')).'</a> '.
+		'<a href="survey.php?survey_id='.(int)$_GET['survey_id'].'&gid='.$row[0].'&action=deletegroup" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(sprintf(get_lang('DeleteSurveyGroup'),$row[1]).'?',ENT_QUOTES,$charset)).'\')) return false;">'.
+		Display::return_icon('delete.gif', get_lang('Delete')).'</a>'.
+		'</td></tr>';
+	}	
+				
+	echo $grouplist.'</table>';
+}
+
 // Footer
 Display :: display_footer();
 ?>