2018-01-11 13:18:17 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// 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 { NavController } from 'ionic-angular';
|
2018-03-12 15:44:41 +01:00
|
|
|
import { CoreCoursesProvider } from '@core/courses/providers/courses';
|
2018-01-11 13:18:17 +01:00
|
|
|
import { CoreCourseFormatHandler } from './format-delegate';
|
|
|
|
import { CoreCourseProvider } from './course';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default handler used when the course format doesn't have a specific implementation.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler {
|
|
|
|
name = 'default';
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
constructor(private coursesProvider: CoreCoursesProvider) { }
|
2018-01-11 13:18:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the handler is enabled on a site level.
|
|
|
|
*
|
|
|
|
* @return {boolean|Promise<boolean>} True or promise resolved with true if enabled.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
isEnabled(): boolean | Promise<boolean> {
|
2018-01-11 13:18:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title to use in course page.
|
|
|
|
*
|
|
|
|
* @param {any} course The course.
|
|
|
|
* @return {string} Title.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getCourseTitle?(course: any): string {
|
2018-01-11 13:18:17 +01:00
|
|
|
return course.fullname || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether it allows seeing all sections at the same time. Defaults to true.
|
|
|
|
*
|
|
|
|
* @param {any} course The course to check.
|
|
|
|
* @type {boolean} Whether it can view all sections.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
canViewAllSections(course: any): boolean {
|
2018-01-11 13:18:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:58:27 +01:00
|
|
|
/**
|
|
|
|
* Whether the option to enable section/module download should be displayed. Defaults to true.
|
|
|
|
*
|
|
|
|
* @param {any} course The course to check.
|
|
|
|
* @return {boolean} Whether the option to enable section/module download should be displayed
|
|
|
|
*/
|
|
|
|
displayEnableDownload(course: any): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:18:17 +01:00
|
|
|
/**
|
|
|
|
* Whether the default section selector should be displayed. Defaults to true.
|
|
|
|
*
|
|
|
|
* @param {any} course The course to check.
|
|
|
|
* @type {boolean} Whether the default section selector should be displayed.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
displaySectionSelector(course: any): boolean {
|
2018-01-11 13:18:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of sections, get the "current" section that should be displayed first.
|
|
|
|
*
|
|
|
|
* @param {any} course The course to get the title.
|
|
|
|
* @param {any[]} sections List of sections.
|
|
|
|
* @return {any|Promise<any>} Current section (or promise resolved with current section).
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
getCurrentSection(course: any, sections: any[]): any | Promise<any> {
|
2018-01-11 13:18:17 +01:00
|
|
|
// We need the "marker" to determine the current section.
|
|
|
|
return this.coursesProvider.getCoursesByField('id', course.id).catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
}).then((courses) => {
|
|
|
|
if (courses && courses[0]) {
|
|
|
|
// Find the marked section.
|
2018-01-29 10:05:20 +01:00
|
|
|
const course = courses[0];
|
2018-01-11 13:18:17 +01:00
|
|
|
for (let i = 0; i < sections.length; i++) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const section = sections[i];
|
2018-01-11 13:18:17 +01:00
|
|
|
if (section.section == course.marker) {
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marked section not found or we couldn't retrieve the marker. Return the first section.
|
|
|
|
for (let i = 0; i < sections.length; i++) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const section = sections[i];
|
2018-01-11 13:18:17 +01:00
|
|
|
if (section.id != CoreCourseProvider.ALL_SECTIONS_ID) {
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate the data required to load the course format.
|
|
|
|
*
|
|
|
|
* @param {any} course The course to get the title.
|
|
|
|
* @param {any[]} sections List of sections.
|
|
|
|
* @return {Promise<any>} Promise resolved when the data is invalidated.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
invalidateData(course: any, sections: any[]): Promise<any> {
|
2018-01-11 13:18:17 +01:00
|
|
|
return this.coursesProvider.invalidateCoursesByField('id', course.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the page to display a course. If not defined, the page CoreCourseSectionPage will be opened.
|
|
|
|
* Implement it only if you want to create your own page to display the course. In general it's better to use the method
|
|
|
|
* getCourseFormatComponent because it will display the course handlers at the top.
|
|
|
|
* Your page should include the course handlers using CoreCoursesDelegate.
|
|
|
|
*
|
|
|
|
* @param {NavController} navCtrl The NavController instance to use.
|
|
|
|
* @param {any} course The course to open. It should contain a "format" attribute.
|
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
openCourse(navCtrl: NavController, course: any): Promise<any> {
|
|
|
|
return navCtrl.push('CoreCourseSectionPage', { course: course });
|
2018-01-11 13:18:17 +01:00
|
|
|
}
|
|
|
|
}
|