Skip to content

Commit

Permalink
Merge branch 'master' into ghatighorias/make_login_case_insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
ghatighorias committed Sep 20, 2017
2 parents 22edfa3 + 57d71b4 commit 5fb7293
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 213 deletions.
2 changes: 1 addition & 1 deletion lib/course_planner/classes/classes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule CoursePlanner.Classes do
Repo.all(from c in Class, where: c.offered_course_id == ^offered_course_id)
end

def classes_with_attendances(offered_course_id, user_id) do
def classes_with_attendances(offered_course_id, user_id) do
query = from c in Class,
left_join: a in assoc(c, :attendances), on: a.student_id == ^user_id,
where: c.offered_course_id == ^offered_course_id,
Expand Down
18 changes: 0 additions & 18 deletions lib/course_planner/courses/offered_courses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ defmodule CoursePlanner.Courses.OfferedCourses do
|> Enum.into(%{})
end

def find_all_by_user(%{role: "Coordinator"}) do
Repo.all(OfferedCourse)
end
def find_all_by_user(%{role: "Teacher", id: user_id}) do
Repo.all(
from oc in OfferedCourse,
join: t in assoc(oc, :teachers),
where: t.id == ^user_id
)
end
def find_all_by_user(%{role: "Student", id: user_id}) do
Repo.all(
from oc in OfferedCourse,
join: s in assoc(oc, :students),
where: s.id == ^user_id
)
end

def student_matrix(term_id) do
offered_courses =
term_id
Expand Down
30 changes: 30 additions & 0 deletions lib/course_planner/terms/terms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule CoursePlanner.Terms do
@moduledoc """
Handle all interactions with Terms, create, list, fetch, edit, and delete
"""
import Ecto.Query

alias CoursePlanner.{Repo, Courses.OfferedCourses, Notifications.Notifier,
Accounts.Coordinators, Notifications}
alias CoursePlanner.Terms.{Holiday, Term}
Expand Down Expand Up @@ -98,4 +100,32 @@ defmodule CoursePlanner.Terms do
students_and_teachers = OfferedCourses.get_subscribed_users(offered_courses)
students_and_teachers ++ Coordinators.all()
end

def find_all_by_user(%{role: "Coordinator"}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
preload: [offered_courses: {oc, course: co}],
order_by: [asc: t.start_date, asc: co.name])
end
def find_all_by_user(%{role: "Teacher", id: user_id}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: te in assoc(oc, :teachers),
preload: [offered_courses: {oc, course: co, teachers: te}],
where: te.id == ^user_id,
order_by: [asc: t.start_date, asc: co.name]
)
end
def find_all_by_user(%{role: "Student", id: user_id}) do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: s in assoc(oc, :students),
preload: [offered_courses: {oc, course: co, students: s}],
where: s.id == ^user_id,
order_by: [asc: t.start_date, asc: co.name]
)
end
end
17 changes: 11 additions & 6 deletions lib/course_planner_web/controllers/class_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ defmodule CoursePlannerWeb.ClassController do
@moduledoc false
use CoursePlannerWeb, :controller

alias CoursePlanner.{Classes.Class, Classes, Attendances}
alias CoursePlanner.{Classes.Class, Classes, Attendances, Terms.Term}

import Canary.Plugs
plug :authorize_controller

def index(conn, _params) do
classes =
Class
|> Repo.all()
|> Repo.preload([:offered_course, offered_course: :term, offered_course: :course])
render(conn, "index.html", classes: classes)
query = from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
join: c in assoc(oc, :classes),
preload: [offered_courses: {oc, classes: c, course: co}],
order_by: [asc: t.start_date, asc: co.name, asc: c.date, asc: c.starting_at, asc: c.finishes_at]

terms = Repo.all(query)

render(conn, "index.html", terms: terms)
end

def new(conn, _params) do
Expand Down
10 changes: 4 additions & 6 deletions lib/course_planner_web/controllers/offered_course_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ defmodule CoursePlannerWeb.OfferedCourseController do
Courses.OfferedCourses,
Accounts.Students,
Accounts.Teachers,
Terms
}
alias Ecto.Changeset
import Ecto.Query, only: [from: 2]

import Canary.Plugs
plug :authorize_controller

