Skip to content

Commit

Permalink
♻️ Remplace les chaînes de caractères par des constantes de type pour…
Browse files Browse the repository at this point in the history
… les questions dans l'administration et l'import/export
  • Loading branch information
Guitguitou committed Nov 15, 2024
1 parent 1299794 commit baea81b
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 40 deletions.
12 changes: 6 additions & 6 deletions app/admin/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def erreur_import(error)

def redirection_apres_import
redirection_paths = {
'QuestionClicDansImage' => admin_questions_clic_dans_image_path,
'QuestionGlisserDeposer' => admin_questions_glisser_deposer_path,
'QuestionQcm' => admin_question_qcms_path,
'QuestionSaisie' => admin_questions_saisies_path,
'QuestionSousConsigne' => admin_question_sous_consignes_path,
'QuestionClicDansTexte' => admin_questions_clic_dans_texte_path
QuestionClicDansImage.QUESTION_TYPE => admin_questions_clic_dans_image_path,
QuestionGlisserDeposer.QUESTION_TYPE => admin_questions_glisser_deposer_path,
QuestionQcm.QUESTION_TYPE => admin_question_qcms_path,
QuestionSaisie.QUESTION_TYPE => admin_questions_saisies_path,
QuestionSousConsigne.QUESTION_TYPE => admin_question_sous_consignes_path,
QuestionClicDansTexte.QUESTION_TYPE => admin_questions_clic_dans_texte_path
}

