All pastes #718748 Raw Edit

Something

public text v1 · immutable
#718748 ·published 2007-09-28 17:44 UTC
rendered paste body
<?php
require_once('global/_top.php');
$listFile = THIS_PAGE;
check_access_page('Activities/Modules');
check_access_page('Translate Zips');
if(isset($_REQUEST['doThis']) && $_REQUEST['doThis']!='')
	$action=$_REQUEST['doThis'];
else
	$action='list';
if(isset($_REQUEST['moduleID']) && $_REQUEST['moduleID']!='' && is_numeric($_REQUEST['moduleID'])) {
	$zipID = $_REQUEST['moduleID'];
	//echo("Setting quizID to '$zipID'"); 
}
if(isset($_REQUEST['questionID']) && $_REQUEST['questionID']!='' && is_numeric($_REQUEST['questionID'])){ 
	$questionID = $_REQUEST['questionID'];
	//die($questionID);
} else {$questionID = "new";}

if($questionID == "new" && !isset($zipID)){
	die("Don't know where to save!");
	//die("The quiz ID should be ".$zipID.".");
}




switch ($action) {
  case 'edit'		:	edit($questionID);		break;
	case 'save'		:	save($questionID);	break;
	case 'delete' : delete($questionID);	break;
	case 'list'		:	listQuestions($zipID);	break;
	default 			:	listQuestions($zipID);	break;

}

function edit($questionID) {
global $db;
			/*
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=fillword">New Fill In Word</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=fillphrase">New Fill In Phrase</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=typeword">New f
			 In Word</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=typephrase">New Type In Phrase</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=arrange">New Arrange</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterword">New Word Scatter</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterpicture">New Picture Scatter</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=spaceout">New Space Out</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=listen">New Listen</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterphrase">New Phrase Scatter</a><br> ';
			*/
	

	$questiontype = $_GET['question_type'];
	$modulid = $_GET['moduleID'];

	
	
		//function select_all($fields, $tables, $conditions = null, $order = null
	$words = $db->select_all(
		array(
			'content_items.id' => 'wordID',
			'content_items.value' => 'word'
		),
		array(
			'content_items',
			'content_groups' => array('content_groups.id' => 'content_items.group_id'),
			'content_types' => array('content_types.id' => 'content_groups.content_type_id')
		),
		array(
			'content_groups.content_type_id' => $db->select('id', 'content_types', array('type_name' => 'word')),
			'content_items.primary_locale_id' => $_SESSION['secondary_locale']
		)
	);


	

	//die($questiontype);
	switch ($questiontype){
		case 'fillin': case 'fillIn'							: fillin($questionID, $words, $modulid); break;
		case 'typeIn'															: typeIn($questionID, $words, $modulid); break;
		case 'arrange'														: arrange($questionID, $words, $modulid); break;
		case 'scatterword'												: scatterword($questionID, $words, $modulid); break;	
		case 'scatterpicture'											: scatterpicture($questionID, $words, $modulid); break;
		case 'spaceout': case 'spaceOut'					: spaceout($questionID, $words, $modulid); break;
		case 'listen'															: listen($questionID, $words, $modulid); break;
		case 'scatterphrase': case 'scatterPhrase': scatterphrase($questionID, $words, $modulid); break;
		default 																	: echo 'Invalid Question Type: '.$questiontype.''; break;
	}
}

function save($questionID) {

			/*
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=fillword">New Fill In Word</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=fillphrase">New Fill In Phrase</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=typeword">New Type In Word</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=typephrase">New Type In Phrase</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=arrange">New Arrange</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterword">New Word Scatter</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterpicture">New Picture Scatter</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=spaceout">New Space Out</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=listen">New Listen</a><br> ';
			echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterphrase">New Phrase Scatter</a><br> ';
			*/



echo '<pre>';
print_r($_GET);
echo '</pre>';


	
	$questiontype = $_GET['question_type'];	
	
	switch ($questiontype){
		case 'fillin': case 'fillIn'					: savefillin($questionID); break;
		case 'typeIn'					: savetypeIn($questionID); break;
		case 'arrange'				: savearrange($questionID); break;
		case 'scatterword'		: savescatterword($questionID); break;	
		case 'scatterpicture'	: savescatterpicture($questionID); break;
		case 'spaceout': case 'spaceOut'				: savespaceout($questionID); break;
		case 'listen'					: savelisten($questionID); break;
		case 'scatterphrase': case 'scatterPhrase'	: savescatterphrase($questionID); break;
		default 							: echo 'broke'; break;
	}
}


function delete($questionID){

global $db;
	$table = 'module_elements';
	//$conditions = array('id' => $_GET['questionID']);
	//$conditions['module_id'] = $quizID;
	

	

	
	
		
	$db->delete($table, array(
		'module_id' => $_REQUEST['moduleID'],
		'id' => $questionID
		)
	);
		header('location:'.THIS_PAGE.'?doThis=list&moduleID='.$_REQUEST['moduleID']);
}


