From 8dd0f882dc0982225c27ca3046faac9eddc1e7c7 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Thu, 25 Mar 2010 13:17:13 +0200 Subject: [PATCH] Feature #272 - "Surveys" tool: Revision and cleaning, part 2. --- main/survey/fillsurvey.php | 1002 ++++----- main/survey/preview.php | 131 +- main/survey/question.php | 124 +- main/survey/reporting.php | 114 +- main/survey/survey.download.inc.php | 72 +- main/survey/survey.lib.php | 3092 +++++++++++---------------- main/survey/survey.php | 144 +- main/survey/survey_invitation.php | 106 +- main/survey/survey_invite.php | 174 +- main/survey/survey_list.php | 166 +- 10 files changed, 2033 insertions(+), 3092 deletions(-) diff --git a/main/survey/fillsurvey.php b/main/survey/fillsurvey.php index a0b758a5f2..c7b13b728c 100755 --- a/main/survey/fillsurvey.php +++ b/main/survey/fillsurvey.php @@ -1,62 +1,39 @@ , Ghent University: cleanup, refactoring and rewriting large parts of the code -* @author Julio Montoya Armas , Dokeos: Personality Test modification and rewriting large parts of the code as well -* @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. -* @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 + * @package chamilo.survey + * @author unknown, the initial survey that did not make it in 1.8 because of bad code + * @author Patrick Cool , Ghent University: cleanup, refactoring and rewriting large parts of the code + * @author Julio Montoya Armas , Chamilo: Personality Test modification and rewriting large parts of the code as well + * @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. + * @todo performance could be improved if not the survey_id was stored with the invitation but the survey_code + */ + +// Language file that needs to be included $language_file = 'survey'; -// unsetting the course id (because it is in the URL) -if (!isset($_GET['cidReq'])) -{ +// Unsetting the course id (because it is in the URL) +if (!isset($_GET['cidReq'])) { $cidReset = true; -} -else -{ +} else { $_cid = $_GET['cidReq']; } -// including the global dokeos file -require ('../inc/global.inc.php'); +// Including the global initialization file +require '../inc/global.inc.php'; -// including additional libraries -//require_once (api_get_path(LIBRARY_PATH)."survey.lib.php"); -require_once('survey.lib.php'); -require_once (api_get_path(LIBRARY_PATH).'course.lib.php'); +// Including additional libraries +//require_once api_get_path(LIBRARY_PATH).'survey.lib.php'; +require_once 'survey.lib.php'; +require_once api_get_path(LIBRARY_PATH).'course.lib.php'; -// breadcrumbs -if (!empty($_user)) -{ - $interbreadcrumb[] = array ("url" => 'survey_list.php?cidReq='.$_GET['course'],'name' => get_lang('SurveyList')); +// Breadcrumbs +if (!empty($_user)) { + $interbreadcrumb[] = array('url' => 'survey_list.php?cidReq='.$_GET['course'], 'name' => get_lang('SurveyList')); } // Header @@ -74,82 +51,74 @@ $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); $table_user = Database :: get_main_table(TABLE_MAIN_USER); $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']); -// first we check if the needed parameters are present -if ((!isset ($_GET['course']) || !isset ($_GET['invitationcode']))&& !isset($_GET['user_id'])) -{ +// First we check if the needed parameters are present +if ((!isset($_GET['course']) || !isset($_GET['invitationcode']))&& !isset($_GET['user_id'])) { Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'), false); Display :: display_footer(); exit; } $invitationcode = $_GET['invitationcode']; -// start auto-invitation feature FS#3403 (all-users-can-do-the-survey-URL handling) -if ($invitationcode == "auto" && isset($_GET['scode'])){ - // not intended for anonymous users - if (!(isset ($_user['user_id']) && $_user['user_id']) || api_is_anonymous($_user['user_id'],true)) { + +// Start auto-invitation feature FS#3403 (all-users-can-do-the-survey-URL handling) +if ($invitationcode == 'auto' && isset($_GET['scode'])){ + // Not intended for anonymous users + if (!(isset($_user['user_id']) && $_user['user_id']) || api_is_anonymous($_user['user_id'],true)) { api_not_allowed(); } $userid = $_user['user_id']; - $scode = Database::escape_string($_GET['scode']); //survey_code of the survey - $autoInvitationcode = "auto-$userid-".$scode; //new invitation code from userid - // the survey code must exist in this course, or the URL is invalid + $scode = Database::escape_string($_GET['scode']); // Survey_code of the survey + $autoInvitationcode = "auto-$userid-$scode"; // New invitation code from userid + // The survey code must exist in this course, or the URL is invalid $sql = "SELECT * FROM $table_survey WHERE code='" . $scode . "'"; $result = Database::query($sql); - if (Database :: num_rows($result) > 0){ // ok - // check availability - $row = Database :: fetch_array($result, 'ASSOC'); // + if (Database :: num_rows($result) > 0) { // Ok + // Check availability + $row = Database :: fetch_array($result, 'ASSOC'); $tempdata = survey_manager :: get_survey($row['survey_id']); check_time_availability($tempdata); //exit if survey not available anymore - // check for double invitation records (insert should be done once) + // Check for double invitation records (insert should be done once) $sql = "SELECT user from $table_survey_invitation where invitation_code = '".Database::escape_string($autoInvitationcode)."'"; $result = Database::query($sql); - if (Database :: num_rows($result) == 0){ // ok + if (Database :: num_rows($result) == 0) { // Ok $sql = "insert into $table_survey_invitation (survey_code,user, invitation_code, invitation_date) "; $sql .= " values (\"$scode\", \"$userid\", \"$autoInvitationcode\", now())"; Database::query($sql); } - // from here we use the new invitationcode auto-userid-surveycode string + // From here we use the new invitationcode auto-userid-surveycode string $_GET['invitationcode'] = $autoInvitationcode; $invitationcode = $autoInvitationcode; } } -// end auto-invitation feature -// now we check if the invitationcode is valid +// Now we check if the invitationcode is valid $sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '" . Database :: escape_string($invitationcode) . "'"; -$result = Database::query($sql, __FILE__, false); // false=suppress errors -if (Database::num_rows($result) < 1) -{ +$result = Database::query($sql, __FILE__, false); // false = suppress errors +if (Database::num_rows($result) < 1) { Display :: display_error_message(get_lang('WrongInvitationCode'), false); Display :: display_footer(); exit; } -$survey_invitation = Database::fetch_array($result,'ASSOC'); +$survey_invitation = Database::fetch_array($result, 'ASSOC'); -// now we check if the user already filled the survey -if ($survey_invitation['answered'] == 1 && !isset($_GET['user_id'])) -{ +// Now we check if the user already filled the survey +if ($survey_invitation['answered'] == 1 && !isset($_GET['user_id'])) { Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false); Display :: display_footer(); exit(); } -// checking if there is another survey with this code. +// Checking if there is another survey with this code. // 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 = Database::query($sql); -if (Database::num_rows($result) > 1) -{ - if ($_POST['language']) - { +if (Database::num_rows($result) > 1) { + if ($_POST['language']) { $survey_invitation['survey_id'] = $_POST['language']; - } - else - { + } else { echo '
'; echo ' '; @@ -159,71 +128,57 @@ if (Database::num_rows($result) > 1) display::display_footer(); exit(); } -} -else -{ - $row=Database::fetch_array($result,'ASSOC'); +} else { + $row=Database::fetch_array($result, 'ASSOC'); $survey_invitation['survey_id'] = $row['survey_id']; } -// getting the survey information +// Getting the survey information $survey_data = survey_manager::get_survey($survey_invitation['survey_id']); //echo '
';print_r($survey_data);echo '
'; //echo $survey_data['form_fields'];echo '-
-';echo $survey_data['show_form_profile']; $survey_data['survey_id'] = $survey_invitation['survey_id']; //print_r($survey_data); -// storing the answers -if (count($_POST)>0) -{ - if ($survey_data['survey_type']==='0') - { - // getting all the types of the question (because of the special treatment of the score question type +// Storing the answers +if (count($_POST) > 0) { + if ($survey_data['survey_type'] === '0') { + // 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 = Database::query($sql); - while ($row = Database::fetch_array($result,'ASSOC')) - { + while ($row = Database::fetch_array($result, 'ASSOC')) { $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); + // 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 (strpos($key, 'question') !== false) { + // 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 + // 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 (is_array($value)) { SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id); - foreach ($value as $answer_key => $answer_value) - { - if ($types[$survey_question_id] == 'score') - { + foreach ($value as $answer_key => & $answer_value) { + if ($types[$survey_question_id] == 'score') { $option_id = $answer_key; $option_value = $answer_value; - } - else - { + } 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); } } - // all the other question types (open question, multiple choice, percentage, ...) - else - { - if ($types[$survey_question_id] == 'percentage') - { + // 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 = Database::query($sql); - $row = Database::fetch_array($result,'ASSOC'); + $row = Database::fetch_array($result, 'ASSOC'); $option_value = $row['option_text']; } else { $option_value = 0; @@ -240,66 +195,57 @@ if (count($_POST)>0) } } } - } - elseif ($survey_data['survey_type']==='1') //conditional/personality-test type surveys - { - // getting all the types of the question (because of the special treatment of the score question type + } elseif ($survey_data['survey_type'] === '1') { //conditional/personality-test type surveys + // Getting all the types of the question (because of the special treatment of the score question type $shuffle=''; - if ($survey_data['shuffle']=='1') - { - $shuffle= ' ORDER BY RAND() ' ; + if ($survey_data['shuffle'] == '1') { + $shuffle= ' ORDER BY RAND() '; } $sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' AND survey_group_pri='0' $shuffle "; $result = Database::query($sql); - // there is only one question type for conditional surveys - while ($row = Database::fetch_array($result,'ASSOC')) - { + // There is only one question type for conditional surveys + while ($row = Database::fetch_array($result, 'ASSOC')) { $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 to 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 + // 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 to a question + if (strpos($key, 'question') !== false) { + // 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 = Database::query($sql); - $row = Database::fetch_array($result,'ASSOC'); + $row = Database::fetch_array($result, 'ASSOC'); $option_value = $row['value']; //$option_value = 0; $survey_question_answer = $value; - //we save the answer after making sure that a possible previous attempt is deleted + // We save the answer after making sure that a possible previous attempt is deleted 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 //in case it's another type than 0 or 1 - { + } else { // In case it's another type than 0 or 1 die(get_lang('ErrorSurveyTypeUnknown')); } } -// displaying the survey title and subtitle (appears on every page) +// Displaying the survey title and subtitle (appears on every page) echo '
'.$survey_data['survey_title'].'
'; echo '
'.$survey_data['survey_subtitle'].'
'; -// checking time availability +// Checking time availability check_time_availability($survey_data); -// displaying the survey introduction -if (!isset($_GET['show'])) -{ - // the first thing we do is delete the session +// Displaying the survey introduction +if (!isset($_GET['show'])) { + // The first thing we do is delete the session unset($_SESSION['paged_questions']); unset($_SESSION['page_questions_sec']); $paged_questions_sec=array(); @@ -310,17 +256,15 @@ if (!isset($_GET['show'])) $user_id = api_get_user_id(); -if ($user_id == 0) -{ +if ($user_id == 0) { $user_id = $survey_invitation['user']; } $user_data = UserManager :: get_user_info_by_id($user_id); -if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_array($user_data)) -{ - //echo "
"; print_r($survey_invitation);
+if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_array($user_data)) {
+	//echo '
'; print_r($survey_invitation);
 	$form_fields = explode('@', $survey_data['form_fields']);
-	$list = array ();
+	$list = array();
 	foreach ($form_fields as $field) {
 		$field_value = explode(':', $field);
 		if ($field_value[1] == 1) {
@@ -331,52 +275,52 @@ if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_arra
 		}
 	}
 
-	// we use the same form as in auth/profile.php
-	require_once (api_get_path(LIBRARY_PATH) . 'formvalidator/FormValidator.class.php');
-	$form = new FormValidator('profile', 'post', api_get_self() . "?" . str_replace('&show_form=1', '&show_form=1', $_SERVER['QUERY_STRING']), null, array (
-		'style' => 'width: 75%; float: ' . ($text_dir == 'rtl' ? 'right;' : 'left;')
-	));
+	// We use the same form as in auth/profile.php
+	require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
+	$form = new FormValidator('profile', 'post', api_get_self() . "?" . str_replace('&show_form=1', '&show_form=1', $_SERVER['QUERY_STRING']), null,
+		array('style' => 'width: 75%; float: ' . ($text_dir == 'rtl' ? 'right;' : 'left;'))
+	);
 
 	if (api_is_western_name_order()) {
 		if ($list['firstname'] == 1 ) {
 			//FIRST NAME
-			$form->addElement('text', 'firstname', get_lang('FirstName'), array ('size' => 40));
+			$form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
 			if (api_get_setting('profile', 'name') !== 'true') {
-				$form->freeze(array ('firstname'));
+				$form->freeze(array('firstname'));
 			}
-			$form->applyFilter(array ('firstname'), 'stripslashes');
-			$form->applyFilter(array ('firstname'), 'trim');
+			$form->applyFilter(array('firstname'), 'stripslashes');
+			$form->applyFilter(array('firstname'), 'trim');
 			$form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
 		}
 		if ($list['lastname'] == 1) {
 			//	LAST NAME
-			$form->addElement('text', 'lastname', get_lang('LastName'), array ('size' => 40));
+			$form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
 			if (api_get_setting('profile', 'name') !== 'true') {
-				$form->freeze(array ('lastname'));
+				$form->freeze(array('lastname'));
 			}
-			$form->applyFilter(array ('lastname'), 'stripslashes');
-			$form->applyFilter(array ('lastname'), 'trim');
+			$form->applyFilter(array('lastname'), 'stripslashes');
+			$form->applyFilter(array('lastname'), 'trim');
 			$form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
 		}
 	} else {
 		if ($list['lastname'] == 1) {
 			//	LAST NAME
-			$form->addElement('text', 'lastname', get_lang('LastName'), array ('size' => 40));
+			$form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
 			if (api_get_setting('profile', 'name') !== 'true') {
-				$form->freeze(array ('lastname'));
+				$form->freeze(array('lastname'));
 			}
-			$form->applyFilter(array ('lastname'), 'stripslashes');
-			$form->applyFilter(array ('lastname'), 'trim');
+			$form->applyFilter(array('lastname'), 'stripslashes');
+			$form->applyFilter(array('lastname'), 'trim');
 			$form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
 		}
 		if ($list['firstname'] == 1 ) {
 			//FIRST NAME
-			$form->addElement('text', 'firstname', get_lang('FirstName'), array ('size' => 40));
+			$form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
 			if (api_get_setting('profile', 'name') !== 'true') {
-				$form->freeze(array ('firstname'));
+				$form->freeze(array('firstname'));
 			}
-			$form->applyFilter(array ('firstname'), 'stripslashes');
-			$form->applyFilter(array ('firstname'), 'trim');
+			$form->applyFilter(array('firstname'), 'stripslashes');
+			$form->applyFilter(array('firstname'), 'trim');
 			$form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
 		}
 	}
@@ -384,51 +328,53 @@ if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_arra
 	if ($list['official_code'] == 1) {
 		//	OFFICIAL CODE
 		if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
-			$form->addElement('text', 'official_code', get_lang('OfficialCode'), array (
-				'size' => 40
-			));
-			if (api_get_setting('profile', 'officialcode') !== 'true')
+			$form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => 40));
+			if (api_get_setting('profile', 'officialcode') !== 'true') {
 				$form->freeze('official_code');
+			}
 			$form->applyFilter('official_code', 'stripslashes');
 			$form->applyFilter('official_code', 'trim');
