Skip to content

Commit

Permalink
Exercise: Add option to hide attempts table on start page - refs BT#1…
Browse files Browse the repository at this point in the history
…9280
  • Loading branch information
cfasanando committed Oct 21, 2021
1 parent 14248c4 commit 0e85fb9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
83 changes: 83 additions & 0 deletions main/exercise/exercise.class.php
Expand Up @@ -97,6 +97,7 @@ class Exercise
public $hideExpectedAnswer;
public $forceShowExpectedChoiceColumn;
public $disableHideCorrectAnsweredQuestions;
public $hideAttemptsTableOnStartPage;

/**
* Constructor of the class.
Expand Down Expand Up @@ -142,6 +143,7 @@ public function __construct($courseId = 0)
$this->hideNoAnswer = false;
$this->hideExpectedAnswer = false;
$this->disableHideCorrectAnsweredQuestions = false;
$this->hideAttemptsTableOnStartPage = 0;

if (!empty($courseId)) {
$courseInfo = api_get_course_info_by_id($courseId);
Expand Down Expand Up @@ -227,6 +229,10 @@ public function read($id, $parseQuestionList = true)
$this->hideQuestionNumber = $object->hide_question_number == 1;
}

if (isset($object->hide_attempts_table)) {
$this->hideAttemptsTableOnStartPage = $object->hide_attempts_table == 1;
}

if (isset($object->show_previous_button)) {
$this->showPreviousButton = $object->show_previous_button == 1;
}
Expand Down Expand Up @@ -1604,6 +1610,7 @@ public function save($type_e = '')
}
$expired_time = (int) $this->expired_time;
$showHideConfiguration = api_get_configuration_value('quiz_hide_question_number');
$showHideAttemptsTableOnStartPage = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');

// Exercise already exists
if ($id) {
Expand Down Expand Up @@ -1681,6 +1688,10 @@ public function save($type_e = '')
$paramsExtra['hide_question_number'] = $this->hideQuestionNumber;
}

if ($showHideAttemptsTableOnStartPage) {
$paramsExtra['hide_attempts_table'] = $this->hideAttemptsTableOnStartPage;
}

$params = array_merge($params, $paramsExtra);

Database::update(
Expand Down Expand Up @@ -2466,6 +2477,15 @@ public function createForm($form, $type = 'full')
get_lang('UpdateTitleInLps')
);

$showHideAttemptsTableOnStartPage = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');
if ($showHideAttemptsTableOnStartPage) {
$group = [
$form->createElement('radio', 'hide_attempts_table', null, get_lang('Yes'), '1'),
$form->createElement('radio', 'hide_attempts_table', null, get_lang('No'), '0'),
];
$form->addGroup($group, null, get_lang('HideAttemptsTableOnStartPage'));
}

$defaults = [];
if (api_get_setting('search_enabled') === 'true') {
require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
Expand Down Expand Up @@ -2668,6 +2688,7 @@ public function createForm($form, $type = 'full')

$this->setPageResultConfigurationDefaults($defaults);
$this->setHideQuestionNumberDefaults($defaults);
$this->setHideAttemptsTableOnStartPageDefaults($defaults);
$form->setDefaults($defaults);

// Freeze some elements.
Expand Down Expand Up @@ -2841,6 +2862,12 @@ public function processCreation($form, $type = '')
if ($showHideConfiguration) {
$this->setHideQuestionNumber($form->getSubmitValue('hide_question_number'));
}

$showHideAttemptsTableOnStartPage = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');
if ($showHideAttemptsTableOnStartPage) {
$this->setHideAttemptsTableOnStartPage($form->getSubmitValue('hide_attempts_table'));
}

$this->preventBackwards = (int) $form->getSubmitValue('prevent_backwards');

$this->start_time = null;
Expand Down Expand Up @@ -8553,6 +8580,34 @@ public function getHideQuestionNumber()
return 0;
}

/**
* Set the value to 1 to hide the attempts table on start page.
*
* @param int $value
*/
public function setHideAttemptsTableOnStartPage($value = 0)
{
$showHideAttemptsTableOnStartPage = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');
if ($showHideAttemptsTableOnStartPage) {
$this->hideAttemptsTableOnStartPage = (int) $value;
}
}

/**
* Gets the value to hide or show the attempts table on start page. If it does not exist, it is set to 0.
*
* @return int 1 if the attempts table must be hidden
*/
public function getHideAttemptsTableOnStartPage()
{
$showHideAttemptsTableOnStartPage = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');
if ($showHideAttemptsTableOnStartPage) {
return (int) $this->hideAttemptsTableOnStartPage;
}

return 0;
}

/**
* @param array $values
*/
Expand Down Expand Up @@ -8597,6 +8652,19 @@ public function setHideQuestionNumberDefaults(&$defaults)
}
}

/**
* Sets the value to show or hide the attempts table on start page in the default settings of the forms.
*
* @param array $defaults
*/
public function setHideAttemptsTableOnStartPageDefaults(&$defaults)
{
$configuration = $this->getHideAttemptsTableOnStartPageConfiguration();
if (!empty($configuration) && !empty($defaults)) {
$defaults = array_merge($defaults, $configuration);
}
}

/**
* @return array
*/
Expand Down Expand Up @@ -8628,6 +8696,21 @@ public function getHideQuestionNumberConfiguration()
return [];
}

/**
* Get the value to show or hide the attempts table on start page in the default settings of the forms.
*
* @return array
*/
public function getHideAttemptsTableOnStartPageConfiguration()
{
$pageConfig = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');
if ($pageConfig) {
return ['hide_attempts_table' => $this->hideAttemptsTableOnStartPage];
}

return [];
}

/**
* @param string $attribute
*
Expand Down
14 changes: 9 additions & 5 deletions main/exercise/overview.php
Expand Up @@ -510,11 +510,15 @@
);
}

$html .= Display::tag(
'div',
$table_content,
['class' => 'table-responsive']
);
$showHideAttemptsTableOnStartPage = api_get_configuration_value('quiz_hide_attempts_table_on_start_page');
$hideAttemptsTable = ($showHideAttemptsTableOnStartPage && 1 == $objExercise->hideAttemptsTableOnStartPage);
if (!$hideAttemptsTable) {
$html .= Display::tag(
'div',
$table_content,
['class' => 'table-responsive']
);
}
$html .= '</div>';

if ($certificateBlock) {
Expand Down
4 changes: 4 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -1362,6 +1362,10 @@
// ALTER TABLE c_quiz ADD COLUMN hide_question_number int NULL DEFAULT 0 COMMENT 'Show/Hide question number in quiz';
//$_configuration['quiz_hide_question_number'] = false;

// Allows you to show or hide the attempts table of an exercise on start page
// ALTER TABLE c_quiz ADD COLUMN hide_attempts_table int NULL DEFAULT 0 COMMENT 'Show/Hide attempts table of an exercise on start page';
//$_configuration['quiz_hide_attempts_table_on_start_page'] = false;

// Allow multiple options for the exercise "save answer" option
// ALTER TABLE c_quiz MODIFY COLUMN save_correct_answers INT NULL DEFAULT NULL;
//$_configuration['allow_quiz_save_correct_options'] = false;
Expand Down

0 comments on commit 0e85fb9

Please sign in to comment.