Skip to content

Commit

Permalink
wrap selectQuestions in the useQuizCreation context; update auto-fill…
Browse files Browse the repository at this point in the history
… on numQuestions change
  • Loading branch information
nucleogenesis committed Jan 30, 2024
1 parent ee329f9 commit 3b82c9a
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions kolibri/plugins/coach/assets/src/composables/useQuizCreation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3b82c9a

Please sign in to comment.