MOBILE-4653 courses: Move CoreCoursesProvider constants

main
Pau Ferrer Ocaña 2024-11-06 10:24:32 +01:00
parent dba799ff90
commit 8846de838d
16 changed files with 214 additions and 76 deletions

View File

@ -17,7 +17,6 @@ import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreTimeUtils } from '@services/utils/time';
import { CoreSites, CoreSitesReadingStrategy } from '@services/sites';
import {
CoreCoursesProvider,
CoreCourses,
CoreCoursesMyCoursesUpdatedEventData,
CoreCourseSummaryData,
@ -38,6 +37,12 @@ import { PageLoadsManager } from '@classes/page-loads-manager';
import { DownloadStatus } from '@/core/constants';
import { CoreSharedModule } from '@/core/shared.module';
import { CoreCoursesComponentsModule } from '@features/courses/components/components.module';
import {
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_FAVOURITE,
CORE_COURSES_STATE_HIDDEN,
} from '@features/courses/constants';
const FILTER_PRIORITY: AddonBlockMyOverviewTimeFilters[] =
['all', 'inprogress', 'future', 'past', 'favourite', 'allincludinghidden', 'hidden'];
@ -137,7 +142,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
}, CoreSites.getCurrentSiteId());
this.coursesObserver = CoreEvents.on(
CoreCoursesProvider.EVENT_MY_COURSES_UPDATED,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
(data) => {
this.refreshCourseList(data);
},
@ -428,29 +433,29 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
}
/**
* Refresh course list based on a EVENT_MY_COURSES_UPDATED event.
* Refresh course list based on a CORE_COURSES_MY_COURSES_UPDATED_EVENT event.
*
* @param data Event data.
* @returns Promise resolved when done.
*/
protected async refreshCourseList(data: CoreCoursesMyCoursesUpdatedEventData): Promise<void> {
if (data.action == CoreCoursesProvider.ACTION_ENROL) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.ENROL) {
// Always update if user enrolled in a course.
return this.refreshContent(true);
}
const course = this.allCourses.find((course) => course.id == data.courseId);
if (data.action == CoreCoursesProvider.ACTION_STATE_CHANGED) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED) {
if (!course) {
// Not found, use WS update.
return this.refreshContent(true);
}
if (data.state == CoreCoursesProvider.STATE_FAVOURITE) {
if (data.state === CORE_COURSES_STATE_FAVOURITE) {
course.isfavourite = !!data.value;
}
if (data.state == CoreCoursesProvider.STATE_HIDDEN) {
if (data.state === CORE_COURSES_STATE_HIDDEN) {
course.hidden = !!data.value;
}
@ -458,7 +463,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
await this.filterCourses();
}
if (data.action == CoreCoursesProvider.ACTION_VIEW && data.courseId != CoreSites.getCurrentSiteHomeId()) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.VIEW && data.courseId != CoreSites.getCurrentSiteHomeId()) {
if (!course) {
// Not found, use WS update.
return this.refreshContent(true);

View File

@ -16,7 +16,6 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreSites } from '@services/sites';
import {
CoreCoursesProvider,
CoreCoursesMyCoursesUpdatedEventData,
CoreCourses,
CoreCourseSummaryData,
@ -33,6 +32,11 @@ import { CoreUtils } from '@services/utils/utils';
import { CoreSite } from '@classes/sites/site';
import { CoreSharedModule } from '@/core/shared.module';
import { CoreCoursesComponentsModule } from '@features/courses/components/components.module';
import {
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_FAVOURITE,
} from '@features/courses/constants';
/**
* Component to render a recent courses block.
@ -73,7 +77,7 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
this.scrollElementId = `addon-block-recentlyaccessedcourses-scroll-${scrollId}`;
this.coursesObserver = CoreEvents.on(
CoreCoursesProvider.EVENT_MY_COURSES_UPDATED,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
(data) => {
this.refreshCourseList(data);
},
@ -170,20 +174,20 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
}
/**
* Refresh course list based on a EVENT_MY_COURSES_UPDATED event.
* Refresh course list based on a CORE_COURSES_MY_COURSES_UPDATED_EVENT event.
*
* @param data Event data.
* @returns Promise resolved when done.
*/
protected async refreshCourseList(data: CoreCoursesMyCoursesUpdatedEventData): Promise<void> {
if (data.action == CoreCoursesProvider.ACTION_ENROL) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.ENROL) {
// Always update if user enrolled in a course.
return this.refreshContent();
}
const courseIndex = this.courses.findIndex((course) => course.id == data.courseId);
const course = this.courses[courseIndex];
if (data.action == CoreCoursesProvider.ACTION_VIEW && data.courseId != CoreSites.getCurrentSiteHomeId()) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.VIEW && data.courseId != CoreSites.getCurrentSiteHomeId()) {
if (!course) {
// Not found, use WS update.
return this.refreshContent();
@ -196,8 +200,8 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
await this.invalidateCourseList();
}
if (data.action == CoreCoursesProvider.ACTION_STATE_CHANGED &&
data.state == CoreCoursesProvider.STATE_FAVOURITE && course) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED &&
data.state === CORE_COURSES_STATE_FAVOURITE && course) {
course.isfavourite = !!data.value;
await this.invalidateCourseList();
}

View File

@ -15,7 +15,7 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreSites } from '@services/sites';
import { CoreCoursesProvider, CoreCoursesMyCoursesUpdatedEventData, CoreCourses } from '@features/courses/services/courses';
import { CoreCoursesMyCoursesUpdatedEventData, CoreCourses } from '@features/courses/services/courses';
import {
CoreCourseSearchedDataWithExtraInfoAndOptions,
CoreEnrolledCourseDataWithOptions,
@ -28,6 +28,11 @@ import { CoreSite } from '@classes/sites/site';
import { AddonBlockStarredCourse, AddonBlockStarredCourses } from '../../services/starredcourses';
import { CoreSharedModule } from '@/core/shared.module';
import { CoreCoursesComponentsModule } from '@features/courses/components/components.module';
import {
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_FAVOURITE,
} from '@features/courses/constants';
/**
* Component to render a starred courses block.
@ -68,7 +73,7 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im
this.scrollElementId = `addon-block-starredcourses-scroll-${scrollId}`;
this.coursesObserver = CoreEvents.on(
CoreCoursesProvider.EVENT_MY_COURSES_UPDATED,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
(data) => {
this.refreshCourseList(data);
},
@ -157,19 +162,19 @@ export class AddonBlockStarredCoursesComponent extends CoreBlockBaseComponent im
}
/**
* Refresh course list based on a EVENT_MY_COURSES_UPDATED event.
* Refresh course list based on a CORE_COURSES_MY_COURSES_UPDATED_EVENT event.
*
* @param data Event data.
* @returns Promise resolved when done.
*/
protected async refreshCourseList(data: CoreCoursesMyCoursesUpdatedEventData): Promise<void> {
if (data.action == CoreCoursesProvider.ACTION_ENROL) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.ENROL) {
// Always update if user enrolled in a course.
// New courses shouldn't be favourite by default, but just in case.
return this.refreshContent();
}
if (data.action == CoreCoursesProvider.ACTION_STATE_CHANGED && data.state == CoreCoursesProvider.STATE_FAVOURITE) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED && data.state == CORE_COURSES_STATE_FAVOURITE) {
const courseIndex = this.courses.findIndex((course) => course.id == data.courseId);
if (courseIndex < 0) {
// Not found, use WS update. Usually new favourite.

View File

@ -17,11 +17,11 @@ import { CoreEnrolAction, CoreEnrolSelfHandler, CoreEnrolInfoIcon } from '@featu
import { Translate, makeSingleton } from '@singletons';
import { AddonEnrolSelf } from './self';
import { CorePasswordModalResponse } from '@components/password-modal/password-modal';
import { CoreCoursesProvider } from '@features/courses/services/courses';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreEnrol, CoreEnrolEnrolmentMethod } from '@features/enrol/services/enrol';
import { CoreModals } from '@services/modals';
import { CoreLoadings } from '@services/loadings';
import { CORE_COURSES_ENROL_INVALID_KEY } from '@features/courses/constants';
/**
* Enrol handler.
@ -122,7 +122,7 @@ export class AddonEnrolSelfHandlerService implements CoreEnrolSelfHandler {
try {
response.validated = await AddonEnrolSelf.selfEnrol(method.courseid, password, method.id);
} catch (error) {
if (error && error.errorcode === CoreCoursesProvider.ENROL_INVALID_KEY) {
if (error && error.errorcode === CORE_COURSES_ENROL_INVALID_KEY) {
response.validated = false;
response.error = error.message;
} else {

View File

@ -16,7 +16,7 @@ import { CoreCacheUpdateFrequency } from '@/core/constants';
import { Injectable } from '@angular/core';
import { CoreWSError } from '@classes/errors/wserror';
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';
import { CoreCoursesProvider } from '@features/courses/services/courses';
import { CORE_COURSES_ENROL_INVALID_KEY } from '@features/courses/constants';
import { CoreSites } from '@services/sites';
import { CoreStatusWithWarningsWSResponse } from '@services/ws';
import { makeSingleton } from '@singletons';
@ -82,7 +82,7 @@ export class AddonEnrolSelfService {
* @param instanceId Enrol instance ID.
* @param siteId Site ID. If not defined, use current site.
* @returns Promise resolved if the user is enrolled. If the password is invalid, the promise is rejected
* with an object with errorcode = CoreCoursesProvider.ENROL_INVALID_KEY.
* with an object with errorcode = CORE_COURSES_ENROL_INVALID_KEY.
*/
async selfEnrol(courseId: number, password: string = '', instanceId?: number, siteId?: string): Promise<boolean> {
@ -112,7 +112,7 @@ export class AddonEnrolSelfService {
warning.warningcode == '2' || warning.warningcode == '3' || warning.warningcode == '4');
if (warning) {
throw new CoreWSError({ errorcode: CoreCoursesProvider.ENROL_INVALID_KEY, message: warning.message });
throw new CoreWSError({ errorcode: CORE_COURSES_ENROL_INVALID_KEY, message: warning.message });
} else {
throw new CoreWSError(response.warnings[0]);
}

View File

@ -47,7 +47,7 @@ import { getBlockServices } from '@features/block/block.module';
import { getCommentsServices } from '@features/comments/comments.module';
import { getContentLinksExportedObjects, getContentLinksServices } from '@features/contentlinks/contentlinks.module';
import { getCourseExportedObjects, getCourseServices, getCourseStandaloneComponents } from '@features/course/course.module';
import { getCoursesServices } from '@features/courses/courses.module';
import { getCoursesExportedObjects, getCoursesServices } from '@features/courses/courses.module';
import { getEditorServices } from '@features/editor/editor.module';
import { getEnrolServices } from '@features/enrol/enrol.module';
import { getFileUploadedServices } from '@features/fileuploader/fileuploader.module';
@ -419,6 +419,7 @@ export class CoreCompileProvider {
getCoreExportedObjects(),
getCoreErrorsExportedObjects(),
getCourseExportedObjects(),
getCoursesExportedObjects(),
getContentLinksExportedObjects(),
getSitePluginsExportedObjects(),
]);

View File

@ -21,7 +21,6 @@ import {
CoreCourseCustomField,
CoreCourses,
CoreCourseSearchedData,
CoreCoursesProvider,
CoreEnrolledCourseData,
} from '@features/courses/services/courses';
import {
@ -42,6 +41,7 @@ import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
import { CoreEnrolHelper } from '@features/enrol/services/enrol-helper';
import { CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate';
import { CoreEnrol, CoreEnrolEnrolmentMethod } from '@features/enrol/services/enrol';
import { CORE_COURSES_MY_COURSES_UPDATED_EVENT, CoreCoursesMyCoursesUpdatedEventAction } from '@features/courses/constants';
/**
* Page that shows the summary of a course including buttons to enrol and other available options.
@ -391,10 +391,10 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy {
await this.refreshData().finally(() => {
// My courses have been updated, trigger event.
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, {
CoreEvents.trigger(CORE_COURSES_MY_COURSES_UPDATED_EVENT, {
courseId: this.courseId,
course: this.course,
action: CoreCoursesProvider.ACTION_ENROL,
action: CoreCoursesMyCoursesUpdatedEventAction.ENROL,
}, CoreSites.getCurrentSiteId());
});

View File

@ -21,13 +21,13 @@ import {
CoreCourseAnyCourseData,
CoreCourseAnyCourseDataWithOptions,
CoreCourses,
CoreCoursesProvider,
CoreCourseUserAdminOrNavOptionIndexed,
} from '@features/courses/services/courses';
import { CoreCourseAccessDataType } from './course';
import { Params } from '@angular/router';
import { makeSingleton } from '@singletons';
import { CorePromisedValue } from '@classes/promised-value';
import { CORE_COURSES_MY_COURSES_REFRESHED_EVENT } from '@features/courses/constants';
/**
* Interface that all course options handlers must implement.
@ -280,7 +280,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate<CoreCourseOpt
async clearAndInvalidateCoursesOptions(courseId?: number): Promise<void> {
const promises: Promise<void>[] = [];
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_REFRESHED);
CoreEvents.trigger(CORE_COURSES_MY_COURSES_REFRESHED_EVENT);
// Invalidate course enabled data for the handlers that are enabled at site level.
if (courseId) {

View File

@ -39,7 +39,6 @@ import { CoreError } from '@classes/errors/error';
import {
CoreCourseAnyCourseData,
CoreCourses,
CoreCoursesProvider,
} from '../../courses/services/courses';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreWSError } from '@classes/errors/wserror';
@ -64,6 +63,7 @@ import { CoreLoadings } from '@services/loadings';
import { CoreArray } from '@singletons/array';
import { CoreText } from '@singletons/text';
import { ArrayElement } from '@/core/utils/types';
import { CORE_COURSES_MY_COURSES_UPDATED_EVENT, CoreCoursesMyCoursesUpdatedEventAction } from '@features/courses/constants';
const ROOT_CACHE_KEY = 'mmCourse:';
@ -1288,9 +1288,9 @@ export class CoreCourseProvider {
if (!response.status) {
throw Error('WS core_course_view_course failed.');
} else {
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, {
CoreEvents.trigger(CORE_COURSES_MY_COURSES_UPDATED_EVENT, {
courseId: courseId,
action: CoreCoursesProvider.ACTION_VIEW,
action: CoreCoursesMyCoursesUpdatedEventAction.VIEW,
}, site.getId());
}
}

View File

@ -23,13 +23,19 @@ import { CoreDomUtils } from '@services/utils/dom';
import { Translate } from '@singletons';
import { CoreColors } from '@singletons/colors';
import { CoreEventCourseStatusChanged, CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreCourseListItem, CoreCourses, CoreCoursesProvider } from '../../services/courses';
import { CoreCourseListItem, CoreCourses } from '../../services/courses';
import { CoreCoursesHelper, CoreEnrolledCourseDataWithExtraInfoAndOptions } from '../../services/courses-helper';
import { CoreEnrolHelper } from '@features/enrol/services/enrol-helper';
import { CoreDownloadStatusTranslatable } from '@components/download-refresh/download-refresh';
import { toBoolean } from '@/core/transforms/boolean';
import { CorePopovers } from '@services/popovers';
import { CoreLoadings } from '@services/loadings';
import {
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_HIDDEN,
CORE_COURSES_STATE_FAVOURITE,
} from '@features/courses/constants';
/**
* This directive is meant to display an item for a list of courses.
@ -356,11 +362,11 @@ export class CoreCoursesCourseListItemComponent implements OnInit, OnDestroy, On
this.course.hidden = hide;
(<CoreEnrolledCourseDataWithExtraInfoAndOptions> this.course).hidden = hide;
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, {
CoreEvents.trigger(CORE_COURSES_MY_COURSES_UPDATED_EVENT, {
courseId: this.course.id,
course: this.course,
action: CoreCoursesProvider.ACTION_STATE_CHANGED,
state: CoreCoursesProvider.STATE_HIDDEN,
action: CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED,
state: CORE_COURSES_STATE_HIDDEN,
value: hide,
}, CoreSites.getCurrentSiteId());
@ -385,11 +391,11 @@ export class CoreCoursesCourseListItemComponent implements OnInit, OnDestroy, On
await CoreCourses.setFavouriteCourse(this.course.id, favourite);
this.course.isfavourite = favourite;
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, {
CoreEvents.trigger(CORE_COURSES_MY_COURSES_UPDATED_EVENT, {
courseId: this.course.id,
course: this.course,
action: CoreCoursesProvider.ACTION_STATE_CHANGED,
state: CoreCoursesProvider.STATE_FAVOURITE,
action: CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED,
state: CORE_COURSES_STATE_FAVOURITE,
value: favourite,
}, CoreSites.getCurrentSiteId());

View File

@ -0,0 +1,38 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export const CORE_COURSES_ENROL_INVALID_KEY = 'CoreCoursesEnrolInvalidKey';
export const CORE_COURSES_MY_COURSES_CHANGED_EVENT = 'courses_my_courses_changed'; // User course list changed while app is running.
// A course was hidden/favourite, or user enroled in a course.
export const CORE_COURSES_MY_COURSES_UPDATED_EVENT = 'courses_my_courses_updated';
export const CORE_COURSES_MY_COURSES_REFRESHED_EVENT = 'courses_my_courses_refreshed';
export const CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT = 'dashboard_download_enabled_changed';
// Actions for event CORE_COURSES_MY_COURSES_UPDATED_EVENT.
export const CoreCoursesMyCoursesUpdatedEventAction = {
/* eslint-disable @typescript-eslint/naming-convention */
ENROL: 'enrol', // User enrolled in a course.
STATE_CHANGED: 'state_changed', // Course state changed (hidden, favourite).
VIEW: 'view', // Course viewed.
/* eslint-enable @typescript-eslint/naming-convention*/
} as const;
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type CoreCoursesMyCoursesUpdatedEventAction =
typeof CoreCoursesMyCoursesUpdatedEventAction[keyof typeof CoreCoursesMyCoursesUpdatedEventAction];
// Possible states changed.
export const CORE_COURSES_STATE_HIDDEN = 'hidden';
export const CORE_COURSES_STATE_FAVOURITE = 'favourite';

View File

@ -57,6 +57,37 @@ export async function getCoursesServices(): Promise<Type<unknown>[]> {
];
}
/**
* Get courses exported objects.
*
* @returns Courses exported objects.
*/
export async function getCoursesExportedObjects(): Promise<Record<string, unknown>> {
const {
CORE_COURSES_ENROL_INVALID_KEY,
CORE_COURSES_MY_COURSES_CHANGED_EVENT,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CORE_COURSES_MY_COURSES_REFRESHED_EVENT,
CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_HIDDEN,
CORE_COURSES_STATE_FAVOURITE,
} = await import('@features/courses/constants');
/* eslint-disable @typescript-eslint/naming-convention */
return {
CORE_COURSES_ENROL_INVALID_KEY,
CORE_COURSES_MY_COURSES_CHANGED_EVENT,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CORE_COURSES_MY_COURSES_REFRESHED_EVENT,
CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_HIDDEN,
CORE_COURSES_STATE_FAVOURITE,
};
/* eslint-enable @typescript-eslint/naming-convention */
}
const mainMenuHomeChildrenRoutes: Routes = [
{
path: CoreDashboardHomeHandlerService.PAGE_NAME,

View File

@ -16,12 +16,17 @@ import { Component, OnDestroy, OnInit } from '@angular/core';
import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils';
import { CoreCategoryData, CoreCourseListItem, CoreCourses, CoreCoursesProvider } from '../../services/courses';
import { CoreCategoryData, CoreCourseListItem, CoreCourses } from '../../services/courses';
import { Translate } from '@singletons';
import { CoreNavigator } from '@services/navigator';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
import { CoreTime } from '@singletons/time';
import {
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT,
} from '@features/courses/constants';
/**
* Page that displays a list of categories and the courses in the current category if any.
@ -59,9 +64,9 @@ export class CoreCoursesCategoriesPage implements OnInit, OnDestroy {
// Update list if user enrols in a course.
this.myCoursesObserver = CoreEvents.on(
CoreCoursesProvider.EVENT_MY_COURSES_UPDATED,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
(data) => {
if (data.action == CoreCoursesProvider.ACTION_ENROL) {
if (data.action === CoreCoursesMyCoursesUpdatedEventAction.ENROL) {
this.fetchCategories();
}
},
@ -77,7 +82,7 @@ export class CoreCoursesCategoriesPage implements OnInit, OnDestroy {
this.downloadEnabled = (this.downloadCourseEnabled || this.downloadCoursesEnabled) && this.downloadEnabled;
}, this.currentSiteId);
this.downloadEnabledObserver = CoreEvents.on(CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED, (data) => {
this.downloadEnabledObserver = CoreEvents.on(CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT, (data) => {
this.downloadEnabled = (this.downloadCourseEnabled || this.downloadCoursesEnabled) && data.enabled;
});

View File

@ -18,10 +18,15 @@ import { CoreNavigator } from '@services/navigator';
import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreCourseBasicSearchedData, CoreCourses, CoreCoursesProvider } from '../../services/courses';
import { CoreCourseBasicSearchedData, CoreCourses } from '../../services/courses';
import { CoreTime } from '@singletons/time';
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
import { Translate } from '@singletons';
import {
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT,
} from '@features/courses/constants';
type CoreCoursesListMode = 'search' | 'all' | 'my';
@ -73,10 +78,10 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {
// Update list if user enrols in a course.
this.myCoursesObserver = CoreEvents.on(
CoreCoursesProvider.EVENT_MY_COURSES_UPDATED,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
(data) => {
if (data.action == CoreCoursesProvider.ACTION_ENROL) {
if (data.action == CoreCoursesMyCoursesUpdatedEventAction.ENROL) {
this.fetchCourses();
}
},
@ -98,7 +103,7 @@ export class CoreCoursesListPage implements OnInit, OnDestroy {
}
}, this.currentSiteId);
this.downloadEnabledObserver = CoreEvents.on(CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED, (data) => {
this.downloadEnabledObserver = CoreEvents.on(CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT, (data) => {
this.downloadEnabled = (this.downloadCourseEnabled || this.downloadCoursesEnabled) && data.enabled;
});

View File

@ -27,6 +27,16 @@ import { AddonEnrolSelf } from '@addons/enrol/self/services/self';
import { CoreEnrol, CoreEnrolEnrolmentInfo, CoreEnrolEnrolmentMethod } from '@features/enrol/services/enrol';
import { CoreSiteWSPreSets, WSObservable } from '@classes/sites/authenticated-site';
import { CoreCacheUpdateFrequency } from '@/core/constants';
import {
CORE_COURSES_ENROL_INVALID_KEY,
CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT,
CORE_COURSES_MY_COURSES_CHANGED_EVENT,
CORE_COURSES_MY_COURSES_REFRESHED_EVENT,
CORE_COURSES_MY_COURSES_UPDATED_EVENT,
CoreCoursesMyCoursesUpdatedEventAction,
CORE_COURSES_STATE_FAVOURITE,
CORE_COURSES_STATE_HIDDEN,
} from '../constants';
declare module '@singletons/events' {
@ -36,9 +46,9 @@ declare module '@singletons/events' {
* @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
*/
export interface CoreEventsData {
[CoreCoursesProvider.EVENT_MY_COURSES_CHANGED]: CoreCoursesMyCoursesChangedEventData;
[CoreCoursesProvider.EVENT_MY_COURSES_UPDATED]: CoreCoursesMyCoursesUpdatedEventData;
[CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED]: CoreCoursesDashboardDownloadEnabledChangedEventData;
[CORE_COURSES_MY_COURSES_CHANGED_EVENT]: CoreCoursesMyCoursesChangedEventData;
[CORE_COURSES_MY_COURSES_UPDATED_EVENT]: CoreCoursesMyCoursesUpdatedEventData;
[CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT]: CoreCoursesDashboardDownloadEnabledChangedEventData;
}
}
@ -51,23 +61,51 @@ export class CoreCoursesProvider {
protected static readonly ROOT_CACHE_KEY = 'mmCourses:';
static readonly SEARCH_PER_PAGE = 20;
static readonly RECENT_PER_PAGE = 10;
static readonly ENROL_INVALID_KEY = 'CoreCoursesEnrolInvalidKey';
static readonly EVENT_MY_COURSES_CHANGED = 'courses_my_courses_changed'; // User course list changed while app is running.
// A course was hidden/favourite, or user enroled in a course.
static readonly EVENT_MY_COURSES_UPDATED = 'courses_my_courses_updated';
static readonly EVENT_MY_COURSES_REFRESHED = 'courses_my_courses_refreshed';
static readonly EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED = 'dashboard_download_enabled_changed';
protected static readonly SEARCH_PER_PAGE = 20;
protected static readonly RECENT_PER_PAGE = 10;
// Actions for event EVENT_MY_COURSES_UPDATED.
static readonly ACTION_ENROL = 'enrol'; // User enrolled in a course.
static readonly ACTION_STATE_CHANGED = 'state_changed'; // Course state changed (hidden, favourite).
static readonly ACTION_VIEW = 'view'; // Course viewed.
/**
* @deprecated since 5.0. Use CORE_COURSES_ENROL_INVALID_KEY instead.
*/
static readonly ENROL_INVALID_KEY = CORE_COURSES_ENROL_INVALID_KEY;
/**
* @deprecated since 5.0. Use CORE_COURSES_MY_COURSES_CHANGED_EVENT instead.
*/
static readonly EVENT_MY_COURSES_CHANGED = CORE_COURSES_MY_COURSES_CHANGED_EVENT;
/**
* @deprecated since 5.0. Use CORE_COURSES_MY_COURSES_UPDATED_EVENT instead.
*/
static readonly EVENT_MY_COURSES_UPDATED = CORE_COURSES_MY_COURSES_UPDATED_EVENT;
/**
* @deprecated since 5.0. Use CORE_COURSES_MY_COURSES_REFRESHED_EVENT instead.
*/
static readonly EVENT_MY_COURSES_REFRESHED = CORE_COURSES_MY_COURSES_REFRESHED_EVENT;
/**
* @deprecated since 5.0. Use CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT instead.
*/
static readonly EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED = CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT;
// Possible states changed.
static readonly STATE_HIDDEN = 'hidden';
static readonly STATE_FAVOURITE = 'favourite';
/**
* @deprecated since 5.0. Use CoreCoursesMyCoursesUpdatedEventAction.ENROL instead.
*/
static readonly ACTION_ENROL = CoreCoursesMyCoursesUpdatedEventAction.ENROL;
/**
* @deprecated since 5.0. Use CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED instead.
*/
static readonly ACTION_STATE_CHANGED = CoreCoursesMyCoursesUpdatedEventAction.STATE_CHANGED;
/**
* @deprecated since 5.0. Use CoreCoursesMyCoursesUpdatedEventAction.VIEW instead.
*/
static readonly ACTION_VIEW = CoreCoursesMyCoursesUpdatedEventAction.VIEW;
/**
* @deprecated since 5.0. Use CORE_COURSES_STATE_HIDDEN instead.
*/
static readonly STATE_HIDDEN = CORE_COURSES_STATE_HIDDEN;
/**
* @deprecated since 5.0. Use CORE_COURSES_STATE_FAVOURITE instead.
*/
static readonly STATE_FAVOURITE = CORE_COURSES_STATE_FAVOURITE;
protected userCoursesIds?: Set<number>;
protected downloadOptionsEnabled = false;
@ -958,7 +996,7 @@ export class CoreCoursesProvider {
if (added.length || removed.length) {
// At least 1 course was added or removed, trigger the event.
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_CHANGED, {
CoreEvents.trigger(CORE_COURSES_MY_COURSES_CHANGED_EVENT, {
added: added,
removed: removed,
}, site.getId());
@ -1232,7 +1270,7 @@ export class CoreCoursesProvider {
* @param instanceId Enrol instance ID.
* @param siteId Site ID. If not defined, use current site.
* @returns Promise resolved if the user is enrolled. If the password is invalid, the promise is rejected
* with an object with errorcode = CoreCoursesProvider.ENROL_INVALID_KEY.
* with an object with errorcode = CORE_COURSES_ENROL_INVALID_KEY.
* @deprecated since 4.3 use CoreEnrolDelegate.enrol instead.
*/
async selfEnrol(courseId: number, password: string = '', instanceId?: number, siteId?: string): Promise<boolean> {
@ -1282,7 +1320,7 @@ export class CoreCoursesProvider {
}
this.downloadOptionsEnabled = enable;
CoreEvents.trigger(CoreCoursesProvider.EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED, { enabled: enable });
CoreEvents.trigger(CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT, { enabled: enable });
}
}
@ -1290,10 +1328,10 @@ export class CoreCoursesProvider {
export const CoreCourses = makeSingleton(CoreCoursesProvider);
/**
* Data sent to the EVENT_MY_COURSES_UPDATED.
* Data sent to the CORE_COURSES_MY_COURSES_UPDATED_EVENT.
*/
export type CoreCoursesMyCoursesUpdatedEventData = {
action: string; // Action performed.
action: CoreCoursesMyCoursesUpdatedEventAction; // Action performed.
courseId?: number; // Course ID affected (if any).
course?: CoreCourseAnyCourseData; // Course affected (if any).
state?: string; // Only for ACTION_STATE_CHANGED. The state that changed (hidden, favourite).
@ -1301,7 +1339,7 @@ export type CoreCoursesMyCoursesUpdatedEventData = {
};
/**
* Data sent to the EVENT_MY_COURSES_CHANGED.
* Data sent to the CORE_COURSES_MY_COURSES_CHANGED_EVENT.
*/
export type CoreCoursesMyCoursesChangedEventData = {
added: number[];
@ -1309,7 +1347,7 @@ export type CoreCoursesMyCoursesChangedEventData = {
};
/**
* Data sent to the EVENT_DASHBOARD_DOWNLOAD_ENABLED_CHANGED.
* Data sent to the CORE_COURSES_DASHBOARD_DOWNLOAD_ENABLED_CHANGED_EVENT.
*/
export type CoreCoursesDashboardDownloadEnabledChangedEventData = {
enabled: boolean;

View File

@ -27,7 +27,6 @@ import { CoreCourseOptionsDelegate } from '@features/course/services/course-opti
import { CoreCourseFormatDelegate } from '@features/course/services/format-delegate';
import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate';
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
import { CoreCoursesProvider } from '@features/courses/services/courses';
import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
import { CoreQuestionBehaviourDelegate } from '@features/question/services/behaviour-delegate';
import { CoreQuestionDelegate } from '@features/question/services/question-delegate';
@ -89,6 +88,7 @@ import { CorePath } from '@singletons/path';
import { CoreEnrolAction, CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate';
import { CoreSitePluginsEnrolHandler } from '../classes/handlers/enrol-handler';
import { CORE_SITE_PLUGINS_COMPONENT } from '../constants';
import { CORE_COURSES_MY_COURSES_CHANGED_EVENT } from '@features/courses/constants';
/**
* Helper service to provide functionalities regarding site plugins. It basically has the features to load and register site
@ -140,7 +140,7 @@ export class CoreSitePluginsInitService {
});
// Re-load plugins restricted for courses when the list of user courses changes.
CoreEvents.on(CoreCoursesProvider.EVENT_MY_COURSES_CHANGED, (data) => {
CoreEvents.on(CORE_COURSES_MY_COURSES_CHANGED_EVENT, (data) => {
if (data.siteId && data.siteId === CoreSites.getCurrentSiteId() && data.added.length) {
this.reloadCourseRestrictHandlers();
}