Commit b8c5b984 by Greg Price

Merge pull request #6076 from edx/gprice/last-visited-module-path

Add last visited module path to course status API
parents 763b29d0 3a7e1251
...@@ -186,7 +186,7 @@ class TestUserApi(ModuleStoreTestCase, APITestCase): ...@@ -186,7 +186,7 @@ class TestUserApi(ModuleStoreTestCase, APITestCase):
self.assertEqual(response.status_code, 401) self.assertEqual(response.status_code, 401)
def test_default_value(self): def test_default_value(self):
(__, __, unit, __) = self._setup_course_skeleton() (section, sub_section, unit, __) = self._setup_course_skeleton()
self.client.login(username=self.username, password=self.password) self.client.login(username=self.username, password=self.password)
url = self._course_status_url() url = self._course_status_url()
...@@ -195,6 +195,10 @@ class TestUserApi(ModuleStoreTestCase, APITestCase): ...@@ -195,6 +195,10 @@ class TestUserApi(ModuleStoreTestCase, APITestCase):
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertEqual(json_data["last_visited_module_id"], unicode(unit.location)) self.assertEqual(json_data["last_visited_module_id"], unicode(unit.location))
self.assertEqual(
json_data["last_visited_module_path"],
[unicode(module.location) for module in [unit, sub_section, section, self.course]]
)
def test_course_update_no_args(self): def test_course_update_no_args(self):
self.client.login(username=self.username, password=self.password) self.client.login(username=self.username, password=self.password)
......
...@@ -90,10 +90,11 @@ class UserCourseStatus(views.APIView): ...@@ -90,10 +90,11 @@ class UserCourseStatus(views.APIView):
http_method_names = ["get", "patch"] http_method_names = ["get", "patch"]
def _last_visited_module_id(self, request, course): def _last_visited_module_path(self, request, course):
""" """
Returns the id of the last module visited by the current user in the given course. Returns the path from the last module visited by the current user in the given course up to
If there is no such visit returns the default (the first item deep enough down the course tree) the course module. If there is no such visit, the first item deep enough down the course
tree is used.
""" """
field_data_cache = FieldDataCache.cache_for_descriptor_descendents( field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
course.id, request.user, course, depth=2) course.id, request.user, course, depth=2)
...@@ -101,13 +102,16 @@ class UserCourseStatus(views.APIView): ...@@ -101,13 +102,16 @@ class UserCourseStatus(views.APIView):
course_module = get_module_for_descriptor(request.user, request, course, field_data_cache, course.id) course_module = get_module_for_descriptor(request.user, request, course, field_data_cache, course.id)
current = course_module current = course_module
path = []
child = current child = current
while child: while child:
path.append(child)
child = get_current_child(current) child = get_current_child(current)
if child: if child:
current = child current = child
return current path.reverse()
return path
def _process_arguments(self, request, username, course_id, course_handler): def _process_arguments(self, request, username, course_id, course_handler):
""" """
...@@ -134,8 +138,12 @@ class UserCourseStatus(views.APIView): ...@@ -134,8 +138,12 @@ class UserCourseStatus(views.APIView):
""" """
Returns the course status Returns the course status
""" """
current_module = self._last_visited_module_id(request, course) path = self._last_visited_module_path(request, course)
return Response({"last_visited_module_id": unicode(current_module.location)}) path_ids = [unicode(module.location) for module in path]
return Response({
"last_visited_module_id": path_ids[0],
"last_visited_module_path": path_ids,
})
def get(self, request, username, course_id): def get(self, request, username, course_id):
""" """
...@@ -151,6 +159,8 @@ class UserCourseStatus(views.APIView): ...@@ -151,6 +159,8 @@ class UserCourseStatus(views.APIView):
* last_visited_module_id: The id of the last module visited by the user in the given course * last_visited_module_id: The id of the last module visited by the user in the given course
* last_visited_module_path: The ids of the modules in the path from the last visited module
to the course module
""" """
return self._process_arguments(request, username, course_id, lambda course: self.get_course_info(request, course)) return self._process_arguments(request, username, course_id, lambda course: self.get_course_info(request, course))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment