All pastes #468109 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-02 22:00 UTC
rendered paste body
<?php

require_once("../settings.php");
require_once(FILE_ROOT . "/database.php");
require_once(FILE_ROOT . "/order.php");


//
//  - Define some variables that will be used throughout the script. -
// 		Create the database connection for our many queries. 
// 		Find the min and max sort_order values for each category. These
//		are used when disabling buttons.
//		These are created upon page load.
//

	$tabindex = 1;
	// Create the databanks connection.
	$conn = db_connect();
	
	
	// Max/Min values for surveys.
	$sql = "SELECT max(sort_order) AS max_order, min(sort_order) AS min_order FROM surveys";
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	$max = $row['max_order'];
	$min = $row['min_order'];


	
/* Start Submission Check Section */	


// --Survey	
// 	
// The user wants to move the selected survey up one position in the list.
// The bounds have already been checked and the button has been disabled if
// it is the minimum or maximum value in the sort order.
if(isset($_REQUEST['Move_Up'])) {
	// First start to find the id of the survey we are moving Down.
	$sql = "SELECT survey_id FROM surveys WHERE sort_order='" .  ($_REQUEST['position'] - 1) . "'";
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	// This is the id of the record that is moving Down one position.
	$id_moving_down = $row['survey_id'];
	
	// Move it up one.
	$sql = "UPDATE surveys SET sort_order='" . ($_REQUEST['position'] - 1) . "' WHERE survey_id='" . $_REQUEST['s_id'] . "'";
	$res = db_query($sql);
	
	// Now move the one that was on top, down one.
	$sql = "UPDATE surveys SET sort_order='" . $_REQUEST['position'] . "' WHERE survey_id='" . $id_moving_down . "'";
	$res = db_query($sql);
	
	// Refresh the page.
	header("Location: list.php");
	exit;
	
}

if(isset($_REQUEST['Move_Down'])) {
	$sql = "SELECT survey_id FROM surveys WHERE sort_order='" .  ($_REQUEST['position'] + 1) . "'";
	
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	
	// This is the id of the record that is moving Up one position.
	$id_moving_down = $row['survey_id'];
	
	
	// Move it up one.
	$sql = "UPDATE surveys SET sort_order='" . ($_REQUEST['position'] + 1) . "' WHERE survey_id='" . $_REQUEST['s_id'] . "'";

	
	$res = db_query($sql);
	
	// Now move the one that was on top, down one.
	$sql = "UPDATE surveys SET sort_order='" . $_REQUEST['position'] . "' WHERE survey_id='" . $id_moving_down . "'";

	$res = db_query($sql);
	
	header("Location: list.php");
	exit;
	
}


// --Section
//
// The user wants to move the selected section up one position in the list.
// The bounds have already been checked and the button has been disabled if
// it is the minimum or maximum value in the sort order.
if(isset($_REQUEST['Move_Sec_Up'])) {
	// First start to find the id of the survey we are moving Down.
	$sql = "SELECT section_id FROM survey_sections WHERE sort_order='" .  ($_REQUEST['position'] - 1) . "'";
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	// This is the id of the record that is moving Down one position.
	$id_moving_down = $row['section_id'];
	
	// Move it up one.
	$sql = "UPDATE survey_sections SET sort_order='" . ($_REQUEST['position'] - 1) . "' WHERE section_id='" . $_REQUEST['sec_id'] . "'";
	$res = db_query($sql);
	
	// Now move the one that was on top, down one.
	$sql = "UPDATE survey_sections SET sort_order='" . $_REQUEST['position'] . "' WHERE section_id='" . $id_moving_down . "'";
	$res = db_query($sql);
	
	// Refresh the page.
	header("Location: list.php");
	exit;
	
}

// The user has chosen to move a section down one in the sort order.
// Only sections < max(sort_order) are allowed to do this. This is 
// Checked when the page is loaded.
if(isset($_REQUEST['Move_Sec_Down'])) {
	$sql = "SELECT section_id FROM survey_sections WHERE sort_order='" .  ($_REQUEST['position'] + 1) . "'";
	
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	
	// This is the id of the record that is moving Up one position.
	$id_moving_down = $row['section_id'];
	
	
	// Move it up one.
	$sql = "UPDATE survey_sections SET sort_order='" . ($_REQUEST['position'] + 1) . "' WHERE section_id='" . $_REQUEST['sec_id'] . "'";

	
	$res = db_query($sql);
	
	// Now move the one that was on top, down one.
	$sql = "UPDATE survey_sections SET sort_order='" . $_REQUEST['position'] . "' WHERE section_id='" . $id_moving_down . "'";

	$res = db_query($sql);
	
	header("Location: list.php");
	exit;
	
}



