@@ -354,7 +354,7 @@ defmodule Cadet.Assessments do
354
354
insert_or_update_assessment_changeset ( assessment_params , force_update )
355
355
)
356
356
357
- if force_update and check_question_count ( assessment_multi , questions_params ) do
357
+ if force_update and invalid_force_update ( assessment_multi , questions_params ) do
358
358
{ :error , "Question count is different" }
359
359
else
360
360
questions_params
@@ -364,6 +364,7 @@ defmodule Cadet.Assessments do
364
364
% { assessment: % Assessment { id: id } } ->
365
365
question_exists =
366
366
Repo . exists? ( where ( Question , [ q ] , q . assessment_id == ^ id and q . display_order == ^ index ) )
367
+ # the !question_exists check allows for force updating of brand new assessments
367
368
if ! force_update or ! question_exists do
368
369
question_params
369
370
|> Map . put ( :display_order , index )
@@ -379,37 +380,48 @@ defmodule Cadet.Assessments do
379
380
end )
380
381
|> Map . put ( :display_order , index )
381
382
382
- % { id: question_id } =
383
+ % { id: question_id , type: type } =
383
384
where ( Question , [ q ] , q . display_order == ^ index and q . assessment_id == ^ id )
384
385
|> Repo . one ( )
385
386
386
- changeset = Question . changeset ( % Question { assessment_id: id , id: question_id } , params )
387
- Repo . update ( changeset )
387
+ if question_params . type != Atom . to_string ( type ) do
388
+ { :error , create_invalid_changeset_with_error ( :question , "Question types should remain the same" ) }
389
+ else
390
+ changeset = Question . changeset ( % Question { assessment_id: id , id: question_id } , params )
391
+ Repo . update ( changeset )
392
+ end
388
393
end
389
394
end )
390
395
end )
391
396
|> Repo . transaction ( )
392
397
end
393
398
end
394
399
395
- defp check_question_count ( assessment_multi , questions_params ) do
400
+ # Function that checks if the force update is invalid. The force update is only invalid
401
+ # if the new question count is different from the old question count.
402
+ defp invalid_force_update ( assessment_multi , questions_params ) do
396
403
assessment_id =
397
404
( assessment_multi . operations
398
405
|> List . first ( )
399
406
|> elem ( 1 )
400
407
|> elem ( 1 ) ) . data . id
401
408
409
+ # check if assessment already exists
402
410
if ! assessment_id do
403
411
false
404
412
else
405
- existing_questions_count =
406
- where ( Question , [ q ] , q . assessment_id == ^ assessment_id )
407
- |> Repo . all ( )
408
- |> Enum . count
409
-
410
- new_questions_count = Enum . count ( questions_params )
411
-
412
- existing_questions_count != new_questions_count
413
+ open_date = Repo . get ( Assessment , assessment_id ) . open_at
414
+ # check if assessment is already opened
415
+ if Timex . after? ( open_date , Timex . now ( ) ) do
416
+ false
417
+ else
418
+ existing_questions_count =
419
+ where ( Question , [ q ] , q . assessment_id == ^ assessment_id )
420
+ |> Repo . all ( )
421
+ |> Enum . count
422
+ new_questions_count = Enum . count ( questions_params )
423
+ existing_questions_count != new_questions_count
424
+ end
413
425
end
414
426
end
415
427
@@ -439,6 +451,7 @@ defmodule Cadet.Assessments do
439
451
params
440
452
|> Map . delete ( :open_at )
441
453
|> Map . delete ( :close_at )
454
+ |> Map . delete ( :is_published )
442
455
443
456
Assessment . changeset ( assessment , new_params )
444
457
0 commit comments