Technology Temerity

Class – Training

<?php

class class_training
{    

	/*
	constants
	Damon Vaughn Caskey
	2012_12_18
	
	Global constants. 
	*/		
		
	private	$oDB		= NULL;	//Class object: Database.
	private $oDBA		= NULL;	//Class object: Database (answers).
	private $oSes		= NULL;	//Class object: Session.
	private	$oUtl		= NULL;	//Class object: Filter.
	private	$oErr		= NULL;	//Class object: Error.
	private $oFrm		= NULL;	//Class object: Forms. 		
	private $cQuery		= NULL;
	private $cParams	= NULL;
	private $cTVarsTD	= NULL;	//Class variable ID array.
	private $cTVars		= NULL;
	
	function __construct($oDep)
	{
		/*
		Constructor
		Damon Vaughn Caskey
		2012_12_29
		
		Class constructor.
		*/
		
		/* Import object dependencies. */
		$this->oSes	= $oDep['Ses'];
		$this->oDB 	= $oDep['DB'];
		$this->oFrm	= $oDep['Frm'];
		$this->oFil	= $oDep['Fil'];
		$this->oErr	= $oDep['Err'];
		$this->oDBA	= new class_db(array('Utl' => $oDep['Utl'], 'Err' => $oDep['Err']));
				
		/* Verify object dependencies. */
		if(!$this->oDBA)	trigger_error("Missing object dependency: Database (Ans).", E_USER_ERROR);
		if(!$this->oDB)		trigger_error("Missing object dependency: Database.", E_USER_ERROR);
		if(!$this->oSes)	trigger_error("Missing object dependency: Session.", E_USER_ERROR);	
		if(!$this->oFrm)	trigger_error("Missing object dependency: Forms.", E_USER_ERROR);	
		if(!$this->oFil)	trigger_error("Missing object dependency: Filter.", E_USER_ERROR);		
		if(!$this->oErr)	trigger_error("Missing object dependency: Error.", E_USER_ERROR);		
	}
	
	public function training_quiz_grade($cQuizID)
	{
		$cQuestionCount = NULL;
		$cQuestionRight = NULL;	
		$val			= NULL;	
		$iQueCnt		= 0;
		$cResponse		= NULL;
		$cQuizGradeStr	= NULL;
		$ans			= 0;
		$cPercentage	= 0;
								
		$cQuestionCount = $this->oSes->session_get('quiz_question_count');
		$cQuestionRight = $this->oSes->session_get('quiz_answer_right');
		
		$cQuestionRight = str_replace (" ", "", $cQuestionRight);	//Remove spaces.
		$cQuestionRight = explode(",", $cQuestionRight);			//Break into array.
		
		foreach($cQuestionRight as $val)				//Loop array collection.
		{		
			/* Increment counter. */
			$iQueCnt++;					
			
			/* Get question response */
			$cResponse = $this->oFil->utl_get_post("Q".$iQueCnt);
							
			$cQuizGradeStr.= "Question ".$iQueCnt." response: ";	
			
			if (!$cResponse)
			{
				$cQuizGradeStr.= "No answer.<br/>";
			}
			else				
			{
				if ($cResponse == $val)
				{
					$cQuizGradeStr.= $cResponse." - Correct.<br/>";
					$ans++;
				}
				else
				{
					$cQuizGradeStr.= $cResponse." - Incorrect.<br/>";					
				}	
			}							
		}
		
		$cPercentage	= round(($ans / $iQueCnt)*100);
			
		$cQuizGradeStr.= "<br />You answered ".$ans. " of ".$iQueCnt. " correctly (".$cPercentage."%).<br /><br />";
					
		$array["ans"] 			= $ans;
		$array["percentage"] 	= $cPercentage;
        $array["text"] 			= $cQuizGradeStr;
        			
		return $array;		
		
	}
		
