def post(self, exploration_id): """Handles POST requests.""" # This will be None if the exploration is not being played within the # context of a collection. collection_id = self.payload.get('collection_id') user_id = self.user_id event_services.CompleteExplorationEventHandler.record( exploration_id, self.payload.get('version'), self.payload.get('state_name'), self.payload.get('session_id'), self.payload.get('client_time_spent_in_secs'), self.payload.get('params'), feconf.PLAY_TYPE_NORMAL) if user_id: learner_progress_services.mark_exploration_as_completed( user_id, exploration_id) if user_id and collection_id: collection_services.record_played_exploration_in_collection_context( user_id, collection_id, exploration_id) collections_left_to_complete = ( collection_services. get_next_exploration_ids_to_complete_by_user( # pylint: disable=line-too-long user_id, collection_id)) if not collections_left_to_complete: learner_progress_services.mark_collection_as_completed( user_id, collection_id) else: learner_progress_services.mark_collection_as_incomplete( user_id, collection_id) self.render_json(self.values)
def setUp(self): super(QuestionsBatchHandlerTest, self).setUp() self.collection_id = 'coll_0' self.exp_id = 'exp_1' self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) self.viewer_id = self.get_user_id_from_email(self.VIEWER_EMAIL) # Create a new collection and exploration. self.save_new_valid_collection(self.collection_id, self.owner_id, exploration_id=self.exp_id) # Add a skill. collection_services.update_collection( self.owner_id, self.collection_id, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_SKILL, 'name': 'test' }], 'Add a new skill') collection = collection_services.get_collection_by_id( self.collection_id) self.skill_id = collection.get_skill_id_from_skill_name('test') collection_node = collection.get_node(self.exp_id) collection_node.update_acquired_skill_ids([self.skill_id]) # Update the acquired skill IDs for the exploration. collection_services.update_collection( self.owner_id, self.collection_id, [{ 'cmd': collection_domain.CMD_EDIT_COLLECTION_NODE_PROPERTY, 'property_name': (collection_domain.COLLECTION_NODE_PROPERTY_ACQUIRED_SKILL_IDS ), # pylint: disable=line-too-long 'exploration_id': self.exp_id, 'new_value': [self.skill_id] }], 'Update skill') question = question_domain.Question( 'dummy', 'A Question', exp_domain.State.create_default_state('ABC').to_dict(), 1, self.collection_id, 'en') question_id = question_services.add_question(self.owner_id, question) self.question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill(self.question.question_id, self.question.collection_id, self.skill_id, self.owner_id) self.signup(self.NEW_USER_EMAIL, self.NEW_USER_USERNAME) self.new_user_id = self.get_user_id_from_email(self.NEW_USER_EMAIL) collection_services.record_played_exploration_in_collection_context( self.new_user_id, self.collection_id, self.exp_id) self.payload = {}
def test_put(self): question_id = question_services.add_question( self.new_user_id, self.question) question_services.add_question_id_to_skill( question_id, self.collection_id, self.skill_id, self.new_user_id) collection_services.record_played_exploration_in_collection_context( self.new_user_id, self.collection_id, self.exp_id) payload = {} change_list = [{'cmd': 'update_question_property', 'property_name': 'title', 'new_value': 'ABC', 'old_value': 'A Question'}] payload['change_list'] = json.dumps(change_list) payload['commit_message'] = 'update title' self.login(self.NEW_USER_EMAIL) response = self.testapp.get('/preferences') csrf_token = self.get_csrf_token_from_response(response) response_json = self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=False) self.assertIn('question_id', response_json.keys()) del payload['change_list'] self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=True, expected_status_int=404) del payload['commit_message'] payload['change_list'] = json.dumps(change_list) self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=True, expected_status_int=404) payload['commit_message'] = 'update title' self.put_json( '%s/%s' % (feconf.QUESTION_DATA_URL, self.collection_id), payload, csrf_token, expect_errors=True, expected_status_int=404) self.logout() self.login(self.random_email) response = self.testapp.get('/preferences') csrf_token = self.get_csrf_token_from_response(response) self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=True, expected_status_int=401)
def test_get_question_batch(self): coll_id_0 = '0_collection_id' exp_id_0 = '0_exploration_id' self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) # Create a new collection and exploration. self.save_new_valid_collection(coll_id_0, self.owner_id, exploration_id=exp_id_0) # Add a skill. collection_services.update_collection( self.owner_id, coll_id_0, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_SKILL, 'name': 'skill0' }], 'Add a new skill') collection = collection_services.get_collection_by_id(coll_id_0) skill_id = collection.get_skill_id_from_skill_name('skill0') collection_node = collection.get_node(exp_id_0) collection_node.update_acquired_skill_ids([skill_id]) # Update the acquired skill IDs for the exploration. collection_services.update_collection( self.owner_id, coll_id_0, [{ 'cmd': collection_domain.CMD_EDIT_COLLECTION_NODE_PROPERTY, 'property_name': (collection_domain.COLLECTION_NODE_PROPERTY_ACQUIRED_SKILL_IDS ), # pylint: disable=line-too-long 'exploration_id': exp_id_0, 'new_value': [skill_id] }], 'Update skill') question = question_domain.Question( 'dummy', 'A Question', exp_domain.State.create_default_state('ABC').to_dict(), 1, coll_id_0, 'en') question_id = question_services.add_question(self.owner_id, question) question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill(question.question_id, coll_id_0, skill_id, self.owner_id) collection_services.record_played_exploration_in_collection_context( self.owner_id, coll_id_0, exp_id_0) question_batch = question_services.get_questions_batch( coll_id_0, [skill_id], self.owner_id, 1) self.assertEqual(question_batch[0].title, question.title)
def post(self, exploration_id): """Handles POST requests.""" # This will be None if the exploration is not being played within the # context of a collection. collection_id = self.payload.get('collection_id') user_id = self.user_id event_services.CompleteExplorationEventHandler.record( exploration_id, self.payload.get('version'), self.payload.get('state_name'), self.payload.get('session_id'), self.payload.get('client_time_spent_in_secs'), self.payload.get('params'), feconf.PLAY_TYPE_NORMAL) if user_id and collection_id: collection_services.record_played_exploration_in_collection_context( user_id, collection_id, exploration_id)
def post(self, exploration_id): """Handles POST requests.""" # This will be None if the exploration is not being played within the # context of a collection. collection_id = self.payload.get('collection_id') user_id = self.user_id event_services.CompleteExplorationEventHandler.record( exploration_id, self.payload.get('version'), self.payload.get('state_name'), self.payload.get('session_id'), self.payload.get('client_time_spent_in_secs'), self.payload.get('params'), feconf.PLAY_TYPE_NORMAL) if user_id and collection_id: collection_services.record_played_exploration_in_collection_context( user_id, collection_id, exploration_id)
def test_batch_get(self): """Tests get method of questions batch handler.""" question_id = question_services.add_question( self.owner_id, self.question) self.question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill( self.question.question_id, self.question.collection_id, self.skill_id, self.owner_id) collection_services.record_played_exploration_in_collection_context( self.new_user_id, self.collection_id, self.exp_id) payload = {} payload['collection_id'] = self.collection_id payload['stringified_skill_ids'] = json.dumps( [self.skill_id, 'test']) self.login(self.NEW_USER_EMAIL) response_json = self.get_json( '%s/batch' % feconf.QUESTION_DATA_URL, payload, expect_errors=False) self.assertIn(self.question.to_dict(), response_json['questions_dict']) self.assertEqual(len(response_json['questions_dict']), 1) response = self.testapp.get( '%s/batch' % feconf.QUESTION_DATA_URL, expect_errors=True) self.assertEqual(response.status_int, 404) del payload['stringified_skill_ids'] response = self.testapp.get( '%s/batch' % feconf.QUESTION_DATA_URL, payload, expect_errors=True) self.assertEqual(response.status_int, 404) self.logout() self.login(self.random_email) response_json = self.testapp.get( '%s/batch' % feconf.QUESTION_DATA_URL, payload, expect_errors=True) self.assertEqual(response.status_int, 404)
def test_welcome_collection(self): """Test a learner's progression through the default collection.""" collection_services.load_demo('0') # Login as the user who will play the collection. self.login(self.VIEWER_EMAIL) # Request the collection from the data handler. response_dict = self.get_json('%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] # Verify the collection was properly loaded. self.assertEqual(collection_dict['objective'], 'To introduce collections using demo explorations.') self.assertEqual(collection_dict['category'], 'Welcome') self.assertEqual(collection_dict['title'], 'Introduction to Collections in Oppia') # Verify there are 4 explorations in this collection, the initial # explorations to be completed, and that there are no explorations # currently completed within the context of this collection. self.assertEqual(len(collection_dict['nodes']), 4) playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], ['19']) self.assertEqual(playthrough_dict['completed_exploration_ids'], []) # 'Complete' the first exploration. This should lead to 1 new one being # suggested to the learner. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '19') response_dict = self.get_json('%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], ['20']) self.assertEqual(playthrough_dict['completed_exploration_ids'], ['19']) # Completing the next exploration results in a third suggested exp. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '20') response_dict = self.get_json('%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], ['21']) self.assertEqual(playthrough_dict['completed_exploration_ids'], ['19', '20']) # Completing the next exploration results in a fourth and final # suggested exp. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '21') response_dict = self.get_json('%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], ['0']) self.assertEqual(playthrough_dict['completed_exploration_ids'], ['19', '20', '21']) # Completing the final exploration should result in no new suggestions. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '0') response_dict = self.get_json('%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], []) self.assertEqual(playthrough_dict['completed_exploration_ids'], ['19', '20', '21', '0'])
def test_welcome_collection(self): """Test a learner's progression through the default collection.""" collection_services.load_demo('0') # Login as the user who will play the collection. self.login(self.VIEWER_EMAIL) # Request the collection from the data handler. response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] # Verify the collection was properly loaded. self.assertEqual( collection_dict['objective'], 'To introduce collections using demo explorations.') self.assertEqual(collection_dict['category'], 'Welcome') self.assertEqual( collection_dict['title'], 'Introduction to Collections in Oppia') # Verify there are 5 explorations in this collection, the initial # explorations to be completed, and that there are no explorations # currently completed within the context of this collection. self.assertEqual(len(collection_dict['nodes']), 5) playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], ['0']) self.assertEqual(playthrough_dict['completed_exploration_ids'], []) # 'Complete' the first exploration. This should lead to 3 more being # suggested to the learner. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '0') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual( playthrough_dict['next_exploration_ids'], ['13', '4', '14']) self.assertEqual(playthrough_dict['completed_exploration_ids'], ['0']) # Completing the 'Solar System' exploration results in no branching. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '13') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual( playthrough_dict['next_exploration_ids'], ['4', '14']) self.assertEqual( playthrough_dict['completed_exploration_ids'], ['0', '13']) # Completing the 'About Oppia' exploration results in another # exploration being suggested. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '14') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual( playthrough_dict['next_exploration_ids'], ['4', '15']) self.assertEqual( playthrough_dict['completed_exploration_ids'], ['0', '13', '14']) # Completing all explorations should lead to no other suggestions. collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '15') collection_services.record_played_exploration_in_collection_context( self.viewer_id, '0', '4') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] playthrough_dict = collection_dict['playthrough_dict'] self.assertEqual(playthrough_dict['next_exploration_ids'], []) self.assertEqual( playthrough_dict['completed_exploration_ids'], ['0', '13', '14', '15', '4'])
def test_welcome_collection(self): """Test a learner's progression through the default collection.""" collection_services.load_demo("0") # Login as the user who will play the collection. self.login(self.VIEWER_EMAIL) # Request the collection from the data handler. response_dict = self.get_json("%s/0" % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict["collection"] # Verify the collection was properly loaded. self.assertEqual(collection_dict["objective"], "To introduce collections using demo explorations.") self.assertEqual(collection_dict["category"], "Welcome") self.assertEqual(collection_dict["title"], "Introduction to Collections in Oppia") # Verify there are 5 explorations in this collection, the initial # explorations to be completed, and that there are no explorations # currently completed within the context of this collection. self.assertEqual(len(collection_dict["nodes"]), 5) playthrough_dict = collection_dict["playthrough_dict"] self.assertEqual(playthrough_dict["next_exploration_ids"], ["0"]) self.assertEqual(playthrough_dict["completed_exploration_ids"], []) # 'Complete' the first exploration. This should lead to 3 more being # suggested to the learner. collection_services.record_played_exploration_in_collection_context(self.viewer_id, "0", "0") response_dict = self.get_json("%s/0" % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict["collection"] playthrough_dict = collection_dict["playthrough_dict"] self.assertEqual(playthrough_dict["next_exploration_ids"], ["13"]) self.assertEqual(playthrough_dict["completed_exploration_ids"], ["0"]) # Completing the 'Solar System' exploration results in no branching. collection_services.record_played_exploration_in_collection_context(self.viewer_id, "0", "13") response_dict = self.get_json("%s/0" % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict["collection"] playthrough_dict = collection_dict["playthrough_dict"] self.assertEqual(playthrough_dict["next_exploration_ids"], ["4"]) self.assertEqual(playthrough_dict["completed_exploration_ids"], ["0", "13"]) # Completing the 'About Oppia' exploration results in another # exploration being suggested. collection_services.record_played_exploration_in_collection_context(self.viewer_id, "0", "14") response_dict = self.get_json("%s/0" % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict["collection"] playthrough_dict = collection_dict["playthrough_dict"] self.assertEqual(playthrough_dict["next_exploration_ids"], ["4"]) self.assertEqual(playthrough_dict["completed_exploration_ids"], ["0", "13", "14"]) # Completing all explorations should lead to no other suggestions. collection_services.record_played_exploration_in_collection_context(self.viewer_id, "0", "15") collection_services.record_played_exploration_in_collection_context(self.viewer_id, "0", "4") response_dict = self.get_json("%s/0" % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict["collection"] playthrough_dict = collection_dict["playthrough_dict"] self.assertEqual(playthrough_dict["next_exploration_ids"], []) self.assertEqual(playthrough_dict["completed_exploration_ids"], ["0", "13", "14", "15", "4"])
def test_welcome_collection(self): """Test a learner's progression through the default collection.""" collection_services.load_demo('0') # Login as the user who will play the collection. self.login(self.VIEWER_EMAIL) # Request the collection from the data handler. response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] # Verify the collection was properly loaded. self.assertEqual( collection_dict['objective'], 'To introduce collections using demo explorations.') self.assertEqual(collection_dict['category'], 'Welcome') self.assertEqual( collection_dict['title'], 'Introduction to Collections in Oppia') # Verify there are 5 explorations in this collection, the initial # explorations to be completed, and that there are no explorations # currently completed within the context of this collection. self.assertEqual(len(collection_dict['nodes']), 5) self.assertEqual(collection_dict['next_exploration_ids'], ['0']) self.assertEqual(collection_dict['completed_exploration_ids'], []) # 'Complete' the first exploration. This should lead to 3 more being # suggested to the learner. collection_services.record_played_exploration_in_collection_context( self.VIEWER_ID, '0', '0') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] self.assertEqual( collection_dict['next_exploration_ids'], ['13', '4', '14']) self.assertEqual(collection_dict['completed_exploration_ids'], ['0']) # Completing the 'Solar System' exploration results in no branching. collection_services.record_played_exploration_in_collection_context( self.VIEWER_ID, '0', '13') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] self.assertEqual( collection_dict['next_exploration_ids'], ['4', '14']) self.assertEqual( collection_dict['completed_exploration_ids'], ['0', '13']) # Completing the 'About Oppia' exploration results in another # exploration being suggested. collection_services.record_played_exploration_in_collection_context( self.VIEWER_ID, '0', '14') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] self.assertEqual( collection_dict['next_exploration_ids'], ['4', '15']) self.assertEqual( collection_dict['completed_exploration_ids'], ['0', '13', '14']) # Completing all explorations should lead to no other suggestions. collection_services.record_played_exploration_in_collection_context( self.VIEWER_ID, '0', '15') collection_services.record_played_exploration_in_collection_context( self.VIEWER_ID, '0', '4') response_dict = self.get_json( '%s/0' % feconf.COLLECTION_DATA_URL_PREFIX) collection_dict = response_dict['collection'] self.assertEqual(collection_dict['next_exploration_ids'], []) self.assertEqual( collection_dict['completed_exploration_ids'], ['0', '13', '14', '15', '4'])