// --Question
//
// The user wants to move the selected section up one position in the list.
// The bounds have already been checked and the button has been disabled if
// it is the minimum or maximum value in the sort order.
if(isset($_REQUEST['Move_Q_Up'])) {

//echo "<pre>"; print_r($_REQUEST['position']); echo "</pre>"; exit;

	// First start to find the id of the survey we are moving Down.
	$sql = "SELECT question_id FROM section_questions WHERE sort_order='" .  ($_REQUEST['position'] - 1) . "'";
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	// This is the id of the record that is moving Down one position.
	$id_moving_down = $row['question_id'];
	
	// Move it up one.
	$sql = "UPDATE section_questions SET sort_order='" . ($_REQUEST['position'] - 1) . "' WHERE question_id='" . $_REQUEST['q_id'] . "'";
	$res = db_query($sql);
	
	// Now move the one that was on top, down one.
	$sql = "UPDATE section_questions SET sort_order='" . $_REQUEST['position'] . "' WHERE question_id='" . $id_moving_down . "'";
	$res = db_query($sql);
	
	// Refresh the page.
	header("Location: list.php");
	exit;
	
}

// The user has chosen to move a section down one in the sort order.
// Only sections < max(sort_order) are allowed to do this. This is 
// Checked when the page is loaded.
if(isset($_REQUEST['Move_Q_Down'])) {
	$sql = "SELECT question_id FROM section_questions WHERE sort_order='" .  ($_REQUEST['position'] + 1) . "'";
	
	$res = db_query($sql);
	$row = mysql_fetch_assoc($res);
	
	// This is the id of the record that is moving Up one position.
	$id_moving_down = $row['question_id'];
	
	
	// Move it up one.
	$sql = "UPDATE section_questions SET sort_order='" . ($_REQUEST['position'] + 1) . "' WHERE question_id='" . $_REQUEST['q_id'] . "'";

	
	$res = db_query($sql);
	
	// Now move the one that was on top, down one.
	$sql = "UPDATE section_questions SET sort_order='" . $_REQUEST['position'] . "' WHERE question_id='" . $id_moving_down . "'";

	$res = db_query($sql);
	
	header("Location: list.php");
	exit;
	
}

/* End Submission Checks */




/*
*			Begin onLoad 
*	This section loops through all the surveys and checks to see if sections
* exist for that survey. If there are sections, then loop through all of them
* checking to see if questions exist for each itteration. If there are questions
* print them out.
*
*/


// --Survey
//
//

$sql = "SELECT survey_id, survey_title, sort_order FROM surveys ORDER BY sort_order ASC";
$sur_res = db_query($sql);