-			if (api_get_setting('registration', 'officialcode') == 'true' && api_get_setting('profile', 'officialcode') == 'true')
+			if (api_get_setting('registration', 'officialcode') == 'true' && api_get_setting('profile', 'officialcode') == 'true') {
 				$form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required');
+			}
 		}
 	}
 	if ($list['email'] == 1) {
 		//	EMAIL
-		$form->addElement('text', 'email', get_lang('Email'), array (
-			'size' => 40
-		));
-		if (api_get_setting('profile', 'email') !== 'true')
+		$form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
+		if (api_get_setting('profile', 'email') !== 'true') {
 			$form->freeze('email');
+		}
 		$form->applyFilter('email', 'stripslashes');
 		$form->applyFilter('email', 'trim');
-		if (api_get_setting('registration', 'email') == 'true')
+		if (api_get_setting('registration', 'email') == 'true') {
 			$form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
+		}
 		$form->addRule('email', get_lang('EmailWrong'), 'email');
 	}
 
 	if ($list['phone'] == 1) {
 		//	PHONE
-		$form->addElement('text', 'phone', get_lang('phone'), array (
-			'size' => 20
-		));
-		if (api_get_setting('profile', 'phone') !== 'true')
+		$form->addElement('text', 'phone', get_lang('phone'), array('size' => 20));
+		if (api_get_setting('profile', 'phone') !== 'true') {
 			$form->freeze('phone');
+		}
 		$form->applyFilter('phone', 'stripslashes');
 		$form->applyFilter('phone', 'trim');
-		if (api_get_setting('profile', 'phone') == 'true')
+		if (api_get_setting('profile', 'phone') == 'true') {
 			$form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
+		}
 	}
 
 	if ($list['language'] == 1) {
 		//	LANGUAGE
 		$form->addElement('select_language', 'language', get_lang('Language'));
-		if (api_get_setting('profile', 'language') !== 'true')
+		if (api_get_setting('profile', 'language') !== 'true') {
 			$form->freeze('language');
-		if (api_get_setting('profile', 'language') == 'true')
+		}
+		if (api_get_setting('profile', 'language') == 'true') {
 			$form->addRule('language', get_lang('ThisFieldIsRequired'), 'required');
+		}
 	}
 
 	// EXTRA FIELDS
@@ -442,70 +388,73 @@ if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_arra
 		if ($list['extra_' . $field_details[1]] == 1) {
 			switch ($field_details[2]) {
 				case USER_FIELD_TYPE_TEXT :
-					$form->addElement('text', 'extra_' . $field_details[1], $field_details[3], array (
-						'size' => 40
-					));
+					$form->addElement('text', 'extra_' . $field_details[1], $field_details[3], array('size' => 40));
 					$form->applyFilter('extra_' . $field_details[1], 'stripslashes');
 					$form->applyFilter('extra_' . $field_details[1], 'trim');
 
-					if ($field_details[7] == 0)
+					if ($field_details[7] == 0) {
 						$form->freeze('extra_' . $field_details[1]);
+					}
 					break;
 				case USER_FIELD_TYPE_TEXTAREA :
 					$form->add_html_editor('extra_' . $field_details[1], $field_details[3], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130'));
 					$form->applyFilter('extra_' . $field_details[1], 'stripslashes');
 					$form->applyFilter('extra_' . $field_details[1], 'trim');
 
-					if ($field_details[7] == 0)
+					if ($field_details[7] == 0) {
 						$form->freeze('extra_' . $field_details[1]);
+					}
 					break;
 				case USER_FIELD_TYPE_RADIO :
-					$group = array ();
+					$group = array();
 					foreach ($field_details[9] as $option_id => $option_details) {
 						$options[$option_details[1]] = $option_details[2];
 						$group[] = & HTML_QuickForm :: createElement('radio', 'extra_' . $field_details[1], $option_details[1], $option_details[2] . '
', $option_details[1]); } $form->addGroup($group, 'extra_' . $field_details[1], $field_details[3], ''); - if ($field_details[7] == 0) + if ($field_details[7] == 0) { $form->freeze('extra_' . $field_details[1]); + } break; case USER_FIELD_TYPE_SELECT : - $options = array (); + $options = array(); foreach ($field_details[9] as $option_id => $option_details) { $options[$option_details[1]] = $option_details[2]; } $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, ''); - if ($field_details[7] == 0) + if ($field_details[7] == 0) { $form->freeze('extra_' . $field_details[1]); + } break; case USER_FIELD_TYPE_SELECT_MULTIPLE : - $options = array (); + $options = array(); foreach ($field_details[9] as $option_id => $option_details) { $options[$option_details[1]] = $option_details[2]; } - $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, array ( - 'multiple' => 'multiple' - )); - if ($field_details[7] == 0) + $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, array('multiple' => 'multiple')); + if ($field_details[7] == 0) { $form->freeze('extra_' . $field_details[1]); + } break; case USER_FIELD_TYPE_DATE : - $form->addElement('datepickerdate', 'extra_' . $field_details[1], $field_details[3],array('form_name'=>'profile')); + $form->addElement('datepickerdate', 'extra_' . $field_details[1], $field_details[3], array('form_name' => 'profile')); $form->_elements[$form->_elementIndex['extra_' . $field_details[1]]]->setLocalOption('minYear', 1900); $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00'); $form -> setDefaults($defaults); - if ($field_details[7] == 0) + if ($field_details[7] == 0) { $form->freeze('extra_' . $field_details[1]); + } $form->applyFilter('theme', 'trim'); break; case USER_FIELD_TYPE_DATETIME : - $form->addElement('datepicker', 'extra_' . $field_details[1], $field_details[3],array('form_name'=>'profile')); - $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear',1900); + $form->addElement('datepicker', 'extra_' . $field_details[1], $field_details[3], array('form_name' => 'profile')); + $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900); $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00'); $form -> setDefaults($defaults); - if ($field_details[7] == 0) + if ($field_details[7] == 0) { $form->freeze('extra_' . $field_details[1]); + } $form->applyFilter('theme', 'trim'); break; case USER_FIELD_TYPE_DOUBLE_SELECT : @@ -520,16 +469,17 @@ if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_arra $group[] = & HTML_QuickForm :: createElement('select', 'extra_' . $field_details[1], '', $values[0], ''); $group[] = & HTML_QuickForm :: createElement('select', 'extra_' . $field_details[1] . '*', '', $values['*'], ''); $form->addGroup($group, 'extra_' . $field_details[1], $field_details[3], ' '); - if ($field_details[7] == 0) + if ($field_details[7] == 0) { $form->freeze('extra_' . $field_details[1]); + } - // recoding the selected values for double : if the user has selected certain values, we have to assign them to the correct select form + // Recoding the selected values for double : if the user has selected certain values, we have to assign them to the correct select form if (key_exists('extra_' . $field_details[1], $extra_data)) { - // exploding all the selected values (of both select forms) + // Exploding all the selected values (of both select forms) $selected_values = explode(';', $extra_data['extra_' . $field_details[1]]); - $extra_data['extra_' . $field_details[1]] = array (); + $extra_data['extra_' . $field_details[1]] = array(); - // looping through the selected values and assigning the selected values to either the first or second select form + // Looping through the selected values and assigning the selected values to either the first or second select form foreach ($selected_values as $key => $selected_value) { if (key_exists($selected_value, $values[0])) { $extra_data['extra_' . $field_details[1]]['extra_' . $field_details[1]] = $selected_value; @@ -546,18 +496,18 @@ if ($survey_data['form_fields']!='' && $survey_data['anonymous'] == 0 && is_arra $form->addRule('extra_' . $field_details[1], get_lang('ThisFieldIsRequired'), 'required'); } } - $form->addElement('style_submit_button', '', get_lang('Next'),array('class'=>'next')); + $form->addElement('style_submit_button', '', get_lang('Next'), array('class' => 'next')); $user_data = array_merge($user_data, $extra_data); $form->setDefaults($user_data); } -if ($survey_data['form_fields'] && $survey_data['anonymous'] == 0 && is_array($user_data) && !isset ($_GET['show'])) { +if ($survey_data['form_fields'] && $survey_data['anonymous'] == 0 && is_array($user_data) && !isset($_GET['show'])) { if ($form->validate()) { $user_data = $form->exportValues(); if (is_array($user_data)) { - if (count($user_data)>0) { - $extras = array (); - // build SQL query + if (count($user_data) > 0) { + $extras = array(); + // Build SQL query $sql = "UPDATE $table_user SET"; foreach ($user_data as $key => $value) { if (substr($key, 0, 6) == 'extra_') { //an extra field @@ -566,11 +516,11 @@ if ($survey_data['form_fields'] && $survey_data['anonymous'] == 0 && is_array($u $sql .= " $key = '" . Database :: escape_string($value) . "',"; } } - // remove trailing , from the query we have so far + // Remove trailing , from the query we have so far $sql = rtrim($sql, ','); $sql .= " WHERE user_id = '" . $user_id . "'"; Database::query($sql); - //update the extra fields + // Update the extra fields if (is_array($extras)) { foreach ($extras as $key => $value) { $myres = UserManager :: update_extra_field_value($user_id, $key, $value); @@ -579,30 +529,29 @@ if ($survey_data['form_fields'] && $survey_data['anonymous'] == 0 && is_array($u echo '
' . get_lang('InformationUpdated') . ' ' . get_lang('PleaseFillSurvey') . '
'; } } - //$_GET['show_form']=0; - //$show_form=0; - $_GET['show']=0; - $show=0; - //we unset the sessions + //$_GET['show_form'] = 0; + //$show_form = 0; + $_GET['show'] = 0; + $show = 0; + // We unset the sessions unset($_SESSION['paged_questions']); unset($_SESSION['page_questions_sec']); - $paged_questions_sec=array(); + $paged_questions_sec = array(); } - //elseif ($_GET['show_form']==1) // // displaying the field + //elseif ($_GET['show_form'] == 1) { // Displaying the field else { echo '
' . get_lang('UpdateInformation') . '
'; - //we unset the sessions + // We unset the sessions unset($_SESSION['paged_questions']); unset($_SESSION['page_questions_sec']); - $paged_questions_sec=array(); + $paged_questions_sec = array(); $form->display(); } } -// displaying the survey thanks message -if (isset($_POST['finish_survey'])) -{ +// Displaying the survey thanks message +if (isset($_POST['finish_survey'])) { echo '
'.get_lang('SurveyFinished').'
'.$survey_data['survey_thanks'].'
'; survey_manager::update_survey_answered($survey_data['survey_id'], $survey_invitation['user'], $survey_invitation['survey_code']); unset($_SESSION['paged_questions']); @@ -611,56 +560,46 @@ if (isset($_POST['finish_survey'])) exit(); } -// sets the random questions -$shuffle=''; -if ($survey_data['shuffle']==1) -{ - $shuffle= ' BY RAND() '; +// Sets the random questions +$shuffle = ''; +if ($survey_data['shuffle'] == 1) { + $shuffle = ' BY RAND() '; } -if ( isset($_GET['show']) || isset($_POST['personality'])) -{ +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 + // As long as there is no pagebreak fount we keep adding questions to the page $questions_displayed = array(); $counter = 0; $paged_questions = array(); - // if non-conditional survey - if ($survey_data['survey_type']==='0') - { - if(empty($_SESSION['paged_questions'])) - { + // If non-conditional survey + if ($survey_data['survey_type'] === '0') { + if (empty($_SESSION['paged_questions'])) { $sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' ORDER BY sort ASC"; $result = Database::query($sql); - while ($row = Database::fetch_array($result,'ASSOC')) - { - if($row['type'] == 'pagebreak') - { + while ($row = Database::fetch_array($result, 'ASSOC')) { + if ($row['type'] == 'pagebreak') { $counter++; - } - else - { + } else { // ids from question of the current survey $paged_questions[$counter][] = $row['question_id']; } } $_SESSION['paged_questions'] = $paged_questions; - } - else - { + } else { $paged_questions = $_SESSION['paged_questions']; } if (key_exists($_GET['show'], $paged_questions)) { if (isset($_GET['user_id'])) { - // get the user into survey answer table (user or anonymus) - $my_user_id=($survey_data['anonymous']==1) ? $_SESSION['surveyuser'] :api_get_user_id(); + // Get the user into survey answer table (user or anonymus) + $my_user_id=($survey_data['anonymous']==1) ? $_SESSION['surveyuser'] :api_get_user_id(); - $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri, + $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 @@ -671,7 +610,7 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) ORDER BY survey_question.sort, survey_question_option.sort ASC"; } else { - $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri, + $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 @@ -685,13 +624,13 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) $result = Database::query($sql); $question_counter_max = Database::num_rows($result); $counter = 0; - $limit=0; + $limit = 0; $questions = array(); while ($row = Database :: fetch_array($result, 'ASSOC')) { - // if the type is not a pagebreak we store it in the $questions array - if ($row['type'] <> 'pagebreak') { + // 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']; @@ -700,31 +639,29 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) $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 + // If the type is a pagebreak we are finished loading the questions for this page else { break; } $counter++; } } - } - elseif ($survey_data['survey_type']==='1') - { + } elseif ($survey_data['survey_type'] === '1') { $my_survey_id=Database::escape_string($survey_invitation['survey_id']); $current_user = Database::escape_string($survey_invitation['user']); - if (isset($_POST['personality'])) - { + if (isset($_POST['personality'])) { // Compute the results to get the 3 groups nearest to the user's personality - if ($shuffle=='') + if ($shuffle == '') { $order = 'BY sort ASC '; - else + } else { $order = $shuffle; + } - $answer_list=array(); + $answer_list = array(); - // get current user results - $results=array(); + // Get 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) @@ -736,15 +673,15 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) $result = Database::query($sql); while ($row = Database :: fetch_array($result)) { - $answer_list['value']=$row['value']; - $answer_list['group']=$row['survey_group_pri']; - $results[]=$answer_list; + $answer_list['value'] = $row['value']; + $answer_list['group'] = $row['survey_group_pri']; + $results[] = $answer_list; } - //echo "
";print_r($results); echo "
"; + //echo '
'; print_r($results); echo '
'; - // get the total score for each group of questions - $totals=array(); + // Get the total score for each group of questions + $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 @@ -756,245 +693,205 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) ORDER BY temp.survey_group_pri"; $result = Database::query($sql); - while ($row = Database::fetch_array($result)) - { - $list['value']=$row['value']; - $list['group']=$row['survey_group_pri']; - $totals[]=$list; + while ($row = Database::fetch_array($result)) { + $list['value'] = $row['value']; + $list['group'] = $row['survey_group_pri']; + $totals[] = $list; } //echo '
'; print_r($totals);
 
-			$final_results=array();
+			$final_results = array();
 
-            //get a percentage score for each group
+            // Get a percentage score for each group
 			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;
+						$group = $totals[$i]['group'];
+						$porcen = ($results[$j]['value'] / $totals[$i]['value']);
+						$final_results[$group] = $porcen;
 					}
 				}
 			}
 
