2019-08-06 11:02:54 +02:00
|
|
|
class Admin::InstructeursController < AdminController
|
2016-02-05 17:40:58 +01:00
|
|
|
include SmartListing::Helper::ControllerExtensions
|
|
|
|
helper SmartListing::Helper
|
|
|
|
|
|
|
|
def index
|
2019-08-06 11:02:54 +02:00
|
|
|
@instructeurs = smart_listing_create :instructeurs,
|
|
|
|
current_administrateur.instructeurs,
|
|
|
|
partial: "admin/instructeurs/list",
|
2017-06-12 13:49:51 +02:00
|
|
|
array: true
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-08-06 11:02:54 +02:00
|
|
|
email = params[:instructeur][:email].downcase
|
2019-10-15 17:44:59 +02:00
|
|
|
@instructeur = Instructeur.by_email(email)
|
2016-05-26 16:44:10 +02:00
|
|
|
procedure_id = params[:procedure_id]
|
2020-06-12 18:16:07 +02:00
|
|
|
procedure = Procedure.find_by(id: procedure_id)
|
2016-05-20 15:39:17 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
if @instructeur.nil?
|
2019-11-04 18:32:56 +01:00
|
|
|
invite_instructeur(email)
|
2016-02-05 17:40:58 +01:00
|
|
|
else
|
2019-08-06 11:02:54 +02:00
|
|
|
assign_instructeur!
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-11-15 05:19:30 +01:00
|
|
|
if procedure_id.present?
|
2020-06-12 18:16:07 +02:00
|
|
|
redirect_to procedure_groupe_instructeur_path(procedure, procedure.defaut_groupe_instructeur)
|
2016-10-18 15:49:46 +02:00
|
|
|
else
|
2019-08-06 11:02:54 +02:00
|
|
|
redirect_to admin_instructeurs_path
|
2016-10-18 15:49:46 +02:00
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-02-09 12:23:49 +01:00
|
|
|
def destroy
|
2019-08-06 11:02:54 +02:00
|
|
|
Instructeur.find(params[:id]).administrateurs.delete current_administrateur
|
|
|
|
redirect_to admin_instructeurs_path
|
2016-02-09 12:23:49 +01:00
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
private
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def invite_instructeur(email)
|
2019-08-14 16:32:22 +02:00
|
|
|
user = User.create_or_promote_to_instructeur(
|
|
|
|
email,
|
|
|
|
SecureRandom.hex,
|
|
|
|
administrateurs: [current_administrateur]
|
|
|
|
)
|
2018-04-27 12:29:01 +02:00
|
|
|
|
2019-08-14 16:32:22 +02:00
|
|
|
if user.valid?
|
2019-08-07 15:52:38 +02:00
|
|
|
user.invite!
|
2019-08-14 16:32:22 +02:00
|
|
|
|
2018-08-29 22:11:38 +02:00
|
|
|
flash.notice = 'Instructeur ajouté'
|
2016-05-24 16:39:39 +02:00
|
|
|
else
|
2019-08-07 15:52:38 +02:00
|
|
|
flash.alert = user.errors.full_messages
|
2016-05-24 16:39:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def assign_instructeur!
|
|
|
|
if current_administrateur.instructeurs.include?(@instructeur)
|
2018-08-29 22:11:38 +02:00
|
|
|
flash.alert = 'Instructeur déjà ajouté'
|
2016-05-24 16:39:39 +02:00
|
|
|
else
|
2019-08-06 11:02:54 +02:00
|
|
|
@instructeur.administrateurs.push current_administrateur
|
2018-08-29 22:11:38 +02:00
|
|
|
flash.notice = 'Instructeur ajouté'
|
2018-01-15 19:14:09 +01:00
|
|
|
# TODO Mailer no assign_to
|
2016-05-24 16:39:39 +02:00
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
2016-10-18 15:49:46 +02:00
|
|
|
end
|