From 7891776282d98c1286e2dae35670444243bb3ca7 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 16 Aug 2018 09:50:54 +0200 Subject: [PATCH] MOBILE-2254 courses: Display course shortname if needed --- .../core-courses-course-list-item.html | 4 +- .../core-courses-course-progress.html | 4 +- src/core/courses/courses.module.ts | 5 +- .../courses/pages/my-courses/my-courses.ts | 19 +---- .../courses/pages/my-overview/my-overview.ts | 19 +---- src/core/courses/providers/helper.ts | 75 +++++++++++++++++++ 6 files changed, 89 insertions(+), 37 deletions(-) create mode 100644 src/core/courses/providers/helper.ts diff --git a/src/core/courses/components/course-list-item/core-courses-course-list-item.html b/src/core/courses/components/course-list-item/core-courses-course-list-item.html index f49027c9c..d37534d63 100644 --- a/src/core/courses/components/course-list-item/core-courses-course-list-item.html +++ b/src/core/courses/components/course-list-item/core-courses-course-list-item.html @@ -1,6 +1,6 @@ - + -

+

diff --git a/src/core/courses/components/course-progress/core-courses-course-progress.html b/src/core/courses/components/course-progress/core-courses-course-progress.html index f11b0171b..9d0cb6f01 100644 --- a/src/core/courses/components/course-progress/core-courses-course-progress.html +++ b/src/core/courses/components/course-progress/core-courses-course-progress.html @@ -2,8 +2,8 @@
- -

+ +

