diff --git a/src/addon/mod/assign/assign.module.ts b/src/addon/mod/assign/assign.module.ts index d14a5851b..32dbecdcb 100644 --- a/src/addon/mod/assign/assign.module.ts +++ b/src/addon/mod/assign/assign.module.ts @@ -13,12 +13,15 @@ // limitations under the License. import { NgModule } from '@angular/core'; +import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; import { AddonModAssignProvider } from './providers/assign'; import { AddonModAssignOfflineProvider } from './providers/assign-offline'; +import { AddonModAssignHelperProvider } from './providers/helper'; import { AddonModAssignFeedbackDelegate } from './providers/feedback-delegate'; import { AddonModAssignSubmissionDelegate } from './providers/submission-delegate'; import { AddonModAssignDefaultFeedbackHandler } from './providers/default-feedback-handler'; import { AddonModAssignDefaultSubmissionHandler } from './providers/default-submission-handler'; +import { AddonModAssignPrefetchHandler } from './providers/prefetch-handler'; @NgModule({ declarations: [ @@ -26,10 +29,16 @@ import { AddonModAssignDefaultSubmissionHandler } from './providers/default-subm providers: [ AddonModAssignProvider, AddonModAssignOfflineProvider, + AddonModAssignHelperProvider, AddonModAssignFeedbackDelegate, AddonModAssignSubmissionDelegate, AddonModAssignDefaultFeedbackHandler, - AddonModAssignDefaultSubmissionHandler + AddonModAssignDefaultSubmissionHandler, + AddonModAssignPrefetchHandler ] }) -export class AddonModAssignModule { } +export class AddonModAssignModule { + constructor(prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModAssignPrefetchHandler) { + prefetchDelegate.registerHandler(prefetchHandler); + } +} diff --git a/src/addon/mod/assign/providers/helper.ts b/src/addon/mod/assign/providers/helper.ts new file mode 100644 index 000000000..2fa522672 --- /dev/null +++ b/src/addon/mod/assign/providers/helper.ts @@ -0,0 +1,447 @@ +// (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 { CoreFileProvider } from '@providers/file'; +import { CoreGroupsProvider } from '@providers/groups'; +import { CoreLoggerProvider } from '@providers/logger'; +import { CoreSitesProvider } from '@providers/sites'; +import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CoreFileUploaderProvider } from '@core/fileuploader/providers/fileuploader'; +import { AddonModAssignFeedbackDelegate } from './feedback-delegate'; +import { AddonModAssignSubmissionDelegate } from './submission-delegate'; +import { AddonModAssignProvider } from './assign'; +import { AddonModAssignOfflineProvider } from './assign-offline'; + +/** + * Service that provides some helper functions for assign. + */ +@Injectable() +export class AddonModAssignHelperProvider { + protected logger; + + constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private fileProvider: CoreFileProvider, + private assignProvider: AddonModAssignProvider, private utils: CoreUtilsProvider, + private assignOffline: AddonModAssignOfflineProvider, private feedbackDelegate: AddonModAssignFeedbackDelegate, + private submissionDelegate: AddonModAssignSubmissionDelegate, private fileUploaderProvider: CoreFileUploaderProvider, + private groupsProvider: CoreGroupsProvider) { + this.logger = logger.getInstance('AddonModAssignHelperProvider'); + } + + /** + * Check if a submission can be edited in offline. + * + * @param {any} assign Assignment. + * @param {any} submission Submission. + * @return {boolean} Whether it can be edited offline. + */ + canEditSubmissionOffline(assign: any, submission: any): boolean { + if (!submission) { + return false; + } + + if (submission.status == AddonModAssignProvider.SUBMISSION_STATUS_NEW || + submission.status == AddonModAssignProvider.SUBMISSION_STATUS_REOPENED) { + // It's a new submission, allow creating it in offline. + return true; + } + + for (let i = 0; i < submission.plugins.length; i++) { + const plugin = submission.plugins[i]; + if (!this.submissionDelegate.canPluginEditOffline(assign, submission, plugin)) { + return false; + } + } + + return true; + } + + /** + * Clear plugins temporary data because a submission was cancelled. + * + * @param {any} assign Assignment. + * @param {any} submission Submission to clear the data for. + * @param {any} inputData Data entered in the submission form. + */ + clearSubmissionPluginTmpData(assign: any, submission: any, inputData: any): void { + submission.plugins.forEach((plugin) => { + this.submissionDelegate.clearTmpData(assign, submission, plugin, inputData); + }); + } + + /** + * Copy the data from last submitted attempt to the current submission. + * Since we don't have any WS for that we'll have to re-submit everything manually. + * + * @param {any} assign Assignment. + * @param {any} previousSubmission Submission to copy. + * @return {Promise} Promise resolved when done. + */ + copyPreviousAttempt(assign: any, previousSubmission: any): Promise { + const pluginData = {}, + promises = []; + + previousSubmission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.copyPluginSubmissionData(assign, plugin, pluginData)); + }); + + return Promise.all(promises).then(() => { + // We got the plugin data. Now we need to submit it. + if (Object.keys(pluginData).length) { + // There's something to save. + return this.assignProvider.saveSubmissionOnline(assign.id, pluginData); + } + }); + } + + /** + * Delete stored submission files for a plugin. See storeSubmissionFiles. + * + * @param {number} assignId Assignment ID. + * @param {string} folderName Name of the plugin folder. Must be unique (both in submission and feedback plugins). + * @param {number} [userId] User ID. If not defined, site's current user. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when done. + */ + deleteStoredSubmissionFiles(assignId: number, folderName: string, userId?: number, siteId?: string): Promise { + return this.assignOffline.getSubmissionPluginFolder(assignId, folderName, userId, siteId).then((folderPath) => { + return this.fileProvider.removeDir(folderPath); + }); + } + + /** + * Delete all drafts of the feedback plugin data. + * + * @param {number} assignId Assignment Id. + * @param {number} userId User Id. + * @param {any} feedback Feedback data. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when done. + */ + discardFeedbackPluginData(assignId: number, userId: number, feedback: any, siteId?: string): Promise { + const promises = []; + + feedback.plugins.forEach((plugin) => { + promises.push(this.feedbackDelegate.discardPluginFeedbackData(assignId, userId, plugin, siteId)); + }); + + return Promise.all(promises); + } + + /** + * List the participants for a single assignment, with some summary info about their submissions. + * + * @param {any} assign Assignment object + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise { + siteId = siteId || this.sitesProvider.getCurrentSiteId(); + + // Get the participants without specifying a group. + return this.assignProvider.listParticipants(assign.id, undefined, siteId).then((participants) => { + if (participants && participants.length > 0) { + return participants; + } + + // If no participants returned, get participants by groups. + return this.groupsProvider.getActivityAllowedGroupsIfEnabled(assign.cmid, undefined, siteId).then((userGroups) => { + const promises = [], + participants = {}; + + userGroups.forEach((userGroup) => { + promises.push(this.assignProvider.listParticipants(assign.id, userGroup.id, siteId).then((parts) => { + // Do not get repeated users. + parts.forEach((participant) => { + participants[participant.id] = participant; + }); + })); + }); + + return Promise.all(promises).then(() => { + return this.utils.objectToArray(participants); + }); + }); + }); + } + + /** + * Get plugin config from assignment config. + * + * @param {any} assign Assignment object including all config. + * @param {string} subtype Subtype name (assignsubmission or assignfeedback) + * @param {string} type Name of the subplugin. + * @return {any} Object containing all configurations of the subplugin selected. + */ + getPluginConfig(assign: any, subtype: string, type: string): any { + const configs = {}; + + assign.configs.forEach((config) => { + if (config.subtype == subtype && config.plugin == type) { + configs[config.name] = config.value; + } + }); + + return configs; + } + + /** + * Get enabled subplugins. + * + * @param {any} assign Assignment object including all config. + * @param {string} subtype Subtype name (assignsubmission or assignfeedback) + * @return {any} List of enabled plugins for the assign. + */ + getPluginsEnabled(assign: any, subtype: string): any[] { + const enabled = []; + + assign.configs.forEach((config) => { + if (config.subtype == subtype && config.name == 'enabled' && parseInt(config.value, 10) === 1) { + // Format the plugin objects. + enabled.push({ + type: config.plugin + }); + } + }); + + return enabled; + } + + /** + * Get a list of stored submission files. See storeSubmissionFiles. + * + * @param {number} assignId Assignment ID. + * @param {string} folderName Name of the plugin folder. Must be unique (both in submission and feedback plugins). + * @param {number} [userId] User ID. If not defined, site's current user. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with the files. + */ + getStoredSubmissionFiles(assignId: number, folderName: string, userId?: number, siteId?: string): Promise { + return this.assignOffline.getSubmissionPluginFolder(assignId, folderName, userId, siteId).then((folderPath) => { + return this.fileProvider.getDirectoryContents(folderPath); + }); + } + + /** + * Get the size that will be uploaded to perform an attempt copy. + * + * @param {any} assign Assignment. + * @param {any} previousSubmission Submission to copy. + * @return {Promise} Promise resolved with the size. + */ + getSubmissionSizeForCopy(assign: any, previousSubmission: any): Promise { + const promises = []; + let totalSize = 0; + + previousSubmission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.getPluginSizeForCopy(assign, plugin).then((size) => { + totalSize += size; + })); + }); + + return Promise.all(promises).then(() => { + return totalSize; + }); + } + + /** + * Get the size that will be uploaded to save a submission. + * + * @param {any} assign Assignment. + * @param {any} submission Submission to check data. + * @param {any} inputData Data entered in the submission form. + * @return {Promise} Promise resolved with the size. + */ + getSubmissionSizeForEdit(assign: any, submission: any, inputData: any): Promise { + const promises = []; + let totalSize = 0; + + submission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.getPluginSizeForEdit(assign, submission, plugin, inputData).then((size) => { + totalSize += size; + })); + }); + + return Promise.all(promises).then(() => { + return totalSize; + }); + } + + /** + * Check if the feedback data has changed for a certain submission and assign. + * + * @param {any} assign Assignment. + * @param {number} userId User Id. + * @param {any} feedback Feedback data. + * @return {Promise} Promise resolved with true if data has changed, resolved with false otherwise. + */ + hasFeedbackDataChanged(assign: any, userId: number, feedback: any): Promise { + const promises = []; + let hasChanged = false; + + feedback.plugins.forEach((plugin) => { + promises.push(this.prepareFeedbackPluginData(assign.id, userId, feedback).then((inputData) => { + return this.feedbackDelegate.hasPluginDataChanged(assign, userId, plugin, inputData, userId).then((changed) => { + if (changed) { + hasChanged = true; + } + }); + }).catch(() => { + // Ignore errors. + })); + }); + + return this.utils.allPromises(promises).then(() => { + return hasChanged; + }); + } + + /** + * Check if the submission data has changed for a certain submission and assign. + * + * @param {any} assign Assignment. + * @param {any} submission Submission to check data. + * @param {any} inputData Data entered in the submission form. + * @return {Promise} Promise resolved with true if data has changed, resolved with false otherwise. + */ + hasSubmissionDataChanged(assign: any, submission: any, inputData: any): Promise { + const promises = []; + let hasChanged = false; + + submission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.hasPluginDataChanged(assign, submission, plugin, inputData).then((changed) => { + if (changed) { + hasChanged = true; + } + }).catch(() => { + // Ignore errors. + })); + }); + + return this.utils.allPromises(promises).then(() => { + return hasChanged; + }); + } + + /** + * Prepare and return the plugin data to send for a certain feedback and assign. + * + * @param {number} assignId Assignment Id. + * @param {number} userId User Id. + * @param {any} feedback Feedback data. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with plugin data to send to server. + */ + prepareFeedbackPluginData(assignId: number, userId: number, feedback: any, siteId?: string): Promise { + const pluginData = {}, + promises = []; + + feedback.plugins.forEach((plugin) => { + promises.push(this.feedbackDelegate.preparePluginFeedbackData(assignId, userId, plugin, pluginData, siteId)); + }); + + return Promise.all(promises).then(() => { + return pluginData; + }); + } + + /** + * Prepare and return the plugin data to send for a certain submission and assign. + * + * @param {any} assign Assignment. + * @param {any} submission Submission to check data. + * @param {any} inputData Data entered in the submission form. + * @param {boolean} [offline] True to prepare the data for an offline submission, false otherwise. + * @return {Promise} Promise resolved with plugin data to send to server. + */ + prepareSubmissionPluginData(assign: any, submission: any, inputData: any, offline?: boolean): Promise { + const pluginData = {}, + promises = []; + + submission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.preparePluginSubmissionData(assign, submission, plugin, inputData, pluginData, + offline)); + }); + + return Promise.all(promises).then(() => { + return pluginData; + }); + } + + /** + * Given a list of files (either online files or local files), store the local files in a local folder + * to be submitted later. + * + * @param {number} assignId Assignment ID. + * @param {string} folderName Name of the plugin folder. Must be unique (both in submission and feedback plugins). + * @param {any[]} files List of files. + * @param {number} [userId] User ID. If not defined, site's current user. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved if success, rejected otherwise. + */ + storeSubmissionFiles(assignId: number, folderName: string, files: any[], userId?: number, siteId?: string): Promise { + // Get the folder where to store the files. + return this.assignOffline.getSubmissionPluginFolder(assignId, folderName, userId, siteId).then((folderPath) => { + return this.fileUploaderProvider.storeFilesToUpload(folderPath, files); + }); + } + + /** + * Upload a file to a draft area. If the file is an online file it will be downloaded and then re-uploaded. + * + * @param {number} assignId Assignment ID. + * @param {any} file Online file or local FileEntry. + * @param {number} [itemId] Draft ID to use. Undefined or 0 to create a new draft ID. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with the itemId. + */ + uploadFile(assignId: number, file: any, itemId?: number, siteId?: string): Promise { + return this.fileUploaderProvider.uploadOrReuploadFile(file, itemId, AddonModAssignProvider.COMPONENT, assignId, siteId); + } + + /** + * Given a list of files (either online files or local files), upload them to a draft area and return the draft ID. + * Online files will be downloaded and then re-uploaded. + * If there are no files to upload it will return a fake draft ID (1). + * + * @param {number} assignId Assignment ID. + * @param {any[]} files List of files. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with the itemId. + */ + uploadFiles(assignId: number, files: any[], siteId?: string): Promise { + return this.fileUploaderProvider.uploadOrReuploadFiles(files, AddonModAssignProvider.COMPONENT, assignId, siteId); + } + + /** + * Upload or store some files, depending if the user is offline or not. + * + * @param {number} assignId Assignment ID. + * @param {string} folderName Name of the plugin folder. Must be unique (both in submission and feedback plugins). + * @param {any[]} files List of files. + * @param {boolean} offline True if files sould be stored for offline, false to upload them. + * @param {number} [userId] User ID. If not defined, site's current user. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when done. + */ + uploadOrStoreFiles(assignId: number, folderName: string, files: any[], offline?: boolean, userId?: number, siteId?: string) + : Promise { + + if (offline) { + return this.storeSubmissionFiles(assignId, folderName, files, userId, siteId); + } else { + return this.uploadFiles(assignId, files, siteId); + } + } +} diff --git a/src/addon/mod/assign/providers/prefetch-handler.ts b/src/addon/mod/assign/providers/prefetch-handler.ts new file mode 100644 index 000000000..e3d104265 --- /dev/null +++ b/src/addon/mod/assign/providers/prefetch-handler.ts @@ -0,0 +1,378 @@ +// (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, Injector } from '@angular/core'; +import { CoreFilepoolProvider } from '@providers/filepool'; +import { CoreGroupsProvider } from '@providers/groups'; +import { CoreTextUtilsProvider } from '@providers/utils/text'; +import { CoreCourseModulePrefetchHandlerBase } from '@core/course/classes/module-prefetch-handler'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { CoreGradesHelperProvider } from '@core/grades/providers/helper'; +import { CoreUserProvider } from '@core/user/providers/user'; +import { AddonModAssignProvider } from './assign'; +import { AddonModAssignHelperProvider } from './helper'; +import { AddonModAssignFeedbackDelegate } from './feedback-delegate'; +import { AddonModAssignSubmissionDelegate } from './submission-delegate'; + +/** + * Handler to prefetch assigns. + */ +@Injectable() +export class AddonModAssignPrefetchHandler extends CoreCourseModulePrefetchHandlerBase { + name = 'AddonModAssign'; + modName = 'assign'; + component = AddonModAssignProvider.COMPONENT; + updatesNames = /^configuration$|^.*files$|^submissions$|^grades$|^gradeitems$|^outcomes$|^comments$/; + + constructor(protected injector: Injector, protected assignProvider: AddonModAssignProvider, + protected textUtils: CoreTextUtilsProvider, protected feedbackDelegate: AddonModAssignFeedbackDelegate, + protected submissionDelegate: AddonModAssignSubmissionDelegate, protected courseProvider: CoreCourseProvider, + protected courseHelper: CoreCourseHelperProvider, protected filepoolProvider: CoreFilepoolProvider, + protected groupsProvider: CoreGroupsProvider, protected gradesHelper: CoreGradesHelperProvider, + protected userProvider: CoreUserProvider, protected assignHelper: AddonModAssignHelperProvider) { + super(injector); + } + + /** + * Check if a certain module can use core_course_check_updates to check if it has updates. + * If not defined, it will assume all modules can be checked. + * The modules that return false will always be shown as outdated when they're downloaded. + * + * @param {any} module Module. + * @param {number} courseId Course ID the module belongs to. + * @return {boolean|Promise} Whether the module can use check_updates. The promise should never be rejected. + */ + canUseCheckUpdates(module: any, courseId: number): boolean | Promise { + // Teachers cannot use the WS because it doesn't check student submissions. + return this.assignProvider.getAssignment(courseId, module.id).then((assign) => { + return this.assignProvider.getSubmissions(assign.id); + }).then((data) => { + return !data.canviewsubmissions; + }).catch(() => { + return false; + }); + } + + /** + * Download the module. + * + * @param {any} module The module object returned by WS. + * @param {number} courseId Course ID. + * @param {string} [dirPath] Path of the directory where to store all the content files. @see downloadOrPrefetch. + * @return {Promise} Promise resolved when all content is downloaded. + */ + download(module: any, courseId: number, dirPath?: string): Promise { + // Same implementation for download or prefetch. + return this.prefetch(module, courseId, false, dirPath); + } + + /** + * Get list of files. If not defined, we'll assume they're in module.contents. + * + * @param {any} module Module. + * @param {Number} courseId Course ID the module belongs to. + * @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with the list of files. + */ + getFiles(module: any, courseId: number, single?: boolean, siteId?: string): Promise { + + siteId = siteId || this.sitesProvider.getCurrentSiteId(); + + return this.assignProvider.getAssignment(courseId, module.id, siteId).then((assign) => { + // Get intro files and attachments. + let files = assign.introattachments || []; + files = files.concat(this.getIntroFilesFromInstance(module, assign)); + + // Now get the files in the submissions. + return this.assignProvider.getSubmissions(assign.id, siteId).then((data) => { + const blindMarking = assign.blindmarking && !assign.revealidentities; + + if (data.canviewsubmissions) { + // Teacher, get all submissions. + return this.assignProvider.getSubmissionsUserData(data.submissions, courseId, assign.id, blindMarking, + undefined, siteId).then((submissions) => { + + const promises = []; + + // Get all the files in the submissions. + submissions.forEach((submission) => { + promises.push(this.getSubmissionFiles(assign, submission.submitid, !!submission.blindid, siteId) + .then((submissionFiles) => { + files = files.concat(submissionFiles); + })); + }); + + return Promise.all(promises).then(() => { + return files; + }); + }); + } else { + // Student, get only his/her submissions. + const userId = this.sitesProvider.getCurrentSiteUserId(); + + return this.getSubmissionFiles(assign, userId, blindMarking, siteId).then((submissionFiles) => { + files = files.concat(submissionFiles); + + return files; + }); + } + }); + }).catch(() => { + // Error getting data, return empty list. + return []; + }); + } + + /** + * Get submission files. + * + * @param {any} assign Assign. + * @param {number} submitId User ID of the submission to get. + * @param {boolean} blindMarking True if blind marking, false otherwise. + * @param {string} siteId Site ID. If not defined, current site. + * @return {Promise} Promise resolved with array of files. + */ + protected getSubmissionFiles(assign: any, submitId: number, blindMarking: boolean, siteId?: string) + : Promise { + + return this.assignProvider.getSubmissionStatus(assign.id, submitId, blindMarking, true, false, siteId).then((response) => { + const promises = []; + + if (response.lastattempt) { + const userSubmission = this.assignProvider.getSubmissionObjectFromAttempt(assign, response.lastattempt); + if (userSubmission) { + // Add submission plugin files. + userSubmission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.getPluginFiles(assign, userSubmission, plugin, siteId)); + }); + } + } + + if (response.feedback) { + // Add feedback plugin files. + response.feedback.plugins.forEach((plugin) => { + promises.push(this.feedbackDelegate.getPluginFiles(assign, response, plugin, siteId)); + }); + } + + return Promise.all(promises); + + }).then((filesLists) => { + let files = []; + + filesLists.forEach((filesList) => { + files = files.concat(filesList); + }); + + return files; + }); + } + + /** + * Invalidate the prefetched content. + * + * @param {number} moduleId The module ID. + * @param {number} courseId The course ID the module belongs to. + * @return {Promise} Promise resolved when the data is invalidated. + */ + invalidateContent(moduleId: number, courseId: number): Promise { + return this.assignProvider.invalidateContent(moduleId, courseId); + } + + /** + * Whether or not the handler is enabled on a site level. + * + * @return {boolean|Promise} A boolean, or a promise resolved with a boolean, indicating if the handler is enabled. + */ + isEnabled(): boolean | Promise { + return this.assignProvider.isPluginEnabled(); + } + + /** + * Prefetch a module. + * + * @param {any} module Module. + * @param {number} courseId Course ID the module belongs to. + * @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section. + * @param {string} [dirPath] Path of the directory where to store all the content files. @see downloadOrPrefetch. + * @return {Promise} Promise resolved when done. + */ + prefetch(module: any, courseId?: number, single?: boolean, dirPath?: string): Promise { + return this.prefetchPackage(module, courseId, single, this.prefetchAssign.bind(this)); + } + + /** + * Prefetch an assignment. + * + * @param {any} module Module. + * @param {number} courseId Course ID the module belongs to. + * @param {boolean} single True if we're downloading a single module, false if we're downloading a whole section. + * @param {String} siteId Site ID. + * @return {Promise} Promise resolved when done. + */ + protected prefetchAssign(module: any, courseId: number, single: boolean, siteId: string): Promise { + const userId = this.sitesProvider.getCurrentSiteUserId(), + promises = []; + + siteId = siteId || this.sitesProvider.getCurrentSiteId(); + + promises.push(this.courseProvider.getModuleBasicInfo(module.id, siteId)); + + // Get assignment to retrieve all its submissions. + promises.push(this.assignProvider.getAssignment(courseId, module.id, siteId).then((assign) => { + const subPromises = [], + blindMarking = assign.blindmarking && !assign.revealidentities; + + if (blindMarking) { + subPromises.push(this.assignProvider.getAssignmentUserMappings(assign.id, undefined, siteId)); + } + + subPromises.push(this.prefetchSubmissions(assign, courseId, module.id, userId, siteId)); + + subPromises.push(this.courseHelper.getModuleCourseIdByInstance(assign.id, 'assign', siteId)); + + // Get all files and fetch them. + subPromises.push(this.getFiles(module, courseId, single, siteId).then((files) => { + return this.filepoolProvider.addFilesToQueue(siteId, files, this.component, module.id); + })); + + return Promise.all(subPromises); + })); + + return Promise.all(promises); + } + + /** + * Prefetch assign submissions. + * + * @param {any} assign Assign. + * @param {number} courseId Course ID. + * @param {number} moduleId Module ID. + * @param {number} [userId] User ID. If not defined, site's current user. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when prefetched, rejected otherwise. + */ + protected prefetchSubmissions(assign: any, courseId: number, moduleId: number, userId?: number, siteId?: string): Promise { + + // Get submissions. + return this.assignProvider.getSubmissions(assign.id, siteId).then((data) => { + const promises = [], + blindMarking = assign.blindmarking && !assign.revealidentities; + + if (data.canviewsubmissions) { + // Teacher. Do not send participants to getSubmissionsUserData to retrieve user profiles. + promises.push(this.assignProvider.getSubmissionsUserData(data.submissions, courseId, assign.id, blindMarking, + undefined, siteId).then((submissions) => { + + const subPromises = []; + + submissions.forEach((submission) => { + subPromises.push(this.assignProvider.getSubmissionStatus(assign.id, submission.submitid, + !!submission.blindid, true, false, siteId).then((subm) => { + return this.prefetchSubmission(assign, courseId, moduleId, subm, submission.submitid, siteId); + })); + }); + + return Promise.all(subPromises); + })); + + // Get list participants. + promises.push(this.assignHelper.getParticipants(assign, siteId).then((participants) => { + participants.forEach((participant) => { + if (participant.profileimageurl) { + this.filepoolProvider.addToQueueByUrl(siteId, participant.profileimageurl); + } + }); + }).catch(() => { + // Fail silently (Moodle < 3.2). + })); + } else { + // Student. + promises.push(this.assignProvider.getSubmissionStatus(assign.id, userId, false, true, false, siteId) + .then((subm) => { + return this.prefetchSubmission(assign, courseId, moduleId, subm, userId, siteId); + })); + } + + promises.push(this.groupsProvider.activityHasGroups(assign.cmid)); + promises.push(this.groupsProvider.getActivityAllowedGroups(assign.cmid, undefined, siteId)); + + return Promise.all(promises); + }); + } + + /** + * Prefetch a submission. + * + * @param {any} assign Assign. + * @param {number} courseId Course ID. + * @param {number} moduleId Module ID. + * @param {any} submission Data returned by AddonModAssignProvider.getSubmissionStatus. + * @param {number} [userId] User ID. If not defined, site's current user. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when prefetched, rejected otherwise. + */ + protected prefetchSubmission(assign: any, courseId: number, moduleId: number, submission: any, userId?: number, + siteId?: string): Promise { + + const promises = [], + blindMarking = assign.blindmarking && !assign.revealidentities; + let userIds = []; + + if (submission.lastattempt) { + const userSubmission = this.assignProvider.getSubmissionObjectFromAttempt(assign, submission.lastattempt); + + // Get IDs of the members who need to submit. + if (!blindMarking && submission.lastattempt.submissiongroupmemberswhoneedtosubmit) { + userIds = userIds.concat(submission.lastattempt.submissiongroupmemberswhoneedtosubmit); + } + + if (userSubmission && userSubmission.id) { + // Prefetch submission plugins data. + userSubmission.plugins.forEach((plugin) => { + promises.push(this.submissionDelegate.prefetch(assign, userSubmission, plugin, siteId)); + }); + + // Get ID of the user who did the submission. + if (userSubmission.userid) { + userIds.push(userSubmission.userid); + } + } + } + + // Prefetch feedback. + if (submission.feedback) { + // Get profile and image of the grader. + if (submission.feedback.grade && submission.feedback.grade.grader) { + userIds.push(submission.feedback.grade.grader); + } + + if (userId) { + promises.push(this.gradesHelper.getGradeModuleItems(courseId, moduleId, userId, undefined, siteId)); + } + + // Prefetch feedback plugins data. + submission.feedback.plugins.forEach((plugin) => { + promises.push(this.feedbackDelegate.prefetch(assign, submission, plugin, siteId)); + }); + } + + // Prefetch user profiles. + promises.push(this.userProvider.prefetchProfiles(userIds, courseId, siteId)); + + return Promise.all(promises); + } +} diff --git a/src/core/user/providers/user.ts b/src/core/user/providers/user.ts index 3464a8d0f..6cb79a6f4 100644 --- a/src/core/user/providers/user.ts +++ b/src/core/user/providers/user.ts @@ -13,11 +13,11 @@ // limitations under the License. import { Injectable } from '@angular/core'; +import { CoreFilepoolProvider } from '@providers/filepool'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSite } from '@classes/site'; import { CoreSitesProvider } from '@providers/sites'; import { CoreUtilsProvider } from '@providers/utils/utils'; -import { CoreFilepoolProvider } from '@providers/filepool'; /** * Service to provide user functionalities. @@ -371,10 +371,10 @@ export class CoreUserProvider { /** * Prefetch user profiles and their images from a certain course. It prevents duplicates. * - * @param {number[]} userIds List of user IDs. - * @param {number} [courseId] Course the users belong to. - * @param {string} [siteId] Site ID. If not defined, current site. - * @return {Promise} Promise resolved when prefetched. + * @param {number[]} userIds List of user IDs. + * @param {number} [courseId] Course the users belong to. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when prefetched. */ prefetchProfiles(userIds: number[], courseId?: number, siteId?: string): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); @@ -383,11 +383,13 @@ export class CoreUserProvider { promises = []; userIds.forEach((userId) => { + userId = Number(userId); // Make sure it's a number. + // Prevent repeats and errors. - if (!treated[userId]) { + if (!isNaN(userId) && !treated[userId]) { treated[userId] = true; - promises.push(this.getProfile(userId, courseId).then((profile) => { + promises.push(this.getProfile(userId, courseId, false, siteId).then((profile) => { if (profile.profileimageurl) { this.filepoolProvider.addToQueueByUrl(siteId, profile.profileimageurl); }