Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/php/templates/bce/
Upload File :
Current File : /var/www/cesa.co.za/php/templates/bce/memorandum.php

<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering

// Include PHPMaker configuration and functions
include_once($_SERVER['DOCUMENT_ROOT'] . "/cceadmin/ewcfg7.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/cceadmin/ewmysql7.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/cceadmin/phpfn7.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/cceadmin/userfn7.php");

// Check authentication
$Security = new cAdvancedSecurity();
if (!$Security->IsLoggedIn()) {
    $Security->AutoLogin();
}
if (!$Security->IsLoggedIn()) {
    $Security->SaveLastUrl();
    header("Location: /cceadmin/login.php");
    exit();
}

$cceExamQuestionObj = new CCEExamQuestion($sqlf);

$questionsWithAnswers = $cceExamQuestionObj->findAllWithAnswers($_REQUEST['exam_id']);

// Export data to CSV
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=exam_memorandum_" . $_REQUEST['exam_id'] . ".csv");

$output = fopen('php://output', 'w');
$showHeader = true;
$showExamName = true;
$showSection = true;
$sectionName = '';
$questionNumber = 0;
$qRow = [];

foreach ($questionsWithAnswers as $question) {
    
    if ($showSection) {
        fputcsv($output, array('Exam:', $question['Questionnaire_Name']));
        $showSection = false;
    }

    if ($question['Section_Name'] !== $sectionName) {
        fputcsv($output, array('Section:', $question['Section_Name']));
        $sectionName = $question['Section_Name'];
    }

    if ($showHeader) {
        fputcsv(
            $output, 
            array(
            'Title', 'Display Order', 'Question', 'Multiple', 'Optional', 'Answer Type', 'Marks', 'Model Answer', 
            'Option 1', 'Option 1 Score', 'Option 2', 'Option 2 Score', 'Option 3', 'Option 3 Score', 
            'Option 4', 'Option 4 Score', 'Option 5', 'Option 5 Score', 'Option 6', 'Option 6 Score'
            )
        );

        $showHeader = false;
    }

    if ($questionNumber !== $question['Question_ID']) {

        if (count($qRow) > 0) {
            fputcsv($output, $qRow);
        }

        $qRow = [];
    }

    $modelAnswer = '';

    if ($question['Answer_Type'] === 'a') {

        $answerType = 'Text Box';

        if (isset($question['Answer']) && strlen($question['Answer']) > 0) {
            $modelAnswer = $question['Answer'];
        } elseif (isset($question['Model_Answer']) && strlen($question['Model_Answer']) > 0) {
            $modelAnswer = $question['Model_Answer'];
        }

    } elseif ($question['Answer_Type'] === 'f') {
        $answerType = 'File';
    } elseif ($question['Answer_Type'] === 'm') {
        $answerType = 'Radio/ Check Box';
    } elseif ($question['Answer_Type'] === 's') {
        $answerType = 'Select';
    } else {
        $answerType = '';
    }

    if (count($qRow) === 0) {

        if ($question['IsMulti'] === '1') {
            $multiple = 'Yes';
        } else {
            $multiple = 'No';
        }

        if ($question['IsOptional'] === '1') {
            $optional = 'Yes';
        } else {
            $optional = 'No';
        }

        $qRow = array(
            $question['QuestionTitle'], 
            $question['Display_Order'], 
            addslashes(cleanTextForCSV($question['Question'])), 
            $multiple, 
            $optional, 
            $answerType, 
            $question['NumMarks'], 
            addslashes(cleanTextForCSV($modelAnswer))
            )
            ;
    }

    if (in_array($question['Answer_Type'], ['m', 's'])) {
        $qRow[] = cleanTextForCSV($question['Answer']);
        $qRow[] = $question['Answer_Score'];
    }

    $questionNumber = $question['Question_ID'];
} //foreach

fclose($output);