Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 56
CRAP
0.00% covered (danger)
0.00%
0 / 961
QuestionPaperService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 56
30450.00
0.00% covered (danger)
0.00%
0 / 961
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
 getCognitiveLevels
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 saveCognitiveLevels
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 showCognitiveTags
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 17
 saveCognitiveTags
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 20
 deleteCognitiveTags
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getCognitiveDetailsByIds
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getSectionsBySubject
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 saveSectionBySubject
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 getSectionBySectionId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 saveModuleWeightage
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 22
 getModuleWeightage
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 saveQuestionBankCreateRule
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 21
 insertQuestionBankCreateRule
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 saveFacultyRelationForQuestionPaperBank
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 19
 saveMarkCategoryRelationForQuestionPaperBank
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 35
 saveCognitiveLevelsRelationForQuestionBank
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 34
 saveModuleRelationForQuestionBank
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 34
 updateQuestionBankCreateRule
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getQuestionBank
0.00% covered (danger)
0.00%
0 / 1
110.00
0.00% covered (danger)
0.00%
0 / 45
 deleteQuestionBank
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 21
 getQuestionsOfQuestionBank
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 deleteQuestionPaperBankModule
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 deleteQuestionPaperBankMarkCategory
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 deleteQuestionPaperBankCognitiveLevel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 deleteQuestionPaperBankFacultyRelation
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 blockUnblockStaff
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 getMinQuestionAssignTypeFromQpBankId
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 28
 getModulesForQuestionPaperBank
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getMarkCategoryForQuestionBank
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getCognitiveLevelForQuestionBank
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 saveMinQuestionNo
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 31
 getQuestionPaperSubjectsForStaff
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getQuestionsCreatedByStaffForASubject
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 53
 saveQuestionCreatedByStaff
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 17
 insertQuestion
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 updateQuestion
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 10
 getDifficultyLevels
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getSectionDetailsBySectionNameAndSubject
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 createIfNotCreatedSection
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 deleteQuestionById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getNoOfQuestionsEntered
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 33
 getAllModules
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getAllMarkCategories
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getAllDifficultyLevels
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getAllCognitiveLevels
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getAllStaffsWhoCreatedQuestionsInASubject
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 16
 getEditEnableStatus
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 upsertQuestionPaperGenSectionDetails
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 validateQuestionPaperGenSectionDetails
0.00% covered (danger)
0.00%
0 / 1
182.00
0.00% covered (danger)
0.00%
0 / 53
 getModuleDetailsBySubject
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 23
 saveModuleRemarks
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 18
 searchQuestionPaperGenDetailsByRequest
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 42
 copyQuestionPaperSections
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
<?php
namespace com\linways\core\ams\professional\service\examcontroller\questionBank;
use com\linways\core\ams\professional\service\BaseService;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\mapper\examcontroller\QuestionPaperServiceMapper;
use com\linways\base\util\CommonUtil;
use com\linways\core\ams\professional\constant\examcontroller\QuestionBankCreateConstants;
use com\linways\core\ams\professional\response\examcontroller\QuestionPaperBankResponse;
use com\linways\base\util\SecurityUtils;
use com\linways\core\ams\autonomous\exception\AutonomousException;
use com\linways\core\ams\autonomous\request\examcontroller\questionPaper\QuestionPaperGenDetails;
use com\linways\core\ams\autonomous\request\examcontroller\questionPaper\QuestionPaperGenSectionDetails;
use com\linways\core\ams\autonomous\constants\examcontroller\QuestionPatternInSectionConstants;
use com\linways\core\ams\autonomous\service\examcontroller\questionPaper\QuestionPaperDetailsService;
use com\linways\core\ams\professional\service\StaffService;
class QuestionPaperService extends BaseService
{
    private static $_instance = null;
    
