Skip to content

Commit 7fb98fe

Browse files
committed
MOBILE-3890 course: Check forced course language and apply it
1 parent 90cb84c commit 7fb98fe

File tree

13 files changed

+201
-25
lines changed

13 files changed

+201
-25
lines changed

src/addons/mod/assign/services/handlers/prefetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export class AddonModAssignPrefetchHandlerService extends CoreCourseActivityPref
256256

257257
promises.push(CoreCourse.getModuleBasicInfoByInstance(assign.id, ADDON_MOD_ASSIGN_MODNAME, { siteId }));
258258
// Get course data, needed to determine upload max size if it's configured to be course limit.
259-
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
259+
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, { siteId })));
260260

261261
// Download intro files and attachments. Do not call getFiles because it'd call some WS twice.
262262
let files: CoreWSFile[] = assign.introattachments || [];

src/addons/mod/data/services/handlers/prefetch-lazy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class AddonModDataPrefetchHandlerLazyService extends AddonModDataPrefetch
262262
promises.push(CoreCourse.getModuleBasicInfoByInstance(database.id, ADDON_MOD_DATA_MODNAME, { siteId }));
263263

264264
// Get course data, needed to determine upload max size if it's configured to be course limit.
265-
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
265+
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, { siteId })));
266266

267267
await Promise.all(promises);
268268
}

src/addons/mod/forum/services/handlers/prefetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class AddonModForumPrefetchHandlerService extends CoreCourseActivityPrefe
250250
promises.push(AddonModForum.getAccessInformation(forum.id, modOptions));
251251

252252
// Get course data, needed to determine upload max size if it's configured to be course limit.
253-
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
253+
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, { siteId })));
254254

255255
await Promise.all(promises);
256256
}

src/addons/mod/glossary/services/handlers/prefetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class AddonModGlossaryPrefetchHandlerService extends CoreCourseActivityPr
191191
promises.push(CoreCourse.getModuleBasicInfoByInstance(glossary.id, ADDON_MOD_GLOSSARY_MODNAME, { siteId }));
192192

193193
// Get course data, needed to determine upload max size if it's configured to be course limit.
194-
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
194+
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, { siteId })));
195195

196196
await Promise.all(promises);
197197
}

src/addons/mod/quiz/services/handlers/prefetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet
321321
promises.push(AddonModQuiz.getAttemptAccessInformation(quiz.id, 0, modOptions)); // Last attempt.
322322

323323
// Get course data, needed to determine upload max size if it's configured to be course limit.
324-
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
324+
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, { siteId })));
325325

326326
await Promise.all(promises);
327327

src/addons/mod/workshop/services/handlers/prefetch-lazy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class AddonModWorkshopPrefetchHandlerLazyService extends AddonModWorkshop
394394
promises.push(CoreCourse.getModuleBasicGradeInfo(module.id, siteId));
395395

396396
// Get course data, needed to determine upload max size if it's configured to be course limit.
397-
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
397+
promises.push(CorePromiseUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, { siteId })));
398398

399399
await Promise.all(promises);
400400

src/core/features/course/course-lazy.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,22 @@ function buildRoutes(injector: Injector): Routes {
4141
},
4242
...indexRoutes.siblings,
4343
],
44+
data: { checkForcedLanguage: 'course' },
4445
},
4546
{
4647
path: ':courseId/:cmId/module-preview',
4748
loadComponent: () => import('@features/course/pages/module-preview/module-preview'),
49+
data: { checkForcedLanguage: 'course' },
4850
},
4951
{
5052
path: ':courseId/list-mod-type',
5153
loadComponent: () => import('@features/course/pages/list-mod-type/list-mod-type'),
54+
data: { checkForcedLanguage: 'course' },
5255
},
5356
{
5457
path: ':courseId/summary',
5558
loadComponent: () => CoreCourseHelper.getCourseSummaryPage(),
59+
data: { checkForcedLanguage: 'course' },
5660
},
5761
];
5862
}

src/core/features/course/course.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { CoreCourseOptionsDelegate } from '@features/course/services/course-opti
3636
import { CoreCourseOverviewOptionHandler } from './services/handlers/overview-option';
3737
import { CoreCourseOverviewLinkHandler } from './services/handlers/overview-link';
3838
import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate';
39+
import { CoreCourseForceLanguage } from './services/course-force-language';
3940