def index(conn, _params) do
offered_courses =
conn.assigns.current_user
|> OfferedCourses.find_all_by_user()
|> Repo.preload([:term, :course])
render(conn, "index.html", offered_courses: offered_courses)
def index(%{assigns: %{current_user: current_user}} = conn, _params) do
terms = Terms.find_all_by_user(current_user)
render(conn, "index.html", terms: terms)
end

def new(conn, _params) do
Expand Down
157 changes: 82 additions & 75 deletions lib/course_planner_web/templates/class/index.html.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<div class="row middle-xs page-header">
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
Classes
Expand All @@ -8,79 +7,87 @@
</div>
</div>

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Term</th>
<th class="mdl-data-table__cell--non-numeric">Course type</th>
<th>Classroom</th>
<th>Class date</th>
<th>Starting at</th>
<th>Finishes at</th>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<th></th>
<% end %>
</tr>
</thead>
<tbody>
<%= Enum.with_index(@classes) |> Enum.map(fn {class,index} -> %>
<tr>
<td class="mdl-data-table__cell--non-numeric">
<%= class.offered_course.term.name %>
</td>
<td class="mdl-data-table__cell--non-numeric">
<%= class.offered_course.course.name %>
</td>
<td>
<%= class.classroom %>
</td>
<td>
<%= class.date %>
</td>
<td>
<%= class.date
|> Ecto.DateTime.from_date_and_time(class.starting_at)
|> Timex.format!("{h24}:{m}") %>
</td>
<td>
<%= class.date
|> Ecto.DateTime.from_date_and_time(class.finishes_at)
|> Timex.format!("{h24}:{m}") %>
</td>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<td>
<button id="tr_menu_<%= class.id %>"
class="mdl-button mdl-js-button mdl-button--icon"
>
<i class="material-icons">more_vert</i>
</button>
<ul
class="
mdl-menu mdl-js-menu
<%=
if index > 10 and index > length(@classes)-4 do
'mdl-menu--top-right'
else
'mdl-menu--bottom-right'
end
%>
"
for="tr_menu_<%= class.id %>"
>
<li class="mdl-menu__item">
<%= link "Edit", to: class_path(@conn, :edit, class) %>
</li>
<li class="mdl-menu__item">
<%= link "Delete", to: class_path(@conn, :delete, class), method: :delete,
data: [confirm: """
Are you sure?
All the attendances of that class will be removed.
"""] %>
</li>
</ul>
<%= for term <- @terms do %>
<div class="row row--hspace row--vspace middle-xs">
<div class="col-xs-9">
<div class="row middle-xs page-header">
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
<%= term.name %>
</div>
</div>
</div>
<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
<thead>
<tr>
<th>Class date</th>
<th>Starting at</th>
<th>Finishes at</th>
<th>Classroom</th>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<th></th>
<% end %>
</tr>
</thead>
<tbody>
<%= for offered_course <- term.offered_courses do %>
<td class="mdl-data-table__cell--non-numeric">
<b><%= offered_course.course.name %></b>
</td>
<%= Enum.with_index(offered_course.classes) |> Enum.map(fn {class,index} -> %>
<tr>
<td>
<%= class.date %>
</td>
<td>
<%= class.date
|> Ecto.DateTime.from_date_and_time(class.starting_at)
|> Timex.format!("{h24}:{m}") %>
</td>
<td>
<%= class.date
|> Ecto.DateTime.from_date_and_time(class.finishes_at)
|> Timex.format!("{h24}:{m}") %>
</td>
<td>
<%= class.classroom %>
</td>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<td>
<button id="tr_menu_<%= class.id %>"
class="mdl-button mdl-js-button mdl-button--icon"
>
<i class="material-icons">more_vert</i>
</button>
<ul
class="
mdl-menu mdl-js-menu
<%=
if index > 10 and index > length(@classes)-4 do
'mdl-menu--top-right'
else
'mdl-menu--bottom-right'
end
%>
"
for="tr_menu_<%= class.id %>"
>
<li class="mdl-menu__item">
<%= link "Edit", to: class_path(@conn, :edit, class) %>
</li>
<li class="mdl-menu__item">
<%= link "Delete", to: class_path(@conn, :delete, class), method: :delete,
data: [confirm: """
Are you sure?
All the attendances of that class will be removed.
"""] %>
</li>
</ul>
</td>
<% end %>
</tr>
<% end) %>
<% end %>
</tr>
<% end) %>
</tbody>
</table>
</tbody>
</table>
</div>
<% end %>
Loading

0 comments on commit 5fb7293

Please sign in to comment.