From fd2a2abd0a556bdec5fcfdcdf3f39f9980b92333 Mon Sep 17 00:00:00 2001
From: HanseGucker <joniweth@gmx.de>
Date: Mon, 22 Apr 2019 15:29:08 +0200
Subject: [PATCH] Show hints in plan and my plan for students

---
 biscuit/apps/chronos/hints.py                 | 20 ++++++++++++-
 .../templates/timetable/hintsinplan.html      | 18 ++++++++++++
 .../chronos/templates/timetable/myplan.html   |  5 ++++
 .../chronos/templates/timetable/plan.html     |  3 ++
 biscuit/apps/chronos/views.py                 | 28 +++++++++++++------
 5 files changed, 64 insertions(+), 10 deletions(-)
 create mode 100644 biscuit/apps/chronos/templates/timetable/hintsinplan.html

diff --git a/biscuit/apps/chronos/hints.py b/biscuit/apps/chronos/hints.py
index 839006fd..d8748bd1 100644
--- a/biscuit/apps/chronos/hints.py
+++ b/biscuit/apps/chronos/hints.py
@@ -1,3 +1,5 @@
+import datetime
+
 from timetable.models import Hint
 
 
@@ -6,9 +8,25 @@ def get_all_hints_by_date(date):
     return hints
 
 
+def get_all_hints_by_class_and_time_period(_class, from_date, to_date):
+    hints_tmp = get_all_hints_by_time_period(from_date, to_date)
+    hints_match = []
+    for hint in hints_tmp:
+        if _class.id in [x.class_id for x in hint.classes.all()]:
+            hints_match.append(hint)
+    return hints_match
+
+
 def get_all_hints_by_time_period(from_date, to_date):
     print(from_date, to_date)
-    hints = Hint.objects.filter(from_date__gte=from_date, to_date__lte=to_date).order_by("from_date", "classes")
+    week_days = [from_date + datetime.timedelta(days=i) for i in range(5)]
+
+    hints = []
+    for week_day in week_days:
+        hints_tmp = get_all_hints_by_date(week_day)
+        for hint in hints_tmp:
+            if hint not in hints:
+                hints.append(hint)
     print(hints)
     return hints
 
