All pastes #562910 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-06-12 23:08 UTC
rendered paste body
class RFPException extends Exception { }

class RFP {
    
    private $dbObj;
    private $name;
    private $title;
    private $text;
    private $id;
    
    private $questions;
    
    public function __construct($id=0) {
	
	$this->dbObj = Database::connect('mysql');
	
	if($id > 0) {
	    $this->loadForm($id);
	    $this->id = $id;
	}
	
    }
    
    public function getID() {
	return $this->id;
    }
    
    public function getName() {
	return $this->name;
    }
    
    public function setName($name) {
	$this->name = $name;
    }
    
    public function getTitle() {
	return $this->title;
    }
    
    public function setTitle($title) {
	$this->title = $title;
    }
    
    public function getText() {
	return $this->text;
    }
    
    public function setText($text) {
	$this->text = $text;
    }
    
    private function loadForm($id) {
	
	if( is_int((int)$id) ) {
	    
	    $query = '
		SELECT
		    r.Name,
		    d.title,
		    d.text
		FROM
		    RFP_Forms r
		LEFT JOIN
		    RFP_FormDesign d on r.ID = d.FormID
		WHERE
		    r.ID = '. $id .'
	    ';
	    
	    $rs = $this->dbObj->getRow( $query );
	    
	    $this->setName($rs['Name']);
	    $this->setTitle($rs['title']);
	    $this->setText($rs['text']);
	    
	}
	
    }
    
    public function getDetails() {
	
    }
    
    public function makeChanges() {
	
    }
    
    public function submitForm() {
	
    }
    
    public function getHTML() {
	
	
	
    }
    
    public function getQuestions() {
	
	$query = '
	    SELECT
		QuestionID
	    FROM
		RFP_FormQuestions
	    WHERE
		FormID = '. $this->id .' AND
		IsActive = 1
	    ORDER BY
		`Order` ASC';
	
	$q = $this->dbObj->getAll($query);
	
	if( is_array($q) ) {
	    
	    foreach($q as $question) {
		
		$this->questions[] = $question['QuestionID'];
		
	    }
	    
	}
	
	if( is_array( $this->questions ) ) {
	    
	    $query = '
		SELECT
		    q.ID,
		    q.Question as `text`,
		    q.AnswerTypeID as `AnswerType`,
		    q.QuestionCategoryID as `CategoryID`,
		    c.Name as `Category`
		FROM
		    RFP_Questions q
		LEFT JOIN
		    RFP_QuestionCategories c on c.ID = q.QuestionCategoryID
		WHERE
		    q.ID in ('. implode(',',$this->questions) .') AND
		    q.IsActive = 1
		ORDER BY
		    FIELD(q.ID,'. implode(',',$this->questions) .')
	    ';
	    
	    $rs = $this->dbObj->getAll( $query );
	    
	    return $this->getAnswers($rs);
	    
	} else {
	    
	    return false;
	    
	}
	
    }
    
    public function __wakeup()
    {
        $this->dbObj = Database::connect();
	unset($this->questions);
	
	$this->loadForm($this->getID());
	
    }
    
    public function getAnswers($questions) {
	
	if(is_array($questions)) {
	    
	    for($i=0; $i<count($questions); $i++) {
		
		$questions[$i]['answer'] = $this->getAnswer($questions[$i]['AnswerType'], $questions[$i]['ID']);
		unset($questions[$i]['AnswerType']);
		
	    }
	    
	}
	
	return $questions;
	
    }
    
    public function getAnswer($typeID, $questionID) {
	
	$AnswerType = '
	    SELECT
		Type
	    FROM
		RFP_AnswerTypes
	    WHERE
		ID = '. $typeID .' and
		IsActive = 1
	';
	
	$type = $this->dbObj->getField($AnswerType);
	
	switch($type) {
	    
	    //Get the options for the questions
	    //add more cases with options later
	    case 'checkbox':
		
		$AnswerOptions = '
		    SELECT
			OptionText
		    FROM
			RFP_AnswerOptions
		    WHERE
			QuestionID = '. $questionID .' AND
			IsActive = 1
		    ORDER BY
			`Order`
		';
		
		$options = $this->dbObj->getAll($AnswerOptions);
		break;
	    
	    default:
		$options = null;
		break;
	    
	}
	
	$html = Forms::getInput($type, $options, $questionID);
	return $html;
	
    }
    
    public function addQuestion($question) {
	
	
	
    }
    
    public function deleteQuestion() {
	
    }
    
    public function modifyQuestion() {
	
    }
    
    public function create(RFPTemplate $t) {
	
	$type = (int) $t->getTypeID();
	
	if(!is_int($type)) {
	    throw new RFPException('Unable to get TypeID: '. $type);
	}
	
	//figure out the type of RFP, Actual Name "Wedding"
	$query = '
	    SELECT
		Type
	    FROM
		RFP_Types
	    WHERE
		ID = '. $type .'
	    LIMIT 1';
	
	$typeName = $this->dbObj->getField($query);
	
	//How many other same type RFPs are there?
	$query = '
	    SELECT
		count(ID)
	    FROM
		RFP_Forms
	    WHERE
		TypeID = '. $type;
	
	$number = $this->dbObj->getField($query) + 1;
	
	//Create the Record for the RFP
	$query = '
	    INSERT INTO
		RFP_Forms
	    SET
		Name = "'. $typeName .' '. $number .'",
		TypeID = '. $type .',
		AccountID = '. $_SESSION['user']->getAccountID();
	
	$this->dbObj->query($query);
	
	$this->setName($typeName .' '. $number);
	
	//save the id of the form we just created
	$FormID = $this->dbObj->getLastID();
	
	$this->id = $FormID;
	
	//add the questions
	if(!is_array($t->getQuestions())) {
	    throw new RFPException('Unable to load questions no array: $this->questions');
	}
	
	$questions = $t->getQuestions();
	
	$query = '
	    INSERT INTO RFP_FormQuestions
	    (`FormID`,`QuestionID`,`Order`,`IsActive`) VALUES 
	';
	
	$i = 1;
	
	while($i <= count($questions)) {
	    
	    $query .= '('. $FormID .', '. $questions[($i - 1)] .', '. $i .', 1)';
	    
	    if($i != count($questions)) {
		$query .= ', ';
	    }
	    
	    $i++;
	    
	}
	
	$this->dbObj->query($query);
	
	$query = '
	    INSERT INTO RFP_FormDesign
	    SET
		FormID = '. $FormID .',
		title = "'. $this->getName() .'",
		text = "Please fill out the request...",
		IsActive = 1
	';
	
	$this->dbObj->query($query);
	
    }
    

}