Skip to content

Commit

Permalink
Add moodle API
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoest committed Jan 10, 2025
1 parent 9a3415a commit 21d4151
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
31 changes: 29 additions & 2 deletions S1/waitinglists/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

load_dotenv()

quiz_ids = [1525, 1526, 1527, 1528]


def send_forum_msg(id: int, title: str, msg: str, link_text: str, link_url: str) -> any:
data = {
Expand All @@ -22,7 +24,9 @@ def send_forum_msg(id: int, title: str, msg: str, link_text: str, link_url: str)
return r.json()


def send_moodle_activity_completion(user_id: int, course_module_id: int) -> tuple[bool, float]:
def send_moodle_activity_completion(
user_id: int, course_module_id: int
) -> tuple[bool, float]:

header = {"Authorization": f"Token {os.getenv("VATGER_API_KEY")}"}
r = requests.get(
Expand All @@ -47,6 +51,18 @@ def send_moodle_enrol_user(user_id: int, course_id: int) -> bool:
return False


def send_moodle_find_user(user_id: int) -> bool:
header = {"Authorization": f"Token {os.getenv("VATGER_API_KEY")}"}
r = requests.get(
f"http://vatsim-germany.org/api/moodle/user/{user_id}",
headers=header,
).json()
try:
return r["id"]
except:
return False


def send_moodle_count_attempts(user_id: int, course_module_id: int) -> int:
header = {"Authorization": f"Token {os.getenv("VATGER_API_KEY")}"}
r = requests.get(
Expand All @@ -58,7 +74,10 @@ def send_moodle_count_attempts(user_id: int, course_module_id: int) -> int:
except:
return 0

def send_moodle_override_attempts(user_id: int, course_module_id: int, attempts: int) -> bool:

def send_moodle_override_attempts(
user_id: int, course_module_id: int, attempts: int
) -> bool:
header = {"Authorization": f"Token {os.getenv("VATGER_API_KEY")}"}
r = requests.get(
f"http://vatsim-germany.org/api/moodle/quiz/{course_module_id}/user/{user_id}/override/attempts/{attempts}",
Expand All @@ -68,3 +87,11 @@ def send_moodle_override_attempts(user_id: int, course_module_id: int, attempts:
return r
except:
return False


def enrol_and_check_overrides(vatsim_id: int):
# Enrols user in Module 2 and updates overrides accordingly.
res = send_moodle_enrol_user(vatsim_id, 86)
for id in quiz_ids:
attempts = send_moodle_count_attempts(vatsim_id, id)
send_moodle_override_attempts(vatsim_id, id, attempts)
10 changes: 8 additions & 2 deletions S1/waitinglists/templates/waitinglists/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h4>My Progress</h4>
</div>
</div>
<div class="row">
{% if is_ger %}
{% if is_ger and is_moodle_signed_up %}
<div class="col-12">
<table class="table table-striped">
<thead>
Expand Down Expand Up @@ -52,11 +52,17 @@ <h4>My Progress</h4>
</tbody>
</table>
</div>
{% else %}
{% endif %}
{% if not is_ger %}
<div class="col-12">
<p>❌ You are not assigned to the German subdivision.</p>
</div>
{% endif %}
{% if not is_moodle_signed_up %}
<div class="col-12">
<p>❌ You are not signed up to the Moodle platform.</p>
</div>
{% endif %}
</div>
</div>

Expand Down
7 changes: 5 additions & 2 deletions S1/waitinglists/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .models import Attendance, Session, WaitingList, Module, Signup
from .forms import AttendanceForm
from .helpers import send_moodle_activity_completion
from .helpers import enrol_and_check_overrides, send_moodle_find_user

import requests
from dotenv import load_dotenv
Expand Down Expand Up @@ -80,7 +80,7 @@ def index(request):
user.userdetail.module_2_completed or module_2_completed(user)
)
is_ger = user.userdetail.subdivision == "GER"

is_moodle_signed_up = send_moodle_find_user(user.username)
waiting_for_modules = []

module_list = Module.objects.all().order_by("name")
Expand Down Expand Up @@ -154,6 +154,7 @@ def index(request):
"attendances": attendances,
"attended_session_ids": attendend_session_ids,
"is_mentor": is_mentor(request.user),
"is_moodle_signed_up": is_moodle_signed_up,
}
return HttpResponse(template.render(context, request))

Expand Down Expand Up @@ -242,6 +243,8 @@ def update_attendance(request, session_id):
except:
pass
check_modules(attendance.user.id)
if session.module.name == "Module 1":
enrol_and_check_overrides(attendance.user.username)
elif attendance.attended == "ABS":
try:
waiting_list_entry = WaitingList.objects.get(
Expand Down

0 comments on commit 21d4151

Please sign in to comment.