diff --git a/config/install/quiz.settings.yml b/config/install/quiz.settings.yml index c9f9a414..7d5f8ad6 100644 --- a/config/install/quiz.settings.yml +++ b/config/install/quiz.settings.yml @@ -25,4 +25,5 @@ admin_review_options_end: solution: solution quiz_feedback: quiz_feedback has_timer: true +timer_format: 'Time left: %-H h %M min %S sec' remove_invalid_quiz_record: 86400 diff --git a/config/schema/quiz.schema.yml b/config/schema/quiz.schema.yml index 8f3b2e30..df309144 100644 --- a/config/schema/quiz.schema.yml +++ b/config/schema/quiz.schema.yml @@ -103,6 +103,9 @@ quiz.settings: has_timer: type: boolean label: Enable timer + timer_format: + type: string + label: Timer format admin_review_options_end: type: ignore admin_review_options_question: diff --git a/quiz.libraries.yml b/quiz.libraries.yml index 0cead3f3..9754ced8 100644 --- a/quiz.libraries.yml +++ b/quiz.libraries.yml @@ -7,4 +7,14 @@ confirm: js/confirm.js: {} jumper: js: - js/jumper.js: {} \ No newline at end of file + js/jumper.js: {} +countdown: + version: VERSION + js: + js/countdown.timer.js: {} + dependencies: + - core/jquery + - core/drupal + - core/drupalSettings + - core/jquery.once + - jquery_countdown/core \ No newline at end of file diff --git a/src/Controller/QuizQuestionController.php b/src/Controller/QuizQuestionController.php index 9eb5791e..9952ff1d 100644 --- a/src/Controller/QuizQuestionController.php +++ b/src/Controller/QuizQuestionController.php @@ -162,39 +162,21 @@ class QuizQuestionController extends EntityController { ]; } - if (function_exists('jquery_countdown_add') && \Drupal::config('quiz.settings')->get('has_timer', 0) && $quiz->time_limit) { - jquery_countdown_add('.countdown', [ - 'until' => ($quiz_result->time_start + $quiz->time_limit - \Drupal::time()->getRequestTime()), - 'onExpiry' => 'quiz_finished', - 'compact' => FALSE, - 'layout' => t('Time left') . ': {hnn}{sep}{mnn}{sep}{snn}', - ]); - // These are the two button op values that are accepted for answering - // questions. - $button_op1 = drupal_json_encode(t('Finish')); - $button_op2 = drupal_json_encode(t('Next')); - $js = " - function quiz_finished() { - // Find all buttons with a name of 'op'. - var buttons = jQuery('input[type=submit][name=op], button[type=submit][name=op]'); - // Filter out the ones that don't have the right op value. - buttons = buttons.filter(function() { - return this.value == $button_op1 || this.value == $button_op2; - }); - if (buttons.length == 1) { - // Since only one button was found, this must be it. - buttons.click(); - } - else { - // Zero, or more than one buttons were found; fall back on a page refresh. - window.location = window.location.href; - } - } - "; - drupal_add_js($js, 'inline'); - - // Add div to be used by jQuery countdown. - $content['body']['countdown']['#markup'] = '
'; + if (\Drupal::config('quiz.settings')->get('has_timer', 0) && $quiz->time_limit->value) { + $settings = [ + 'since' => $quiz_result->time_start->value + $quiz->time_limit->value - \Drupal::time()->getRequestTime(), + 'format' => \Drupal::config('quiz.settings')->get('timer_format', 'Time left: %-H h %M min %S sec'), + 'id' => 'countdown-quiz-' . $quiz->id() + ]; + + $build = []; + $content['body']['countdown'] = [ + '#markup' => '', + ]; + + // Attach library containing js files. + $content['#attached']['library'][] = 'quiz/countdown'; + $content['#attached']['drupalSettings']['jquery_countdown_quiz'][] = $settings; } $form = Drupal::formBuilder() diff --git a/src/Form/QuizAdminForm.php b/src/Form/QuizAdminForm.php index f4508c26..ac80e831 100644 --- a/src/Form/QuizAdminForm.php +++ b/src/Form/QuizAdminForm.php @@ -162,12 +162,26 @@ class QuizAdminForm extends ConfigFormBase { '#collapsed' => TRUE, ]; + $moduleHandler = \Drupal::service('module_handler'); $form['quiz_addons']['has_timer'] = [ '#type' => 'checkbox', '#title' => t('Display timer'), '#default_value' => $config->get('has_timer', 0), '#description' => t("@jquery_countdown is an optional module for Quiz. It is used to display a timer when taking a quiz. Without this timer, the user will not know how much time they have left to complete the Quiz.", $links), - '#disabled' => !function_exists('jquery_countdown_add'), + '#disabled' => !$moduleHandler->moduleExists('jquery_countdown'), + ]; + + $form['quiz_addons']['timer_format'] = [ + '#type' => 'textfield', + '#title' => t('Timer format'), + '#default_value' => $config->get('timer_format', 'Time left: %-H h %M min %S sec'), + '#description' => t('Timer format'), + '#disabled' => !$moduleHandler->moduleExists('jquery_countdown'), + '#states' => array( + 'invisible' => array( + ':input[name="has_timer"]' => array('checked' => FALSE), + ), + ), ]; $form['quiz_look_feel'] = [