added auto course codes
This commit is contained in:
@@ -61,7 +61,8 @@ if ($course_id) {
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="code">Course Code</label>
|
||||
<input type="text" id="code" name="code" class="form-control" maxlength="12" value="<?php echo $course ? htmlspecialchars($course['code']) : ''; ?>" placeholder="Optional code e.g., CRSE001">
|
||||
<input type="text" id="code" name="code" class="form-control" maxlength="12" value="<?php echo $course ? htmlspecialchars($course['code']) : ''; ?>" placeholder="Optional code e.g., CRSE001" data-manual="0">
|
||||
<small class="form-text text-muted">Auto-generated from type + date (you can edit manually)</small>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -151,4 +152,70 @@ if ($course_id) {
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
// Auto-generate course code from type and date: ABBREVIATION_MMDD
|
||||
(function(){
|
||||
var typeMap = {
|
||||
'driver_training': 'DRVTRN',
|
||||
'bush_mechanics': 'BUSHMEC',
|
||||
'rescue_recovery': 'RESREC',
|
||||
'ladies_driver_training': 'LADYTRN'
|
||||
};
|
||||
|
||||
var $type = document.getElementById('course_type');
|
||||
var $date = document.getElementById('date');
|
||||
var $code = document.getElementById('code');
|
||||
|
||||
function getMMDDFromISO(isoDate) {
|
||||
if (!isoDate) return '';
|
||||
// expecting YYYY-MM-DD
|
||||
var parts = isoDate.split('-');
|
||||
if (parts.length !== 3) return '';
|
||||
return parts[1] + parts[2];
|
||||
}
|
||||
|
||||
function generateCode() {
|
||||
try {
|
||||
var manual = $code.getAttribute('data-manual') === '1';
|
||||
if (manual) return; // user has manually edited
|
||||
var t = $type.value;
|
||||
var d = $date.value;
|
||||
if (!t || !d) return;
|
||||
var abbr = typeMap[t] || t.toUpperCase().replace(/[^A-Z0-9]/g,'').substring(0,7);
|
||||
var mmdd = getMMDDFromISO(d);
|
||||
if (!mmdd) return;
|
||||
var newCode = abbr + '_' + mmdd;
|
||||
$code.value = newCode;
|
||||
} catch (e) {
|
||||
console.error('generateCode error', e);
|
||||
}
|
||||
}
|
||||
|
||||
// mark manual when user types
|
||||
$code.addEventListener('input', function(){
|
||||
var val = $code.value.trim();
|
||||
if (val.length === 0) {
|
||||
$code.setAttribute('data-manual','0');
|
||||
} else {
|
||||
// if value matches auto pattern for currently selected type+date, keep as auto; otherwise mark manual
|
||||
var expected = '';
|
||||
try { expected = (typeMap[$type.value] || $type.value.toUpperCase().replace(/[^A-Z0-9]/g,'').substring(0,7)) + '_' + ( ($date.value) ? $date.value.split('-')[1] + $date.value.split('-')[2] : '' ); } catch(e){ expected=''; }
|
||||
if (val === expected) {
|
||||
$code.setAttribute('data-manual','0');
|
||||
} else {
|
||||
$code.setAttribute('data-manual','1');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$type.addEventListener('change', generateCode);
|
||||
$date.addEventListener('change', generateCode);
|
||||
|
||||
// generate on load if empty
|
||||
if ($code.value.trim().length === 0) {
|
||||
generateCode();
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php include_once($rootPath . '/components/insta_footer.php'); ?>
|
||||
|
||||
@@ -43,6 +43,22 @@ try {
|
||||
throw new Exception('Invalid date format');
|
||||
}
|
||||
|
||||
// If code not provided, generate from type + date using ABBR_MMDD format
|
||||
if (empty($code)) {
|
||||
$abbrMap = [
|
||||
'driver_training' => 'DRVTRN',
|
||||
'bush_mechanics' => 'BUSHMEC',
|
||||
'rescue_recovery' => 'RESREC',
|
||||
'ladies_driver_training' => 'LADYTRN'
|
||||
];
|
||||
|
||||
$abbr = $abbrMap[$course_type] ?? strtoupper(preg_replace('/[^A-Z0-9]/', '', $course_type));
|
||||
// ensure abbr fits (reserve 1 char for underscore and 4 for MMDD)
|
||||
$abbr = substr($abbr, 0, 7);
|
||||
$mmdd = date('md', strtotime($date));
|
||||
$code = strtoupper(substr($abbr . '_' . $mmdd, 0, 12));
|
||||
}
|
||||
|
||||
if ($capacity <= 0) {
|
||||
throw new Exception('Capacity must be greater than 0');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user