    // /Condition 2 - Locked down the constructor
    private function __construct() {
        $this->mapper = QuestionPaperServiceMapper::getInstance()->getMapper();
    }
    // Prevent any oustide instantiation of this class
    
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone() {
    }
    // Prevent any copy of this object
    
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance() {
        if (! is_object ( self::$_instance )) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self ();
            
            return self::$_instance;
    }
    /**
     * view cognitive domain
     * @author Aswin
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getCognitiveLevels()
    {
        $query="select * from question_paper_cognitive_levels";
        
        try{
            $response=$this->executeQueryForList($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * update cognitive domain
     * @author Aswin
     * @param unknown $id
     * @param unknown $levelName
     * @throws ProfessionalException
     * @return string
     */
    public function saveCognitiveLevels($id,$levelName)
    {
        $id=$this->realEscapeString($id);
        $levelName=$this->realEscapeString($levelName);
        
        $query="update question_paper_cognitive_levels set levelName='$levelName' where id='$id'";
        
        try{
            $response=$this->executeQueryForList($query);
            return "saved";
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * show cognitive tags
     * @author Aswin
     * @param unknown $cognitiveLevelId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function showCognitiveTags($cognitiveLevelId)
    {
        $cognitiveLevelId=$this->realEscapeString($cognitiveLevelId);
        
        $query="select * from question_paper_cognitive_levels_tags where question_paper_cognitive_levels_id = '$cognitiveLevelId'";
        
        try{
            $response=$this->executeQueryForList($query);
            if($response)
            {
                return $response;
            }
            else {
                throw new ProfessionalException(ProfessionalException::ARRAY_EMPTY,"No Cognitive Level Tags Found");
            }
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * save cognitive tags
     * @author Aswin
     * @param unknown $cognitiveLevelId
     * @param unknown $tagName
     * @param unknown $id
     * @throws ProfessionalException
     * @return string
     */
    public function saveCognitiveTags($cognitiveLevelId,$tagName,$id=null)
    {
        $cognitiveLevelId=$this->realEscapeString($cognitiveLevelId);
        $tagName=$this->realEscapeString($tagName);
        $id=$this->realEscapeString($id);
        
        if(!$id)
        {
            $query="insert into question_paper_cognitive_levels_tags (tagName,question_paper_cognitive_levels_id) values ('$tagName',$cognitiveLevelId);";
        }
        else
        {
            $query="update question_paper_cognitive_levels_tags set tagName='$tagName',question_paper_cognitive_levels_id=$cognitiveLevelId where id=$id";
        }
        
        try{
            $response=$this->executeQueryForList($query);
            return "saved";
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * delete cognitive tags
     * @author Aswin
     * @param unknown $id
     * @throws ProfessionalException
     * @return string
     */
    public function deleteCognitiveTags($id)
    {
        $id=$this->realEscapeString($id);
        
        $query="delete from question_paper_cognitive_levels_tags where id=$id";
        
        try{
            $response=$this->executeQueryForList($query);
            return "deleted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * get cognitive details by ids
     * @author Aswin
     * @param unknown $cognitiveIds
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getCognitiveDetailsByIds($cognitiveIds)
    {
        $cognitiveIds=$this->realEscapeString($cognitiveIds);
        
        $query="select * from question_paper_cognitive_levels where id in ($cognitiveIds)";
        
        try{
            $response=$this->executeQueryForList($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * get section details by subject
     * @author Aswin
     * @param unknown $subjectID
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getSectionsBySubject($subjectID)
    {
        $subjectID=$this->realEscapeString($subjectID);
        
        $query="SELECT * FROM question_paper_sections WHERE subjectID=\"$subjectID\"";
        
        try{
            
            $response=$this->executeQueryForList($query);
            return $response;
        
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * save section details
     * @author Aswin
     * @param unknown $sectionName
     * @param unknown $subjectID
     * @param unknown $staffID
     * @throws ProfessionalException
     * @return unknown
     */
    public function saveSectionBySubject($sectionName,$subjectID,$staffID)
    {
        $sectionName=$this->realEscapeString($sectionName);
        $subjectID=$this->realEscapeString($subjectID);
        $staffID=$this->realEscapeString($staffID);
        
        $query="INSERT INTO question_paper_sections (sectionName,subjectID,createdBy,createdDate) values ('$sectionName',$subjectID,$staffID,'".date("Y-m-d H:i:s")."')";
        
        try{
            $response=$this->executeQueryForObject($query,TRUE);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    
    
    /**
     * get section by section id
     * @author Aswin
     * @param unknown $sectionId
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getSectionBySectionId($sectionId)
    {
        $sectionId=$this->realEscapeString($sectionId);
        
        $query="SELECT * FROM question_paper_sections WHERE id=\"$sectionId\"";
        
        try{
            $response=$this->executeQueryForObject($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * save module weightage
     * @author Aswin
     * @param unknown $weightage
     * @param unknown $moduleID
     * @param unknown $subjectID
     * @param unknown $staffID
     * @throws ProfessionalException
     * @return string
     */
    public function saveModuleWeightage($weightage,$moduleID,$subjectID,$staffID)
    {
        $weightage=$this->realEscapeString($weightage);
        $moduleID=$this->realEscapeString($moduleID);
        $subjectID=$this->realEscapeString($subjectID);
        $staffID=$this->realEscapeString($staffID);
        try{
            $response=$this->getModuleWeightage($moduleID, $subjectID, $staffID);
            if($response)
            {
                $query="UPDATE question_paper_module_weightages SET weightage=\"$weightage\" WHERE question_paper_module_id=$moduleID and subjectID=$subjectID and staffID=$staffID";
            }
            else
            {
                $query="INSERT INTO question_paper_module_weightages (weightage,question_paper_module_id,subjectID,staffID) VALUES (\"$weightage\",$moduleID,$subjectID,$staffID)";
            }
            
            $response=$this->executeQueryForObject($query,TRUE);
            return "inserted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,$e->getMessage());
        }
        
        
    }
    /**
     * get module weightage
     * @author Aswin
     * @param unknown $moduleID
     * @param unknown $subjectID
     * @param unknown $staffID
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getModuleWeightage($moduleID,$subjectID,$staffID)
    {
        $moduleID=$this->realEscapeString($moduleID);
        $subjectID=$this->realEscapeString($subjectID);
        $staffID=$this->realEscapeString($staffID);
        
        $query="SELECT * FROM question_paper_module_weightages WHERE question_paper_module_id=\"$moduleID\" and subjectID=\"$subjectID\" and staffID=\"$staffID\"";
        
        try{
            $response=$this->executeQueryForObject($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,$e->getMessage());
        }
        
        
    }
    
    
    
    /*****************Question bank creation rule services**************************/
    
    /**
     * save question bank 
     * @author Aswin
     * @param unknown $questionBank
     * @throws ProfessionalException
     * @return object|string
     */
    public function saveQuestionBankCreateRule($questionBank)
    {
        $questionBank=$this->realEscapeObject($questionBank);
        
        try{
            if(!$questionBank->qpBankId)
            {
                $questionBank->qpBankId = $this->insertQuestionBankCreateRule($questionBank);
            }
            else 
            {
                $response = $this->updateQuestionBankCreateRule($questionBank);
            }
            $response = $this->saveModuleRelationForQuestionBank($questionBank);
            $response = $this->saveMarkCategoryRelationForQuestionPaperBank($questionBank);
            $response = $this->saveCognitiveLevelsRelationForQuestionBank($questionBank);
            $response = $this->saveFacultyRelationForQuestionPaperBank($questionBank);
            return $questionBank;            
            
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Saving Question Bank Assign Details Failed!Please Contact Administrator");
        }
    }
    
    
    public function insertQuestionBankCreateRule($questionBank)
    {
        $questionBank =  $this->realEscapeObject($questionBank);
        
        $query="insert into question_paper_bank (subjectId,batchId,semId,startDate,endDate,min_questions_type,total_min_questions)
 values ($questionBank->subjectId,$questionBank->batchId,$questionBank->semId,'$questionBank->startDate','$questionBank->endDate','$questionBank->minQuestionType',$questionBank->minQuestions)";
        
        try{
            
            $response = $this->executeQueryForObject($query,true);
            return $response;
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Inserting of Question Bank Assign Details Failed!Please Contact Administrator");
        }
    }
    
    public function saveFacultyRelationForQuestionPaperBank($questionBank)
    {
        $questionBank = $this->realEscapeObject($questionBank);
        
        try{
            
        $query = "delete from question_paper_bank_faculty where question_paper_bank_id = '$questionBank->qpBankId'";
        
        $response = $this->executeQuery($query);
        
        $faculties = explode(",", $questionBank->staffId);
        $query = "insert into question_paper_bank_faculty (question_paper_bank_id,staffId,isBlock) values";
        foreach ($faculties as $f)
        {
            $query.=" ('$questionBank->qpBankId','$f','0'),";
        }
        $query = rtrim($query,",");
        
        $response = $this->executeQueryForObject($query,true);
        return $response;
        
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Saving Faculty For Question Bank Failed");
        }
    }
    
    
    public function saveMarkCategoryRelationForQuestionPaperBank($questionBank)
    {
        $questionBank = $this->realEscapeObject($questionBank);
        
        try{
            if($questionBank->qpMarkCategoryId){
                $query = "delete from question_paper_bank_mark_categories where question_paper_bank_id = '$questionBank->qpBankId' AND question_paper_mark_category_id NOT IN (".$questionBank->qpMarkCategoryId.")";
            }else{
                $query = "delete from question_paper_bank_mark_categories where question_paper_bank_id = '$questionBank->qpBankId'";
            }
            
            $response = $this->executeQuery($query);
            
            $markCategory = explode(",", $questionBank->qpMarkCategoryId);
            $query = "";
            foreach ($markCategory as $m)
            {
                $query = "";
                $query .= "insert into question_paper_bank_mark_categories (question_paper_mark_category_id,question_paper_bank_id) values ";
                $query.="('$m','$questionBank->qpBankId')";
                if($questionBank->minQuestionType = 'ALL'){
                    $query.= " ON DUPLICATE KEY UPDATE min_questions = NULL";
                }
                try {
                    $this->executeQuery($query);
                } catch (\Exception $e) {
                    if($e->getCode() == ProfessionalException::DUPLICATE_ENTRY){
                        continue;//ignore duplicate entry exception
                    }else{
                        throw new ProfessionalException($e->getMessage(),$e->getCode());
                    }
                }
                
            }
                //             $query = rtrim($query,",");
            
            return true;
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Saving Mark Category For Question Bank Failed");
        }
    }
    
    public function  saveCognitiveLevelsRelationForQuestionBank($questionBank)
    {
        $questionBank = $this->realEscapeObject($questionBank);
        
        try{
            if($questionBank->qpCognitiveLevelId){
                $query = "delete from question_paper_bank_cognitive_levels where question_paper_bank_id = '$questionBank->qpBankId' AND question_paper_cognitive_levels_id NOT IN (".$questionBank->qpCognitiveLevelId.")";
            }else{
                $query = "delete from question_paper_bank_cognitive_levels where question_paper_bank_id = '$questionBank->qpBankId'";
            }
            $respone = $this->executeQuery($query);
            
            $cognitiveLevels = explode(",", $questionBank->qpCognitiveLevelId);
            
            $query = "";
            foreach ($cognitiveLevels as $c)
            {
                $query = "";
                $query = "insert into question_paper_bank_cognitive_levels (question_paper_cognitive_levels_id,question_paper_bank_id) values ";
                $query.="('$c','$questionBank->qpBankId')";
                if($questionBank->minQuestionType = 'ALL'){
                    $query.= " ON DUPLICATE KEY UPDATE min_questions = NULL";
                }
                try {
                    $this->executeQuery($query);
                } catch (\Exception $e) {
                    if($e->getCode() == ProfessionalException::DUPLICATE_ENTRY){
                        //ignore duplicate entry exception
                    }else{
                        throw new ProfessionalException($e->getMessage(),$e->getCode());
                    }
                }
            }
        
            
            return true;
            
        }catch (\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Saving Cognitive Levels For Question Bank Failed");
        }
    }
    
    public function saveModuleRelationForQuestionBank($questionBank)
    {
        $questionBank = $this->realEscapeObject($questionBank);
        
        try{
            
            if($questionBank->qpModuleId){
                $query = "delete from question_paper_bank_modules where question_paper_bank_id = '$questionBank->qpBankId' AND question_paper_module_id NOT IN (".$questionBank->qpModuleId.")";
            }else{
                $query = "delete from question_paper_bank_modules where question_paper_bank_id = '$questionBank->qpBankId'";
            }
            
            $response = $this->executeQuery($query);
            
            $modules = explode(",", $questionBank->qpModuleId);
           
            $query = "";
            foreach ($modules as $m)
            {
                $query = "";
                $query = "insert into question_paper_bank_modules (question_paper_module_id,question_paper_bank_id) values ";
                $query.="('$m','$questionBank->qpBankId')";
                if($questionBank->minQuestionType = 'ALL'){
                    $query.= " ON DUPLICATE KEY UPDATE min_questions = NULL";
                }
                try {
                    $this->executeQuery($query);
                } catch (\Exception $e) {
                    if($e->getCode() == ProfessionalException::DUPLICATE_ENTRY){
                        //ignore duplicate entry exception
                    }else{
                        throw new ProfessionalException($e->getMessage(),$e->getCode());
                    }
                }
            }
            return true;
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Saving Module For Question Bank Failed");
        }
    }
    
    public function updateQuestionBankCreateRule($questionBank)
    {
        $questionBank = $this->realEscapeObject($questionBank);
        
        $query = "update question_paper_bank set subjectId = '$questionBank->subjectId',batchId = '$questionBank->batchId',semId = '$questionBank->semId',startDate = '$questionBank->startDate',endDate = '$questionBank->endDate',min_questions_type = '$questionBank->minQuestionType',total_min_questions = '$questionBank->minQuestions' where id = '$questionBank->qpBankId'";
        
        try{
            $response = $this->executeQueryForObject($query);
            return "updated";
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get question bank
     * @author Aswin
     * @param unknown $batchId
     * @param unknown $semId
     * @param unknown $staffId
     * @param unknown $subjectId
     * @param unknown $startDate
     * @param unknown $endDate
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getQuestionBank($batchId=null,$semId=null,$staffId=null,$subjectId=null,$startDate=null,$endDate=null,$moduleId = null,$markCategoryId=null)
    {
        $batchId=$this->realEscapeString($batchId);
        $semId=$this->realEscapeString($semId);
        $staffId=$this->realEscapeString($staffId);
        $subjectId=$this->realEscapeString($subjectId);
        $startDate=$this->realEscapeString($startDate);
        $endDate=$this->realEscapeString($endDate);
        $moduleId=$this->realEscapeString($moduleId);
        $markCategoryId=$this->realEscapeString($markCategoryId);
        
        $query="select qpb.id as qpBankId,qpb.subjectId,qpb.batchId,qpb.semId,qpb.startDate,qpb.endDate,qpb.min_questions_type as minQuestionType,qpb.total_min_questions as minQuestions,qpbm.question_paper_module_id as qpModuleId,qpbm.min_questions as qpModuleMinQuestions,qpbmc.question_paper_mark_category_id as qpMarkCategoryId,qpbmc.min_questions as qpMarkCategoryMinQuestions,qpbcl.question_paper_cognitive_levels_id as qpCognitiveLevelId,qpbcl.min_questions as qpCognitiveLevelMinQuestions,qpbf.staffId,qpbf.isBlock as isBlocked,qpm.name as qpModuleName,qpmc.name as qpMarkCategoryName,qpcl.levelName as qpCognitiveLevelName,sa.staffName,s.subjectName,s.subjectDesc from question_paper_bank qpb inner join question_paper_bank_modules qpbm on qpb.id = qpbm.question_paper_bank_id inner join question_paper_bank_mark_categories qpbmc on qpb.id = qpbmc.question_paper_bank_id inner join question_paper_bank_cognitive_levels qpbcl on qpb.id = qpbcl.question_paper_bank_id inner join question_paper_bank_faculty qpbf on qpb.id = qpbf.question_paper_bank_id inner join subjects s on qpb.subjectId = s.subjectId inner join question_paper_module qpm on qpbm.question_paper_module_id=qpm.id inner join question_paper_mark_category qpmc on qpbmc.question_paper_mark_category_id = qpmc.id inner join question_paper_cognitive_levels qpcl on qpbcl.question_paper_cognitive_levels_id = qpcl.id inner join staffaccounts sa on qpbf.staffId = sa.staffID where 1=1";
        
        if($batchId && $semId)
        {
            $query.=" and qpb.batchId = '$batchId' and qpb.semId = '$semId'";
        }
        if($staffId)
        {
            $query.=" and qpbf.staffId='$staffId'";
        }
        if($subjectId)
        {
            $query.=" and qpb.subjectId='$subjectId'";
        }
        if($startDate)
        {
            $query.=" and qpb.startDate >= '$startDate'";
        }
        if($endDate)
        {
            $query.=" and qpb.endDate <='$endDate'";
        }
        if($moduleId){
            $query .=" AND qpbm.question_paper_module_id = $moduleId";
        }
        if($markCategoryId){
            $query .=" AND qpbmc.question_paper_mark_category_id = $markCategoryId";
        }
        $query .= " order by qpm.no ASC ,qpmc.mark ASC ,qpcl.priority DESC";
        try{
            $response = $this->executeQueryForList($query,$this->mapper[QuestionPaperServiceMapper::GET_QUESTION_BANK_DETAILS]);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Question Bank Not Found");
        }
    }
    
    /**
     * delete question bank 
     * @author Aswin
     * @param unknown $questionBankId
     * @throws ProfessionalException
     * @return boolean|string
     */
    public function deleteQuestionBank($questionBankId)
    {
        $questionBankId = $this->realEscapeString($questionBankId);
        
        try{
            $isQuestionBankInUse = $this->getQuestionsOfQuestionBank($questionBankId);
            if($isQuestionBankInUse)
            {
                throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Cannot Delete Question Bank..Question Bank Already in Use");
                return false;
            }
            
            $response = $this->deleteQuestionPaperBankModule($questionBankId);
            $response = $this->deleteQuestionPaperBankMarkCategory($questionBankId);
            $response = $this->deleteQuestionPaperBankCognitiveLevel($questionBankId);
            $response = $this->deleteQuestionPaperBankFacultyRelation($questionBankId);
            $query = "delete from question_paper_bank where id = '$questionBankId'";
            $response = $this->executeQuery($query);
            return "deleted";
            
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Cannot Delete Question Bank..Question Bank Already in Use");
        }
    }
    
    /**
     * get questions  in a question paper bank
     * @author Aswin
     * @param unknown $questionBankId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getQuestionsOfQuestionBank($questionBankId)
    {
        $questionBankId = $this->realEscapeString($questionBankId);
        
        $query = "select * from question_paper_pool where question_paper_bank_id = '$questionBankId'";
        
        try{
            
            $response = $this->executeQueryForList($query);
            return $response;
            
        }catch (\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"No questions found for question bank");
        }
    }
    
    /**
     * delete module from question paper bank
     * @author Aswin
     * @param unknown $questionBankId
     * @throws ProfessionalException
     * @return string
     */
    public function deleteQuestionPaperBankModule($questionBankId)
    {
        $questionBankId = $this->realEscapeString($questionBankId);
        
        $query = "delete from question_paper_bank_modules where question_paper_bank_id = '$questionBankId'";
        
        try{
            $response = $this->executeQuery($query);
            return "deleted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Modules Cannot Be Deleted");
        }
    }
    
    /**
     * delete mark categories from question bank
     * @author Aswin
     * @param unknown $questionBankId
     * @throws ProfessionalException
     * @return string
     */
    public function deleteQuestionPaperBankMarkCategory($questionBankId)
    {
        $questionBankId = $this->realEscapeString($questionBankId);
        
        $query = "delete from question_paper_bank_mark_categories where question_paper_bank_id = '$questionBankId'";
        
        try{
            $response = $this->executeQuery($query);
            return "deleted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Modules Cannot Be Deleted");
        }
    }
    
    /**
     * delete cognitive levels from question paper bank
     * @author Aswin
     * @param unknown $questionBankId
     * @throws ProfessionalException
     * @return string
     */
    public function deleteQuestionPaperBankCognitiveLevel($questionBankId)
    {
        $questionBankId = $this->realEscapeString($questionBankId);
        
        $query = "delete from question_paper_bank_cognitive_levels where question_paper_bank_id = '$questionBankId'";
        
        try{
            $response = $this->executeQuery($query);
            return "deleted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Modules Cannot Be Deleted");
        }
    }
    
    /**
     * delete faculty from question paper bank
     * @author Aswin
     * @param unknown $questionBankId
     * @throws ProfessionalException
     * @return string
     */
    public function deleteQuestionPaperBankFacultyRelation($questionBankId)
    {
        $questionBankId = $this->realEscapeString($questionBankId);
        $query = "delete from question_paper_bank_faculty where question_paper_bank_id = '$questionBankId'";
        
        try{
            $response = $this->executeQuery($query);
            return "deleted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED,"Modules Cannot Be Deleted");
        }        
        
    }
    
    /**
     * block and unblock staff from question paper creation
     * @author Aswin
     * @param unknown $staffId
     * @param unknown $qpBankId
     * @throws ProfessionalException
     * @return string
     */
    public function blockUnblockStaff($staffId,$qpBankId)
    {
        $staffId = $this->realEscapeString($staffId);
        $qpBankId = $this->realEscapeString($qpBankId);
        
        $query = "update question_paper_bank_faculty set isBlock = IF(isBlock = 0, 1, 0) where question_paper_bank_id = '$qpBankId' and staffId = '$staffId'";
        
        try{
            $response = $this->executeQueryForObject($query);
        }catch (\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        return "blocked";
    }
    
    /**
     * get minimum questions assign type and min questions 
     * @author Aswin
     * @param unknown $qpBankId
     * @throws ProfessionalException
     * @return \com\linways\core\ams\autonomous\response\examcontroller\QuestionPaperBankResponse
     */
    public function getMinQuestionAssignTypeFromQpBankId($qpBankId)
    {
        $qpBankId = $this->realEscapeString($qpBankId);
        
        $query = "select min_questions_type as minQuestionType,total_min_questions as minQuestions from question_paper_bank where id = '$qpBankId'";
        
        try{
            
            $response=$this->executeQueryForObject($query);
            if($response->minQuestionType === QuestionBankCreateConstants::MODULE)
            {
                $response1 = $this->getModulesForQuestionPaperBank($qpBankId);
            }
            else if($response->minQuestionType === QuestionBankCreateConstants::MARK_CATEGORY)
            {
                $response1 = $this->getMarkCategoryForQuestionBank($qpBankId);
            }
            else if($response->minQuestionType === QuestionBankCreateConstants::COGNITIVE_LEVEL)
            {
                $response1 = $this->getCognitiveLevelForQuestionBank($qpBankId);
            }
            
            $questionPaperBankResponse = new QuestionPaperBankResponse();
            $questionPaperBankResponse->minQuestionType = $response->minQuestionType;
            $questionPaperBankResponse->minQuestions = $response->minQuestions;
            $questionPaperBankResponse->qpBankId = $qpBankId;
            $questionPaperBankResponse->minQuestionApplyArray = $response1;
            
            return $questionPaperBankResponse;           
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
    }
    
    /**
     * get modules for question bank
     * @author Aswin
     * @param unknown $qpBankId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getModulesForQuestionPaperBank($qpBankId)
    {
        $qpBankId = $this->realEscapeString($qpBankId);
        
        $query = "select qpbm.id as qpbModuleId,qpbm.question_paper_module_id as qpModuleId,qpbm.question_paper_bank_id as qpBankId,qpbm.min_questions as qpModuleMinQuestions,qpm.name as qpModuleName from question_paper_bank_modules qpbm inner join question_paper_module qpm on qpbm.question_paper_module_id = qpm.id where qpbm.question_paper_bank_id = '$qpBankId'";
        
        try{
            
            $response = $this->executeQueryForList($query);
            return $response;
        
        }catch (\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get mark categories for question bank
     * @author Aswin
     * @param unknown $qpBankId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getMarkCategoryForQuestionBank($qpBankId)
    {
        $qpBankId = $this->realEscapeString($qpBankId);
        
        $query = "select qpbmc.id as qpbMarkCategoryId,qpbmc.question_paper_mark_category_id as qpMarkCategoryId,qpbmc.question_paper_bank_id as qpBankId,qpbmc.min_questions as qpMarkCategoryMinQuestions,qpmc.name as qpMarkCategoryName from question_paper_bank_mark_categories qpbmc inner join question_paper_mark_category qpmc on qpbmc.question_paper_mark_category_id = qpmc.id where qpbmc.question_paper_bank_id = '$qpBankId'";
        
        try{
            $response = $this->executeQueryForList($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get cognitive level assigned for question bank
     * @author Aswin
     * @param unknown $qpBankId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getCognitiveLevelForQuestionBank($qpBankId)
    {
        $qpBankId = $this->realEscapeString($qpBankId);
        
        $query = "select qpbcl.id as qpbCognitiveLevelId,qpbcl.question_paper_cognitive_levels_id as qpCognitiveLEvelId,qpbcl.question_paper_bank_id as qpBankId,qpbcl.min_questions as qpCognitiveLevelMinQuestions,qpcl.levelName as qpCognitiveLevelName from question_paper_bank_cognitive_levels qpbcl inner join question_paper_cognitive_levels qpcl on qpbcl.question_paper_cognitive_levels_id = qpcl.id where qpbcl.question_paper_bank_id = '$qpBankId'";
        
        try{
            
            $response = $this->executeQueryForList($query);
            return $response;
            
        }catch (\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * save minimum question numbers for question type
     * @author Aswin
     * @param unknown $minQustionNos
     * @param unknown $minQuestionType
     * @throws ProfessionalException
     * @return string
     */
    public function saveMinQuestionNo($minQustionNos,$minQuestionType)
    {
        $minQuestionType = $this->realEscapeString($minQuestionType);
        $minQustionNos = $this->realEscapeArray($minQustionNos);
        
        try{
            foreach ($minQustionNos as $id => $minNo)
            {
                if($minQuestionType === QuestionBankCreateConstants::COGNITIVE_LEVEL)
                {
                    $query = "update question_paper_bank_cognitive_levels set min_questions = $minNo where id =$id";
                }
                else if ($minQuestionType  === QuestionBankCreateConstants::MARK_CATEGORY)
                {
                    $query = "update question_paper_bank_mark_categories set min_questions = $minNo where id =$id";
                }
                else if ($minQuestionType === QuestionBankCreateConstants::MODULE)
                {
                    $query = "update question_paper_bank_modules set min_questions = $minNo where id =$id";
                }
                
                try{
                    $response = $this->executeQueryForObject($query);
                }catch(\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(),$e->getMessage());
                }
            }
            return "min question no updated";
            
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get subjects assigned to staff for question creation
     * @author Aswin
     * @param unknown $staffId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getQuestionPaperSubjectsForStaff($staffId)
    {
        $staffId=$this->realEscapeString($staffId);
        
        $query = "select qpb.id as qpBankId,qpb.subjectId,qpb.batchId,qpb.semId,qpb.startDate,qpb.endDate,qpbf.staffId,qpbf.isBlock,s.subjectName,s.subjectDesc,b.batchName,sem.semName from question_paper_bank qpb inner join question_paper_bank_faculty qpbf on qpb.id = qpbf.question_paper_bank_id inner join subjects s on s.subjectID=qpb.subjectId inner join batches b on qpb.batchId=b.batchID inner join semesters sem on qpb.semId=sem.semID where qpbf.staffId=$staffId and qpbf.isBlock=0 and '".date("Y-m-d")."' between qpb.startDate and qpb.endDate";
        
        try{
            $response = $this->executeQueryForList($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get questions created by staff for a subject
     * @author Aswin
     * @param unknown $qpBankId
     * @param unknown $staffId
     * @param unknown $batchId
     * @param unknown $semId
     * @param unknown $subjectId
     * @param unknown $qpModuleId
     * @param unknown $qpMarkCategoryId
     * @param unknown $qpCognitiveLevelId
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getQuestionsCreatedByStaffForASubject($qpBankId,$staffId,$batchId,$semId,$subjectId,$qpModuleId,$qpMarkCategoryId,$qpCognitiveLevelId)
    {
        $qpBankId=$this->realEscapeString($qpBankId);
        $staffId=$this->realEscapeString($staffId);
        $batchId=$this->realEscapeString($batchId);
        $semId=$this->realEscapeString($semId);
        $subjectId=$this->realEscapeString($subjectId);
        $qpModuleId=$this->realEscapeString($qpModuleId);
        $qpMarkCategoryId=$this->realEscapeString($qpMarkCategoryId);
        $qpCognitiveLevelId=$this->realEscapeString($qpCognitiveLevelId);
        
        $query="SELECT 
        qpp.id AS questionId,
        qpp.question_paper_bank_id AS qpBankId,
        qpp.staffID AS staffId,
        qpp.batchID AS batchId,
        qpp.semID AS semId,
        qpp.subjectID AS subjectId,
        qpp.question_paper_moduleID AS qpModuleId,
        qpp.question_paper_mark_categoryID AS qpMarkCategoryId,
        qpp.isBlocked,
        qpp.question,
        qpp.att_path,
        qpp.question_paper_difficulty_levelID AS qpDifficultyLevelId,
        qpp.cognitiveLevels AS qpCognitiveLevelId,
        qpp.question_paper_sections_id AS qpSectionId,
        qpp.placeholder,
        qps.sectionName,
        qpp.enableEdit
    FROM
        question_paper_pool qpp
            LEFT JOIN
        question_paper_sections qps ON qpp.question_paper_sections_id = qps.id
    WHERE
        qpp.question_paper_bank_id = '$qpBankId'
            AND qpp.staffID = '$staffId'
            AND qpp.batchID = '$batchId'
            AND qpp.semID = '$semId'
            AND qpp.subjectID = '$subjectId'
            AND qpp.question_paper_moduleID = '$qpModuleId'
            AND qpp.question_paper_mark_categoryID = '$qpMarkCategoryId'
            AND qpp.isblocked = '0'
            AND qpp.cognitiveLevels = '$qpCognitiveLevelId';";
        
        try{
            $response=$this->executeQueryForList($query);
            foreach ($response as $r)
            {
                $r->question=SecurityUtils::prepareSafeHTML($r->question);
            }
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * save question
     * @author Aswin
     * @param unknown $question
     * @throws ProfessionalException
     * @return unknown
     */
    public function saveQuestionCreatedByStaff($question)
    {
        $question=$this->realEscapeObject($question);
        try{
            if($question->questionId)
            {
                $response=$this->updateQuestion($question);
            }
            else
            {
                $response=$this->insertQuestion($question);
            }
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * insert question
     * @author Aswin
     * @param unknown $question
     * @throws ProfessionalException
     * @return string
     */
    public function insertQuestion($question)
    {
        $query="insert into question_paper_pool (question_paper_bank_id,staffID,batchID,semID,subjectID,question_paper_moduleID,question_paper_mark_categoryID,question,att_path,question_paper_difficulty_levelID,cognitiveLevels,question_paper_sections_id,placeholder) values ('$question->qpBankId','$question->staffId','$question->batchId','$question->semId','$question->subjectId','$question->qpModuleId','$question->qpMarkCategoryId',\"$question->question\",'$question->att_path','$question->qpDifficultyLevelId','$question->qpCognitiveLevelId','$question->qpSectionId','$question->placeholder')";
        
        try{
            $response=$this->executeQueryForObject($query);
            return "inserted";
        }catch (\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * update question
     * @author Aswin
     * @param unknown $question
     * @throws ProfessionalException
     * @return string
     */
    public function updateQuestion($question)
    {
        $query="update question_paper_pool set question_paper_moduleID='$question->qpModuleId',question_paper_mark_categoryID='$question->qpMarkCategoryId',question=\"$question->question\",".($question->att_path?"att_path=\"$question->att_path\",":"")."question_paper_difficulty_levelID='$question->qpDifficultyLevelId',cognitiveLevels='$question->qpCognitiveLevelId',question_paper_sections_id='$question->qpSectionId',placeholder=\"$question->placeholder\" where id=$question->questionId";
        
        try{
            $response=$this->executeQueryForObject($query);
            return "updated";
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        
    }
    
    /**
     * get difficulty levels
     * @author Aswin
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getDifficultyLevels()
    {
        $query = "SELECT id, name FROM question_paper_difficulty_level";
        
        try{
            $response = $this->executeQueryForList($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get section details by section name and subject
     * @author Aswin
     * @param unknown $sectionName
     * @param unknown $subjectId
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getSectionDetailsBySectionNameAndSubject($sectionName,$subjectId)
    {
        $sectionName=$this->realEscapeString($sectionName);
        $subjectId=$this->realEscapeString($subjectId);
        
        $query="select * from question_paper_sections where sectionName=\"$sectionName\" and subjectID=\"$subjectId\"";
        
        try{
            $response=$this->executeQueryForObject($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        
    }
    
    /**
     * create if subject section is created else return sectionid
     * @author Aswin
     * @param unknown $sectionName
     * @param unknown $subjectId
     * @param unknown $staffId
     * @return unknown
     */
    public function createIfNotCreatedSection($sectionName,$subjectId,$staffId)
    {
        $sectionName=$this->realEscapeString($sectionName);
        $subjectId=$this->realEscapeString($subjectId);
        $staffId=$this->realEscapeString($staffId);
        
        $section=$this->getSectionDetailsBySectionNameAndSubject($sectionName, $subjectId);
        
        if(!$section)
        {
            $section->id=$this->saveSectionBySubject($sectionName, $subjectId, $staffId);
        }
        
        return $section->id;
    }
    
    /**
     * delete question
     * @author Aswin
     * @param unknown $questionId
     * @throws ProfessionalException
     * @return string
     */
    public function deleteQuestionById($questionId)
    {
        $questionId=$this->realEscapeString($questionId);
        
        $query="delete from question_paper_pool where id='$questionId'";
        
        try{
            $response=$this->executeQuery($query);
            return "deleted";
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    /**
     * get count of questions entered by staff
     * @author Aswin
     * @param unknown $qpBankId
     * @param unknown $staffId
     * @param unknown $subjectId
     * @param unknown $batchId
     * @param unknown $semId
     * @param unknown $qpModuleId
     * @param unknown $qpMarkCategoryId
     * @param unknown $qpCognitiveLevelId
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getNoOfQuestionsEntered($qpBankId,$staffId,$subjectId,$batchId,$semId,$qpModuleId=null,$qpMarkCategoryId=null,$qpCognitiveLevelId=null)
    {
        $qpBankId=$this->realEscapeString($qpBankId);
        $staffId=$this->realEscapeString($staffId);
        $subjectId=$this->realEscapeString($subjectId);
        $batchId=$this->realEscapeString($batchId);
        $semId=$this->realEscapeString($semId);
        $qpModuleId=$this->realEscapeString($qpModuleId);
        $qpMarkCategoryId=$this->realEscapeString($qpMarkCategoryId);
        $qpCognitiveLevelId=$this->realEscapeString($qpCognitiveLevelId);
        
        $query="select count(*) as questionCount from question_paper_pool where question_paper_bank_id='$qpBankId'  and batchID='$batchId' and semID='$semId' and subjectID='$subjectId'";
        
        if($staffId){
            $query .= " and staffID='$staffId'";
        }
        if($qpModuleId)
        {
            $query.=" and question_paper_moduleID='$qpModuleId'";
        }
        if($qpMarkCategoryId)
        {
            $query.=" and question_paper_mark_categoryID='$qpMarkCategoryId'";
        }
        if($qpCognitiveLevelId)
        {
            $query.=" and cognitiveLevels='$qpCognitiveLevelId'";
        }
        
        
        try{
            $response=$this->executeQueryForObject($query);
            return $response;
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    
    /**
     * 
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getAllModules() {
        $sql = "";
        $sql = "SELECT id, name, no as number, description from question_paper_module";
        $moduleList = [];
        try{
            $moduleList = $this->executeQueryForList($sql);
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        return $moduleList;
    }
    
    /**
     * 
     * @throws ProfessionalException
     * @return object|array|\com\linways\base\util\$objectList[]
     */
    public function getAllMarkCategories() {
        $sql = "";
        $sql = "SELECT id, name, mark, description from question_paper_mark_category";
        $markList = [];
        try{
            $markList = $this->executeQueryForList($sql);
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        return $markList;
    }
    
    public function getAllDifficultyLevels() {
        $sql = "";
        $sql = "SELECT id, name, points, description FROM question_paper_difficulty_level";
        $difficultyLevels = [];
        try{
            $difficultyLevels = $this->executeQueryForList($sql);
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        return $difficultyLevels;
    }
    
    
    
    public function getAllCognitiveLevels() {
        $sql = "";
        $sql = "SELECT id, levelName, priority FROM question_paper_cognitive_levels ORDER BY priority;";
        $cognitiveLevels = [];
        try{
            $cognitiveLevels = $this->executeQueryForList($sql);
        }catch(\Exception $e)
        {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        return $cognitiveLevels;
    }
    
    
    public function getAllStaffsWhoCreatedQuestionsInASubject($batchId, $semId, $subjectId){
        
        $batchId = $this->realEscapeString($batchId);
        $semId = $this->realEscapeString($semId);
        $subjectId = $this->realEscapeString($subjectId);
        
        $sql = "";
        $sql = "SELECT DISTINCT(staffID) as staffId FROM question_paper_pool WHERE batchID='$batchId' AND semID='$semId' AND subjectID = '$subjectId'";
        try {
            $staffList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        $response = [];
        foreach ($staffList as $staff){
            $response[] = $staff->staffId;
        }
        return $response;
    }
    
    public function getEditEnableStatus($questionId){
        
        $questionId = $this->realEscapeString($questionId);
        
        $sql = "";
        $sql = "SELECT id,question,enableEdit FROM question_paper_pool WHERE id='$questionId'";
        try {
            $questionObj = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        return $questionObj;
    }
    /**
     * Method to insert section details.
     *
     * @param QuestionPaperGenSectionDetails[] $questionPaperSectionDetailsList
     * @throws AutonomousException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function upsertQuestionPaperGenSectionDetails($questionPaperSectionDetailsList)
    {
        $questionPaperSectionDetailsList = $this->realEscapeArray($questionPaperSectionDetailsList);
        $this->validateQuestionPaperGenSectionDetails($questionPaperSectionDetailsList);
        $sql = "";
        foreach ($questionPaperSectionDetailsList as $questionPaperSectionDetails) {
            $sql = "";
            $sql = "INSERT INTO `question_paper_gen_section_details` (`id`, `question_paper_gen_details_id`, `name`, `total_no_of_questions`, `mark_of_each_question`, `max_mark_of_section`, `pattern`, `no_of_questions_answerable`, `created_by`, `updated_by`, `created_date`, `updated_date`) VALUES ('$questionPaperSectionDetails->id', '$questionPaperSectionDetails->questionPaperGenDetailsId', '$questionPaperSectionDetails->name', '$questionPaperSectionDetails->totalNoOfQuestions', '$questionPaperSectionDetails->markOfEachQuestion', '$questionPaperSectionDetails->maxMarkOfSection', '$questionPaperSectionDetails->pattern', '$questionPaperSectionDetails->noOfQuestionsAnswerable', '$questionPaperSectionDetails->createdBy', '$questionPaperSectionDetails->updatedBy', UTC_TIMESTAMP(), UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE `question_paper_gen_details_id`='$questionPaperSectionDetails->questionPaperGenDetailsId', `name`='$questionPaperSectionDetails->name', `total_no_of_questions`='$questionPaperSectionDetails->totalNoOfQuestions', `mark_of_each_question`='$questionPaperSectionDetails->markOfEachQuestion', `max_mark_of_section`='$questionPaperSectionDetails->maxMarkOfSection', `pattern`='$questionPaperSectionDetails->pattern', `no_of_questions_answerable`='$questionPaperSectionDetails->noOfQuestionsAnswerable', `updated_by`='$questionPaperSectionDetails->updatedBy', `updated_date`=UTC_TIMESTAMP()";
            try {
                $this->executeQuery($sql);
            } catch (\Exception $e) {
                throw new AutonomousException($e->getCode(), $e->getMessage());
            }
        }
        return true;
    }
    /**
     *
     * @param
     * $questionPaperSectionDetailsList
     * @throws AutonomousException
     */
    public function validateQuestionPaperGenSectionDetails($questionPaperSectionDetailsList)
    {
        // check if section details are given
        if (empty($questionPaperSectionDetailsList)) {
            $msg = "";
            $msg = "Please Provide Section Details.";
            throw new AutonomousException(AutonomousException::SECTION_DETAILS_NOT_GIVEN, $msg);
        }
        $questionPaperDetails = QuestionPaperDetailsService::getInstance()->getQuestionPaperGenDetailsById($questionPaperSectionDetailsList[0]->questionPaperGenDetailsId);
        if (empty($questionPaperDetails)) {
            $msg = "";
            $msg = "There was an error while fetching question paper details.";
            throw new AutonomousException(AutonomousException::QUESTION_PAPER_DOES_NOT_EXIST, $msg);
        }
        // check if no of sections is valid
        if ($questionPaperDetails->noOfSections != sizeof($questionPaperSectionDetailsList)) {
            $msg = "";
            $msg = "Number of sections is not same as given while entering question paper details.";
            throw new AutonomousException(AutonomousException::INVALID_NO_OF_SECTIONS, $msg);
        }
        $totalMarkOfAllSections = 0;
        foreach ($questionPaperSectionDetailsList as $questionPaperSectionDetails) {
            // check if maximum mark of a section is equal to product of noOfQuestionsAnswerable and markOfEachQuestion
            if ($questionPaperSectionDetails->isPg && ($questionPaperSectionDetails->noOfQuestionsAnswerable * $questionPaperSectionDetails->markOfEachQuestion) != $questionPaperSectionDetails->maxMarkOfSection) {
                $msg = "";
                $msg = "Maximum mark of section " . $questionPaperSectionDetails->name . " is not equal to product of number of answerable questions and mark of each question";
                throw new AutonomousException(AutonomousException::INCONSISTENT_MAX_SECTION_MARKS, $msg);
            }
            $totalMarkOfAllSections += $questionPaperSectionDetails->noOfQuestionsAnswerable * $questionPaperSectionDetails->markOfEachQuestion;
            switch ($questionPaperSectionDetails->pattern) {
                case 'ALL':
                    if ($questionPaperSectionDetails->noOfQuestionsAnswerable != $questionPaperSectionDetails->totalNoOfQuestions) {
                        $msg = "";
                        $msg = "Number of questions answerable is not equal to total number of questions in the section " . $questionPaperSectionDetails->name;
                        throw new AutonomousException(AutonomousException::INCONSISTENT_NUMBER_OF_QUESTIONS, $msg);
                    }
                    break;
                case 'ANY':
                    if ($questionPaperSectionDetails->noOfQuestionsAnswerable > $questionPaperSectionDetails->totalNoOfQuestions) {
                        $msg = "";
                        $msg = "Number of questions answerable should be less than on equal to total number of questions in the section " . $questionPaperSectionDetails->name;
                        throw new AutonomousException(AutonomousException::INCONSISTENT_NUMBER_OF_QUESTIONS, $msg);
                    }
                    break;
                default:
                    $msg = "";
                    $msg = "Invalid Section Pattern";
                    throw new AutonomousException(AutonomousException::INVALID_SECTION_PATTERN, $msg);
            }
        }
        if ($questionPaperSectionDetails->isPg && ($totalMarkOfAllSections != $questionPaperDetails->maxMarks)) {
            $msg = "";
            $msg = "Total marks of all sections is not equal to the total marks of the question paper.";
            throw new AutonomousException(AutonomousException::INCONSISTENT_TOTAL_MARKS, $msg);
        }
        return true;
    }
    /**
     * get module details
     * @author Sibin
     * @param unknown $request
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getModuleDetailsBySubject($request)
    {
        $request= $this->realEscapeObject($request);
        $condition ="";
        if($request->subjectId){
            $condition .= " AND qpmw.subjectID='$request->subjectId'";
        }
        $staffList = StaffService::getInstance()->getStaffListBySubjectID($request->subjectId, $request->batchId, $request->semId);
        if(!empty($staffList)){
            $staffList = implode(",",array_column($staffList, "staffID"));
            $condition .= " AND qpmw.staffID IN ($staffList)";
        }
        $query = "SELECT qpm.name AS module,qpmw.weightage,qpmw.remarks  
                    FROM question_paper_module_weightages qpmw 
                    INNER JOIN  question_paper_module qpm ON qpm.id = qpmw.question_paper_module_id
                    WHERE 1=1  $condition 
                    GROUP BY qpmw.question_paper_module_id";
        try {
            $response = $this->executeQueryForList($query);
            return $response;
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED, $e->getMessage());
        }
    }
    /**
     * save module remarks
     * @author Sibin
     * @param unknown $remarks
     * @param unknown $moduleID
     * @param unknown $subjectID
     * @param unknown $staffID
     * @throws ProfessionalException
     * @return string
     */
    public function saveModuleRemarks($remarks, $moduleID, $subjectID, $staffID)
    {
        $remarks = $this->realEscapeString($remarks);
        $moduleID = $this->realEscapeString($moduleID);
        $subjectID = $this->realEscapeString($subjectID);
        $staffID = $this->realEscapeString($staffID);
        try {
            $response = $this->getModuleWeightage($moduleID, $subjectID, $staffID);
            if ($response) {
                $query = "UPDATE question_paper_module_weightages SET remarks='$remarks' WHERE question_paper_module_id=$moduleID and subjectID=$subjectID and staffID=$staffID";
            } else {
                $query = "INSERT INTO question_paper_module_weightages (remarks,question_paper_module_id,subjectID,staffID) VALUES ('$remarks',$moduleID,$subjectID,$staffID)";
            }
            $response = $this->executeQueryForObject($query, TRUE);
            return "inserted";
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED, $e->getMessage());
        }
    }
    
    /**
     * 
     * @param SearchQuestionPaperRequest $request
     */
    public function searchQuestionPaperGenDetailsByRequest($request){
        $request = $this->realEscapeObject($request);
        $sql = "";
        $sql = "SELECT qpgd.id, 
                    qpgd.code, 
                    qpgd.duration, 
                    qpgd.duration_type as durationType, 
                    qpgd.max_marks as maxMarks, 
                    qpgd.no_of_sections as noOfSections, 
                    IF (qpgd.exam_registration_id, 'REGULAR', 'SUPPLEMENTARY') AS examType, 
                    IF (qpgd.exam_registration_id, qpgd.exam_registration_id, qpgd.exam_supplementary_id) AS examRegistration,
                    qpgd.applicable_to as applicableTo,
                    CONCAT(s.subjectDesc, ' (',qpgd.code,')')  AS displaySubjectName 
                FROM 
                    question_paper_gen_details qpgd 
                INNER JOIN question_paper_gen_section_details qpgsd ON 
                    qpgsd.question_paper_gen_details_id = qpgd.id
                INNER JOIN question_paper_gen_exam_mapping qpgem ON
                    qpgem.question_paper_gen_details_id = qpgd.id
                INNER JOIN exam e ON
                    e.examID = qpgem.exam_id
                INNER JOIN subjects s ON
                    s.subjectID    = e.subjectID
                WHERE 1=1
            ";
                        
        if($request->id){
            $sql .= " AND qpgd.id = $request->id";
        }
        if($request->notConsiderSamePaper && $request->paperId){
            $sql .= " AND qpgd.id NOT IN ($request->paperId)";
        }
        if($request->noOfSections){
            $sql .= " AND qpgd.no_of_sections = $request->noOfSections";
        }
        $sql .= " GROUP BY qpgd.id";
        $sql .= " ORDER BY  qpgd.created_date DESC";        
        try {
            $questionPaperDetailsList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $questionPaperDetailsList;
    }
    /**
     * copy sections from question paper
     * @param SearchQuestionPaperRequest $request
     */
    public function copyQuestionPaperSections($request){
        $request = $this->realEscapeObject($request);
        $staffId = $_SESSION['adminID'];
        $sql = "";
        $sql = "INSERT INTO question_paper_gen_section_details (`question_paper_gen_details_id`, `name`, `total_no_of_questions`, `mark_of_each_question`, `max_mark_of_section`, `pattern`, `no_of_questions_answerable`, created_by, updated_by, created_date, updated_date)
        SELECT '$request->paperId', name, total_no_of_questions, mark_of_each_question, max_mark_of_section, pattern, no_of_questions_answerable, '$staffId', '$staffId', now(),now()
        FROM question_paper_gen_section_details WHERE question_paper_gen_details_id = '$request->questionPaperSelectedForCopy'";
                           
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return true;
    }
}