Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/cesanet/
Upload File :
Current File : /var/www/cesa.co.za/cesanet/v3_feedback_form.php

<?php
include("inc_functions.php");

header('Content-Type: application/json');

if (!isset($_SESSION["StudentID"]) || empty($_SESSION["StudentID"]) || $_SESSION["StudentID"] == null) {
    echo json_encode(array('success' => false, 'error' => 'Not authenticated'));
    exit;
}

if (!CurrentStudent()) {
    echo json_encode(array('success' => false, 'error' => 'Only available to current students'));
    exit;
}

$assignmentId = isset($_GET['assignment_id']) ? intval($_GET['assignment_id']) : 0;
if ($assignmentId <= 0) {
    echo json_encode(array('success' => false, 'error' => 'Missing assignment_id'));
    exit;
}

$apiBaseUrl = getenv('CESA_V3_API_BASE_URL');
if (empty($apiBaseUrl)) {
    echo json_encode(array('success' => false, 'error' => 'Missing CESA_V3_API_BASE_URL'));
    exit;
}
$apiBaseUrl = rtrim($apiBaseUrl, '/');

$assignmentObj = new CCEAssignment($sqlf);
$copyResult = $assignmentObj->getOrCreateV3FeedbackFormByStudentAssignment(
    intval($_SESSION["StudentID"]),
    $assignmentId
);
if (empty($copyResult['success']) || empty($copyResult['form_id'])) {
    echo json_encode(array('success' => false, 'error' => $copyResult['error'] ?? 'Unable to create/get v3 form'));
    exit;
}

$formId = (string) $copyResult['form_id'];
$renderEndpoint = $apiBaseUrl . '/api/external/forms/' . rawurlencode($formId) . '/render';
$renderResult = Utils::curlRequest(
    $renderEndpoint,
    'GET',
    null,
    array('Accept: application/json'),
    10,
    20
);

if (!empty($renderResult['error'])) {
    echo json_encode(array('success' => false, 'error' => (string) $renderResult['error']));
    exit;
}
if (intval($renderResult['http_code']) < 200 || intval($renderResult['http_code']) >= 300) {
    echo json_encode(array('success' => false, 'error' => 'V3 render HTTP ' . intval($renderResult['http_code'])));
    exit;
}

$decoded = json_decode((string) ($renderResult['response'] ?? ''), true);
if (!is_array($decoded) || empty($decoded['success'])) {
    echo json_encode(array('success' => false, 'error' => 'Invalid v3 render response'));
    exit;
}

$resolvedFormId = (string)($decoded['form_id'] ?? $formId);
if ($resolvedFormId !== '') {
    $assignmentObj->storeFeedbackFormId(
        intval($_SESSION["StudentID"]),
        $assignmentId,
        $resolvedFormId
    );
}

echo json_encode(array(
    'success' => true,
    'form_id' => $resolvedFormId,
    'title' => $decoded['title'] ?? ($copyResult['title'] ?? 'Feedback form'),
    'form_completed_id' => $decoded['form_completed_id'] ?? null,
    'html' => $decoded['html'] ?? '',
));