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 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 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)
Example #4
0
    def test_remove_exploration_from_learner_playlist(self):
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [])

        # Add explorations to learner playlist.
        learner_playlist_services.mark_exploration_to_be_played_later(
            self.user_id, self.EXP_ID_0)
        learner_playlist_services.mark_exploration_to_be_played_later(
            self.user_id, self.EXP_ID_1)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_0, self.EXP_ID_1])

        # Removing an exploration.
        learner_playlist_services.remove_exploration_from_learner_playlist(
            self.user_id, self.EXP_ID_0)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_1])

        # Removing the same exploration again has no effect.
        learner_playlist_services.remove_exploration_from_learner_playlist(
            self.user_id, self.EXP_ID_0)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_1])

        # Removing the second exploration.
        learner_playlist_services.remove_exploration_from_learner_playlist(
            self.user_id, self.EXP_ID_1)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [])
Example #5
0
    def test_mark_exploration_to_be_played_later(self):
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [])

        # Add an exploration to the learner playlist.
        learner_progress_services.add_exp_to_learner_playlist(
            self.user_id, self.EXP_ID_0)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_0])

        # Add another exploration.
        learner_progress_services.add_exp_to_learner_playlist(
            self.user_id, self.EXP_ID_1)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_0, self.EXP_ID_1])

        # Add the first exploration in a different position.
        learner_progress_services.add_exp_to_learner_playlist(
            self.user_id, self.EXP_ID_0, 1)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_1, self.EXP_ID_0])

        # Add the first exploration in the first position.
        learner_progress_services.add_exp_to_learner_playlist(
            self.user_id, self.EXP_ID_0, 0)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_0, self.EXP_ID_1])

        # Add an exploration to the in progress list of the user.
        state_name = 'state_name'
        version = 1
        learner_progress_services.mark_exploration_as_incomplete(
            self.user_id, self.EXP_ID_3, state_name, version)

        # Test that the exploration added to the in progress list doesn't get
        # added to the learner playlist.
        learner_progress_services.add_exp_to_learner_playlist(
            self.user_id, self.EXP_ID_3)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [self.EXP_ID_0, self.EXP_ID_1])

        # Empty the learner playlist.
        learner_playlist_services.remove_exploration_from_learner_playlist(
            self.user_id, self.EXP_ID_0)
        learner_playlist_services.remove_exploration_from_learner_playlist(
            self.user_id, self.EXP_ID_1)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         [])

        # Test that the length of the learner playlist doesn't exceed
        # MAX_LEARNER_PLAYLIST_ACTIVITY_COUNT.
        # List of explorations to be added.
        exp_ids = [
            'SAMPLE_EXP_ID_%s' % index
            for index in range(0, MAX_LEARNER_PLAYLIST_ACTIVITY_COUNT)
        ]
        for exp_id in exp_ids:
            learner_progress_services.add_exp_to_learner_playlist(
                self.user_id, exp_id)
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         exp_ids)

        # Now if we try adding another exploration, it shouldn't be added as the
        # list length would exceed MAX_LEARNER_PLAYLIST_ACTIVITY_COUNT.
        learner_playlist_services.mark_exploration_to_be_played_later(
            self.user_id, 'SAMPLE_EXP_ID')

        # The list still remains the same.
        self.assertEqual(self._get_all_learner_playlist_exp_ids(self.user_id),
                         exp_ids)