File "download_order_copy.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/question_paper_orders/download_order_copy.php
File size: 9.98 KiB (10220 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once '../../config/config.php';

$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($order_id <= 0) {
    http_response_code(400);
    echo 'Invalid order id';
    exit;
}

// Load order meta
$sql = "SELECT qpo.order_id, qpo.exam_subject_id, qpo.setter_id, qpo.status, qpo.time_duration,
               qpo.approver_id, qpo.approved_at, qpo.final_pattern_id,
               e.exam_name, e.academic_year, e.semester,
               sub.subject_name, sub.subject_code,
               setter.first_name AS setter_first, setter.last_name AS setter_last,
               appr.first_name AS approver_first, appr.last_name AS approver_last
        FROM question_paper_orders qpo
        JOIN exam_subjects es ON qpo.exam_subject_id = es.exam_subject_id
        JOIN exams e ON es.exam_id = e.exam_id
        JOIN subjects sub ON es.subject_id = sub.subject_id
        LEFT JOIN staff setter ON qpo.setter_id = setter.staff_id
        LEFT JOIN staff appr ON qpo.approver_id = appr.staff_id
        WHERE qpo.order_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$order_id]);
$order = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$order) { http_response_code(404); echo 'Order not found'; exit; }

// Load sections
$sectionsStmt = $pdo->prepare('SELECT * FROM pattern_sections WHERE pattern_id = ? ORDER BY section_id');
$sectionsStmt->execute([$order['final_pattern_id']]);
$sections = $sectionsStmt->fetchAll(PDO::FETCH_ASSOC);

// Load questions per section
$questionsBySection = [];
$qStmt = $pdo->prepare('SELECT * FROM pattern_questions WHERE section_id = ? ORDER BY question_no');
foreach ($sections as $s) {
    $qStmt->execute([$s['section_id']]);
    $questionsBySection[$s['section_id']] = $qStmt->fetchAll(PDO::FETCH_ASSOC);
}

// Try to load TCPDF (Composer or bundled path)
$tcpdfLoaded = false;
// Composer autoload
$vendorAutoload = __DIR__ . '/../../vendor/autoload.php';
if (file_exists($vendorAutoload)) {
    require_once $vendorAutoload;
    if (class_exists('TCPDF')) { $tcpdfLoaded = true; }
}
// Direct library paths
if (!$tcpdfLoaded) {
    $candidates = [
        __DIR__ . '/../../vendor/tecnickcom/tcpdf/tcpdf.php',
        __DIR__ . '/../../assets/vendor/libs/tcpdf/tcpdf.php'
    ];
    foreach ($candidates as $p) {
        if (file_exists($p)) { require_once $p; $tcpdfLoaded = true; break; }
    }
}
// Defer PDF generation until after building HTML. If TCPDF is not available, we'll output HTML fallback.

$collegeName = 'Issac Newton Engineering College';
$setterName = trim(($order['setter_first'] ?? '') . ' ' . ($order['setter_last'] ?? ''));
$approverName = trim(($order['approver_first'] ?? '') . ' ' . ($order['approver_last'] ?? ''));

