MOBILE-2345 lesson: Implement index page and component

main
Dani Palou 2018-05-01 09:34:44 +02:00
parent 490842e426
commit 8a2fdca74b
7 changed files with 961 additions and 0 deletions

View File

@ -0,0 +1,45 @@
// (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 { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IonicModule } from 'ionic-angular';
import { TranslateModule } from '@ngx-translate/core';
import { CoreComponentsModule } from '@components/components.module';
import { CoreDirectivesModule } from '@directives/directives.module';
import { CoreCourseComponentsModule } from '@core/course/components/components.module';
import { AddonModLessonIndexComponent } from './index/index';
@NgModule({
declarations: [
AddonModLessonIndexComponent
],
imports: [
CommonModule,
IonicModule,
TranslateModule.forChild(),
CoreComponentsModule,
CoreDirectivesModule,
CoreCourseComponentsModule
],
providers: [
],
exports: [
AddonModLessonIndexComponent
],
entryComponents: [
AddonModLessonIndexComponent
]
})
export class AddonModLessonComponentsModule {}

View File

@ -0,0 +1,247 @@
<!-- Buttons to add to the header. -->
<core-navbar-buttons end>
<core-context-menu>
<core-context-menu-item *ngIf="externalUrl" [priority]="900" [content]="'core.openinbrowser' | translate" [href]="externalUrl" [iconAction]="'open'"></core-context-menu-item>
<core-context-menu-item *ngIf="description" [priority]="800" [content]="'core.moduleintro' | translate" (action)="expandDescription()" [iconAction]="'arrow-forward'"></core-context-menu-item>
<core-context-menu-item *ngIf="loaded && !hasOffline && isOnline" [priority]="700" [content]="'core.refresh' | translate" (action)="doRefresh(null, $event)" [iconAction]="refreshIcon" [closeOnClick]="false"></core-context-menu-item>
<core-context-menu-item *ngIf="loaded && hasOffline && isOnline" [priority]="600" [content]="'core.settings.synchronizenow' | translate" (action)="doRefresh(null, $event, true)" [iconAction]="syncIcon" [closeOnClick]="false"></core-context-menu-item>
<core-context-menu-item *ngIf="prefetchStatusIcon" [priority]="500" [content]="prefetchText" (action)="prefetch()" [iconAction]="prefetchStatusIcon" [closeOnClick]="false"></core-context-menu-item>
<core-context-menu-item *ngIf="size" [priority]="400" [content]="size" [iconDescription]="'cube'" (action)="removeFiles()" [iconAction]="'trash'"></core-context-menu-item>
</core-context-menu>
</core-navbar-buttons>
<!-- Content. -->
<core-loading [hideUntil]="loaded" class="core-loading-center">
<core-tabs [selectedIndex]="selectedTab">
<!-- Index/Preview tab. -->
<core-tab [title]="'addon.mod_lesson.preview' | translate">
<ng-template>
<core-course-module-description [description]="description" [component]="component" [componentId]="componentId"></core-course-module-description>
<!-- Prevent access messages. Only show the first one. -->
<div class="core-info-card" icon-start *ngIf="lesson && preventMessages && preventMessages.length">
<ion-icon name="information"></ion-icon>
<core-format-text [component]="component" [componentId]="componentId" [text]="preventMessages[0].message"></core-format-text>
</div>
<!-- Lesson has data to be synchronized -->
<div class="core-warning-card" icon-start *ngIf="hasOffline">
<ion-icon name="warning"></ion-icon>
{{ 'core.hasdatatosync' | translate: {$a: moduleName} }}
</div>
<!-- Input password for protected lessons. -->
<ion-card *ngIf="askPassword">
<form ion-list (ngSubmit)="submitPassword(passwordinput)">
<ion-item>
<core-show-password item-content [name]="'password'">
<ion-label>{{ 'addon.mod_lesson.enterpassword' | translate }}</ion-label>
<ion-input name="password" type="password" placeholder="{{ 'core.login.password' | translate }}" core-auto-focus #passwordinput></ion-input>
</core-show-password>
</ion-item>
<ion-item>
<button ion-button block type="submit" icon-end>
{{ 'addon.mod_lesson.continue' | translate }}
<ion-icon name="arrow-forward" md="ios-arrow-forward"></ion-icon>
</button>
</ion-item>
</form>
</ion-card>
<core-loading [hideUntil]="!showSpinner">
<ion-list *ngIf="lesson && (!preventMessages || !preventMessages.length)">
<ion-item text-wrap *ngIf="retakeToReview">
<!-- A retake was finished in a synchronization, allow reviewing it. -->
<p>{{ 'addon.mod_lesson.retakefinishedinsync' | translate }}</p>
<a ion-button block (click)="review()">{{ 'addon.mod_lesson.review' | translate }}</a>
</ion-item>
<ion-item text-wrap *ngIf="leftDuringTimed && !lesson.timelimit">
<!-- User left during the session and there is no time limit, ask to continue. -->
<p [innerHTML]="'addon.mod_lesson.youhaveseen' | translate"></p>
<ion-grid>
<ion-row>
<ion-col>
<a ion-button block color="light" (click)="start(false)">{{ 'core.no' | translate }}</a>
</ion-col>
<ion-col>
<a ion-button block (click)="start(true)">{{ 'core.yes' | translate }}</a>
</ion-col>
</ion-row>
</ion-grid>
</ion-item>
<ion-item text-wrap *ngIf="leftDuringTimed && lesson.timelimit && lesson.retake">
<!-- User left during the session with time limit and retakes allowed, ask to continue. -->
<p [innerHTML]="'addon.mod_lesson.leftduringtimed' | translate"></p>
<a ion-button block icon-end (click)="start(false)">
{{ 'addon.mod_lesson.continue' | translate }}
<ion-icon name="arrow-forward" md="ios-arrow-forward"></ion-icon>
</a>
</ion-item>
<ion-item text-wrap *ngIf="leftDuringTimed && lesson.timelimit && !lesson.retake">
<!-- User left during the session with time limit and retakes not allowed. This should be handled by preventMessages -->
<p [innerHTML]="'addon.mod_lesson.leftduringtimednoretake' | translate"></p>
</ion-item>
<ion-item text-wrap *ngIf="!leftDuringTimed">
<!-- User hasn't left during the session, show a start button. -->
<a ion-button block *ngIf="!canManage" icon-end (click)="start(false)">
{{ 'core.start' | translate }}
<ion-icon name="arrow-forward" md="ios-arrow-forward"></ion-icon>
</a>
<a ion-button block *ngIf="canManage" icon-end (click)="start(false)">
{{ 'addon.mod_lesson.preview' | translate }}
<ion-icon name="search"></ion-icon>
</a>
</ion-item>
</ion-list>
</core-loading>
</ng-template>
</core-tab>
<!-- Reports tab. -->
<core-tab *ngIf="canViewReports" [title]="'addon.mod_lesson.reports' | translate" (ionSelect)="reportsSelected()">
<ng-template>
<core-loading [hideUntil]="reportLoaded">
<!-- Group selector if the activity uses groups. -->
<ion-item text-wrap *ngIf="groupInfo && (groupInfo.separateGroups || groupInfo.visibleGroups)">
<ion-label id="addon-mod_lesson-groupslabel" *ngIf="groupInfo.separateGroups">{{ 'core.groupsseparate' | translate }}</ion-label>
<ion-label id="addon-mod_lesson-groupslabel" *ngIf="groupInfo.visibleGroups">{{ 'core.groupsvisible' | translate }}</ion-label>
<ion-select [(ngModel)]="group" (ionChange)="setGroup(group)" aria-labelledby="addon-mod_lesson-groupslabel" interface="popover">
<ion-option *ngFor="let groupOpt of groupInfo.groups" [value]="groupOpt.id">{{groupOpt.name}}</ion-option>
</ion-select>
</ion-item>
<!-- No lesson retakes. -->
<core-empty-box *ngIf="!overview && selectedGroupName" icon="stats" [message]="'addon.mod_lesson.nolessonattemptsgroup' | translate:{$a: selectedGroupName}">
</core-empty-box>
<core-empty-box *ngIf="!overview && !selectedGroupName" icon="stats" [message]="'addon.mod_lesson.nolessonattempts' | translate">
</core-empty-box>
<!-- General statistics for the current group. -->
<ion-card class="addon-mod_lesson-lessonstats" *ngIf="overview">
<ion-card-header text-wrap>
{{ 'addon.mod_lesson.lessonstats' | translate }}
</ion-card-header>
<!-- In tablet, max 2 rows with 3 columns. -->
<ion-list class="hidden-phone">
<ion-item text-wrap *ngIf="overview.lessonscored">
<ion-row>
<ion-col text-center>
<p class="item-heading">{{ 'addon.mod_lesson.averagescore' | translate }}</p>
<p *ngIf="overview.numofattempts > 0">{{ 'core.percentagenumber' | translate:{$a: overview.avescore} }}</p>
<p *ngIf="overview.numofattempts <= 0">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col text-center>
<p class="item-heading">{{ 'addon.mod_lesson.highscore' | translate }}</p>
<p *ngIf="overview.highscore != null">{{ 'core.percentagenumber' | translate:{$a: overview.highscore} }}</p>
<p *ngIf="overview.highscore == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col text-center>
<p class="item-heading">{{ 'addon.mod_lesson.lowscore' | translate }}</p>
<p *ngIf="overview.lowscore != null">{{ 'core.percentagenumber' | translate:{$a: overview.lowscore} }}</p>
<p *ngIf="overview.lowscore == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
</ion-row>
</ion-item>
<ion-item text-wrap>
<ion-row>
<ion-col text-center>
<p class="item-heading">{{ 'addon.mod_lesson.averagetime' | translate }}</p>
<p *ngIf="overview.avetime != null && overview.numofattempts">{{ overview.avetimeReadable }}</p>
<p *ngIf="overview.avetime == null || !overview.numofattempts">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col text-center>
<p class="item-heading">{{ 'addon.mod_lesson.hightime' | translate }}</p>
<p *ngIf="overview.hightime != null">{{ overview.hightimeReadable }}</p>
<p *ngIf="overview.hightime == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col text-center>
<p class="item-heading">{{ 'addon.mod_lesson.lowtime' | translate }}</p>
<p *ngIf="overview.lowtime != null">{{ overview.lowtimeReadable }}</p>
<p *ngIf="overview.lowtime == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
</ion-row>
</ion-item>
</ion-list>
<!-- In phone, 3 rows with 1 or 2 columns. -->
<ion-list class="hidden-tablet">
<ion-item text-wrap>
<ion-row>
<ion-col text-center *ngIf="overview.lessonscored">
<p class="item-heading">{{ 'addon.mod_lesson.averagescore' | translate }}</p>
<p *ngIf="overview.numofattempts > 0">{{ 'core.percentagenumber' | translate:{$a: overview.avescore} }}</p>
<p *ngIf="overview.numofattempts <= 0">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col [attr.text-center]="overview.lessonscored ? true : null">
<p class="item-heading">{{ 'addon.mod_lesson.averagetime' | translate }}</p>
<p *ngIf="overview.avetime != null && overview.numofattempts">{{ overview.avetimeReadable }}</p>
<p *ngIf="overview.avetime == null || !overview.numofattempts">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
</ion-row>
</ion-item>
<ion-item text-wrap>
<ion-row>
<ion-col text-center *ngIf="overview.lessonscored">
<p class="item-heading">{{ 'addon.mod_lesson.highscore' | translate }}</p>
<p *ngIf="overview.highscore != null">{{ 'core.percentagenumber' | translate:{$a: overview.highscore} }}</p>
<p *ngIf="overview.highscore == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col [attr.text-center]="overview.lessonscored ? true : null">
<p class="item-heading">{{ 'addon.mod_lesson.hightime' | translate }}</p>
<p *ngIf="overview.hightime != null">{{ overview.hightimeReadable }}</p>
<p *ngIf="overview.hightime == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
</ion-row>
</ion-item>
<ion-item text-wrap>
<ion-row>
<ion-col text-center *ngIf="overview.lessonscored">
<p class="item-heading">{{ 'addon.mod_lesson.lowscore' | translate }}</p>
<p *ngIf="overview.lowscore != null">{{ 'core.percentagenumber' | translate:{$a: overview.lowscore} }}</p>
<p *ngIf="overview.lowscore == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
<ion-col [attr.text-center]="overview.lessonscored ? true : null">
<p class="item-heading">{{ 'addon.mod_lesson.lowtime' | translate }}</p>
<p *ngIf="overview.lowtime != null">{{ overview.lowtimeReadable }}</p>
<p *ngIf="overview.lowtime == null">{{ 'addon.mod_lesson.notcompleted' | translate }}</p>
</ion-col>
</ion-row>
</ion-item>
</ion-list>
</ion-card>
<!-- List of students that have retakes. -->
<ion-card *ngIf="overview">
<ion-card-header text-wrap>
{{ 'addon.mod_lesson.overview' | translate }}
</ion-card-header>
<a ion-item text-wrap *ngFor="let student of overview.students" [navPush]="'AddonModLessonUserRetakePage'" [navParams]="{courseId: courseId, lessonId: lesson.id, userId: student.id}">
<ion-avatar item-start *ngIf="student.profileimageurl" core-user-link [userId]="student.id" [courseId]="courseId">
<img [src]="student.profileimageurl" [alt]="'core.pictureof' | translate:{$a: student.fullname}" core-external-content onError="this.src='assets/img/user-avatar.png'" role="presentation">
</ion-avatar>
<h2>{{ student.fullname }}</h2>
<core-progress-bar [progress]="student.bestgrade"></core-progress-bar>
</a>
</ion-card>
</core-loading>
</ng-template>
</core-tab>
</core-tabs>
</core-loading>

