From 3b82c9a952630a140431d7dff811ada490109bca Mon Sep 17 00:00:00 2001 From: Jacob Pierce Date: Tue, 30 Jan 2024 13:53:21 -0800 Subject: [PATCH] wrap selectQuestions in the useQuizCreation context; update auto-fill on numQuestions change --- .../assets/src/composables/useQuizCreation.js | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/kolibri/plugins/coach/assets/src/composables/useQuizCreation.js b/kolibri/plugins/coach/assets/src/composables/useQuizCreation.js index e8f7e46d2e8..652a2625ebd 100644 --- a/kolibri/plugins/coach/assets/src/composables/useQuizCreation.js +++ b/kolibri/plugins/coach/assets/src/composables/useQuizCreation.js @@ -170,13 +170,8 @@ export default function useQuizCreation(DEBUG = false) { } else if (question_count > (targetSection.question_count || 0)) { // If the question_count is being increased, we need to add new questions to the end of the // questions array - const newQuestions = selectQuestions( - question_count - (targetSection.question_count || 0), - targetSection.resource_pool.map(r => r.content_id), - targetSection.resource_pool.map(r => r.title), - targetSection.resource_pool.map(r => r.questions.map(q => q.question_id)), - get(_quiz).seed - ); + const numQuestionsToAdd = question_count - (targetSection.question_count || 0); + const newQuestions = selectRandomQuestionsFromResources(numQuestionsToAdd); updates.questions = [...targetSection.questions, ...newQuestions]; } } @@ -193,6 +188,23 @@ export default function useQuizCreation(DEBUG = false) { }); } + /** + * @description Selects random questions from the active section's `resource_pool` - no side + * effects + * @param numQuestions + * @returns {QuizQuestion[]} + */ + function selectRandomQuestionsFromResources(numQuestions) { + const pool = get(activeResourcePool); + return selectQuestions( + numQuestions, + pool.map(r => r.content_id), + pool.map(r => r.title), + pool.map(r => r.assessmentmetadata.assessment_item_ids), + get(_quiz).seed + ); + } + /** * @param {QuizQuestion[]} newQuestions * @affects _quiz - Updates the active section's `questions` property @@ -264,6 +276,16 @@ export default function useQuizCreation(DEBUG = false) { setActiveSection(newSection.section_id); } _fetchChannels(); + + // Set watcher once we have a section in place + watch(activeResourcePool, (resourcePool, old) => { + if (!isEqual(resourcePool, old)) { + updateSection({ + section_id: get(_activeSectionId), + questions: selectRandomQuestionsFromResources(get(activeSection).question_count), + }); + } + }); } // // Method to initialize the working resource pool @@ -487,8 +509,6 @@ export default function useQuizCreation(DEBUG = false) { return !get(allQuestionsSelected) && !get(noQuestionsSelected); }); - watch(activeResourcePool, () => {}); - provide('saveQuiz', saveQuiz); provide('initializeWorkingResourcePool', initializeWorkingResourcePool); provide('addToWorkingResourcePool', addToWorkingResourcePool);