Skip to content

Commit

Permalink
move working resource pool business into ResourceSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
nucleogenesis committed Feb 7, 2024
1 parent 95731e7 commit 29a97ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 92 deletions.
72 changes: 0 additions & 72 deletions kolibri/plugins/coach/assets/src/composables/useQuizCreation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { v4 } from 'uuid';
import isEqual from 'lodash/isEqual';
import uniqWith from 'lodash/uniqWith';
import range from 'lodash/range';
import shuffle from 'lodash/shuffle';
import { enhancedQuizManagementStrings } from 'kolibri-common/strings/enhancedQuizManagementStrings';
Expand Down Expand Up @@ -38,10 +37,6 @@ export default function useQuizCreation(DEBUG = false) {
// Local state
// -----------

/** @type {ComputedRef<QuizExercise[]>} Currently selected resource_pool
* from the side_panel*/
const _working_resource_pool = ref([]);

/** @type {ref<Quiz>}
* The "source of truth" quiz object from which all reactive properties should derive */
const _quiz = ref(objectWithDefaults({}, Quiz));
Expand Down Expand Up @@ -288,12 +283,6 @@ export default function useQuizCreation(DEBUG = false) {
});
}

// // Method to initialize the working resource pool
function initializeWorkingResourcePool() {
// Set the value of _working_resource_pool to the resource_pool of the active section
set(_working_resource_pool, get(activeResourcePool));
}

/**
* @returns {Promise<Quiz>}
* @throws {Error} if quiz is not valid
Expand Down Expand Up @@ -378,11 +367,6 @@ export default function useQuizCreation(DEBUG = false) {
}
}

function resetWorkingResourcePool() {
// Set the WorkingResource to empty array again!
set(_working_resource_pool, []);
}

/**
* @affects _channels - Fetches all channels with exercises and sets them to _channels */
function _fetchChannels() {
Expand Down Expand Up @@ -442,9 +426,6 @@ export default function useQuizCreation(DEBUG = false) {
/** @type {ComputedRef<Array>} A list of all channels available which have exercises */
const channels = computed(() => get(_channels));

// /** @type {ComputedRef<QuizExercise[]>} The current value of _working_resource_pool */
const workingResourcePool = computed(() => get(_working_resource_pool));

/** Handling the Select All Checkbox
* See: remove/toggleQuestionFromSelection() & selectAllQuestions() for more */

Expand Down Expand Up @@ -482,45 +463,12 @@ export default function useQuizCreation(DEBUG = false) {
}
});

/**
* @param {QuizExercise[]} resources
* @affects _working_resource_pool -- Updates it with the given resources and is ensured to have
* a list of unique resources to avoid unnecessary duplication
*/
function addToWorkingResourcePool(resources = []) {
set(_working_resource_pool, uniqWith([...get(_working_resource_pool), ...resources], isEqual));
}

/**
* @param {QuizExercise} content
* @affects _working_resource_pool - Remove given quiz exercise from _working_resource_pool
*/
function removeFromWorkingResourcePool(content) {
set(
_working_resource_pool,
_working_resource_pool.value.filter(obj => obj.id !== content.id)
);
}

/**
* @param {QuizExercise} content
* Check if the content is present in working_resource_pool
*/
function contentPresentInWorkingResourcePool(content) {
const workingResourceIds = get(workingResourcePool).map(wr => wr.id);
return workingResourceIds.includes(content.id);
}

/** @type {ComputedRef<Boolean>} Whether the select all checkbox should be indeterminate */
const selectAllIsIndeterminate = computed(() => {
return !get(allQuestionsSelected) && !get(noQuestionsSelected);
});

