Skip to content

Commit

Permalink
MBS-9099: Extend get_tool_config methode with a default parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
PM84 authored and PhMemmel committed May 27, 2024
1 parent 7f01a27 commit 84f46c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
46 changes: 11 additions & 35 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,61 +849,37 @@ public function set_tool_config(object|int $pageorid, string $name, string $valu
/**
* Get the tools config.
*
* @param int|object $pageorid
* @param mixed $pageorid
* @param string $name
* @param string $default
* @return mixed
* @throws dml_exception
*/
public static function get_tool_config($pageorid, $name = "") {
public static function get_tool_config(mixed $pageorid, string $name = '', string $default = '') {
global $DB;

$pageid = $pageorid;
if (is_object($pageorid)) {
$pageorid = $pageorid->id;
$pageid = $pageorid->id;
}

$conditions = ['pageid' => $pageorid];
$conditions = ['pageid' => $pageid];

if (!empty($name)) {
$conditions['name'] = $name;
$field = $DB->get_field('mootimeter_tool_settings', 'value', $conditions);
if (is_null($field) || $field === false) {
return '';
if (!empty($default)) {
$helper = new \mod_mootimeter\helper();
$helper->set_tool_config($pageid, $name, $default);
}
return $default;
}
return $field;
}

return (object) $DB->get_records_menu('mootimeter_tool_settings', $conditions, '', 'name, value');
}

/**
* Get the modified timestamp of setting.
*
* @param int|object $pageorid
* @param string $name
* @return mixed
* @throws dml_exception
*/
public static function get_tool_config_timemodified(int|object $pageorid, string $name = "") {
global $DB;

if (is_object($pageorid)) {
$pageorid = $pageorid->id;
}

$conditions = ['pageid' => $pageorid];

if (!empty($name)) {
$conditions['name'] = $name;
$field = $DB->get_field('mootimeter_tool_settings', 'timemodified', $conditions);
if (empty($field)) {
return "";
}
return $field;
}

return (object) $DB->get_records_menu('mootimeter_tool_settings', $conditions, '', 'name, timemodified');
}

/**
* Hook handle to validate the settings value with tool specific needs.
*
Expand Down
14 changes: 7 additions & 7 deletions tools/quiz/classes/quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class quiz extends \mod_mootimeter\toolhelper {
/** @var int Visualization id for pie chart */
const VISUALIZATION_ID_CHART_PIE = 4;

/** @var int Default value of maximum count of answers */
const MAXANSWERSDEFAULT = 1;

/** @var Answer cloum */
const MTMT_VIEW_RESULT_TEACHERPERMISSION = 2;

Expand Down Expand Up @@ -448,7 +451,7 @@ public function get_renderer_params(object $page) {
));

$inputtype = 'cb';
if (intval(self::get_tool_config($page->id, 'maxanswersperuser')) === 1) {
if (intval(self::get_tool_config($page->id, 'maxanswersperuser', self::MAXANSWERSDEFAULT)) === 1) {
$inputtype = 'rb';
}
foreach ($answeroptions as $answeroption) {
Expand Down Expand Up @@ -644,14 +647,11 @@ public function get_col_settings_tool_params(object $page, array $params = []) {
],
];

$maxanswers = self::get_tool_config($page->id, "maxanswersperuser");
if (empty($maxanswers) && !is_number($maxanswers)) {
// Empty also evaluates to true if $maxanswers equals "0", so we have to check that separately.
// If not specified set default value.
$maxanswers = 1;
} else {
$maxanswers = self::get_tool_config($page->id, "maxanswersperuser", self::MAXANSWERSDEFAULT);
if (!is_number($maxanswers)) {
$maxanswers = intval($maxanswers);
}

$params['maxanswers'] = [
'title' => get_string('answers_max_number', 'mootimetertool_quiz'),
'additional_class' => 'mootimeter_settings_selector',
Expand Down

0 comments on commit 84f46c0

Please sign in to comment.