Your IP : 216.73.217.117


Current Path : /var/www/cesa.co.za/php/includes/
Upload File :
Current File : /var/www/cesa.co.za/php/includes/CCEStudents.php

<?php

require_once __DIR__ . '/BaseTableClass.php';
require_once __DIR__ . '/CCEStudentDisplayOrder.php';

/*
 * Contact otto@igotafrica.com for details
 */

/**
 * Manage the CCEStudents table
 *
 * @author otto
 */
class CCEStudents extends BaseTableClass {

    /**
     * SQL ORDER BY expression: KnownAs, else FirstName, then Surname.
     *
     * @param string $tableAlias
     * @return string
     */
    public static function sqlOrderByDisplayName($tableAlias = 'CCEStudents')
    {
        return CCEStudentDisplayOrder::sqlOrderByDisplayName($tableAlias);
    }

    /**
     * @param string $direction Asc|Desc|ASC|DESC
     * @return string Includes leading " ORDER BY "
     */
    public static function sqlOrderByLookup($direction = 'ASC')
    {
        return CCEStudentDisplayOrder::sqlOrderByLookupFragment($direction);
    }

    /**
     * @param array<int, array<string, mixed>> $rows
     */
    public static function sortRowsByDisplayName(array &$rows)
    {
        CCEStudentDisplayOrder::sortRows($rows);
    }

    public function getECSACategorySelectArray() {

        $pickList = new Picklists($this->sqlf);

        return $pickList->getSelectArray('ECSA Registration Categories');
    }

    public function getECSACategorySelectList($val) {

        return HTMLHelper::drawSelect(
                        [
                            'name' => 'ECSACategory',
                            'value' => $val,
                            'choices' => $this->getECSACategorySelectArray(),
                            'attr' => ['class' => 'select2-field'],
                        ]
                );
    }


    /**
     * Parse ModulesExcluded CSV into unique positive module IDs.
     *
     * @param string|null $csv
     * @return int[]
     */
    public static function parseModulesExcludedCsv($csv)
    {
        $ids = array();
        if ($csv === null || $csv === '') {
            return $ids;
        }
        foreach (explode(',', (string)$csv) as $part) {
            $id = intval(trim($part));
            if ($id > 0) {
                $ids[$id] = $id;
            }
        }
        return array_values($ids);
    }

    /**
     * SQL fragment: exclude scores for students with this module in ModulesExcluded.
     * Safe when ModulesExcluded is NULL or empty.
     *
     * @param string $studentAlias Table alias for CCEStudents
     * @param string $moduleExpr SQL expression for CCEModuleID (e.g. CCEAssignments.CCEModuleID)
     * @return string
     */
    public static function sqlClassAverageExclusionClause($studentAlias = 'CCEStudents', $moduleExpr = 'CCEAssignments.CCEModuleID')
    {
                $studentAlias = preg_replace('/[^A-Za-z0-9_]/', '', (string)$studentAlias);
        if ($studentAlias === '') {
            $studentAlias = 'CCEStudents';
        }
        return " AND FIND_IN_SET(" . $moduleExpr . ", IFNULL(" . $studentAlias . ".ModulesExcluded, '')) = 0";
    }

    /**
     * @param int $studentId
     * @return int[]
     */
    public function getModulesExcluded($studentId)
    {
        $studentId = intval($studentId);
        if ($studentId <= 0) {
            return array();
        }

        $row = null;
        if (is_object($this->sqlf)) {
            $row = $this->sqlf->getRow(
                'SELECT ModulesExcluded FROM CCEStudents WHERE CCEStudentID=' . $studentId
            );
        }
        if (!is_array($row)) {
            $row = self::fetchModulesExcludedRowViaMysqli($studentId);
        }
        if (!is_array($row)) {
            return array();
        }

        return self::parseModulesExcludedCsv($this->rowColumn($row, 'ModulesExcluded'));
    }

    /**
     * Fallback read when ADODB/sqlf is unavailable or returns a failed result set.
     *
     * @param int $studentId
     * @return array<string, mixed>|null
     */
    private static function fetchModulesExcludedRowViaMysqli($studentId)
    {
        global $conni;

        if (!isset($conni) || !$conni) {
            return null;
        }

        $sql = 'SELECT ModulesExcluded FROM CCEStudents WHERE CCEStudentID=' . intval($studentId);
        $rec = mysqli_query($conni, $sql);
        if (!$rec) {
            return null;
        }

        $row = mysqli_fetch_assoc($rec);
        mysqli_free_result($rec);

        return is_array($row) ? $row : null;
    }

