def modify_chapter(course_id, chapter_id): if "user_id" not in session: return redirect("/") if session["role"] not in ["teacher", "admin"]: return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä.") if (session["role"] == "teacher" and not courses.user_enrolled(session["user_id"],course_id)): return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä \ ennen kuin olet ilmottautunut kurssille.") if request.method == "GET": chapter = course_contents.get_chapter(course_id, chapter_id) if chapter == None: return render_template( "error.html", message="Lukua ei löydy kyseessä olevan kurssin alta.") return render_template( "/courses/chapters/modify.html", course_id=course_id, chapter_id=chapter_id, chapter=chapter) if request.method == "POST": if session["csrf_token"] != request.form["csrf_token"]: abort(403) if course_contents.update_chapter(request.form): return redirect("/courses/course"+str(course_id)+ "/chapters/chapter"+str(chapter_id)) else: return render_template( "error.html", message="Jokin meni pieleen muokatessa lukua. :(")
def new_chapter(id): if "user_id" not in session: return redirect("/") if session["role"] not in ["teacher", "admin"]: return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä.") if session["role"] == "teacher" and not courses.user_enrolled(session["user_id"],id): return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä \ ennen kuin olet ilmottautunut kurssille.") chapter_count = len(course_contents.get_course_chapters(id)) print("toimii") if request.method == "GET": return render_template( "/courses/chapters/new.html", course_id=id, chapter_count=chapter_count) if request.method == "POST": if session["csrf_token"] != request.form["csrf_token"]: abort(403) ordinal = request.form["ordinal"] name = request.form["name"] content = request.form["content"] chapter_id = course_contents.add_chapter(ordinal, name, content, id) if chapter_id != None: return redirect("/courses/course"+str(id)+ "/chapters/chapter"+str(chapter_id)) else: return render_template( "error.html", message="Jokin meni pieleen lisättäessä lukua. :(")
def exercise(course_id, chapter_id, exercise_id): if "user_id" not in session: return redirect("/") chapter = course_contents.get_chapter(course_id, chapter_id) exercise = course_contents.get_exercise(course_id, chapter_id, exercise_id) if chapter == None or exercise == None: return render_template( "error.html", message="Lukua tai tehtävää ei löydy kurssin alta.") enrolled = courses.user_enrolled(session["user_id"],course_id) if session["role"] == "student" and not enrolled: return render_template( "error.html", message="Sinulla ei ole oikeuksia tarkastella kurssin sisältöä \ ennen kuin olet ilmoittautunut kurssille.") choices = course_contents.get_exercise_choises(exercise_id) answer = course_contents.get_answer_of_current_user(exercise_id) if "message" in session: message = session["message"] del session["message"] else: message = None if request.method == "GET": return render_template( "/courses/exercises/exercise.html", course_id=course_id, chapter_id=chapter_id, exercise_id=exercise_id, chapter=chapter, exercise=exercise, choices=choices, answer=answer, message=message, enrolled=enrolled) if request.method == "POST": if session["csrf_token"] != request.form["csrf_token"]: abort(403) if session["role"] in ["teacher","admin"]: return render_template( "/courses/exercises/exercise.html", course_id=course_id, chapter_id=chapter_id, exercise_id=exercise_id, chapter=chapter, exercise=exercise, choices=choices, answer=request.form["choice"], enrolled=enrolled) if course_contents.add_answer(exercise_id, request.form["choice"]): return redirect("/courses/course"+str(course_id)+ "/chapters/chapter"+str(chapter_id)+ "/exercises/exercise"+str(exercise_id)) else: return render_template( "error.html", message="Jokin meni pieleen vastatessa tehtävään. :(")
def new_exercise(course_id, chapter_id): if "user_id" not in session: return redirect("/") if session["role"] not in ["teacher", "admin"]: return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä.") if (session["role"] == "teacher" and not courses.user_enrolled(session["user_id"],course_id)): return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä \ ennen kuin olet ilmottautunut kurssille.") if course_contents.get_chapter(course_id, chapter_id) == None: return render_template( "error.html", message="Lukua ei löydy kurssin alta. \ Teethän tehtävän olemassa olevan kurssin ja luvun alle.") exercise_count = len(course_contents.get_chapter_exercises(chapter_id)) if request.method == "GET": return render_template( "/courses/exercises/new_initial.html", course_id=course_id, chapter_id=chapter_id, choices=4, exercise_count=exercise_count) if request.method == "POST": if session["csrf_token"] != request.form["csrf_token"]: abort(403) if request.form["button"] == "Päivitä vaihtoehtojen määrä": choices = int(request.form["choices"]) return render_template( "/courses/exercises/new_updated.html", course_id=course_id, chapter_id=chapter_id, choices=choices, exercise_count=exercise_count, form=request.form) if request.form["button"] == "Luo tehtävä": exercise_id = course_contents.add_exercise(request.form) if exercise_id != None: session["message"] = "Tehtävä luotu onnistuneesti! \ Voit nyt katsella tehtävää opiskelijan näkökulmasta." return redirect("/courses/course"+str(course_id)+ "/chapters/chapter"+str(chapter_id)+ "/exercises/exercise"+str(exercise_id)) else: return render_template( "error.html", message="Jokin meni pieleen luodessa tehtävää. :(")
def course(id): if "user_id" not in session: return redirect("/") course = courses.get_course(id) enrolled = courses.user_enrolled(session["user_id"],id) participants = courses.get_participants_of_course(id) chapters = course_contents.get_course_chapters(id) exercise_results = course_contents.get_course_result_summary(id) return render_template( "courses/course.html", course=course, enrolled = enrolled, participants=participants, chapters=chapters, exercise_results=exercise_results)
def modify_exercise(course_id, chapter_id, exercise_id): if session["role"] not in ["teacher", "admin"]: return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä.") if (session["role"] == "teacher" and not courses.user_enrolled(session["user_id"],course_id)): return render_template( "error.html", message="Sinulla ei ole oikeuksia muokata kurssin sisältöä \ ennen kuin olet ilmottautunut kurssille.") if course_contents.get_chapter(course_id, chapter_id) == None: return render_template( "error.html", message="Lukua ei löydy kurssin alta. \ Teethän tehtävän olemassa olevan kurssin ja luvun alle.") exercise = course_contents.get_exercise(course_id, chapter_id, exercise_id) choices = course_contents.get_exercise_choises(exercise_id) if request.method == "GET": if exercise != None: return render_template( "courses/exercises/modify.html", exercise=exercise, choices=choices, course_id=course_id, chapter_id=chapter_id, exercise_id=exercise_id) else: return render_template( "error.html", message="Tehtävää ei löydy kurssin luvun alta.") if request.method == "POST": if session["csrf_token"] != request.form["csrf_token"]: abort(403) parameters = request.form if course_contents.update_exercise_and_choices(parameters, choices): session["message"] = "Tehtävän päivitys onnistui!" return redirect("/courses/course"+str(course_id)+ "/chapters/chapter"+str(chapter_id)+ "/exercises/exercise"+str(exercise_id)) else: return render_template( "error.html", message="Jokin meni pieleen päivitettäessä tehtävää.")
def chapter(course_id, chapter_id): if "user_id" not in session: return redirect("/") chapter = course_contents.get_chapter(course_id, chapter_id) if chapter == None: return render_template( "error.html", message="Lukua ei löydy kyseessä olevan kurssin alta") enrolled = courses.user_enrolled(session["user_id"],course_id) if session["role"] == "student" and not enrolled: return render_template( "error.html", message="Sinulla ei ole oikeuksia tarkastella kurssin sisältöä \ ennen kuin olet ilmoittautunut kurssille.") exercises = course_contents.get_chapter_exercises(chapter_id) return render_template( "/courses/chapters/chapter.html", chapter=chapter, chapter_id=chapter_id, course_id=course_id, exercises=exercises, enrolled=enrolled)