Initial commit
This commit is contained in:
166
lib/confient_web/controllers/deposit_controller.ex
Normal file
166
lib/confient_web/controllers/deposit_controller.ex
Normal file
@@ -0,0 +1,166 @@
|
||||
defmodule ConfientWeb.DepositController do
|
||||
use ConfientWeb, :controller
|
||||
import Phoenix.HTML
|
||||
|
||||
def index(conn, _params) do
|
||||
works = Confient.Deposit.list_works()
|
||||
render(conn, "index.html", works: works)
|
||||
end
|
||||
|
||||
def form(conn, params = %{"id" => id}) do
|
||||
case Confient.School.get_class(id) do
|
||||
{:error, :no_class} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"La classe demandée n'existe pas."
|
||||
)
|
||||
|> redirect(to: "/")
|
||||
|
||||
{:ok, class} ->
|
||||
next_assignments = Confient.Works.get_next_assignments(class)
|
||||
|
||||
if length(next_assignments) > 0 && length(class.students) do
|
||||
changeset = Confient.Student.Work.changeset(%Confient.Student.Work{}, %{})
|
||||
|
||||
student =
|
||||
with {:ok, id} <- Map.fetch(params, "student_id"),
|
||||
{val, _} <- Integer.parse(id) do
|
||||
Enum.find(class.students, Enum.at(class.students, 0), &(&1.id == val))
|
||||
else
|
||||
:error -> Enum.at(class.students, 0)
|
||||
end
|
||||
|
||||
assignment =
|
||||
with {:ok, id} <- Map.fetch(params, "assignment_id"),
|
||||
{val, _} <- Integer.parse(id) do
|
||||
Enum.find(next_assignments, Enum.at(next_assignments, 0), &(&1.id == val))
|
||||
else
|
||||
:error -> Enum.at(next_assignments, 0)
|
||||
end
|
||||
|
||||
render(conn, "form.html",
|
||||
assignment_id: assignment.id,
|
||||
student_id: student.id,
|
||||
changeset: changeset,
|
||||
class: class,
|
||||
assignments: next_assignments
|
||||
)
|
||||
else
|
||||
conn
|
||||
|> put_flash(
|
||||
:info,
|
||||
"Aucun devoir à rendre pour la classe #{class.name}"
|
||||
)
|
||||
|> redirect(to: "/")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deposit(conn, %{
|
||||
"id" => id,
|
||||
"work" => %{"assignment_id" => assignment_id, "student_id" => student_id, "file" => work}
|
||||
}) do
|
||||
with {:ok, class} <- Confient.School.get_class(id),
|
||||
{:ok, student} <- Confient.School.get_student(student_id),
|
||||
{:ok, assignment} <- Confient.Works.get_assignment(assignment_id),
|
||||
:ok <- Confient.Deposit.verify_date(assignment.due),
|
||||
{:ok, ext} <- Confient.Deposit.verify_file(work) do
|
||||
dir =
|
||||
Confient.Deposit.get_upload_dir(class.name, assignment.slug)
|
||||
|> Confient.Deposit.ensure_upload_dir()
|
||||
|
||||
fname = Confient.Deposit.gen_filename(class.name, assignment.slug, student, ext)
|
||||
|
||||
case File.copy(work.path, "#{dir}/#{fname}") do
|
||||
{:ok, _} ->
|
||||
Confient.Works.create_or_update_work(%{
|
||||
path: fname,
|
||||
student_id: student.id,
|
||||
assignment_id: assignment.id
|
||||
})
|
||||
|
||||
conn
|
||||
|> put_flash(
|
||||
:info,
|
||||
raw("Fichier <strong>#{work.filename}</strong> déposé avec succès<br>
|
||||
Elève : #{student.lastname} #{student.firstname}<br>
|
||||
Devoir : #{assignment.title}<br>
|
||||
Classe : #{class.name}")
|
||||
)
|
||||
|> redirect(to: "/")
|
||||
|
||||
{:error, _} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Erreur lors du téléversement du fichier."
|
||||
)
|
||||
|> redirect(
|
||||
to:
|
||||
Routes.deposit_path(conn, :form, id,
|
||||
student_id: student.id,
|
||||
assignment_id: assignment.id
|
||||
)
|
||||
)
|
||||
end
|
||||
else
|
||||
{:error, :date_too_late} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"La date de rendue pour le devoir sélectionné est dépassée."
|
||||
)
|
||||
|> redirect(to: Routes.deposit_path(conn, :form, id))
|
||||
|
||||
{:error, :no_class} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"La classe demandé n'existe pas."
|
||||
)
|
||||
|> redirect(to: "/")
|
||||
|
||||
{:error, :no_student} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"L'étudiant demandé n'existe pas."
|
||||
)
|
||||
|> redirect(to: "/")
|
||||
|
||||
{:error, :no_assignment} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Le devoir demandé n'existe pas."
|
||||
)
|
||||
|> redirect(to: "/")
|
||||
|
||||
{:error, :invalid_file_type} ->
|
||||
{:ok, student} = Confient.School.get_student(student_id)
|
||||
{:ok, assignment} = Confient.Works.get_assignment(assignment_id)
|
||||
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
raw("Format de fichier invalide pour le fichier <strong>#{work.filename}</strong><br>
|
||||
Elève : #{student.lastname} #{student.firstname}<br>
|
||||
Devoir : #{assignment.title}")
|
||||
)
|
||||
|> redirect(
|
||||
to:
|
||||
Routes.deposit_path(conn, :form, id,
|
||||
student_id: student.id,
|
||||
assignment_id: assignment.id
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def deposit(conn, %{"id" => id}),
|
||||
do:
|
||||
conn
|
||||
|> put_flash(:error, "Aucun fichier spécifié")
|
||||
|> redirect(to: Routes.deposit_path(conn, :form, id))
|
||||
end
|
||||
Reference in New Issue
Block a user