function listQuestions($zipID) {
	//default action - lists the questions associated with this quiz 
	//$zipID = 405; //debugging
	global $db;
	//$db->echo = true;

	$games = $db->select_all(array(
					'game_type.type_name' => 'type',
					'word_element.id' => 'wordID',
					'module_group.sequence' => 'dir',
					'answer_element.value' => 'correctAnswer',
					'clue_element.value' => 'clueData',
					'prompt_element.value' => 'prePop',
					'audio.location' => 'audioFile',
					'word_audio.location' => 'wordAudioFile',
					'module_elements.id' => 'KE_value',
				), array(
					'module_elements',
					'module_group' => array('on' => 'content_groups', 'module_group.id' => 'module_elements.content_group_id'),
					'game_type' => array('on' => 'content_types', 'game_type.id' => 'module_group.content_type_id'),
					
					'word_element' => array('on' => 'content_items', 'word_element.id' => 'module_elements.content_item_id'),
					'word_audio' => array('on' => 'media_assets', 'word_audio.content_group_id' => 'word_element.group_id'),
					
					'clue_rel' => array(
						'on' => 'content_group_relationships',
						'clue_rel.primary_group_id' => 'module_group.id',
						'clue_rel.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue')),
					),
					'clue_group' => array('on' => 'content_groups', 'clue_group.id' => 'clue_rel.secondary_group_id'),
					'clue_element' => array(
						'on' => 'content_items',
						'clue_element.group_id' => 'clue_group.id',
						'clue_element.primary_locale_id' => 'module_elements.primary_locale_id',
						'clue_element.secondary_locale_id' => 'module_elements.secondary_locale_id',
						'clue_element.weave_level' => 'module_elements.weave_level',
					),
					
					'prompt_rel' => array(
						'on' => 'content_group_relationships',
						'prompt_rel.primary_group_id' => 'module_group.id',
						'prompt_rel.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt')),
					),
					'prompt_group' => array('on' => 'content_groups', 'prompt_group.id' => 'prompt_rel.secondary_group_id'),
					'prompt_element' => array(
						'on' => 'content_items',
						'prompt_element.group_id' => 'prompt_group.id',
						'prompt_element.primary_locale_id' => 'module_elements.primary_locale_id',
						'prompt_element.secondary_locale_id' => 'module_elements.secondary_locale_id',
						'prompt_element.weave_level' => 'module_elements.weave_level',
					),
					
					'answer_rel' => array(
						'on' => 'content_group_relationships',
						'answer_rel.primary_group_id' => 'module_group.id',
						'answer_rel.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer')),
					),
					'answer_group' => array('on' => 'content_groups', 'answer_group.id' => 'answer_rel.secondary_group_id'),
					'answer_element' => array(
						'on' => 'content_items',
						'answer_element.group_id' => 'answer_group.id',
						'answer_element.primary_locale_id' => 'module_elements.primary_locale_id',
						'answer_element.secondary_locale_id' => 'module_elements.secondary_locale_id',
						'answer_element.weave_level' => 'module_elements.weave_level',
					),
					'audio' => array('on' => 'media_assets', 'audio.content_group_id' => 'answer_group.id')
				), array(
					'module_elements.module_id' => $zipID,
				), 'module_elements.sequence');

	
	
	

	echo '<pre>';
	print_r($games);
	echo '</pre>';

	
	
	
	
	
	HEAD_Includes();
	echo '
	<h1>Questions for Translate Zip '.$zipID.'</h1>';
		echo '<table class="moduleList" cellpadding="0" cellspacing="0">
		<tr>
			<th>id</th>
			<th>Type</th>
			<th>PrePop</th>
			<th>Clue(if one exsists)</th>
			<th>Answer(s)</th>
			<th>&nbsp;</th>
		</tr>';
		$fanfold = true;

		
		/*
		echo '<pre>';
		print_r($questions);
		echo '</pre>';
		
		*/
		//function count($fields, $tables, $conditions = null, $allow_deleted = false) {
		foreach($games AS $question) {
				if($fanfold){
					$rowStyle="fanfold2";
					$fanfold = !$fanfold;
				}
				else{
					$fanfold=!$fanfold;
					$rowStyle="fanfold1";
				}
				//function select($fields, $tables, $conditions = null, $allow_deleted = false, $group = null) {
				/*
				echo '<pre>';
				print_r(get_answers($question));
				echo '</pre>';
				
				*/	
				
				
				/*
				die();
				*/
				
				switch ($question['type']) {
				  case 'fillIn':	
						$quest = $question['clueData'];
						$ans = $db->select(
							array(
								'content_items.value'
							),
							array(
								'module_elements',
								'content_items' => array('content_items.id' => 'module_elements.content_item_id')
							),
							array(
								'module_elements.module_id' => $zipID,
								'content_items.id' => $question['wordID']
							)
						);
						
						if (empty($ans['value'])) {
						$ans = $question['correctAnswer'];
						}	else {
						$ans = implode(" ", $ans);
        		}					
					break;
					case 'arrange':	
						$quest = $question['clueData'];
						$ans = $db->select(
							array(
								'content_items.value'
							),
							array(
								'module_elements',
								'content_items' => array('content_items.id' => 'module_elements.content_item_id')
							),
							array(
								'module_elements.module_id' => $zipID,
								'content_items.id' => $question['wordID']
							)
						);
						$ans = implode(" ", $ans);
					break;
					case 'typeIn': 
						$quest = $question['clueData'];	
						$ans = $db->select(
							array(
								'content_items.value'
							),
							array(
								'module_elements',
								'content_items' => array('content_items.id' => 'module_elements.content_item_id')
							),
							array(
								'module_elements.module_id' => $zipID,
								'content_items.id' => $question['wordID']
							)
						);
						if (empty($ans['value'])) {
						$ans = $question['correctAnswer'];
						}	else {
        		
						$ans = implode(" ", $ans);
        		}					
					break;
					case 'spaceOut':	
						$quest = $question['clueData'];
						$ans = $question['correctAnswer'];
						
					break;
					case 'scatterWords':
						$quest = 'No Clue';
						$qex = explode(",", $question['prePop']);
						$ans = array();
						for ($i = 0; $i < count($qex); $i++) {
						
							$ans[$i] = $db->select(
								array(
									'content_items.value'
								),
								array(
									'module_elements',
									'content_items' => array('content_items.id' => 'module_elements.content_item_id')
								),
								array(
									'module_elements.module_id' => $zipID,
									'content_items.id' => $qex[$i]
								)
							);
					}
					$ans = implode(", ", $ans[0]);
					break;
					case 'scatterPhrase':
						$index = $db->select(
							'answer.value',
							array(
								'module_elements',
								'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'module_elements.content_group_id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('relationship_types.type_name' => 'correct answer')) ),
								'answer' => array('on' => 'content_items', 'answer.group_id' => 'content_group_relationships.secondary_group_id')
							),
							array(
								'module_elements.id' => $question['KE_value']
							)
						);
						$quest = $question['clueData'];	
						$qex = explode(":::", $question['prePop']);						
						$prompt = implode(", ", $qex);						
						$ans = $qex[$index];
										
				
					break;
					default:	
						die('Broken');
					break;
				
				}
				
				if (isset($prompt)){
					$preprompt = $prompt;
				} elseif (!empty($question['prePop'])){
					$preprompt = $question['prePop'];
				} else {
					$preprompt = 'none';
				}
				
				
				
				echo '<pre>';
				print_r($preprompt);
				echo '</pre>';
				
				
					
				echo '
				<tr>
					<td class="'.$rowStyle.' smallGray">'.$question['KE_value'].'</td>
					<td class="'.$rowStyle.' smallGray">'.$question['type'].'</td>
					<td class="'.$rowStyle.'">'.$preprompt.'</td>
					<td class="'.$rowStyle.'"><a href="'.THIS_PAGE.'?doThis=edit&questionID='.$question['KE_value'].'&question_type='.$question['type'].'&moduleID='.$zipID.'">'.(!empty($quest)?$quest:'none').'</a></td>
					<td class="'.$rowStyle.'">'.$ans.'</td>
					<td class="'.$rowStyle.'"><a href="'.THIS_PAGE.'?doThis=edit&questionID='.$question['KE_value'].'&question_type='.$question['type'].'&moduleID='.$zipID.'"><img src="'.ROOT_PATH.'images/i_sm_edit.gif"></a>
					 <a href="'.THIS_PAGE.'?doThis=delete&questionID='.$question['KE_value'].'&moduleID='.$zipID.'" onclick="return confirm(\'Are you sure you want to delete this question?\')"><img src="'.ROOT_PATH.'images/i_sm_del.gif"></a>
					</td>
					</tr>';
					
					
	
		}
	echo '<pre>';
		echo '</pre>';
	echo '</table>';	
	
	/*
	echo '<pre>';
	print_r($zipID);
	echo '</pre>';
	TypeIn word	TypeIn Phrase	FillIn Word	Arrange	Scatter words	Scatter Pictures	FillIn Phrase	Space Out	Listen	Scatter Phrase

	*/
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=fillin">New Fill In</a><br> ';
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=typeIn">New Type In</a><br> ';
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=arrange">New Arrange(Not Working)</a><br> ';
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterpicture">New Picture Scatter(Not Working)</a><br> ';
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=spaceout">New Space Out</a><br> ';
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=listen">New Listen(Not Working)</a><br> ';
	echo '<a href="'.THIS_PAGE.'?doThis=edit&moduleID='.$zipID.'&question_type=scatterphrase">New Phrase Scatter(Not Working)</a><br> ';
	HTML_Footer();
}

