Skip to content

Commit

Permalink
MOBILE-4526 filter: Use get_all_states WS if available
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Mar 12, 2024
1 parent 016f32a commit 6563424
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/core/features/filter/services/filter-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { Injectable, ViewContainerRef } from '@angular/core';

import { CoreSites } from '@services/sites';
import { CoreFilter, CoreFilterFilter, CoreFilterFormatTextOptions } from './filter';
import { CoreFilter, CoreFilterFilter, CoreFilterFormatTextOptions, CoreFilterStateValue } from './filter';
import { CoreFilterDefaultHandler } from './handlers/default-filter';
import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
import { CoreSite } from '@classes/sites/site';
Expand Down Expand Up @@ -169,7 +169,7 @@ export class CoreFilterDelegateService extends CoreDelegate<CoreFilterHandler> {
filter: handler.filterName,
inheritedstate: 1,
instanceid: instanceId,
localstate: 1,
localstate: CoreFilterStateValue.ON,
});
}

Expand Down Expand Up @@ -245,7 +245,10 @@ export class CoreFilterDelegateService extends CoreDelegate<CoreFilterHandler> {
skipFilters?: string[],
): boolean {

if (filter.localstate == -1 || (filter.localstate == 0 && filter.inheritedstate == -1)) {
if (
filter.localstate === CoreFilterStateValue.OFF ||
(filter.localstate === CoreFilterStateValue.INHERIT && filter.inheritedstate === CoreFilterStateValue.OFF)
) {
// Filter is disabled, ignore it.
return false;
}
Expand All @@ -255,7 +258,7 @@ export class CoreFilterDelegateService extends CoreDelegate<CoreFilterHandler> {
return false;
}

if (skipFilters && skipFilters.indexOf(filter.filter) != -1) {
if (skipFilters && skipFilters.indexOf(filter.filter) !== -1) {
// Skip this filter.
return false;
}
Expand Down
96 changes: 96 additions & 0 deletions src/core/features/filter/services/filter-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
CoreFilterFormatTextOptions,
CoreFilterClassifiedFilters,
CoreFiltersGetAvailableInContextWSParamContext,
CoreFilterStateValue,
CoreFilterAllStates,
} from './filter';
import { CoreCourse } from '@features/course/services/course';
import { CoreCourses } from '@features/courses/services/courses';
Expand Down Expand Up @@ -198,6 +200,11 @@ export class CoreFilterHelperProvider {
return await CoreFilterDelegate.getEnabledFilters(contextLevel, instanceId);
}

const filters = await this.getFiltersInContextUsingAllStates(contextLevel, instanceId, options, site);
if (filters) {
return filters;
}

const courseId = options.courseId;
let hasFilters = true;

Expand Down Expand Up @@ -238,6 +245,95 @@ export class CoreFilterHelperProvider {
}
}

/**
* Get filters in context using the all states data.
*
* @param contextLevel The context level.
* @param instanceId Instance ID related to the context.
* @param options Options.
* @param site Site.
* @returns Filters, undefined if all states cannot be used.
*/
protected async getFiltersInContextUsingAllStates(
contextLevel: ContextLevel,
instanceId: number,
options: CoreFilterFormatTextOptions = {},
site?: CoreSite,
): Promise<CoreFilterFilter[] | undefined> {
site = site || CoreSites.getCurrentSite();

if (!CoreFilter.canGetAllStatesInSite(site)) {
return;
}

const allStates = await CoreFilter.getAllStates({ siteId: site?.getId() });
if (
contextLevel !== ContextLevel.SYSTEM &&
contextLevel !== ContextLevel.COURSECAT &&
this.hasCategoryOverride(allStates)
) {
// A category has an override, we cannot calculate the right filters for child contexts.
return;
}

const contexts = CoreFilter.getContextsTreeList(contextLevel, instanceId, { courseId: options.courseId });
const contextId = Object.values(allStates[contextLevel]?.[instanceId] ?? {})[0]?.contextid;

const filters: Record<string, CoreFilterFilter> = {};
contexts.reverse().forEach((context) => {
const isParentContext = context.contextLevel !== contextLevel;
const filtersInContext = allStates[context.contextLevel]?.[context.instanceId];
if (!filtersInContext) {
return;
}

for (const name in filtersInContext) {
const filterInContext = filtersInContext[name];
if (filterInContext.localstate === CoreFilterStateValue.DISABLED) {
// Ignore disabled filters to make it consistent with available in context.
continue;
}

filters[name] = {
contextlevel: contextLevel,
instanceid: instanceId,
contextid: contextId,
filter: name,
localstate: isParentContext ? CoreFilterStateValue.INHERIT : filterInContext.localstate,
inheritedstate: isParentContext ?
filterInContext.localstate :
filters[name]?.inheritedstate ?? filterInContext.localstate,
};
}
});

return Object.values(filters);
}

/**
* Check if there is an override for a category in the states of all filters.
*
* @param states States to check.
* @returns True if has category override, false otherwise.
*/
protected hasCategoryOverride(states: CoreFilterAllStates): boolean {
if (!states[ContextLevel.COURSECAT]) {
return false;
}

for (const instanceId in states[ContextLevel.COURSECAT]) {
for (const name in states[ContextLevel.COURSECAT][instanceId]) {
if (
states[ContextLevel.COURSECAT][instanceId][name].localstate !== states[ContextLevel.SYSTEM][0][name].localstate
) {
return true;
}
}
}

return false;
}

/**
* Get filters and format text.
*
Expand Down
Loading

0 comments on commit 6563424

Please sign in to comment.