View File

@ -0,0 +1,2 @@
addon-mod-lesson-index {
}

View File

@ -0,0 +1,552 @@
// (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 { Component, Optional, Injector, Input } from '@angular/core';
import { Content, NavController } from 'ionic-angular';
import { CoreGroupsProvider, CoreGroupInfo } from '@providers/groups';
import { CoreTimeUtilsProvider } from '@providers/utils/time';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreCourseModuleMainActivityComponent } from '@core/course/classes/main-activity-component';
import { CoreUserProvider } from '@core/user/providers/user';
import { AddonModLessonProvider } from '../../providers/lesson';
import { AddonModLessonOfflineProvider } from '../../providers/lesson-offline';
import { AddonModLessonSyncProvider } from '../../providers/lesson-sync';
import { AddonModLessonPrefetchHandler } from '../../providers/prefetch-handler';
import { CoreConstants } from '@core/constants';
/**
* Component that displays a lesson entry page.
*/
@Component({
selector: 'addon-mod-lesson-index',
templateUrl: 'index.html',
})
export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityComponent {
@Input() group: number; // The group to display.
@Input() action: string; // The "action" to display first.
component = AddonModLessonProvider.COMPONENT;
moduleName = 'lesson';
lesson: any; // The lesson.
selectedTab: number; // The initial selected tab.
askPassword: boolean; // Whether to ask the password.
canManage: boolean; // Whether the user can manage the lesson.
canViewReports: boolean; // Whether the user can view the lesson reports.
showSpinner: boolean; // Whether to display a spinner.
hasOffline: boolean; // Whether there's offline data.
retakeToReview: any; // A retake to review.
preventMessages: string[]; // List of messages that prevent the lesson from being seen.
leftDuringTimed: boolean; // Whether the user has started and left a retake.
groupInfo: CoreGroupInfo; // The group info.
reportLoaded: boolean; // Whether the report data has been loaded.
selectedGroupName: string; // The name of the selected group.
overview: any; // Reports overview data.
protected syncEventName = AddonModLessonSyncProvider.AUTO_SYNCED;
protected accessInfo: any; // Lesson access info.
protected password: string; // The password for the lesson.
protected hasPlayed: boolean; // Whether the user has gone to the lesson player (attempted).
constructor(injector: Injector, protected lessonProvider: AddonModLessonProvider, @Optional() content: Content,
protected groupsProvider: CoreGroupsProvider, protected lessonOffline: AddonModLessonOfflineProvider,
protected lessonSync: AddonModLessonSyncProvider, protected utils: CoreUtilsProvider,
protected prefetchHandler: AddonModLessonPrefetchHandler, protected navCtrl: NavController,
protected timeUtils: CoreTimeUtilsProvider, protected userProvider: CoreUserProvider) {
super(injector, content);
}
/**
* Component being initialized.
*/
ngOnInit(): void {
super.ngOnInit();
this.selectedTab = this.action == 'report' ? 1 : 0;
this.loadContent(false, true).then(() => {
if (!this.lesson || (this.preventMessages && this.preventMessages.length)) {
return;
}
this.logView();
});
}
/**
* Change the group displayed.
*
* @param {number} groupId Group ID to display.
*/
changeGroup(groupId: number): void {
this.reportLoaded = false;
this.setGroup(groupId).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'Error getting report.');
}).finally(() => {
this.reportLoaded = true;
});
}
/**
* Get the lesson data.
*
* @param {boolean} [refresh=false] If it's refreshing content.
* @param {boolean} [sync=false] If the refresh is needs syncing.
* @param {boolean} [showErrors=false] If show errors to the user of hide them.
* @return {Promise<any>} Promise resolved when done.
*/
protected fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<any> {
let lessonReady = true;
this.askPassword = false;
return this.lessonProvider.getLesson(this.courseId, this.module.id).then((lessonData) => {
this.lesson = lessonData;
this.dataRetrieved.emit(this.lesson);
this.description = this.lesson.intro; // Show description only if intro is present.
if (sync) {
// Try to synchronize the lesson.
return this.syncActivity(showErrors);
}
}).then(() => {
return this.lessonProvider.getAccessInformation(this.lesson.id);
}).then((info) => {
const promises = [];
this.accessInfo = info;
this.canManage = info.canmanage;
this.canViewReports = info.canviewreports;
if (this.lessonProvider.isLessonOffline(this.lesson)) {
// Handle status.
this.setStatusListener();
// Check if there is offline data.
promises.push(this.lessonSync.hasDataToSync(this.lesson.id, info.attemptscount).then((hasOffline) => {
this.hasOffline = hasOffline;
}));
// Check if there is a retake finished in a synchronization.
promises.push(this.lessonSync.getRetakeFinishedInSync(this.lesson.id).then((retake) => {
if (retake && retake.retake == info.attemptscount - 1) {
// The retake finished is still the last retake. Allow reviewing it.
this.retakeToReview = retake;
} else {
this.retakeToReview = undefined;
if (retake) {
this.lessonSync.deleteRetakeFinishedInSync(this.lesson.id);
}
}
}));
// Update the list of content pages viewed and question attempts.
promises.push(this.lessonProvider.getContentPagesViewedOnline(this.lesson.id, info.attemptscount));
promises.push(this.lessonProvider.getQuestionsAttemptsOnline(this.lesson.id, info.attemptscount));
}
if (info.preventaccessreasons && info.preventaccessreasons.length) {
const askPassword = info.preventaccessreasons.length == 1 && this.lessonProvider.isPasswordProtected(info);
if (askPassword) {
// The lesson requires a password. Check if there is one in memory or DB.
const promise = this.password ? Promise.resolve(this.password) :
this.lessonProvider.getStoredPassword(this.lesson.id);
promises.push(promise.then((password) => {
return this.validatePassword(password);
}).catch(() => {
// No password or the validation failed. Show password form.
this.askPassword = true;
this.preventMessages = info.preventaccessreasons;
lessonReady = false;
}));
} else {
// Lesson cannot be started.
this.preventMessages = info.preventaccessreasons;
lessonReady = false;
}
}
if (this.selectedTab == 1 && this.canViewReports) {
// Only fetch the report data if the tab is selected.
promises.push(this.fetchReportData());
}
return Promise.all(promises).then(() => {
if (lessonReady) {
// Lesson can be started, don't ask the password and don't show prevent messages.
this.lessonReady(refresh);
}
});
});
}
/**
* Fetch the reports data.
*
* @return {Promise<any>} Promise resolved when done.
*/
protected fetchReportData(): Promise<any> {
return this.groupsProvider.getActivityGroupInfo(this.module.id).then((groupInfo) => {
this.groupInfo = groupInfo;
return this.setGroup(this.group || 0);
}).finally(() => {
this.reportLoaded = true;
});
}
/**
* Checks if sync has succeed from result sync data.
*
* @param {any} result Data returned on the sync function.
* @return {boolean} If suceed or not.
*/
protected hasSyncSucceed(result: any): boolean {
return result.updated;
}
/**
* User entered the page that contains the component.
*/
ionViewDidEnter(): void {
super.ionViewDidEnter();
// Update data when we come back from the player since the status could have changed.
if (this.hasPlayed) {
this.hasPlayed = false;
this.showLoadingAndRefresh(true, false);
}
}
/**
* User left the page that contains the component.
*/
ionViewDidLeave(): void {
super.ionViewDidLeave();
if (this.navCtrl.getActive().component.name == 'AddonModLessonPlayerPage') {
this.hasPlayed = true;
}
}
/**
* Perform the invalidate content function.
*
* @return {Promise<any>} Resolved when done.
*/
protected invalidateContent(): Promise<any> {
const promises = [];
promises.push(this.lessonProvider.invalidateLessonData(this.courseId));
if (this.lesson) {
promises.push(this.lessonProvider.invalidateAccessInformation(this.lesson.id));
promises.push(this.lessonProvider.invalidatePages(this.lesson.id));
promises.push(this.lessonProvider.invalidateLessonWithPassword(this.lesson.id));
promises.push(this.lessonProvider.invalidateTimers(this.lesson.id));
promises.push(this.lessonProvider.invalidateContentPagesViewed(this.lesson.id));
promises.push(this.lessonProvider.invalidateQuestionsAttempts(this.lesson.id));
promises.push(this.lessonProvider.invalidateRetakesOverview(this.lesson.id));
promises.push(this.groupsProvider.invalidateActivityGroupInfo(this.module.id));
}
return Promise.all(promises);
}
/**
* Compares sync event data with current data to check if refresh content is needed.
*
* @param {any} syncEventData Data receiven on sync observer.
* @return {boolean} True if refresh is needed, false otherwise.
*/
protected isRefreshSyncNeeded(syncEventData: any): boolean {
return this.lesson && syncEventData.lessonId == this.lesson.id;
}
/**
* Function called when the lesson is ready to be seen (no pending prevent access reasons).
*
* @param {boolean} [refresh=false] If it's refreshing content.
*/
protected lessonReady(refresh?: boolean): void {
this.askPassword = false;
this.preventMessages = [];
this.leftDuringTimed = this.hasOffline || this.lessonProvider.leftDuringTimed(this.accessInfo);
if (this.password) {
// Store the password in DB.
this.lessonProvider.storePassword(this.lesson.id, this.password);
}
// All data obtained, now fill the context menu.
this.fillContextMenu(refresh);
}
/**
* Log viewing the lesson.
*/
protected logView(): void {
this.lessonProvider.logViewLesson(this.lesson.id, this.password).then(() => {
this.courseProvider.checkModuleCompletion(this.courseId, this.module.completionstatus);
}).catch((error) => {
// Ignore errors.
});
}
/**
* Open the lesson player.
*
* @param {boolean} continueLast Whether to continue the last retake.
* @return {Promise<any>} Promise resolved when done.
*/
protected playLesson(continueLast: boolean): Promise<any> {
// Calculate the pageId to load. If there is timelimit, lesson is always restarted from the start.
let promise;
if (this.hasOffline) {
if (continueLast) {
promise = this.lessonProvider.getLastPageSeen(this.lesson.id, this.accessInfo.attemptscount);
} else {
promise = Promise.resolve(this.accessInfo.firstpageid);
}
} else if (this.leftDuringTimed && !this.lesson.timelimit) {
promise = Promise.resolve(continueLast ? this.accessInfo.lastpageseen : this.accessInfo.firstpageid);
} else {
promise = Promise.resolve();
}
return promise.then((pageId) => {
this.navCtrl.push('AddonModLessonPlayerPage', {
courseId: this.courseId,
lessonId: this.lesson.id,
pageId: pageId,
password: this.password
});
});
}
/**
* Reports tab selected.
*/
reportsSelected(): void {
if (!this.groupInfo) {
this.fetchReportData().catch((error) => {
this.domUtils.showErrorModalDefault(error, 'Error getting report.');
});
}
}
/**
* Review the lesson.
*/
review(): void {
if (!this.retakeToReview) {
// No retake to review, stop.
return;
}
this.navCtrl.push('AddonModLessonPlayerPage', {
courseId: this.courseId,
lessonId: this.lesson.id,
pageId: this.retakeToReview.pageId,
password: this.password,
review: true,
retake: this.retakeToReview.retake
});
}
/**
* Set a group to view the reports.
*
* @param {number} groupId Group ID.
* @return {Promise<any>} Promise resolved when done.
*/
protected setGroup(groupId: number): Promise<any> {
this.group = groupId;
this.selectedGroupName = '';
// Search the name of the group if it isn't all participants.
if (groupId && this.groupInfo && this.groupInfo.groups) {
for (let i = 0; i < this.groupInfo.groups.length; i++) {
const group = this.groupInfo.groups[i];
if (groupId == group.id) {
this.selectedGroupName = group.name;
break;
}
}
}
// Get the overview of retakes for the group.
return this.lessonProvider.getRetakesOverview(this.lesson.id, groupId).then((data) => {
const promises = [];
// Format times and grades.
if (data && data.avetime != null && data.numofattempts) {
data.avetime = Math.floor(data.avetime / data.numofattempts);
data.avetimeReadable = this.timeUtils.formatTime(data.avetime);
}
if (data && data.hightime != null) {
data.hightimeReadable = this.timeUtils.formatTime(data.hightime);
}
if (data && data.lowtime != null) {
data.lowtimeReadable = this.timeUtils.formatTime(data.lowtime);
}
if (data && data.lessonscored) {
if (data.numofattempts) {
data.avescore = this.textUtils.roundToDecimals(data.avescore, 2);
}
if (data.highscore != null) {
data.highscore = this.textUtils.roundToDecimals(data.highscore, 2);
}
if (data.lowscore != null) {
data.lowscore = this.textUtils.roundToDecimals(data.lowscore, 2);
}
}
if (data && data.students) {
// Get the user data for each student returned.
data.students.forEach((student) => {
student.bestgrade = this.textUtils.roundToDecimals(student.bestgrade, 2);
promises.push(this.userProvider.getProfile(student.id, this.courseId, true).then((user) => {
student.profileimageurl = user.profileimageurl;
}).catch(() => {
// Error getting profile, resolve promise without adding any extra data.
}));
});
}
return this.utils.allPromises(promises).catch(() => {
// Shouldn't happen.
}).then(() => {
this.overview = data;
});
});
}
/**
* Displays some data based on the current status.
*
* @param {string} status The current status.
* @param {string} [previousStatus] The previous status. If not defined, there is no previous status.
*/
protected showStatus(status: string, previousStatus?: string): void {
this.showSpinner = status == CoreConstants.DOWNLOADING;
}
/**
* Start the lesson.
*
* @param {boolean} [continueLast] Whether to continue the last attempt.
*/
start(continueLast?: boolean): void {
if (this.showSpinner) {
// Lesson is being downloaded, abort.
return;
}
if (this.lessonProvider.isLessonOffline(this.lesson)) {
// Lesson supports offline, check if it needs to be downloaded.
if (this.currentStatus != CoreConstants.DOWNLOADED) {
// Prefetch the lesson.
this.showSpinner = true;
this.prefetchHandler.prefetch(this.module, this.courseId, true).then(() => {
// Success downloading, open lesson.
this.playLesson(continueLast);
}).catch((error) => {
if (this.hasOffline) {
// Error downloading but there is something offline, allow continuing it.
this.playLesson(continueLast);
} else {
this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true);
}
}).finally(() => {
this.showSpinner = false;
});
} else {
// Already downloaded, open it.
this.playLesson(continueLast);
}
} else {
this.playLesson(continueLast);
}
}
/**
* Submit password for password protected lessons.
*
* @param {HTMLInputElement} passwordEl The password input.
*/
submitPassword(passwordEl: HTMLInputElement): void {
const password = passwordEl && passwordEl.value;
if (!password) {
this.domUtils.showErrorModal('addon.mod_lesson.emptypassword', true);
return;
}
this.loaded = false;
this.refreshIcon = 'spinner';
this.syncIcon = 'spinner';
this.validatePassword(password).then(() => {
// Password validated.
this.lessonReady(false);
// Log view now that we have the password.
this.logView();
}).catch((error) => {
this.domUtils.showErrorModal(error);
}).finally(() => {
this.loaded = true;
this.refreshIcon = 'refresh';
this.syncIcon = 'sync';
});
}
/**
* Performs the sync of the activity.
*
* @return {Promise<any>} Promise resolved when done.
*/
protected sync(): Promise<any> {
return this.lessonSync.syncLesson(this.lesson.id, true);
}
/**
* Validate a password and retrieve extra data.
*
* @param {string} password The password to validate.
* @return {Promise<any>} Promise resolved when done.
*/
protected validatePassword(password: string): Promise<any> {
return this.lessonProvider.getLessonWithPassword(this.lesson.id, password).then((lessonData) => {
this.lesson = lessonData;
this.password = password;
}).catch((error) => {
this.password = '';
return Promise.reject(error);
});
}
}

