From 7cd0a6693c65609c0645ff93c1a7fa23e657f29b Mon Sep 17 00:00:00 2001 From: venkata-s123 <154975888+venkata-s123@users.noreply.github.com> Date: Tue, 31 Dec 2024 18:37:14 +0530 Subject: [PATCH] merged moodle data --- common/djangoapps/student/views/management.py | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/common/djangoapps/student/views/management.py b/common/djangoapps/student/views/management.py index 98b14bda06ed..4e646821abf5 100644 --- a/common/djangoapps/student/views/management.py +++ b/common/djangoapps/student/views/management.py @@ -1297,6 +1297,7 @@ def merge_grades(responses): def attendance_report(request): try: moodle_base_url = configuration_helpers.get_value("MOODLE_URL", "") + multiple_base_url = configuration_helpers.get_value("MULTIPLE_MOODLE_URLS", "") moodle_service_url = moodle_base_url + "/webservice/rest/server.php" moodle_wstoken = configuration_helpers.get_value("MOODLE_TOKEN", "") course_attendance_function = "mod_wsattendance_get_attendance" @@ -1306,8 +1307,23 @@ def attendance_report(request): site = "" headers = {'content-type': "text/plain"} querystring = {"wstoken" : moodle_wstoken, "wsfunction" : course_attendance_function, "moodlewsrestformat" : "json", "user_email":request.user.email, "site_name" : site } - response = requests.request("POST", moodle_service_url, headers = headers, params = querystring) - context = {'attendance_report' : json.loads(response.text), 'cohort_name' : request.GET["cohort_name"]} + + responses = {} + for index_no,api_url in enumerate(multiple_base_url): + + moodle_service_url = api_url + "/webservice/rest/server.php" + response = requests.request("POST", moodle_service_url, headers = headers, params = querystring) + + + context_data = json.loads(response.text) + log.info("API call response for Course {0} and data is {1} ".format(api_url,context_data)) + + + if "Response" + str(index_no) not in responses: + responses["Response" + str(index_no)] = context_data + + final_response = merged_attendance(responses) if len(responses) > 1 else responses.get(list(responses.keys())[0]) if len(responses) == 1 else {} + context = {'attendance_report' : final_response, 'cohort_name' : request.GET["cohort_name"]} if configuration_helpers.get_value('ATTENDANCE_TEMPLATE'): return render(request, configuration_helpers.get_value('ATTENDANCE_TEMPLATE'), context = context) else : @@ -1316,6 +1332,39 @@ def attendance_report(request): log.info(e) return {} +def merged_attendance(responses): + mergeddata = {} + for key,value in responses.items(): + for key_data, value_data in value.items(): + mergeddata[key_data] = value_data if key_data not in mergeddata else mergeddata[key_data] + value_data + + mergeddata["status_summary"] = [{"status": status, "count": sum(entry["count"] for entry in mergeddata["status_summary"] if entry["status"] == status)} for status in set(entry["status"] for entry in mergeddata["status_summary"])] + mergeddata["session_summary"] = [{"total_sessions": sum(data["total_sessions"] for data in mergeddata["session_summary"]), + "sessions_present": sum(data["sessions_present"] for data in mergeddata["session_summary"]), + "overall_percentage": f"{(sum(data['sessions_present'] for data in mergeddata['session_summary']) * 100) / sum(data['total_sessions'] for data in mergeddata['session_summary']):.2f}"}] + + total_numtakensessions ,total_takensessionspoints ,total_takensessionsmaxpoints= 0,0,0 + + for course in mergeddata["percentage"]: + total_numtakensessions += int(course["numtakensessions"]) + total_takensessionspoints += float(course["takensessionspoints"]) + total_takensessionsmaxpoints += float(course["takensessionsmaxpoints"]) + + mergeddata["percentage"] = [ + { + "numtakensessions": str(total_numtakensessions), + "takensessionspoints": str(total_takensessionspoints), + "takensessionsmaxpoints": str(total_takensessionsmaxpoints), + "takensessionspercentage": total_takensessionspoints/total_takensessionsmaxpoints, + "userstakensessionsbyacronym": "", + "pointssessionscompleted": mergeddata["percentage"][0]["pointssessionscompleted"], + "percentagesessionscompleted": f"{((total_takensessionspoints / total_takensessionsmaxpoints) * 100):.1f}%", + "course": mergeddata["percentage"][0]["course"], + "course_id": mergeddata["percentage"][0]["course_id"] + } + ] + return mergeddata + @csrf_exempt def extras_reset_password_link(request):