	public function training_quiz_questions($cQuizID, $cOrder=NULL, $cQuantity=NULL)
	{
		/*
		training_quiz_questions
		Damon V. Caskey
		2011_11_29
		~2012_12_23: DB Class.
		
		Populate question list and possible answers from database from Quiz ID.
		
		$cQuizID:	Quiz ID to get quiz questions from. 
		*/		
  					
		$cQuizStr 			= NULL;								//Final output string to be placed into page.
		$QuestionID			= NULL;								//Current question ID in question loop.	
		$cQuestionCount		= 0;								//Include ID into question header.		
		$cQuestionRight		= NULL;								
				
		if(!$cOrder)
		{
			$cOrder = NULL;	//Order in table.	
		}
		else if($cOrder == 1)
		{
			$cOrder = "ORDER BY	question_order";	//User specified order.
		}
		else if($cOrder == 2)
		{
			$cOrder = "ORDER BY	newid()";	//Random order	
		}
		
		$cQuantity = $cQuantity ? "TOP ".$cQuantity : NULL;
		
		/* Construct questions query string. */
		$this->cQuery = "SELECT "
			.$cQuantity
			." *
			FROM 		tbl_class_train_questions
			WHERE		fk_tbl_class_train_parameters_guid_id = ? "
			.$cOrder;	
		
		/* Apply parameters. */
		$this->cParams = array(&$cQuizID);
		
		/* Execute questions query. */
		$this->oDB->db_basic_select($this->cQuery, $this->cParams);
		
		/* Construct answers query string. */
		$this->cQuery = "SELECT 
			* 
			FROM 	tbl_class_train_answers
			WHERE	fk_tbl_class_train_questions_guid_id = ?
			ORDER BY answer_order, value";
		
		/* Apply parameters. */
		$this->cParams = array(&$QuestionID);
		
		while($this->oDB->db_line())
		{
			$QuestionID = $this->oDB->cDBLine["guid_id"];
			
			/*	Get answer set matching current question ID. */
			$this->cParams = array(&$QuestionID);
			
			/*Record answer array. First element will be blank. ( ", A, C, B, A, etc.") */
			if($cQuestionRight)
			{
				$cQuestionRight .= ", " .$this->oDB->cDBLine["right_answer"];
			}
			else
			{
				$cQuestionRight = $this->oDB->cDBLine["right_answer"];
			}
			
			/* Build question string. */
			$cQuizStr 	.="<p><span class='TrainQuestionHeader'>"
						."Question ".++$cQuestionCount			
						."</span><br />"
						."<span class='TrainQuestionText'>"			
						.$this->oDB->cDBLine["question"]		
						."</span><br />";			
			
			/* Execute answers query. */
			$this->oDBA->db_basic_select($this->cQuery, $this->cParams);
			
			while ($this->oDBA->db_line())
    		{			
				$cQuizStr	.=	"<input type='radio' name='Q"					
							.$cQuestionCount							
							."' value='"	
							.$this->oDBA->cDBLine["value"]	
							."' />"
							."<span class='TrainQuestionAnswerHeader'>"									
							.$this->oDBA->cDBLine["value"]
							.")</span> "
							."<span class='TrainQuestionAnswerText'>"										
							.$this->oDBA->cDBLine["text"]
							."</span><br />";							
			}						
		}			
		
		if(!$cQuizStr)
		{
			$cQuizStr = "<h2><span class='alert'>No questions available.</span></h2>";
		}
		
		$this->oSes->session_set('quiz_answer_right', $cQuestionRight);
		$this->oSes->session_set('quiz_question_count', $cQuestionCount);
				
		return $cQuizStr;
	}
	