function get_questions($moduleID = null, $questionID = null) {
	global $db;
	//$db->echo = true;
	$conditions = array('content_types.type_name'=>array('typeIn', 'fillIn', 'arrange', 'spaceOut', 'listen', 'scatterWords', 'scatterPictures', 'scatterPhrase'));
	if ($moduleID != null) $conditions['module_elements.module_id'] = $moduleID;
	if ($questionID != null) $conditions['module_elements.id'] = $questionID;
	
	$questions = $db->select_all(
		array(
			'content_groups.id' => 'groupID',
			'module_elements.id' =>'element_id',
			'module_elements.sequence',
			'module_elements.module_id',
			'content_items.value'=>'question',
			'content_types.type_name'=>'question_type'
		),
		array(
			'module_elements',
			'content_groups' => array('content_groups.id'=>'module_elements.content_group_id'),
			'content_types'	=> array('content_types.id'=>'content_groups.content_type_id'),
			'content_items'	=> array('content_items.group_id'=>'content_groups.id')
		),
		$conditions			
	);
	foreach($questions as $question) {
		$questionData = get_answers($question);
	}
	return $questionData;

}

function get_answers($question) {
	global $db;
	//$db->echo = true;	
		$answerData = array();

	
	
		switch($question['question_type']) {
			case 'multiple-choice question' :
				//$db->echo=true;
				$question['answers'] = $db->select_all(
					array(//fields
						'answerGroup.id',
						'answerGroup.sequence',
						'content_items.value'=>'answer',
						'relationship_types.type_name'=>'value'
					),
					array(//tables
						'content_groups',
						'content_group_relationships' => array('content_group_relationships.primary_group_id'=>'content_groups.id'),
						'answerGroup' => array('on'=>'content_groups','answerGroup.id'=>'content_group_relationships.secondary_group_id'),
						'content_items' => array('content_items.group_id'=>'answerGroup.id'),
						'content_types' => array('content_types.id'=>'answerGroup.content_type_id'),
						'relationship_types' => array('relationship_types.id'=>'content_group_relationships.relationship_type_id')
					),
					array(//conditions
						'content_group_relationships.primary_group_id'=>$question['groupID'],
						'content_types.type_name'=>"question answer",
						'content_items.primary_locale_id' => $_SESSION['primary_locale'],
						'content_items.secondary_locale_id' => $_SESSION['secondary_locale']
					),
					//order
					array('sequence','id')
				);
				
				//die(print_r($question));
				break;
			case 'true/false question'	: //return the options True/false, with correct status
				//$db->echo=true;
				$answerData = $db->select(
					//$fields
					'property_options.value', 
					array(//$tables
						'content_group_properties',
						'property_options' => array('property_options.id' => 'content_group_properties.property_id')	
					), 
					array(//$conditions = null
						'content_group_properties.item_id' => $question['groupID']				
					)
				);//select only the correct response
			
				$question['answers'] = array(0=>array('value' => $answerData));
				
				break;
			case 'short-answer question'	: //select and return only the correct response
//			default	:
				//echo '<div class="blueText">DEFAULT was selected in get_answers</div>';
				$question['answers'] = $db->select_all(
					
					array(//$fields
						'answerGroup.id' => 'answerGroup_id',
						'answerGroup.sequence',
						//'questionGroup.id' => 'questionGroup_id',
						//'questionItem.value' => 'question',
						'content_items.value' => 'answer',
						'relationship_types.type_name' => 'value'						
					),					
					array(//$tables
						'questionGroup' => 'content_groups',
						'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'questionGroup.id'),
						'answerGroup' => array('on'=>'content_groups','answerGroup.id'=>'content_group_relationships.secondary_group_id'),
						'content_items' => array('content_items.group_id'=>'answerGroup.id'),
						'content_types' => array('content_types.id'=>'answerGroup.content_type_id'),
						'relationship_types' => array('relationship_types.id'=>'content_group_relationships.relationship_type_id')					
					), 
					array(//$conditions = null
						'content_group_relationships.primary_group_id'=>$question['groupID'],
						'content_types.type_name'=>"question answer",
						'content_items.primary_locale_id' => $_SESSION['primary_locale'],
						'content_items.secondary_locale_id' => $_SESSION['secondary_locale']
					)
				);
				break;
			default:
				$question['answers'] = 'undefined';
				break;
		}	
	return $question;
}

