def score(launch_id, earned_score, time_spent): tool_conf = ToolConfJsonFile(get_lti_config_path()) flask_request = FlaskRequest() message_launch = ExtendedFlaskMessageLaunch.from_cache( launch_id, flask_request, tool_conf) if not message_launch.has_ags(): raise Forbidden("Don't have grades!") sub = message_launch.get_launch_data().get('sub') timestamp = datetime.datetime.utcnow().isoformat() + 'Z' earned_score = int(earned_score) time_spent = int(time_spent) grades = message_launch.get_ags() sc = Grade() sc.set_score_given(earned_score) \ .set_score_maximum(100) \ .set_timestamp(timestamp) \ .set_activity_progress('Completed') \ .set_grading_progress('FullyGraded') \ .set_user_id(sub) sc_line_item = LineItem() sc_line_item.set_tag('score') \ .set_score_maximum(100) \ .set_label('Score') grades.put_grade(sc, sc_line_item) tm = Grade() tm.set_score_given(time_spent) \ .set_score_maximum(999) \ .set_timestamp(timestamp) \ .set_activity_progress('Completed') \ .set_grading_progress('FullyGraded') \ .set_user_id(sub) tm_line_item = LineItem() tm_line_item.set_tag('time') \ .set_score_maximum(999) \ .set_label('Time Taken') result = grades.put_grade(tm, tm_line_item) return jsonify({'success': True, 'result': result.get('body')})
def scoreboard(launch_id): tool_conf = ToolConfJsonFile(get_lti_config_path()) flask_request = FlaskRequest() message_launch = ExtendedFlaskMessageLaunch.from_cache( launch_id, flask_request, tool_conf) if not message_launch.has_nrps(): raise Forbidden("Don't have names and roles!") if not message_launch.has_ags(): raise Forbidden("Don't have grades!") ags = message_launch.get_ags() score_line_item = LineItem() score_line_item.set_tag('score') \ .set_score_maximum(100) \ .set_label('Score') scores = ags.get_grades(score_line_item) time_line_item = LineItem() time_line_item.set_tag('time') \ .set_score_maximum(999) \ .set_label('Time Taken') times = ags.get_grades(time_line_item) members = message_launch.get_nrps().get_members() scoreboard_result = [] for sc in scores: result = {'score': sc['resultScore']} for tm in times: if tm['userId'] == sc['userId']: result['time'] = tm['resultScore'] break for member in members: if member['user_id'] == sc['userId']: result['name'] = member.get('name', 'Unknown') break scoreboard_result.append(result) return jsonify(scoreboard_result)
def scoreboard(request, launch_id): tool_conf = get_tool_conf() launch_data_storage = get_launch_data_storage() message_launch = ExtendedDjangoMessageLaunch.from_cache( launch_id, request, tool_conf, launch_data_storage=launch_data_storage) resource_link_id = message_launch.get_launch_data() \ .get('https://purl.imsglobal.org/spec/lti/claim/resource_link', {}).get('id') if not message_launch.has_nrps(): return HttpResponseForbidden("Don't have names and roles!") if not message_launch.has_ags(): return HttpResponseForbidden("Don't have grades!") ags = message_launch.get_ags() score_line_item = LineItem() score_line_item.set_tag('score') \ .set_score_maximum(100) \ .set_label('Score') if resource_link_id: score_line_item.set_resource_id(resource_link_id) scores = ags.get_grades(score_line_item) time_line_item = LineItem() time_line_item.set_tag('time') \ .set_score_maximum(999) \ .set_label('Time Taken') if resource_link_id: time_line_item.set_resource_id(resource_link_id) times = ags.get_grades(time_line_item) members = message_launch.get_nrps().get_members() scoreboard_result = [] for sc in scores: result = {'score': sc['resultScore']} for tm in times: if tm['userId'] == sc['userId']: result['time'] = tm['resultScore'] break for member in members: if member['user_id'] == sc['userId']: result['name'] = member.get('name', 'Unknown') break scoreboard_result.append(result) return JsonResponse(scoreboard_result, safe=False)
def score(request, launch_id, earned_score, time_spent): tool_conf = get_tool_conf() launch_data_storage = get_launch_data_storage() message_launch = ExtendedDjangoMessageLaunch.from_cache( launch_id, request, tool_conf, launch_data_storage=launch_data_storage) resource_link_id = message_launch.get_launch_data() \ .get('https://purl.imsglobal.org/spec/lti/claim/resource_link', {}).get('id') if not message_launch.has_ags(): return HttpResponseForbidden("Don't have grades!") sub = message_launch.get_launch_data().get('sub') timestamp = datetime.datetime.utcnow().isoformat() + 'Z' earned_score = int(earned_score) time_spent = int(time_spent) grades = message_launch.get_ags() sc = Grade() sc.set_score_given(earned_score)\ .set_score_maximum(100)\ .set_timestamp(timestamp)\ .set_activity_progress('Completed')\ .set_grading_progress('FullyGraded')\ .set_user_id(sub) sc_line_item = LineItem() sc_line_item.set_tag('score')\ .set_score_maximum(100)\ .set_label('Score') if resource_link_id: sc_line_item.set_resource_id(resource_link_id) grades.put_grade(sc, sc_line_item) tm = Grade() tm.set_score_given(time_spent)\ .set_score_maximum(999)\ .set_timestamp(timestamp)\ .set_activity_progress('Completed')\ .set_grading_progress('FullyGraded')\ .set_user_id(sub) tm_line_item = LineItem() tm_line_item.set_tag('time')\ .set_score_maximum(999)\ .set_label('Time Taken') if resource_link_id: tm_line_item.set_resource_id(resource_link_id) result = grades.put_grade(tm, tm_line_item) return JsonResponse({'success': True, 'result': result.get('body')})
def test_get_grades(self, name, line_items_exist): # pylint: disable=unused-argument from pylti1p3.contrib.django import DjangoMessageLaunch tool_conf = get_test_tool_conf() with patch.object(DjangoMessageLaunch, "_get_jwt_body", autospec=True) as get_jwt_body: message_launch = DjangoMessageLaunch(FakeRequest(), tool_conf) line_items_url = 'http://canvas.docker/api/lti/courses/1/line_items' get_jwt_body.side_effect = lambda x: self._get_jwt_body() with patch('socket.gethostbyname', return_value="127.0.0.1"): with requests_mock.Mocker() as m: m.post(self._get_auth_token_url(), text=json.dumps(self._get_auth_token_response())) line_items_response = [] if line_items_exist: line_items_response = [{ 'scoreMaximum': 100.0, 'tag': 'score', 'id': 'http://canvas.docker/api/lti/courses/1/line_items/1', 'label': 'Score' }, { 'scoreMaximum': 999.0, 'tag': 'time', 'id': 'http://canvas.docker/api/lti/courses/1/line_items/2', 'label': 'Time Taken' }] else: m.post( line_items_url, text=json.dumps({ 'scoreMaximum': 100.0, 'tag': 'score', 'id': 'http://canvas.docker/api/lti/courses/1/line_items/1', 'label': 'Score' })) m.get(line_items_url, text=json.dumps(line_items_response)) m.get( 'http://canvas.docker/api/lti/courses/1/line_items/1/results', text=json.dumps([{ 'resultScore': 13.0, 'resultMaximum': 100.0, 'userId': '20eb59f5-26e8-46bc-87b0-57ed54820aeb', 'id': 'http://canvas.docker/api/lti/courses/1/line_items/1/results/1', 'scoreOf': 'http://canvas.docker/api/lti/courses/1/line_items/1' }])) score_line_item = LineItem() score_line_item.set_tag('score') \ .set_score_maximum(100) \ .set_label('Score') scores = message_launch.validate_registration().get_ags( ).get_grades(score_line_item) self.assertEqual(len(scores), 1) self.assertDictEqual( scores[0], { 'resultScore': 13.0, 'resultMaximum': 100.0, 'userId': '20eb59f5-26e8-46bc-87b0-57ed54820aeb', 'id': 'http://canvas.docker/api/lti/courses/1/line_items/1/results/1', 'scoreOf': 'http://canvas.docker/api/lti/courses/1/line_items/1' })