@@ -51,6 +51,7 @@ class Exercise
51
51
public $ review_answers ;
52
52
public $ randomByCat ;
53
53
public $ text_when_finished ;
54
+ public $ text_when_finished_failure ;
54
55
public $ display_category_name ;
55
56
public $ pass_percentage ;
56
57
public $ edit_exercise_in_lp = false ;
@@ -127,6 +128,7 @@ public function __construct($courseId = 0)
127
128
$ this ->review_answers = false ;
128
129
$ this ->randomByCat = 0 ;
129
130
$ this ->text_when_finished = '' ;
131
+ $ this ->text_when_finished_failure = '' ;
130
132
$ this ->display_category_name = 0 ;
131
133
$ this ->pass_percentage = 0 ;
132
134
$ this ->modelType = 1 ;
@@ -204,6 +206,7 @@ public function read($id, $parseQuestionList = true)
204
206
$ this ->saveCorrectAnswers = $ object ->save_correct_answers ;
205
207
$ this ->randomByCat = $ object ->random_by_category ;
206
208
$ this ->text_when_finished = $ object ->text_when_finished ;
209
+ $ this ->text_when_finished_failure = $ object ->text_when_finished_failure ;
207
210
$ this ->display_category_name = $ object ->display_category_name ;
208
211
$ this ->pass_percentage = $ object ->pass_percentage ;
209
212
$ this ->is_gradebook_locked = api_resource_is_locked_by_gradebook ($ id , LINK_EXERCISE );
@@ -456,6 +459,28 @@ public function updateTextWhenFinished($text)
456
459
$ this ->text_when_finished = $ text ;
457
460
}
458
461
462
+ /**
463
+ * Get the text to display when the user has failed the test.
464
+ *
465
+ * @return string html text : the text to display ay the end of the test
466
+ */
467
+ public function getTextWhenFinishedFailure (): string
468
+ {
469
+ if (empty ($ this ->text_when_finished_failure )) {
470
+ return '' ;
471
+ }
472
+
473
+ return $ this ->text_when_finished_failure ;
474
+ }
475
+
476
+ /**
477
+ * Set the text to display when the user has succeeded in the test.
478
+ */
479
+ public function setTextWhenFinishedFailure (string $ text ): void
480
+ {
481
+ $ this ->text_when_finished_failure = $ text ;
482
+ }
483
+
459
484
/**
460
485
* return 1 or 2 if randomByCat.
461
486
*
@@ -1599,6 +1624,7 @@ public function save($type_e = '')
1599
1624
$ review_answers = isset ($ this ->review_answers ) && $ this ->review_answers ? 1 : 0 ;
1600
1625
$ randomByCat = (int ) $ this ->randomByCat ;
1601
1626
$ text_when_finished = $ this ->text_when_finished ;
1627
+ $ text_when_finished_failure = $ this ->text_when_finished_failure ;
1602
1628
$ display_category_name = (int ) $ this ->display_category_name ;
1603
1629
$ pass_percentage = (int ) $ this ->pass_percentage ;
1604
1630
$ session_id = $ this ->sessionId ;
@@ -1654,6 +1680,10 @@ public function save($type_e = '')
1654
1680
'hide_question_title ' => $ this ->getHideQuestionTitle (),
1655
1681
];
1656
1682
1683
+ if (true === api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
1684
+ $ paramsExtra ['text_when_finished_failure ' ] = $ text_when_finished_failure ;
1685
+ }
1686
+
1657
1687
$ allow = api_get_configuration_value ('allow_quiz_show_previous_button_setting ' );
1658
1688
if ($ allow === true ) {
1659
1689
$ paramsExtra ['show_previous_button ' ] = $ this ->showPreviousButton ();
@@ -1758,6 +1788,10 @@ public function save($type_e = '')
1758
1788
'hide_question_title ' => $ this ->getHideQuestionTitle (),
1759
1789
];
1760
1790
1791
+ if (true === api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
1792
+ $ params ['text_when_finished_failure ' ] = $ text_when_finished_failure ;
1793
+ }
1794
+
1761
1795
$ allow = api_get_configuration_value ('allow_exercise_categories ' );
1762
1796
if (true === $ allow ) {
1763
1797
if (!empty ($ this ->getExerciseCategoryId ())) {
@@ -2505,6 +2539,16 @@ public function createForm($form, $type = 'full')
2505
2539
$ editor_config
2506
2540
);
2507
2541
2542
+ if (true === api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
2543
+ $ form ->addHtmlEditor (
2544
+ 'text_when_finished_failure ' ,
2545
+ get_lang ('TextAppearingAtTheEndOfTheTestWhenTheUserHasFailed ' ),
2546
+ false ,
2547
+ false ,
2548
+ $ editor_config
2549
+ );
2550
+ }
2551
+
2508
2552
$ allow = api_get_configuration_value ('allow_notification_setting_per_exercise ' );
2509
2553
if ($ allow === true ) {
2510
2554
$ settings = ExerciseLib::getNotificationSettings ();
@@ -2572,6 +2616,7 @@ public function createForm($form, $type = 'full')
2572
2616
'notifications ' ,
2573
2617
'remedialcourselist ' ,
2574
2618
'advancedcourselist ' ,
2619
+ 'subscribe_session_when_finished_failure ' ,
2575
2620
], //exclude
2576
2621
false , // filter
2577
2622
false , // tag as select
@@ -2632,6 +2677,25 @@ public function createForm($form, $type = 'full')
2632
2677
}
2633
2678
}
2634
2679
2680
+ if (true === api_get_configuration_value ('exercise_subscribe_session_when_finished_failure ' )) {
2681
+ $ optionSessionWhenFailure = [];
2682
+
2683
+ if ($ failureSession = ExerciseLib::getSessionWhenFinishedFailure ($ this ->iid )) {
2684
+ $ defaults ['subscribe_session_when_finished_failure ' ] = $ failureSession ->getId ();
2685
+ $ optionSessionWhenFailure [$ failureSession ->getId ()] = $ failureSession ->getName ();
2686
+ }
2687
+
2688
+ $ form ->addSelectAjax (
2689
+ 'extra_subscribe_session_when_finished_failure ' ,
2690
+ get_lang ('SubscribeSessionWhenFinishedFailure ' ),
2691
+ $ optionSessionWhenFailure ,
2692
+ [
2693
+ 'url ' => api_get_path (WEB_AJAX_PATH ).'session.ajax.php? '
2694
+ .http_build_query (['a ' => 'search_session ' ]),
2695
+ ]
2696
+ );
2697
+ }
2698
+
2635
2699
$ settings = api_get_configuration_value ('exercise_finished_notification_settings ' );
2636
2700
if (!empty ($ settings )) {
2637
2701
$ options = [];
@@ -2687,6 +2751,10 @@ public function createForm($form, $type = 'full')
2687
2751
$ defaults ['exercise_category_id ' ] = $ this ->getExerciseCategoryId ();
2688
2752
$ defaults ['prevent_backwards ' ] = $ this ->getPreventBackwards ();
2689
2753
2754
+ if (true === api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
2755
+ $ defaults ['text_when_finished_failure ' ] = $ this ->getTextWhenFinishedFailure ();
2756
+ }
2757
+
2690
2758
if (!empty ($ this ->start_time )) {
2691
2759
$ defaults ['activate_start_date_check ' ] = 1 ;
2692
2760
}
@@ -2715,6 +2783,11 @@ public function createForm($form, $type = 'full')
2715
2783
$ defaults ['results_disabled ' ] = 0 ;
2716
2784
$ defaults ['randomByCat ' ] = 0 ;
2717
2785
$ defaults ['text_when_finished ' ] = '' ;
2786
+
2787
+ if (true === api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
2788
+ $ defaults ['text_when_finished_failure ' ] = '' ;
2789
+ }
2790
+
2718
2791
$ defaults ['start_time ' ] = date ('Y-m-d 12:00:00 ' );
2719
2792
$ defaults ['display_category_name ' ] = 1 ;
2720
2793
$ defaults ['end_time ' ] = date ('Y-m-d 12:00:00 ' , time () + 84600 );
@@ -2886,6 +2959,11 @@ public function processCreation($form, $type = '')
2886
2959
$ this ->updateSaveCorrectAnswers ($ form ->getSubmitValue ('save_correct_answers ' ));
2887
2960
$ this ->updateRandomByCat ($ form ->getSubmitValue ('randomByCat ' ));
2888
2961
$ this ->updateTextWhenFinished ($ form ->getSubmitValue ('text_when_finished ' ));
2962
+
2963
+ if (true === api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
2964
+ $ this ->setTextWhenFinishedFailure ($ form ->getSubmitValue ('text_when_finished_failure ' ));
2965
+ }
2966
+
2889
2967
$ this ->updateDisplayCategoryName ($ form ->getSubmitValue ('display_category_name ' ));
2890
2968
$ this ->updateReviewAnswers ($ form ->getSubmitValue ('review_answers ' ));
2891
2969
$ this ->updatePassPercentage ($ form ->getSubmitValue ('pass_percentage ' ));
@@ -11759,6 +11837,34 @@ public static function getFeedbackTypeLiteral(int $feedbackType): string
11759
11837
return $ result ;
11760
11838
}
11761
11839
11840
+ /**
11841
+ * Return the text to display, based on the score and the max score.
11842
+ *
11843
+ * @param int|float $score
11844
+ * @param int|float $maxScore
11845
+ */
11846
+ public function getFinishText ($ score , $ maxScore ): string
11847
+ {
11848
+ if (true !== api_get_configuration_value ('exercise_text_when_finished_failure ' )) {
11849
+ return $ this ->getTextWhenFinished ();
11850
+ }
11851
+
11852
+ $ passPercentage = $ this ->selectPassPercentage ();
11853
+
11854
+ if (!empty ($ passPercentage )) {
11855
+ $ percentage = float_format (
11856
+ ($ score / (0 != $ maxScore ? $ maxScore : 1 )) * 100 ,
11857
+ 1
11858
+ );
11859
+
11860
+ if ($ percentage < $ passPercentage ) {
11861
+ return $ this ->getTextWhenFinishedFailure ();
11862
+ }
11863
+ }
11864
+
11865
+ return $ this ->getTextWhenFinished ();
11866
+ }
11867
+
11762
11868
/**
11763
11869
* Get number of questions in exercise by user attempt.
11764
11870
*
0 commit comments