Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/php/includes/
Upload File :
Current File : /var/www/cesa.co.za/php/includes/ADecQuestionnaires.php

<?php

/*
 * Contact otto@igotafrica.com for details
 */

include_once('BaseTableClass.php');
include_once('ADecQuestions.php');
include_once('ADQAnswers.php');
include_once('ADQFirms.php');
include_once('Utils.php');

/**
 * Control the  ADecQuestionnaires table
 *
 * @author Otto Saayman
 */
class ADecQuestionnaires extends BaseTableClass
{

    public function findByQId($qid)
    {

        if (strlen($qid) <= 0) {
            return array();
        }

        $sql = "SELECT * FROM ADecQuestionnaires adq "
            . "WHERE adq.ADQID=" . $qid;

        //        echo $sql . '<br />';

        return $this->sqlf->getRow($sql);
    }

    public function getExportDataAsString($qid)
    {

        $questionnaire = $this->findByQId($qid);
        $fileStr = '"FirmID","Firm","Score","ScorePerc","DateStarted","DateCompleted","AuditYear"';
        $fileStrExtended = '';

        if (!is_array($questionnaire) || count($questionnaire) <= 0) {
            return $fileStr . "\r\n";
        }

        $questionObj = new ADecQuestions($this->sqlf);
        $questions = $questionObj->findByQId($qid);

        if (!is_array($questions) || count($questions) <= 0) {
            return $fileStr . "\r\n";
        }

        foreach ($questions as $question) {
            $fileStr .= ',"' . $question['ADQuestionID'] . '"';
            $fileStrExtended .= ',"' . $question['ADQuestionID'] . ' Evidence","' . $question['ADQuestionID'] . ' Justification","' . $question['ADQuestionID']
                . ' Reviewer Changed Answer"';
        }

        $fileStr .= $fileStrExtended . "\r\n";

        $firmObj = new ADQFirms($this->sqlf);
        $answerObj = new ADQAnswers($this->sqlf);
        $firms = $firmObj->getAllFromQId($qid);

        echo 'Exporting ' . count($firms) . ' firms <br />';

        foreach ($firms as $firm) {

            $fileOneRowStr = '"' . $firm["FirmID"] . '","' . strip_chars($firm["FirmName"]) . '","' . $firm["Score"] . '",'
                . '"' . $firm["ScorePerc"] . '","' . $firm["DateStarted"] . '",'
                . '"' . $firm["DateCompleted"] . '","' . $firm["AuditYear"] . '"';
            $fileOneRowStrExtended = '';

            $answers = $answerObj->getByADQFirmIdQId($qid, $firm["ADQFirmID"]);

            if (!is_array($answers) || count($answers) <= 0) {
                foreach ($questions as $question) {
                    $fileOneRowStr .= ',""';
                    $fileOneRowStrExtended .= ',"","",""';
                }
            } else {
                foreach ($answers as $answer) {
                    $fileOneRowStr .= ',"' . $answer['Answer'] . '"';
                    $fileOneRowStrExtended .= ',"' . strip_chars($answer['Evidence']) . '","' . strip_chars($answer['Justification']) . '","'
                        . $answer['ReviewerChangedAnswer'] . '"';
                }
            }

            // echo 'Answers for firm ' . $firm["FirmID"] . ': ' . count($answers) . '<br />';

            $fileOneRowStr .= $fileOneRowStrExtended;
            $fileOneRowStr = str_replace("\r\n", ' ', $fileOneRowStr);
            $fileOneRowStr = str_replace("\r", ' ', $fileOneRowStr);
            $fileOneRowStr = str_replace("\n", ' ', $fileOneRowStr);
            $fileOneRowStr = str_replace('  ', ' ', $fileOneRowStr);

            $fileStr .= $fileOneRowStr .  "\r\n";
        }

        return $fileStr;
    }
}