<?php
	/**
	 * QuestionManager.php
	 *
	 * Description	: A Simple Getter/Setter Bin Class
	 *
	 * Developed By : Suresh Shinde
	 *
	 * Developed On : 07 June, 2010
	 *
	 * Liscence 	: GPL
	 *
	 * Created On 	: 05/07/2011 10:05:19
	 *
	 * Created By 	: Rohit Soni
	 *
	 */

	/**
	 * Include BIN (Getters & Setters) Class
	 */
	require_once(dirname(__FILE__)."/Question.php");

	/**
	 * Include Exception Class
	 */
	require_once(dirname(__FILE__)."/QuestionException.php");

	/**
	 * Start of class QuestionManager
	 */
	class QuestionManager
	{
		/**
		 * Add Question
		 */
		function addQuestion ($Question)
		{
			try
			{
				$conn	= new ConnectionPool();

				$sql	=
					"INSERT INTO tcv_question
					 (
						question_id,
						test_id,
						answer_type,
						answer_type_mobile,
						answer_type_web,
						question_image,
						display_order,
						correct_answers_id,
						create_date,
						update_date,
						status,
						created_by,
						updated_by,
						creator_type_id,
						modifier_type_id
					 )
					 VALUES
					 (
						'".addslashes($Question->getQuestionId())."',
						'".addslashes($Question->getTestId())."',
						'".addslashes($Question->getAnswerType())."',
						'".addslashes($Question->getAnswerTypeMobile())."',
						'".addslashes($Question->getAnswerTypeWeb())."',
						'".addslashes($Question->getQuestionImage())."',
						'".addslashes($Question->getDisplayOrder())."',
						'".addslashes($Question->getCorrectAnswersId())."',
						'".addslashes($Question->getCreateDate())."',
						'".addslashes($Question->getUpdateDate())."',
						'".addslashes($Question->getStatus())."',
						'".addslashes($Question->getCreatedBy())."',
						'".addslashes($Question->getUpdatedBy())."',
						'".addslashes($Question->getCreatorTypeId())."',
						'".addslashes($Question->getModifierTypeId())."'
					 )
					";

				if(!$conn->db_query($sql))
				{
					throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
				}
				else
				{
					return $conn->db_last_insert_id();
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}


		/**
		 * Update Question
		 */
		function updateQuestion ($Question)
		{
			try
			{
				$conn	= new ConnectionPool();

				$sql	=
					"UPDATE tcv_question
					 SET
						test_id = '".addslashes($Question->getTestId())."',
						answer_type = '".addslashes($Question->getAnswerType())."',
						answer_type_mobile = '".addslashes($Question->getAnswerTypeMobile())."',
						answer_type_web = '".addslashes($Question->getAnswerTypeWeb())."',
						question_image = '".addslashes($Question->getQuestionImage())."',
						display_order = '".addslashes($Question->getDisplayOrder())."',
						correct_answers_id = '".addslashes($Question->getCorrectAnswersId())."',
						create_date = '".addslashes($Question->getCreateDate())."',
						update_date = '".addslashes($Question->getUpdateDate())."',
						status = '".addslashes($Question->getStatus())."',
						created_by = '".addslashes($Question->getCreatedBy())."',
						updated_by = '".addslashes($Question->getUpdatedBy())."',
						creator_type_id = '".addslashes($Question->getCreatorTypeId())."',
						modifier_type_id = '".addslashes($Question->getModifierTypeId())."'
   					 WHERE question_id = '".$Question->getQuestionId()."'
					";

				if(!$conn->db_query($sql))
				{
					throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}


		/**
		 * Delete Question
		 */
		function deleteQuestion ($QuestionId)
		{
			try
			{
				$conn	= new ConnectionPool();

				$sql	=
					"DELETE FROM tcv_question
   					 WHERE question_id = $QuestionId
					";

				if(!$conn->db_query($sql))
				{
					throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}


		/**
		 * Get Single Question
		 */
		function getSingleQuestion ($question_id)
		{
			try
			{
				$conn	= new ConnectionPool();

				$sql	=
					"SELECT *
					 FROM tcv_question
					 WHERE question_id = $question_id
					";

				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					//throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
					return array();
				}
				else
				{
					$row = $conn->db_fetch_object($result);

					$arr = new Question();

					$arr->setQuestionId(stripslashes($row->question_id));
					$arr->setTestId(stripslashes($row->test_id));
					$arr->setAnswerType(stripslashes($row->answer_type));
					$arr->setAnswerTypeMobile(stripslashes($row->answer_type_mobile));
					$arr->setAnswerTypeWeb(stripslashes($row->answer_type_web));
					$arr->setQuestionImage(stripslashes($row->question_image));
					$arr->setDisplayOrder(stripslashes($row->display_order));
					$arr->setCorrectAnswersId(stripslashes($row->correct_answers_id));
					$arr->setCreateDate(stripslashes($row->create_date));
					$arr->setUpdateDate(stripslashes($row->update_date));
					$arr->setStatus(stripslashes($row->status));
					$arr->setCreatedBy(stripslashes($row->created_by));
					$arr->setUpdatedBy(stripslashes($row->updated_by));
					$arr->setCreatorTypeId(stripslashes($row->creator_type_id));
					$arr->setModifierTypeId(stripslashes($row->modifier_type_id));

					return $arr;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}


		/**
		 * Get All Question
		 */
		function getAllQuestion ($startIndex = '',$endIndex = '',$where_clause = NULL,$sort = '' ,$ordertype = '')
		{
			try
			{
				$conn 	= new ConnectionPool();

				$sql 	=
							"	SELECT *
								FROM tcv_question
								LEFT JOIN  tcv_cms_test ON  tcv_cms_test.cms_id  = tcv_question.question_id  "
								;

				if($where_clause != NULL)
					$sql .= " WHERE ".$where_clause;
				
				//echo "<br><br><br><br>";	
				if($sort != '')
				 $sql .=  "ORDER BY ".$sort." ".$ordertype." LIMIT ".$startIndex.",".$endIndex;

				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					//throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
					return array();
				}
				else
				{
					for($count = 0; $row = $conn->db_fetch_object($result); $count ++)
					{
						$arr[$count] = new Question();

						$arr[$count]->setQuestionId(stripslashes($row->question_id));
						$arr[$count]->setTestId(stripslashes($row->test_id));
						$arr[$count]->setAnswerType(stripslashes($row->answer_type));
						$arr[$count]->setAnswerTypeMobile(stripslashes($row->answer_type_mobile));
						$arr[$count]->setAnswerTypeWeb(stripslashes($row->answer_type_web));
						$arr[$count]->setQuestionImage(stripslashes($row->question_image));
						$arr[$count]->setDisplayOrder(stripslashes($row->display_order));
						$arr[$count]->setCorrectAnswersId(stripslashes($row->correct_answers_id));
						$arr[$count]->setCreateDate(stripslashes($row->create_date));
						$arr[$count]->setUpdateDate(stripslashes($row->update_date));
						$arr[$count]->setStatus(stripslashes($row->status));
						$arr[$count]->setCreatedBy(stripslashes($row->created_by));
						$arr[$count]->setUpdatedBy(stripslashes($row->updated_by));
						$arr[$count]->setCreatorTypeId(stripslashes($row->creator_type_id));
						$arr[$count]->setModifierTypeId(stripslashes($row->modifier_type_id));
						$arr[$count]->setDescription(stripslashes($row->description));
					}

					return $arr;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}


		/**
		 * Get All Question Count
		 */
		function getAllQuestionCount ($where_clause = NULL)
		{
			try
			{
				$conn 	= new ConnectionPool();

				$sql 	=
					"SELECT count(*) AS count
					 FROM tcv_question
					 LEFT JOIN  tcv_cms_test ON  tcv_cms_test.cms_id  = tcv_question.question_id 
					";

				if($where_clause != NULL)
					$sql .= " WHERE ".$where_clause;

				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					//throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
					//return array();
					return 0;
				}
				else
				{
					$row	= $conn->db_fetch_object($result);

					return $row->count;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		
		function updateCorrecrAnsofQuestion ($correct_answers_id,$QuestionId)
		{
			try
			{
				$conn	= new ConnectionPool();

				 $sql	=
					"UPDATE tcv_question
					 SET
						correct_answers_id = ".$correct_answers_id."
					 WHERE question_id = ".$QuestionId."
					";

				if(!$conn->db_query($sql))
				{
					throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		function updateQuestionStatus ($Status,$QuestionId)
		{
			try
			{
				$conn	= new ConnectionPool();
				$updatedate		= date('Y-m-d H:i:s');
				 $sql	=
					"UPDATE tcv_question
					 SET
						 status  = '".$Status."',
						 update_date  = '".$updatedate."'
					 WHERE question_id = ".$QuestionId."
					";
				//echo  $sql;exit;
				if(!$conn->db_query($sql))
				{
					throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		
		function getAllQuestionsOrder($where_clause = NULL,$sort,$ordertype)
		{
			try
			{
				$conn 	= new ConnectionPool();

				$sql 	=
							"	SELECT *
								FROM tcv_question
								LEFT JOIN  tcv_cms_test ON  tcv_cms_test.cms_id  = tcv_question.question_id  "
								;

				if($where_clause != NULL)
					$sql .= " WHERE ".$where_clause;
				
				//echo "<br><br><br><br>";	
				 $sql .=  "ORDER BY ".$sort." ".$ordertype;

				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					//throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
					return array();
				}
				else
				{
					for($count = 0; $row = $conn->db_fetch_object($result); $count ++)
					{
						$arr[$count] = new Question();

						$arr[$count]->setQuestionId(stripslashes($row->question_id));
						$arr[$count]->setTestId(stripslashes($row->test_id));
						$arr[$count]->setAnswerType(stripslashes($row->answer_type));
						$arr[$count]->setAnswerTypeMobile(stripslashes($row->answer_type_mobile));
						$arr[$count]->setAnswerTypeWeb(stripslashes($row->answer_type_web));
						$arr[$count]->setQuestionImage(stripslashes($row->question_image));
						$arr[$count]->setDisplayOrder(stripslashes($row->display_order));
						$arr[$count]->setCorrectAnswersId(stripslashes($row->correct_answers_id));
						$arr[$count]->setCreateDate(stripslashes($row->create_date));
						$arr[$count]->setUpdateDate(stripslashes($row->update_date));
						$arr[$count]->setStatus(stripslashes($row->status));
						$arr[$count]->setCreatedBy(stripslashes($row->created_by));
						$arr[$count]->setUpdatedBy(stripslashes($row->updated_by));
						$arr[$count]->setCreatorTypeId(stripslashes($row->creator_type_id));
						$arr[$count]->setModifierTypeId(stripslashes($row->modifier_type_id));
						$arr[$count]->setDescription(stripslashes($row->description));
					}

					return $arr;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		function updateTestQuestionOrder ($TestQuestId,$order)
		{
			try
			{
				$conn	= new ConnectionPool();
				$update_date		= date('Y-m-d H:i:s',time()+86400);
				   $sql	=
					"UPDATE 
					    tcv_question
						SET tcv_question.display_order  = '$order', update_date = '$update_date'
   					 WHERE question_id  = '$TestQuestId'
					"; 

				
				if(!$conn->db_query($sql))
				{
					throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		 /**
	 * Created by : Mrunal Malusare
	 * Description : Get All Question of test
	 * Parameters : condition
	 * Get All Question
	 */
		function getTestAllQuestions ($where_clause = NULL)
		{
			try
			{
				$conn 	= new ConnectionPool();

				/*$sql 	=
							"SELECT * FROM tcv_question
							JOIN tcv_cms_test ON tcv_cms_test.cms_id = tcv_question.question_id
							AND cms_type = 'Q' AND tcv_question.status = 'A'
							";*/
				$sql 	=
							"SELECT * FROM tcv_question
							JOIN tcv_cms_test ON tcv_cms_test.cms_id = tcv_question.question_id
							AND cms_type = 'Q'
							";			

				if($where_clause != NULL)
					$sql .= $where_clause;
				//echo $sql.'<br/>';
				//echo "<br><br><br><br>";	
			//	 $sql .=  "ORDER BY ".$sort." ".$ordertype." LIMIT ".$startIndex.",".$endIndex;
				//die($sql);
				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					throw new QuestionException(mysql_errno($conn->dbLink),mysql_error($conn->dbLink));
					return array();
				}
				else
				{
					for($count = 0; $row = $conn->db_fetch_object($result); $count ++)
					{
						$arr[$count] = new Question();

						$arr[$count]->setQuestionId(stripslashes($row->question_id));
						$arr[$count]->setTestId(stripslashes($row->test_id));
						$arr[$count]->setAnswerType(stripslashes($row->answer_type));
						$arr[$count]->setAnswerTypeMobile(stripslashes($row->answer_type_mobile));
						$arr[$count]->setAnswerTypeWeb(stripslashes($row->answer_type_web));
						$arr[$count]->setQuestionImage(stripslashes($row->question_image));
						
						$arr[$count]->setDisplayOrder(stripslashes($row->display_order));
						$arr[$count]->setCorrectAnswersId(stripslashes($row->correct_answers_id));
						$arr[$count]->setCreateDate(stripslashes($row->create_date));
						$arr[$count]->setUpdateDate(stripslashes($row->update_date));
						$arr[$count]->setStatus(stripslashes($row->status));
						$arr[$count]->setCreatedBy(stripslashes($row->created_by));
						$arr[$count]->setUpdatedBy(stripslashes($row->updated_by));
						$arr[$count]->setCreatorTypeId(stripslashes($row->creator_type_id));
						$arr[$count]->setModifierTypeId(stripslashes($row->modifier_type_id));
						$arr[$count]->setDescription(stripslashes($row->description));
					}

					return $arr;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		function getTestQuestionCnt ($where_clause = NULL)
		{
			try
			{
				$conn 	= new ConnectionPool();

				$sql 	=
					"SELECT count(*) AS count
					 FROM tcv_question
					";

				if($where_clause != NULL)
					$sql .= " WHERE ".$where_clause;
				//echo $sql;exit;
				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					//throw new QuestionException(mysql_errno($conn->connection),mysql_error($conn->connection));
					//return array();
					return 0;
				}
				else
				{
					$row	= $conn->db_fetch_object($result);

					return $row->count;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}
		
		function getAllQuestionForWindowsApp ($where_clause = NULL,$test_type)
		{
			try
			{
				$conn 	= new ConnectionPool();
				/*echo $test_type;
				echo "<br>";*/
				//Image
				if($test_type == 'C' || $test_type == 'E'|| $test_type == 'F'){
				/* $sql 	=
							"SELECT * FROM tcv_question
							 INNER JOIN tcv_cms_test ON tcv_cms_test.cms_id = tcv_question.question_id
							 INNER JOIN tcv_tests_answers ON tcv_tests_answers.question_id = tcv_question.question_id
							";	*/
				 $sql 	=
				 		"
						SELECT 
									tcv_question.* ,
									tcv_tests_answers.* ,
									quedesc.description as questdescription , 
									ansdesc.description as ansdescription 
									FROM tcv_question 
									INNER JOIN tcv_cms_test AS quedesc ON quedesc.cms_id = tcv_question.question_id 
									INNER JOIN tcv_tests_answers ON tcv_tests_answers.question_id = tcv_question.question_id 
									LEFT JOIN tcv_cms_test AS ansdesc ON ansdesc.cms_id = tcv_tests_answers.test_answers_id 
						       ";			
				}
				if($test_type == 'A'){			
				//Text					
				 $sql 	=
							"SELECT tcv_question.* , tcv_tests_answers.* , quedesc.description as questdescription , ansdesc.description as ansdescription
								FROM tcv_question
								INNER JOIN tcv_cms_test AS quedesc ON quedesc.cms_id = tcv_question.question_id
								INNER JOIN tcv_tests_answers ON tcv_tests_answers.question_id = tcv_question.question_id
								INNER JOIN tcv_cms_test AS ansdesc ON ansdesc.cms_id = tcv_tests_answers.test_answers_id
							";	
					}		
					
				if($where_clause != NULL)
					   $sql .= " WHERE ".$where_clause ;
					
				$result	= $conn->db_query("SET SQL_BIG_SELECTS = 1;");	
				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					throw new QuestionException(mysql_errno($conn->dbLink),mysql_error($conn->dbLink));
					return array();
				}
				else
				{
					for($count = 0; $row = $conn->db_fetch_object($result); $count ++)
					{
						$arr[$count] = new Question();

						$arr[$count]->setQuestionId(stripslashes($row->question_id));
						$arr[$count]->setTestId(stripslashes($row->test_id));
						$arr[$count]->setAnswerType(stripslashes($row->answer_type));
						$arr[$count]->setAnswerTypeMobile(stripslashes($row->answer_type_mobile));
						$arr[$count]->setAnswerTypeWeb(stripslashes($row->answer_type_web));
						$arr[$count]->setQuestionImage(stripslashes($row->question_image));
						$arr[$count]->setDisplayOrder(stripslashes($row->display_order));
						$arr[$count]->setCorrectAnswersId(stripslashes($row->correct_answers_id));
						$arr[$count]->setCreateDate(stripslashes($row->create_date));
						$arr[$count]->setUpdateDate(stripslashes($row->update_date));
						$arr[$count]->setStatus(stripslashes($row->status));
						$arr[$count]->setCreatedBy(stripslashes($row->created_by));
						$arr[$count]->setUpdatedBy(stripslashes($row->updated_by));
						$arr[$count]->setCreatorTypeId(stripslashes($row->creator_type_id));
						$arr[$count]->setModifierTypeId(stripslashes($row->modifier_type_id));
						$arr[$count]->setImagePath(stripslashes($row->image_path));
						$arr[$count]->setIsCorrect(stripslashes($row->is_correct));
						//$arr[$count]->setDescription(stripslashes($row->description));
						$arr[$count]->setquestDescription(stripslashes($row->questdescription));
						$arr[$count]->setansDescription(stripslashes($row->ansdescription));
					}
					 
					return $arr;
					 
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}

	    function getTestAllQuestionsForMobile ($where_clause = NULL)
		{
			try
			{
				$conn 	= new ConnectionPool();

				/*$sql 	=
							"SELECT * FROM tcv_question
							JOIN tcv_cms_test ON tcv_cms_test.cms_id = tcv_question.question_id
							AND cms_type = 'Q' AND tcv_question.status = 'A'
							";*/
				$sql 	=
							"SELECT tcv_question.*, tcv_question.status as ques_status FROM tcv_question
							JOIN tcv_test ON tcv_test.test_id = tcv_question.test_id
							";			

				if($where_clause != NULL)
					 $sql .= " WHERE ".$where_clause ;
				//echo $sql.'<br/>';
				//echo "<br><br><br><br>";	
			//	 $sql .=  "ORDER BY ".$sort." ".$ordertype." LIMIT ".$startIndex.",".$endIndex;
				//die($sql);
				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					//throw new QuestionException(mysql_errno($conn->dbLink),mysql_error($conn->dbLink));
					return array();
				}
				else
				{
					for($count = 0; $row = $conn->db_fetch_object($result); $count ++)
					{
						$arr[$count] = new Question();

						$arr[$count]->setQuestionId(stripslashes($row->question_id));
						$arr[$count]->setTestId(stripslashes($row->test_id));
						$arr[$count]->setAnswerType(stripslashes($row->answer_type));
						$arr[$count]->setAnswerTypeMobile(stripslashes($row->answer_type_mobile));
						$arr[$count]->setAnswerTypeWeb(stripslashes($row->answer_type_web));
						$arr[$count]->setQuestionImage(stripslashes($row->question_image));
						
						$arr[$count]->setDisplayOrder(stripslashes($row->display_order));
						$arr[$count]->setCorrectAnswersId(stripslashes($row->correct_answers_id));
						$arr[$count]->setCreateDate(stripslashes($row->create_date));
						$arr[$count]->setUpdateDate(stripslashes($row->update_date));
						$arr[$count]->setStatus(stripslashes($row->ques_status));
						$arr[$count]->setCreatedBy(stripslashes($row->created_by));
						$arr[$count]->setUpdatedBy(stripslashes($row->updated_by));
						$arr[$count]->setCreatorTypeId(stripslashes($row->creator_type_id));
						$arr[$count]->setModifierTypeId(stripslashes($row->modifier_type_id));
						$arr[$count]->setDescription(stripslashes($row->description));
					}

					return $arr;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}

		function getAllQuestionImages( $where_clause=NULL )
		{
			try
			{
				$conn 	= new ConnectionPool();

				$sql = "SELECT DISTINCT(question_image) FROM tcv_question";			

				if($where_clause != NULL)
					 $sql .= " WHERE ".$where_clause;//. " LIMIT 0, 50" ;

				//echo $sql;	

				$result	= $conn->db_query($sql);

				if((!$result) || ($conn->db_num_rows($result) == 0))
				{
					return array();
				}
				else
				{
					for($count = 0; $row = $conn->db_fetch_object($result); $count ++)
					{
						/*$arr[$count] = new Question();

						$arr[$count]->setQuestionImage(stripslashes($row->question_image));*/
						$arr[] = $row->question_image;
						//$arr[] = array('img_name' => $row->question_image);
					
					}
					//echo "<pre>"; print_r(json_encode($arr));die;
					return $arr;
				}
			}
			catch(Exception $e)
			{
				throw $e;
			}
		}


	}
	/**
	 * End of class QuestionManager
	 */
	 
	
?>
