MOBILE-3939 feedback: Attempts swipe navigation
parent
00a12df79b
commit
12e30f1c86
|
@ -0,0 +1,163 @@
|
||||||
|
// (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 { CoreRoutedItemsManagerSource } from '@classes/items-management/routed-items-manager-source';
|
||||||
|
import { CoreGroupInfo, CoreGroups } from '@services/groups';
|
||||||
|
import {
|
||||||
|
AddonModFeedback,
|
||||||
|
AddonModFeedbackProvider,
|
||||||
|
AddonModFeedbackWSAnonAttempt,
|
||||||
|
AddonModFeedbackWSAttempt,
|
||||||
|
AddonModFeedbackWSFeedback,
|
||||||
|
} from '../services/feedback';
|
||||||
|
import { AddonModFeedbackHelper } from '../services/feedback-helper';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Feedback attempts.
|
||||||
|
*/
|
||||||
|
export class AddonModFeedbackAttemptsSource extends CoreRoutedItemsManagerSource<AddonModFeedbackAttemptItem> {
|
||||||
|
|
||||||
|
readonly COURSE_ID: number;
|
||||||
|
readonly CM_ID: number;
|
||||||
|
|
||||||
|
selectedGroup?: number;
|
||||||
|
identifiable?: AddonModFeedbackWSAttempt[];
|
||||||
|
identifiableTotal?: number;
|
||||||
|
anonymous?: AddonModFeedbackWSAnonAttempt[];
|
||||||
|
anonymousTotal?: number;
|
||||||
|
groupInfo?: CoreGroupInfo;
|
||||||
|
|
||||||
|
protected feedback?: AddonModFeedbackWSFeedback;
|
||||||
|
|
||||||
|
constructor(courseId: number, cmId: number) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.COURSE_ID = courseId;
|
||||||
|
this.CM_ID = cmId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
getItemPath(attempt: AddonModFeedbackAttemptItem): string {
|
||||||
|
return attempt.id.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
getPagesLoaded(): number {
|
||||||
|
if (!this.identifiable || !this.anonymous) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageLength = this.getPageLength();
|
||||||
|
|
||||||
|
return Math.ceil(Math.max(this.anonymous.length, this.identifiable.length) / pageLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type guard to infer AddonModFeedbackWSAttempt objects.
|
||||||
|
*
|
||||||
|
* @param discussion Item to check.
|
||||||
|
* @return Whether the item is an identifieable attempt.
|
||||||
|
*/
|
||||||
|
isIdentifiableAttempt(attempt: AddonModFeedbackAttemptItem): attempt is AddonModFeedbackWSAttempt {
|
||||||
|
return 'fullname' in attempt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type guard to infer AddonModFeedbackWSAnonAttempt objects.
|
||||||
|
*
|
||||||
|
* @param discussion Item to check.
|
||||||
|
* @return Whether the item is an anonymous attempt.
|
||||||
|
*/
|
||||||
|
isAnonymousAttempt(attempt: AddonModFeedbackAttemptItem): attempt is AddonModFeedbackWSAnonAttempt {
|
||||||
|
return 'number' in attempt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidate feedback cache.
|
||||||
|
*/
|
||||||
|
async invalidateCache(): Promise<void> {
|
||||||
|
await Promise.all([
|
||||||
|
CoreGroups.invalidateActivityGroupInfo(this.CM_ID),
|
||||||
|
this.feedback && AddonModFeedback.invalidateResponsesAnalysisData(this.feedback.id),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load feedback.
|
||||||
|
*/
|
||||||
|
async loadFeedback(): Promise<void> {
|
||||||
|
this.feedback = await AddonModFeedback.getFeedback(this.COURSE_ID, this.CM_ID);
|
||||||
|
this.groupInfo = await CoreGroups.getActivityGroupInfo(this.CM_ID);
|
||||||
|
|
||||||
|
this.selectedGroup = CoreGroups.validateGroupId(this.selectedGroup, this.groupInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected getPageLength(): number {
|
||||||
|
return AddonModFeedbackProvider.PER_PAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected async loadPageItems(page: number): Promise<{ items: AddonModFeedbackAttemptItem[]; hasMoreItems: boolean }> {
|
||||||
|
if (!this.feedback) {
|
||||||
|
throw new Error('Can\'t load attempts without feeback');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await AddonModFeedbackHelper.getResponsesAnalysis(this.feedback.id, {
|
||||||
|
page,
|
||||||
|
groupId: this.selectedGroup,
|
||||||
|
cmId: this.CM_ID,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (page === 0) {
|
||||||
|
this.identifiableTotal = result.totalattempts;
|
||||||
|
this.anonymousTotal = result.totalanonattempts;
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalItemsLoaded = this.getPageLength() * (page + 1);
|
||||||
|
const pageAttempts: AddonModFeedbackAttemptItem[] = [
|
||||||
|
...result.attempts,
|
||||||
|
...result.anonattempts,
|
||||||
|
];
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: pageAttempts,
|
||||||
|
hasMoreItems: result.totalattempts > totalItemsLoaded || result.totalanonattempts > totalItemsLoaded,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected setItems(attempts: AddonModFeedbackAttemptItem[], hasMoreItems: boolean): void {
|
||||||
|
this.identifiable = attempts.filter(this.isIdentifiableAttempt);
|
||||||
|
this.anonymous = attempts.filter(this.isAnonymousAttempt);
|
||||||
|
|
||||||
|
super.setItems((this.identifiable as AddonModFeedbackAttemptItem[]).concat(this.anonymous), hasMoreItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of items that can be held in the source.
|
||||||
|
*/
|
||||||
|
export type AddonModFeedbackAttemptItem = AddonModFeedbackWSAttempt | AddonModFeedbackWSAnonAttempt;
|
|
@ -12,45 +12,47 @@
|
||||||
</ion-toolbar>
|
</ion-toolbar>
|
||||||
</ion-header>
|
</ion-header>
|
||||||
<ion-content>
|
<ion-content>
|
||||||
<core-loading [hideUntil]="loaded">
|
<core-swipe-navigation [manager]="attempts">
|
||||||
<ion-list class="ion-no-margin" *ngIf="attempt || anonAttempt">
|
<core-loading [hideUntil]="loaded">
|
||||||
<ion-item *ngIf="attempt" class="ion-text-wrap" core-user-link [userId]="attempt.userid"
|
<ion-list class="ion-no-margin" *ngIf="attempt || anonAttempt">
|
||||||
[attr.aria-label]=" 'core.user.viewprofile' | translate" [courseId]="attempt.courseid">
|
<ion-item *ngIf="attempt" class="ion-text-wrap" core-user-link [userId]="attempt.userid"
|
||||||
<core-user-avatar [user]="attempt" slot="start"></core-user-avatar>
|
[attr.aria-label]=" 'core.user.viewprofile' | translate" [courseId]="attempt.courseid">
|
||||||
<ion-label>
|
<core-user-avatar [user]="attempt" slot="start"></core-user-avatar>
|
||||||
<h2>{{attempt.fullname}}</h2>
|
<ion-label>
|
||||||
<p *ngIf="attempt.timemodified">{{attempt.timemodified * 1000 | coreFormatDate }}</p>
|
<h2>{{attempt.fullname}}</h2>
|
||||||
</ion-label>
|
<p *ngIf="attempt.timemodified">{{attempt.timemodified * 1000 | coreFormatDate }}</p>
|
||||||
</ion-item>
|
</ion-label>
|
||||||
|
</ion-item>
|
||||||
|
|
||||||
<ion-item class="ion-text-wrap" *ngIf="anonAttempt">
|
<ion-item class="ion-text-wrap" *ngIf="anonAttempt">
|
||||||
<ion-label>
|
<ion-label>
|
||||||
<h2>
|
<h2>
|
||||||
{{ 'addon.mod_feedback.response_nr' |translate }}: {{anonAttempt.number}}
|
{{ 'addon.mod_feedback.response_nr' |translate }}: {{anonAttempt.number}}
|
||||||
({{ 'addon.mod_feedback.anonymous' |translate }})
|
({{ 'addon.mod_feedback.anonymous' |translate }})
|
||||||
</h2>
|
</h2>
|
||||||
</ion-label>
|
</ion-label>
|
||||||
</ion-item>
|
</ion-item>
|
||||||
<ng-container *ngIf="items && items.length">
|
<ng-container *ngIf="items && items.length">
|
||||||
<ng-container *ngFor="let item of items">
|
<ng-container *ngFor="let item of items">
|
||||||
<core-spacer *ngIf="item.typ == 'pagebreak'"></core-spacer>
|
<core-spacer *ngIf="item.typ == 'pagebreak'"></core-spacer>
|
||||||
<ion-item class="ion-text-wrap" *ngIf="item.typ != 'pagebreak'" [color]="item.dependitem > 0 ? 'light' : ''">
|
<ion-item class="ion-text-wrap" *ngIf="item.typ != 'pagebreak'" [color]="item.dependitem > 0 ? 'light' : ''">
|
||||||
<ion-label>
|
<ion-label>
|
||||||
<h2 *ngIf="item.name" [core-mark-required]="item.required">
|
<h2 *ngIf="item.name" [core-mark-required]="item.required">
|
||||||
<span *ngIf="feedback!.autonumbering && item.itemnumber">{{item.itemnumber}}. </span>
|
<span *ngIf="feedback!.autonumbering && item.itemnumber">{{item.itemnumber}}. </span>
|
||||||
<core-format-text [component]="component" [componentId]="cmId" [text]="item.name" contextLevel="module"
|
<core-format-text [component]="component" [componentId]="cmId" [text]="item.name" contextLevel="module"
|
||||||
[contextInstanceId]="cmId" [courseId]="courseId">
|
[contextInstanceId]="cmId" [courseId]="courseId">
|
||||||
</core-format-text>
|
</core-format-text>
|
||||||
</h2>
|
</h2>
|
||||||
<p *ngIf="item.submittedValue">
|
<p *ngIf="item.submittedValue">
|
||||||
<core-format-text [component]="component" [componentId]="cmId" [text]="item.submittedValue"
|
<core-format-text [component]="component" [componentId]="cmId" [text]="item.submittedValue"
|
||||||
contextLevel="module" [contextInstanceId]="cmId" [courseId]="courseId">
|
contextLevel="module" [contextInstanceId]="cmId" [courseId]="courseId">
|
||||||
</core-format-text>
|
</core-format-text>
|
||||||
</p>
|
</p>
|
||||||
</ion-label>
|
</ion-label>
|
||||||
</ion-item>
|
</ion-item>
|
||||||
|
</ng-container>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
</ng-container>
|
</ion-list>
|
||||||
</ion-list>
|
</core-loading>
|
||||||
</core-loading>
|
</core-swipe-navigation>
|
||||||
</ion-content>
|
</ion-content>
|
||||||
|
|
|
@ -12,10 +12,14 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
||||||
|
import { ActivatedRouteSnapshot } from '@angular/router';
|
||||||
|
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
|
||||||
|
import { CoreSwipeNavigationItemsManager } from '@classes/items-management/swipe-navigation-items-manager';
|
||||||
import { CoreNavigator } from '@services/navigator';
|
import { CoreNavigator } from '@services/navigator';
|
||||||
import { CoreDomUtils } from '@services/utils/dom';
|
import { CoreDomUtils } from '@services/utils/dom';
|
||||||
import { CoreTextUtils } from '@services/utils/text';
|
import { CoreTextUtils } from '@services/utils/text';
|
||||||
|
import { AddonModFeedbackAttemptsSource } from '../../classes/feedback-attempts-source';
|
||||||
import {
|
import {
|
||||||
AddonModFeedback,
|
AddonModFeedback,
|
||||||
AddonModFeedbackProvider,
|
AddonModFeedbackProvider,
|
||||||
|
@ -32,7 +36,7 @@ import { AddonModFeedbackFormItem, AddonModFeedbackHelper } from '../../services
|
||||||
selector: 'page-addon-mod-feedback-attempt',
|
selector: 'page-addon-mod-feedback-attempt',
|
||||||
templateUrl: 'attempt.html',
|
templateUrl: 'attempt.html',
|
||||||
})
|
})
|
||||||
export class AddonModFeedbackAttemptPage implements OnInit {
|
export class AddonModFeedbackAttemptPage implements OnInit, OnDestroy {
|
||||||
|
|
||||||
protected attemptId!: number;
|
protected attemptId!: number;
|
||||||
|
|
||||||
|
@ -40,6 +44,7 @@ export class AddonModFeedbackAttemptPage implements OnInit {
|
||||||
courseId!: number;
|
courseId!: number;
|
||||||
feedback?: AddonModFeedbackWSFeedback;
|
feedback?: AddonModFeedbackWSFeedback;
|
||||||
attempt?: AddonModFeedbackWSAttempt;
|
attempt?: AddonModFeedbackWSAttempt;
|
||||||
|
attempts?: AddonModFeedbackAttemptsSwipeManager;
|
||||||
anonAttempt?: AddonModFeedbackWSAnonAttempt;
|
anonAttempt?: AddonModFeedbackWSAnonAttempt;
|
||||||
items: AddonModFeedbackAttemptItem[] = [];
|
items: AddonModFeedbackAttemptItem[] = [];
|
||||||
component = AddonModFeedbackProvider.COMPONENT;
|
component = AddonModFeedbackProvider.COMPONENT;
|
||||||
|
@ -53,6 +58,15 @@ export class AddonModFeedbackAttemptPage implements OnInit {
|
||||||
this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId');
|
this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId');
|
||||||
this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId');
|
this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId');
|
||||||
this.attemptId = CoreNavigator.getRequiredRouteNumberParam('attemptId');
|
this.attemptId = CoreNavigator.getRequiredRouteNumberParam('attemptId');
|
||||||
|
|
||||||
|
const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource(
|
||||||
|
AddonModFeedbackAttemptsSource,
|
||||||
|
[this.courseId, this.cmId],
|
||||||
|
);
|
||||||
|
|
||||||
|
this.attempts = new AddonModFeedbackAttemptsSwipeManager(source);
|
||||||
|
|
||||||
|
this.attempts.start();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
CoreDomUtils.showErrorModal(error);
|
CoreDomUtils.showErrorModal(error);
|
||||||
|
|
||||||
|
@ -64,6 +78,13 @@ export class AddonModFeedbackAttemptPage implements OnInit {
|
||||||
this.fetchData();
|
this.fetchData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
this.attempts?.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch all the data required for the view.
|
* Fetch all the data required for the view.
|
||||||
*
|
*
|
||||||
|
@ -131,3 +152,17 @@ export class AddonModFeedbackAttemptPage implements OnInit {
|
||||||
type AddonModFeedbackAttemptItem = AddonModFeedbackFormItem & {
|
type AddonModFeedbackAttemptItem = AddonModFeedbackFormItem & {
|
||||||
submittedValue?: string;
|
submittedValue?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to manage swiping within a collection of discussions.
|
||||||
|
*/
|
||||||
|
class AddonModFeedbackAttemptsSwipeManager extends CoreSwipeNavigationItemsManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null {
|
||||||
|
return route.params.attemptId;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -10,77 +10,57 @@
|
||||||
</ion-header>
|
</ion-header>
|
||||||
<ion-content>
|
<ion-content>
|
||||||
<core-split-view>
|
<core-split-view>
|
||||||
<ion-refresher slot="fixed" [disabled]="!loaded" (ionRefresh)="refreshFeedback($event.target)">
|
<ion-refresher slot="fixed" [disabled]="!attempts || !attempts.loaded" (ionRefresh)="refreshFeedback($event.target)">
|
||||||
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
|
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
|
||||||
</ion-refresher>
|
</ion-refresher>
|
||||||
<core-loading [hideUntil]="loaded">
|
<core-loading [hideUntil]="attempts && attempts.loaded">
|
||||||
<ion-list class="ion-no-margin">
|
<ion-list class="ion-no-margin">
|
||||||
<ion-item class="ion-text-wrap" *ngIf="groupInfo && (groupInfo.separateGroups || groupInfo.visibleGroups)">
|
<ion-item class="ion-text-wrap" *ngIf="groupInfo && (groupInfo.separateGroups || groupInfo.visibleGroups)">
|
||||||
<ion-label id="addon-feedback-groupslabel">
|
<ion-label id="addon-feedback-groupslabel">
|
||||||
<ng-container *ngIf="groupInfo.separateGroups">{{'core.groupsseparate' | translate }}</ng-container>
|
<ng-container *ngIf="groupInfo.separateGroups">{{'core.groupsseparate' | translate }}</ng-container>
|
||||||
<ng-container *ngIf="groupInfo.visibleGroups">{{'core.groupsvisible' | translate }}</ng-container>
|
<ng-container *ngIf="groupInfo.visibleGroups">{{'core.groupsvisible' | translate }}</ng-container>
|
||||||
</ion-label>
|
</ion-label>
|
||||||
<ion-select [(ngModel)]="selectedGroup" (ionChange)="loadAttempts(selectedGroup)"
|
<ion-select [(ngModel)]="selectedGroup" (ionChange)="reloadAttempts()" aria-labelledby="addon-feedback-groupslabel"
|
||||||
aria-labelledby="addon-feedback-groupslabel" interface="action-sheet"
|
interface="action-sheet" [interfaceOptions]="{header: 'core.group' | translate}">
|
||||||
[interfaceOptions]="{header: 'core.group' | translate}">
|
|
||||||
<ion-select-option *ngFor="let groupOpt of groupInfo.groups" [value]="groupOpt.id">
|
<ion-select-option *ngFor="let groupOpt of groupInfo.groups" [value]="groupOpt.id">
|
||||||
{{groupOpt.name}}
|
{{groupOpt.name}}
|
||||||
</ion-select-option>
|
</ion-select-option>
|
||||||
</ion-select>
|
</ion-select>
|
||||||
</ion-item>
|
</ion-item>
|
||||||
|
|
||||||
<ng-container *ngIf="attempts.identifiable.total > 0">
|
<ng-container *ngIf="identifiableAttemptsTotal > 0">
|
||||||
<ion-item-divider>
|
<ion-item-divider>
|
||||||
<ion-label>
|
<ion-label>
|
||||||
<h2>{{ 'addon.mod_feedback.non_anonymous_entries' | translate : {$a: attempts.identifiable.total } }}
|
<h2>{{ 'addon.mod_feedback.non_anonymous_entries' | translate : {$a: identifiableAttemptsTotal } }}</h2>
|
||||||
</h2>
|
|
||||||
</ion-label>
|
</ion-label>
|
||||||
</ion-item-divider>
|
</ion-item-divider>
|
||||||
<ion-item *ngFor="let attempt of attempts.identifiable.items" class="ion-text-wrap" button detail="true"
|
<ion-item *ngFor="let attempt of identifiableAttempts" class="ion-text-wrap" button detail="true"
|
||||||
(click)="attempts.select(attempt)" [attr.aria-current]="attempts.getItemAriaCurrent(attempt)">
|
(click)="attempts?.select(attempt)" [attr.aria-current]="attempts?.getItemAriaCurrent(attempt)">
|
||||||
<core-user-avatar [user]="attempt" slot="start"></core-user-avatar>
|
<core-user-avatar [user]="attempt" [linkProfile]="false" slot="start"></core-user-avatar>
|
||||||
<ion-label>
|
<ion-label>
|
||||||
<p class="item-heading">{{ attempt.fullname }}</p>
|
<p class="item-heading">{{ attempt.fullname }}</p>
|
||||||
<p *ngIf="attempt.timemodified">{{attempt.timemodified * 1000 | coreFormatDate }}</p>
|
<p *ngIf="attempt.timemodified">{{ attempt.timemodified * 1000 | coreFormatDate }}</p>
|
||||||
</ion-label>
|
|
||||||
</ion-item>
|
|
||||||
|
|
||||||
<!-- Button and spinner to show more attempts. -->
|
|
||||||
<ion-button *ngIf="attempts.identifiable.canLoadMore && !loadingMore" class="ion-margin" expand="block"
|
|
||||||
(click)="loadAttempts()">
|
|
||||||
{{ 'core.loadmore' | translate }}
|
|
||||||
</ion-button>
|
|
||||||
<ion-item *ngIf="attempts.identifiable.canLoadMore && loadingMore" class="ion-text-center">
|
|
||||||
<ion-label>
|
|
||||||
<ion-spinner [attr.aria-label]="'core.loading' | translate"></ion-spinner>
|
|
||||||
</ion-label>
|
</ion-label>
|
||||||
</ion-item>
|
</ion-item>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
<ng-container *ngIf="attempts.anonymous.total > 0">
|
<ng-container *ngIf="identifiableAttemptsTotal === identifiableAttempts.length && anonymousAttemptsTotal > 0">
|
||||||
<ion-item-divider>
|
<ion-item-divider>
|
||||||
<ion-label>
|
<ion-label>
|
||||||
<h2>{{ 'addon.mod_feedback.anonymous_entries' |translate : {$a: attempts.anonymous.total } }}</h2>
|
<h2>{{ 'addon.mod_feedback.anonymous_entries' | translate : {$a: anonymousAttemptsTotal } }}</h2>
|
||||||
</ion-label>
|
</ion-label>
|
||||||
</ion-item-divider>
|
</ion-item-divider>
|
||||||
<ion-item *ngFor="let attempt of attempts.anonymous.items" class="ion-text-wrap" button detail="true"
|
<ion-item *ngFor="let attempt of anonymousAttempts" class="ion-text-wrap" button detail="true"
|
||||||
(click)="attempts.select(attempt)" [attr.aria-current]="attempts.getItemAriaCurrent(attempt)">
|
(click)="attempts?.select(attempt)" [attr.aria-current]="attempts?.getItemAriaCurrent(attempt)">
|
||||||
<ion-label>
|
<ion-label>
|
||||||
<h2>{{ 'addon.mod_feedback.response_nr' |translate }}: {{attempt.number}}</h2>
|
<h2>{{ 'addon.mod_feedback.response_nr' | translate }}: {{attempt.number}}</h2>
|
||||||
</ion-label>
|
|
||||||
</ion-item>
|
|
||||||
|
|
||||||
<!-- Button and spinner to show more attempts. -->
|
|
||||||
<ion-button *ngIf="attempts.anonymous.canLoadMore && !loadingMore" class="ion-margin" expand="block"
|
|
||||||
(click)="loadAttempts()">
|
|
||||||
{{ 'core.loadmore' | translate }}
|
|
||||||
</ion-button>
|
|
||||||
<ion-item *ngIf="attempts.anonymous.canLoadMore && loadingMore" class="ion-text-center">
|
|
||||||
<ion-label>
|
|
||||||
<ion-spinner [attr.aria-label]="'core.loading' | translate"></ion-spinner>
|
|
||||||
</ion-label>
|
</ion-label>
|
||||||
</ion-item>
|
</ion-item>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
|
<core-infinite-loading [enabled]="attempts && attempts.loaded && !attempts.completed" [error]="fetchFailed"
|
||||||
|
(action)="fetchMoreAttempts($event)">
|
||||||
|
</core-infinite-loading>
|
||||||
</ion-list>
|
</ion-list>
|
||||||
</core-loading>
|
</core-loading>
|
||||||
</core-split-view>
|
</core-split-view>
|
||||||
|
|
|
@ -12,22 +12,19 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import { AfterViewInit, Component, ViewChild } from '@angular/core';
|
import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core';
|
||||||
import { ActivatedRoute } from '@angular/router';
|
import { ActivatedRoute } from '@angular/router';
|
||||||
import { CorePageItemsListManager } from '@classes/page-items-list-manager';
|
import { CoreRoutedItemsManagerSourcesTracker } from '@classes/items-management/routed-items-manager-sources-tracker';
|
||||||
|
import { CoreListItemsManager } from '@classes/items-management/list-items-manager';
|
||||||
|
import { CorePromisedValue } from '@classes/promised-value';
|
||||||
import { CoreSplitViewComponent } from '@components/split-view/split-view';
|
import { CoreSplitViewComponent } from '@components/split-view/split-view';
|
||||||
import { IonRefresher } from '@ionic/angular';
|
import { IonRefresher } from '@ionic/angular';
|
||||||
import { CoreGroupInfo, CoreGroups } from '@services/groups';
|
import { CoreGroupInfo } from '@services/groups';
|
||||||
import { CoreNavigator } from '@services/navigator';
|
import { CoreNavigator } from '@services/navigator';
|
||||||
import { CoreDomUtils } from '@services/utils/dom';
|
import { CoreDomUtils } from '@services/utils/dom';
|
||||||
import { CoreUtils } from '@services/utils/utils';
|
import { CoreUtils } from '@services/utils/utils';
|
||||||
import {
|
import { AddonModFeedbackAttemptItem, AddonModFeedbackAttemptsSource } from '../../classes/feedback-attempts-source';
|
||||||
AddonModFeedback,
|
import { AddonModFeedbackWSAnonAttempt, AddonModFeedbackWSAttempt } from '../../services/feedback';
|
||||||
AddonModFeedbackWSAnonAttempt,
|
|
||||||
AddonModFeedbackWSAttempt,
|
|
||||||
AddonModFeedbackWSFeedback,
|
|
||||||
} from '../../services/feedback';
|
|
||||||
import { AddonModFeedbackHelper, AddonModFeedbackResponsesAnalysis } from '../../services/feedback-helper';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page that displays feedback attempts.
|
* Page that displays feedback attempts.
|
||||||
|
@ -36,27 +33,52 @@ import { AddonModFeedbackHelper, AddonModFeedbackResponsesAnalysis } from '../..
|
||||||
selector: 'page-addon-mod-feedback-attempts',
|
selector: 'page-addon-mod-feedback-attempts',
|
||||||
templateUrl: 'attempts.html',
|
templateUrl: 'attempts.html',
|
||||||
})
|
})
|
||||||
export class AddonModFeedbackAttemptsPage implements AfterViewInit {
|
export class AddonModFeedbackAttemptsPage implements AfterViewInit, OnDestroy {
|
||||||
|
|
||||||
@ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent;
|
@ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent;
|
||||||
|
|
||||||
protected cmId!: number;
|
promisedAttempts: CorePromisedValue<CoreListItemsManager<AddonModFeedbackAttemptItem, AddonModFeedbackAttemptsSource>>;
|
||||||
protected courseId!: number;
|
fetchFailed = false;
|
||||||
protected page = 0;
|
|
||||||
protected feedback?: AddonModFeedbackWSFeedback;
|
|
||||||
|
|
||||||
attempts: AddonModFeedbackAttemptsManager;
|
constructor(protected route: ActivatedRoute) {
|
||||||
selectedGroup!: number;
|
this.promisedAttempts = new CorePromisedValue();
|
||||||
groupInfo?: CoreGroupInfo;
|
}
|
||||||
loaded = false;
|
|
||||||
loadingMore = false;
|
|
||||||
|
|
||||||
constructor(
|
get attempts(): CoreListItemsManager<AddonModFeedbackAttemptItem, AddonModFeedbackAttemptsSource> | null {
|
||||||
route: ActivatedRoute,
|
return this.promisedAttempts.value;
|
||||||
) {
|
}
|
||||||
this.attempts = new AddonModFeedbackAttemptsManager(
|
|
||||||
route.component,
|
get groupInfo(): CoreGroupInfo | undefined {
|
||||||
);
|
return this.attempts?.getSource().groupInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
get selectedGroup(): number | undefined {
|
||||||
|
return this.attempts?.getSource().selectedGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
set selectedGroup(group: number | undefined) {
|
||||||
|
if (!this.attempts) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.attempts.getSource().selectedGroup = group;
|
||||||
|
this.attempts.getSource().setDirty(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
get identifiableAttempts(): AddonModFeedbackWSAttempt[] {
|
||||||
|
return this.attempts?.getSource().identifiable ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
get identifiableAttemptsTotal(): number {
|
||||||
|
return this.attempts?.getSource().identifiableTotal ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
get anonymousAttempts(): AddonModFeedbackWSAnonAttempt[] {
|
||||||
|
return this.attempts?.getSource().anonymous ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
get anonymousAttemptsTotal(): number {
|
||||||
|
return this.attempts?.getSource().anonymousTotal ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,9 +86,16 @@ export class AddonModFeedbackAttemptsPage implements AfterViewInit {
|
||||||
*/
|
*/
|
||||||
async ngAfterViewInit(): Promise<void> {
|
async ngAfterViewInit(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId');
|
const cmId = CoreNavigator.getRequiredRouteNumberParam('cmId');
|
||||||
this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId');
|
const courseId = CoreNavigator.getRequiredRouteNumberParam('courseId');
|
||||||
this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0;
|
const source = CoreRoutedItemsManagerSourcesTracker.getOrCreateSource(
|
||||||
|
AddonModFeedbackAttemptsSource,
|
||||||
|
[courseId, cmId],
|
||||||
|
);
|
||||||
|
|
||||||
|
source.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0;
|
||||||
|
|
||||||
|
this.promisedAttempts.resolve(new CoreListItemsManager(source, this.route.component));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
CoreDomUtils.showErrorModal(error);
|
CoreDomUtils.showErrorModal(error);
|
||||||
|
|
||||||
|
@ -75,79 +104,47 @@ export class AddonModFeedbackAttemptsPage implements AfterViewInit {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.fetchData();
|
const attempts = await this.promisedAttempts;
|
||||||
|
|
||||||
this.attempts.start(this.splitView);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch all the data required for the view.
|
|
||||||
*
|
|
||||||
* @param refresh Empty events array first.
|
|
||||||
* @return Promise resolved when done.
|
|
||||||
*/
|
|
||||||
async fetchData(refresh: boolean = false): Promise<void> {
|
|
||||||
this.page = 0;
|
|
||||||
this.attempts.resetItems();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.feedback = await AddonModFeedback.getFeedback(this.courseId, this.cmId);
|
this.fetchFailed = false;
|
||||||
|
|
||||||
this.groupInfo = await CoreGroups.getActivityGroupInfo(this.cmId);
|
await attempts.getSource().loadFeedback();
|
||||||
|
await attempts.load();
|
||||||
this.selectedGroup = CoreGroups.validateGroupId(this.selectedGroup, this.groupInfo);
|
|
||||||
|
|
||||||
await this.loadGroupAttempts(this.selectedGroup);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
|
this.fetchFailed = true;
|
||||||
|
|
||||||
if (!refresh) {
|
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
|
||||||
// Some call failed on first fetch, go back.
|
|
||||||
CoreNavigator.back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await attempts.start(this.splitView);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load Group attempts.
|
* @inheritdoc
|
||||||
*
|
|
||||||
* @param groupId If defined it will change group if not, it will load more attempts for the same group.
|
|
||||||
* @return Resolved with the attempts loaded.
|
|
||||||
*/
|
*/
|
||||||
protected async loadGroupAttempts(groupId?: number): Promise<void> {
|
ngOnDestroy(): void {
|
||||||
if (groupId === undefined) {
|
this.attempts?.destroy();
|
||||||
this.page++;
|
}
|
||||||
this.loadingMore = true;
|
|
||||||
} else {
|
/**
|
||||||
this.selectedGroup = groupId;
|
* Fetch more attempts, if any.
|
||||||
this.page = 0;
|
*
|
||||||
this.attempts.resetItems();
|
* @param infiniteComplete Complete callback for infinite loader.
|
||||||
}
|
*/
|
||||||
|
async fetchMoreAttempts(infiniteComplete?: () => void): Promise<void> {
|
||||||
|
const attempts = await this.promisedAttempts;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const attempts = await AddonModFeedbackHelper.getResponsesAnalysis(this.feedback!.id, {
|
this.fetchFailed = false;
|
||||||
groupId: this.selectedGroup,
|
|
||||||
page: this.page,
|
|
||||||
cmId: this.cmId,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.attempts.setAttempts(attempts);
|
await attempts.load();
|
||||||
|
} catch (error) {
|
||||||
|
this.fetchFailed = true;
|
||||||
|
|
||||||
|
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
|
||||||
} finally {
|
} finally {
|
||||||
this.loadingMore = false;
|
infiniteComplete && infiniteComplete();
|
||||||
this.loaded = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Change selected group or load more attempts.
|
|
||||||
*
|
|
||||||
* @param groupId Group ID selected. If not defined, it will load more attempts.
|
|
||||||
*/
|
|
||||||
async loadAttempts(groupId?: number): Promise<void> {
|
|
||||||
try {
|
|
||||||
await this.loadGroupAttempts(groupId);
|
|
||||||
} catch (error) {
|
|
||||||
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,100 +154,30 @@ export class AddonModFeedbackAttemptsPage implements AfterViewInit {
|
||||||
* @param refresher Refresher.
|
* @param refresher Refresher.
|
||||||
*/
|
*/
|
||||||
async refreshFeedback(refresher: IonRefresher): Promise<void> {
|
async refreshFeedback(refresher: IonRefresher): Promise<void> {
|
||||||
const promises: Promise<void>[] = [];
|
const attempts = await this.promisedAttempts;
|
||||||
|
|
||||||
promises.push(CoreGroups.invalidateActivityGroupInfo(this.cmId));
|
|
||||||
if (this.feedback) {
|
|
||||||
promises.push(AddonModFeedback.invalidateResponsesAnalysisData(this.feedback.id));
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await CoreUtils.ignoreErrors(Promise.all(promises));
|
this.fetchFailed = false;
|
||||||
|
|
||||||
await this.fetchData(true);
|
await CoreUtils.ignoreErrors(attempts.getSource().invalidateCache());
|
||||||
|
await attempts.getSource().loadFeedback();
|
||||||
|
await attempts.reload();
|
||||||
|
} catch (error) {
|
||||||
|
this.fetchFailed = true;
|
||||||
|
|
||||||
|
CoreDomUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
|
||||||
} finally {
|
} finally {
|
||||||
refresher.complete();
|
refresher.complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Type of items that can be held by the entries manager.
|
|
||||||
*/
|
|
||||||
type EntryItem = AddonModFeedbackWSAttempt | AddonModFeedbackWSAnonAttempt;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Entries manager.
|
|
||||||
*/
|
|
||||||
class AddonModFeedbackAttemptsManager extends CorePageItemsListManager<EntryItem> {
|
|
||||||
|
|
||||||
identifiable: AddonModFeedbackIdentifiableAttempts = {
|
|
||||||
items: [],
|
|
||||||
total: 0,
|
|
||||||
canLoadMore: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
anonymous: AddonModFeedbackAnonymousAttempts = {
|
|
||||||
items: [],
|
|
||||||
total: 0,
|
|
||||||
canLoadMore: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor(pageComponent: unknown) {
|
|
||||||
super(pageComponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update attempts.
|
* Reload attempts list.
|
||||||
*
|
|
||||||
* @param attempts Attempts.
|
|
||||||
*/
|
*/
|
||||||
setAttempts(attempts: AddonModFeedbackResponsesAnalysis): void {
|
async reloadAttempts(): Promise<void> {
|
||||||
this.identifiable.total = attempts.totalattempts;
|
const attempts = await this.promisedAttempts;
|
||||||
this.anonymous.total = attempts.totalanonattempts;
|
|
||||||
|
|
||||||
if (this.anonymous.items.length < attempts.totalanonattempts) {
|
await attempts.reload();
|
||||||
this.anonymous.items = this.anonymous.items.concat(attempts.anonattempts);
|
|
||||||
}
|
|
||||||
if (this.identifiable.items.length < attempts.totalattempts) {
|
|
||||||
this.identifiable.items = this.identifiable.items.concat(attempts.attempts);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.anonymous.canLoadMore = this.anonymous.items.length < attempts.totalanonattempts;
|
|
||||||
this.identifiable.canLoadMore = this.identifiable.items.length < attempts.totalattempts;
|
|
||||||
|
|
||||||
this.setItems((<EntryItem[]> this.identifiable.items).concat(this.anonymous.items));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
resetItems(): void {
|
|
||||||
super.resetItems();
|
|
||||||
this.identifiable.total = 0;
|
|
||||||
this.identifiable.items = [];
|
|
||||||
this.anonymous.total = 0;
|
|
||||||
this.anonymous.items = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
protected getItemPath(entry: EntryItem): string {
|
|
||||||
return entry.id.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddonModFeedbackIdentifiableAttempts = {
|
|
||||||
items: AddonModFeedbackWSAttempt[];
|
|
||||||
total: number;
|
|
||||||
canLoadMore: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
type AddonModFeedbackAnonymousAttempts = {
|
|
||||||
items: AddonModFeedbackWSAnonAttempt[];
|
|
||||||
total: number;
|
|
||||||
canLoadMore: boolean;
|
|
||||||
};
|
|
||||||
|
|
|
@ -0,0 +1,147 @@
|
||||||
|
// (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.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Promise wrapper to expose result synchronously.
|
||||||
|
*/
|
||||||
|
export class CorePromisedValue<T = unknown> implements Promise<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap an existing promise.
|
||||||
|
*
|
||||||
|
* @param promise Promise.
|
||||||
|
* @returns Promised value.
|
||||||
|
*/
|
||||||
|
static from<T>(promise: Promise<T>): CorePromisedValue<T> {
|
||||||
|
const promisedValue = new CorePromisedValue<T>();
|
||||||
|
|
||||||
|
promise
|
||||||
|
.then(promisedValue.resolve.bind(promisedValue))
|
||||||
|
.catch(promisedValue.reject.bind(promisedValue));
|
||||||
|
|
||||||
|
return promisedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _resolvedValue?: T;
|
||||||
|
private _rejectedReason?: Error;
|
||||||
|
declare private promise: Promise<T>;
|
||||||
|
declare private _resolve: (result: T) => void;
|
||||||
|
declare private _reject: (error?: Error) => void;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.initPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Symbol.toStringTag]: string;
|
||||||
|
|
||||||
|
get value(): T | null {
|
||||||
|
return this._resolvedValue ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the promise resolved successfully.
|
||||||
|
*
|
||||||
|
* @return Whether the promise resolved successfuly.
|
||||||
|
*/
|
||||||
|
isResolved(): this is { value: T } {
|
||||||
|
return '_resolvedValue' in this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the promise was rejected.
|
||||||
|
*
|
||||||
|
* @return Whether the promise was rejected.
|
||||||
|
*/
|
||||||
|
isRejected(): boolean {
|
||||||
|
return '_rejectedReason' in this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the promise is settled.
|
||||||
|
*
|
||||||
|
* @returns Whether the promise is settled.
|
||||||
|
*/
|
||||||
|
isSettled(): boolean {
|
||||||
|
return this.isResolved() || this.isRejected();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
then<TResult1 = T, TResult2 = never>(
|
||||||
|
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
|
||||||
|
onRejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | undefined | null,
|
||||||
|
): Promise<TResult1 | TResult2> {
|
||||||
|
return this.promise.then(onFulfilled, onRejected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
catch<TResult = never>(
|
||||||
|
onRejected?: ((reason: Error) => TResult | PromiseLike<TResult>) | undefined | null,
|
||||||
|
): Promise<T | TResult> {
|
||||||
|
return this.promise.catch(onRejected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
finally(onFinally?: (() => void) | null): Promise<T> {
|
||||||
|
return this.promise.finally(onFinally);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the promise.
|
||||||
|
*
|
||||||
|
* @param value Promise result.
|
||||||
|
*/
|
||||||
|
resolve(value: T): void {
|
||||||
|
if (this.isSettled()) {
|
||||||
|
delete this._rejectedReason;
|
||||||
|
|
||||||
|
this.initPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
this._resolvedValue = value;
|
||||||
|
this._resolve(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reject the promise.
|
||||||
|
*
|
||||||
|
* @param value Rejection reason.
|
||||||
|
*/
|
||||||
|
reject(reason?: Error): void {
|
||||||
|
if (this.isSettled()) {
|
||||||
|
delete this._resolvedValue;
|
||||||
|
|
||||||
|
this.initPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
this._rejectedReason = reason;
|
||||||
|
this._reject(reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the promise and the callbacks.
|
||||||
|
*/
|
||||||
|
private initPromise(): void {
|
||||||
|
this.promise = new Promise((resolve, reject) => {
|
||||||
|
this._resolve = resolve;
|
||||||
|
this._reject = reject;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
// (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 { CorePromisedValue } from '../promised-value';
|
||||||
|
|
||||||
|
describe('PromisedValue', () => {
|
||||||
|
|
||||||
|
it('works like a promise', async () => {
|
||||||
|
const promisedString = new CorePromisedValue<string>();
|
||||||
|
expect(promisedString.value).toBe(null);
|
||||||
|
expect(promisedString.isResolved()).toBe(false);
|
||||||
|
|
||||||
|
promisedString.resolve('foo');
|
||||||
|
expect(promisedString.isResolved()).toBe(true);
|
||||||
|
expect(promisedString.value).toBe('foo');
|
||||||
|
|
||||||
|
const resolvedValue = await promisedString;
|
||||||
|
expect(resolvedValue).toBe('foo');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can update values', async () => {
|
||||||
|
const promisedString = new CorePromisedValue<string>();
|
||||||
|
promisedString.resolve('foo');
|
||||||
|
promisedString.resolve('bar');
|
||||||
|
|
||||||
|
expect(promisedString.isResolved()).toBe(true);
|
||||||
|
expect(promisedString.value).toBe('bar');
|
||||||
|
|
||||||
|
const resolvedValue = await promisedString;
|
||||||
|
expect(resolvedValue).toBe('bar');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue