97 lines
3.2 KiB
TypeScript

// (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 { CoreCourseProvider } from '@features/course/services/course';
import {
CoreCourseAccess,
CoreCourseOptionsHandler,
CoreCourseOptionsHandlerData,
} from '@features/course/services/course-options-delegate';
import { CoreEnrolledCourseDataWithExtraInfoAndOptions } from '@features/courses/services/courses-helper';
import { makeSingleton } from '@singletons';
import { AddonCourseCompletion } from '../coursecompletion';
/**
* Handler to inject an option into the course main menu.
*/
@Injectable({ providedIn: 'root' })
export class AddonCourseCompletionCourseOptionHandlerService implements CoreCourseOptionsHandler {
name = 'AddonCourseCompletion';
priority = 200;
/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return AddonCourseCompletion.isPluginViewEnabled();
}
/**
* @inheritdoc
*/
async isEnabledForCourse(courseId: number, accessData: CoreCourseAccess): Promise<boolean> {
if (accessData && accessData.type == CoreCourseProvider.ACCESS_GUEST) {
return false; // Not enabled for guests.
}
const courseEnabled = await AddonCourseCompletion.isPluginViewEnabledForCourse(courseId);
// If is not enabled in the course, is not enabled for the user.
if (!courseEnabled) {
return false;
}
return AddonCourseCompletion.isPluginViewEnabledForUser(courseId);
}
/**
* @inheritdoc
*/
getDisplayData(): CoreCourseOptionsHandlerData | Promise<CoreCourseOptionsHandlerData> {
return {
title: 'addon.coursecompletion.completionmenuitem',
class: 'addon-coursecompletion-course-handler',
page: 'coursecompletion',
};
}
/**
* @inheritdoc
*/
async invalidateEnabledForCourse(courseId: number): Promise<void> {
await AddonCourseCompletion.invalidateCourseCompletion(courseId);
}
/**
* @inheritdoc
*/
async prefetch(course: CoreEnrolledCourseDataWithExtraInfoAndOptions): Promise<void> {
try {
await AddonCourseCompletion.getCompletion(course.id, undefined, {
getFromCache: false,
emergencyCache: false,
});
} catch (error) {
if (error && error.errorcode == 'notenroled') {
// Not enrolled error, probably a teacher. Ignore error.
} else {
throw error;
}
}
}
}
export const AddonCourseCompletionCourseOptionHandler = makeSingleton(AddonCourseCompletionCourseOptionHandlerService);