80 lines
2.5 KiB
TypeScript
Raw Normal View History

// (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 { Component } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { CoreSitesProvider } from '../../../../providers/sites';
import { CoreDomUtilsProvider } from '../../../../providers/utils/dom';
import { CoreCoursesProvider } from '../../providers/courses';
/**
* Page that displays available courses in current site.
*/
2018-01-29 10:05:20 +01:00
@IonicPage({ segment: 'core-courses-available-courses' })
@Component({
selector: 'page-core-courses-available-courses',
templateUrl: 'available-courses.html',
})
export class CoreCoursesAvailableCoursesPage {
courses: any[] = [];
coursesLoaded: boolean;
constructor(private coursesProvider: CoreCoursesProvider, private domUtils: CoreDomUtilsProvider,
2018-01-29 10:05:20 +01:00
private sitesProvider: CoreSitesProvider) { }
/**
* View loaded.
*/
2018-01-29 10:05:20 +01:00
ionViewDidLoad(): void {
this.loadCourses().finally(() => {
this.coursesLoaded = true;
});
}
/**
* Load the courses.
2018-01-29 10:05:20 +01:00
*
* @return {Promise<any>} Promise resolved when done.
*/
2018-01-29 10:05:20 +01:00
protected loadCourses(): Promise<any> {
const frontpageCourseId = this.sitesProvider.getCurrentSite().getSiteHomeId();
2018-01-29 10:05:20 +01:00
return this.coursesProvider.getCoursesByField().then((courses) => {
this.courses = courses.filter((course) => {
return course.id != frontpageCourseId;
});
}).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'core.courses.errorloadcourses', true);
});
}
/**
* Refresh the courses.
*
* @param {any} refresher Refresher.
*/
2018-01-29 10:05:20 +01:00
refreshCourses(refresher: any): void {
const promises = [];
promises.push(this.coursesProvider.invalidateUserCourses());
promises.push(this.coursesProvider.invalidateCoursesByField());
Promise.all(promises).finally(() => {
this.loadCourses().finally(() => {
refresher.complete();
});
});
2018-01-29 10:05:20 +01:00
}
}