Mini Kabibi Habibi

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

<?php
session_start();
@include 'include/config.php';
require_once 'vendor/autoload.php';

use PhpOffice\PhpWord\TemplateProcessor;

// Check if the user is logged in
if (!isset($_SESSION['username_school']) || empty($_SESSION['username_school'])) {
    header('Location: login.php');
    exit();
}

$username = $_SESSION['username_school'];
$plan_id = $_GET['plan_id'] ?? null;

if (!$plan_id) {
    die("Missing Plan ID");
}

// Default values
$fullname = 'N/A';
$immediate_supervisor = 'N/A';
$activities = [];

// Ensure the database connection is valid
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get user info from the 'information' table using the logged-in username
$user_stmt = $conn->prepare("SELECT fullname, immediate_supervisor FROM information WHERE username = ?");
if ($user_stmt === false) {
    die('Prepare failed: ' . $conn->error);
}
$user_stmt->bind_param("s", $username);
$user_stmt->execute();
$user_result = $user_stmt->get_result();
if ($user_result->num_rows > 0) {
    $row = $user_result->fetch_assoc();
    $fullname = $row['fullname'];
    $immediate_supervisor = $row['immediate_supervisor']; // Immediate supervisor value
} else {
    die("User not found.");
}

// Get the MISTA plan data using 'mista_plans' table
$planQuery = $conn->prepare("SELECT * FROM mista_plans WHERE id = ?");
if ($planQuery === false) {
    die('Prepare failed: ' . $conn->error);
}
$planQuery->bind_param("i", $plan_id);
$planQuery->execute();
$planResult = $planQuery->get_result()->fetch_assoc();

if (!$planResult) {
    die("Plan not found or invalid Plan ID.");
}

// Get activities related to the plan
$activitiesQuery = $conn->prepare("SELECT * FROM mista_plan_activities WHERE plan_id = ?");
if ($activitiesQuery === false) {
    die('Prepare failed: ' . $conn->error);
}
$activitiesQuery->bind_param("i", $plan_id);
$activitiesQuery->execute();
$activitiesResult = $activitiesQuery->get_result();
if ($activitiesResult->num_rows > 0) {
    while ($row = $activitiesResult->fetch_assoc()) {
        $activities[] = [
            'date' => date('F d, Y', strtotime($row['date'])),
            'instructional_supervisory_and_monitoring_activity' => $row['instructional_supervisory_and_monitoring_activity'],
            'school' => $row['school'],
            'school_head' => $row['school_head'],
            'remarks' => $row['remarks']
        ];
    }
} else {
    $activities[] = [
        'date' => 'N/A',
        'instructional_supervisory_and_monitoring_activity' => 'No activities recorded.',
        'school' => 'N/A',
        'school_head' => 'N/A',
        'remarks' => 'N/A'
    ];
}

// Load the template for MISTA Plan
$templatePath = 'C:\\xampp\\htdocs\\cid\\system\\templates\\mistap.docx';
if (!file_exists($templatePath)) {
    die("Template file not found.");
}

$templateProcessor = new TemplateProcessor($templatePath);

// Fill in the basic plan data
$templateProcessor->setValue('title', $planResult['title'] ?? 'Monthly Instructional Supervisory and Technical Assistance Plan');
$templateProcessor->setValue('division', $planResult['division']);
$templateProcessor->setValue('section', $planResult['section']);
$templateProcessor->setValue('district', $planResult['district']);
$templateProcessor->setValue('month', $planResult['month']);
$templateProcessor->setValue('year', $planResult['year']);

// Fill in the fullname and immediate supervisor placeholders
$templateProcessor->setValue('fullname', strtoupper($fullname));
$templateProcessor->setValue('immediate_supervisor', strtoupper($immediate_supervisor));

// Set the current date for employee and supervisor date fields
$currentDate = date('F d, Y');
$templateProcessor->setValue('employee_date', $currentDate); // Employee's date
$templateProcessor->setValue('supervisor_date', $currentDate); // Supervisor's date

// Clone the activity rows into the template
$templateProcessor->cloneRowAndSetValues('date', $activities);

// Output the generated Word file
$filename = "MISTA_Plan_" . $plan_id . ".docx";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
$templateProcessor->saveAs("php://output");
exit();
?>