-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
learner_progress_services.py
1093 lines (892 loc) · 44.3 KB
/
learner_progress_services.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
#
# Copyright 2017 The Oppia Authors. All Rights Reserved.
#
# 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.
"""Services for tracking the progress of the learner."""
from core.domain import collection_services
from core.domain import exp_services
from core.domain import learner_progress_domain
from core.domain import learner_playlist_services
from core.domain import subscription_services
from core.domain import user_domain
from core.platform import models
import utils
(user_models,) = models.Registry.import_models([models.NAMES.user])
datastore_services = models.Registry.import_datastore_services()
def _get_completed_activities_from_model(completed_activities_model):
"""Returns an activities completed domain object given a
activities completed model loaded from the datastore.
Args:
completed_activities_model: CompletedActivitiesModel. The
activities completed model loaded from the datastore.
Returns:
CompletedActivities. The domain object corresponding to the
given model.
"""
return user_domain.CompletedActivities(
completed_activities_model.id,
completed_activities_model.exploration_ids,
completed_activities_model.collection_ids)
def _get_incomplete_activities_from_model(incomplete_activities_model):
"""Returns an incomplete activities domain object given an incomplete
activities model loaded from the datastore.
Args:
incomplete_activities_model: IncompleteActivitiesModel. The
incomplete activities model loaded from the datastore.
Returns:
IncompleteActivities. An IncompleteActivities domain object
corresponding to the given model.
"""
return user_domain.IncompleteActivities(
incomplete_activities_model.id,
incomplete_activities_model.exploration_ids,
incomplete_activities_model.collection_ids)
def _get_last_playthrough_information(last_playthrough_model):
"""Returns an ExpUserLastPlaythrough domain object given an
ExpUserLastPlaythroughModel loaded from the datastore.
Args:
last_playthrough_model: ExpUserLastPlaythroughModel. The last
last playthrough information loaded from the datastore.
Returns:
ExpUserLastPlaythrough. The last playthrough information domain object
corresponding to the given model.
"""
return user_domain.ExpUserLastPlaythrough(
last_playthrough_model.user_id,
last_playthrough_model.exploration_id,
last_playthrough_model.last_played_exp_version,
last_playthrough_model.last_updated,
last_playthrough_model.last_played_state_name)
def _save_completed_activities(activities_completed):
"""Save an activities completed domain object as a
CompletedActivitiesModel instance in the datastore.
Args:
activities_completed: CompletedActivities. The activities
completed domain object to be saved in the datastore.
"""
completed_activities_model = user_models.CompletedActivitiesModel(
id=activities_completed.id,
exploration_ids=(
activities_completed.exploration_ids),
collection_ids=activities_completed.collection_ids)
completed_activities_model.put()
def _save_incomplete_activities(incomplete_activities):
"""Save an incomplete activities domain object as an
IncompleteActivitiesModel instance in the datastore.
Args:
incomplete_activities: IncompleteActivities. The incomplete
activities domain object to be saved in the datastore.
"""
incomplete_activities_model = user_models.IncompleteActivitiesModel(
id=incomplete_activities.id,
exploration_ids=(
incomplete_activities.exploration_ids),
collection_ids=(
incomplete_activities.collection_ids))
incomplete_activities_model.put()
def _save_last_playthrough_information(last_playthrough_information):
"""Save an ExpUserLastPlaythrough domain object as an
ExpUserLastPlaythroughModel instance in the datastore.
Args:
last_playthrough_information: ExpUserLastPlaythrough. The last
playthrough information domain object to be saved in the datastore.
"""
last_playthrough_information_model = (
user_models.ExpUserLastPlaythroughModel(
id=last_playthrough_information.id,
user_id=last_playthrough_information.user_id,
exploration_id=last_playthrough_information.exploration_id,
last_played_exp_version=(
last_playthrough_information.last_played_exp_version),
last_played_state_name=(
last_playthrough_information.last_played_state_name)))
last_playthrough_information_model.put()
def mark_exploration_as_completed(user_id, exp_id):
"""Adds the exploration id to the completed list of the user unless the
exploration has already been completed or has been created/edited by the
user. It is also removed from the incomplete list and the learner playlist
(if present).
Args:
user_id: str. The id of the user who has completed the exploration.
exp_id: str. The id of the completed exploration.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if not completed_activities_model:
completed_activities_model = (
user_models.CompletedActivitiesModel(id=user_id))
# We don't want anything that appears in the user's creator dashboard to
# appear in the learner dashboard. Since the subscribed explorations
# (edited/created) appear in the creator dashboard they will not appear in
# the learner dashboard.
subscribed_exploration_ids = (
subscription_services.get_exploration_ids_subscribed_to(user_id))
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
if (exp_id not in subscribed_exploration_ids and
exp_id not in activities_completed.exploration_ids):
# Remove the exploration from the in progress and learner playlist
# (if present) as it is now completed.
remove_exp_from_incomplete_list(user_id, exp_id)
learner_playlist_services.remove_exploration_from_learner_playlist(
user_id, exp_id)
activities_completed.add_exploration_id(exp_id)
_save_completed_activities(activities_completed)
def mark_collection_as_completed(user_id, collection_id):
"""Adds the collection id to the list of collections completed by the user
unless the collection has already been completed or has been created/edited
by the user. It is also removed from the incomplete list and the play later
list (if present).
Args:
user_id: str. The id of the user who completed the collection.
collection_id: str. The id of the completed collection.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if not completed_activities_model:
completed_activities_model = (
user_models.CompletedActivitiesModel(id=user_id))
# We don't want anything that appears in the user's creator dashboard to
# appear in the learner dashboard. Since the subscribed collections
# (edited/created) appear in the creator dashboard they will not appear in
# the learner dashboard.
subscribed_collection_ids = (
subscription_services.get_collection_ids_subscribed_to(user_id))
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
if (collection_id not in subscribed_collection_ids and
collection_id not in activities_completed.collection_ids):
# Remove the collection from the in progress and learner playlist
# (if present) as it is now completed.
remove_collection_from_incomplete_list(user_id, collection_id)
learner_playlist_services.remove_collection_from_learner_playlist(
user_id, collection_id)
activities_completed.add_collection_id(collection_id)
_save_completed_activities(activities_completed)
def mark_exploration_as_incomplete(
user_id, exploration_id, state_name, exploration_version):
"""Adds the exploration id to the incomplete list of the user unless the
exploration has been already completed or has been created/edited by the
user. If the exploration is already present in the incomplete list, just the
details associated with it are updated. If the exploration is present in the
learner playlist, it is removed.
Args:
user_id: str. The id of the user who partially completed the
exploration.
exploration_id: str. The id of the partially completed exploration.
state_name: str. The name of the state at which the user left the
exploration.
exploration_version: str. The version of the exploration played by the
learner.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(
user_id, strict=False))
if not incomplete_activities_model:
incomplete_activities_model = (
user_models.IncompleteActivitiesModel(id=user_id))
exploration_ids = get_all_completed_exp_ids(user_id)
subscribed_exploration_ids = (
subscription_services.get_exploration_ids_subscribed_to(user_id))
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
if (exploration_id not in exploration_ids and
exploration_id not in subscribed_exploration_ids):
if exploration_id not in incomplete_activities.exploration_ids:
# Remove the exploration from the learner playlist (if present) as
# it is currently now being completed.
learner_playlist_services.remove_exploration_from_learner_playlist(
user_id, exploration_id)
incomplete_activities.add_exploration_id(exploration_id)
last_playthrough_information_model = (
user_models.ExpUserLastPlaythroughModel.get(
user_id, exploration_id))
if not last_playthrough_information_model:
last_playthrough_information_model = (
user_models.ExpUserLastPlaythroughModel.create(
user_id, exploration_id))
last_playthrough_information = _get_last_playthrough_information(
last_playthrough_information_model)
last_playthrough_information.update_last_played_information(
exploration_version, state_name)
_save_last_playthrough_information(last_playthrough_information)
_save_incomplete_activities(incomplete_activities)
def mark_collection_as_incomplete(user_id, collection_id):
"""Adds the collection id to the list of collections partially completed by
the user unless the collection has already been completed or has been
created/edited by the user or is already present in the incomplete list.
If the collection is present in the learner playlist, it is removed.
Args:
user_id: str. The id of the user who partially completed the collection.
collection_id: str. The id of the partially completed collection.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(user_id, strict=False))
if not incomplete_activities_model:
incomplete_activities_model = (
user_models.IncompleteActivitiesModel(id=user_id))
collection_ids = get_all_completed_collection_ids(user_id)
subscribed_collection_ids = (
subscription_services.get_collection_ids_subscribed_to(user_id))
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
if (collection_id not in subscribed_collection_ids and
collection_id not in incomplete_activities.collection_ids and
collection_id not in collection_ids):
# Remove the collection from the learner playlist (if present) as it
# is currently now being completed.
learner_playlist_services.remove_collection_from_learner_playlist(
user_id, collection_id)
incomplete_activities.add_collection_id(collection_id)
_save_incomplete_activities(incomplete_activities)
def add_collection_to_learner_playlist(
user_id, collection_id, position_to_be_inserted=None):
"""This function checks if the collection exists in the completed list or
the incomplete list. If it does not exist we call the function in learner
playlist services to add the collection to the play later list.
Args:
user_id: str. The id of the user.
collection_id: str. The id of the collection to be added to the
learner playlist.
position_to_be_inserted: int|None. If this is specified the collection
gets inserted at the given position. Otherwise it gets added at the
end.
Returns:
(bool, bool, bool). The first boolean indicates whether the collection
already exists in either of the "completed collections" or
"incomplete collections" lists, the second boolean indicates
whether the playlist limit of the user has been
exceeded, and the third boolean indicates whether the collection
belongs to the created or edited collections of the user.
"""
completed_collection_ids = get_all_completed_collection_ids(user_id)
incomplete_collection_ids = get_all_incomplete_collection_ids(user_id)
playlist_limit_exceeded = False
belongs_to_subscribed_activities = False
belongs_to_completed_or_incomplete_list = False
if (collection_id not in completed_collection_ids and
collection_id not in incomplete_collection_ids):
(playlist_limit_exceeded, belongs_to_subscribed_activities) = (
learner_playlist_services.mark_collection_to_be_played_later(
user_id, collection_id, position_to_be_inserted))
belongs_to_completed_or_incomplete_list = False
else:
belongs_to_completed_or_incomplete_list = True
return (belongs_to_completed_or_incomplete_list,
playlist_limit_exceeded,
belongs_to_subscribed_activities)
def add_exp_to_learner_playlist(
user_id, exploration_id, position_to_be_inserted=None):
"""This function checks if the exploration exists in the completed list or
the incomplete list. If it does not exist we call the function in learner
playlist services to add the exploration to the play later list.
Args:
user_id: str. The id of the user.
exploration_id: str. The id of the exploration to be added to the
learner playlist.
position_to_be_inserted: int|None. If this is specified the exploration
gets inserted at the given position. Otherwise it gets added at the
end.
Returns:
(bool, bool, bool). The first boolean indicates whether the exploration
already exists in either of the "completed explorations" or
"incomplete explorations" lists, the second boolean indicates
whether the playlist limit of the user has been
exceeded, and the third boolean indicates whether the exploration
belongs to the created or edited explorations of the user.
"""
completed_exploration_ids = get_all_completed_exp_ids(user_id)
incomplete_exploration_ids = get_all_incomplete_exp_ids(user_id)
playlist_limit_exceeded = False
belongs_to_subscribed_activities = False
belongs_to_completed_or_incomplete_list = False
if (exploration_id not in completed_exploration_ids and
exploration_id not in incomplete_exploration_ids):
(playlist_limit_exceeded, belongs_to_subscribed_activities) = (
learner_playlist_services.mark_exploration_to_be_played_later(
user_id, exploration_id, position_to_be_inserted))
belongs_to_completed_or_incomplete_list = False
else:
belongs_to_completed_or_incomplete_list = True
return (belongs_to_completed_or_incomplete_list,
playlist_limit_exceeded,
belongs_to_subscribed_activities)
def _remove_activity_ids_from_playlist(user_id, exploration_ids,
collection_ids):
"""Removes the explorations and collections from the playlist of the user.
Args:
user_id: str. The id of the user.
exploration_ids: list(str). The ids of the explorations to be removed.
collection_ids: list(str). The ids of the collections to be removed.
"""
learner_playlist_model = user_models.LearnerPlaylistModel.get(
user_id, strict=False)
if learner_playlist_model:
learner_playlist = (
learner_playlist_services.get_learner_playlist_from_model(
learner_playlist_model))
for exploration_id in exploration_ids:
learner_playlist.remove_exploration_id(exploration_id)
for collection_id in collection_ids:
learner_playlist.remove_collection_id(collection_id)
learner_playlist_services.save_learner_playlist(learner_playlist)
def remove_exp_from_completed_list(user_id, exploration_id):
"""Removes the exploration from the completed list of the user
(if present).
Args:
user_id: str. The id of the user.
exploration_id: str. The id of the exploration to be removed.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if completed_activities_model:
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
if exploration_id in activities_completed.exploration_ids:
activities_completed.remove_exploration_id(exploration_id)
_save_completed_activities(activities_completed)
def remove_collection_from_completed_list(user_id, collection_id):
"""Removes the collection id from the list of completed collections
(if present).
Args:
user_id: str. The id of the user.
collection_id: str. The id of the collection to be removed.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if completed_activities_model:
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
if collection_id in activities_completed.collection_ids:
activities_completed.remove_collection_id(collection_id)
_save_completed_activities(activities_completed)
def _remove_activity_ids_from_completed_list(user_id, exploration_ids,
collection_ids):
"""Removes the explorations and collections from the completed list of the
learner.
Args:
user_id: str. The id of the user.
exploration_ids: list(str). The ids of the explorations to be removed.
collection_ids: list(str). The ids of the collections to be removed.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if completed_activities_model:
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
for exploration_id in exploration_ids:
activities_completed.remove_exploration_id(exploration_id)
for collection_id in collection_ids:
activities_completed.remove_collection_id(collection_id)
_save_completed_activities(activities_completed)
def remove_exp_from_incomplete_list(user_id, exploration_id):
"""Removes the exploration from the incomplete list of the user
(if present).
Args:
user_id: str. The id of the user.
exploration_id: str. The id of the exploration to be removed.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(user_id, strict=False))
if incomplete_activities_model:
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
if exploration_id in incomplete_activities.exploration_ids:
incomplete_activities.remove_exploration_id(exploration_id)
last_playthrough_information_model = (
user_models.ExpUserLastPlaythroughModel.get(
user_id, exploration_id))
last_playthrough_information_model.delete()
_save_incomplete_activities(incomplete_activities)
def remove_collection_from_incomplete_list(user_id, collection_id):
"""Removes the collection id from the list of incomplete collections
(if present).
Args:
user_id: str. The id of the user.
collection_id: str. The id of the collection to be removed.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(user_id, strict=False))
if incomplete_activities_model:
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
if collection_id in incomplete_activities.collection_ids:
incomplete_activities.remove_collection_id(collection_id)
_save_incomplete_activities(incomplete_activities)
def _remove_activity_ids_from_incomplete_list(user_id, exploration_ids=None,
collection_ids=None):
"""Removes the collections and explorations from the incomplete list of the
user.
Args:
user_id: str. The id of the user.
collection_ids: list(str). The ids of the collections to be removed.
exploration_ids: list(str). The ids of the explorations to be removed.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(
user_id, strict=False))
if incomplete_activities_model:
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
for exploration_id in exploration_ids:
incomplete_activities.remove_exploration_id(exploration_id)
for collection_id in collection_ids:
incomplete_activities.remove_collection_id(collection_id)
_save_incomplete_activities(incomplete_activities)
def get_all_completed_exp_ids(user_id):
"""Returns a list with the ids of all the explorations completed by the
user.
Args:
user_id: str. The id of the user.
Returns:
list(str). A list of the ids of the explorations completed by the
learner.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if completed_activities_model:
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
return activities_completed.exploration_ids
else:
return []
def _get_filtered_completed_exp_summaries(exploration_summaries,
exploration_ids):
"""Returns a list of summaries of the completed exploration ids and the
ids of explorations that are no longer present.
Args:
exploration_summaries: list(ExplorationSummary). The list of exploration
summary domain objects to be filtered.
exploration_ids: list(str). The ids of the explorations corresponding to
the exploration summary domain objects.
Returns:
tuple. A 2-tuple whose elements are as follows:
- list(ExplorationSummary). Filtered list of ExplorationSummary domain
objects of the completed explorations.
- list(str). The ids of the explorations that are no longer present.
"""
nonexistent_completed_exp_ids = []
filtered_completed_exp_summaries = []
for index, exploration_summary in enumerate(exploration_summaries):
if exploration_summary is None:
nonexistent_completed_exp_ids.append(exploration_ids[index])
else:
filtered_completed_exp_summaries.append(exploration_summary)
return filtered_completed_exp_summaries, nonexistent_completed_exp_ids
def get_all_completed_collection_ids(user_id):
"""Returns a list with the ids of all the collections completed by the
user.
Args:
user_id: str. The id of the learner.
Returns:
list(str). A list of the ids of the collections completed by the
learner.
"""
completed_activities_model = (
user_models.CompletedActivitiesModel.get(
user_id, strict=False))
if completed_activities_model:
activities_completed = _get_completed_activities_from_model(
completed_activities_model)
return activities_completed.collection_ids
else:
return []
def _get_filtered_completed_collection_summaries(user_id, collection_summaries,
collection_ids):
"""Returns a list of summaries of the completed collection ids, the ids
of collections that are no longer present and the summaries of the
collections being shifted to the incomplete section on account of new
addition of explorations.
Args:
user_id: str. The id of the learner.
collection_summaries: list(CollectionSummary). The list of collection
summary domain objects to be filtered.
collection_ids: list(str). The ids of the collection corresponding to
the collection summary domain objects.
Returns:
tuple. A 3-tuple whose elements are as follows:
- list(CollectionSummary). A filtered list with the summary domain
objects of the completed collections.
- list(str). The ids of the collections that are no longer present.
- list(CollectionSummary). The summaries corresponding to those
collections which have been moved to the in progress section on
account of new explorations being added to them.
"""
nonexistent_completed_collection_ids = []
completed_to_incomplete_collections = []
filtered_completed_collection_summaries = []
completed_collections = collection_services.get_multiple_collections_by_id(
collection_ids, strict=False)
exploration_ids_completed_in_collections = (
collection_services.get_explorations_completed_in_collections(
user_id, collection_ids))
for index, collection_summary in enumerate(collection_summaries):
if collection_summary is None:
nonexistent_completed_collection_ids.append(collection_ids[index])
else:
completed_exploration_ids = (
exploration_ids_completed_in_collections[index])
collection_model = completed_collections[collection_ids[index]]
if collection_model.get_next_exploration_ids(
completed_exploration_ids):
collection_id = collection_summary.id
remove_collection_from_completed_list(user_id, collection_id)
mark_collection_as_incomplete(user_id, collection_id)
completed_to_incomplete_collections.append(collection_summary)
else:
filtered_completed_collection_summaries.append(
collection_summary)
return (filtered_completed_collection_summaries,
nonexistent_completed_collection_ids,
completed_to_incomplete_collections)
def get_all_incomplete_exp_ids(user_id):
"""Returns a list with the ids of all the explorations partially completed
by the user.
Args:
user_id: str. The id of the learner.
Returns:
list(str). A list of the ids of the explorations partially completed by
the learner.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(
user_id, strict=False))
if incomplete_activities_model:
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
return incomplete_activities.exploration_ids
else:
return []
def _get_filtered_incomplete_exp_summaries(exploration_summaries,
exploration_ids):
"""Returns a list of summaries of the incomplete exploration ids and the ids
of explorations that are no longer present.
Args:
exploration_summaries: list(ExplorationSummary). The list of exploration
summary domain objects to be filtered.
exploration_ids: list(str). The ids of the explorations corresponding to
the exploration summary domain objects.
Returns:
tuple. A 2-tuple whose elements are as follows:
- list(ExplorationSummary). Filtered list of ExplorationSummary domain
objects of the incomplete explorations.
- list(str). The ids of the explorations that are no longer present.
"""
nonexistent_incomplete_exp_ids = []
filtered_incomplete_exp_summaries = []
for index, exploration_summary in enumerate(exploration_summaries):
if exploration_summary is None:
nonexistent_incomplete_exp_ids.append(exploration_ids[index])
else:
filtered_incomplete_exp_summaries.append(exploration_summary)
return filtered_incomplete_exp_summaries, nonexistent_incomplete_exp_ids
def get_all_incomplete_collection_ids(user_id):
"""Returns a list with the ids of all the collections partially completed
by the user.
Args:
user_id: str. The id of the learner.
Returns:
list(str). A list of the ids of the collections partially completed by
the learner.
"""
incomplete_activities_model = (
user_models.IncompleteActivitiesModel.get(user_id, strict=False))
if incomplete_activities_model:
incomplete_activities = _get_incomplete_activities_from_model(
incomplete_activities_model)
return incomplete_activities.collection_ids
else:
return []
def _get_filtered_incomplete_collection_summaries(collection_summaries,
collection_ids):
"""Returns a list of summaries of the incomplete collection ids and the ids
of collections that are no longer present.
Args:
collection_summaries: list(CollectionSummary). The list of collection
summary domain objects to be filtered.
collection_ids: list(str). The ids of the collection corresponding to
the collection summary domain objects.
Returns:
tuple. A 2-tuple whose elements are as follows:
- list(CollectionSummary). A filtered list with the summary domain
objects of the incomplete collections.
- list(str). The ids of the collections that are no longer present.
"""
nonexistent_incomplete_collection_ids = []
filtered_incomplete_collection_summaries = []
for index, collection_summary in enumerate(collection_summaries):
if collection_summary is None:
nonexistent_incomplete_collection_ids.append(collection_ids[index])
else:
filtered_incomplete_collection_summaries.append(collection_summary)
return (filtered_incomplete_collection_summaries,
nonexistent_incomplete_collection_ids)
def _get_filtered_exp_playlist_summaries(exploration_summaries,
exploration_ids):
"""Returns a list of summaries of the explorations in the learner playlist
and the ids of explorations that are no longer present.
Args:
exploration_summaries: list(ExplorationSummary). The list of exploration
summary domain objects to be filtered.
exploration_ids: list(str). The ids of the explorations corresponding to
the exploration summary domain objects.
Returns:
tuple. A 2-tuple whose elements are as follows:
- list(ExplorationSummary). Filtered list of ExplorationSummary domain
objects of the explorations in the learner playlist.
- list(str). The ids of the explorations that are no longer present.
"""
nonexistent_playlist_exp_ids = []
filtered_exp_playlist_summaries = []
for index, exploration_summary in enumerate(exploration_summaries):
if exploration_summary is None:
nonexistent_playlist_exp_ids.append(exploration_ids[index])
else:
filtered_exp_playlist_summaries.append(exploration_summary)
return filtered_exp_playlist_summaries, nonexistent_playlist_exp_ids
def _get_filtered_collection_playlist_summaries(collection_summaries,
collection_ids):
"""Returns a list of summaries of the collections in the learner playlist
and the ids of collections that are no longer present.
Args:
collection_summaries: list(CollectionSummary). The list of collection
summary domain objects to be filtered.
collection_ids: list(str). The ids of the collections corresponding to
the collection summary domain objects.
Returns:
tuple. A 2-tuple whose elements are as follows:
- list(CollectionSummary). Filtered list of CollectionSummary domain
objects of the collections in the learner playlist.
- list(str). The ids of the collections that are no longer present.
"""
nonexistent_playlist_collection_ids = []
filtered_collection_playlist_summaries = []
for index, collection_summary in enumerate(collection_summaries):
if collection_summary is None:
nonexistent_playlist_collection_ids.append(collection_ids[index])
else:
filtered_collection_playlist_summaries.append(collection_summary)
return (filtered_collection_playlist_summaries,
nonexistent_playlist_collection_ids)
def get_collection_summary_dicts(collection_summaries):
"""Returns a displayable summary dict of the the collection summaries
given to it.
Args:
collection_summaries: list(CollectionSummary). A list of the
summary domain objects.
Returns:
list(dict). The summary dict objects corresponding to the given summary
domain objects.
"""
summary_dicts = []
for collection_summary in collection_summaries:
summary_dicts.append({
'id': collection_summary.id,
'title': collection_summary.title,
'category': collection_summary.category,
'objective': collection_summary.objective,
'language_code': collection_summary.language_code,
'last_updated': utils.get_time_in_millisecs(
collection_summary.collection_model_last_updated),
'created_on': utils.get_time_in_millisecs(
collection_summary.collection_model_created_on),
'status': collection_summary.status,
'node_count': collection_summary.node_count,
'community_owned': collection_summary.community_owned,
'thumbnail_icon_url': (
utils.get_thumbnail_icon_url_for_category(
collection_summary.category)),
'thumbnail_bg_color': utils.get_hex_color_for_category(
collection_summary.category),
})
return summary_dicts
def get_learner_dashboard_activities(user_id):
"""Returns the ids of each of the activities that are present in the various
sections of the learner dashboard, namely the completed section, the
incomplete section and the playlist section.
Args:
user_id: str. The id of the learner.
Returns:
ActivityIdsInLearnerDashboard. The domain object containing the ids of
all activities in the learner dashboard.
"""
learner_progress_models = (
datastore_services.fetch_multiple_entities_by_ids_and_models(
[
('CompletedActivitiesModel', [user_id]),
('IncompleteActivitiesModel', [user_id]),
('LearnerPlaylistModel', [user_id])
]))
# If completed model is present.
if learner_progress_models[0][0]:
activities_completed = _get_completed_activities_from_model(
learner_progress_models[0][0])
completed_exploration_ids = activities_completed.exploration_ids
completed_collection_ids = activities_completed.collection_ids
else:
completed_collection_ids = []
completed_exploration_ids = []
# If incomplete model is present.
if learner_progress_models[1][0]:
incomplete_activities = _get_incomplete_activities_from_model(
learner_progress_models[1][0])
incomplete_exploration_ids = incomplete_activities.exploration_ids
incomplete_collection_ids = incomplete_activities.collection_ids
else:
incomplete_exploration_ids = []
incomplete_collection_ids = []
# If learner playlist model is present.
if learner_progress_models[2][0]:
learner_playlist = (
learner_playlist_services.get_learner_playlist_from_model(
learner_progress_models[2][0]))
exploration_playlist_ids = learner_playlist.exploration_ids
collection_playlist_ids = learner_playlist.collection_ids
else:
exploration_playlist_ids = []
collection_playlist_ids = []
activity_ids = learner_progress_domain.ActivityIdsInLearnerDashboard(
completed_exploration_ids, completed_collection_ids,
incomplete_exploration_ids, incomplete_collection_ids,
exploration_playlist_ids, collection_playlist_ids)
return activity_ids
def get_activity_progress(user_id):
"""Returns the progress of the learners - the explorations and collections
completed by the user and those in progress.
Args:
user_id: str. The id of the learner.
Returns:
(LearnerProgress, dict, list(str)). The first return value is the
learner progress domain object corresponding to the particular
learner. The second return value is the numbers of the activities
that are no longer present. It contains four keys:
- incomplete_explorations: int. The number of incomplete
explorations no longer present.
- incomplete_collections: int. The number of incomplete collections
no longer present.
- completed_explorations: int. The number of completed explorations
no longer present.
- completed_collections: int. The number of completed collections no
longer present.
The third return valus is the titles of the collections to which new
explorations have been added.
"""
activity_ids_in_learner_dashboard = (
get_learner_dashboard_activities(user_id))
completed_exploration_ids = (
activity_ids_in_learner_dashboard.completed_exploration_ids)
completed_collection_ids = (
activity_ids_in_learner_dashboard.completed_collection_ids)
incomplete_exploration_ids = (
activity_ids_in_learner_dashboard.incomplete_exploration_ids)
incomplete_collection_ids = (
activity_ids_in_learner_dashboard.incomplete_collection_ids)
exploration_playlist_ids = (
activity_ids_in_learner_dashboard.exploration_playlist_ids)
collection_playlist_ids = (
activity_ids_in_learner_dashboard.collection_playlist_ids)
activity_models = (
datastore_services.fetch_multiple_entities_by_ids_and_models(
[
('ExpSummaryModel', incomplete_exploration_ids),
('CollectionSummaryModel', incomplete_collection_ids),
('ExpSummaryModel', completed_exploration_ids),
('CollectionSummaryModel', completed_collection_ids),
('ExpSummaryModel', exploration_playlist_ids),