Skip to content

Commit

Permalink
add teacher preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jtwaleson committed Nov 29, 2023
1 parent 59513ed commit 8fb82f1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,8 @@ def index(level, program_id):

customizations = {}
if current_user()['username']:
customizations = DATABASE.get_student_class_customizations(current_user()['username'])
customizations = DATABASE.get_student_class_customizations(
current_user()['username'], preview_class_as_teacher=session.get("preview_class"))

if 'levels' in customizations:
available_levels = customizations['levels']
Expand Down
2 changes: 2 additions & 0 deletions templates/for-teachers.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2>{{_('my_classes')}}</h2>
<!-- This is a bucket-fix to get the first letter capitalized, should find a long-term solution -->
<th class="px-4 py-2">{{_('students')[0]|upper}}{{_('students')[1:]}}</th>
<th class="p-2">{{_('duplicate')}}</th>
<th class="p-2">{{_('preview')}}</th>
<th class="p-2">{{_('remove')}}</th>
</tr>
</thead>
Expand All @@ -32,6 +33,7 @@ <h2>{{_('my_classes')}}</h2>
<td class="username_cell">{{class.teacher}}</td>
<td class="text-center p-2">{{class.students|length}}</td>
<td class="text-center p-2"><a class="no-underline cursor-pointer" onclick='hedyApp.duplicate_class("{{class.id}}", {{_('class_name_prompt')|default(None)|tojson}}, {% if class.teacher != username %}"{{class.name}}"{% else %}" "{% endif %})'><span class="fas fa-copy"></span></a></td>
<td class="text-center p-2"><a href="for-teachers/class/{{class.id}}/preview" class="no-underline cursor-pointer preview_class" data-cy="preview_class_link">👁</a></td>
<td class="text-center p-2"><a class="no-underline cursor-pointer" onclick='hedyApp.delete_class("{{class.id}}", {{_('delete_class_prompt')|default(None)|tojson}})'>🗑️</a></td>
</tr>
{% endfor %}
Expand Down
6 changes: 6 additions & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
{% if menu != False %}
{% include "incl/menubar.html" %}
{% endif %}
{% if session.preview_class %}
<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4" role="alert">
<p class="font-bold">Preview mode</p>
<p>You are previewing a class as a teacher. <a href="/for-teachers/clear-preview-class">Exit preview mode</a></p>
</div>
{% endif %}
{# Can't reindent this as it may contain preformatted code blocks whose indentation is important. #}
{% block full_width_content %}{% endblock %}

Expand Down
7 changes: 6 additions & 1 deletion website/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ def get_class_customizations(self, class_id):
customizations = CUSTOMIZATIONS.get({"id": class_id})
return customizations

def get_student_class_customizations(self, user):
def get_student_class_customizations(self, user, preview_class_as_teacher=None):
"""Return customizations for the very first class this user is part of.
If the user is part of multiple classes, they will only get the customizations
Expand All @@ -775,6 +775,11 @@ def get_student_class_customizations(self, user):
if student_classes:
class_customizations = self.get_class_customizations(student_classes[0]["id"])
return class_customizations or {}
elif preview_class_as_teacher:
for Class in self.get_teacher_classes(user):
if preview_class_as_teacher == Class["id"]:
class_customizations = self.get_class_customizations(preview_class_as_teacher)
return class_customizations or {}
return {}

def progress_by_username(self, username):
Expand Down
22 changes: 21 additions & 1 deletion website/for_teachers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import uuid

from flask import g, jsonify, request, session, url_for
from flask import g, jsonify, request, session, url_for, redirect
from jinja_partials import render_partial
from flask_babel import gettext

Expand Down Expand Up @@ -208,6 +208,26 @@ def get_class(self, user, class_id):
survey_later=survey_later,
)

@route("/class/<class_id>/preview", methods=["GET"])
@requires_login
def preview_class_as_teacher(self, user, class_id):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(class_id)
if not Class or (not utils.can_edit_class(user, Class) and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
session["preview_class"] = Class["id"]
return redirect("/hedy")

@route("/clear-preview-class", methods=["GET"])
# Note: we explicitly do not need login here, anyone can exit preview mode
def clear_preview_class(self):
try:
del session["preview_class"]
except KeyError:
pass
return redirect("/for-teachers")

@route("/class/<class_id>/programs/<username>", methods=["GET", "POST"])
@requires_teacher
def public_programs(self, user, class_id, username):
Expand Down

0 comments on commit 8fb82f1

Please sign in to comment.