-			// sort the results by score (getting a list of group IDs by score into $groups)
+			// Sort the results by score (getting a list of group IDs by score into $groups)
 			arsort($final_results);
 			$groups=array_keys($final_results);
 			/*
             echo '
';
 			echo 'Group id =>  %';
-			echo "
"; + echo '
'; print_r($final_results); echo '
'; */ - $result=array(); - $count_result=0; - foreach ($final_results as $key => $sub_result) { - $result[] = array ( - 'group' => $key, - 'value' => $sub_result - ); + $result = array(); + $count_result = 0; + foreach ($final_results as $key => & $sub_result) { + $result[] = array('group' => $key, 'value' => $sub_result); $count_result++; } - /* //i.e 70% - 70% -70% 70% $equal_count =3 - while(1) - { - if ($result[$i]['value'] == $result[$i+1]['value']) - { + while (1) { + if ($result[$i]['value'] == $result[$i+1]['value']) { $equal_count++; - } - else - { + } else { break; } $i++; } echo 'eq'. $equal_count; - echo "
"; - if ($equal_count==0) - { + echo '
'; + if ($equal_count == 0) { //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 + 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; + } elseif ($equal_count == 2) { + $group_cant = 2; + } else { + $group_cant = -1; } */ - //i.e 70% - 70% -70% 70% $equal_count =3 + // i.e 70% - 70% -70% 70% $equal_count =3 $i = 0; $group_cant = 0; $equal_count = 0; - // this is the case if the user does not select any question - if ($count_result>0) - { + // This is the case if the user does not select any question + if ($count_result > 0) { // Count the number of scores equal to the first - while (1) - { - if ($result[$i]['value'] == $result[$i +1]['value']) - { + while (1) { + if ($result[$i]['value'] == $result[$i + 1]['value']) { $equal_count++; - } - else - { + } else { break; } $i++; } - } - else - { - //we force the exit of the survey undeterminated + } else { + // We force the exit of the survey undeterminated $equal_count=10; } //echo '
';
 			//print_r($result);
 
-            // if we have only 3 or less equal scores (i.e. 0,1 or 2 equalities), then we can use the three first groups
-			if ($equal_count<4)
-			{
-				//if there is one or less score equalities
-				if ($equal_count === 0 || $equal_count === 1)
-					{
-					//i.e 70% - 70% -60% - 60%  $equal_count = 1 we only get the first 2 options
-					if (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value']))
-					{
+            // If we have only 3 or less equal scores (i.e. 0,1 or 2 equalities), then we can use the three first groups
+			if ($equal_count < 4) {
+				// If there is one or less score equalities
+				if ($equal_count === 0 || $equal_count === 1) {
+					// i.e 70% - 70% -60% - 60%  $equal_count = 1 we only get the first 2 options
+					if (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
 						$group_cant = 1;
 					}
-					//i.e 70% - 70% -0% - 0% 	-	$equal_count = 0 we only get the first 2 options
-					/*elseif (($result[0]['value'] == $result[1]['value']) && ($result[1]['value'] != $result[2]['value']) ) {
-						$group_cant=0;
+					// i.e 70% - 70% -0% - 0% 	-	$equal_count = 0 we only get the first 2 options
+					/*elseif (($result[0]['value'] == $result[1]['value']) && ($result[1]['value'] != $result[2]['value'])) {
+						$group_cant = 0;
 					}*/
 					/*
-					//i.e 70% - 70% -60% - 60%  $equal_count = 0 we only get the first 2 options
-					elseif ( ($result[0]['value']  == $result[1]['value'])  &&  ($result[2]['value'] == $result[3]['value']) )
-					{
-						$group_cant=0;
+					// i.e 70% - 70% -60% - 60%  $equal_count = 0 we only get the first 2 options
+					elseif (($result[0]['value'] == $result[1]['value'])  &&  ($result[2]['value'] == $result[3]['value'])) {
+						$group_cant = 0;
 					}*/
-					//i.e. 80% - 70% - 70% - 70%
+					// i.e. 80% - 70% - 70% - 70%
 					elseif (($result[0]['value'] != $result[1]['value']) && ($result[1]['value'] == $result[2]['value']) && ($result[2]['value'] == $result[3]['value'])) {
 						$group_cant = 0;
-					}
-					else
-					{
-					//i.e. 80% - 70% - 70% - 50
-					//i.e. 80% - 80% - 70% - 50
+					} else {
+						// i.e. 80% - 70% - 70% - 50
+						// i.e. 80% - 80% - 70% - 50
 
-						// by default we choose the highest 3
-						$group_cant=2;
+						// By default we choose the highest 3
+						$group_cant = 2;
 					}
-				}
-				else
-				{
-					//if there are two score equalities
-					$group_cant=$equal_count;
+				} else {
+					// If there are two score equalities
+					$group_cant = $equal_count;
 				}
 
-				//@todo
+				//@todo Translate these comments.
 				// conditional_status
 				// 0 no determinado
 				// 1 determinado
 				// 2 un solo valor
 				// 3 valores iguales
 
-				if ($group_cant>0)
-				{
+				if ($group_cant > 0) {
 					//echo '$equal_count'.$group_cant;
-					// we only get highest 3
-					$secondary ='';
-					$combi='';
+					// 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
+						$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];
+							$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."
"; + $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'
'; } 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."
"; + $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'
'; } 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."
"; + $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'
'; } } } /* echo '
';
-                    echo "Pair of Groups 

"; + echo 'Pair of Groups

'; echo $combi; echo '
'; */ - // create the new select with the questions from the secondary phase - if (empty($_SESSION['page_questions_sec']) && !is_array($_SESSION['page_questions_sec']) && count($_SESSION['page_questions_sec']==0)) - { + // Create the new select with the questions from the secondary phase + if (empty($_SESSION['page_questions_sec']) && !is_array($_SESSION['page_questions_sec']) && count($_SESSION['page_questions_sec'] == 0)) { $sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".$my_survey_id."' AND ($secondary ) ORDER BY sort ASC"; $result = Database::query($sql); - $counter=0; - while ($row = Database::fetch_array($result,'ASSOC')) - { - if ($survey_data['one_question_per_page']==1) - { + $counter = 0; + while ($row = Database::fetch_array($result, 'ASSOC')) { + if ($survey_data['one_question_per_page'] == 1) { $paged_questions_sec[$counter][] = $row['question_id']; $counter++; + } elseif ($row['type'] == 'pagebreak') { + $counter++; + } else { + // ids from question of the current survey + $paged_questions_sec[$counter][] = $row['question_id']; } - else - if($row['type'] == 'pagebreak') - { - $counter++; - } else { - // ids from question of the current survey - $paged_questions_sec[$counter][] = $row['question_id']; - } } $_SESSION['paged_questions_sec'] = $paged_questions_sec; - } - else - { + } else { $paged_questions_sec = $_SESSION['paged_questions_sec']; } //print_r($paged_questions_sec); - $paged_questions = $_SESSION['paged_questions']; //for the sake of pages counting - //$paged_questions = $paged_questions_sec; //for the sake of pages counting coming up at display time... + $paged_questions = $_SESSION['paged_questions']; // For the sake of pages counting + //$paged_questions = $paged_questions_sec; // For the sake of pages counting coming up at display time... - if ($shuffle=='') - $shuffle=' BY survey_question.sort, survey_question_option.sort ASC '; + if ($shuffle == '') { + $shuffle = ' BY survey_question.sort, survey_question_option.sort ASC '; + } - //$val=0; - //if ($survey_data['one_question_per_page']==0) - //{ - $val=(int)$_POST['personality']; + //$val = 0; + //if ($survey_data['one_question_per_page'] == 0) { + $val = (int)$_POST['personality']; //} - //echo '
';print_r($paged_questions_sec);echo '
'; - if (is_array($paged_questions_sec)) - { + //echo '
'; print_r($paged_questions_sec); echo '
'; + if (is_array($paged_questions_sec)) { + $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 @@ -1008,12 +905,11 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) $result = Database::query($sql); $question_counter_max = Database::num_rows($result); $counter = 0; - $limit=0; + $limit = 0; $questions = array(); - while ($row = Database::fetch_array($result,'ASSOC')) - { - // if the type is not a pagebreak we store it in the $questions array - if ($row['type'] <> 'pagebreak') { + while ($row = Database::fetch_array($result, 'ASSOC')) { + // 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']; @@ -1021,60 +917,48 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) $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 + // 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 - { + // If the type is a pagebreak we are finished loading the questions for this page + else { break; } $counter++; } - } - else - { + } else { echo get_lang('SurveyUndetermined'); } - - } - else - { + } else { echo get_lang('SurveyUndetermined'); } - } - else - { + } else { echo get_lang('SurveyUndetermined'); } - } - else - { - //We need this variable only in the 2nd set of questions when personality is set. + } else { + // We need this variable only in the 2nd set of questions when personality is set. unset($_SESSION['page_questions_sec']); $paged_questions_sec=array(); // Only the questions from the basic group - //the 50 questions A B C D E F G - $order_sql= $shuffle; - if ($shuffle=='') + // the 50 questions A B C D E F G + $order_sql = $shuffle; + if ($shuffle == '') { $order_sql=' BY question_id '; + } - if(empty($_SESSION['paged_questions'])) - { + if (empty($_SESSION['paged_questions'])) { $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 "
";echo "
"; + //echo '
'; echo '
'; $result = Database::query($sql); - $counter=0; - while ($row = Database::fetch_array($result,'ASSOC')) - { - if ($survey_data['one_question_per_page']==1) - { + $counter = 0; + while ($row = Database::fetch_array($result, 'ASSOC')) { + if ($survey_data['one_question_per_page'] == 1) { $paged_questions[$counter][] = $row['question_id']; $counter++; } else { @@ -1087,33 +971,29 @@ if ( isset($_GET['show']) || isset($_POST['personality'])) } } $_SESSION['paged_questions'] = $paged_questions; - } - else - { + } else { $paged_questions = $_SESSION['paged_questions']; } //print_r($paged_questions); //print_r($paged_questions); - //if (key_exists($_GET['show'],$paged_questions)) - //{ - $order_sql= $shuffle; - if ($shuffle=='') + //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']; + //$val = 0; + //if ($survey_data['one_question_per_page'] == 0) { + $val = $_GET['show']; //} - //echo '
';print_r($paged_questions);echo $val;
-
-			$result=null;
-			if ($val!='')
-			{
-				$imploded= implode(',', $paged_questions[$val]);
-				if ($imploded!=''){
-					// the answers are always in the same order NO shuffle
+			//echo '
'; print_r($paged_questions); echo $val;
+
+			$result = null;
+			if ($val != '') {
+				$imploded = implode(',', $paged_questions[$val]);
+				if ($imploded != '') {
+					// The answers are always in the same order NO shuffle
 					$order_sql = ' BY survey_question.sort, survey_question_option.sort ASC ';
 					$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,
@@ -1128,16 +1008,13 @@ if ( isset($_GET['show']) || isset($_POST['personality']))
 					$question_counter_max = Database :: num_rows($result);
 				}
 			}
-			if (!is_null($result))
-			{
+			if (!is_null($result)) {
 				$counter = 0;
 				$limit = 0;
-				$questions = array ();
-				while ($row = Database :: fetch_array($result, 'ASSOC'))
-				{
-					// if the type is not a pagebreak we store it in the $questions array
-					if ($row['type'] <> 'pagebreak')
-					{
+				$questions = array();
+				while ($row = Database :: fetch_array($result, 'ASSOC')) {
+					// 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'];
@@ -1145,12 +1022,12 @@ if ( isset($_GET['show']) || isset($_POST['personality']))
 						$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
+						// 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
+					// If the type is a pagebreak we are finished loading the questions for this page
 					else {
 						break;
 					}
@@ -1158,66 +1035,53 @@ if ( isset($_GET['show']) || isset($_POST['personality']))
 				}
 			}
 		}
-	}
-	else //in case it's another type than 0 or 1
-    {
+	} else { // In case it's another type than 0 or 1
         echo get_lang('ErrorSurveyTypeUnknown');
     }
 }
 
-// selecting the maximum number of pages
+// 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 = Database::query($sql);
 $numberofpages = Database::num_rows($result) + 1;
 
 // Displaying the form with the questions
-if (isset($_GET['show']))
-{
+if (isset($_GET['show'])) {
 	$show = (int)$_GET['show'] + 1;
-}
-else
-{
+} else {
 	$show = 0;
 }
 
 // Displaying the form with the questions
-if (isset($_POST['personality']))
-{
+if (isset($_POST['personality'])) {
 	$personality = (int)$_POST['personality'] + 1;
-}
-else
-{
-	$personality= 0;
+} 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']):'');
+$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']) : '';
 
-$add_parameters=isset($_GET['user_id']) ? 'user_id='.$_GET['user_id'].'&': '';
+$add_parameters = isset($_GET['user_id']) ? 'user_id='.$_GET['user_id'].'&' : '';
 
 echo '';
 echo '';
 
-if(isset($questions) && is_array($questions))
-{
-	foreach ($questions as $key=>$question)
-	{
+if (isset($questions) && is_array($questions)) {
+	foreach ($questions as $key => & $question) {
 		$ch_type = 'ch_'.$question['type'];
 		$display = new $ch_type;
 		$display->render_question($question);
 	}
 }
 
-if ($survey_data['survey_type']==='0')
-{
-	if ($survey_data['show_form_profile']==0)
-	{
-		// the normal survey as always
-		if (($show < $numberofpages) || !$_GET['show']) //$show = $_GET['show']+1
-		{
+if ($survey_data['survey_type'] === '0') {
+	if ($survey_data['show_form_profile'] == 0) {
+		// The normal survey as always
+		if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
 			//echo '';
 			echo '';
 		}
@@ -1226,125 +1090,96 @@ if ($survey_data['survey_type']==='0')
 			//echo '';
 			echo '';
 		}
-	}
-	else
-	{
-		// the normal survey as always but with the form profile
-		if (isset ($_GET['show']))
-		{
-			$numberofpages=count($paged_questions);
-			if (($show < $numberofpages) || !$_GET['show']) //$show = $_GET['show']+1
-			{
+	} else {
+		// The normal survey as always but with the form profile
+		if (isset($_GET['show'])) {
+			$numberofpages = count($paged_questions);
+			if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
 				//echo '';
 				echo '';
 			}
 
-			if ($show >= $numberofpages && $_GET['show'] )
-			{
+			if ($show >= $numberofpages && $_GET['show'] ) {
 				//echo '';
 				echo '';
 			}
 		}
 	}
-
-}
-elseif ($survey_data['survey_type'] === '1') //conditional/personality-test type survey
-{
-	if (isset ($_GET['show']) || isset ($_POST['personality']))
-	{
+} elseif ($survey_data['survey_type'] === '1') { //conditional/personality-test type survey
+	if (isset($_GET['show']) || isset($_POST['personality'])) {
 		$numberofpages = count($paged_questions);
-		//echo "
"; echo 'num pages norma:'.$numberofpages;echo "
"; - //echo '
';print_r($paged_questions_sec);
-		if (!empty ($paged_questions_sec) && count($paged_questions_sec) > 0)
-		{
-			//in case we're in the second phase, also sum the second group questions
+		//echo '
'; echo 'num pages norma:'.$numberofpages; echo '
'; + //echo '
'; print_r($paged_questions_sec);
+		if (!empty ($paged_questions_sec) && count($paged_questions_sec) > 0) {
+			// In case we're in the second phase, also sum the second group questions
 			//echo 'pagesec :'.count($paged_questions_sec);
 			$numberofpages += count($paged_questions_sec);
 			//echo 'pagesec :';
-		}
-		else
-		{
-			// we need this variable only if personality ==1
+		} else {
+			// We need this variable only if personality == 1
 			unset($_SESSION['page_questions_sec']);
-			$paged_questions_sec=array();
+			$paged_questions_sec = array();
 		}
 
-		/*echo "
"; - echo 'num pages:'.$numberofpages;echo "
"; - echo 'show :'.$show;echo "
"; + /*echo '
'; + echo 'num pages:'.$numberofpages; echo '
'; + echo 'show :'.$show;echo '
'; echo 'personality :'.$personality; - echo "
"; + echo '
'; */ - //echo $show.' / '.$numberofpages."
"; - if ($personality ==0) - if ( ($show <= $numberofpages) || !$_GET['show'] ) //$show = $_GET['show']+1 - { + //echo $show.' / '.$numberofpages.'
'; + if ($personality == 0) + if (($show <= $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1 //echo ''; echo ''; - if ($survey_data['one_question_per_page']==0) - { - if ($personality >= 0 ) - { + if ($survey_data['one_question_per_page'] == 0) { + if ($personality >= 0) { echo ''; } - } - else - { - if ($personality > 0 ) - { + } else { + if ($personality > 0) { echo ''; } } - if ($numberofpages == $show) + if ($numberofpages == $show) { echo ''; + } } - if ($show > $numberofpages && $_GET['show'] && $personality==0) - { + if ($show > $numberofpages && $_GET['show'] && $personality == 0) { echo ''; //$numberofpages = count($paged_questions); //echo $numberofpages = count($paged_questions_sec); //echo $personality.' / '.$numberofpages; - //echo "
"; + //echo '
'; //if ($personality > count($paged_questions_sec) - 1) //|| $numberofpages == $show +$personality +1 //echo $show + $personality; //echo $numberofpages; - } - elseif ($personality >0 ) - { - if ($survey_data['one_question_per_page']==1) - { - - if ($show >= $numberofpages) - { + } elseif ($personality > 0) { + if ($survey_data['one_question_per_page'] == 1) { + if ($show >= $numberofpages) { //echo ''; echo ''; - } - else - { + } else { echo ''; //echo ''; echo ''; } - } - else - { + } else { // if the personality test hidden input was set. //echo ''; echo ''; } } } - // this is the case when the show_profile_form is true but there are not form_fields - elseif ($survey_data['form_fields'] == '') - { + // This is the case when the show_profile_form is true but there are not form_fields + elseif ($survey_data['form_fields'] == '') { //echo ''; echo ''; - } - elseif(!is_array($user_data)) - { // if the user is not registered in the platform we do not show the form to update his information + } elseif(!is_array($user_data)) { + // If the user is not registered in the platform we do not show the form to update his information //echo ''; echo ''; } @@ -1355,9 +1190,9 @@ echo ''; Display :: display_footer(); /** - * Check if this survey has ended. If so, display message and exit rhis script + * Check whether this survey has ended. If so, display message and exit rhis script */ -function check_time_availability($surv_data){ +function check_time_availability($surv_data) { $start_date = mktime(0, 0, 0, substr($surv_data['start_date'], 5, 2), substr($surv_data['start_date'], 8, 2), substr($surv_data['start_date'], 0, 4)); $end_date = mktime(0, 0, 0, substr($surv_data['end_date'], 5, 2), substr($surv_data['end_date'], 8, 2), substr($surv_data['end_date'], 0, 4)); $cur_date = time(); @@ -1367,12 +1202,9 @@ function check_time_availability($surv_data){ Display :: display_footer(); exit; } - if ($cur_date > $end_date) - { + if ($cur_date > $end_date) { Display :: display_warning_message(get_lang('SurveyNotAvailableAnymore'), false); Display :: display_footer(); exit; } } - -?> diff --git a/main/survey/preview.php b/main/survey/preview.php index e77aee34b7..b7c8f979ea 100755 --- a/main/survey/preview.php +++ b/main/survey/preview.php @@ -1,42 +1,24 @@ , Ghent University: cleanup, refactoring and rewriting large parts of the code -* @author Julio Montoya Armas , Dokeos: Personality Test modifications -* @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $ -* -* @todo use quickforms for the forms -*/ - -// name of the language file that needs to be included + * @package chamilo.survey + * @author unknown, the initial survey that did not make it in 1.8 because of bad code + * @author Patrick Cool , Ghent University: cleanup, refactoring and rewriting large parts of the code + * @author Julio Montoya Armas , Chamilo: Personality Test modifications + * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $ + * + * @todo use quickforms for the forms + */ + +// Language file that needs to be included $language_file = 'survey'; -// including the global dokeos file -require ('../inc/global.inc.php'); -require_once('survey.lib.php'); +// Including the global initialization file +require '../inc/global.inc.php'; +require_once 'survey.lib.php'; -$this_section=SECTION_COURSES; +$this_section = SECTION_COURSES; // Database table definitions $table_survey = Database :: get_course_table(TABLE_SURVEY); @@ -46,15 +28,14 @@ $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); $table_user = Database :: get_main_table(TABLE_MAIN_USER); // We exit here if ther is no valid $_GET parameter -if (!isset($_GET['survey_id']) OR !is_numeric($_GET['survey_id'])){ +if (!isset($_GET['survey_id']) || !is_numeric($_GET['survey_id'])){ Display :: display_header(get_lang('SurveyPreview')); Display :: display_error_message(get_lang('InvallidSurvey'), false); Display :: display_footer(); exit; } - -// getting the survey information +// Getting the survey information $survey_id = intval($_GET['survey_id']); $survey_data = survey_manager::get_survey($survey_id); @@ -65,14 +46,14 @@ if (empty($survey_data)) { exit; } -$urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40)); +$urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40)); if (api_strlen(strip_tags($survey_data['title'])) > 40) { $urlname .= '...'; } -// breadcrumbs -$interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList')); -$interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$survey_id, "name" => $urlname); +// Breadcrumbs +$interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList')); +$interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname); // Header Display :: display_header(get_lang('SurveyPreview')); @@ -80,38 +61,34 @@ Display :: display_header(get_lang('SurveyPreview')); // We exit here is the first or last question is a pagebreak (which causes errors) SurveyUtil::check_first_last_question($survey_id, false); -// only a course admin is allowed to preview a survey: you are NOT a course admin => error message +// Only a course admin is allowed to preview a survey: you are NOT a course admin => error message /* -if (!api_is_allowed_to_edit(false,true)) -{ +if (!api_is_allowed_to_edit(false, true)) { Display :: display_error_message(get_lang('NotAllowed'), false); }*/ $counter_question = 0; -// only a course admin is allowed to preview a survey: you are a course admin -if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView']=='true') || api_is_allowed_to_session_edit(false,true)) { - // survey information +// Only a course admin is allowed to preview a survey: you are a course admin +if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView'] == 'true') || api_is_allowed_to_session_edit(false, true)) { + // Survey information echo '
'.$survey_data['survey_title'].'
'; echo '
'.$survey_data['survey_subtitle'].'
'; - // displaying the survey introduction - if (!isset($_GET['show'])) - { + // Displaying the survey introduction + if (!isset($_GET['show'])) { echo '
'.$survey_data['survey_introduction'].'
'; $limit = 0; } - // displaying the survey thanks message - if(isset($_POST['finish_survey'])) - { + // Displaying the survey thanks message + if (isset($_POST['finish_survey'])) { echo '
'.get_lang('SurveyFinished').' '.$survey_data['survey_thanks'].'
'; Display :: display_footer(); exit; } - if (isset($_GET['show'])) - { + if (isset($_GET['show'])) { // 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(); @@ -122,17 +99,15 @@ if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView']==' ORDER BY sort ASC"; $result = Database::query($sql); - while ($row = Database::fetch_array($result)) - { - if($row['type'] == 'pagebreak') { + while ($row = Database::fetch_array($result)) { + if ($row['type'] == 'pagebreak') { $counter++; } else { $paged_questions[$counter][] = $row['question_id']; - } + } } - if (array_key_exists($_GET['show'], $paged_questions)) - { + if (array_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 @@ -144,12 +119,10 @@ if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView']==' $result = Database::query($sql); $question_counter_max = Database::num_rows($result); - $limit=0; - while ($row = Database::fetch_array($result)) - { - // if the type is not a pagebreak we store it in the $questions array - if($row['type'] <> 'pagebreak') - { + $limit = 0; + while ($row = Database::fetch_array($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']; @@ -158,46 +131,38 @@ if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView']==' $questions[$row['sort']]['options'][intval($row['option_sort'])] = $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 - { + // If the type is a pagebreak we are finished loading the questions for this page + else { break; } $counter_question++; } } } - // selecting the maximum number of pages + // 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_id)."'"; $result = Database::query($sql); $numberofpages = Database::num_rows($result) + 1; // Displaying the form with the questions - if (isset($_GET['show'])) - { + if (isset($_GET['show'])) { $show = (int)$_GET['show'] + 1; - } - else - { + } else { $show = 0; } echo '
'; - if(is_array($questions) && count($questions)>0) - { - foreach ($questions as $key=>$question) - { + if (is_array($questions) && count($questions) > 0) { + foreach ($questions as $key => & $question) { $ch_type = 'ch_'.$question['type']; $display = new $ch_type; $display->render_question($question); } } - if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) - { + if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) { echo '
'; } - if ($show >= $numberofpages && $_GET['show'] || ( isset($_GET['show']) && count($questions) == 0)) - { + if ($show >= $numberofpages && $_GET['show'] || (isset($_GET['show']) && count($questions) == 0)) { if (count($questions) == 0) { echo '

'.get_lang('ThereAreNotQuestionsForthisSurvey').'

'; } diff --git a/main/survey/question.php b/main/survey/question.php index 883aff3aa4..7fd367f20f 100755 --- a/main/survey/question.php +++ b/main/survey/question.php @@ -1,66 +1,47 @@ , Ghent University: cleanup, refactoring and rewriting large parts of the code * @version $Id: question.php 21734 2009-07-02 17:12:41Z cvargas1 $ */ -// name of the language file that needs to be included +// Language file that needs to be included $language_file = 'survey'; -// including the global dokeos file -require ('../inc/global.inc.php'); +// Including the global initialization file +require '../inc/global.inc.php'; -// including additional libraries -//require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php"); -require_once('survey.lib.php'); +// Including additional libraries +//require_once api_get_path(LIBRARY_PATH).'survey.lib.php'; +require_once 'survey.lib.php'; $htmlHeadXtra[] = ''; //jQuery $htmlHeadXtra[] = ''; /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/ -if (!api_is_allowed_to_edit(false,true)) { +if (!api_is_allowed_to_edit(false, true)) { Display :: display_header(); Display :: display_error_message(get_lang('NotAllowed'), false); Display :: display_footer(); exit; } -//Is valid request -$is_valid_request=$_REQUEST['is_executable']; -if ($request_index<>$is_valid_request) { - if ($request_index=='save_question') { +// Is valid request +$is_valid_request = $_REQUEST['is_executable']; +if ($request_index != $is_valid_request) { + if ($request_index == 'save_question') { unset($_POST[$request_index]); - } elseif ($request_index=='add_answer') { + } elseif ($request_index == 'add_answer') { unset($_POST[$request_index]); - } elseif($request_index=='remove_answer') { + } elseif($request_index == 'remove_answer') { unset($_POST[$request_index]); } } @@ -72,7 +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); -// getting the survey information +// Getting the survey information $survey_data = survey_manager::get_survey($_GET['survey_id']); if (empty($survey_data)) { Display :: display_header(get_lang('ToolSurvey')); @@ -82,12 +63,12 @@ if (empty($survey_data)) { } -$urlname = api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40); +$urlname = api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40); if (api_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_QUESTION_GROUP).' WHERE survey_id = '.(int)$_GET['survey_id'].' LIMIT 1'; $rs = Database::query($sql); if(Database::num_rows($rs)===0) { @@ -96,9 +77,9 @@ if($survey_data['survey_type']==1) { } } -// breadcrumbs -$interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList')); -$interbreadcrumb[] = array ("url" => 'survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']), 'name' => strip_tags($urlname)); +// Breadcrumbs +$interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList')); +$interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']), 'name' => strip_tags($urlname)); // Tool name if ($_GET['action'] == 'add') { @@ -108,34 +89,33 @@ if ($_GET['action'] == 'edit') { $tool_name = get_lang('EditQuestion'); } -// the possible question types -$possible_types = array('personality','yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score'); +// The possible question types +$possible_types = array('personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score'); -// actions +// Actions $actions = ''; -// checking if it is a valid type -if (!in_array($_GET['type'], $possible_types)) -{ +// Checking if it is a valid type +if (!in_array($_GET['type'], $possible_types)) { Display :: display_header($tool_name,'Survey'); echo $actions; Display :: display_error_message(get_lang('TypeDoesNotExist'), false); Display :: display_footer(); } -// displaying the form for adding or editing the question -if (empty($_POST['save_question']) && 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)) { if (!isset($_POST['save_question'])) { // Displaying the header - Display::display_header($tool_name,'Survey'); + Display::display_header($tool_name, 'Survey'); echo $actions; - $error_message=''; + $error_message = ''; // Displys message if exists if (isset($_SESSION['temp_sys_message'])) { $error_message=$_SESSION['temp_sys_message']; unset($_SESSION['temp_sys_message']); - if ($error_message=='PleaseEnterAQuestion' || $error_message=='PleasFillAllAnswer'|| $error_message=='PleaseChooseACondition'|| $error_message=='ChooseDifferentCategories') { + if ($error_message == 'PleaseEnterAQuestion' || $error_message == 'PleasFillAllAnswer'|| $error_message == 'PleaseChooseACondition'|| $error_message == 'ChooseDifferentCategories') { Display::display_error_message(get_lang($error_message), true); } } @@ -147,42 +127,41 @@ if (empty($_POST['save_question']) && in_array($_GET['type'],$possible_types)) { $form_content['answers'] = array('', ''); if ($_GET['type'] == 'yesno') { - $form_content['answers'][0]=get_lang('Yes'); - $form_content['answers'][1]=get_lang('No'); + $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; + $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'])) { + if (isset($_GET['question_id']) && !empty($_GET['question_id'])) { $form_content = survey_manager::get_question($_GET['question_id']); } - // an action has been performed (for instance adding a possible answer, moving an answer, ...) + // 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); } - if ($error_message!='') { - $form_content['question']=$_SESSION['temp_user_message']; - $form_content['answers']=$_SESSION['temp_answers']; - $form_content['values']=$_SESSION['temp_values']; + if ($error_message != '') { + $form_content['question'] = $_SESSION['temp_user_message']; + $form_content['answers'] = $_SESSION['temp_answers']; + $form_content['values'] = $_SESSION['temp_values']; $form_content['horizontalvertical'] = $_SESSION['temp_horizontalvertical']; - unset($_SESSION['temp_user_message']); unset($_SESSION['temp_answers']); unset($_SESSION['temp_values']); @@ -199,4 +178,3 @@ if (empty($_POST['save_question']) && in_array($_GET['type'],$possible_types)) { // Footer Display :: display_footer(); -?> diff --git a/main/survey/reporting.php b/main/survey/reporting.php index be823b0a23..bd664ee7ea 100755 --- a/main/survey/reporting.php +++ b/main/survey/reporting.php @@ -1,55 +1,31 @@ -, Ghent University: cleanup, refactoring and rewriting large parts of the code -* @version $Id: reporting.php 21652 2009-06-27 17:07:35Z herodoto $ -* -* @todo The question has to be more clearly indicated (same style as when filling the survey) -*/ - -// name of the language file that needs to be included + * @package chamilo.survey + * @author unknown, the initial survey that did not make it in 1.8 because of bad code + * @author Patrick Cool , Ghent University: cleanup, refactoring and rewriting large parts of the code + * @version $Id: reporting.php 21652 2009-06-27 17:07:35Z herodoto $ + * + * @todo The question has to be more clearly indicated (same style as when filling the survey) + */ + +// Language file that needs to be included $language_file = 'survey'; -// including the global dokeos file -require ('../inc/global.inc.php'); -require_once('survey.lib.php'); -$this_section=SECTION_COURSES; +// Including the global initialization file +require '../inc/global.inc.php'; +require_once 'survey.lib.php'; +$this_section = SECTION_COURSES; $survey_id = Security::remove_XSS($_GET['survey_id']); - -// export +// Export /** * @todo use export_table_csv($data, $filename = 'export') */ -if ($_POST['export_report']) -{ - switch($_POST['export_format']) - { +if ($_POST['export_report']) { + switch($_POST['export_format']) { case 'xls': $survey_data = survey_manager::get_survey($survey_id); $filename = 'survey_results_'.$survey_id.'.xls'; @@ -66,16 +42,12 @@ if ($_POST['export_report']) header('Content-type: application/octet-stream'); header('Content-Type: application/force-download'); - if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) - { + if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) { header('Content-Disposition: filename= '.$filename); - } - else - { + } else { header('Content-Disposition: attachment; filename= '.$filename); } - if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) - { + if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { header('Pragma: '); header('Cache-Control: '); header('Cache-Control: public'); // IE cannot download from sessions without a cache @@ -88,16 +60,15 @@ if ($_POST['export_report']) } } -// including additional libraries -//require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php"); -require_once (api_get_path(LIBRARY_PATH)."/course.lib.php"); +// Including additional libraries +//require_once api_get_path(LIBRARY_PATH).'survey.lib.php'; +require_once api_get_path(LIBRARY_PATH).'course.lib.php'; // Checking the parameters SurveyUtil::check_parameters(); /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/ -if (!api_is_allowed_to_edit(false,true)) -{ +if (!api_is_allowed_to_edit(false, true)) { Display :: display_header(get_lang('ToolSurvey')); Display :: display_error_message(get_lang('NotAllowed'), false); Display :: display_footer(); @@ -112,7 +83,7 @@ $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); // TODO: To be checked. TABLE_MAIN_SURVEY_REMINDER has not been defined. -// getting the survey information +// Getting the survey information $survey_data = survey_manager::get_survey($survey_id); if (empty($survey_data)) { @@ -121,24 +92,20 @@ if (empty($survey_data)) { Display :: display_footer(); exit; } -$urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40)); -if (api_strlen(strip_tags($survey_data['title'])) > 40) -{ + +$urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40)); +if (api_strlen(strip_tags($survey_data['title'])) > 40) { $urlname .= '...'; } -// breadcrumbs -$interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList')); -$interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname); -if (!$_GET['action'] OR $_GET['action'] == 'overview') -{ +// Breadcrumbs +$interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList')); +$interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname); +if (!$_GET['action'] OR $_GET['action'] == 'overview') { $tool_name = get_lang('Reporting'); -} -else -{ - $interbreadcrumb[] = array ("url" => "reporting.php?survey_id=".$survey_id, "name" => get_lang('Reporting')); - switch ($_GET['action']) - { +} else { + $interbreadcrumb[] = array('url' => 'reporting.php?survey_id='.$survey_id, 'name' => get_lang('Reporting')); + switch ($_GET['action']) { case 'questionreport': $tool_name = get_lang('DetailedReportByQuestion'); break; @@ -155,18 +122,18 @@ else } // Displaying the header -Display::display_header($tool_name,'Survey'); +Display::display_header($tool_name, 'Survey'); // Action handling SurveyUtil::handle_reporting_actions(); -// actions bar +// Actions bar echo ''; -// content -if (!$_GET['action'] OR $_GET['action'] == 'overview') { +// Content +if (!$_GET['action'] || $_GET['action'] == 'overview') { $myweb_survey_id = $survey_id; echo '
'.get_lang('DetailedReportByQuestionDetail').'
'; echo '
'.get_lang('DetailedReportByUserDetail').'.
'; @@ -174,6 +141,5 @@ if (!$_GET['action'] OR $_GET['action'] == 'overview') { echo '
'.get_lang('CompleteReportDetail').'
'; } - // Footer Display :: display_footer(); diff --git a/main/survey/survey.download.inc.php b/main/survey/survey.download.inc.php index 80cf894f39..7cf6553851 100755 --- a/main/survey/survey.download.inc.php +++ b/main/survey/survey.download.inc.php @@ -1,38 +1,22 @@ * @version $Id: $ * - * small peace code to enable user to access images included into survey + * A small peace of code to enable user to access images included into survey * which are accessible by non authenticated users. This file is included * by document/download.php */ + function check_download_survey($course, $invitation, $doc_url) { - require_once('survey.lib.php'); - require_once (api_get_path(LIBRARY_PATH)."/course.lib.php"); + require_once 'survey.lib.php'; + require_once api_get_path(LIBRARY_PATH).'course.lib.php'; - // getting all the course information + // Getting all the course information $_course = CourseManager::get_course_information($course); // Database table definitions @@ -43,48 +27,37 @@ function check_download_survey($course, $invitation, $doc_url) { $table_user = Database :: get_main_table(TABLE_MAIN_USER); $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']); - - // now we check if the invitationcode is valid + // Now we check if the invitationcode is valid $sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '".Database::escape_string($invitation)."'"; $result = Database::query($sql); - if (Database::num_rows($result) < 1) - { + if (Database::num_rows($result) < 1) { Display :: display_error_message(get_lang('WrongInvitationCode'), false); Display :: display_footer(); exit; } $survey_invitation = Database::fetch_assoc($result); - // now we check if the user already filled the survey - if ($survey_invitation['answered'] == 1) - { + // Now we check if the user already filled the survey + if ($survey_invitation['answered'] == 1) { Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false); Display :: display_footer(); exit; } - // very basic security check: check if a text field from a survey/answer/option contains the name of the document requested + // Very basic security check: check if a text field from a survey/answer/option contains the name of the document requested - - ////////////// - // fetch survey ID - ////////////// + // Fetch survey ID // 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 = Database::query($sql); - if (Database::num_rows($result) > 1) - { - if ($_POST['language']) - { + if (Database::num_rows($result) > 1) { + if ($_POST['language']) { $survey_invitation['survey_id'] = $_POST['language']; - } - else - { + } else { echo ''; echo ' '; @@ -93,12 +66,11 @@ function check_download_survey($course, $invitation, $doc_url) { display::display_footer(); exit; } - } - else - { + } else { $row = Database::fetch_assoc($result); $survey_invitation['survey_id'] = $row['survey_id']; } + $sql = "select count(*) from $table_survey where survey_id = ".$survey_invitation['survey_id']." and ( title LIKE '%$doc_url%' @@ -117,15 +89,11 @@ function check_download_survey($course, $invitation, $doc_url) { )"; $result = Database::query($sql); - if (Database::num_rows($result) == 0) - { + if (Database::num_rows($result) == 0) { Display :: display_error_message(get_lang('WrongInvitationCode'), false); Display :: display_footer(); exit; } - return $_course; } - -?> diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index 7f1730b60f..9a44beed08 100755 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -1,34 +1,16 @@ , Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code - @author Julio Montoya Armas , Dokeos: Personality Test modification and rewriting large parts of the code -* @version $Id: survey.lib.php 22296 2009-07-22 22:05:50Z cfasanando $ -* -* @todo move this file to inc/lib -* @todo use consistent naming for the functions (save vs store for instance) -*/ + * @package chamilo.survey + * @author Patrick Cool , Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code + * @author Julio Montoya Armas , Chamilo: Personality Test modification and rewriting large parts of the code + * @version $Id: survey.lib.php 22296 2009-07-22 22:05:50Z cfasanando $ + * + * @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 '../inc/global.inc.php'; require_once api_get_path(LIBRARY_PATH).'sortabletable.class.php'; @@ -45,11 +27,11 @@ $htmlHeadXtra[] = ''; -class survey_manager -{ - /****************************************************************************************************** - SURVEY FUNCTIONS - *****************************************************************************************************/ +class survey_manager { + + /*** + * SYRVEY FUNCTIONS + */ /** * This function retrieves all the survey information @@ -65,22 +47,20 @@ class survey_manager * * @todo this is the same function as in create_new_survey.php */ - function get_survey($survey_id,$shared=0,$course_code='') - { + function get_survey($survey_id, $shared = 0, $course_code = '') { global $_course; - // table definition - + // Table definition if (!empty($course_code)) { $my_course_id = $course_code; } else if (isset($_GET['course'])) { - $my_course_id=Security::remove_XSS($_GET['course']); + $my_course_id = Security::remove_XSS($_GET['course']); } else { - $my_course_id=api_get_course_id(); + $my_course_id = api_get_course_id(); } - $my_course_info=api_get_course_info($my_course_id); + $my_course_info = api_get_course_info($my_course_id); $table_survey = Database :: get_course_table(TABLE_SURVEY, $my_course_info['dbName']); - if ($shared<>0) { + if ($shared != 0) { $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); } @@ -90,7 +70,7 @@ class survey_manager if (Database::num_rows($result)> 0) { $return = Database::fetch_array($result,'ASSOC'); - // we do this (temporarily) to have the array match the quickform elements immediately + // We do this (temporarily) to have the array match the quickform elements immediately // idealiter the fields in the db match the quickform fields $return['survey_code'] = $return['code']; $return['survey_title'] = $return['title']; @@ -107,9 +87,9 @@ class survey_manager $return['show_form_profile'] = $return['show_form_profile']; $return['input_name_list'] = $return['input_name_list']; - $return['shuffle'] = $return['shuffle']; - $return['parent_id'] = $return['parent_id']; - $return['survey_version'] = $return['survey_version']; + $return['shuffle'] = $return['shuffle']; + $return['parent_id'] = $return['parent_id']; + $return['survey_version'] = $return['survey_version']; return $return; } else { return $return; @@ -125,72 +105,60 @@ class survey_manager * @author Patrick Cool , Ghent University * @version February 2007 */ - function store_survey($values) - { + function store_survey($values) { global $_user; - // table defnitions + // Table defnitions $table_survey = Database :: get_course_table(TABLE_SURVEY); - /*if ($values['survey_share']['survey_share'] !== '0') - { + /*if ($values['survey_share']['survey_share'] !== '0') { $shared_survey_id = survey_manager::store_shared_survey($values); }*/ - $shared_survey_id=0; + $shared_survey_id = 0; - if (!$values['survey_id'] OR !is_numeric($values['survey_id'])) - { - // check if the code doesn't soon exists in this language + if (!$values['survey_id'] || !is_numeric($values['survey_id'])) { + // Check if the code doesn't soon exists in this language $sql = 'SELECT 1 FROM '.$table_survey.' WHERE code="'.Database::escape_string($values['survey_code']).'" AND lang="'.Database::escape_string($values['survey_language']).'"'; $rs = Database::query($sql); - if(Database::num_rows($rs)>0) - { + if (Database::num_rows($rs) > 0) { $return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage'; $return['type'] = 'error'; - $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0; + $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0; return $return; } - if ($values['anonymous']=='') - { - $values['anonymous']=0; + if ($values['anonymous'] == '') { + $values['anonymous'] = 0; } $additional['columns'] = ''; $additional['values'] = ''; - if ($values['anonymous']==0) - { - // input_name_list + if ($values['anonymous'] == 0) { + // Input_name_list $additional['columns'] .= ', show_form_profile'; $additional['values'] .= ",'".Database::escape_string($values['show_form_profile'])."'"; - if ($values['show_form_profile']==1) - { - // input_name_list - $fields=explode(',',$values['input_name_list']); - $field_values=''; - foreach ($fields as $field ) - { - if ($field!='') - { - if ($values[$field]=='') - $values[$field]=0; + if ($values['show_form_profile'] == 1) { + // Input_name_list + $fields = explode(',', $values['input_name_list']); + $field_values = ''; + foreach ($fields as & $field) { + if ($field != '') { + if ($values[$field] == '') { + $values[$field] = 0; + } $field_values.= $field.':'.$values[$field].'@'; } } $additional['columns'] .= ', form_fields'; $additional['values'] .= ",'".Database::escape_string($field_values)."'"; - } - else - { + } else { $additional['columns'] .= ', form_fields'; $additional['values'] .= ",''"; } - } - else - { - // input_name_list + } else { + // Input_name_list $additional['columns'] .= ', show_form_profile'; $additional['values'] .= ",'0'"; @@ -198,8 +166,7 @@ class survey_manager $additional['values'] .= ",''"; } - if($values['survey_type']==1) - { + if ($values['survey_type'] == 1) { $additional['columns'] .= ', survey_type'; $additional['values'] .= ",'1'"; @@ -212,45 +179,37 @@ class survey_manager $additional['columns'] .= ', parent_id'; $additional['values'] .= ",'".Database::escape_string($values['parent_id'])."'"; - // logic for versioning surveys - if(!empty($values['parent_id'])) - { + // Logic for versioning surveys + if (!empty($values['parent_id'])) { $additional['columns'] .= ', survey_version'; $sql = 'SELECT survey_version FROM '.$table_survey.' WHERE parent_id = '.Database::escape_string($values['parent_id']).' ORDER BY survey_version DESC LIMIT 1'; $rs = Database::query($sql); - if(Database::num_rows($rs)===0) { + if (Database::num_rows($rs) === 0) { $sql = 'SELECT survey_version FROM '.$table_survey.' WHERE survey_id = '.Database::escape_string($values['parent_id']); $rs = Database::query($sql); - $getversion = Database::fetch_array($rs,ASSOC); - if(empty($getversion['survey_version'])) - { + $getversion = Database::fetch_array($rs, 'ASSOC'); + if (empty($getversion['survey_version'])) { $additional['values'] .= ",'".++$getversion['survey_version']."'"; - } - else - { + } else { $additional['values'] .= ",'".$getversion['survey_version'].".1'"; } - } - else - { - $row = Database::fetch_array($rs,ASSOC); + } else { + $row = Database::fetch_array($rs, 'ASSOC'); $pos = api_strpos($row['survey_version']); - if($pos===false) - { - //$new_version= substr($row['survey_version'],$pos, count()) + if ($pos === false) { + //$new_version = substr($row['survey_version'],$pos, count()) $row['survey_version'] = $row['survey_version'] + 1; $additional['values'] .= ",'".$row['survey_version']."'"; - } - else - { - $getlast = api_split('\.',$row['survey_version']); + } else { + $getlast = api_split('\.', $row['survey_version']); $lastversion = array_pop($getlast); $lastversion = $lastversion + 1; - $add=implode('.',$getlast); - if ($add!='') + $add = implode('.', $getlast); + if ($add != '') { $insertnewversion = $add.'.'.$lastversion; - else + } else { $insertnewversion = $lastversion; + } $additional['values'] .= ",'".$insertnewversion."'"; } } @@ -258,16 +217,16 @@ class survey_manager } $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date, anonymous".$additional['columns'].", session_id) VALUES ( '".Database::escape_string(strtolower(generate_course_code(api_substr($values['survey_code'],0))))."', - '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_title'])),COURSEMANAGERLOWSECURITY))."', - '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_subtitle'])),COURSEMANAGERLOWSECURITY))."', + '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_title'])), COURSEMANAGERLOWSECURITY))."', + '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_subtitle'])), COURSEMANAGERLOWSECURITY))."', '".Database::escape_string($_user['user_id'])."', '".Database::escape_string($values['survey_language'])."', '".Database::escape_string($values['start_date'])."', '".Database::escape_string($values['end_date'])."', '".Database::escape_string($shared_survey_id)."', '".Database::escape_string('template')."', - '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_introduction'])),COURSEMANAGERLOWSECURITY))."', - '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_thanks'])),COURSEMANAGERLOWSECURITY))."', + '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_introduction'])), COURSEMANAGERLOWSECURITY))."', + '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['survey_thanks'])), COURSEMANAGERLOWSECURITY))."', '".date('Y-m-d H:i:s')."', '".Database::escape_string($values['anonymous'])."'".$additional['values'].", ".intval($_SESSION['id_session'])." @@ -275,11 +234,11 @@ class survey_manager $result = Database::query($sql); $survey_id = Database::insert_id(); if ($survey_id > 0) { - //insert into item_property + // Insert into item_property api_item_property_update(api_get_course_info(), TOOL_SURVEY, $survey_id, 'SurveyAdded', api_get_user_id()); } - if($values['survey_type']==1 && !empty($values['parent_id'])){ + if ($values['survey_type'] == 1 && !empty($values['parent_id'])) { survey_manager::copy_survey($values['parent_id'],$survey_id); } @@ -287,61 +246,49 @@ class survey_manager //$return['message'] .= ''.get_lang('ClickHere').''; $return['message'] = 'SurveyCreatedSuccesfully'; $return['type'] = 'confirmation'; - $return['id'] = $survey_id; - } - else - { + $return['id'] = $survey_id; - // check if the code doesn't soon exists in this language + } else { + + // Check whether the code doesn't soon exists in this language $sql = 'SELECT 1 FROM '.$table_survey.' WHERE code="'.Database::escape_string($values['survey_code']).'" AND lang="'.Database::escape_string($values['survey_language']).'" AND survey_id!='.intval($values['survey_id']); $rs = Database::query($sql); - if(Database::num_rows($rs)>0) - { + if (Database::num_rows($rs) > 0) { $return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage'; $return['type'] = 'error'; - $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0; + $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0; return $return; } - if ($values['anonymous']=='') - { - $values['anonymous']=0; + if ($values['anonymous'] == '') { + $values['anonymous'] = 0; } $additionalsets = ", shuffle = '".Database::escape_string($values['shuffle'])."'"; $additionalsets .= ", one_question_per_page = '".Database::escape_string($values['one_question_per_page'])."'"; - if ($values['anonymous']==0) - { - + if ($values['anonymous'] == 0) { $additionalsets .= ", show_form_profile = '".Database::escape_string($values['show_form_profile'])."'"; - if ($values['show_form_profile']==1) - { - $fields=explode(',',$values['input_name_list']); - $field_values=''; - foreach ($fields as $field ) - { - if ($field!='') - { - if ($values[$field]=='') - $values[$field]=0; + if ($values['show_form_profile'] == 1) { + $fields = explode(',',$values['input_name_list']); + $field_values = ''; + foreach ($fields as & $field) { + if ($field != '') { + if ($values[$field] == '') { + $values[$field] = 0; + } $field_values.= $field.':'.$values[$field].'@'; } } $additionalsets .= ", form_fields = '".Database::escape_string($field_values)."'"; - } - else - { + } else { $additionalsets .= ", form_fields = '' "; } - } - else - { + } else { $additionalsets .= ", show_form_profile = '0'"; $additionalsets .= ", form_fields = '' "; } - $sql = "UPDATE $table_survey SET title = '".Database::escape_string($values['survey_title'])."', subtitle = '".Database::escape_string($values['survey_subtitle'])."', @@ -357,7 +304,7 @@ class survey_manager WHERE survey_id = '".Database::escape_string($values['survey_id'])."'"; $result = Database::query($sql); - //update into item_property (update) + // Update into item_property (update) api_item_property_update(api_get_course_info(), TOOL_SURVEY, Database::escape_string($values['survey_id']), 'SurveyUpdated', api_get_user_id()); //$return['message'] = get_lang('SurveyUpdatedSuccesfully').'
'.get_lang('YouCanNowAddQuestionToYourSurvey').': '; @@ -368,7 +315,7 @@ class survey_manager $return['id'] = $values['survey_id']; } - return $return; + return $return; } /** @@ -380,16 +327,13 @@ class survey_manager * @author Patrick Cool , Ghent University * @version February 2007 */ - function store_shared_survey($values) - { - global $_user; - global $_course; + function store_shared_survey($values) { + global $_user, $_course; - // table defnitions + // Table defnitions $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY); - if (!$values['survey_id'] OR !is_numeric($values['survey_id']) OR $values['survey_share']['survey_share'] == 'true') - { + if (!$values['survey_id'] || !is_numeric($values['survey_id']) || $values['survey_share']['survey_share'] == 'true') { $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code) VALUES ( '".Database::escape_string($values['survey_code'])."', '".Database::escape_string($values['survey_title'])."', @@ -403,9 +347,7 @@ class survey_manager '".$_course['id']."')"; $result = Database::query($sql); $return = Database::insert_id(); - } - else - { + } else { $sql = "UPDATE $table_survey SET code = '".Database::escape_string($values['survey_code'])."', title = '".Database::escape_string($values['survey_title'])."', @@ -431,56 +373,55 @@ class survey_manager * @author Patrick Cool , Ghent University * @version January 2007 */ - function delete_survey($survey_id, $shared=false, $course_code = '') - { + function delete_survey($survey_id, $shared=false, $course_code = '') { // Database table definitions - $table_survey = Database :: get_course_table(TABLE_SURVEY,$course_code); - $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP,$course_code); - if ($shared) - { - $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY); + $table_survey = Database :: get_course_table(TABLE_SURVEY,$course_code); + $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP, $course_code); + if ($shared) { + $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY); } - // deleting the survey + // Deleting the survey $sql = "DELETE from $table_survey WHERE survey_id='".Database::escape_string($survey_id)."'"; $res = Database::query($sql); - // deleting groups of this survey + // Deleting groups of this survey $sql = "DELETE from $table_survey_question_group WHERE survey_id='".Database::escape_string($survey_id)."'"; $res = Database::query($sql); - // deleting the questions of the survey + // Deleting the questions of the survey survey_manager::delete_all_survey_questions($survey_id, $shared); - //update into item_property (delete) + // Update into item_property (delete) api_item_property_update(api_get_course_info(), TOOL_SURVEY, Database::escape_string($survey_id), 'delete', api_get_user_id()); return true; } - function copy_survey($parent_survey, $new_survey_id) - { + function copy_survey($parent_survey, $new_survey_id) { // Database table definitions - $table_survey = Database :: get_course_table(TABLE_SURVEY); + $table_survey = Database :: get_course_table(TABLE_SURVEY); $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP); $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $table_survey_options = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); $parent_survey = Database::escape_string($parent_survey); - //get groups + // Get groups $sql = "SELECT * from $table_survey_question_group WHERE survey_id='".$parent_survey."'"; $res = Database::query($sql); - if(Database::num_rows($res)===0) return true; + if (Database::num_rows($res) === 0) { + return true; + } - while($row = Database::fetch_array($res,ASSOC)){ + while($row = Database::fetch_array($res, 'ASSOC')){ $sql1 = 'INSERT INTO '.$table_survey_question_group.' (name,description,survey_id) VALUES (\''.Database::escape_string($row['name']).'\',\''.Database::escape_string($row['description']).'\',\''.$new_survey_id.'\')'; $res1 = Database::query($sql1); $group_id[$row['id']] = Database::insert_id(); } - //get questions + // Get questions $sql = "SELECT * FROM $table_survey_question WHERE survey_id='".$parent_survey."'"; $res = Database::query($sql); - while($row = Database::fetch_array($res,ASSOC)){ + 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,survey_group_pri,survey_group_sec1,survey_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['survey_group_pri']].'\',\''.$group_id[$row['survey_group_sec1']].'\',\''.$group_id[$row['survey_group_sec2']].'\')'; @@ -488,10 +429,10 @@ class survey_manager $question_id[$row['question_id']] = Database::insert_id(); } - //get questions options + // Get questions options $sql = "SELECT * FROM $table_survey_options WHERE survey_id='".$parent_survey."'"; $res = Database::query($sql); - while($row = Database::fetch_array($res,ASSOC)){ + while($row = Database::fetch_array($res ,'ASSOC')){ $sql3 = 'INSERT INTO '.$table_survey_options.' (question_id,survey_id,option_text,sort,value) VALUES ('. "'".$question_id[$row['question_id']]."','".$new_survey_id."','".Database::escape_string($row['option_text'])."','".$row['sort']."','".$row['value']."')"; $res3 = Database::query($sql3); @@ -508,17 +449,15 @@ class survey_manager * @author Eric Marguin , Elixir Interactive * @version October 2007 */ - function empty_survey($survey_id) - { + function empty_survey($survey_id) { // Database table definitions $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); $table_survey = Database :: get_course_table(TABLE_SURVEY); $datas = survey_manager::get_survey($survey_id); - $session_where=''; - if (api_get_session_id()!=0) - { + $session_where = ''; + if (api_get_session_id() != 0) { $session_where = ' AND session_id = "'.api_get_session_id().'" '; } @@ -543,23 +482,22 @@ class survey_manager * @author Patrick Cool , Ghent University * @version February 2007 */ - function update_survey_answered($survey_id, $user, $survey_code) - { + function update_survey_answered($survey_id, $user, $survey_code) { global $_course; // Database table definitions $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']); $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']); - // getting a list with all the people who have filled the survey + // Getting a list with all the people who have filled the survey $people_filled = survey_manager::get_people_who_filled_survey($survey_id); $number = count($people_filled); - // storing this value in the survey table + // Storing this value in the survey table $sql = "UPDATE $table_survey SET answered = '".Database::escape_string($number)."' WHERE survey_id = '".Database::escape_string($survey_id)."'"; $res = Database::query($sql); - // storing that the user has finished the survey. + // Storing that the user has finished the survey. $sql = "UPDATE $table_survey_invitation SET answered='1' WHERE session_id='".api_get_session_id()."' AND user='".Database::escape_string($user)."' AND survey_code='".Database::escape_string($survey_code)."'"; $res = Database::query($sql); } @@ -575,28 +513,25 @@ class survey_manager * @author Patrick Cool , Ghent University * @version February 2007 */ - function get_complete_survey_structure($survey_id, $shared=0) - { + function get_complete_survey_structure($survey_id, $shared = 0) { // Database table definitions $table_survey = Database :: get_course_table(TABLE_SURVEY); $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); - if ($shared<>0) - { + if ($shared != 0) { $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $table_survey_question = Database :: get_course_table(TABLE_SHARED_SURVEY_QUESTION); $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } $structure = survey_manager::get_survey($survey_id, $shared); - $structure['questions'] = survey_manager::get_questions($survey_id); } - /****************************************************************************************************** - SURVEY QUESTION FUNCTIONS - *****************************************************************************************************/ + /*** + * SYRVEY QUESTION FUNCTIONS + */ /** * This function return the "icon" of the question type @@ -604,32 +539,27 @@ class survey_manager * @author Patrick Cool , Ghent University * @version February 2007 */ - function icon_question($type) - { + function icon_question($type) { // the possible question types $possible_types = array('personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score'); // the images array - $icon_question = array - ( - 'yesno' => 'yesno.gif', - 'personality' => 'yesno.gif', - 'multiplechoice' => 'mcua.gif', - 'multipleresponse' => 'mcma.gif', - 'open' => 'open_answer.gif', - 'dropdown' => 'dropdown.gif', - 'percentage' => 'percentagequestion.gif', - 'score' => 'scorequestion.gif', - 'comment' => 'commentquestion.gif', - 'pagebreak' => 'page_end.gif', - ); - - if (in_array($type, $possible_types)) - { + $icon_question = array( + 'yesno' => 'yesno.gif', + 'personality' => 'yesno.gif', + 'multiplechoice' => 'mcua.gif', + 'multipleresponse' => 'mcma.gif', + 'open' => 'open_answer.gif', + 'dropdown' => 'dropdown.gif', + 'percentage' => 'percentagequestion.gif', + 'score' => 'scorequestion.gif', + 'comment' => 'commentquestion.gif', + 'pagebreak' => 'page_end.gif', + ); + + if (in_array($type, $possible_types)) { return $icon_question[$type]; - } - else - { + } else { return false; } } @@ -645,49 +575,42 @@ class survey_manager * * @todo one sql call should do the trick */ - function get_question($question_id, $shared=false) - { - // table definitions + function get_question($question_id, $shared = false) { + // Table definitions $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); - if ($shared) - { + if ($shared) { $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } - // getting the information of the question + // Getting the information of the question $sql = "SELECT * FROM $tbl_survey_question WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort`"; $result = Database::query($sql); $row = Database::fetch_array($result,'ASSOC'); $return['survey_id'] = $row['survey_id']; - $return['question_id'] = $row['question_id']; - $return['type'] = $row['type']; - $return['question'] = $row['survey_question']; - $return['horizontalvertical'] = $row['display']; - $return['shared_question_id'] = $row['shared_question_id']; - $return['maximum_score'] = $row['max_value']; - - if($row['survey_group_pri']!=0) - { + $return['question_id'] = $row['question_id']; + $return['type'] = $row['type']; + $return['question'] = $row['survey_question']; + $return['horizontalvertical'] = $row['display']; + $return['shared_question_id'] = $row['shared_question_id']; + $return['maximum_score'] = $row['max_value']; + + 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; } - // getting the information of the question options + // 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 = Database::query($sql); - while ($row = Database::fetch_array($result,'ASSOC')) - { + while ($row = Database::fetch_array($result, 'ASSOC')) { /** @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) */ @@ -696,7 +619,6 @@ class survey_manager return $return; } - /** * This function gets all the question of any given survey * @@ -708,32 +630,29 @@ class survey_manager * * @todo one sql call should do the trick */ - function get_questions($survey_id) - { - // table definitions + function get_questions($survey_id) { + // Table definitions $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); - // getting the information of the question + // Getting the information of the question $sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."'"; $result = Database::query($sql); - while ($row = Database::fetch_array($result,'ASSOC')) - { + while ($row = Database::fetch_array($result, 'ASSOC')) { $return[$row['question_id']]['survey_id'] = $row['survey_id']; - $return[$row['question_id']]['question_id'] = $row['question_id']; - $return[$row['question_id']]['type'] = $row['type']; - $return[$row['question_id']]['question'] = $row['survey_question']; - $return[$row['question_id']]['horizontalvertical'] = $row['display']; - $return[$row['question_id']]['maximum_score'] = $row['max_value']; - $return[$row['question_id']]['sort'] = $row['sort']; + $return[$row['question_id']]['question_id'] = $row['question_id']; + $return[$row['question_id']]['type'] = $row['type']; + $return[$row['question_id']]['question'] = $row['survey_question']; + $return[$row['question_id']]['horizontalvertical'] = $row['display']; + $return[$row['question_id']]['maximum_score'] = $row['max_value']; + $return[$row['question_id']]['sort'] = $row['sort']; } - // getting the information of the question options + // Getting the information of the question options $sql = "SELECT * FROM $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."'"; $result = Database::query($sql); - while ($row = Database::fetch_array($result,'ASSOC')) - { + while ($row = Database::fetch_array($result, 'ASSOC')) { $return[$row['question_id']]['answers'][] = $row['option_text']; } @@ -750,91 +669,75 @@ class survey_manager * @version January 2007 */ - function save_question($form_content) - { + function save_question($form_content) { global $survey_data; - if (strlen($form_content['question'])>1) - { //checks lenght of the question - $empty_answer=false; + 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'])) - { + 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'])) - { + if (($form_content['choose']==2)&&($form_content['assigned1'] == $form_content['assigned2'])) { $return_message = 'ChooseDifferentCategories'; return $return_message; } } - if ($form_content['type'] != 'percentage') - { - for($i=0;$i 0) - { + // Storing the question in the shared database + if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) { $shared_question_id = survey_manager::save_shared_question($form_content, $survey_data); $form_content['shared_question_id'] = $shared_question_id; } - // storing a new question - if ($form_content['question_id'] == '' OR !is_numeric($form_content['question_id'])) - { - // finding the max sort order of the questions in the given survey + // Storing a new question + if ($form_content['question_id'] == '' || !is_numeric($form_content['question_id'])) { + // Finding the max sort order of the questions in the given survey $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($form_content['survey_id'])."'"; $result = Database::query($sql); $row = Database::fetch_array($result,'ASSOC'); $max_sort = $row['max_sort']; - //some variables defined for survey-test type + // Some variables defined for survey-test type $additional['column'] = ''; $additional['value'] = ''; - if($_POST['choose']==1) - { + 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'])."'"; } - // adding the question to the survey_question table + // 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".$additional['column'].") VALUES ( '".Database::escape_string($form_content['survey_id'])."', '".Database::escape_string($form_content['question'])."', @@ -851,23 +754,20 @@ class survey_manager $form_content['question_id'] = $question_id; $return_message = 'QuestionAdded'; } - // updating an existing question - else - { + // Updating an existing question + else { $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']).'\' '; } $setadditionals = $additional['set'][1].$additional['set'][2].$additional['set'][3]; - // adding the question to the survey_question table + // 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'])."', @@ -878,17 +778,13 @@ class survey_manager $result = Database::query($sql); $return_message = 'QuestionUpdated'; } - // storing the options of the question + // Storing the options of the question $message_options=survey_manager::save_question_options($form_content, $survey_data); + } else { + $return_message = 'PleasFillAllAnswer'; } - else - { - $return_message='PleasFillAllAnswer'; - } - } - else - { - $return_message='PleaseEnterAQuestion'; + } else { + $return_message = 'PleaseEnterAQuestion'; } return $return_message; } @@ -904,17 +800,15 @@ class survey_manager * * @todo editing of a shared question */ - function save_shared_question($form_content, $survey_data) - { + function save_shared_question($form_content, $survey_data) { global $_course; - // table definitions + // Table definitions $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); - // storing a new question - if ($form_content['shared_question_id'] == '' OR !is_numeric($form_content['shared_question_id'])) - { - // finding the max sort order of the questions in the given survey + // Storing a new question + if ($form_content['shared_question_id'] == '' || !is_numeric($form_content['shared_question_id'])) { + // Finding the max sort order of the questions in the given survey $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_data['survey_share'])."' AND code='".Database::escape_string($_course['id'])."'"; @@ -922,7 +816,7 @@ class survey_manager $row = Database::fetch_array($result,'ASSOC'); $max_sort = $row['max_sort']; - // adding the question to the survey_question table + // Adding the question to the survey_question table $sql = "INSERT INTO $tbl_survey_question (survey_id, survey_question, survey_question_comment, type, display, sort, code) VALUES ( '".Database::escape_string($survey_data['survey_share'])."', '".Database::escape_string($form_content['question'])."', @@ -934,9 +828,8 @@ class survey_manager $result = Database::query($sql); $shared_question_id = Database::insert_id(); } - // updating an existing question - else - { + // Updating an existing question + else { // adding the question to the survey_question table $sql = "UPDATE $tbl_survey_question SET survey_question = '".Database::escape_string($form_content['question'])."', @@ -961,34 +854,28 @@ class survey_manager * @author Patrick Cool , Ghent University * @version January 2007 */ - function move_survey_question($direction, $survey_question_id, $survey_id) - { - // table definition + function move_survey_question($direction, $survey_question_id, $survey_id) { + // Table definition $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); - if ($direction == 'moveup') - { + if ($direction == 'moveup') { $sort = 'DESC'; } - if ($direction == 'movedown') - { + if ($direction == 'movedown') { $sort = 'ASC'; } - // finding the two questions that needs to be swapped + // Finding the two questions that needs to be swapped $sql = "SELECT * FROM $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort $sort"; $result = Database::query($sql); $found = false; - while ($row = Database::fetch_array($result,'ASSOC')) - { - if ($found == true) - { + while ($row = Database::fetch_array($result, 'ASSOC')) { + if ($found) { $question_id_two = $row['question_id']; $question_sort_two = $row['sort']; $found = false; } - if ($row['question_id'] == $survey_question_id) - { + if ($row['question_id'] == $survey_question_id) { $found = true; $question_id_one = $row['question_id']; $question_sort_one = $row['sort']; @@ -1012,27 +899,24 @@ class survey_manager * @author Patrick Cool , Ghent University * @version January 2007 */ - function delete_all_survey_questions($survey_id, $shared=false) - { - // table definitions + function delete_all_survey_questions($survey_id, $shared = false) { + // Table definitions $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); - if ($shared) - { + if ($shared) { $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); } - // deleting the survey questions + // Deleting the survey questions $sql = "DELETE from $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."'"; $res = Database::query($sql); - // deleting all the options of the questions of the survey + // Deleting all the options of the questions of the survey survey_manager::delete_all_survey_questions_options($survey_id, $shared); - // deleting all the answers on this survey + // Deleting all the answers on this survey survey_manager::delete_all_survey_answers($survey_id); } - /** * This function deletes a survey question and all its options * @@ -1045,21 +929,18 @@ class survey_manager * @author Patrick Cool , Ghent University * @version March 2007 */ - function delete_survey_question($survey_id, $question_id, $shared=false) - { - // table definitions + function delete_survey_question($survey_id, $question_id, $shared = false) { + // Table definitions $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); - if ($shared) - { + if ($shared) { survey_manager::delete_shared_survey_question($survey_id, $question_id); } - // deleting the survey questions + // 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 = Database::query($sql); - - // deleting the options of the question of the survey + // Deleting the options of the question of the survey survey_manager::delete_survey_question_option($survey_id, $question_id, $shared); } @@ -1074,27 +955,26 @@ class survey_manager * @author Patrick Cool , Ghent University * @version March 2007 */ - function delete_shared_survey_question($survey_id, $question_id) - { - // table definitions + function delete_shared_survey_question($survey_id, $question_id) { + // Table definitions $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); - // first we have to get the shared_question_id + // First we have to get the shared_question_id $question_data = survey_manager::get_question($question_id); - // deleting the survey questions + // Deleting the survey questions $sql = "DELETE FROM $table_survey_question WHERE question_id='".Database::escape_string($question_data['shared_question_id'])."'"; $res = Database::query($sql); - // deleting the options of the question of the survey question + // Deleting the options of the question of the survey question $sql = "DELETE FROM $table_survey_question_option WHERE question_id='".Database::escape_string($question_data['shared_question_id'])."'"; $res = Database::query($sql); } - /****************************************************************************************************** - SURVEY QUESTION OPTIONS FUNCTIONS - *****************************************************************************************************/ + /** + * SURVEY QUESTION OPTIONS FUNCTIONS + */ /** * This function stores the options of the questions in the table @@ -1106,38 +986,31 @@ class survey_manager * * @todo writing the update statement when editing a question */ - function save_question_options($form_content, $survey_data) - { - // a percentage question type has options 1 -> 100 - if ($form_content['type'] == 'percentage') - { - for($i=1;$i<101;$i++) - { + function save_question_options($form_content, $survey_data) { + // A percentage question type has options 1 -> 100 + if ($form_content['type'] == 'percentage') { + for($i = 1; $i < 101; $i++) { $form_content['answers'][] = $i; } } - if (is_numeric($survey_data['survey_share']) AND $survey_data['survey_share'] <> 0) - { + if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) { survey_manager::save_shared_question_options($form_content, $survey_data); } - // table defintion + // Table defintion $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); - // we are editing a question so we first have to remove all the existing options from the database - if (is_numeric($form_content['question_id'])) - { + // We are editing a question so we first have to remove all the existing options from the database + if (is_numeric($form_content['question_id'])) { $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['question_id'])."'"; $result = Database::query($sql); } - $counter=1; - if(is_array($form_content['answers'])) - { - //foreach ($form_content['answers'] as $key=>$answer) { - for ($i=0;$i $answer) { + for ($i = 0; $i < count($form_content['answers']); $i++) { $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'])."', @@ -1147,7 +1020,6 @@ class survey_manager $result = Database::query($sql); $counter++; } - } } @@ -1162,21 +1034,18 @@ class survey_manager * * @todo writing the update statement when editing a question */ - function save_shared_question_options($form_content, $survey_data) - { - if (is_array($form_content) && is_array($form_content['answers'])) - { - // table defintion + function save_shared_question_options($form_content, $survey_data) { + if (is_array($form_content) && is_array($form_content['answers'])) { + // Table defintion $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); - // we are editing a question so we first have to remove all the existing options from the database + // We are editing a question so we first have to remove all the existing options from the database $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'"; $result = Database::query($sql); $counter = 1; - foreach ($form_content['answers'] as $key=>$answer) - { + foreach ($form_content['answers'] as $key => & $answer) { $sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES ( '".Database::escape_string($form_content['shared_question_id'])."', '".Database::escape_string($survey_data['is_shared'])."', @@ -1189,8 +1058,7 @@ class survey_manager } /* - if (is_numeric($survey_data['survey_share']) AND $survey_data['survey_share'] <> 0) - { + if (is_numeric($survey_data['survey_share']) AND $survey_data['survey_share'] != 0) { $form_content = survey_manager::save_shared_question($form_content, $survey_data); } */ @@ -1206,22 +1074,19 @@ class survey_manager * @author Patrick Cool , Ghent University * @version January 2007 */ - function delete_all_survey_questions_options($survey_id, $shared=false) - { - // table definitions + function delete_all_survey_questions_options($survey_id, $shared = false) { + // Table definitions $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); - if ($shared) - { + if ($shared) { $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } - // deleting the options of the survey questions + // Deleting the options of the survey questions $sql = "DELETE from $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."'"; $res = Database::query($sql); return true; } - /** * This function deletes the options of a given question * @@ -1233,27 +1098,22 @@ class survey_manager * @author Patrick Cool , Ghent University * @version March 2007 */ - function delete_survey_question_option($survey_id, $question_id, $shared=false) - { - // table definitions + function delete_survey_question_option($survey_id, $question_id, $shared = false) { + // Table definitions $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); - if ($shared) - { + if ($shared) { $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } - // deleting the options of the survey questions + // Deleting the options of the survey questions $sql = "DELETE from $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'"; $res = Database::query($sql); return true; } - - - /****************************************************************************************************** - SURVEY ANSWERS FUNCTIONS - *****************************************************************************************************/ - + /** + * SURVEY ANSWERS FUNCTIONS + */ /** * This function deletes all the answers anyone has given on this survey @@ -1268,7 +1128,7 @@ class survey_manager * @version January 2007,december 2008 */ function delete_all_survey_answers($survey_id) { - $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); + $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); Database::query('DELETE FROM '.$table_survey_answer.' WHERE survey_id='.$survey_id); return true; } @@ -1282,22 +1142,20 @@ class survey_manager * @author Patrick Cool , Ghent University * @version February 2007 */ - function get_people_who_filled_survey($survey_id, $all_user_info = false) - { + function get_people_who_filled_survey($survey_id, $all_user_info = false) { global $_course; // Database table definition $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']); $table_user = Database :: get_main_table('user'); - // variable initialisation + // Variable initialisation $return = array(); - // getting the survey information + // Getting the survey information $survey_data = survey_manager::get_survey($survey_id); - if ($all_user_info) - { + if ($all_user_info) { $order_clause = api_sort_by_first_name() ? ' ORDER BY user.firstname, user.lastname' : ' ORDER BY user.lastname, user.firstname'; $sql = "SELECT DISTINCT answered_user.user as invited_user, user.firstname, user.lastname, user.user_id FROM $table_survey_answer answered_user @@ -1305,20 +1163,14 @@ class survey_manager ON answered_user.user = user.user_id WHERE survey_id= '".Database::escape_string($survey_data['survey_id'])."'". $order_clause; - } - else - { + } else { $sql = "SELECT DISTINCT user FROM $table_survey_answer WHERE survey_id= '".Database::escape_string($survey_data['survey_id'])."'"; } $res = Database::query($sql); - while ($row = Database::fetch_array($res,'ASSOC')) - { - if ($all_user_info) - { + while ($row = Database::fetch_array($res, 'ASSOC')) { + if ($all_user_info) { $return[] = $row; - } - else - { + } else { $return[] = $row['user']; } } @@ -1328,16 +1180,11 @@ class survey_manager } -class survey_question -{ - // the html code of the form +class survey_question { + + // The html code of the form public $html; - /** - * This function does the generic part of any survey question: the question field - * - * @return unknown - */ /** * This function does the generic part of any survey question: the question field * @@ -1347,13 +1194,12 @@ class survey_question * @todo the form_text has to become a wysiwyg editor or adding a question_comment field * @todo consider adding a question_comment form element */ - function create_form($form_content) - { - global $charset; + function create_form($form_content) { + global $survey_data; //$tool_name = ''.get_lang(ucfirst($_GET['type'])).''; - $tool_name = Display::return_icon(survey_manager::icon_question(Security::remove_XSS($_GET['type'])),get_lang(ucfirst(Security::remove_XSS($_GET['type']))),array('align'=>'middle', 'height'=>'22px')).' '; + $tool_name = Display::return_icon(survey_manager::icon_question(Security::remove_XSS($_GET['type'])), get_lang(ucfirst(Security::remove_XSS($_GET['type']))), array('align' => 'middle', 'height' => '22px')).' '; if ($_GET['action'] == 'add') { $tool_name .= get_lang('AddQuestion'); } @@ -1382,12 +1228,10 @@ class survey_question $this->html .= ' * '.get_lang('Question'); $this->html .= ' '; $this->html .= '
'; - $this->html .= api_return_html_area('question', Security::remove_XSS(stripslashes($form_content['question']),STUDENT), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')); + $this->html .= api_return_html_area('question', Security::remove_XSS(stripslashes($form_content['question']), STUDENT), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')); $this->html .= '
'; $this->html .= ' '; - - /* $this->html .= ' '; $this->html .= ' '; @@ -1400,34 +1244,37 @@ class survey_question //$this->html .='  '; - if($survey_data['survey_type']==1) { + if ($survey_data['survey_type'] == 1) { $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); $sql = 'SELECT id,name FROM '.$table_survey_question_group.' WHERE survey_id = '.(int)$_GET['survey_id'].' ORDER BY name'; $rs = Database::query($sql); - while($row = Database::fetch_array($rs,NUM)) { + while($row = Database::fetch_array($rs, 'NUM')) { $glist .= ''; } $grouplist = $grouplist1 = $grouplist2 = $glist; - if(!empty($form_content['assigned'])) + if (!empty($form_content['assigned'])) { $grouplist = str_replace('