View File

@ -0,0 +1,16 @@
<ion-header>
<ion-navbar>
<ion-title><core-format-text [text]="title"></core-format-text></ion-title>
<ion-buttons end>
<!-- The buttons defined by the component will be added in here. -->
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ion-refresher [enabled]="lessonComponent.loaded" (ionRefresh)="lessonComponent.doRefresh($event)">
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
</ion-refresher>
<addon-mod-lesson-index [module]="module" [courseId]="courseId" [group]="group" [action]="action" (dataRetrieved)="updateData($event)"></addon-mod-lesson-index>
</ion-content>

View File

@ -0,0 +1,33 @@
// (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 { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { TranslateModule } from '@ngx-translate/core';
import { CoreDirectivesModule } from '@directives/directives.module';
import { AddonModLessonComponentsModule } from '../../components/components.module';
import { AddonModLessonIndexPage } from './index';
@NgModule({
declarations: [
AddonModLessonIndexPage,
],
imports: [
CoreDirectivesModule,
AddonModLessonComponentsModule,
IonicPageModule.forChild(AddonModLessonIndexPage),
TranslateModule.forChild()
],
})
export class AddonModLessonIndexPageModule {}

View File

@ -0,0 +1,66 @@
// (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 { Component, ViewChild } from '@angular/core';
import { IonicPage, NavParams } from 'ionic-angular';
import { AddonModLessonIndexComponent } from '../../components/index/index';
/**
* Page that displays the lesson entry page.
*/
@IonicPage({ segment: 'addon-mod-lesson-index' })
@Component({
selector: 'page-addon-mod-lesson-index',
templateUrl: 'index.html',
})
export class AddonModLessonIndexPage {
@ViewChild(AddonModLessonIndexComponent) lessonComponent: AddonModLessonIndexComponent;
title: string;
module: any;
courseId: number;
group: number; // The group to display.
action: string; // The "action" to display first.
constructor(navParams: NavParams) {
this.module = navParams.get('module') || {};
this.courseId = navParams.get('courseId');
this.group = navParams.get('group');
this.action = navParams.get('action');
this.title = this.module.name;
}
/**
* Update some data based on the lesson instance.
*
* @param {any} lesson Lesson instance.
*/
updateData(lesson: any): void {
this.title = lesson.name || this.title;
}
/**
* User entered the page.
*/
ionViewDidEnter(): void {
this.lessonComponent.ionViewDidEnter();
}
/**
* User left the page.
*/
ionViewDidLeave(): void {
this.lessonComponent.ionViewDidLeave();
}
}