Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merged moodle data #125

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions common/djangoapps/student/views/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 :
Expand All @@ -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):
Expand Down
Loading