// (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 { CoreLoggerProvider } from '../../../providers/logger'; import { CoreSitesProvider } from '../../../providers/sites'; import { TranslateService } from '@ngx-translate/core'; import { CoreCoursesProvider } from '../../courses/providers/courses'; import { CoreCourseProvider } from '../../course/providers/course'; import { CoreGradesProvider } from './grades'; import { CoreTextUtilsProvider } from '../../../providers/utils/text'; import { CoreUrlUtilsProvider } from '../../../providers/utils/url'; import { CoreDomUtilsProvider } from '../../../providers/utils/dom'; /** * Service that provides some features regarding grades information. */ @Injectable() export class CoreGradesHelperProvider { protected logger; constructor(logger: CoreLoggerProvider, private coursesProvider: CoreCoursesProvider, private gradesProvider: CoreGradesProvider, private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider, private courseProvider: CoreCourseProvider, private domUtils: CoreDomUtilsProvider, private translate: TranslateService, private urlUtils: CoreUrlUtilsProvider) { this.logger = logger.getInstance('CoreGradesHelperProvider'); } /** * Formats a row from the grades table te be rendered in a page. * * @param {any} tableRow JSON object representing row of grades table data. * @return {any} Formatted row object. */ protected formatGradeRow(tableRow: any): any { const row = {}; for (const name in tableRow) { if (typeof(tableRow[name].content) != 'undefined') { let content = tableRow[name].content; if (name == 'itemname') { this.setRowIcon(row, content); row['link'] = this.getModuleLink(content); row['rowclass'] += tableRow[name].class.indexOf('hidden') >= 0 ? ' hidden' : ''; row['rowclass'] += tableRow[name].class.indexOf('dimmed_text') >= 0 ? ' dimmed_text' : ''; content = content.replace(/<\/span>/gi, '\n'); content = this.textUtils.cleanTags(content); } else { content = this.textUtils.replaceNewLines(content, '
'); } if (content == ' ') { content = ''; } row[name] = content.trim(); } } return row; } /** * Formats a row from the grades table to be rendered in one table. * * @param {any} tableRow JSON object representing row of grades table data. * @return {any} Formatted row object. */ protected formatGradeRowForTable(tableRow: any): any { const row = {}; for (let name in tableRow) { if (typeof(tableRow[name].content) != 'undefined') { let content = tableRow[name].content; if (name == 'itemname') { this.setRowIcon(row, content); row['rowclass'] = tableRow[name].class.indexOf('leveleven') < 0 ? 'odd' : 'even'; row['rowclass'] += tableRow[name].class.indexOf('hidden') >= 0 ? ' hidden' : ''; row['rowclass'] += tableRow[name].class.indexOf('dimmed_text') >= 0 ? ' dimmed_text' : ''; content = content.replace(/<\/span>/gi, '\n'); content = this.textUtils.cleanTags(content); row['id'] = parseInt(tableRow[name].id.split('_')[1], 10); row['colspan'] = tableRow[name].colspan; row['rowspan'] = (tableRow['leader'] && tableRow['leader'].rowspan) || 1; name = 'gradeitem'; } else { content = this.textUtils.replaceNewLines(content, '
'); } if (content == ' ') { content = ''; } row[name] = content.trim(); } } return row; } /** * Removes suffix formatted to compatibilize data from table and items. * * @param {any} item Grade item to format. * @return {any} Grade item formatted. */ protected formatGradeItem(item: any): any { for (const name in item) { let index = name.indexOf('formatted'); if (index > 0) { item[name.substr(0, index)] = item[name]; } } return item; } /** * Formats the response of gradereport_user_get_grades_table to be rendered. * * @param {any} table JSON object representing a table with data. * @return {any} Formatted HTML table. */ formatGradesTable(table: any): any { const maxDepth = table.maxdepth, formatted = { columns: [], rows: [] }, // Columns, in order. columns = { gradeitem: true, weight: false, grade: false, range: false, percentage: false, lettergrade: false, rank: false, average: false, feedback: false, contributiontocoursetotal: false }; formatted.rows = table.tabledata.map((row: any) => { return this.formatGradeRowForTable(row); }).filter((row: any) => { return typeof row.gradeitem !== 'undefined'; }); // Get a row with some info. let normalRow = formatted.rows.find((e) => { return e.itemtype != 'leader' && (typeof e.grade != 'undefined' || typeof e.percentage != 'undefined'); }); // Decide if grades or percentage is being shown on phones. if (normalRow && typeof normalRow.grade != 'undefined') { columns.grade = true; } else if (normalRow && typeof normalRow.percentage != 'undefined') { columns.percentage = true; } else { normalRow = formatted.rows.find((e) => { return e.itemtype != 'leader'; }); columns.grade = true; } for (const colName in columns) { if (typeof normalRow[colName] != 'undefined') { formatted.columns.push({ name: colName, colspan: colName == 'gradeitem' ? maxDepth : 1, hiddenPhone: !columns[colName] }); } } return formatted; } /** * Get course data for grades since they only have courseid. * * @param {Object[]} grades Grades to get the data for. * @return {Promise} Promise always resolved. Resolve param is the formatted grades. */ getGradesCourseData(grades: any): Promise { // Using cache for performance reasons. return this.coursesProvider.getUserCourses(true).then((courses) => { const indexedCourses = {}; courses.forEach((course) => { indexedCourses[course.id] = course; }); grades.forEach((grade) => { if (typeof indexedCourses[grade.courseid] != 'undefined') { grade.coursefullname = indexedCourses[grade.courseid].fullname; } }); return grades; }); } /** * Get an specific grade item. * * @param {number} courseId ID of the course to get the grades from. * @param {number} gradeId Grade ID. * @param {number} [userId] ID of the user to get the grades from. If not defined use site's current user. * @param {string} [siteId] Site ID. If not defined, current site. * @param {boolean} [ignoreCache=false] True if it should ignore cached data (it will always fail in offline or server down). * @return {Promise} Promise to be resolved when the grades are retrieved. */ getGradeItem(courseId: number, gradeId: number, userId?: number, siteId?: string, ignoreCache: boolean = false): Promise { return this.gradesProvider.getCourseGradesTable(courseId, userId, siteId, ignoreCache).then((grades) => { if (grades) { return this.getGradesTableRow(grades, gradeId); } return Promise.reject(null); }); } /** * Get the grade items for a certain module. Keep in mind that may have more than one item to include outcomes and scales. * * @param {number} courseId ID of the course to get the grades from. * @param {number} moduleId Module ID. * @param {number} [userId] ID of the user to get the grades from. If not defined use site's current user. * @param {number} [groupId] ID of the group to get the grades from. Not used for old gradebook table. * @param {string} [siteId] Site ID. If not defined, current site. * @param {boolean} [ignoreCache=false] True if it should ignore cached data (it will always fail in offline or server down). * @return {Promise} Promise to be resolved when the grades are retrieved. */ getGradeModuleItems(courseId: number, moduleId: number, userId?: number, groupId?: number, siteId?: string, ignoreCache: boolean = false): Promise { return this.gradesProvider.getGradeItems(courseId, userId, groupId, siteId, ignoreCache).then((grades) => { if (grades) { if (typeof grades.tabledata != 'undefined') { // Table format. return this.getModuleGradesTableRows(grades, moduleId); } else { return grades.filter((item) => { return item.cmid == moduleId; }).map((item) => { return this.formatGradeItem(item); }); } } return Promise.reject(null); }); } /** * Gets the link to the module for the selected grade. * * @param {string} text HTML where the link is present. * @return {string | false} URL linking to the module. */ protected getModuleLink(text: string): string | false { const el = this.domUtils.toDom(text)[0], link = el.attributes['href'] ? el.attributes['href'].value : false; if (!link || link.indexOf('/mod/') < 0) { return false; } return link; } /** * Get a row from the grades table. * * @param {any} table JSON object representing a table with data. * @param {number} gradeId Grade Object identifier. * @return {any} Formatted HTML table. */ getGradesTableRow(table: any, gradeId: number): any { if (table.tabledata) { const selectedRow = table.tabledata.find((row) => { return row.itemname && row.itemname.id && row.itemname.id.substr(0, 3) == 'row' && parseInt(row.itemname.id.split('_')[1], 10) == gradeId; }); if (selectedRow) { return this.formatGradeRow(selectedRow); } } return ''; } /** * Get the rows related to a module from the grades table. * * @param {any} table JSON object representing a table with data. * @param {number} moduleId Grade Object identifier. * @return {any} Formatted HTML table. */ getModuleGradesTableRows(table: any, moduleId: number): any { if (table.tabledata) { // Find href containing "/mod/xxx/xxx.php". const regex = /href="([^"]*\/mod\/[^"|^\/]*\/[^"|^\.]*\.php[^"]*)/; return table.tabledata.filter((row) => { if (row.itemname && row.itemname.content) { const matches = row.itemname.content.match(regex); if (matches && matches.length) { const hrefParams = this.urlUtils.extractUrlParams(matches[1]); return hrefParams && hrefParams.id == moduleId; } } return false; }).map((row) => { return this.formatGradeRow(row); }); } return []; } /** * Invalidate the grade items for a certain module. * * @param {number} courseId ID of the course to invalidate the grades. * @param {number} [userId] ID of the user to invalidate. If not defined use site's current user. * @param {number} [groupId] ID of the group to invalidate. Not used for old gradebook table. * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise to be resolved when the grades are invalidated. */ invalidateGradeModuleItems(courseId: number, userId?: number, groupId?: number, siteId?: string): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); return this.sitesProvider.getSite(siteId).then((site) => { userId = userId || site.getUserId(); return this.gradesProvider.isGradeItemsAvalaible(siteId).then((enabled) => { if (enabled) { return this.gradesProvider.invalidateCourseGradesItemsData(courseId, userId, groupId, siteId); } else { return this.gradesProvider.invalidateCourseGradesData(courseId, userId, siteId); } }); }); } /** * Parses the image and sets it to the row. * * @param {any} row Formatted grade row object. * @param {string} text HTML where the image will be rendered. * @return {any} Row object with the image. */ protected setRowIcon(row: any, text: string): any { text = text.replace('%2F', '/').replace('%2f', '/'); if (text.indexOf('/agg_mean') > -1) { row['itemtype'] = 'agg_mean'; row['image'] = 'assets/img/grades/agg_mean.png'; } else if (text.indexOf('/agg_sum') > -1) { row['itemtype'] = 'agg_sum'; row['image'] = 'assets/img/grades/agg_sum.png'; } else if (text.indexOf('/outcomes') > -1 || text.indexOf('fa-tasks') > -1) { row['itemtype'] = 'outcome'; row['image'] = 'assets/img/grades/outcomes.png'; } else if (text.indexOf('i/folder') > -1 || text.indexOf('fa-folder') > -1) { row['itemtype'] = 'category'; row['icon'] = 'folder'; } else if (text.indexOf('/manual_item') > -1 || text.indexOf('fa-square-o') > -1) { row['itemtype'] = 'manual'; row['icon'] = 'square-outline'; } else if (text.indexOf('/mod/') > -1) { const module = text.match(/mod\/([^\/]*)\//); if (typeof module[1] != 'undefined') { row['itemtype'] = 'mod'; row['itemmodule'] = module[1]; row['image'] = this.courseProvider.getModuleIconSrc(module[1]); } } else if (text.indexOf('src=') > -1) { const src = text.match(/src="([^"]*)"/); row['image'] = src[1]; } return row; } }