provide('saveQuiz', saveQuiz);
provide('initializeWorkingResourcePool', initializeWorkingResourcePool);
provide('addToWorkingResourcePool', addToWorkingResourcePool);
provide('removeFromWorkingResourcePool', removeFromWorkingResourcePool);
provide('contentPresentInWorkingResourcePool', contentPresentInWorkingResourcePool);
provide('updateSection', updateSection);
provide('replaceSelectedQuestions', replaceSelectedQuestions);
provide('addSection', addSection);
Expand All @@ -530,15 +478,13 @@ export default function useQuizCreation(DEBUG = false) {
provide('updateQuiz', updateQuiz);
provide('addQuestionToSelection', addQuestionToSelection);
provide('removeQuestionFromSelection', removeQuestionFromSelection);
provide('resetWorkingResourcePool', resetWorkingResourcePool);
provide('channels', channels);
provide('quiz', quiz);
provide('allSections', allSections);
provide('activeSection', activeSection);
provide('inactiveSections', inactiveSections);
provide('activeResourcePool', activeResourcePool);
provide('activeResourceMap', activeResourceMap);
provide('workingResourcePool', workingResourcePool);
provide('activeQuestionsPool', activeQuestionsPool);
provide('activeQuestions', activeQuestions);
provide('selectedActiveQuestions', selectedActiveQuestions);
Expand All @@ -550,13 +496,8 @@ export default function useQuizCreation(DEBUG = false) {
return {
// Methods
saveQuiz,
initializeWorkingResourcePool,
removeFromWorkingResourcePool,
addToWorkingResourcePool,
contentPresentInWorkingResourcePool,
updateSection,
replaceSelectedQuestions,
resetWorkingResourcePool,
addSection,
removeSection,
setActiveSection,
Expand All @@ -571,7 +512,6 @@ export default function useQuizCreation(DEBUG = false) {
allSections,
activeSection,
inactiveSections,
workingResourcePool,
activeResourcePool,
activeResourceMap,
activeQuestionsPool,
Expand All @@ -596,14 +536,9 @@ export default function useQuizCreation(DEBUG = false) {

export function injectQuizCreation() {
const saveQuiz = inject('saveQuiz');
const initializeWorkingResourcePool = inject('initializeWorkingResourcePool');
const removeFromWorkingResourcePool = inject('removeFromWorkingResourcePool');
const contentPresentInWorkingResourcePool = inject('contentPresentInWorkingResourcePool');
const addToWorkingResourcePool = inject('addToWorkingResourcePool');
const updateSection = inject('updateSection');
const replaceSelectedQuestions = inject('replaceSelectedQuestions');
const addSection = inject('addSection');
const resetWorkingResourcePool = inject('resetWorkingResourcePool');
const removeSection = inject('removeSection');
const setActiveSection = inject('setActiveSection');
const initializeQuiz = inject('initializeQuiz');
Expand All @@ -617,7 +552,6 @@ export function injectQuizCreation() {
const inactiveSections = inject('inactiveSections');
const activeResourcePool = inject('activeResourcePool');
const activeResourceMap = inject('activeResourceMap');
const workingResourcePool = inject('workingResourcePool');
const activeQuestionsPool = inject('activeQuestionsPool');
const activeQuestions = inject('activeQuestions');
const selectedActiveQuestions = inject('selectedActiveQuestions');
Expand All @@ -629,14 +563,9 @@ export function injectQuizCreation() {
return {
// Methods
saveQuiz,
initializeWorkingResourcePool,
addToWorkingResourcePool,
contentPresentInWorkingResourcePool,
removeFromWorkingResourcePool,
deleteActiveSelectedQuestions,
selectAllQuestions,
updateSection,
resetWorkingResourcePool,
replaceSelectedQuestions,
addSection,
removeSection,
Expand All @@ -653,7 +582,6 @@ export function injectQuizCreation() {
allSections,
activeSection,
inactiveSections,
workingResourcePool,
activeResourcePool,
activeResourceMap,
activeQuestionsPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@
allSections,
activeSection,
inactiveSections,
workingResourcePool,
activeQuestionsPool,
activeResourceMap,
activeResourcePool,
Expand Down Expand Up @@ -445,7 +444,6 @@
allSections,
activeSection,
inactiveSections,
workingResourcePool,
activeResourceMap,
activeResourcePool,
activeQuestionsPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@

<script>
import uniqWith from 'lodash/uniqWith';
import isEqual from 'lodash/isEqual';
import { enhancedQuizManagementStrings } from 'kolibri-common/strings/enhancedQuizManagementStrings';
import { computed, ref, getCurrentInstance, watch } from 'kolibri.lib.vueCompositionApi';
import commonCoreStrings from 'kolibri.coreVue.mixins.commonCoreStrings';
Expand All @@ -114,23 +116,8 @@
const store = getCurrentInstance().proxy.$store;
const route = computed(() => store.state.route);
const topicId = computed(() => route.value.params.topic_id);
// We use this query parameter to decide if we want to show the Bookmarks Card
// or the actual exercises that are bookmarked and can be selected
// to be added to Quiz Section.
const showBookmarks = computed(() => route.value.query.showBookmarks);
const {
updateSection,
activeSection,
selectAllQuestions,
workingResourcePool,
addToWorkingResourcePool,
removeFromWorkingResourcePool,
resetWorkingResourcePool,
contentPresentInWorkingResourcePool,
initializeWorkingResourcePool,
} = injectQuizCreation();
const { updateSection, activeResourcePool, selectAllQuestions } = injectQuizCreation();
initializeWorkingResourcePool();
const {
sectionSettings$,
selectFromBookmarks$,
Expand All @@ -151,7 +138,43 @@
const { windowIsSmall } = useKResponsiveWindow();
//const { channels, loading, bookmarks, contentList } = useExerciseResources();
/**
* @type {Ref<QuizExercise[]>} - The uncommitted version of the section's resource_pool
*/
const workingResourcePool = ref(activeResourcePool.value);
/**
* @param {QuizExercise[]} resources
* @affects workingResourcePool -- Updates it with the given resources and is ensured to have
* a list of unique resources to avoid unnecessary duplication
*/
function addToWorkingResourcePool(resources = []) {
workingResourcePool.value = uniqWith([...workingResourcePool.value, ...resources], isEqual);
}
/**
* @param {QuizExercise} content
* @affects workingResourcePool - Remove given quiz exercise from workingResourcePool
*/
function removeFromWorkingResourcePool(content) {
workingResourcePool.value = workingResourcePool.value.filter(obj => obj.id !== content.id);
}
/**
* @affects workingResourcePool - Resets the workingResourcePool to the previous state
*/
function resetWorkingResourcePool() {
workingResourcePool.value = activeResourcePool.value;
}
/**
* @param {QuizExercise} content
* Check if the content is present in workingResourcePool
*/
function contentPresentInWorkingResourcePool(content) {
const workingResourceIds = workingResourcePool.value.map(wr => wr.id);
return workingResourceIds.includes(content.id);
}
const {
hasCheckbox,
Expand Down Expand Up @@ -262,7 +285,6 @@
channels,
viewMoreButtonState,
updateSection,
activeSection,
selectAllQuestions,
workingResourcePool,
addToWorkingResourcePool,
Expand Down

0 comments on commit 29a97ba

Please sign in to comment.