Skip to content

Commit

Permalink
Merge pull request #49 from portabilis/portabilis-patch-2019-07-26
Browse files Browse the repository at this point in the history
Portabilis Patch 26/07/2019
  • Loading branch information
jayata committed Jul 29, 2019
2 parents c0c4651 + 2d662ec commit 5f888e0
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 20 deletions.
7 changes: 7 additions & 0 deletions app/enumerations/parallel_exams_calculation_types.rb
@@ -0,0 +1,7 @@
class ParallelExamsCalculationTypes < EnumerateIt::Base
associate_values substitution: 1,
average: 2,
sum: 3

sort_by :none
end
7 changes: 6 additions & 1 deletion app/models/exam_rule.rb
Expand Up @@ -9,6 +9,7 @@ class ExamRule < ActiveRecord::Base
has_enumeration_for :frequency_type, with: FrequencyTypes
has_enumeration_for :opinion_type, with: OpinionTypes
has_enumeration_for :recovery_type, with: RecoveryTypes
has_enumeration_for :parallel_exams_calculation_type, with: ParallelExamsCalculationTypes

belongs_to :rounding_table
belongs_to :differentiated_exam_rule, class_name: 'ExamRule'
Expand Down Expand Up @@ -40,6 +41,10 @@ def conceptual_rounding_table
end

def calculate_school_term_average?
calculate_avg_parallel_exams?
parallel_exams_calculation_type == ParallelExamsCalculationTypes::AVERAGE
end

def calculate_school_term_sum?
parallel_exams_calculation_type == ParallelExamsCalculationTypes::SUM
end
end
2 changes: 2 additions & 0 deletions app/models/remote_grade.rb
Expand Up @@ -4,6 +4,8 @@ class RemoteGrade
attr_accessor :attributes

def self.all(collection)
return [] if collection.blank?

collection.map do |record|
new(record)
end
Expand Down
7 changes: 3 additions & 4 deletions app/reports/exam_record_report.rb
Expand Up @@ -188,10 +188,9 @@ def daily_notes_table
recovery_student = RecoveryDiaryRecordStudent.find_by(student_id: student_id, recovery_diary_record_id: exam.recovery_diary_record_id)

score = recovery_student.present? ? recovery_student.try(:score) : (student_enrolled_on_date?(student_id, exam.recorded_at) ? '' :NullDailyNoteStudent.new.note)
if recovery_student.try(:score).present?
recovery_average = SchoolTermAverageCalculator.new(classroom).calculate(averages[student_enrollment.student_id], recovery_student.score)
averages[student_enrollment.student_id] = ScoreRounder.new(classroom, RoundedAvaliations::SCHOOL_TERM_RECOVERY).round(recovery_average)
end

recovery_average = SchoolTermAverageCalculator.new(classroom).calculate(averages[student_enrollment.student_id], recovery_student.try(:score))
averages[student_enrollment.student_id] = ScoreRounder.new(classroom, RoundedAvaliations::SCHOOL_TERM_RECOVERY).round(recovery_average)
end

student = Student.find(student_id)
Expand Down
46 changes: 46 additions & 0 deletions app/services/conceptual_exam_value_creator.rb
@@ -0,0 +1,46 @@
class ConceptualExamValueCreator
def self.create_empty_by(classroom_id, teacher_id)
new(classroom_id, teacher_id).create_empty
end

def initialize(classroom_id, teacher_id)
raise ArgumentError if classroom_id.blank? || teacher_id.blank?

@classroom = Classroom.find(classroom_id)
@teacher_id = teacher_id
end

def create_empty
TeacherDisciplineClassroom.joins(join_conceptual_exam_value)
.joins(join_conceptual_exam)
.select(
'conceptual_exams.id AS conceptual_exam_id,
teacher_discipline_classrooms.discipline_id AS discipline_id'
)
.by_teacher_id(@teacher_id)
.by_classroom(@classroom)
.where('conceptual_exams.discarded_at IS NULL')
.where('conceptual_exam_values.id IS NULL').each do |record|
ConceptualExamValue.create!(
conceptual_exam_id: record.conceptual_exam_id,
discipline_id: record.discipline_id,
value: nil,
exempted_discipline: false
)
end
end

def join_conceptual_exam
<<-SQL
JOIN conceptual_exams
ON conceptual_exams.classroom_id = teacher_discipline_classrooms.classroom_id
SQL
end

def join_conceptual_exam_value
<<-SQL
LEFT JOIN conceptual_exam_values
ON conceptual_exam_values.discipline_id = teacher_discipline_classrooms.discipline_id
SQL
end
end
Expand Up @@ -30,7 +30,9 @@ def update_exam_rules(exam_rules)
exam_rule_record.tabela_arredondamento_id_conceitual
).try(:id)
exam_rule.rounding_table_concept_api_code = exam_rule_record.tabela_arredondamento_id_conceitual
exam_rule.calculate_avg_parallel_exams = exam_rule_record.calcula_media_rec_paralela == '1'
exam_rule.parallel_exams_calculation_type =
exam_rule_record.tipo_calculo_recuperacao_paralela.to_i ||
ParallelExamsCalculationTypes::SUBSTITUTION
exam_rule.save! if exam_rule.changed?