    /**
     * @param int $studentId
     * @param int $moduleId
     * @return bool
     */
    public function isExcludedFromModule($studentId, $moduleId)
    {
        $moduleId = intval($moduleId);
        if ($moduleId <= 0) {
            return false;
        }
        return in_array($moduleId, $this->getModulesExcluded($studentId), true);
    }

    /**
     * @param int $studentId
     * @param int[] $moduleIds
     * @return bool
     */
    public function setModulesExcluded($studentId, array $moduleIds)
    {
        $studentId = intval($studentId);
        if ($studentId <= 0) {
            return false;
        }
        $clean = array();
        foreach ($moduleIds as $id) {
            $id = intval($id);
            if ($id > 0) {
                $clean[$id] = $id;
            }
        }
        $csv = count($clean) ? implode(',', array_values($clean)) : '';
        $record = array('ModulesExcluded' => $csv);
        return (bool)$this->sqlf->autoExecute(
            'CCEStudents',
            $record,
            'UPDATE',
            'CCEStudentID=' . $studentId,
            'CCEStudentID'
        );
    }

    /**
     * Allowed "Where did you hear about the BCE MDP?" dropdown values.
     *
     * @return string[]
     */
    public static function getBceMarketingSourceOptions()
    {
        return array(
            'CESA.co.za',
            'E-mail advertisement',
            'Word of Mouth',
            'Other',
        );
    }

    /**
     * @param string $source
     * @param string $other
     * @return array{MarketingSource:string,MarketingSourceOther:string}
     */
    public static function normalizeBceMarketingSource($source, $other = '')
    {
        $source = trim((string)$source);
        $other = trim((string)$other);
        $allowed = self::getBceMarketingSourceOptions();
        if (!in_array($source, $allowed, true)) {
            return array(
                'MarketingSource' => '',
                'MarketingSourceOther' => '',
            );
        }
        if ($source !== 'Other') {
            $other = '';
        }
        return array(
            'MarketingSource' => $source,
            'MarketingSourceOther' => $other,
        );
    }

    /**
     * @param string $source
     * @param string $other
     * @return string|null Error message or null if valid
     */
    public static function validateBceMarketingSource($source, $other = '')
    {
        $normalized = self::normalizeBceMarketingSource($source, $other);
        if ($normalized['MarketingSource'] === '') {
            return 'Please tell us where you heard about the BCE MDP.';
        }
        if ($normalized['MarketingSource'] === 'Other' && $normalized['MarketingSourceOther'] === '') {
            return 'Please specify where you heard about the BCE MDP.';
        }
        return null;
    }

    /**
     * @param string $source
     * @param string $other
     * @return string
     */
    public static function formatBceMarketingSourceForDisplay($source, $other = '')
    {
        $normalized = self::normalizeBceMarketingSource($source, $other);
        if ($normalized['MarketingSource'] === '') {
            return '';
        }
        if ($normalized['MarketingSource'] === 'Other') {
            return 'Other: ' . $normalized['MarketingSourceOther'];
        }
        return $normalized['MarketingSource'];
    }

}

function studentListOptionsLoad(&$objThis) {

    // ini_set('display_errors', 1);
    // error_reporting(E_ALL);

    // Set all action items to display on left
    $objThis->ListOptions->Items["view"]->OnLeft = TRUE;
    // $objThis->ListOptions->Items["edit"]->OnLeft = TRUE; 
    // $objThis->ListOptions->Items["copy"]->OnLeft = TRUE;
    // $objThis->ListOptions->Items["delete"]->OnLeft = TRUE;
    
    // Move all items to beginning of list
    $objThis->ListOptions->MoveItem("view", 0);
    // $objThis->ListOptions->MoveItem("edit", 1);
    // $objThis->ListOptions->MoveItem("copy", 2);
    // $objThis->ListOptions->MoveItem("delete", 3);

    // Add extras column (keep existing extras code)
    $objThis->ListOptions->Add("extras");
    $objThis->ListOptions->Items["extras"]->CssStyle = "white-space: nowrap;";

}