MOBILE-3245 grades: Fix course data population in grades

main
Noel De Martin 2019-12-10 13:39:55 +01:00
parent ad4fc5c371
commit 5337c77b7e
5 changed files with 84 additions and 23 deletions

View File

@ -919,7 +919,7 @@ export class CoreCourseHelperProvider {
const totalOffline = offlineCompletions.length;
let loaded = 0;
offlineCompletions = this.utils.arrayToObject(offlineCompletions, 'cmid');
const offlineCompletionsMap = this.utils.arrayToObject(offlineCompletions, 'cmid');
// Load the offline data in the modules.
for (let i = 0; i < sections.length; i++) {
@ -931,7 +931,7 @@ export class CoreCourseHelperProvider {
for (let j = 0; j < section.modules.length; j++) {
const module = section.modules[j],
offlineCompletion = offlineCompletions[module.id];
offlineCompletion = offlineCompletionsMap[module.id];
if (offlineCompletion && typeof module.completiondata != 'undefined' &&
offlineCompletion.timecompleted >= module.completiondata.timecompleted * 1000) {

View File

@ -15,7 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreEventsProvider } from '@providers/events';
import { CoreLoggerProvider } from '@providers/logger';
import { CoreSitesProvider } from '@providers/sites';
import { CoreSitesProvider, ReadingStrategy } from '@providers/sites';
import { CoreSite } from '@classes/site';
/**
@ -774,18 +774,21 @@ export class CoreCoursesProvider {
* @param siteId Site to get the courses from. If not defined, use current site.
* @return Promise resolved with the courses.
*/
getUserCourses(preferCache?: boolean, siteId?: string): Promise<any[]> {
getUserCourses(preferCache?: boolean, siteId?: string, strategy?: ReadingStrategy): Promise<any[]> {
return this.sitesProvider.getSite(siteId).then((site) => {
const userId = site.getUserId(),
data: any = {
userid: userId
},
strategyPreSets = strategy
? this.sitesProvider.getReadingStrategyPreSets(strategy)
: { omitExpires: !!preferCache },
preSets = {
cacheKey: this.getUserCoursesCacheKey(),
getCacheUsingCacheKey: true,
omitExpires: !!preferCache,
updateFrequency: CoreSite.FREQUENCY_RARELY
updateFrequency: CoreSite.FREQUENCY_RARELY,
...strategyPreSets,
};
if (site.isVersionGreaterEqualThan('3.7')) {

View File

@ -107,7 +107,7 @@ export class CoreCoursesHelperProvider {
return Promise.resolve();
}
let coursesInfo = [],
let coursesInfo = {},
courseInfoAvailable = false;
const site = this.sitesProvider.getCurrentSite(),

View File

@ -15,7 +15,7 @@
import { Injectable } from '@angular/core';
import { NavController } from 'ionic-angular';
import { CoreLoggerProvider } from '@providers/logger';
import { CoreSitesProvider } from '@providers/sites';
import { CoreSitesProvider, ReadingStrategy } from '@providers/sites';
import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreGradesProvider } from './grades';
@ -198,22 +198,50 @@ export class CoreGradesHelperProvider {
* @param grades Grades to get the data for.
* @return Promise always resolved. Resolve param is the formatted grades.
*/
getGradesCourseData(grades: any): Promise<any> {
// Using cache for performance reasons.
return this.coursesProvider.getUserCourses(true).then((courses) => {
const indexedCourses = {};
courses.forEach((course) => {
indexedCourses[course.id] = course;
});
async getGradesCourseData(grades: any[]): Promise<any> {
// Obtain courses from cache to prevent network requests.
const courses = await this.coursesProvider.getUserCourses(undefined, undefined, ReadingStrategy.OnlyCache);
grades.forEach((grade) => {
if (typeof indexedCourses[grade.courseid] != 'undefined') {
grade.courseFullName = indexedCourses[grade.courseid].fullname;
const coursesMap = this.utils.arrayToObject(courses, 'id');
const coursesWereMissing = this.addCourseData(grades, coursesMap);
// If any course wasn't found, make a network request.
if (coursesWereMissing) {
const coursesPromise = this.coursesProvider.isGetCoursesByFieldAvailable()
? this.coursesProvider.getCoursesByField('ids', grades.map((grade) => grade.courseid))
: this.coursesProvider.getUserCourses(undefined, undefined, ReadingStrategy.PreferNetwork);
const courses = await coursesPromise;
const coursesMap = this.utils.arrayToObject(courses, 'id');
this.addCourseData(grades, coursesMap);
}
});
return grades;
});
return grades.filter((grade) => grade.courseFullName !== undefined);
}
/**
* Adds course data to grades.
*
* @param grades Array of grades to populate.
* @param courses HashMap of courses to read data from.
* @return Boolean indicating if some courses were not found.
*/
protected addCourseData(grades: any[], courses: any): boolean {
let someCoursesAreMissing = false;
for (const grade of grades) {
if (!(grade.courseid in courses)) {
someCoursesAreMissing = true;
continue;
}
grade.courseFullName = courses[grade.courseid].fullname;
}
return someCoursesAreMissing;
}
/**

View File

@ -26,7 +26,7 @@ import { CoreUtilsProvider } from './utils/utils';
import { CoreWSProvider } from './ws';
import { CoreConstants } from '@core/constants';
import { CoreConfigConstants } from '../configconstants';
import { CoreSite } from '@classes/site';
import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb';
import { Md5 } from 'ts-md5/dist/md5';
import { WP_PROVIDER } from '@app/app.module';
@ -158,6 +158,12 @@ export interface CoreSiteSchema {
migrate?(db: SQLiteDB, oldVersion: number, siteId: string): Promise<any> | void;
}
export const enum ReadingStrategy {
OnlyCache,
PreferCache,
PreferNetwork,
}
/*
* Service to manage and interact with sites.
* It allows creating tables in the databases of all sites. Each service or component should be responsible of creating
@ -1730,4 +1736,28 @@ export class CoreSitesProvider {
return reset;
}
/**
* Returns presets for a given reading strategy.
*
* @param strategy Reading strategy.
* @return PreSets options object.
*/
getReadingStrategyPreSets(strategy: ReadingStrategy): CoreSiteWSPreSets {
switch (strategy) {
case ReadingStrategy.PreferCache:
return {
omitExpires: true,
};
case ReadingStrategy.OnlyCache:
return {
omitExpires: true,
forceOffline: true,
};
case ReadingStrategy.PreferNetwork:
default:
return {};
}
}
}