Skip to content

Commit

Permalink
Merge 5e13dae into f916fb9
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnonose committed Nov 2, 2017
2 parents f916fb9 + 5e13dae commit 3eb308a
Show file tree
Hide file tree
Showing 52 changed files with 1,568 additions and 209 deletions.
33 changes: 33 additions & 0 deletions lib/course_planner/accounts/supervisors.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule CoursePlanner.Accounts.Supervisors do
@moduledoc false
import Ecto.Query
alias CoursePlanner.{Repo, Accounts.User, Accounts.Users}

@supervisors from u in User, where: u.role == "Supervisor",
order_by: [u.name, u.family_name, u.nickname]

def all do
Repo.all(@supervisors)
end

def new(user, token) do
user
|> Map.put("role", "Supervisor")
|> Users.new_user(token)
end

def update(id, params) do
case Repo.get(User, id) do
nil -> {:error, :not_found}
supervisor ->
supervisor
|> User.changeset(params, :update)
|> Repo.update
|> format_error(supervisor)
end
end

defp format_error({:ok, supervisor}, _), do: {:ok, supervisor}
defp format_error({:error, changeset}, supervisor), do: {:error, supervisor, changeset}

end
1 change: 0 additions & 1 deletion lib/course_planner/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ defmodule CoursePlanner.Accounts.User do
def changeset(model, params, :seed) do
model
|> changeset(params)
|> validate_current_password()
|> validate_password_if_changed()
end

Expand Down
1 change: 1 addition & 0 deletions lib/course_planner/events/calendars.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule CoursePlanner.Events.Calendars do
def get_user_events(user, true, week_range) do
case user.role do
"Coordinator" -> get_all_events(week_range)
"Supervisor" -> get_all_events(week_range)
_ -> get_user_events(user.id, week_range)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/course_planner/terms/terms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ defmodule CoursePlanner.Terms do
students_and_teachers ++ Coordinators.all()
end