diff --git a/biscuit/apps/chronos/templates/timetable/hintsinplan.html b/biscuit/apps/chronos/templates/timetable/hintsinplan.html
new file mode 100644
index 00000000..0af7d78b
--- /dev/null
+++ b/biscuit/apps/chronos/templates/timetable/hintsinplan.html
@@ -0,0 +1,18 @@
+{% load martortags %}
+{% if hints %}
+    {% for hint in hints %}
+        <div class="alert primary">
+            <div>
+                <em class="right">Hinweis für {{ hint.from_date|date:"D" }}
+                    {% if hint.from_date != hint.to_date %}
+                        bis {{ hint.to_date|date:"D" }}
+                    {% endif %} </em>
+
+                <i class="material-icons left">announcement</i>
+                {{ hint.text|safe_markdown }}
+
+                {#        {{ hint }}#}
+            </div>
+        </div>
+    {% endfor %}
+{% endif %}
\ No newline at end of file
diff --git a/biscuit/apps/chronos/templates/timetable/myplan.html b/biscuit/apps/chronos/templates/timetable/myplan.html
index 798b4a68..c2ae550c 100644
--- a/biscuit/apps/chronos/templates/timetable/myplan.html
+++ b/biscuit/apps/chronos/templates/timetable/myplan.html
@@ -13,6 +13,11 @@
         </div>
     </div>
 
+    <div class="row nomargin">
+        <div class="col m12 s12 l6">
+            {% include "timetable/hintsinplan.html" %}
+        </div>
+    </div>
 
     <script type="text/javascript">
         var dest = "/timetable/my/";
diff --git a/biscuit/apps/chronos/templates/timetable/plan.html b/biscuit/apps/chronos/templates/timetable/plan.html
index 5d0297d4..611b5103 100755
--- a/biscuit/apps/chronos/templates/timetable/plan.html
+++ b/biscuit/apps/chronos/templates/timetable/plan.html
@@ -93,6 +93,9 @@
             </a>
         {% endif %}
     </div>
+
+    {% include "timetable/hintsinplan.html" %}
+
     <div class="timetable-plan">
 
         {#  Week days #}
diff --git a/biscuit/apps/chronos/views.py b/biscuit/apps/chronos/views.py
index 5fb9c75c..788e3d9d 100755
--- a/biscuit/apps/chronos/views.py
+++ b/biscuit/apps/chronos/views.py
@@ -11,7 +11,7 @@ from material import Fieldset, Row
 from schoolapps.settings import WEEK_DAYS
 from timetable.filters import HintFilter
 from timetable.forms import HintForm
-from timetable.hints import get_all_hints_by_date, get_all_hints_by_time_period
+from timetable.hints import get_all_hints_by_date, get_all_hints_by_time_period, get_all_hints_by_class_and_time_period
 from timetable.pdf import generate_class_tex, generate_pdf
 
 from untisconnect.plan import get_plan, TYPE_TEACHER, TYPE_CLASS, TYPE_ROOM, parse_lesson_times
@@ -94,7 +94,10 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
         smart = True
 
     monday_of_week = get_calendar_week(calendar_week, year)["first_day"]
+    friday = monday_of_week + datetime.timedelta(days=4)
+
     # print(monday_of_week)
+    hints = None
 
     if plan_type == 'teacher':
         _type = TYPE_TEACHER
@@ -102,6 +105,12 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
     elif plan_type == 'class':
         _type = TYPE_CLASS
         el = get_class_by_id(plan_id)
+
+        # Get hints
+        if smart:
+            hints = list(get_all_hints_by_class_and_time_period(el, monday_of_week, friday))
+            print(hints)
+
     elif plan_type == 'room':
         _type = TYPE_ROOM
         el = get_room_by_id(plan_id)
@@ -111,12 +120,6 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
     plan = get_plan(_type, plan_id, smart=smart, monday_of_week=monday_of_week)
     # print(parse_lesson_times())
 
-    # Get hints
-    if smart:
-        friday = monday_of_week + datetime.timedelta(days=4)
-        hints = list(get_all_hints_by_time_period(monday_of_week, friday))
-        print(hints)
-
     context = {
         "smart": smart,
         "type": _type,
@@ -128,7 +131,8 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
         "weeks": get_calendar_weeks(year=year),
         "selected_week": calendar_week,
         "selected_year": year,
-        "week_days": WEEK_DAYS
+        "week_days": WEEK_DAYS,
+        "hints": hints
     }
 
     return render(request, 'timetable/plan.html', context)
@@ -158,6 +162,7 @@ def my_plan(request, year=None, day=None, month=None):
         plan_id = el.id
         raw_type = "teacher"
         # print(el)
+        hints = []
     elif _type == UserInformation.STUDENT:
         _type = TYPE_CLASS
         _name = UserInformation.user_classes(request.user)[0]
@@ -165,6 +170,10 @@ def my_plan(request, year=None, day=None, month=None):
         el = get_class_by_name(_name)
         plan_id = el.id
         raw_type = "class"
+
+        # Get hints
+        hints = list(get_all_hints_by_class_and_time_period(el, date, date))
+        print(hints)
     else:
         return redirect("timetable_admin_all")
     # print(monday_of_week)
@@ -183,7 +192,8 @@ def my_plan(request, year=None, day=None, month=None):
         "week_days": WEEK_DAYS,
         "date": date,
         "date_js": int(date.timestamp()) * 1000,
-        "display_date_only": True
+        "display_date_only": True,
+        "hints": hints
     }
     # print(context["week_day"])
 
-- 
GitLab