redirection_paths[params[:type]] || admin_questions_path
Expand Down
2 changes: 1 addition & 1 deletion 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 do
link_to 'Importer questions clic dans image',
admin_import_xls_path(type: 'QuestionClicDansImage')
admin_import_xls_path(type: QuestionClicDansImage.QUESTION_TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_clic_dans_texte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

action_item :importer_question, only: :index do
link_to 'Importer questions Clic dans texte',
admin_import_xls_path(type: 'QuestionClicDansTexte')
admin_import_xls_path(type: QuestionClicDansTexte.QUESTION_TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

action_item :importer_question, only: :index do
link_to 'Importer questions glisser déposer',
admin_import_xls_path(type: 'QuestionGlisserDeposer')
admin_import_xls_path(type: QuestionGlisserDeposer.QUESTION_TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_qcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
form partial: 'form'

action_item :importer_question, only: :index do
link_to 'Importer questions QCM', admin_import_xls_path(type: 'QuestionQcm')
link_to 'Importer questions QCM', admin_import_xls_path(type: QuestionQcm.QUESTION_TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/questions_saisie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
filter :nom_technique

action_item :importer_question, only: :index do
link_to 'Importer questions saisies', admin_import_xls_path(type: 'QuestionSaisie')
link_to 'Importer questions saisies', admin_import_xls_path(type: QuestionSaisie.QUESTION_TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
3 changes: 2 additions & 1 deletion app/admin/questions_sous_consigne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
filter :nom_technique

action_item :importer_question, only: :index do
link_to 'Importer questions sous consigne', admin_import_xls_path(type: 'QuestionSousConsigne')
link_to 'Importer questions sous consigne',
admin_import_xls_path(type: QuestionSousConsigne.QUESTION_TYPE)
end

action_item :exporter_question, only: :show do
Expand Down
10 changes: 5 additions & 5 deletions app/models/import_export/questions/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def remplis_champs_additionnels

def remplis_champs_specifiques
case @question.type
when 'QuestionClicDansImage' then remplis_champs_clic_dans_image
when 'QuestionGlisserDeposer' then remplis_champs_glisser_deposer
when 'QuestionQcm' then remplis_champs_qcm
when 'QuestionSaisie' then remplis_champs_saisie
when 'QuestionClicDansTexte' then remplis_champs_clic_dans_texte
when QuestionClicDansImage.QUESTION_TYPE then remplis_champs_clic_dans_image
when QuestionGlisserDeposer.QUESTION_TYPE then remplis_champs_glisser_deposer
when QuestionQcm.QUESTION_TYPE then remplis_champs_qcm
when QuestionSaisie.QUESTION_TYPE then remplis_champs_saisie
when QuestionClicDansTexte.QUESTION_TYPE then remplis_champs_clic_dans_texte
end
end

Expand Down
10 changes: 5 additions & 5 deletions app/models/import_export/questions/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def cree_transcription(categorie, audio_url, ecrit)

def update_champs_specifiques
updates = {
'QuestionClicDansImage' => :update_clic_dans_image,
'QuestionGlisserDeposer' => :update_glisser_deposer,
'QuestionQcm' => :update_qcm,
'QuestionSaisie' => :update_saisie,
'QuestionClicDansTexte' => :update_clic_dans_texte
QuestionClicDansImage.QUESTION_TYPE => :update_clic_dans_image,
QuestionGlisserDeposer.QUESTION_TYPE => :update_glisser_deposer,
QuestionQcm.QUESTION_TYPE => :update_qcm,
QuestionSaisie.QUESTION_TYPE => :update_saisie,
QuestionClicDansTexte.QUESTION_TYPE => :update_clic_dans_texte
}
send(updates[@type]) if updates.key?(@type)
end
Expand Down
12 changes: 6 additions & 6 deletions app/models/import_export/questions/import_export_donnees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class ImportExportDonnees
intitule_audio].freeze
HEADERS_COMMUN = %i[libelle nom_technique illustration intitule_ecrit intitule_audio
consigne_ecrit consigne_audio description].freeze
HEADERS_ATTENDUS = { 'QuestionClicDansImage' => HEADERS_COMMUN + HEADERS_CLIC_DANS_IMAGE,
'QuestionClicDansTexte' => HEADERS_COMMUN + HEADERS_CLIC_DANS_TEXTE,
'QuestionGlisserDeposer' => HEADERS_COMMUN + HEADERS_GLISSER_DEPOSER,
'QuestionQcm' => HEADERS_COMMUN + HEADERS_QCM,
'QuestionSaisie' => HEADERS_COMMUN + HEADERS_SAISIE,
'QuestionSousConsigne' => HEADERS_SOUS_CONSIGNE }.freeze
HEADERS_ATTENDUS = { QuestionClicDansImage.QUESTION_TYPE => HEADERS_COMMUN + HEADERS_CLIC_DANS_IMAGE,
QuestionClicDansTexte.QUESTION_TYPE => HEADERS_COMMUN + HEADERS_CLIC_DANS_TEXTE,
QuestionGlisserDeposer.QUESTION_TYPE => HEADERS_COMMUN + HEADERS_GLISSER_DEPOSER,
QuestionQcm.QUESTION_TYPE => HEADERS_COMMUN + HEADERS_QCM,
QuestionSaisie.QUESTION_TYPE => HEADERS_COMMUN + HEADERS_SAISIE,
QuestionSousConsigne.QUESTION_TYPE => HEADERS_SOUS_CONSIGNE }.freeze

def initialize(questions: nil, type: nil)
@questions = questions
Expand Down
18 changes: 9 additions & 9 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class Question < ApplicationRecord
CATEGORIE = %i[situation scolarite sante appareils].freeze
AUDIO_TYPES = %i[intitule modalite_reponse consigne].freeze
INTERACTION_TYPES = {
'QuestionQcm' => 'qcm',
'QuestionClicDansImage' => 'clic dans image',
'QuestionGlisserDeposer' => 'glisser deposer',
'QuestionSousConsigne' => 'sous consigne',
'QuestionSaisie' => 'saisie',
'QuestionClicDansTexte' => 'clic dans texte'
QuestionQcm.QUESTION_TYPE => 'qcm',
QuestionClicDansImage.QUESTION_TYPE => 'clic dans image',
QuestionGlisserDeposer.QUESTION_TYPE => 'glisser deposer',
QuestionSousConsigne.QUESTION_TYPE => 'sous consigne',
QuestionSaisie.QUESTION_TYPE => 'saisie',
QuestionClicDansTexte.QUESTION_TYPE => 'clic dans texte'
}.freeze

enum :categorie, CATEGORIE.zip(CATEGORIE.map(&:to_s)).to_h, prefix: true
Expand Down Expand Up @@ -84,15 +84,15 @@ def interaction
end

def saisie?
type == 'QuestionSaisie'
type == QuestionSaisie.QUESTION_TYPE
end

def qcm?
type == 'QuestionQcm'
type == QuestionQcm.QUESTION_TYPE
end

def sous_consigne?
type == 'QuestionSousConsigne'
type == QuestionSousConsigne.QUESTION_TYPE
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/question_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class QuestionClicDansImage < Question
CLASS_BONNE_REPONSE = 'bonne-reponse'
QUESTION_TYPE = 'QuestionClicDansImage'

has_one_attached :zone_cliquable
has_one_attached :image_au_clic
Expand Down
2 changes: 1 addition & 1 deletion app/models/question_clic_dans_texte.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class QuestionClicDansTexte < Question
# validates :texte_sur_illustration, presence: true
QUESTION_TYPE = 'QuestionClicDansTexte'

def as_json(_options = nil)
base_json
Expand Down
1 change: 1 addition & 0 deletions app/models/question_glisser_deposer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class QuestionGlisserDeposer < Question
QUESTION_TYPE = 'QuestionGlisserDeposer'
has_many :reponses, -> { order(position: :asc) },
foreign_key: :question_id,
class_name: 'Choix',
Expand Down
1 change: 1 addition & 0 deletions app/models/question_qcm.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class QuestionQcm < Question
QUESTION_TYPE = 'QuestionQcm'
enum :metacompetence, { numeratie: 0, ccf: 1, 'syntaxe-orthographe': 2 }
enum :type_qcm, { standard: 0, jauge: 1 }
has_many :choix, lambda {
Expand Down
2 changes: 2 additions & 0 deletions app/models/question_saisie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class QuestionSaisie < Question
QUESTION_REDACTION = 'redaction_note'
QUESTION_TYPE = 'QuestionSaisie'

enum :type_saisie, { redaction: 0, numerique: 1 }

has_one :bonne_reponse, class_name: 'Choix', foreign_key: :question_id, dependent: :destroy
Expand Down
2 changes: 2 additions & 0 deletions app/models/question_sous_consigne.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class QuestionSousConsigne < Question
QUESTION_TYPE = 'QuestionSousConsigne'

def as_json(_options = nil)
json = slice(:id, :nom_technique)
json['type'] = 'sous-consigne'
Expand Down
4 changes: 2 additions & 2 deletions app/models/restitution/questions_reponses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def choix_repondu(question, evenement_reponse)

def questions_repondues
questions_ids = reponses.collect { |r| r.donnees['question'] }
Question.where(id: questions_ids, type: 'QuestionQcm').includes(:choix) +
Question.where(id: questions_ids).where.not(type: 'QuestionQcm')
Question.where(id: questions_ids, type: QuestionQcm.QUESTION_TYPE).includes(:choix) +
Question.where(id: questions_ids).where.not(type: QuestionQcm.QUESTION_TYPE)
end
end
end

0 comments on commit baea81b

Please sign in to comment.