2018-08-16 09:50:54 +02: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 { CoreUtilsProvider } from '@providers/utils/utils';
|
|
|
|
import { CoreCoursesProvider } from './courses';
|
2018-11-06 16:12:18 +01:00
|
|
|
import { AddonCourseCompletionProvider } from '@addon/coursecompletion/providers/coursecompletion';
|
2018-08-16 09:50:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to gather some common courses functions.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreCoursesHelperProvider {
|
|
|
|
|
2018-11-06 16:12:18 +01:00
|
|
|
constructor(private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider,
|
|
|
|
private courseCompletionProvider: AddonCourseCompletionProvider) { }
|
2018-08-16 09:50:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a course object returned by core_enrol_get_users_courses and another one returned by core_course_get_courses_by_field,
|
|
|
|
* load some extra data to the first one.
|
|
|
|
*
|
|
|
|
* @param {any} course Course returned by core_enrol_get_users_courses.
|
|
|
|
* @param {any} courseByField Course returned by core_course_get_courses_by_field.
|
|
|
|
*/
|
|
|
|
loadCourseExtraInfo(course: any, courseByField: any): void {
|
|
|
|
if (courseByField) {
|
|
|
|
course.displayname = courseByField.displayname;
|
|
|
|
|
|
|
|
if (courseByField.overviewfiles && courseByField.overviewfiles[0]) {
|
2018-10-30 09:19:09 +01:00
|
|
|
course.courseImage = courseByField.overviewfiles[0].fileurl;
|
2018-08-16 09:50:54 +02:00
|
|
|
} else {
|
2018-10-30 09:19:09 +01:00
|
|
|
course.courseImage = false;
|
2018-08-16 09:50:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete course.displayname;
|
2018-10-30 09:19:09 +01:00
|
|
|
course.courseImage = false;
|
2018-08-16 09:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of courses returned by core_enrol_get_users_courses, load some extra data using the WebService
|
|
|
|
* core_course_get_courses_by_field if available.
|
|
|
|
*
|
|
|
|
* @param {any[]} courses List of courses.
|
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
|
|
|
*/
|
|
|
|
loadCoursesExtraInfo(courses: any[]): Promise<any> {
|
2018-10-25 12:51:47 +02:00
|
|
|
if (courses[0] && typeof courses[0].overviewfiles != 'undefined' && typeof courses[0].displayname != 'undefined') {
|
|
|
|
// We already have the extra data. Call loadCourseExtraInfo to load the calculated fields.
|
|
|
|
courses.forEach((course) => {
|
|
|
|
this.loadCourseExtraInfo(course, course);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:50:54 +02:00
|
|
|
if (!courses.length || !this.coursesProvider.isGetCoursesByFieldAvailable()) {
|
|
|
|
// No courses or cannot get the data, stop.
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const courseIds = courses.map((course) => {
|
|
|
|
return course.id;
|
|
|
|
}).join(',');
|
|
|
|
|
|
|
|
// Get the extra data for the courses.
|
|
|
|
return this.coursesProvider.getCoursesByField('ids', courseIds).then((coursesInfo) => {
|
|
|
|
coursesInfo = this.utils.arrayToObject(coursesInfo, 'id');
|
|
|
|
|
|
|
|
courses.forEach((course) => {
|
|
|
|
this.loadCourseExtraInfo(course, coursesInfo[course.id]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-10-10 16:16:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user courses with admin and nav options.
|
|
|
|
*
|
2018-11-06 16:12:18 +01:00
|
|
|
* @param {string} [sort=fullname] Sort courses after get them. If sort is not defined it won't be sorted.
|
|
|
|
* @param {number} [slice=0] Slice results to get the X first one. If slice > 0 it will be done after sorting.
|
2018-11-07 10:31:25 +01:00
|
|
|
* @param {string} [filter] Filter using some field.
|
2018-11-06 16:12:18 +01:00
|
|
|
* @return {Promise<any[]>} Courses filled with options.
|
2018-10-10 16:16:41 +02:00
|
|
|
*/
|
2018-11-07 10:31:25 +01:00
|
|
|
getUserCoursesWithOptions(sort: string = 'fullname', slice: number = 0, filter?: string): Promise<any[]> {
|
2018-10-10 16:16:41 +02:00
|
|
|
return this.coursesProvider.getUserCourses().then((courses) => {
|
|
|
|
const promises = [],
|
|
|
|
courseIds = courses.map((course) => {
|
|
|
|
return course.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.coursesProvider.canGetAdminAndNavOptions()) {
|
|
|
|
// Load course options of the course.
|
|
|
|
promises.push(this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
|
|
|
courses.forEach((course) => {
|
|
|
|
course.navOptions = options.navOptions[course.id];
|
|
|
|
course.admOptions = options.admOptions[course.id];
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(this.loadCoursesExtraInfo(courses));
|
|
|
|
|
|
|
|
return Promise.all(promises).then(() => {
|
2018-11-06 16:12:18 +01:00
|
|
|
if (courses.length <= 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-11-07 10:31:25 +01:00
|
|
|
|
|
|
|
switch (filter) {
|
|
|
|
case 'isfavourite':
|
|
|
|
courses = courses.filter((course) => {
|
|
|
|
return !!course.isfavourite;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Filter not implemented.
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:12:18 +01:00
|
|
|
switch (sort) {
|
|
|
|
case 'fullname':
|
|
|
|
courses.sort((a, b) => {
|
|
|
|
const compareA = a.fullname.toLowerCase(),
|
|
|
|
compareB = b.fullname.toLowerCase();
|
|
|
|
|
|
|
|
return compareA.localeCompare(compareB);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'lastaccess':
|
|
|
|
courses.sort((a, b) => {
|
2019-01-02 11:49:09 +01:00
|
|
|
return b.lastaccess - a.lastaccess;
|
2018-11-06 16:12:18 +01:00
|
|
|
});
|
|
|
|
break;
|
2018-11-07 10:31:25 +01:00
|
|
|
case 'timemodified':
|
|
|
|
courses.sort((a, b) => {
|
2019-01-02 11:49:09 +01:00
|
|
|
return b.timemodified - a.timemodified;
|
2018-11-07 10:31:25 +01:00
|
|
|
});
|
|
|
|
break;
|
2018-11-06 16:12:18 +01:00
|
|
|
default:
|
|
|
|
// Sort not implemented. Do not sort.
|
|
|
|
}
|
2018-11-07 10:31:25 +01:00
|
|
|
|
2018-11-06 16:12:18 +01:00
|
|
|
courses = slice > 0 ? courses.slice(0, slice) : courses;
|
|
|
|
|
2018-11-09 11:18:41 +01:00
|
|
|
// Fetch course completion status if needed.
|
2018-11-06 16:12:18 +01:00
|
|
|
return Promise.all(courses.map((course) => {
|
2018-11-09 11:18:41 +01:00
|
|
|
if (typeof course.completed != 'undefined') {
|
|
|
|
// The WebService already returns the completed status, no need to fetch it.
|
|
|
|
return Promise.resolve(course);
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:12:18 +01:00
|
|
|
if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) {
|
|
|
|
// Completion is disabled for this course, there is no need to fetch the completion status.
|
|
|
|
return Promise.resolve(course);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.courseCompletionProvider.getCompletion(course.id).catch(() => {
|
|
|
|
// Ignore error, maybe course completion is disabled or user has no permission.
|
|
|
|
}).then((completion) => {
|
|
|
|
course.completed = completion && completion.completed;
|
|
|
|
|
|
|
|
return course;
|
|
|
|
});
|
|
|
|
}));
|
2018-10-10 16:16:41 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-08-16 09:50:54 +02:00
|
|
|
}
|