Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/generate_mqcais_word.php

<?php
require_once 'vendor/autoload.php';

use PhpOffice\PhpWord\TemplateProcessor;

session_start();
@include 'include/config.php';

// Get the report ID from the URL
$report_id = $_GET['report_id'] ?? null;
if (!$report_id) {
    die("Missing Report ID");
}

// Load user data
$username = $_SESSION['username_school'] ?? null;
$fullname = 'N/A';
$immediate_supervisor = 'N/A';

if ($username) {
    $user_stmt = $conn->prepare("SELECT fullname, immediate_supervisor FROM information WHERE username = ?");
    $user_stmt->bind_param("s", $username);
    $user_stmt->execute();
    $user_result = $user_stmt->get_result();
    if ($user_result->num_rows > 0) {
        $user_info = $user_result->fetch_assoc();
        $fullname = strtoupper($user_info['fullname']);
        $immediate_supervisor = strtoupper($user_info['immediate_supervisor']);
    }
    $user_stmt->close();
}

// Get report data from mqcais_reports
$reportStmt = $conn->prepare("SELECT * FROM admin_mqcais_reports WHERE id = ?");
$reportStmt->bind_param("i", $report_id);
$reportStmt->execute();
$reportResult = $reportStmt->get_result()->fetch_assoc();
$reportStmt->close();

// If report is not found
if (!$reportResult) {
    die("Report not found for ID $report_id");
}

// Get activity data from mqcais_monitoring_reports
$activitiesStmt = $conn->prepare("SELECT * FROM admin_mqcais_monitoring_reports WHERE report_id = ?");
$activitiesStmt->bind_param("i", $report_id);
$activitiesStmt->execute();
$activitiesResult = $activitiesStmt->get_result();
$activitiesStmt->close();

// If no activities found
if ($activitiesResult->num_rows === 0) {
    die("No activities found for this report.");
}

// Load the template
$templatePath = 'C:/xampp/htdocs/cid/system/templates/mqcais.docx';
if (!file_exists($templatePath)) {
    die("Template not found at: $templatePath");
}

$templateProcessor = new TemplateProcessor($templatePath);

// Set header values in the template
$templateProcessor->setValue('division', $reportResult['division']);
$templateProcessor->setValue('section', $reportResult['section']);
$templateProcessor->setValue('district', $reportResult['district']);
$templateProcessor->setValue('month', $reportResult['month']);
$templateProcessor->setValue('school_year', $reportResult['school_year']); // School year from the mqcais_reports table
$templateProcessor->setValue('fullname', $fullname);
$templateProcessor->setValue('immediate_supervisor', $immediate_supervisor);

// Set dates
$currentDate = date('F d, Y');
$templateProcessor->setValue('employee_date', $currentDate);
$templateProcessor->setValue('supervisor_date', $currentDate);

// Prepare rows for the table with activity data
$activityRows = [];
while ($row = $activitiesResult->fetch_assoc()) {
    $activityRows[] = [
        'date' => $row['date'],
        'no_of_schools_monitored_and_provided_ta' => $row['no_of_schools_monitored_and_provided_ta'],
        'activities_undertaken' => $row['activities_undertaken'],
        'findings' => $row['findings'],
        'technical_assistance_provided' => $row['technical_assistance_provided'],
        'agreement' => $row['agreement']
    ];
}

// Clone table rows
$templateProcessor->cloneRowAndSetValues('date', $activityRows);

// Output the final document
$filename = "MQCAIS_Report_{$report_id}.docx";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
$templateProcessor->saveAs("php://output");
exit();
?>