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 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 delete(self, activity_type, activity_id): if activity_type == constants.ACTIVITY_TYPE_EXPLORATION: learner_playlist_services.remove_exploration_from_learner_playlist( self.user_id, activity_id) elif activity_type == constants.ACTIVITY_TYPE_COLLECTION: learner_playlist_services.remove_collection_from_learner_playlist( self.user_id, activity_id) self.render_json(self.values)
def test_remove_collection_from_learner_playlist(self): self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), []) # Add collections to learner playlist. learner_playlist_services.mark_collection_to_be_played_later( self.user_id, self.COL_ID_0) learner_playlist_services.mark_collection_to_be_played_later( self.user_id, self.COL_ID_1) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_0, self.COL_ID_1]) # Removing a collection. learner_playlist_services.remove_collection_from_learner_playlist( self.user_id, self.COL_ID_0) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_1]) # Removing the same collection again has no effect. learner_playlist_services.remove_collection_from_learner_playlist( self.user_id, self.COL_ID_0) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_1]) # Removing the second collection. learner_playlist_services.remove_collection_from_learner_playlist( self.user_id, self.COL_ID_1) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [])
def test_mark_collection_to_be_played_later(self): self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), []) # Add a collection to the learner playlist. learner_progress_services.add_collection_to_learner_playlist( self.user_id, self.COL_ID_0) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_0]) # Add another collection. learner_progress_services.add_collection_to_learner_playlist( self.user_id, self.COL_ID_1) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_0, self.COL_ID_1]) # Add the first collection in a different position. learner_progress_services.add_collection_to_learner_playlist( self.user_id, self.COL_ID_0, 1) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_1, self.COL_ID_0]) # Add the first collection in the first position. learner_progress_services.add_collection_to_learner_playlist( self.user_id, self.COL_ID_0, 0) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_0, self.COL_ID_1]) # Add a collection to the in progress list of the user. learner_progress_services.mark_collection_as_incomplete( self.user_id, self.COL_ID_3) # Test that the collection added to the in progress list doesn't get # added to the learner playlist. learner_progress_services.add_collection_to_learner_playlist( self.user_id, self.COL_ID_3) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), [self.COL_ID_0, self.COL_ID_1]) # Empty the learner playlist. learner_playlist_services.remove_collection_from_learner_playlist( self.user_id, self.COL_ID_0) learner_playlist_services.remove_collection_from_learner_playlist( self.user_id, self.COL_ID_1) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), []) # Test that the length of the learner playlist doesn't exceed # MAX_LEARNER_PLAYLIST_ACTIVITY_COUNT. # List of collections to be added. collection_ids = ([ 'SAMPLE_COLLECTION_ID_%s' % index for index in range(0, MAX_LEARNER_PLAYLIST_ACTIVITY_COUNT) ]) for collection_id in collection_ids: learner_progress_services.add_collection_to_learner_playlist( self.user_id, collection_id) self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), collection_ids) # Now if we try adding another collection, it shouldn't be added as the # list length would exceed MAX_LEARNER_PLAYLIST_ACTIVITY_COUNT. learner_playlist_services.mark_collection_to_be_played_later( self.user_id, 'SAMPLE_COLLECTION_ID') # The list still remains the same. self.assertEqual( self._get_all_learner_playlist_collection_ids(self.user_id), collection_ids)