function fillin($questionID, $wordlist, $modulid){
global $db;
	
	//die('I will now end so that I dont create to many things');
	
	
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	
	$db->echo=true;
	//Todo: finish ME
	$questiondata = $db->select(
		array(
			'answer_item.value' => 'answer',
			'clue_item.value' => 'clue',
			'prepop_item.value' => 'prepop',
			'word_item.value' => 'word',
			'module_group.sequence' => 'dir'
		),
		array(
			'module_elements',
			'word_item' => array('on' => 'content_items', 'word_item.id' => 'module_elements.content_item_id'),
			'module_group' => array('on' => 'content_groups', 'module_group.id' => 'module_elements.content_group_id'),
			'answer_relation' => array('on' => 'content_group_relationships', 'answer_relation.primary_group_id' => 'module_group.id', 'answer_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))),
			'answer_group' => array('on' => 'content_groups', 'answer_group.id' => 'answer_relation.secondary_group_id'),
			'answer_item' => array('on' => 'content_items', 'answer_item.group_id' => 'answer_group.id'),
			'clue_relation' => array('on' => 'content_group_relationships', 'clue_relation.primary_group_id' => 'module_group.id', 'clue_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
			'clue_group' => array('on' => 'content_groups', 'clue_group.id' => 'clue_relation.secondary_group_id'),
			'clue_item' => array('on' => 'content_items', 'clue_item.group_id' => 'clue_group.id'),
			'prepop_relation' => array('on' => 'content_group_relationships', 'prepop_relation.primary_group_id' => 'module_group.id', 'prepop_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))),
			'prepop_group' => array('on' => 'content_groups', 'prepop_group.id' => 'prepop_relation.secondary_group_id'),
			'prepop_item' => array('on' => 'content_items', 'prepop_item.group_id' => 'prepop_group.id'),
		),
		array(
			'module_elements.id' => $questionID
		)
	);
	
	
	echo '<pre>';
	print_r($questiondata);
	echo '</pre>';
	
	
	
	
	echo '<h2>Fill Word</h2>';


	echo '<form name="fillword" method="post" action="'.THIS_PAGE.'?doThis=save&moduleID='.$modulid.'&question_type='.$_GET['question_type'].'">';

	echo'
		<input type="hidden" name="doThis" value="save">
		<input type="hidden" name="questionID" value="'.$questionID.'">
		<input type="hidden" name="question_type" value="fillin">
		<input type="radio" name="dir" value="0" '.(($questiondata['dir'] == 0)?'checked=checked':'').'>'.$_SESSION['primary_locale_name'].' to '.$_SESSION['secondary_locale_name'].'
		<br>
		<input type="radio" name="dir" value="1" '.(($questiondata['dir'] == 1)?'checked=checked':'').'>'.$_SESSION['secondary_locale_name'].' to '.$_SESSION['primary_locale_name'].'
		<p class="instructions">Instructions: Write your Clue in the box below and the answer in the field labeled Answer.  
	The student will be asked to fill in a word.</p>';
	echo '<h3>Prompt: </h3> <input type="text" name="prepop" value="';
		if (!empty($questiondata['prepop'])){
			echo $questiondata['prepop'];
		}
	echo '">';
	echo '<h3>Clue: </h3> <input type="text" name="question" value="';
	if (!empty($questiondata['clue'])){
		echo $questiondata['clue'];
	}
	echo '">';
	echo '<h3>Answer:</h3> <input type="text" name="answer" value="';
	if (!empty($questiondata['answer'])){
		echo $questiondata['answer'];
	}else{
		if (!empty($questiondata['word'])) {
  		echo $questiondata['word'];
  	}
	}
	echo '"><br /><br /></sel ect><input type="submit" name="submitMC" value="Save Question and Answers">
				<input type="hidden" name="module_id" value="'.$_GET['moduleID'].'">
		</form>';
}

function savefillin($questionID){
	global $db;

	echo 'I am Now on Fillword with the following to work with <br /><br />';
	echo 'POST = ';
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
	echo '<br />';
	echo 'GET = ';
	echo '<pre>';
	print_r($_GET);
	echo '</pre>';
	echo '<br />';
	echo 'QuestionID = ';
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	echo '<br />';
	
	
	
		$db->echo=true;
		
		if ($questionID == 'new'){
	//A fill in word first needs a Group
	$element_group_id = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'fillIn')),
			'sequence' => $_POST['dir']
		)
	);
	
	//Lets create a module element! WOO
	$element_id = $db->insert('module_elements',
		array(
			'module_id' => $_POST['module_id'],
			'content_group_id' => $element_group_id,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale']
		)
	);
	
	//Fillin Phrase Elements
	//CorrectAnswer -> content_group_relationship -> Content_Group -> Content_items
	//clueData -> content_group_relationship -> content_group -> content_items
	//prePop -> content_group_relationships -> content_group -> content_items
	
	
		//correctAnswer
			//answer_group
	$answer_group = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'question answer'))
		)
	);		
		
		//answer_item	
	$answer_item = $db->insert(
			'content_items',
		array(
			'group_id' => $answer_group,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale'],
			'value' => $_POST['answer']
		)
	);
	
		//answer_relation
	$answer_relation = $db->insert('content_group_relationships',
		array(
			'primary_group_id' => $element_group_id,
			'secondary_group_id' => $answer_group,
			'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))
		)
	);
	
		//clueData
			//clue_group
		$clue_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'clue'))
			)
		);
			
			//clue_item
		$clue_item = $db->insert(
				'content_items',
			array(
				'group_id' => $clue_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['question']
			)
		);
			
			//clue_relation
		$clue_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $clue_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))
			)
		);
	
		//prePop
			//prePop_group
		$prePop_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'prompt'))
			)
		);
			
			//prePop_item
		$prePop_item = $db->insert(
			'content_items',
			array(
				'group_id' => $prePop_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['prepop']
			)
		);
			
			//clue_relation
		$prePop_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $prePop_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))
			)
		);

	} else {
	//TODO SELECT SOMETHIAGN
	
		$element_group_id = $db->select('content_groups.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id')
			),
			array(
				'module_elements.id' => $questionID	
			)
		);
		
		$db->update(
			array(
				'content_groups'
			),
			array(
				'content_groups.sequence' => $_POST['dir']
			),	
			array(
				'content_groups.id' => $element_group_id
			)
		);
		
		$answer_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
	
		if (!$answer_id){
		$answer_group = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'question answer'))
		)
	);		
		
		//answer_item	
	$answer_item = $db->insert(
			'content_items',
		array(
			'group_id' => $answer_group,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale'],
			'value' => $_POST['answer']
		)
	);
	
		//answer_relation
	$answer_relation = $db->insert('content_group_relationships',
		array(
			'primary_group_id' => $element_group_id,
			'secondary_group_id' => $answer_group,
			'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))
		)
	);
	
	} else {
	
		$answer_item = $db->save(
			array(
				'content_items'
			),
			array(
				'value' => $_POST['answer']
			),
			array(
				'id' => $answer_id
			)
		);
		
	}
	
	
		$clue_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
			
		if (!$clue_id) {
  	//clueData
			//clue_group
		$clue_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'clue'))
			)
		);
			
			//clue_item
		$clue_item = $db->insert(
				'content_items',
			array(
				'group_id' => $clue_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['question']
			)
		);
			
			//clue_relation
		$clue_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $clue_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))
			)
		);
  	
  } else {
			//clue_item
		$clue_item = $db->save(
				'content_items',
			array(
				'value' => $_POST['question']
			),
			array(
				'id' => $clue_id
			)
		);
		
		}
		//prePop
			//prePop_group
		$prepop_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
			
		if (!$prepop_id) {
  		//prePop_group
		$prePop_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'prompt'))
			)
		);
			
			//prePop_item
		$prePop_item = $db->insert(
			'content_items',
			array(
				'group_id' => $prePop_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['prepop']
			)
		);
			
			//clue_relation
		$prePop_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $prePop_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))
			)
		);
  } else{
			
			//prePop_item
		$prePop_item = $db->save(
			'content_items',
			array(
			'value' => $_POST['prepop']
			),
			array(
				'id' => $prepop_id
			)
		);
		}
		//function update($table, $updates, $conditions) {
		$db->update('module_elements',
			array(
				'content_item_id' => NULL
			),
			array(
				'id' => $questionID
			)
		);
	}

		
}