	public function training_quiz_questions_setup($cQuizID, $cOrder=NULL, $cQuantity=NULL)
	{		
		/*
		class_quiz_questions_0003
		Damon V. Caskey
		2012_10_04
		
		Create form to modifiy questions and answers.
		
		$cQuizID:	Quiz ID to get quiz questions from. 
		*/		
  			
		$cQuizStr 			= NULL;								//Final output string to be placed into page.
		$cQuery				= NULL;								//Query string.
		$cParams			= NULL;								//Parameter array.
		$oDBA				= NULL;								//Database class object (answers). 
		$QuestionID			= NULL;								//Current question ID in question loop.	
		$cOrderLst			= NULL;								//Droplist values for question order.
		$cAnswerValLst		= NULL;
		
		/* Initialize database class objects. */
		$oDBA	= new class_db(array("Utl" => $oUtl, "Err" => $oErr));					//Database class object (Answers).
		
		/* Prepare answer dropdown list values */
		$cAnswerValLst 		= array_merge(array("-" => "-"), range('A', 'Z'));
				
		/* Construct questions query. */
		$cQuery = "SELECT "
			.$cQuantity
			." *
			FROM 		tbl_class_train_questions
			WHERE		fk_tbl_class_train_parameters_guid_id = ? 
			ORDER BY	question_order";	
						
		/* Apply question parameters. */
		$cParams = array(&$cQuizID);
		
		/* Execute questions query */
		$oDB->db_basic_select($cQuery, $cParams);						
		
		$cQuery = "SELECT 
						* 
						FROM 	tbl_class_train_answers
						WHERE	fk_tbl_class_train_questions_guid_id = ?
						ORDER BY answer_order, value";
		
		/* Apply answer parameters. */
		$cParams = array(&$QuestionID);
		
		/* Prepare question order dropdown list values */
		$cOrderLst = range(1, $oDB->iDBRowCount);
			
		while($oDB->db_line())
		{
			$QuestionID = $oDB->cDBLine["guid_id"];						
			$cAnswers = NULL;					  
												
			$oDBA->db_basic_select($cQuery, $cParams);
			
			while($oDBA->db_line())
			{			
				$cAnswers .= "  
					
					<form name='frm_answer_update' id='frm_answer_update_".$oDBA->cDBLine["guid_id"]."' method='post' action='".$_SERVER['PHP_SELF']."#a_question_".$oDB->cDBLine["question_order"]."'>
						<input type='hidden' name='EditMode' value='1'  />
						<input type='hidden' name='id_guid' value='".$oDBA->cDBLine["guid_id"]."' />
						  <table width='100%' border='0' cellspacing='0' cellpadding='2' bgcolor='#DDDDFF'>
							<tr>
							  <td width='10%'>
								  <select name='frm_lst_answer_val' id='frm_lst_answer_val_".$oDBA->cDBLine["guid_id"]."'>"
									.$this->oFrm->forms_select_options($cAnswerValLst, NULL, $oDBA->cDBLine["value"], FALSE).
								  "</select>
							  </td>
							  <td width='70%'><textarea name='frm_ta_answer_text' id='frm_ta_answer_text_".$oDBA->cDBLine["guid_id"]."' cols='35' rows='1'>".$oDBA->cDBLine["text"]."</textarea></td>
							  <td width='10%' align='center'><input type='image' src='/media/image/icon_save_0001.png' name='frm_btn_ans_save' id='frm_btn_save_ans_".$oDBA->cDBLine["guid_id"]."' value='Save' /></td>
							  <td width='10%' align='center'><input type='image' src='/media/image/icon_delete_0001.png' name='frm_btn_ans_delete' id='frm_btn_delete_ans_".$oDBA->cDBLine["guid_id"]."' value='Delete' /></td>
							</tr>
						  </table>
					</form><br />";									
										
			}				  
		  
			$cAnswers .= "<div id='new_answer_".$oDB->cDBLine["guid_id"]."'>

		  	<form name='frm_answer_new' id='frm_answer_new_".$oDB->cDBLine["guid_id"]."' method='post' action='".$_SERVER['PHP_SELF']."#a_question_".$oDB->cDBLine["question_order"]."'>
					<input type='hidden' name='EditMode' value='1'  />
					<input type='hidden' name='id_guid' value='".$oDB->cDBLine["guid_id"]."' />
					  <table width='100%' border='0' cellspacing='0' cellpadding='2' bgcolor='#DDDDFF'>
						<tr>
						  <td width='10%'>                          
							  <select name='frm_lst_answer_val' id='frm_lst_answer_val_".$oDB->cDBLine["guid_id"]."' >"
							   .$this->oFrm->forms_select_options($cAnswerValLst, NULL, "-", FALSE). 
							  "</select>
						  </td>
						  <td width='70%'><textarea name='frm_ta_answer_text' id='frm_ta_answer_text_".$oDB->cDBLine["guid_id"]."' cols='35' rows='1'></textarea></td>
						  <td width='20%' align='center'><input type='image' src='/media/image/icon_save_0001.png' name='frm_btn_ans_add' id='frm_btn_ans_add_".$oDB->cDBLine["guid_id"]."' value='Add' /></td>
						</tr>
					  </table>
				</form></div>";
		  
			  $cQuizStr 	.= "
			  <br />
			  <div id='question_".$oDB->cDBLine["guid_id"]."'>
			  <table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F0FF'>
				<tr>
				  <td>
				  
					  <a name='a_question_".$oDB->cDBLine["question_order"]."' id='a_question_".$oDB->cDBLine["question_order"]."'></a>
					  <form name='frm_question_update' id='frm_question_update_".$oDB->cDBLine["guid_id"]."'  method='post' action='".$_SERVER['PHP_SELF']."#a_question_".$oDB->cDBLine["question_order"]."'>							  
					  <input type='hidden' name='EditMode' value='1'  />
					  <input type='hidden' name='id_guid' value='".$oDB->cDBLine["guid_id"]."' />
						<table width='100%' border='0' cellspacing='0' cellpadding='1' >
						  <tr>
							<th colspan='2'><table width='100%' border='0' cellspacing='0' cellpadding='0'>
							  <tr>
								<td width='77%' align='left'>Question
								  <select name='frm_lst_order' id='frm_lst_order_".$oDB->cDBLine["guid_id"]."'>"
									.$this->oFrm->forms_select_options($cOrderLst, NULL, $oDB->cDBLine["question_order"], FALSE).
								  "</select>
								  
								  </td>
								<td width='11%'><input type='submit' name='frm_btn_que_save' id='frm_btn_que_save_".$oDB->cDBLine["guid_id"]."' value='Save' /></td>
								<td width='12%'><input type='submit' name='frm_btn_que_delete' id='frm_btn_que_delete_".$oDB->cDBLine["guid_id"]."' value='Delete' onclick='return confirmSubmit()' /></td>
							  </tr>
							</table></th>
						  </tr>
						  <tr>
							<td><label for='frm_ta_question_val'>Question Text</label></td>
							<td><textarea name='frm_ta_question_val' id='frm_ta_question_".$oDB->cDBLine["guid_id"]."' cols='45' rows='5'>".$oDB->cDBLine["question"]."</textarea></td>
						  </tr>
						  <tr>
							<td><label for='frm_lst_right_answer'>Correct Response</label></td>
							<td><select name='frm_lst_right_answer' id='frm_lst_right_answer_".$oDB->cDBLine["guid_id"]."' >"
							  .$this->oFrm->forms_select_options($cAnswerValLst, NULL, $oDB->cDBLine["right_answer"], FALSE).
							"</select></td>
						  </tr>
						</table>
					  </form><br />"
					  
					  ."<div id='answers_".$oDB->cDBLine["guid_id"]."'>"
					  .$cAnswers
																 
				  ."</div></td>
				</tr>
			  </table></div>";			
								
		}	
		
			$cOrderLst[$oDB->iDBRowCount+1] = $oDB->iDBRowCount+1; //Add one more to the order list.
		
			$cQuizStr .= "<br />
					  <div id='question_new'>
					  <table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F0FF'>
						<tr>
						  <td>						  
							  <a name='a_question_".($oDB->iDBRowCount+1)."' id='a_question_".($oDB->iDBRowCount+1)."'></a>
							  <form name='frm_question_new' id='frm_question_new'  method='post' action='".$_SERVER['PHP_SELF']."#a_question_".($oDB->iDBRowCount+1)."'>
							  <input type='hidden' name='trigger_type' value='question_add'  />
							  <input type='hidden' name='id_guid' value='".$oDBA->cDBLine["guid_id"]."' />
							  <input type='hidden' name='EditMode' value='1'  />
							  	<table width='100%' border='0' cellspacing='0' cellpadding='1' >
								  <tr>
									<th colspan='2'><table width='100%' border='0' cellspacing='0' cellpadding='0'>
									  <tr>
										<td width='77%' align='left'>Question
										  <select name='frm_lst_order' id='frm_lst_order_".$oDB->cDBLine["guid_id"]."'>"
											.$this->oFrm->forms_select_options($cOrderLst, NULL, ($oDB->iDBRowCount+1), FALSE).
										  "</select>
										  
										  </td>
										<td width='11%'><input type='submit' name='frm_btn_que_add' id='frm_btn_que_add' value='Add' /></td>
									  </tr>
									</table></th>
								  </tr>
								  <tr>
									<td><label for='frm_ta_question_val'>Question Text</label></td>
									<td><textarea name='frm_ta_question_val' id='frm_ta_question_val' cols='45' rows='5'></textarea></td>
								  </tr>
								  <tr>
									<td><label for='frm_lst_right_answer'>Correct Response</label></td>
									<td><select name='frm_lst_right_answer' id='frm_lst_right_answer' >"
									  .$this->oFrm->forms_select_options($cAnswerValLst, NULL, NULL, FALSE).
									"</select></td>
								  </tr>
								</table>
							  </form>
							</td>
						</tr>
					  </table></div>";
		
		if(!$cQuizStr)
		{
			$cQuizStr = "<h2><span class='alert'>No questions available.</span></h2>";
		}
			
		return $cQuizStr;
	}
	
