2021-01-20 15:33:41 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// 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 { Directive, Input, OnInit, ElementRef } from '@angular/core';
|
|
|
|
|
|
|
|
import { CoreDomUtils } from '@services/utils/dom';
|
2021-12-16 13:29:50 +01:00
|
|
|
import { CoreCourse, CoreCourseModuleContentFile } from '@features/course/services/course';
|
|
|
|
import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper';
|
2021-05-25 11:01:23 +02:00
|
|
|
import { CoreUtilsOpenFileOptions } from '@services/utils/utils';
|
2021-01-20 15:33:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to allow downloading and open the main file of a module.
|
|
|
|
* When the item with this directive is clicked, the module will be downloaded (if needed) and opened.
|
|
|
|
* This is meant for modules like mod_resource.
|
|
|
|
*
|
|
|
|
* This directive must receive either a module or a moduleId. If no files are provided, it will use module.contents.
|
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: '[core-course-download-module-main-file]',
|
|
|
|
})
|
|
|
|
export class CoreCourseDownloadModuleMainFileDirective implements OnInit {
|
|
|
|
|
2021-12-16 13:29:50 +01:00
|
|
|
@Input() module?: CoreCourseModuleData; // The module.
|
2021-01-20 15:33:41 +01:00
|
|
|
@Input() moduleId?: string | number; // The module ID. Required if module is not supplied.
|
|
|
|
@Input() courseId?: string | number; // The course ID.
|
|
|
|
@Input() component?: string; // Component to link the file to.
|
|
|
|
@Input() componentId?: string | number; // Component ID to use in conjunction with the component. If not defined, use moduleId.
|
|
|
|
@Input() files?: CoreCourseModuleContentFile[]; // List of files of the module. If not provided, use module.contents.
|
2021-05-25 11:01:23 +02:00
|
|
|
@Input() options?: CoreUtilsOpenFileOptions = {};
|
2021-01-20 15:33:41 +01:00
|
|
|
|
|
|
|
protected element: HTMLElement;
|
|
|
|
|
|
|
|
constructor(element: ElementRef) {
|
|
|
|
this.element = element.nativeElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.element.addEventListener('click', async (ev: Event) => {
|
|
|
|
if (!this.module && !this.moduleId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
const modal = await CoreDomUtils.showModalLoading();
|
2021-01-20 15:33:41 +01:00
|
|
|
const courseId = typeof this.courseId == 'string' ? parseInt(this.courseId, 10) : this.courseId;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!this.module) {
|
|
|
|
// Try to get the module from cache.
|
|
|
|
this.moduleId = typeof this.moduleId == 'string' ? parseInt(this.moduleId, 10) : this.moduleId;
|
2021-03-02 11:41:04 +01:00
|
|
|
this.module = await CoreCourse.getModule(this.moduleId!, courseId);
|
2021-01-20 15:33:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const componentId = this.componentId || module.id;
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreCourseHelper.downloadModuleAndOpenFile(
|
2021-01-20 15:33:41 +01:00
|
|
|
this.module,
|
2021-12-16 10:46:07 +01:00
|
|
|
courseId ?? this.module.course,
|
2021-01-20 15:33:41 +01:00
|
|
|
this.component,
|
|
|
|
componentId,
|
|
|
|
this.files,
|
2021-05-25 11:01:23 +02:00
|
|
|
undefined,
|
|
|
|
this.options,
|
2021-01-20 15:33:41 +01:00
|
|
|
);
|
|
|
|
} catch (error) {
|
2021-03-02 11:41:04 +01:00
|
|
|
CoreDomUtils.showErrorModalDefault(error, 'core.errordownloading', true);
|
2021-01-20 15:33:41 +01:00
|
|
|
} finally {
|
|
|
|
modal.dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|