function typeIn($questionID, $wordlist, $modulid){
global $db;
	
	//die('I will now end so that I dont create to many things');
	
	
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	
	$db->echo=true;
	//Todo: finish ME
	$questiondata = $db->select(
		array(
			'answer_item.value' => 'answer',
			'clue_item.value' => 'clue',
			'word_item.value' => 'word',
			'module_group.sequence' => 'dir'
		),
		array(
			'module_elements',
			'word_item' => array('on' => 'content_items', 'word_item.id' => 'module_elements.content_item_id'),
			'module_group' => array('on' => 'content_groups', 'module_group.id' => 'module_elements.content_group_id'),
			'answer_relation' => array('on' => 'content_group_relationships', 'answer_relation.primary_group_id' => 'module_group.id', 'answer_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))),
			'answer_group' => array('on' => 'content_groups', 'answer_group.id' => 'answer_relation.secondary_group_id'),
			'answer_item' => array('on' => 'content_items', 'answer_item.group_id' => 'answer_group.id'),
			'clue_relation' => array('on' => 'content_group_relationships', 'clue_relation.primary_group_id' => 'module_group.id', 'clue_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
			'clue_group' => array('on' => 'content_groups', 'clue_group.id' => 'clue_relation.secondary_group_id'),
			'clue_item' => array('on' => 'content_items', 'clue_item.group_id' => 'clue_group.id')
		),
		array(
			'module_elements.id' => $questionID
		)
	);
	
	
	echo '<pre>';
	print_r($questiondata);
	echo '</pre>';
	
	
	
	
	echo '<h2>Type Word</h2>';


	echo '<form name="fillword" method="post" action="'.THIS_PAGE.'?doThis=save&moduleID='.$modulid.'&question_type='.$_GET['question_type'].'">';

	echo'
		<input type="hidden" name="doThis" value="save">
		<input type="hidden" name="questionID" value="'.$questionID.'">
		<input type="hidden" name="question_type" value="typeIn">
		<input type="radio" name="dir" value="0" '.(($questiondata['dir'] == 0)?'checked=checked':'').'>'.$_SESSION['primary_locale_name'].' to '.$_SESSION['secondary_locale_name'].'
		<br>
		<input type="radio" name="dir" value="1" '.(($questiondata['dir'] == 1)?'checked=checked':'').'>'.$_SESSION['secondary_locale_name'].' to '.$_SESSION['primary_locale_name'].'
		<p class="instructions">Instructions: Write your Clue in the box below and the answer in the field labeled Answer.  
	The student will be asked to fill in a word.</p>';
	echo '<h3>Clue: </h3> <input type="text" name="question" value="';
	if (!empty($questiondata['clue'])){
		echo $questiondata['clue'];
	}
	echo '">';
	echo '<h3>Answer:</h3> <input type="text" name="answer" value="';
	if (!empty($questiondata['answer'])){
		echo $questiondata['answer'];
	}else{
		if (!empty($questiondata['word'])) {
  		echo $questiondata['word'];
  	}
	}
	echo '"><br /><br /></sel ect><input type="submit" name="submitMC" value="Save Question and Answers">
				<input type="hidden" name="module_id" value="'.$_GET['moduleID'].'">
		</form>';
}
	
function savetypeIn($questionID){
	global $db;

	echo 'I am Now on typeIn with the following to work with <br /><br />';
	echo 'POST = ';
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
	echo '<br />';
	echo 'GET = ';
	echo '<pre>';
	print_r($_GET);
	echo '</pre>';
	echo '<br />';
	echo 'QuestionID = ';
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	echo '<br />';
	
	
	
		$db->echo=true;
		
		if ($questionID == 'new'){
	//A fill in word first needs a Group
	$element_group_id = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'typeIn')),
			'sequence' => $_POST['dir']
		)
	);
	
	//Lets create a module element! WOO
	$element_id = $db->insert('module_elements',
		array(
			'module_id' => $_POST['module_id'],
			'content_group_id' => $element_group_id,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale']
		)
	);
	
	//Fillin Phrase Elements
	//CorrectAnswer -> content_group_relationship -> Content_Group -> Content_items
	//clueData -> content_group_relationship -> content_group -> content_items
	//prePop -> content_group_relationships -> content_group -> content_items
	
	
		//correctAnswer
			//answer_group
	$answer_group = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'question answer'))
		)
	);		
		
		//answer_item	
	$answer_item = $db->insert(
			'content_items',
		array(
			'group_id' => $answer_group,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale'],
			'value' => $_POST['answer']
		)
	);
	
		//answer_relation
	$answer_relation = $db->insert('content_group_relationships',
		array(
			'primary_group_id' => $element_group_id,
			'secondary_group_id' => $answer_group,
			'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))
		)
	);
	
		//clueData
			//clue_group
		$clue_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'clue'))
			)
		);
			
			//clue_item
		$clue_item = $db->insert(
				'content_items',
			array(
				'group_id' => $clue_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['question']
			)
		);
			
			//clue_relation
		$clue_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $clue_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))
			)
		);

	} else {
	//TODO SELECT SOMETHIAGN
	
		$element_group_id = $db->select('content_groups.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id')
			),
			array(
				'module_elements.id' => $questionID	
			)
		);
		
		$db->update(
			array(
				'content_groups'
			),
			array(
				'content_groups.sequence' => $_POST['dir']
			),	
			array(
				'content_groups.id' => $element_group_id
			)
		);
		
		$answer_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
			
		if (!$answer_id){
		
		$answer_group = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'question answer'))
		)
	);		
		
		//answer_item	
	$answer_item = $db->insert(
			'content_items',
		array(
			'group_id' => $answer_group,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale'],
			'value' => $_POST['answer']
		)
	);
	
		//answer_relation
	$answer_relation = $db->insert('content_group_relationships',
		array(
			'primary_group_id' => $element_group_id,
			'secondary_group_id' => $answer_group,
			'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))
		)
	);
	
	} else {
	
		$answer_item = $db->save('content_items',
			array(
				'value' => $_POST['answer']
			),
			array(
				'id' => $answer_id
			)
		);
		
	}
	
		$clue_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
		
		if (!$clue_id){
		
		$clue_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'clue'))
			)
		);
			
			//clue_item
		$clue_item = $db->insert(
				'content_items',
			array(
				'group_id' => $clue_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['question']
			)
		);
			
			//clue_relation
		$clue_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $clue_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))
			)
		);
		
		} else {
			
			//clue_item
		$clue_item = $db->save(
				'content_items',
			array(
				'value' => $_POST['question']
			),
			array(
				'id' => $clue_id
			)
		);
		
		}
		
		//function update($table, $updates, $conditions) {
		$db->update('module_elements',
			array(
				'content_item_id' => NULL
			),
			array(
				'id' => $questionID
			)
		);
		
		
		
		
	}
}



