MOBILE-3807 core: Add some delegates to user menu

main
Pau Ferrer Ocaña 2021-10-21 15:09:23 +02:00
parent 81fdd00902
commit 806e29ce10
8 changed files with 79 additions and 100 deletions

View File

@ -16,7 +16,7 @@ import { Injectable } from '@angular/core';
import { CoreBlockHandlerData } from '@features/block/services/block-delegate';
import { CoreBlockOnlyTitleComponent } from '@features/block/components/only-title-block/only-title-block';
import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler';
import { AddonPrivateFilesMainMenuHandlerService } from '@/addons/privatefiles/services/handlers/mainmenu';
import { AddonPrivateFilesUserHandlerService } from '@addons/privatefiles/services/handlers/user';
import { makeSingleton } from '@singletons';
/**
@ -39,7 +39,7 @@ export class AddonBlockPrivateFilesHandlerService extends CoreBlockBaseHandler {
title: 'addon.block_privatefiles.pluginname',
class: 'addon-block-private-files',
component: CoreBlockOnlyTitleComponent,
link: AddonPrivateFilesMainMenuHandlerService.PAGE_NAME,
link: AddonPrivateFilesUserHandlerService.PAGE_NAME,
linkParams: { root: 'my' },
navOptions: {
preferCurrentTab: false,

View File

@ -16,14 +16,14 @@ import { Injector, NgModule } from '@angular/core';
import { RouterModule, ROUTES, Routes } from '@angular/router';
import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.module';
import { AddonPrivateFilesMainMenuHandlerService } from './services/handlers/mainmenu';
import { AddonPrivateFilesUserHandlerService } from './services/handlers/user';
function buildRoutes(injector: Injector): Routes {
return [
{
path: 'root',
data: {
mainMenuTabRoot: AddonPrivateFilesMainMenuHandlerService.PAGE_NAME,
mainMenuTabRoot: AddonPrivateFilesUserHandlerService.PAGE_NAME,
},
loadChildren: () => import('./pages/index/index.module').then(m => m.AddonPrivateFilesIndexPageModule),
},

View File

@ -15,12 +15,12 @@
import { APP_INITIALIZER, NgModule, Type } from '@angular/core';
import { Routes } from '@angular/router';
import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
import { CoreMainMenuRoutingModule } from '@features/mainmenu/mainmenu-routing.module';
import { AddonPrivateFilesMainMenuHandler, AddonPrivateFilesMainMenuHandlerService } from './services/handlers/mainmenu';
import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module';
import { AddonPrivateFilesProvider } from './services/privatefiles';
import { AddonPrivateFilesHelperProvider } from './services/privatefiles-helper';
import { CoreUserDelegate } from '@features/user/services/user-delegate';
import { AddonPrivateFilesUserHandler, AddonPrivateFilesUserHandlerService } from './services/handlers/user';
export const ADDON_PRIVATEFILES_SERVICES: Type<unknown>[] = [
AddonPrivateFilesProvider,
@ -29,7 +29,7 @@ export const ADDON_PRIVATEFILES_SERVICES: Type<unknown>[] = [
const routes: Routes = [
{
path: AddonPrivateFilesMainMenuHandlerService.PAGE_NAME,
path: AddonPrivateFilesUserHandlerService.PAGE_NAME,
loadChildren: () => import('@/addons/privatefiles/privatefiles-lazy.module').then(m => m.AddonPrivateFilesLazyModule),
},
];
@ -45,7 +45,7 @@ const routes: Routes = [
provide: APP_INITIALIZER,
multi: true,
useValue: () => {
CoreMainMenuDelegate.registerHandler(AddonPrivateFilesMainMenuHandler.instance);
CoreUserDelegate.registerHandler(AddonPrivateFilesUserHandler.instance);
},
},
],

View File

@ -14,44 +14,57 @@
import { Injectable } from '@angular/core';
import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '@features/mainmenu/services/mainmenu-delegate';
import { AddonPrivateFiles } from '@/addons/privatefiles/services/privatefiles';
import { makeSingleton } from '@singletons';
import { CoreUserDelegateService, CoreUserProfileHandler, CoreUserProfileHandlerData } from '@features/user/services/user-delegate';
import { CoreUserProfile } from '@features/user/services/user';
import { CoreNavigator } from '@services/navigator';
import { CoreSites } from '@services/sites';
/**
* Handler to inject an option into main menu.
* Handler to inject an option into user menu.
*/
@Injectable({ providedIn: 'root' })
export class AddonPrivateFilesMainMenuHandlerService implements CoreMainMenuHandler {
export class AddonPrivateFilesUserHandlerService implements CoreUserProfileHandler {
static readonly PAGE_NAME = 'private';
name = 'AddonPrivateFiles';
priority = 400;
priority = 300;
type = CoreUserDelegateService.TYPE_NEW_PAGE;
cacheEnabled = true;
/**
* Check if the handler is enabled on a site level.
*
* @return Whether or not the handler is enabled on a site level.
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return AddonPrivateFiles.isPluginEnabled();
}
/**
* Returns the data needed to render the handler.
*
* @return Data needed to render the handler.
* @inheritdoc
*/
getDisplayData(): CoreMainMenuHandlerData {
async isEnabledForUser(user: CoreUserProfile): Promise<boolean> {
// Private files only available for the current user.
return user.id == CoreSites.getCurrentSiteUserId();
}
/**
* @inheritdoc
*/
getDisplayData(): CoreUserProfileHandlerData {
return {
icon: 'fas-folder',
title: 'addon.privatefiles.files',
page: AddonPrivateFilesMainMenuHandlerService.PAGE_NAME,
class: 'addon-privatefiles-handler',
action: (event): void => {
event.preventDefault();
event.stopPropagation();
CoreNavigator.navigateToSitePath(AddonPrivateFilesUserHandlerService.PAGE_NAME);
},
};
}
}
export const AddonPrivateFilesMainMenuHandler = makeSingleton(AddonPrivateFilesMainMenuHandlerService);
export const AddonPrivateFilesUserHandler = makeSingleton(AddonPrivateFilesUserHandlerService);

View File

@ -23,13 +23,13 @@ import { CoreGradesCoursePage } from './pages/course/course.page';
import { CoreGradesCoursePageModule } from './pages/course/course.module';
import { CoreGradesCoursesPage } from './pages/courses/courses.page';
import { CoreGradesGradePage } from './pages/grade/grade.page';
import { CoreGradesMainMenuHandlerService } from './services/handlers/mainmenu';
import { CoreGradesUserHandlerService } from './services/handlers/user';
const mobileRoutes: Routes = [
{
path: '',
data: {
mainMenuTabRoot: CoreGradesMainMenuHandlerService.PAGE_NAME,
mainMenuTabRoot: CoreGradesUserHandlerService.PAGE_NAME,
},
component: CoreGradesCoursesPage,
},
@ -47,7 +47,7 @@ const tabletRoutes: Routes = [
{
path: '',
data: {
mainMenuTabRoot: CoreGradesMainMenuHandlerService.PAGE_NAME,
mainMenuTabRoot: CoreGradesUserHandlerService.PAGE_NAME,
},
component: CoreGradesCoursesPage,
children: [

View File

@ -19,14 +19,12 @@ import { CoreCourseIndexRoutingModule } from '@features/course/pages/index/index
import { CoreCourseOptionsDelegate } from '@features/course/services/course-options-delegate';
import { CoreMainMenuRoutingModule } from '@features/mainmenu/mainmenu-routing.module';
import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module';
import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
import { CoreUserDelegate } from '@features/user/services/user-delegate';
import { CoreGradesProvider } from './services/grades';
import { CoreGradesHelperProvider } from './services/grades-helper';
import { CoreGradesCourseOptionHandler } from './services/handlers/course-option';
import { CoreGradesMainMenuHandler, CoreGradesMainMenuHandlerService } from './services/handlers/mainmenu';
import { CoreGradesOverviewLinkHandler } from './services/handlers/overview-link';
import { CoreGradesUserHandler } from './services/handlers/user';
import { CoreGradesUserHandler, CoreGradesUserHandlerService } from './services/handlers/user';
import { CoreGradesUserLinkHandler } from './services/handlers/user-link';
export const CORE_GRADES_SERVICES: Type<unknown>[] = [
@ -36,7 +34,7 @@ export const CORE_GRADES_SERVICES: Type<unknown>[] = [
const routes: Routes = [
{
path: CoreGradesMainMenuHandlerService.PAGE_NAME,
path: CoreGradesUserHandlerService.PAGE_NAME,
loadChildren: () => import('@features/grades/grades-lazy.module').then(m => m.CoreGradesLazyModule),
},
{
@ -63,7 +61,6 @@ const courseIndexRoutes: Routes = [
provide: APP_INITIALIZER,
multi: true,
useValue: () => {
CoreMainMenuDelegate.registerHandler(CoreGradesMainMenuHandler.instance);
CoreUserDelegate.registerHandler(CoreGradesUserHandler.instance);
CoreContentLinksDelegate.registerHandler(CoreGradesUserLinkHandler.instance);
CoreContentLinksDelegate.registerHandler(CoreGradesOverviewLinkHandler.instance);

View File

@ -1,56 +0,0 @@
// (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.
import { Injectable } from '@angular/core';
import { CoreGrades } from '@features/grades/services/grades';
import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '@features/mainmenu/services/mainmenu-delegate';
import { makeSingleton } from '@singletons';
/**
* Handler to inject an option into main menu.
*/
@Injectable({ providedIn: 'root' })
export class CoreGradesMainMenuHandlerService implements CoreMainMenuHandler {
static readonly PAGE_NAME = 'grades';
name = 'CoreGrades';
priority = 600;
/**
* Check if the handler is enabled on a site level.
*
* @return Whether or not the handler is enabled on a site level.
*/
isEnabled(): Promise<boolean> {
return CoreGrades.isCourseGradesEnabled();
}
/**
* Returns the data needed to render the handler.
*
* @return Data needed to render the handler.
*/
getDisplayData(): CoreMainMenuHandlerData {
return {
icon: 'fas-chart-bar',
title: 'core.grades.grades',
page: CoreGradesMainMenuHandlerService.PAGE_NAME,
class: 'core-grades-coursesgrades-handler',
};
}
}
export const CoreGradesMainMenuHandler = makeSingleton(CoreGradesMainMenuHandlerService);

View File

@ -22,6 +22,7 @@ import {
CoreUserProfileHandlerData,
} from '@features/user/services/user-delegate';
import { CoreNavigator } from '@services/navigator';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { makeSingleton } from '@singletons';
@ -31,6 +32,8 @@ import { makeSingleton } from '@singletons';
@Injectable({ providedIn: 'root' })
export class CoreGradesUserHandlerService implements CoreUserProfileHandler {
static readonly PAGE_NAME = 'grades';
name = 'CoreGrades:viewGrades';
priority = 400;
type = CoreUserDelegateService.TYPE_NEW_PAGE;
@ -47,32 +50,54 @@ export class CoreGradesUserHandlerService implements CoreUserProfileHandler {
* @inheritdoc
*/
async isEnabledForCourse(courseId?: number): Promise<boolean> {
return CoreUtils.ignoreErrors(CoreGrades.isPluginEnabledForCourse(courseId), false);
if (courseId) {
return CoreUtils.ignoreErrors(CoreGrades.isPluginEnabledForCourse(courseId), false);
} else {
return CoreGrades.isCourseGradesEnabled();
}
}
/**
* @inheritdoc
*/
async isEnabledForUser(user: CoreUserProfile, courseId?: number): Promise<boolean> {
return CoreUtils.promiseWorks(CoreGrades.getCourseGradesTable(courseId!, user.id));
if (courseId) {
return CoreUtils.promiseWorks(CoreGrades.getCourseGradesTable(courseId, user.id));
}
// All course grades only available for the current user.
return user.id == CoreSites.getCurrentSiteUserId();
}
/**
* @inheritdoc
*/
getDisplayData(): CoreUserProfileHandlerData {
return {
icon: 'fas-chart-bar',
title: 'core.grades.grades',
class: 'core-grades-user-handler',
action: (event, user, courseId): void => {
event.preventDefault();
event.stopPropagation();
CoreNavigator.navigateToSitePath(`/user-grades/${courseId}`, {
params: { userId: user.id },
});
},
};
getDisplayData(user: CoreUserProfile, courseId?: number): CoreUserProfileHandlerData {
if (courseId) {
return {
icon: 'fas-chart-bar',
title: 'core.grades.grades',
class: 'core-grades-user-handler',
action: (event, user, courseId): void => {
event.preventDefault();
event.stopPropagation();
CoreNavigator.navigateToSitePath(`/user-grades/${courseId}`, {
params: { userId: user.id },
});
},
};
} else {
return {
icon: 'fas-chart-bar',
title: 'core.grades.grades',
class: 'core-grades-coursesgrades-handler',
action: (event): void => {
event.preventDefault();
event.stopPropagation();
CoreNavigator.navigateToSitePath(CoreGradesUserHandlerService.PAGE_NAME);
},
};
}
}
}