Initial commit
This commit is contained in:
124
lib/confient_web/controllers/assignment_controller.ex
Normal file
124
lib/confient_web/controllers/assignment_controller.ex
Normal file
@@ -0,0 +1,124 @@
|
||||
defmodule ConfientWeb.AssignmentController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
require Logger
|
||||
|
||||
import Phoenix.HTML
|
||||
|
||||
alias Confient.Works
|
||||
alias Confient.Works.Assignment
|
||||
|
||||
def index(conn, _params) do
|
||||
assignments = Works.list_assignments()
|
||||
render(conn, "index.html", assignments: assignments)
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = Works.change_assignment(%Assignment{})
|
||||
classes = Confient.School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
|
||||
render(conn, "new.html", changeset: changeset, classes: classes)
|
||||
end
|
||||
|
||||
def create(conn, %{"assignment" => assignment_params}) do
|
||||
IO.inspect(assignment_params)
|
||||
|
||||
case Works.create_assignment(assignment_params) do
|
||||
{:ok, assignment} ->
|
||||
conn
|
||||
|> put_flash(:info, "Devoir créé avec succès.")
|
||||
|> redirect(to: Routes.assignment_path(conn, :show, assignment))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
classes = Confient.School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
render(conn, "new.html", changeset: changeset, classes: classes)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
assignment = Works.get_full_assignment!(id)
|
||||
missing = Works.get_missing_works_from_student_in_assignement(id, assignment.class_id)
|
||||
render(conn, "show.html", assignment: assignment, missing: missing)
|
||||
end
|
||||
|
||||
def edit(conn, %{"id" => id}) do
|
||||
classes = Confient.School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
assignment = Works.get_assignment!(id)
|
||||
changeset = Works.change_assignment(assignment)
|
||||
render(conn, "edit.html", assignment: assignment, changeset: changeset, classes: classes)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "assignment" => assignment_params}) do
|
||||
assignment = Works.get_assignment!(id)
|
||||
|
||||
case Works.update_assignment(assignment, assignment_params) do
|
||||
{:ok, assignment} ->
|
||||
conn
|
||||
|> put_flash(:info, "Devoir mis à jour avec succès.")
|
||||
|> redirect(to: Routes.assignment_path(conn, :show, assignment))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "edit.html", assignment: assignment, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
assignment = Works.get_assignment!(id)
|
||||
{:ok, _assignment} = Works.delete_assignment(assignment)
|
||||
|
||||
File.rm_rf(Confient.Deposit.get_upload_dir(assignment.class.name, assignment.slug))
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Devoir supprimé avec succès.")
|
||||
|> redirect(to: Routes.assignment_path(conn, :index))
|
||||
end
|
||||
|
||||
def archive(conn, %{"id" => assignment_id}) do
|
||||
case Works.get_full_assignment!(assignment_id) do
|
||||
nil ->
|
||||
conn |> put_flash(:error, "Devoir inexistant") |> redirect(to: "/")
|
||||
|
||||
assignment ->
|
||||
base = Application.get_env(:confiant, :base_upload_dir, "/tmp/confient/uploads")
|
||||
|
||||
files =
|
||||
Enum.map(assignment.students_works, fn v -> v.path end)
|
||||
|> Enum.map(&String.to_charlist/1)
|
||||
|
||||
archive_name = "#{assignment.class.name}_#{assignment.slug}.zip"
|
||||
archive_sub = "#{assignment.class.name}/#{assignment.slug}"
|
||||
archive_path = "#{base}/#{archive_sub}/#{archive_name}"
|
||||
|
||||
case :zip.create(
|
||||
String.to_charlist(archive_path),
|
||||
files,
|
||||
[{:cwd, "#{base}/#{archive_sub}"}]
|
||||
) do
|
||||
{:ok, _fname} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:info,
|
||||
raw(
|
||||
"Archive créé est disponible <a href=#{
|
||||
Path.join([
|
||||
Application.fetch_env!(:confient, :domain),
|
||||
"uploads",
|
||||
assignment.class.name,
|
||||
assignment.slug,
|
||||
archive_name
|
||||
])
|
||||
}>#{archive_name}</href>"
|
||||
)
|
||||
)
|
||||
|> redirect(to: Routes.assignment_path(conn, :show, assignment))
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error(reason)
|
||||
|
||||
conn
|
||||
|> put_flash(:error, "Error lors de la création de l'archive")
|
||||
|> redirect(to: Routes.assignment_path(conn, :show, assignment))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
65
lib/confient_web/controllers/class_controller.ex
Normal file
65
lib/confient_web/controllers/class_controller.ex
Normal file
@@ -0,0 +1,65 @@
|
||||
defmodule ConfientWeb.ClassController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
alias Confient.School
|
||||
alias Confient.School.Class
|
||||
|
||||
def index(conn, _params) do
|
||||
classes = School.list_classes()
|
||||
render(conn, "index.html", classes: classes)
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = School.change_class(%Class{})
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
|
||||
def create(conn, %{"class" => class_params}) do
|
||||
case School.create_class(class_params) do
|
||||
{:ok, class} ->
|
||||
conn
|
||||
|> put_flash(:info, "Classe créé avec succès.")
|
||||
|> redirect(to: Routes.class_path(conn, :show, class))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
class = School.get_class!(id)
|
||||
render(conn, "show.html", class: class)
|
||||
end
|
||||
|
||||
def edit(conn, %{"id" => id}) do
|
||||
class = School.get_class!(id)
|
||||
changeset = School.change_class(class)
|
||||
render(conn, "edit.html", class: class, changeset: changeset)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "class" => class_params}) do
|
||||
class = School.get_class!(id)
|
||||
|
||||
case School.update_class(class, class_params) do
|
||||
{:ok, class} ->
|
||||
conn
|
||||
|> put_flash(:info, "Classe mise à jour avec succès.")
|
||||
|> redirect(to: Routes.class_path(conn, :show, class))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "edit.html", class: class, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
class = School.get_class!(id)
|
||||
{:ok, _class} = School.delete_class(class)
|
||||
|
||||
dir = Application.fetch_env!(:confient, :upload_dir)
|
||||
File.rm_rf("#{dir}/#{class.name}")
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Classe supprimée.")
|
||||
|> redirect(to: Routes.class_path(conn, :index))
|
||||
end
|
||||
end
|
||||
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
|
||||
9
lib/confient_web/controllers/error_controller.ex
Normal file
9
lib/confient_web/controllers/error_controller.ex
Normal file
@@ -0,0 +1,9 @@
|
||||
defmodule ConfientWeb.ErrorController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
def notfound(conn, _params) do
|
||||
conn
|
||||
|> put_flash(:error, "Fichier inexistant")
|
||||
|> redirect(to: "/")
|
||||
end
|
||||
end
|
||||
10
lib/confient_web/controllers/page_controller.ex
Normal file
10
lib/confient_web/controllers/page_controller.ex
Normal file
@@ -0,0 +1,10 @@
|
||||
defmodule ConfientWeb.PageController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
alias Confient.School
|
||||
|
||||
def index(conn, _params) do
|
||||
classes = School.list_classes()
|
||||
render(conn, "index.html", classes: classes)
|
||||
end
|
||||
end
|
||||
33
lib/confient_web/controllers/session_controller.ex
Normal file
33
lib/confient_web/controllers/session_controller.ex
Normal file
@@ -0,0 +1,33 @@
|
||||
defmodule ConfientWeb.SessionController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
alias Confient.Account.Auth
|
||||
import Phoenix.HTML
|
||||
|
||||
def new(conn, _params) do
|
||||
render(conn, "new.html")
|
||||
end
|
||||
|
||||
def create(conn, %{"session" => auth}) do
|
||||
case Auth.login(auth) do
|
||||
{:ok, user} ->
|
||||
conn
|
||||
|> put_session(:current_user_id, user.id)
|
||||
|> put_flash(:info, raw("Utilisateur <strong>#{user.username}</strong> connecté."))
|
||||
|> redirect(to: "/")
|
||||
|
||||
:error ->
|
||||
conn
|
||||
|> put_flash(:error, "Il y a un problème avec la combinaison utilisateur/mot de passe")
|
||||
|> redirect(to: Routes.session_path(conn, :new))
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, _params) do
|
||||
conn
|
||||
|> delete_session(:current_user_id)
|
||||
|> delete_session(:current_user)
|
||||
|> put_flash(:info, "Déconnecté.")
|
||||
|> redirect(to: "/")
|
||||
end
|
||||
end
|
||||
99
lib/confient_web/controllers/student_controller.ex
Normal file
99
lib/confient_web/controllers/student_controller.ex
Normal file
@@ -0,0 +1,99 @@
|
||||
defmodule ConfientWeb.StudentController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
alias Confient.School
|
||||
alias Confient.School.Student
|
||||
|
||||
def index(conn, _params) do
|
||||
students = School.list_students()
|
||||
render(conn, "index.html", students: students)
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = School.change_student(%Student{})
|
||||
classes = School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
|
||||
render(conn, "new.html",
|
||||
changeset: changeset,
|
||||
classes: classes
|
||||
)
|
||||
end
|
||||
|
||||
def bulkform(conn, _params) do
|
||||
classes = School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
|
||||
render(conn, "bulkform.html",
|
||||
classes: classes,
|
||||
token: get_csrf_token()
|
||||
)
|
||||
end
|
||||
|
||||
def create(conn, %{"student" => student_params}) do
|
||||
case School.create_student(student_params) do
|
||||
{:ok, student} ->
|
||||
conn
|
||||
|> put_flash(:info, "Elève créé avec succès.")
|
||||
|> redirect(to: Routes.student_path(conn, :show, student))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
classes = Confient.School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
render(conn, "new.html", changeset: changeset, classes: classes)
|
||||
end
|
||||
end
|
||||
|
||||
def bulkcreate(conn, %{"file" => %{"list" => ""}}) do
|
||||
conn
|
||||
|> put_flash(:error, "Aucun fichier CSV.")
|
||||
|> redirect(to: Routes.student_path(conn, :bulkform))
|
||||
end
|
||||
|
||||
def bulkcreate(conn, %{"file" => %{"list" => file}, "class" => %{"class_id" => class_id}}) do
|
||||
class = School.get_class!(class_id)
|
||||
{:ok, students} = CSV.parse(file.path)
|
||||
|
||||
for {lastname, firstname} <- students do
|
||||
unless School.student_exists?(lastname, firstname, class) do
|
||||
School.create_student(class, %{"lastname" => lastname, "firstname" => firstname})
|
||||
end
|
||||
end
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Elèves créés avec succès.")
|
||||
|> redirect(to: Routes.student_path(conn, :bulkform))
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
student = School.get_full_student!(id)
|
||||
render(conn, "show.html", student: student)
|
||||
end
|
||||
|
||||
def edit(conn, %{"id" => id}) do
|
||||
student = School.get_student!(id)
|
||||
changeset = School.change_student(student)
|
||||
classes = School.list_classes() |> Enum.map(&{&1.name, &1.id})
|
||||
render(conn, "edit.html", student: student, changeset: changeset, classes: classes)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "student" => student_params}) do
|
||||
student = School.get_student!(id)
|
||||
|
||||
case School.update_student(student, student_params) do
|
||||
{:ok, student} ->
|
||||
conn
|
||||
|> put_flash(:info, "Elève mis à jour avec succès.")
|
||||
|> redirect(to: Routes.student_path(conn, :show, student))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "edit.html", student: student, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
student = School.get_student!(id)
|
||||
{:ok, _student} = School.delete_student(student)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Elève supprimé.")
|
||||
|> redirect(to: Routes.student_path(conn, :index))
|
||||
end
|
||||
end
|
||||
6
lib/confient_web/controllers/user_controller.ex
Normal file
6
lib/confient_web/controllers/user_controller.ex
Normal file
@@ -0,0 +1,6 @@
|
||||
defmodule ConfientWeb.UserController do
|
||||
use ConfientWeb, :controller
|
||||
|
||||
# alias Confient.Account
|
||||
# alias Confient.Account.User
|
||||
end
|
||||
Reference in New Issue
Block a user