4041
/**
4142
* Get course services.
@@ -186,6 +187,7 @@ const courseIndexRoutes: Routes = [
186187
CoreContentLinksDelegate.registerHandler(CoreCourseOverviewLinkHandler.instance);
187188

188189
CoreCourse.initialize();
190+
CoreCourseForceLanguage.initialize();
189191
CoreCourseModulePrefetchDelegate.initialize();
190192
}),
191193
],
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { Injectable } from '@angular/core';
16+
import { ActivatedRoute, NavigationEnd } from '@angular/router';
17+
18+
import { CoreSitesReadingStrategy } from '@services/sites';
19+
import { makeSingleton, Router } from '@singletons';
20+
import {
21+
CoreCourses,
22+
CoreCourseSearchedData,
23+
} from '../../courses/services/courses';
24+
import { CoreNavigator } from '@services/navigator';
25+
import { filter } from 'rxjs/operators';
26+
import { CorePromiseUtils } from '@singletons/promise-utils';
27+
import { CoreLang } from '@services/lang';
28+
29+
/**
30+
* Service that provides some features course and module forced languages.
31+
*/
32+
@Injectable({ providedIn: 'root' })
33+
export class CoreCourseForceLanguageService {
34+
35+
protected lastNavigationCheck: {
36+
courseId: number;
37+
courseLang: string | undefined;
38+
timestamp: number;
39+
}= {
40+
courseId: 0,
41+
courseLang: undefined,
42+
timestamp: 0,
43+
};
44+
45+
/**
46+
* Initialize.
47+
*/
48+
initialize(): void {
49+
Router.events
50+
.pipe(filter(event => event instanceof NavigationEnd))
51+
.subscribe(async () => {
52+
this.lastNavigationCheck.timestamp = Date.now();
53+
const currentNavigationCheck = this.lastNavigationCheck.timestamp;
54+
55+
let route: ActivatedRoute | null = CoreNavigator.getCurrentRoute();
56+
let routeData = CoreNavigator.getRouteData(route);
57+
while (!routeData.checkForcedLanguage && route) {
58+
route = route.parent;
59+
routeData = route ? CoreNavigator.getRouteData(route) : {};
60+
}
61+
62+
let lang: string | undefined = undefined;
63+
if (route && routeData.checkForcedLanguage) {
64+
lang = await this.getForcedLanguageFromRoute();
65+
}
66+
67+
if (currentNavigationCheck === this.lastNavigationCheck.timestamp) {
68+
await CoreLang.forceCourseLanguage(lang);
69+
}
70+
});
71+
}
72+
73+
/**
74+
* Get forced language from route.
75+
*
76+
* @returns Language code if forced.
77+
*/
78+
protected async getForcedLanguageFromRoute(): Promise<string | undefined> {
79+
let course = CoreNavigator.getRouteParam<CoreCourseSearchedData>('course');
80+
if (course?.lang !== undefined) {
81+
this.lastNavigationCheck.courseLang = course.lang;
82+
this.lastNavigationCheck.courseId = course.id;
83+
84+
return course.lang;
85+
}
86+
87+
const courseId = CoreNavigator.getRouteNumberParam('courseId');
88+
if (!courseId) {
89+
// Not in a course/module, empty the cache and return.
90+
this.lastNavigationCheck.courseId = 0;
91+
this.lastNavigationCheck.courseLang = undefined;
92+
93+
return;
94+
}
95+
96+
if (this.lastNavigationCheck.courseId === courseId) {
97+
return this.lastNavigationCheck.courseLang;
98+
}
99+
this.lastNavigationCheck.courseId = courseId;
100+
101+
course = await CorePromiseUtils.ignoreErrors(
102+
CoreCourses.getCourseByField('id', courseId, { readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }),
103+
);
104+
105+
this.lastNavigationCheck.courseLang = course?.lang;
106+
107+
return course?.lang;
108+
}
109+
110+
}
111+
export const CoreCourseForceLanguage = makeSingleton(CoreCourseForceLanguageService);

src/core/features/course/services/course-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ export class CoreCourseHelperProvider {
967967
// Not enrolled or an error happened. Try to use another WebService.
968968
}
969969

970-
const course = await CoreCourses.getCourseByField('id', courseId, siteId);
970+
const course = await CoreCourses.getCourseByField('id', courseId, { siteId });
971971

972972
return ({ enrolled: false, course: course });
973973
}
@@ -2037,7 +2037,7 @@ export class CoreCourseHelperProvider {
20372037
return course.communicationroomurl;
20382038
}
20392039

2040-
course = await CoreCourses.getCourseByField('id', course.id, site.id);
2040+
course = await CoreCourses.getCourseByField('id', course.id, { siteId: site.id });
20412041
if ('communicationroomurl' in course) {
20422042
return course.communicationroomurl;
20432043
}

0 commit comments

Comments
 (0)