<commit_before>// (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 { Component, Optional, Injector, Input } from '@angular/core';
import { Content, ModalController } from 'ionic-angular';
import { CoreAppProvider } from '@providers/app';
import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreCourseModuleMainResourceComponent } from '@core/course/classes/main-resource-component';
import { AddonModBookProvider, AddonModBookContentsMap, AddonModBookTocChapter } from '../../providers/book';
import { AddonModBookPrefetchHandler } from '../../providers/prefetch-handler';
import { CoreTagProvider } from '@core/tag/providers/tag';

/**
 * Component that displays a book.
 */
@Component({
    selector: 'addon-mod-book-index',
    templateUrl: 'addon-mod-book-index.html',
})
export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComponent {
    @Input() initialChapterId: string; // The initial chapter ID to load.

    component = AddonModBookProvider.COMPONENT;
    chapterContent: string;
    previousChapter: string;
    nextChapter: string;
    tagsEnabled: boolean;

    protected chapters: AddonModBookTocChapter[];
    protected currentChapter: string;
    protected contentsMap: AddonModBookContentsMap;

    constructor(injector: Injector, private bookProvider: AddonModBookProvider, private courseProvider: CoreCourseProvider,
            private appProvider: CoreAppProvider, private prefetchDelegate: AddonModBookPrefetchHandler,
            private modalCtrl: ModalController, private tagProvider: CoreTagProvider, @Optional() private content: Content) {
        super(injector);
    }

    /**
     * Component being initialized.
     */
    ngOnInit(): void {
        super.ngOnInit();

        this.tagsEnabled = this.tagProvider.areTagsAvailableInSite();

        this.loadContent();
    }

    /**
     * Show the TOC.
     *
     * @param event Event.
     */
    showToc(event: MouseEvent): void {
        // Create the toc modal.
        const modal =  this.modalCtrl.create('AddonModBookTocPage', {
            moduleId: this.module.id,
            chapters: this.chapters,
            selected: this.currentChapter,
            courseId: this.courseId
        }, { cssClass: 'core-modal-lateral',
            showBackdrop: true,
            enableBackdropDismiss: true,
            enterAnimation: 'core-modal-lateral-transition',
            leaveAnimation: 'core-modal-lateral-transition' });

        modal.onDidDismiss((chapterId) => {
            if (chapterId) {
                this.changeChapter(chapterId);
            }
        });

        modal.present({
            ev: event
        });
    }

    /**
     * Change the current chapter.
     *
     * @param chapterId Chapter to load.
     * @return Promise resolved when done.
     */
    changeChapter(chapterId: string): void {
        if (chapterId && chapterId != this.currentChapter) {
            this.loaded = false;
            this.refreshIcon = 'spinner';
            this.loadChapter(chapterId);
        }
    }

    /**
     * Perform the invalidate content function.
     *
     * @return Resolved when done.
     */
    protected invalidateContent(): Promise<any> {
        return this.bookProvider.invalidateContent(this.module.id, this.courseId);
    }

    /**
     * Download book contents and load the current chapter.
     *
     * @param refresh Whether we're refreshing data.
     * @return Promise resolved when done.
     */
    protected fetchContent(refresh?: boolean): Promise<any> {
        const promises = [];
        let downloadFailed = false;
        let downloadFailError;

        // Try to get the book data.
        promises.push(this.bookProvider.getBook(this.courseId, this.module.id).then((book) => {
            this.dataRetrieved.emit(book);
            this.description = book.intro;
        }).catch(() => {
            // Ignore errors since this WS isn't available in some Moodle versions.
        }));

        // Download content. This function also loads module contents if needed.
        promises.push(this.prefetchDelegate.download(this.module, this.courseId).catch((error) => {
            // Mark download as failed but go on since the main files could have been downloaded.
            downloadFailed = true;
            downloadFailError = error;

            if (!this.module.contents.length) {
                // Try to load module contents for offline usage.
                return this.courseProvider.loadModuleContents(this.module, this.courseId);
            }
        }));

        return Promise.all(promises).then(() => {
            this.contentsMap = this.bookProvider.getContentsMap(this.module.contents);
            this.chapters = this.bookProvider.getTocList(this.module.contents);

            if (typeof this.currentChapter == 'undefined' && typeof this.initialChapterId != 'undefined' && this.chapters) {
                // Initial chapter set. Validate that the chapter exists.
                const chapter = this.chapters.find((chapter) => {
                    return chapter.id == this.initialChapterId;
                });

                if (chapter) {
                    this.currentChapter = this.initialChapterId;
                }
            }

            if (typeof this.currentChapter == 'undefined') {
                // Load the first chapter.
                this.currentChapter = this.bookProvider.getFirstChapter(this.chapters);
            }

            // Show chapter.
            return this.loadChapter(this.currentChapter).then(() => {
                if (downloadFailed && this.appProvider.isOnline()) {
                    // We could load the main file but the download failed. Show error message.
                    this.showErrorDownloadingSomeFiles(downloadFailError);
                }
            }).catch(() => {
                // Ignore errors, they're handled inside the loadChapter function.
            });
        }).finally(() => {
            this.fillContextMenu(refresh);
        });
    }

    /**
     * Load a book chapter.
     *
     * @param chapterId Chapter to load.
     * @return Promise resolved when done.
     */
    protected loadChapter(chapterId: string): Promise<void> {
        this.currentChapter = chapterId;
        this.domUtils.scrollToTop(this.content);

        return this.bookProvider.getChapterContent(this.contentsMap, chapterId, this.module.id).then((content) => {
            this.chapterContent = content;
            this.previousChapter = this.bookProvider.getPreviousChapter(this.chapters, chapterId);
            this.nextChapter = this.bookProvider.getNextChapter(this.chapters, chapterId);

            // Chapter loaded, log view. We don't return the promise because we don't want to block the user for this.
            this.bookProvider.logView(this.module.instance, chapterId, this.module.name).then(() => {
                // Module is completed when last chapter is viewed, so we only check completion if the last is reached.
                if (this.nextChapter == '0') {
                    this.courseProvider.checkModuleCompletion(this.courseId, this.module.completiondata);
                }
            }).catch(() => {<commit_msg>MOBILE-3350 book: Don't pass chapterId to logView when open book<commit_after>// (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 { Component, Optional, Injector, Input } from '@angular/core';
import { Content, ModalController } from 'ionic-angular';
import { CoreAppProvider } from '@providers/app';
import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreCourseModuleMainResourceComponent } from '@core/course/classes/main-resource-component';
import { AddonModBookProvider, AddonModBookContentsMap, AddonModBookTocChapter } from '../../providers/book';
import { AddonModBookPrefetchHandler } from '../../providers/prefetch-handler';
import { CoreTagProvider } from '@core/tag/providers/tag';

/**
 * Component that displays a book.
 */
@Component({
    selector: 'addon-mod-book-index',
    templateUrl: 'addon-mod-book-index.html',
})
export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComponent {
    @Input() initialChapterId: string; // The initial chapter ID to load.

    component = AddonModBookProvider.COMPONENT;
    chapterContent: string;
    previousChapter: string;
    nextChapter: string;
    tagsEnabled: boolean;

    protected chapters: AddonModBookTocChapter[];
    protected currentChapter: string;
    protected contentsMap: AddonModBookContentsMap;

    constructor(injector: Injector, private bookProvider: AddonModBookProvider, private courseProvider: CoreCourseProvider,
            private appProvider: CoreAppProvider, private prefetchDelegate: AddonModBookPrefetchHandler,
            private modalCtrl: ModalController, private tagProvider: CoreTagProvider, @Optional() private content: Content) {
        super(injector);
    }

    /**
     * Component being initialized.
     */
    ngOnInit(): void {
        super.ngOnInit();

        this.tagsEnabled = this.tagProvider.areTagsAvailableInSite();

        this.loadContent();
    }

    /**
     * Show the TOC.
     *
     * @param event Event.
     */
    showToc(event: MouseEvent): void {
        // Create the toc modal.
        const modal =  this.modalCtrl.create('AddonModBookTocPage', {
            moduleId: this.module.id,
            chapters: this.chapters,
            selected: this.currentChapter,
            courseId: this.courseId
        }, { cssClass: 'core-modal-lateral',
            showBackdrop: true,
            enableBackdropDismiss: true,
            enterAnimation: 'core-modal-lateral-transition',
            leaveAnimation: 'core-modal-lateral-transition' });

        modal.onDidDismiss((chapterId) => {
            if (chapterId) {
                this.changeChapter(chapterId);
            }
        });

        modal.present({
            ev: event
        });
    }

    /**
     * Change the current chapter.
     *
     * @param chapterId Chapter to load.
     * @return Promise resolved when done.
     */
    changeChapter(chapterId: string): void {
        if (chapterId && chapterId != this.currentChapter) {
            this.loaded = false;
            this.refreshIcon = 'spinner';
            this.loadChapter(chapterId, true);
        }
    }

    /**
     * Perform the invalidate content function.
     *
     * @return Resolved when done.
     */
    protected invalidateContent(): Promise<any> {
        return this.bookProvider.invalidateContent(this.module.id, this.courseId);
    }

    /**
     * Download book contents and load the current chapter.
     *
     * @param refresh Whether we're refreshing data.
     * @return Promise resolved when done.
     */
    protected fetchContent(refresh?: boolean): Promise<any> {
        const promises = [];
        let downloadFailed = false;
        let downloadFailError;

        // Try to get the book data.
        promises.push(this.bookProvider.getBook(this.courseId, this.module.id).then((book) => {
            this.dataRetrieved.emit(book);
            this.description = book.intro;
        }).catch(() => {
            // Ignore errors since this WS isn't available in some Moodle versions.
        }));

        // Download content. This function also loads module contents if needed.
        promises.push(this.prefetchDelegate.download(this.module, this.courseId).catch((error) => {
            // Mark download as failed but go on since the main files could have been downloaded.
            downloadFailed = true;
            downloadFailError = error;

            if (!this.module.contents.length) {
                // Try to load module contents for offline usage.
                return this.courseProvider.loadModuleContents(this.module, this.courseId);
            }
        }));

        return Promise.all(promises).then(() => {
            this.contentsMap = this.bookProvider.getContentsMap(this.module.contents);
            this.chapters = this.bookProvider.getTocList(this.module.contents);

            if (typeof this.currentChapter == 'undefined' && typeof this.initialChapterId != 'undefined' && this.chapters) {
                // Initial chapter set. Validate that the chapter exists.
                const chapter = this.chapters.find((chapter) => {
                    return chapter.id == this.initialChapterId;
                });

                if (chapter) {
                    this.currentChapter = this.initialChapterId;
                }
            }

            if (typeof this.currentChapter == 'undefined') {
                // Load the first chapter.
                this.currentChapter = this.bookProvider.getFirstChapter(this.chapters);
            }

            // Show chapter.
            return this.loadChapter(this.currentChapter, refresh).then(() => {
                if (downloadFailed && this.appProvider.isOnline()) {
                    // We could load the main file but the download failed. Show error message.
                    this.showErrorDownloadingSomeFiles(downloadFailError);
                }
            }).catch(() => {
                // Ignore errors, they're handled inside the loadChapter function.
            });
        }).finally(() => {
            this.fillContextMenu(refresh);
        });
    }

    /**
     * Load a book chapter.
     *
     * @param chapterId Chapter to load.
     * @param logChapterId Whether chapter ID should be passed to the log view function.
     * @return Promise resolved when done.
     */
    protected loadChapter(chapterId: string, logChapterId: boolean): Promise<void> {
        this.currentChapter = chapterId;
        this.domUtils.scrollToTop(this.content);

        return this.bookProvider.getChapterContent(this.contentsMap, chapterId, this.module.id).then((content) => {
            this.chapterContent = content;
            this.previousChapter = this.bookProvider.getPreviousChapter(this.chapters, chapterId);
            this.nextChapter = this.bookProvider.getNextChapter(this.chapters, chapterId);

            // Chapter loaded, log view. We don't return the promise because we don't want to block the user for this.
            this.bookProvider.logView(this.module.instance, logChapterId ? chapterId : undefined, this.module.name).then(() => {
                // Module is completed when last chapter is viewed, so we only check completion if the last is reached.
                if (this.nextChapter == '0') {
                    this.courseProvider.checkModuleCompletion(this.courseId, this.module.completiondata);
                }
            }).catch(() => {
{'fraction_non_alphanumeric': 0.10397011334135377, 'fraction_numerical': 0.0013930222250364086, 'mean_word_length': 2.341231224878358, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032861', 'n_tokens_mistral': 4159, 'n_tokens_neox': 3952, 'n_words': 1092}