	public function training_class_record($cTrainingParams)
	{		
		/*
		training_class_record_0001
		Damon Vaughn Caskey
		2013-03-27 (Converted to function class_record_0001 include)
		
		Inserts user variables into class participant database.	
		*/
		
		$cQuery		= NULL;	//Query string.
		$cParams	= NULL;	//Parameter array.
		$cClassID	= NULL;	//Class ID.
		$p_id		= NULL;	//Participant ID.
		$listing_id	= NULL;	//Class listing ID.
		
		/* Build query string. */
		$cQuery ="MERGE INTO tbl_class_participant
		USING 
			(SELECT ? AS Search_Col) AS SRC
		ON 
			tbl_class_participant.account = SRC.Search_Col
		WHEN MATCHED THEN
			UPDATE SET
				name_l				= ?,
				name_f				= ?,									
				room				= ?,
				status				= ?,
				phone				= ?,									
				department			= ?,
				supervisor_name_f	= ?,
				supervisor_name_l	= ?
		WHEN NOT MATCHED THEN
			INSERT (account, name_l, name_f, room, status, phone, department, supervisor_name_f, supervisor_name_l)
			VALUES (SRC.Search_Col, ?, ?, ?, ?, ?, ?, ?, ?)
			OUTPUT INSERTED.id_int;";		
		
		/* Apply parameters. */
		$cParams = array(&$cTrainingParams['account'],
						&$cTrainingParams['name_l'],
						&$cTrainingParams['name_f'],
						&$cTrainingParams['room'],
						&$cTrainingParams['status'],
						&$cTrainingParams['phone'],
						&$cTrainingParams['department'],
						&$cTrainingParams['supervisor_name_f'],
						&$cTrainingParams['supervisor_name_l'],
						&$cTrainingParams['name_l'],
						&$cTrainingParams['name_f'],
						&$cTrainingParams['room'],
						&$cTrainingParams['status'],
						&$cTrainingParams['phone'],
						&$cTrainingParams['department'],
						&$cTrainingParams['supervisor_name_f'],
						&$cTrainingParams['supervisor_name_l']);	
		
		/* Execute query. */	
		$this->oDB->db_basic_action($cQuery, $cParams, TRUE);
		
		/* Get ID of created/updated record. */
		$p_id = $this->oDB->cDBLine["id_int"];
		
		/* 	User demographics have now been found or inserted. Now we will deal with class type, instructor and time. */		
		$cQuery = "INSERT INTO	tbl_class
		
								(class_type,
								trainer_id,
								class_date)
					OUTPUT INSERTED.class_id
								VALUES	(?, ?, ?)";	
		
		$cParams = array(&$cTrainingParams['class'],
			&$cTrainingParams['trainer'],
			&$cTrainingParams['taken']);
						
		/* Execute query. */	
		$this->oDB->db_basic_action($cQuery, $cParams, TRUE);
		
		/* Get ID of new record. */		
		$cClassID = $this->oDB->cDBLine["class_id"];
					
		/* Insert newly created id and participant id to class listing table. */		
		$cQuery = "INSERT INTO tbl_class_listing
		
								(participant_id,
								class_id)
					OUTPUT INSERTED.id_int
								VALUES (?, ?)";	
		
		$cParams = array(&$p_id,
						&$cClassID);
						
		/* Execute query. */	
		$this->oDB->db_basic_action($cQuery, $cParams, TRUE);
		
		/* Get ID of new record. */		
		return $this->oDB->cDBLine["id_int"];			  
	}
	
	public function training_vars_get()
	{
		//foreach ($this->cClassVars as $key => $val)
		//{
		//	$this 		
		//}
	}
}



Author: Damon Caskey

Hello all, Damon Caskey here - the esteemed owner of this little slice of cyberspace. Welcome!

Leave a Reply