@@ -16,7 +16,7 @@ import {
16
16
AddonCourseCompletion ,
17
17
AddonCourseCompletionCourseCompletionStatus ,
18
18
} from '@addons/coursecompletion/services/coursecompletion' ;
19
- import { Component , OnInit } from '@angular/core' ;
19
+ import { Component , computed , OnInit , signal } from '@angular/core' ;
20
20
import { CoreUser , CoreUserProfile } from '@features/user/services/user' ;
21
21
import { CoreAnalytics , CoreAnalyticsEventType } from '@services/analytics' ;
22
22
import { CoreLoadings } from '@services/overlays/loadings' ;
@@ -26,6 +26,7 @@ import { Translate } from '@singletons';
26
26
import { CoreTime } from '@singletons/time' ;
27
27
import { CoreAlerts } from '@services/overlays/alerts' ;
28
28
import { CoreSharedModule } from '@/core/shared.module' ;
29
+ import { AddonCourseCompletionAggregation } from '@addons/coursecompletion/constants' ;
29
30
30
31
/**
31
32
* Page that displays the course completion report.
@@ -39,26 +40,56 @@ import { CoreSharedModule } from '@/core/shared.module';
39
40
} )
40
41
export default class AddonCourseCompletionReportPage implements OnInit {
41
42
42
- protected userId ! : number ;
43
- protected logView : ( ) => void ;
43
+ protected readonly aggregationType = AddonCourseCompletionAggregation ;
44
+ protected readonly userId = signal ( 0 ) ;
45
+ protected logView ! : ( ) => void ;
44
46
45
- courseId ! : number ;
46
- completionLoaded = false ;
47
- completion ?: AddonCourseCompletionCourseCompletionStatus ;
48
- showSelfComplete = false ;
49
- tracked = true ; // Whether completion is tracked.
50
- statusText ?: string ;
51
- user ?: CoreUserProfile ;
47
+ readonly courseId ! : number ;
48
+ readonly loaded = signal ( false ) ;
49
+ readonly completion = signal < AddonCourseCompletionCourseCompletionStatus | undefined > ( undefined ) ;
50
+
51
+ readonly showSelfComplete = computed ( ( ) => {
52
+ const completion = this . completion ( ) ;
53
+ const userId = this . userId ( ) ;
54
+
55
+ if ( ! completion ) {
56
+ return false ;
57
+ }
58
+
59
+ return AddonCourseCompletion . canMarkSelfCompleted ( userId , completion ) ;
60
+ } ) ;
61
+
62
+ readonly tracked = signal ( true ) ; // Whether completion is tracked.
63
+ readonly statusText = computed ( ( ) => {
64
+ const completion = this . completion ( ) ;
65
+ if ( ! completion ) {
66
+ return '' ;
67
+ }
68
+
69
+ return AddonCourseCompletion . getCompletedStatusText ( completion ) ;
70
+ } ) ;
71
+
72
+ readonly user = signal < CoreUserProfile | undefined > ( undefined ) ;
52
73
53
74
constructor ( ) {
75
+ try {
76
+ this . courseId = CoreNavigator . getRequiredRouteNumberParam ( 'courseId' ) ;
77
+ this . userId . set ( CoreNavigator . getRouteNumberParam ( 'userId' ) || CoreSites . getCurrentSiteUserId ( ) ) ;
78
+ } catch ( error ) {
79
+ CoreAlerts . showError ( error ) ;
80
+ CoreNavigator . back ( ) ;
81
+
82
+ return ;
83
+ }
84
+
54
85
this . logView = CoreTime . once ( ( ) => {
55
86
CoreAnalytics . logEvent ( {
56
87
type : CoreAnalyticsEventType . VIEW_ITEM ,
57
88
ws : 'core_completion_get_course_completion_status' ,
58
89
name : Translate . instant ( 'addon.coursecompletion.coursecompletion' ) ,
59
90
data : {
60
91
course : this . courseId ,
61
- user : this . userId ,
92
+ user : this . userId ( ) ,
62
93
} ,
63
94
url : `/blocks/completionstatus/details.php?course=${ this . courseId } &user=${ this . userId } ` ,
64
95
} ) ;
@@ -69,18 +100,8 @@ export default class AddonCourseCompletionReportPage implements OnInit {
69
100
* @inheritdoc
70
101
*/
71
102
ngOnInit ( ) : void {
72
- try {
73
- this . courseId = CoreNavigator . getRequiredRouteNumberParam ( 'courseId' ) ;
74
- this . userId = CoreNavigator . getRouteNumberParam ( 'userId' ) || CoreSites . getCurrentSiteUserId ( ) ;
75
- } catch ( error ) {
76
- CoreAlerts . showError ( error ) ;
77
- CoreNavigator . back ( ) ;
78
-
79
- return ;
80
- }
81
-
82
103
this . fetchCompletion ( ) . finally ( ( ) => {
83
- this . completionLoaded = true ;
104
+ this . loaded . set ( true ) ;
84
105
} ) ;
85
106
}
86
107
@@ -89,19 +110,16 @@ export default class AddonCourseCompletionReportPage implements OnInit {
89
110
*/
90
111
protected async fetchCompletion ( ) : Promise < void > {
91
112
try {
92
- this . user = await CoreUser . getProfile ( this . userId , this . courseId , true ) ;
93
-
94
- this . completion = await AddonCourseCompletion . getCompletion ( this . courseId , this . userId ) ;
113
+ this . user . set ( await CoreUser . getProfile ( this . userId ( ) , this . courseId , true ) ) ;
95
114
96
- this . statusText = AddonCourseCompletion . getCompletedStatusText ( this . completion ) ;
97
- this . showSelfComplete = AddonCourseCompletion . canMarkSelfCompleted ( this . userId , this . completion ) ;
115
+ this . completion . set ( await AddonCourseCompletion . getCompletion ( this . courseId , this . userId ( ) ) ) ;
98
116
99
- this . tracked = true ;
117
+ this . tracked . set ( true ) ;
100
118
this . logView ( ) ;
101
119
} catch ( error ) {
102
- if ( error && error . errorcode == 'notenroled' ) {
120
+ if ( error ? .errorcode = == 'notenroled' ) {
103
121
// Not enrolled error, probably a teacher.
104
- this . tracked = false ;
122
+ this . tracked . set ( false ) ;
105
123
} else {
106
124
CoreAlerts . showError ( error , { default : Translate . instant ( 'addon.coursecompletion.couldnotloadreport' ) } ) ;
107
125
}
@@ -114,7 +132,7 @@ export default class AddonCourseCompletionReportPage implements OnInit {
114
132
* @param refresher Refresher instance.
115
133
*/
116
134
async refreshCompletion ( refresher ?: HTMLIonRefresherElement ) : Promise < void > {
117
- await AddonCourseCompletion . invalidateCourseCompletion ( this . courseId , this . userId ) . finally ( ( ) => {
135
+ await AddonCourseCompletion . invalidateCourseCompletion ( this . courseId , this . userId ( ) ) . finally ( ( ) => {
118
136
this . fetchCompletion ( ) . finally ( ( ) => {
119
137
refresher ?. complete ( ) ;
120
138
} ) ;
0 commit comments