while($sur_row = mysql_fetch_assoc($sur_res)) {
	
		$html .= '
	<div style="clear: both;"></div>
	<div class="Form_Row">
		<div class="Form_Label">Survey Title:</div>
		<div class="Form_Data">' . $sur_row['survey_title'] . '</div>
	</div>
	<div style="clear: both;"></div>
	<form action="' . $_SERVER['PHP_SELF'] . '" method="post">	
	<div class="Form_Row">
		<div class="Form_Label">&nbsp;</div>
		<div class="Form_Data"><input name="Move_Up" id="Move_Up" type="submit" value="Move Up" tabindex="' . $tabindex++ . '"' . ($sur_row['sort_order'] == $min ? 'disabled' : '') . '/><input name="Move_Down" id="Move_Down" type="submit" value="Move Down" tabindex="' . $tabindex++ . '"' . ($sur_row['sort_order'] == $max ? 'disabled' : '') . '/></div>
	</div>
	<div style="clear: both;"></div>
	<input name="s_id" type="hidden" value="' . $sur_row['survey_id'] . '" />
	<input name="position" type="hidden" value="' . $sur_row['sort_order'] . '" />
	</form>';


//
//
//


	$sql = "SELECT section_id, section_title, sort_order FROM survey_sections WHERE survey_id='" . $sur_row['survey_id'] . "' ORDER BY sort_order ASC";
	$sec_res = db_query($sql);
	
	if(mysql_num_rows($sec_res) > 0) {
		while($sec_row = mysql_fetch_assoc($sec_res)) {
		
		
		$m_sql = "SELECT max(sort_order) AS sec_max, min(sort_order) AS sec_min FROM survey_sections WHERE survey_id='" . $sur_row['survey_id'] . "'";
		$m_res = db_query($m_sql);
		$m_row = mysql_fetch_assoc($m_res);
		$sec_max = $m_row['sec_max'];
		$sec_min = $m_row['sec_min'];
		
		
		$html .= '
	<div style="clear: both;"></div>
	<div class="Form_Row">
		<div class="Form_Label">Section Title:</div>
		<div class="Form_Data">' . $sec_row['section_title'] . '</div>
	</div>
	<div style="clear: both;"></div>
	<form action="' . $_SERVER['PHP_SELF'] . '" method="post">	
	<div class="Form_Row">
		<div class="Form_Label">&nbsp;</div>
		<div class="Form_Data"><input name="Move_Sec_Up" id="Move_Sec_Up" type="submit" value="Move Up" tabindex="' . $tabindex++ . '"' .  ($sec_row['sort_order'] == $sec_min ? 'disabled' : '') . '/><input name="Move_Sec_Down" id="Move_Sec_Down" type="submit" value="Move Down" tabindex="' . $tabindex++ . '"' . ($sec_row['sort_order'] == $sec_max ? 'disabled' : '') . '/></div>
	</div>
	<div style="clear: both;"></div>
	<input name="sec_id" type="hidden" value="' . $sec_row['section_id'] . '" />
	<input name="position" type="hidden" value="' . $sec_row['sort_order'] . 
	'" />
	</form>		
		';
		

//		
//	
//


		$q_sql = "SELECT question_id, question, sort_order FROM section_questions WHERE section_id='" . $sec_row['section_id'] . "' ORDER BY sort_order ASC";
			$q_res = db_query($q_sql);
			if(mysql_num_rows($q_res) > 0) {
				while($q_row = mysql_fetch_assoc($q_res)) {


		$m_sql = "SELECT max(sort_order) AS q_max, min(sort_order) AS q_min FROM section_questions WHERE section_id='" . $sec_row['section_id'] . "' ORDER BY sort_order ASC";
		$m_res = db_query($m_sql);
		$m_row = mysql_fetch_assoc($m_res);
		$q_max = $m_row['q_max'];
		$q_min = $m_row['q_min'];



					$html .= '
					
	<div class="Form_Row">
		<div class="Form_Label">Question:</div>
		<div class="Form_Data">' . $q_row['question'] . '</div>
	</div>
	<div style="clear: both;"></div>
	<form action="' . $_SERVER['PHP_SELF'] . '" method="post">	
	<div class="Form_Row">
		<div class="Form_Label">&nbsp;</div>
		<div class="Form_Data"><input name="Move_Q_Up" id="Move_Q_Up" type="submit" value="Move Up" tabindex="' . $tabindex++ . '"' . ($q_row['sort_order'] == $q_min ? 'disabled' : '') . '/> <input name="Move_Q_Down" id="Move_Q_Down" type="submit" value="Move Down" tabindex="' . $tabindex++ . '"' . ($q_row['sort_order'] == $q_max ? 'disabled' : '') . '/></div>
	</div>
	<div style="clear: both;"></div>
	<input name="q_id" type="hidden" value="' . $q_row['question_id'] . '" />
	<input name="position" type="hidden" value="' . $q_row['sort_order'] . '" />
	</form>					
										
					';
				}
			}
						
		}
	}

}



?>

<html>
<head>
<style type="text/css">
/* <![CDATA[ */
@import url('../layout.css'); 
@import url('../form.css'); 
/* ]]> */
</style>
<script language="javascript" type="text/javascript" src="../sort.js"></script>
<title>
</title>
</head>
<body>

<?php echo $html ?>

</body>
</html>