From daeb78489f1b66e5246126f1d6e120e395bd12c3 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Fri, 1 Jun 2018 15:47:46 +0200 Subject: [PATCH] MOBILE-2354 core: Implement makeGradesMenu method in grades helper --- src/core/grades/providers/helper.ts | 54 ++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/core/grades/providers/helper.ts b/src/core/grades/providers/helper.ts index 4f40b6caf..c7c7a62a1 100644 --- a/src/core/grades/providers/helper.ts +++ b/src/core/grades/providers/helper.ts @@ -20,6 +20,7 @@ import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreGradesProvider } from './grades'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreUrlUtilsProvider } from '@providers/utils/url'; +import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; /** @@ -32,7 +33,7 @@ export class CoreGradesHelperProvider { constructor(logger: CoreLoggerProvider, private coursesProvider: CoreCoursesProvider, private gradesProvider: CoreGradesProvider, private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider, private courseProvider: CoreCourseProvider, - private domUtils: CoreDomUtilsProvider, private urlUtils: CoreUrlUtilsProvider) { + private domUtils: CoreDomUtilsProvider, private urlUtils: CoreUrlUtilsProvider, private utils: CoreUtilsProvider) { this.logger = logger.getInstance('CoreGradesHelperProvider'); } @@ -446,4 +447,55 @@ export class CoreGradesHelperProvider { return row; } + + /** + * Creates an array that represents all the current grades that can be chosen using the given grading type. + * Negative numbers are scales, zero is no grade, and positive numbers are maximum grades. + * + * Taken from make_grades_menu on moodlelib.php + * + * @param {number} gradingType If positive, max grade you can provide. If negative, scale Id. + * @param {number} moduleId Module Id needed to retrieve the scale. + * @param {string} [defaultLabel] Element that will become default option, if not defined, it won't be added. + * @param {any} [defaultValue] Element that will become default option value. Default ''. + * @param {string} [scale] Scale csv list String. If not provided, it will take it from the module grade info. + * @return {Promise} Array with objects with value and label to create a propper HTML select. + */ + makeGradesMenu(gradingType: number, moduleId: number, defaultLabel: string = '', defaultValue: any = '', scale?: string): + Promise { + if (gradingType < 0) { + if (scale) { + return Promise.resolve(this.utils.makeMenuFromList(scale, defaultLabel, undefined, defaultValue)); + } else { + return this.courseProvider.getModuleBasicGradeInfo(moduleId).then((gradeInfo) => { + if (gradeInfo.scale) { + return this.utils.makeMenuFromList(gradeInfo.scale, defaultLabel, undefined, defaultValue); + } + + return []; + }); + } + } + + if (gradingType > 0) { + const grades = []; + if (defaultLabel) { + // Key as string to avoid resorting of the object. + grades.push({ + label: defaultLabel, + value: defaultValue + }); + } + for (let i = gradingType; i >= 0; i--) { + grades.push({ + label: i + ' / ' + gradingType, + value: i + }); + } + + return Promise.resolve(grades); + } + + return Promise.resolve([]); + } }