if exam_rule_record.regra_diferenciada_id.present?
Expand Down
Expand Up @@ -33,6 +33,20 @@ def update_teacher_discipline_classrooms(teacher_discipline_classrooms)
existing_discipline_api_codes
)
)

classroom = classroom(teacher_discipline_classroom_record.turma_id)
classroom_id = classroom.try(:id) unless classroom.discarded?

teacher_id = teacher(teacher_discipline_classroom_record.servidor_id).try(:id)

next if classroom_id.nil? || teacher_id.nil?

CreateEmptyConceptualExamValueWorker.perform_in(
1.second,
entity_id,
classroom_id,
teacher_id
)
end
end
end
Expand Down
22 changes: 16 additions & 6 deletions app/services/school_term_average_calculator.rb
Expand Up @@ -4,20 +4,30 @@ def initialize(classroom)
end

def calculate(average, recovery_score)
return average unless recovery_score > 0
return calculate_sum(average, recovery_score) if calculate_sum?
return average if recovery_score.nil? || recovery_score <= 0
return calculate_average(average, recovery_score) if calculate_average?

if calculate_average?
(recovery_score.to_f + average.to_f) / 2
else
recovery_score > average ? recovery_score : average
end
recovery_score > average ? recovery_score : average
end

private

attr_accessor :classroom

def calculate_sum(average, recovery_score)
(recovery_score.presence || average).to_f + average.to_f
end

def calculate_average(average, recovery_score)
(recovery_score.to_f + average.to_f) / 2
end

def calculate_average?
classroom.exam_rule.try(:calculate_school_term_average?)
end

def calculate_sum?
classroom.exam_rule.try(:calculate_school_term_sum?)
end
end
11 changes: 11 additions & 0 deletions app/workers/create_empty_conceptual_exam_value_worker.rb
@@ -0,0 +1,11 @@
class CreateEmptyConceptualExamValueWorker
include Sidekiq::Worker

sidekiq_options unique: :until_and_while_executing, queue: :low

def perform(entity_id, classroom_id, teacher_id)
Entity.find(entity_id).using_connection do
ConceptualExamValueCreator.create_empty_by(classroom_id, teacher_id)
end
end
end
6 changes: 5 additions & 1 deletion config/initializers/redis.rb
@@ -1 +1,5 @@
$REDIS_DB = Redis.new
$REDIS_DB = if Rails.application.secrets[:redis_url]
Redis.new(url: Rails.application.secrets[:redis_url])
else
Redis.new
end
@@ -0,0 +1,29 @@
class MigrateCalculateAvgParallelExamsToParallelExamsCalculationType < ActiveRecord::Migration
def up
add_column :exam_rules, :parallel_exams_calculation_type, :integer, null: false, default: 1

execute <<-SQL
UPDATE exam_rules
SET parallel_exams_calculation_type = CASE
WHEN calculate_avg_parallel_exams THEN 2
ELSE 1
END
SQL

remove_column :exam_rules, :calculate_avg_parallel_exams
end

def down
add_column :exam_rules, :calculate_avg_parallel_exams, :boolean, null: false, default: false

execute <<-SQL
UPDATE exam_rules
SET calculate_avg_parallel_exams = CASE
WHEN parallel_exams_calculation_type = 2 THEN true
ELSE false
END
SQL

remove_column :exam_rules, :parallel_exams_calculation_type
end
end
33 changes: 26 additions & 7 deletions spec/services/school_term_average_calculator_spec.rb
Expand Up @@ -9,9 +9,9 @@
)
end

context 'should calculate average' do
context 'calculation type is average' do
before do
classroom.exam_rule.calculate_avg_parallel_exams = true
classroom.exam_rule.parallel_exams_calculation_type = ParallelExamsCalculationTypes::AVERAGE
end

it 'returns average of parameters' do
Expand All @@ -21,18 +21,37 @@
end
end

context 'should calculate greater score' do
context 'calculation type is sum' do
before do
classroom.exam_rule.calculate_avg_parallel_exams = false
classroom.exam_rule.parallel_exams_calculation_type = ParallelExamsCalculationTypes::SUM
end

context 'recovery_score is a number' do
it 'returns sum of parameters' do
expect(subject.calculate(5, 9)).to eq(14)
expect(subject.calculate(6, 0)).to eq(6)
end
end

context 'recovery_score is nil' do
it 'returns double of first parameter' do
expect(subject.calculate(6, nil)).to eq(12)
end
end
end

context 'calculation type is substitution' do
before do
classroom.exam_rule.parallel_exams_calculation_type = ParallelExamsCalculationTypes::SUBSTITUTION
end

it 'returns greater parameter passed' do
greater_score = 4
lower_score = 2

expect(subject.calculate(lower_score,greater_score)).to eq(greater_score)
expect(subject.calculate(greater_score,lower_score)).to eq(greater_score)
expect(subject.calculate(greater_score,greater_score)).to eq(greater_score)
expect(subject.calculate(lower_score, greater_score)).to eq(greater_score)
expect(subject.calculate(greater_score, lower_score)).to eq(greater_score)
expect(subject.calculate(greater_score, greater_score)).to eq(greater_score)
end
end
end

0 comments on commit 5f888e0

Please sign in to comment.