def find_all_by_user(%{role: "Coordinator"}) do
def find_all_by_user(%{role: role}) when role in ["Coordinator", "Supervisor"] do
Repo.all(from t in Term,
join: oc in assoc(t, :offered_courses),
join: co in assoc(oc, :course),
Expand Down
2 changes: 1 addition & 1 deletion lib/course_planner/types/user_role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ defmodule CoursePlanner.Types.UserRole do
use CoursePlanner.Types.Enum

def type, do: :user_role
def valid_types, do: ["Student", "Teacher", "Coordinator", "Volunteer"]
def valid_types, do: ["Student", "Teacher", "Coordinator", "Volunteer", "Supervisor"]
end
13 changes: 11 additions & 2 deletions lib/course_planner_web/controllers/attendance_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ defmodule CoursePlannerWeb.AttendanceController do
plug :authorize_controller

def index(%{assigns: %{current_user: %{id: _id, role: "Coordinator"}}} = conn, _params) do

offered_courses = Attendances.get_all_offered_courses()

render(conn, "index_coordinator.html", offered_courses: offered_courses)
end

def index(%{assigns: %{current_user: %{id: _id, role: "Supervisor"}}} = conn, _params) do

offered_courses = Attendances.get_all_offered_courses()

render(conn, "index_supervisor.html", offered_courses: offered_courses)
end

def index(%{assigns: %{current_user: %{id: id, role: "Teacher"}}} = conn, _params) do
offered_courses = Attendances.get_all_teacher_offered_courses(id)

Expand All @@ -25,8 +33,9 @@ defmodule CoursePlannerWeb.AttendanceController do
render(conn, "index_student.html", offered_courses: offered_courses)
end

def show(%{assigns: %{current_user: %{id: _id, role: "Coordinator"}}} = conn,
%{"id" => offered_course_id}) do
def show(%{assigns: %{current_user: %{id: _id, role: role}}} = conn,
%{"id" => offered_course_id}) when role in ["Coordinator", "Supervisor"] do

case Attendances.get_course_attendances(offered_course_id) do
nil ->
conn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule CoursePlannerWeb.OfferedCourseController do
end

def show(%{assigns: %{current_user: %{role: user_role}}} = conn, %{"id" => id})
when user_role in ["Coordinator", "Teacher"] do
when user_role in ["Coordinator", "Supervisor", "Teacher"] do
offered_course =
OfferedCourse
|> Repo.get!(id)
Expand Down
3 changes: 3 additions & 0 deletions lib/course_planner_web/controllers/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ defimpl Canada.Can, for: CoursePlanner.Accounts.User do

def can?(%User{role: "Coordinator"}, _action, _controller), do: true

def can?(%User{role: "Supervisor"}, action, _controller)
when action in [:index, :show], do: true

def can?(%User{role: "Teacher"}, _action, AttendanceController), do: true
def can?(%User{role: "Teacher"}, action, OfferedCourseController)
when action in [:index, :show, :edit, :update], do: true
Expand Down
4 changes: 3 additions & 1 deletion lib/course_planner_web/controllers/setting_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule CoursePlannerWeb.SettingController do
import Canary.Plugs
plug :authorize_controller

def show(%{assigns: %{current_user: %{role: "Coordinator"}}} = conn, _param) do
def show(%{assigns: %{current_user: %{role: role}}} = conn, _param)
when role in ["Coordinator", "Supervisor"] do

visible_system_variables = Settings.get_visible_systemvariables()
program_system_variables =
Settings.filter_program_systemvariables(visible_system_variables)
Expand Down
83 changes: 83 additions & 0 deletions lib/course_planner_web/controllers/supervisor_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule CoursePlannerWeb.SupervisorController do
@moduledoc false
use CoursePlannerWeb, :controller
alias CoursePlanner.{Accounts.Users, Accounts.User,
Accounts.Supervisors,
Auth.Helper}
alias CoursePlannerWeb.{Router.Helpers, Auth.UserEmail}

import Canary.Plugs
plug :authorize_resource, model: User

def index(conn, _params) do
render(conn, "index.html", supervisors: Supervisors.all())
end

def new(conn, _params) do
changeset = User.changeset(%User{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"user" => user}) do
token = Helper.get_random_token_with_length 48
url = Helpers.password_url(conn, :edit, token)
case Supervisors.new(user, token) do
{:ok, supervisor} ->
UserEmail.send_user_email(:welcome, supervisor, url)
conn
|> put_flash(:info, "Supervisor created and notified by.")
|> redirect(to: supervisor_path(conn, :index))
{:error, changeset} ->
conn
|> put_flash(:error, "Something went wrong.")
|> render("new.html", changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
supervisor = Repo.get!(User, id)
render(conn, "show.html", supervisor: supervisor)
end

def edit(conn, %{"id" => id}) do
supervisor = Repo.get!(User, id)
changeset = User.changeset(supervisor)
render(conn, "edit.html", supervisor: supervisor, changeset: changeset)
end

def update(%{assigns: %{current_user: current_user}} = conn, %{"id" => id, "user" => params}) do
case Supervisors.update(id, params) do
{:ok, supervisor} ->
Users.notify_user(supervisor,
current_user,
:user_modified,
supervisor_url(conn, :show, supervisor))
conn
|> put_flash(:info, "Supervisor updated successfully.")
|> redirect(to: supervisor_path(conn, :show, supervisor))
{:error, :not_found} ->
conn
|> put_status(404)
|> render(CoursePlannerWeb.ErrorView, "404.html")
{:error, supervisor, changeset} ->
render(conn, "edit.html", supervisor: supervisor, changeset: changeset)
end
end

def delete(%{assigns: %{current_user: %User{id: current_user_id}}} = conn, %{"id" => id}) do
case Users.delete(id, current_user_id) do
{:ok, _supervisor} ->
conn
|> put_flash(:info, "Supervisor deleted successfully.")
|> redirect(to: supervisor_path(conn, :index))
{:error, :not_found} ->
conn
|> put_flash(:error, "Supervisor was not found.")
|> redirect(to: supervisor_path(conn, :index))
{:error, _changeset} ->
conn
|> put_flash(:error, "Something went wrong.")
|> redirect(to: supervisor_path(conn, :index))
end
end
end
1 change: 1 addition & 0 deletions lib/course_planner_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ defmodule CoursePlannerWeb.Router do
resources "/bulk", BulkController, only: [:new, :create], singleton: true

resources "/coordinators", CoordinatorController
resources "/supervisors", SupervisorController
resources "/students", StudentController
resources "/teachers", TeacherController
resources "/volunteers", VolunteerController
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

<div class="row middle-xs page-header">
<div class="col-xs-12 page-title">
Attendances by courses
</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 class="mdl-data-table__cell--non-numeric">Teachers</th>
<th></th>
</tr>
</thead>
<tbody>
<%= Enum.with_index(@offered_courses) |> Enum.map(fn {offered_course,index} -> %>
<tr>
<td class="mdl-data-table__cell--non-numeric">
<%= offered_course.term.name %>
</td>
<td class="mdl-data-table__cell--non-numeric">
<%= offered_course.course.name %>
</td>
<td class="mdl-data-table__cell--non-numeric">
<%= get_teacher_display_name(offered_course.teachers) %>
</td>
<td>
<button id="tr_menu_<%= offered_course.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(@offered_courses)-4 do
'mdl-menu--top-right'
else
'mdl-menu--bottom-right'
end
%>
"
for="tr_menu_<%= offered_course.id %>"
>
<li class="mdl-menu__item">
<%= link "Show", to: attendance_path(@conn, :show, offered_course) %>
</li>
</ul>
</td>
</tr>
<% end) %>
</tbody>
</table>
8 changes: 5 additions & 3 deletions lib/course_planner_web/templates/class/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
Classes
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New class", to: class_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New class", to: class_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<% end %>
</div>

<%= for term <- @terms do %>
Expand Down
8 changes: 5 additions & 3 deletions lib/course_planner_web/templates/coordinator/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
Coordinators
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New coordinator", to: coordinator_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New coordinator", to: coordinator_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<% end %>
</div>

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
Expand Down
8 changes: 5 additions & 3 deletions lib/course_planner_web/templates/course/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
Course types
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New course type", to: course_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New course type", to: course_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<% end %>
</div>

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<%= CoursePlannerWeb.SharedView.navbar_separator %>
<%= CoursePlannerWeb.SharedView.navbar_item "Coordinators", @conn, coordinator_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Students", @conn, student_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Supervisors", @conn, supervisor_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Teachers", @conn, teacher_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Volunteers", @conn, volunteer_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "All users", @conn, user_path(@conn, :index) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%= CoursePlannerWeb.SharedView.navbar "Course Planner" do %>
<%= CoursePlannerWeb.SharedView.navbar_item "Dashboard", @conn, dashboard_path(@conn, :show) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Calendar", @conn, schedule_path(@conn, :show) %>
<%= CoursePlannerWeb.SharedView.navbar_separator %>
<%= CoursePlannerWeb.SharedView.navbar_item "Coordinators", @conn, coordinator_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Students", @conn, student_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Supervisors", @conn, supervisor_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Teachers", @conn, teacher_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Volunteers", @conn, volunteer_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "All users", @conn, user_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_separator %>
<%= CoursePlannerWeb.SharedView.navbar_item "Course types", @conn, course_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Terms", @conn, term_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_separator %>
<%= CoursePlannerWeb.SharedView.navbar_item "Courses", @conn, offered_course_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Classes", @conn, class_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Attendances", @conn, attendance_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Tasks", @conn, task_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_item "Events", @conn, event_path(@conn, :index) %>
<%= CoursePlannerWeb.SharedView.navbar_separator %>
<%= CoursePlannerWeb.SharedView.navbar_item "Settings", @conn, setting_path(@conn, :show) %>
<%= if show_program_about?(), do: CoursePlannerWeb.SharedView.navbar_item "About #{get_program_name()}", @conn, about_path(@conn, :show) %>
<% end %>
16 changes: 10 additions & 6 deletions lib/course_planner_web/templates/setting/show.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
System Settings
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "Edit", to: setting_path(@conn, :edit, setting_type: "system"), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "Edit", to: setting_path(@conn, :edit, setting_type: "system"), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<% end %>
</div>

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
Expand Down Expand Up @@ -32,9 +34,11 @@
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
Program Settings
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "Edit", to: setting_path(@conn, :edit, setting_type: "program"), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "Edit", to: setting_path(@conn, :edit, setting_type: "program"), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<% end %>
</div>

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
Expand Down
8 changes: 5 additions & 3 deletions lib/course_planner_web/templates/student/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
<div class="col-xs-6 col-sm-9 col-md-10 page-title">
Students
</div>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New student", to: student_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<%= if @conn.assigns.current_user.role == "Coordinator" do %>
<div class="col-xs-6 col-sm-3 col-md-2">
<%= link "New student", to: student_path(@conn, :new), class: "mdl-button mdl-js-button mdl-button--raised" %>
</div>
<% end %>
</div>

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp page">
Expand Down
Loading

0 comments on commit 3eb308a

Please sign in to comment.