function spaceout($questionID, $words, $modulid){
global $db;
	
	//die('I will now end so that I dont create to many things');
	
	
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	
	$db->echo=true;
	//Todo: finish ME
	$questiondata = $db->select(
		array(
			'answer_item.value' => 'answer',
			'clue_item.value' => 'clue',
			'word_item.value' => 'word',
			'module_group.sequence' => 'dir',
			'prepop_item.value' => 'prepop'
		),
		array(
			'module_elements',
			'word_item' => array('on' => 'content_items', 'word_item.id' => 'module_elements.content_item_id'),
			'module_group' => array('on' => 'content_groups', 'module_group.id' => 'module_elements.content_group_id'),
			'answer_relation' => array('on' => 'content_group_relationships', 'answer_relation.primary_group_id' => 'module_group.id', 'answer_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))),
			'answer_group' => array('on' => 'content_groups', 'answer_group.id' => 'answer_relation.secondary_group_id'),
			'answer_item' => array('on' => 'content_items', 'answer_item.group_id' => 'answer_group.id'),
			'clue_relation' => array('on' => 'content_group_relationships', 'clue_relation.primary_group_id' => 'module_group.id', 'clue_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
			'clue_group' => array('on' => 'content_groups', 'clue_group.id' => 'clue_relation.secondary_group_id'),
			'clue_item' => array('on' => 'content_items', 'clue_item.group_id' => 'clue_group.id'),
			'prepop_relation' => array('on' => 'content_group_relationships', 'prepop_relation.primary_group_id' => 'module_group.id', 'prepop_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))),
			'prepop_group' => array('on' => 'content_groups', 'prepop_group.id' => 'prepop_relation.secondary_group_id'),
			'prepop_item' => array('on' => 'content_items', 'prepop_item.group_id' => 'prepop_group.id')
		),
		array(
			'module_elements.id' => $questionID
		)
	);
	
	
	echo '<pre>';
	print_r($questiondata);
	echo '</pre>';
	
	
	
	
	echo '<h2>Space Out</h2>';


	echo '<form name="fillword" method="post" action="'.THIS_PAGE.'?doThis=save&moduleID='.$modulid.'&question_type='.$_GET['question_type'].'">';

	echo'
		<input type="hidden" name="doThis" value="save">
		<input type="hidden" name="questionID" value="'.$questionID.'">
		<input type="hidden" name="question_type" value="spaceout">
		<input type="radio" name="dir" value="0" '.(($questiondata['dir'] == 0)?'checked=checked':'').'>'.$_SESSION['primary_locale_name'].' to '.$_SESSION['secondary_locale_name'].'
		<br>
		<input type="radio" name="dir" value="1" '.(($questiondata['dir'] == 1)?'checked=checked':'').'>'.$_SESSION['secondary_locale_name'].' to '.$_SESSION['primary_locale_name'].'
		<p class="instructions">Instructions: Write your Clue in the box below and the answer in the field labeled Answer.  
	The student will be asked to fill in a word.</p>';
	echo '<h3>Prompt: </h3> <input type="text" name="prepop" value="';
	if (!empty($questiondata['prepop'])){
		echo $questiondata['prepop'];
	}
	echo '">';
	echo '<h3>Clue: </h3> <input type="text" name="question" value="';
	if (!empty($questiondata['clue'])){
		echo $questiondata['clue'];
	}
	echo '">';
	echo '<h3>Answer:</h3> <input type="text" name="answer" value="';
	if (!empty($questiondata['answer'])){
		echo $questiondata['answer'];
	}else{
		if (!empty($questiondata['word'])) {
  		echo $questiondata['word'];
  	}
	}
	echo '"><br /><br /></select><input type="submit" name="submitMC" value="Save Question and Answers">
				<input type="hidden" name="module_id" value="'.$_GET['moduleID'].'">
		</form>';



}

function savespaceout($questionID){
global $db;

	echo 'I am Now on Fillword with the following to work with <br /><br />';
	echo 'POST = ';
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
	echo '<br />';
	echo 'GET = ';
	echo '<pre>';
	print_r($_GET);
	echo '</pre>';
	echo '<br />';
	echo 'QuestionID = ';
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	echo '<br />';
	
	
	
		$db->echo=true;
		
		if ($questionID == 'new'){
	//A fill in word first needs a Group
	$element_group_id = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'spaceOut')),
			'sequence' => $_POST['dir']
		)
	);
	
	//Lets create a module element! WOO
	$element_id = $db->insert('module_elements',
		array(
			'module_id' => $_POST['module_id'],
			'content_group_id' => $element_group_id,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale']
		)
	);
	
	//Fillin Phrase Elements
	//CorrectAnswer -> content_group_relationship -> Content_Group -> Content_items
	//clueData -> content_group_relationship -> content_group -> content_items
	//prePop -> content_group_relationships -> content_group -> content_items
	
	
		//correctAnswer
			//answer_group
	$answer_group = $db->insert('content_groups',
		array(
			'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'question answer'))
		)
	);		
		
		//answer_item	
	$answer_item = $db->insert(
			'content_items',
		array(
			'group_id' => $answer_group,
			'primary_locale_id' => $_SESSION['primary_locale'],
			'secondary_locale_id' => $_SESSION['secondary_locale'],
			'value' => $_POST['answer']
		)
	);
	
		//answer_relation
	$answer_relation = $db->insert('content_group_relationships',
		array(
			'primary_group_id' => $element_group_id,
			'secondary_group_id' => $answer_group,
			'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))
		)
	);
	
		//clueData
			//clue_group
		$clue_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'clue'))
			)
		);
			
			//clue_item
		$clue_item = $db->insert(
				'content_items',
			array(
				'group_id' => $clue_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['question']
			)
		);
			
			//clue_relation
		$clue_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $clue_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))
			)
		);
	
		//prePop
			//prePop_group
		$prePop_group = $db->insert('content_groups',
			array(
				'content_type_id' => $db->select('id', 'content_types', array('type_name' => 'prompt'))
			)
		);
			
			//prePop_item
		$prePop_item = $db->insert(
			'content_items',
			array(
				'group_id' => $prePop_group,
				'primary_locale_id' => $_SESSION['primary_locale'],
				'secondary_locale_id' => $_SESSION['secondary_locale'],
				'value' => $_POST['prepop']
			)
		);
			
			//clue_relation
		$prePop_relation = $db->insert('content_group_relationships',
			array(
				'primary_group_id' => $element_group_id,
				'secondary_group_id' => $prePop_group,
				'relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))
			)
		);

	} else {
	//TODO SELECT SOMETHIAGN
	
		$element_group_id = $db->select('content_groups.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id')
			),
			array(
				'module_elements.id' => $questionID	
			)
		);
		
		$db->update(
			array(
				'content_groups'
			),
			array(
				'content_groups.sequence' => $_POST['dir']
			),	
			array(
				'content_groups.id' => $element_group_id
			)
		);
		
		$answer_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'correct answer'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
	
		$answer_item = $db->save(
			array(
				'content_items'
			),
			array(
				'value' => $_POST['answer']
			),
			array(
				'id' => $answer_id
			)
		);

	
	
		$clue_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);		
				//clue_item
		$clue_item = $db->save(
				'content_items',
			array(
				'value' => $_POST['question']
			),
			array(
				'id' => $clue_id
			)
		);
		
			//prePp
			//prePop_group
		$prepop_id = $db->select(
				'content_items.id',
			array(
				'module_elements',
				'content_groups' => array('content_groups.id' => 'module_elements.content_group_id'),
				'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'content_groups.id', 'content_group_relationships.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))),
				'answer_groups' => array('on' => 'content_groups', 'answer_groups.id' => 'content_group_relationships.secondary_group_id'),
				'content_items' => array('content_items.group_id' => 'answer_groups.id')
			),
			array(
				'module_elements.id' => $questionID
			)	
		);
	
			//prePop_item
		$prePop_item = $db->save(
			'content_items',
			array(
			'value' => $_POST['prepop']
			),
			array(
				'id' => $prepop_id
			)
		);

		//function update($table, $updates, $conditions) {
		$db->update('module_elements',
			array(
				'content_item_id' => NULL
			),
			array(
				'id' => $questionID
			)
		);
	}



}