$html = '<style>
 h1{font-size:18px;margin:0 0 6px 0;}
 h2{font-size:13px;margin:6px 0 4px 0;}
 .meta{font-size:11px;color:#444;margin-bottom:6px}
 table{width:100%;border-collapse:collapse;font-size:11px}
 th,td{border:1px solid #999;padding:4px;}
 .badge{display:inline-block;border:1px solid #666;padding:1px 4px;margin-left:4px;}
 .certificate{border:1px solid #333; padding:8px;}
 .section-block{page-break-inside: avoid;}
 .sigblock{page-break-inside: avoid;}
 .terms{page-break-inside: avoid;}
</style>';

$html .= '<div class="certificate">';
$html .= '<h1 style="text-align:center">'.$collegeName.'</h1>';
$html .= '<h2 style="text-align:center">Question Paper Order</h2>';
$html .= '<div class="meta">Order ID: '.$order_id.' &nbsp;•&nbsp; Exam: '.htmlspecialchars($order['exam_name']).' ('.$order['academic_year'].') &nbsp;•&nbsp; Semester: '.$order['semester'].'<br/>';
$html .= 'Subject: '.htmlspecialchars($order['subject_name']).' ('.$order['subject_code'].') &nbsp;•&nbsp; Duration: '.(int)$order['time_duration'].' mins<br/>';
$html .= 'Setter: '.htmlspecialchars($setterName ?: '-') . ' &nbsp;•&nbsp; Approver: '.htmlspecialchars($approverName ?: '-') . ' &nbsp;•&nbsp; Status: '.htmlspecialchars($order['status']).'</div>';

foreach ($sections as $s) {
    $html .= '<div class="section-block">';
    $html .= '<h2>'.htmlspecialchars($s['section_name']).' — '.$s['num_questions'].' × '.$s['marks_per_question'].' = '.$s['section_total'].' (Q'.$s['question_start_num'].'-'.$s['question_end_num'].')</h2>';
    $html .= '<table nobr="true"><thead><tr><th style="width:10%">Q.No</th><th>Question</th><th style="width:20%">CO</th><th style="width:20%">PO</th></tr></thead><tbody>';
    $qFrom = (int)$s['question_start_num']; $qTo = (int)$s['question_end_num'];
    $map = $questionsBySection[$s['section_id']] ?? [];
    for ($i=$qFrom; $i<=$qTo; $i++) {
        $q = null; foreach ($map as $qq) { if ((int)$qq['question_no'] === $i) { $q = $qq; break; } }
        $text = $q['question_text'] ?? '';
        $co = '';
        if (!empty($q['co_codes'])) { $dec=json_decode($q['co_codes'], true); $co = is_array($dec)? implode(', ', $dec): $q['co_codes']; }
        $po = '';
        if (!empty($q['po_codes'])) { $dec=json_decode($q['po_codes'], true); $po = is_array($dec)? implode(', ', $dec): $q['po_codes']; }
        $html .= '<tr><td>'.$i.'</td><td>'.htmlspecialchars($text).'</td><td>'.htmlspecialchars($co).'</td><td>'.htmlspecialchars($po).'</td></tr>';
    }
    $html .= '</tbody></table>';
    $html .= '</div>';
}

// Professional footer: date, signatures, seal, terms & conditions
$today = date('d-m-Y');
$html .= '<br/>';
$html .= '<table class="sigblock" style="width:100%; border:0; font-size:11px; margin-top:8px;">
  <tr>
    <td style="width:50%; vertical-align:top; padding-right:8px;">
      <div style="margin-bottom:8px;"><strong>Date:</strong> '.$today.'</div>
      <div style="margin-bottom:4px;"><strong>Terms & Conditions</strong></div>
      <ol style="margin:0 0 8px 16px; padding:0;">
        <li>Question paper content is highly confidential; sharing or duplication is prohibited.</li>
        <li>Strictly adhere to approved pattern and mark allocation; no deviations without approval.</li>
        <li>Ensure clarity, legibility, and unambiguous wording for every question.</li>
        <li>Provide required data tables/figures and units; avoid external dependencies.</li>
        <li>Map each question to relevant CO(s)/PO(s) accurately; avoid mismatches.</li>
        <li>Submit the draft within the stipulated timeline for moderation/approval.</li>
        <li>Do not reveal identities or add hints about examiners/valuators anywhere.</li>
        <li>Comply with academic integrity and anti‑plagiarism guidelines at all times.</li>
        <li>Report unforeseen issues immediately to the Controller of Examinations.</li>
        <li>In case of recall/revision, resubmit corrected copy promptly as instructed.</li>
      </ol>
      <div style="font-size:10px;color:#666;">This order copy is for official use of '.$collegeName.'.</div>
    </td>
    <td style="width:50%; vertical-align:top; padding-left:8px;">
      <table style="width:100%; border:0; font-size:11px;">
        <tr>
          <td style="height:40px; border-bottom:1px solid #999;">&nbsp;</td>
        </tr>
        <tr>
          <td style="text-align:center; padding-top:4px;">Prepared By (COE Office)</td>
        </tr>
        <tr><td style="height:16px;"></td></tr>
        <tr>
          <td style="height:40px; border-bottom:1px solid #999;">&nbsp;</td>
        </tr>
        <tr>
          <td style="text-align:center; padding-top:4px;">Question Paper Setter</td>
        </tr>
        <tr><td style="height:16px;"></td></tr>
        <tr>
          <td style="height:40px; border-bottom:1px solid #999;">&nbsp;</td>
        </tr>
        <tr>
          <td style="text-align:center; padding-top:4px;">Approver / HOD</td>
        </tr>
        <tr><td style="height:16px;"></td></tr>
        <tr>
          <td style="height:40px; border-bottom:1px solid #999;">&nbsp;</td>
        </tr>
        <tr>
          <td style="text-align:center; padding-top:4px;">Controller of Examinations</td>
        </tr>
      </table>
      <div style="margin-top:8px; font-size:10px; text-align:center;">Institution Seal</div>
      <div style="margin:8px auto 0 auto; width:120px; height:60px; border:1px dashed #666; border-radius:6px; text-align:center; line-height:60px; color:#888;">SEAL</div>
    </td>
  </tr>
</table>';
$html .= '</div>'; // end certificate

if ($tcpdfLoaded) {
    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
    $pdf->SetCreator('Autonomous College');
    $pdf->SetAuthor('Autonomous College');
    $pdf->SetTitle('Question Paper Order #'.$order_id);
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetMargins(8, 8, 8);
    $pdf->SetAutoPageBreak(true, 8);
    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, true, false, '');
    // Draw border frame (certificate look)
    $n = $pdf->getNumPages();
    for ($p=1; $p<=$n; $p++) {
        $pdf->setPage($p);
        // Watermark on each page
        $pdf->SetAlpha(0.08);
        $pdf->SetFont('helvetica', 'B', 40);
        $pdf->StartTransform();
        $pdf->Rotate(35, 60, 120);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Text(28, 120, 'Controller of Examinations');
        $pdf->StopTransform();
        $pdf->SetAlpha(1);
        // Border
        $pdf->SetDrawColor(50,50,50);
        $pdf->Rect(6, 6, $pdf->getPageWidth()-12, $pdf->getPageHeight()-12);
    }
    $pdf->Output('order_'.$order_id.'.pdf', 'D');
    exit;
} else {
    // HTML fallback with print option
    header('Content-Type: text/html; charset=UTF-8');
    echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>Order #'.$order_id.'</title>';
    echo '<style>
      @media print { .noprint{ display:none } body{ margin:10mm } }
      body{ margin:10mm; font-family: Arial, Helvetica, sans-serif; }
      .toolbar{margin:8px 0}
      button{padding:6px 10px;}
      .wm{ position:fixed; top:40%; left:5%; transform:rotate(-30deg); font-weight:bold; font-size:48px; color:rgba(0,0,0,.08); pointer-events:none; }
    </style>';
    echo '</head><body>'; 
    echo '<div class="toolbar noprint"><button onclick="window.print()">Print / Save as PDF</button></div>';
    echo '<div class="wm">Controller of Examinations</div>';
    echo $html;
    echo '</body></html>';
    exit;
}


PHP File Manager