diff --git a/src/core/courses/courses.module.ts b/src/core/courses/courses.module.ts index 200ab9db2..06243e676 100644 --- a/src/core/courses/courses.module.ts +++ b/src/core/courses/courses.module.ts @@ -14,6 +14,7 @@ import { NgModule } from '@angular/core'; import { CoreCoursesProvider } from './providers/courses'; +import { CoreCoursesHelperProvider } from './providers/helper'; import { CoreCoursesMainMenuHandler } from './providers/mainmenu-handler'; import { CoreCoursesMyOverviewProvider } from './providers/my-overview'; import { CoreCoursesCourseLinkHandler } from './providers/course-link-handler'; @@ -25,7 +26,8 @@ import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate' // List of providers (without handlers). export const CORE_COURSES_PROVIDERS: any[] = [ CoreCoursesProvider, - CoreCoursesMyOverviewProvider + CoreCoursesMyOverviewProvider, + CoreCoursesHelperProvider ]; @NgModule({ @@ -35,6 +37,7 @@ export const CORE_COURSES_PROVIDERS: any[] = [ providers: [ CoreCoursesProvider, CoreCoursesMyOverviewProvider, + CoreCoursesHelperProvider, CoreCoursesMainMenuHandler, CoreCoursesCourseLinkHandler, CoreCoursesIndexLinkHandler, diff --git a/src/core/courses/pages/my-courses/my-courses.ts b/src/core/courses/pages/my-courses/my-courses.ts index 72ba51a12..e585c2613 100644 --- a/src/core/courses/pages/my-courses/my-courses.ts +++ b/src/core/courses/pages/my-courses/my-courses.ts @@ -17,8 +17,8 @@ import { IonicPage, Searchbar, NavController } from 'ionic-angular'; import { CoreEventsProvider } from '@providers/events'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; -import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreCoursesProvider } from '../../providers/courses'; +import { CoreCoursesHelperProvider } from '../../providers/helper'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate'; @@ -51,7 +51,7 @@ export class CoreCoursesMyCoursesPage implements OnDestroy { constructor(private navCtrl: NavController, private coursesProvider: CoreCoursesProvider, private domUtils: CoreDomUtilsProvider, private eventsProvider: CoreEventsProvider, private sitesProvider: CoreSitesProvider, private courseHelper: CoreCourseHelperProvider, - private courseOptionsDelegate: CoreCourseOptionsDelegate, private utils: CoreUtilsProvider) { } + private courseOptionsDelegate: CoreCourseOptionsDelegate, private coursesHelper: CoreCoursesHelperProvider) { } /** * View loaded. @@ -96,20 +96,7 @@ export class CoreCoursesMyCoursesPage implements OnDestroy { this.courseIds = courseIds.join(','); - if (this.courseIds && this.coursesProvider.isGetCoursesByFieldAvailable()) { - // Load course image of all the courses. - promises.push(this.coursesProvider.getCoursesByField('ids', this.courseIds).then((coursesInfo) => { - coursesInfo = this.utils.arrayToObject(coursesInfo, 'id'); - courses.forEach((course) => { - if (coursesInfo[course.id] && coursesInfo[course.id].overviewfiles && - coursesInfo[course.id].overviewfiles[0]) { - course.imageThumb = coursesInfo[course.id].overviewfiles[0].fileurl; - } else { - course.imageThumb = false; - } - }); - })); - } + promises.push(this.coursesHelper.loadCoursesExtraInfo(courses)); if (this.coursesProvider.canGetAdminAndNavOptions()) { promises.push(this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => { diff --git a/src/core/courses/pages/my-overview/my-overview.ts b/src/core/courses/pages/my-overview/my-overview.ts index 279439e98..bb5243bba 100644 --- a/src/core/courses/pages/my-overview/my-overview.ts +++ b/src/core/courses/pages/my-overview/my-overview.ts @@ -17,8 +17,8 @@ import { IonicPage, Searchbar, NavController } from 'ionic-angular'; import { CoreEventsProvider } from '@providers/events'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; -import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreCoursesProvider } from '../../providers/courses'; +import { CoreCoursesHelperProvider } from '../../providers/helper'; import { CoreCoursesMyOverviewProvider } from '../../providers/my-overview'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate'; @@ -84,7 +84,7 @@ export class CoreCoursesMyOverviewPage implements OnDestroy { private domUtils: CoreDomUtilsProvider, private myOverviewProvider: CoreCoursesMyOverviewProvider, private courseHelper: CoreCourseHelperProvider, private sitesProvider: CoreSitesProvider, private siteHomeProvider: CoreSiteHomeProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate, - private eventsProvider: CoreEventsProvider, private utils: CoreUtilsProvider) { + private eventsProvider: CoreEventsProvider, private coursesHelper: CoreCoursesHelperProvider) { this.loadSiteName(); } @@ -241,20 +241,7 @@ export class CoreCoursesMyOverviewPage implements OnDestroy { this.courseIds = courseIds.join(','); - if (this.courseIds && this.coursesProvider.isGetCoursesByFieldAvailable()) { - // Load course image of all the courses. - promises.push(this.coursesProvider.getCoursesByField('ids', this.courseIds).then((coursesInfo) => { - coursesInfo = this.utils.arrayToObject(coursesInfo, 'id'); - courses.forEach((course) => { - if (coursesInfo[course.id] && coursesInfo[course.id].overviewfiles && - coursesInfo[course.id].overviewfiles[0]) { - course.imageThumb = coursesInfo[course.id].overviewfiles[0].fileurl; - } else { - course.imageThumb = false; - } - }); - })); - } + promises.push(this.coursesHelper.loadCoursesExtraInfo(courses)); return Promise.all(promises).then(() => { return courses.sort((a, b) => { diff --git a/src/core/courses/providers/helper.ts b/src/core/courses/providers/helper.ts new file mode 100644 index 000000000..2ecf338c0 --- /dev/null +++ b/src/core/courses/providers/helper.ts @@ -0,0 +1,75 @@ +// (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'; + +/** + * Helper to gather some common courses functions. + */ +@Injectable() +export class CoreCoursesHelperProvider { + + constructor(private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider) { } + + /** + * 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]) { + course.imageThumb = courseByField.overviewfiles[0].fileurl; + } else { + course.imageThumb = false; + } + } else { + delete course.displayname; + course.imageThumb = false; + } + } + + /** + * 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} Promise resolved when done. + */ + loadCoursesExtraInfo(courses: any[]): Promise { + 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]); + }); + }); + } +}