function scatterphrase($questionID, $words, $modulid){
global $db;
	
	//die('I will now end so that I dont create to many things');
	
	
	echo '<pre>';
	print_r($questionID);
	echo '</pre>';
	
	$db->echo=true;
	//Todo: finish ME
	$questiondata = $db->select(
		array(
			'clue_item.value' => 'clue',
			'word_item.value' => 'word',
			'module_group.sequence' => 'dir',
			'prepop_item.value' => 'prepop'
		),
		array(
			'module_elements',
			'word_item' => array('on' => 'content_items', 'word_item.id' => 'module_elements.content_item_id'),
			'module_group' => array('on' => 'content_groups', 'module_group.id' => 'module_elements.content_group_id'),
			'clue_relation' => array('on' => 'content_group_relationships', 'clue_relation.primary_group_id' => 'module_group.id', 'clue_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip clue'))),
			'clue_group' => array('on' => 'content_groups', 'clue_group.id' => 'clue_relation.secondary_group_id'),
			'clue_item' => array('on' => 'content_items', 'clue_item.group_id' => 'clue_group.id'),
			'prepop_relation' => array('on' => 'content_group_relationships', 'prepop_relation.primary_group_id' => 'module_group.id', 'prepop_relation.relationship_type_id' => $db->select('id', 'relationship_types', array('type_name' => 'transzip prompt'))),
			'prepop_group' => array('on' => 'content_groups', 'prepop_group.id' => 'prepop_relation.secondary_group_id'),
			'prepop_item' => array('on' => 'content_items', 'prepop_item.group_id' => 'prepop_group.id')
		),
		array(
			'module_elements.id' => $questionID
		)
	);
	
	$answers = $db->select_all(
		array(
			'answer.id',
			'relationship_types.type_name' => 'con'
		),
		array(
			'module_elements',
			'content_group_relationships' => array('content_group_relationships.primary_group_id' => 'module_elements.content_group_id'),
			'relationship_types' => array('relationship_types.id' => 'content_group_relationships.relationship_type_id'),
			'content_groups' => array('content_groups.id' => 'content_group_relationships.secondary_group_id', 'content_groups.content_type_id' => $db->select('id', 'content_types', array('content_types.type_name' => 'question answer'))),
			'answer' => array('on' => 'content_items', 'answer.group_id' => 'content_groups.id')
		),
		array(
			''
		)
	);
	
	
	
	
	
	
	echo '<pre>';
	print_r($questiondata);
	echo '</pre>';
	
	
	
	
	echo '<h2>Space Out</h2>';


	echo '<form name="fillword" method="post" action="'.THIS_PAGE.'?doThis=save&moduleID='.$modulid.'&question_type='.$_GET['question_type'].'">';

	echo'
		<input type="hidden" name="doThis" value="save">
		<input type="hidden" name="questionID" value="'.$questionID.'">
		<input type="hidden" name="question_type" value="spaceout">
		<input type="radio" name="dir" value="0" '.(($questiondata['dir'] == 0)?'checked=checked':'').'>'.$_SESSION['primary_locale_name'].' to '.$_SESSION['secondary_locale_name'].'
		<br>
		<input type="radio" name="dir" value="1" '.(($questiondata['dir'] == 1)?'checked=checked':'').'>'.$_SESSION['secondary_locale_name'].' to '.$_SESSION['primary_locale_name'].'
		<p class="instructions">Instructions: Write your Clue in the box below and the answer in the field labeled Answer.  
	The student will be asked to fill in a word.</p>';
	echo '<h3>Prompt: </h3> <input type="text" name="prepop" value="';
	if (!empty($questiondata['prepop'])){
		echo $questiondata['prepop'];
	}
	echo '">';
	echo '<h3>Clue: </h3> <input type="text" name="question" value="';
	if (!empty($questiondata['clue'])){
		echo $questiondata['clue'];
	}
	echo '">';
	echo '<h3>Answer:</h3> <input type="text" name="answer" value="';
	if (!empty($questiondata['answer'])){
		echo $questiondata['answer'];
	}else{
		if (!empty($questiondata['word'])) {
  		echo $questiondata['word'];
  	}
	}
	echo '"><br /><br /></select><input type="submit" name="submitMC" value="Save Question and Answers">
				<input type="hidden" name="module_id" value="'.$_GET['moduleID'].'">
		</form>';



}

function savescatterphrase($questionID){


}



?>