Skip to content

Commit a169d93

Browse files
committed
MOBILE-4660 course: Refactor how subsections are handled
This commit doesn't refactor course downloads yet, it will be done in another commit.
1 parent b5b44a8 commit a169d93

File tree

28 files changed

+654
-624
lines changed

28 files changed

+654
-624
lines changed

src/addons/block/activitymodules/components/activitymodules/activitymodules.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,41 @@ export class AddonBlockActivityModulesComponent extends CoreBlockBaseComponent i
7070
let modFullNames: Record<string, string> = {};
7171
const brandedIcons: Record<string, boolean|undefined> = {};
7272

73-
sections.forEach((section) => {
74-
if (!section.modules) {
73+
const modules = CoreCourseHelper.getSectionsModules(sections, {
74+
ignoreSection: section => !CoreCourseHelper.canUserViewSection(section),
75+
ignoreModule: module => !CoreCourseHelper.canUserViewModule(module) || !CoreCourse.moduleHasView(module),
76+
});
77+
78+
modules.forEach((mod) => {
79+
if (archetypes[mod.modname] !== undefined) {
7580
return;
7681
}
7782

78-
section.modules.forEach((mod) => {
79-
if (archetypes[mod.modname] !== undefined ||
80-
!CoreCourseHelper.canUserViewModule(mod, section) ||
81-
!CoreCourse.moduleHasView(mod)) {
82-
// Ignore this module.
83-
return;
84-
}
85-
86-
// Get the archetype of the module type.
87-
archetypes[mod.modname] = CoreCourseModuleDelegate.supportsFeature<number>(
88-
mod.modname,
89-
CoreConstants.FEATURE_MOD_ARCHETYPE,
90-
CoreConstants.MOD_ARCHETYPE_OTHER,
91-
);
92-
93-
// Get the full name of the module type.
94-
if (archetypes[mod.modname] === CoreConstants.MOD_ARCHETYPE_RESOURCE) {
95-
// All resources are gathered in a single "Resources" option.
96-
if (!modFullNames['resources']) {
97-
modFullNames['resources'] = Translate.instant('core.resources');
98-
}
99-
} else {
100-
modFullNames[mod.modname] = mod.modplural;
83+
// Get the archetype of the module type.
84+
archetypes[mod.modname] = CoreCourseModuleDelegate.supportsFeature<number>(
85+
mod.modname,
86+
CoreConstants.FEATURE_MOD_ARCHETYPE,
87+
CoreConstants.MOD_ARCHETYPE_OTHER,
88+
);
89+
90+
// Get the full name of the module type.
91+
if (archetypes[mod.modname] === CoreConstants.MOD_ARCHETYPE_RESOURCE) {
92+
// All resources are gathered in a single "Resources" option.
93+
if (!modFullNames['resources']) {
94+
modFullNames['resources'] = Translate.instant('core.resources');
10195
}
96+
} else {
97+
modFullNames[mod.modname] = mod.modplural;
98+
}
10299

103-
brandedIcons[mod.modname] = mod.branded;
100+
brandedIcons[mod.modname] = mod.branded;
104101

105-
// If this is not a theme image, leave it undefined to avoid having specific activity icons.
106-
if (CoreUrl.isThemeImageUrl(mod.modicon)) {
107-
modIcons[mod.modname] = mod.modicon;
108-
}
109-
});
102+
// If this is not a theme image, leave it undefined to avoid having specific activity icons.
103+
if (CoreUrl.isThemeImageUrl(mod.modicon)) {
104+
modIcons[mod.modname] = mod.modicon;
105+
}
110106
});
107+
111108
// Sort the modnames alphabetically.
112109
modFullNames = CoreUtils.sortValues(modFullNames);
113110
for (const modName in modFullNames) {

src/addons/block/sitemainmenu/components/sitemainmenu/addon-block-sitemainmenu.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ <h2>{{ 'addon.block_sitemainmenu.pluginname' | translate }}</h2>
1212
</ion-label>
1313
</ion-item>
1414

15-
<core-course-module *ngFor="let module of mainMenuBlock.modules" [module]="module" [section]="mainMenuBlock" />
15+
<ng-container *ngFor="let modOrSubsection of mainMenuBlock.contents">
16+
<core-course-module *ngIf="isModule(modOrSubsection)" [module]="modOrSubsection" [section]="mainMenuBlock" />
17+
</ng-container>
1618
</ion-list>
1719
</core-loading>

src/addons/block/sitemainmenu/components/sitemainmenu/sitemainmenu.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import { Component, OnInit } from '@angular/core';
1616
import { CoreSites } from '@services/sites';
17-
import { CoreCourse } from '@features/course/services/course';
17+
import { CoreCourse, sectionContentIsModule } from '@features/course/services/course';
1818
import { CoreCourseHelper, CoreCourseSection } from '@features/course/services/course-helper';
1919
import { CoreSiteHome, FrontPageItemNames } from '@features/sitehome/services/sitehome';
2020
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
@@ -39,6 +39,7 @@ export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent impl
3939
component = 'AddonBlockSiteMainMenu';
4040
mainMenuBlock?: CoreCourseSection;
4141
siteHomeId = 1;
42+
isModule = sectionContentIsModule;
4243

4344
protected fetchContentDefaultError = 'Error getting main menu data.';
4445

@@ -66,9 +67,12 @@ export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent impl
6667
promises.push(CoreCourse.invalidateSections(this.siteHomeId));
6768
promises.push(CoreSiteHome.invalidateNewsForum(this.siteHomeId));
6869

69-
if (this.mainMenuBlock && this.mainMenuBlock.modules) {
70+
if (this.mainMenuBlock?.contents.length) {
7071
// Invalidate modules prefetch data.
71-
promises.push(CoreCourseModulePrefetchDelegate.invalidateModules(this.mainMenuBlock.modules, this.siteHomeId));
72+
promises.push(CoreCourseModulePrefetchDelegate.invalidateModules(
73+
CoreCourse.getSectionsModules([this.mainMenuBlock]),
74+
this.siteHomeId,
75+
));
7276
}
7377

7478
await Promise.all(promises);
@@ -114,11 +118,11 @@ export class AddonBlockSiteMainMenuComponent extends CoreBlockBaseComponent impl
114118
try {
115119
const forum = await CoreSiteHome.getNewsForum(this.siteHomeId);
116120
// Search the module that belongs to site news.
117-
const forumIndex =
118-
this.mainMenuBlock.modules.findIndex((mod) => mod.modname == 'forum' && mod.instance == forum.id);
121+
const forumIndex = this.mainMenuBlock.contents.findIndex((mod) =>
122+
sectionContentIsModule(mod) && mod.modname == 'forum' && mod.instance == forum.id);
119123

120124
if (forumIndex >= 0) {
121-
this.mainMenuBlock.modules.splice(forumIndex, 1);
125+
this.mainMenuBlock.contents.splice(forumIndex, 1);
122126
}
123127
} catch {
124128
// Ignore errors.

src/addons/mod/subsection/services/handlers/index-link.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { CoreCourse } from '@features/course/services/course';
1919
import { CoreLoadings } from '@services/loadings';
2020
import { CoreDomUtils } from '@services/utils/dom';
2121
import { makeSingleton } from '@singletons';
22-
import { AddonModSubsection } from '../subsection';
22+
import { CoreSites } from '@services/sites';
23+
import { CoreCourseHelper } from '@features/course/services/course-helper';
2324

2425
/**
2526
* Handler to treat links to subsection.
@@ -33,6 +34,29 @@ export class AddonModSubsectionIndexLinkHandlerService extends CoreContentLinksM
3334
super('AddonModSubsection', 'subsection', 'id');
3435
}
3536

37+
/**
38+
* Open a subsection.
39+
*
40+
* @param sectionId Section ID.
41+
* @param courseId Course ID.
42+
* @param siteId Site ID. If not defined, current site.
43+
* @returns Promise resolved when done.
44+
*/
45+
async openSubsection(sectionId: number, courseId: number, siteId?: string): Promise<void> {
46+
const pageParams = {
47+
sectionId,
48+
};
49+
50+
if (
51+
(!siteId || siteId === CoreSites.getCurrentSiteId()) &&
52+
CoreCourse.currentViewIsCourse(courseId)
53+
) {
54+
CoreCourse.selectCourseTab('', pageParams);
55+
} else {
56+
await CoreCourseHelper.getAndOpenCourse(courseId, pageParams, siteId);
57+
}
58+
}
59+
3660
/**
3761
* @inheritdoc
3862
*/
@@ -51,7 +75,7 @@ export class AddonModSubsectionIndexLinkHandlerService extends CoreContentLinksM
5175
// Get the module.
5276
const module = await CoreCourse.getModule(moduleId, courseId, undefined, true, false, siteId);
5377

54-
await AddonModSubsection.openSubsection(module.section, module.course, siteId);
78+
await this.openSubsection(module.section, module.course, siteId);
5579
} catch (error) {
5680
CoreDomUtils.showErrorModalDefault(error, 'Error opening link.');
5781
} finally {

src/addons/mod/subsection/services/handlers/module.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/addons/mod/subsection/services/subsection.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/addons/mod/subsection/subsection.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

1515
import { APP_INITIALIZER, NgModule } from '@angular/core';
1616
import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate';
17-
import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate';
1817
import { AddonModSubsectionIndexLinkHandler } from './services/handlers/index-link';
19-
import { AddonModSubsectionModuleHandler } from './services/handlers/module';
2018

2119
@NgModule({
2220
providers: [
2321
{
2422
provide: APP_INITIALIZER,
2523
multi: true,
2624
useValue: () => {
27-
CoreCourseModuleDelegate.registerHandler(AddonModSubsectionModuleHandler.instance);
2825
CoreContentLinksDelegate.registerHandler(AddonModSubsectionIndexLinkHandler.instance);
2926
},
3027
},

src/addons/storagemanager/pages/courses-storage/courses-storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class AddonStorageManagerCoursesStoragePage implements OnInit, OnDestroy
240240
*/
241241
private async calculateDownloadedCourseSize(courseId: number): Promise<number> {
242242
const sections = await CoreCourse.getSections(courseId);
243-
const modules = sections.map((section) => section.modules).flat();
243+
const modules = CoreCourseHelper.getSectionsModules(sections);
244244
const promisedModuleSizes = modules.map(async (module) => {
245245
const size = await CoreCourseModulePrefetchDelegate.getModuleStoredSize(module, courseId);
246246

src/core/features/course/components/course-format/course-format.html

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<core-dynamic-component [component]="singleSectionComponent" [data]="data">
1313
<ion-accordion-group [multiple]="true" (ionChange)="accordionMultipleChange($event.detail)"
1414
[value]="accordionMultipleValue">
15-
<core-course-section *ngIf="!selectedSection.hiddenbynumsections && selectedSection.id !== stealthModulesSectionId &&
16-
!selectedSection.component" [course]="course" [section]="selectedSection" [lastModuleViewed]="lastModuleViewed"
17-
[viewedModules]="viewedModules" [collapsible]="false" [subSections]="subSections" />
15+
<core-course-section *ngIf="!selectedSection.hiddenbynumsections && selectedSection.id !== stealthModulesSectionId"
16+
[course]="course" [section]="selectedSection" [lastModuleViewed]="lastModuleViewed" [viewedModules]="viewedModules"
17+
[collapsible]="false" />
1818
</ion-accordion-group>
1919
<core-empty-box *ngIf="!selectedSection.hasContent" icon="fas-table-cells-large"
2020
[message]="'core.course.nocontentavailable' | translate" />
@@ -27,12 +27,11 @@
2727
<ion-accordion-group [multiple]="true" (ionChange)="accordionMultipleChange($event.detail)"
2828
[value]="accordionMultipleValue">
2929
@for (section of sections; track section.id) {
30-
@if ($index <= lastShownSectionIndex && !section.hiddenbynumsections && section.id !== allSectionsId &&
31-
section.id !== stealthModulesSectionId && !section.component) {
32-
<core-course-section
33-
[course]="course" [section]="section" [lastModuleViewed]="lastModuleViewed" [viewedModules]="viewedModules"
34-
[collapsible]="true" [subSections]="subSections" />
35-
}
30+
@if ($index
31+
<= lastShownSectionIndex && !section.hiddenbynumsections && section.id !==allSectionsId && section.id
32+
!==stealthModulesSectionId) { <core-course-section [course]="course" [section]="section"
33+
[lastModuleViewed]="lastModuleViewed" [viewedModules]="viewedModules" [collapsible]="true" />
34+
}
3635
}
3736
</ion-accordion-group>
3837
</core-dynamic-component>

0 commit comments

Comments
 (0)