Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,63 @@ <h3 class="modal-sprint-title">${sprintTitle}</h3>
}
}

/**
* ============================================
* AUTO-CREATE ASSIGNMENT FROM FRONTMATTER
* ============================================
*/
async function autoCreateAssignmentIfNeeded() {
// Only proceed if assignment: true but assignment_id is not set
const hasAssignment = {{ page.assignment | default: false }};
const hasAssignmentId = ASSIGNMENT_ID !== null;

if (!hasAssignment || hasAssignmentId) {
return; // Already has ID or not an assignment page
}

try {
// Call the Spring API to auto-create assignment
// This endpoint allows any authenticated user (student, person, teacher, admin)
const createRes = await fetch(`${javaURI}/api/assignments/auto-create`, {
...fetchOptions,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
name: PAGE_TITLE || '{{ page.title }}',
contentUrl: CURRENT_LESSON_ID,
description: '{{ page.description | default: "" }}'
})
});

if (createRes.ok) {
const result = await createRes.json();
// Set global ASSIGNMENT_ID and ASSIGNMENT_NAME for use in submission forms
window.ASSIGNMENT_ID = result.id;
window.ASSIGNMENT_NAME = result.name;

// Distinguish between newly created (201) and already existing (200)
if (createRes.status === 201) {
console.log('✓ Assignment auto-created with ID:', result.id);
} else if (createRes.status === 200) {
console.log('ℹ Assignment already exists with ID:', result.id, '- reusing for this session');
} else {
console.log('Assignment resolved with ID:', result.id);
}
} else if (createRes.status === 403) {
console.warn('Not authenticated - assignment creation skipped. Please log in to enable assignments.');
} else if (createRes.status === 400) {
const error = await createRes.json();
console.error('Bad request for auto-create assignment:', error.error);
} else {
console.error('Failed to auto-create assignment:', createRes.status, createRes.statusText);
}
} catch (error) {
console.error('Error auto-creating assignment:', error);
}
}

/**
* ============================================
* MASTER INITIALIZATION
Expand All @@ -743,6 +800,9 @@ <h3 class="modal-sprint-title">${sprintTitle}</h3>
setupMobileModal();
window.addEventListener('resize', () => setupMobileModal());

// Auto-create assignment if needed
autoCreateAssignmentIfNeeded();

// Assignment submission
setupSubmissionForms();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"description: \"AP CSA 2018 FRQ #4 - 2D Arrays - ArrayTester Class and Latin Squares\"\n",
"permalink: /csa/frqs/2018/4\n",
"author: Database Guys - Yash Parikh\n",
"assignment: true\n",
"---"
]
},
Expand Down