Skip to content

Commit

Permalink
✨ Permet d'importer une question pour les autres types de question
Browse files Browse the repository at this point in the history
  • Loading branch information
marouria committed Oct 30, 2024
1 parent b6ab8e8 commit 07e6601
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/admin/import_xls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

content title: 'Importer question' do
render partial: 'admin/questions/import_xls',
locals: { url: params[:url] }
locals: { type: params[:type] }
end
end
16 changes: 16 additions & 0 deletions app/admin/questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

ActiveAdmin.register Question do
menu false

controller do
def import_xls
return if params[:csv_file].blank?

import = ImportQuestion.new(params[:type])
question = import.preremplis_donnees(params[:csv_file])
## TODO gère l'erreur si la question n'a pas pu être créée
redirect_to edit_admin_question_clic_dans_image_path(question)
end
end
end
11 changes: 1 addition & 10 deletions app/admin/questions_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

action_item :importer_question, only: :index, if: -> { can? :manage, Question } do
link_to 'Importer question clic dans image',
admin_import_xls_path(url: import_xls_admin_questions_clic_dans_image_path)
admin_import_xls_path(type: 'QuestionClicDansImage')
end

show do
Expand All @@ -46,14 +46,5 @@
def set_question
@question = Question.includes(transcriptions: :audio_attachment).find(params[:id])
end

def import_xls
return if params[:csv_file].blank?

import = ImportQuestion.new('QuestionClicDansImage')

question = import.preremplis_donnees(params[:csv_file])
redirect_to edit_admin_question_clic_dans_image_path(question)
end
end
end
5 changes: 5 additions & 0 deletions app/admin/questions_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

form partial: 'form'

action_item :importer_question, only: :index, if: -> { can? :manage, Question } do
link_to 'Importer question glisser déposer',
admin_import_xls_path(type: 'QuestionGlisserDeposer')
end

index do
column :libelle do |q|
link_to q.libelle, admin_question_glisser_deposer_path(q)
Expand Down
5 changes: 5 additions & 0 deletions app/admin/questions_qcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

form partial: 'form'

action_item :importer_question, only: :index, if: -> { can? :manage, Question } do
link_to 'Importer question qcm',
admin_import_xls_path(type: 'QuestionQcm')
end

index do
column :libelle do |q|
link_to q.libelle, admin_question_qcm_path(q)
Expand Down
5 changes: 5 additions & 0 deletions app/admin/questions_saisie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
filter :libelle
filter :nom_technique

action_item :importer_question, only: :index, if: -> { can? :manage, Question } do
link_to 'Importer question saisie',
admin_import_xls_path(type: 'QuestionSaisie')
end

form partial: 'form'

index do
Expand Down
5 changes: 5 additions & 0 deletions app/admin/questions_sous_consigne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
filter :libelle
filter :nom_technique

action_item :importer_question, only: :index, if: -> { can? :manage, Question } do
link_to 'Importer question sous consigne',
admin_import_xls_path(type: 'QuestionSousConsigne')
end

form do |f|
f.semantic_errors
f.inputs do
Expand Down
24 changes: 20 additions & 4 deletions app/models/import_question.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# frozen_string_literal: true

class ImportQuestion
HEADERS_CLIC_DANS_IMAGE = %i[libelle nom_technique description intitule_ecrit intitule_audio
consigne_ecrit consigne_audio illustration zone_cliquable
image_au_clic].freeze
## TODO rajouter les réponses et choix
HEADERS_CLIC_DANS_IMAGE = %i[zone_cliquable image_au_clic].freeze
HEADERS_GLISSER_DEPOSER = %i[zone_depot].freeze
HEADERS_QCM = %i[type_qcm].freeze
HEADERS_SAISIE = %i[suffix_reponse reponse_placeholder type_saisie bonne_reponse_intitule
bonne_reponse_nom_technique].freeze
HEADERS_SOUS_CONSIGNE = %i[libelle nom_technique intitule_ecrit intitule_audio
illustration].freeze
HEADERS_COMMUN = %i[libelle nom_technique description intitule_ecrit intitule_audio
consigne_ecrit consigne_audio illustration].freeze

def initialize(type)
@type = type
Expand All @@ -15,6 +22,7 @@ def preremplis_donnees(file)
sheet = spreadsheet.worksheet(0)
return unless headers_valides?(sheet.rows[0])

## TODO gère le message d'erreur si les headers ne sont pas valides
genere_nouvelle_question(sheet.rows[1])
end

Expand All @@ -28,7 +36,15 @@ def headers_valides?(headers)
def headers_attendus
case @type
when 'QuestionClicDansImage'
HEADERS_CLIC_DANS_IMAGE
HEADERS_COMMUN + HEADERS_CLIC_DANS_IMAGE
when 'QuestionGlisserDeposer'
HEADERS_COMMUN + HEADERS_GLISSER_DEPOSER
when 'QuestionQcm'
HEADERS_COMMUN + HEADERS_QCM
when 'QuestionSaisie'
HEADERS_SAISIE + HEADERS_SAISIE
when 'SousConsigne'
HEADERS_SOUS_CONSIGNE
end
end

Expand Down
6 changes: 4 additions & 2 deletions app/views/admin/questions/_import_xls.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<%= form_with url: url, method: :post, local: true do |form| %>
<%= form_with url: import_xls_admin_questions_path, method: :post, local: true do |form| %>
<div class="field">
<%= form.label :csv_file, "CSV File" %>
<%= form.label :csv_file, "Fichier XLS" %>
<%= form.file_field :csv_file, accept: '.xls' %>
</div>

<%= form.hidden_field :type, value: type %>

<div class="actions">
<%= form.submit 'Importer le fichier' %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
resource :reponses, only: [:show], defaults: { format: 'xls' }
end
end
resources :questions_clic_dans_image do
resources :questions do
collection do
post 'import_xls'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/question_clic_dans_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
expect(page).to have_content 'Comment ça va ?'
end

it "redirige vers le formulaire d'importation de questions" do
it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question clic dans image'
end
Expand Down
7 changes: 7 additions & 0 deletions spec/features/admin/question_glisser_deposer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
it do
expect(page).to have_content 'Comment ça va ?'
end

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question glisser déposer'
end
expect(page).to have_content 'Importer question'
end
end

describe 'création' do
Expand Down
7 changes: 7 additions & 0 deletions spec/features/admin/question_qcm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
it do
expect(page).to have_content 'Comment ça va ?'
end

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question qcm'
end
expect(page).to have_content 'Importer question'
end
end

describe 'création' do
Expand Down
22 changes: 22 additions & 0 deletions spec/features/admin/question_saisie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@
describe 'Admin - Question Saisie', type: :feature do
before { se_connecter_comme_superadmin }

describe 'index' do
let!(:question) do
create :question_saisie
end
let!(:transcription) do
create :transcription, question_id: question.id, ecrit: 'Comment ça va ?'
end

before { visit admin_questions_saisie_path }

it do
expect(page).to have_content 'Comment ça va ?'
end

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question saisie'
end
expect(page).to have_content 'Importer question'
end
end

describe 'création' do
before do
visit new_admin_question_saisie_path
Expand Down
7 changes: 7 additions & 0 deletions spec/features/admin/question_sous_consigne_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@
it do
expect(page).to have_content 'Intitulé sous consigne'
end

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question sous consigne'
end
expect(page).to have_content 'Importer question'
end
end
end

0 comments on commit 07e6601

Please sign in to comment.