Skip to content

Commit

Permalink
MDL-73397 adminpresets: Create new core_adminpresets component
Browse files Browse the repository at this point in the history
The core_adminpresets component has been added and some data and
methods from the tool_admin_presets have been moved here.
  • Loading branch information
sarjona authored and Amaia Anabitarte committed Jan 3, 2022
1 parent 541d999 commit 727f0d4
Show file tree
Hide file tree
Showing 47 changed files with 5,702 additions and 3 deletions.
387 changes: 387 additions & 0 deletions adminpresets/classes/helper.php

Large diffs are not rendered by default.

@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

/**
* Select setting for blog's bloglevel setting.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_bloglevel extends adminpresets_admin_setting_configselect {

/**
* Extended to change the block visibility.
*
* @param bool $name Setting name to store.
* @param mixed $value Setting value to store.
* @return int|false config_log inserted id or false whenever the value has not been saved.
*/
public function save_value($name = false, $value = null) {
global $DB;

if (!$id = parent::save_value($name, $value)) {
return false;
}

// Object values if no arguments.
if ($value === null) {
$value = $this->value;
}

// Pasted from admin_setting_bloglevel (can't use write_config).
if ($value == 0) {
$DB->set_field('block', 'visible', 0, ['name' => 'blog_menu']);
} else {
$DB->set_field('block', 'visible', 1, ['name' => 'blog_menu']);
}

return $id;
}
}
@@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

/**
* Checkbox setting.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configcheckbox extends adminpresets_setting {

protected function set_value($value) {
$this->value = clean_param($value, PARAM_BOOL);
return true;
}

protected function set_visiblevalue() {
if ($this->value) {
$str = get_string('yes');
} else {
$str = get_string('no');
}

$this->visiblevalue = $str;
}
}
@@ -0,0 +1,45 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

use admin_setting;

/**
* Checkbox with an advanced checkbox that controls an additional $name.'_adv' config setting.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configcheckbox_with_advanced extends adminpresets_admin_setting_configcheckbox {

public function __construct(admin_setting $settingdata, $dbsettingvalue) {
// To look for other values.
$this->attributes = ['adv' => $settingdata->name . '_adv'];
parent::__construct($settingdata, $dbsettingvalue);
}

/**
* Uses delegation
*/
protected function set_visiblevalue() {
parent::set_visiblevalue();
$value = $this->attributesvalues[$this->attributes['adv']];
$this->visiblevalue .= $this->delegation->extra_set_visiblevalue($value, 'advanced');
}
}
@@ -0,0 +1,45 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

use admin_setting;

/**
* Checkbox with an advanced checkbox that controls an additional $name.'_locked' config setting.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configcheckbox_with_lock extends adminpresets_admin_setting_configcheckbox {

public function __construct(admin_setting $settingdata, $dbsettingvalue) {
// To look for other values.
$this->attributes = ['locked' => $settingdata->name . '_locked'];
parent::__construct($settingdata, $dbsettingvalue);
}

/**
* Uses delegation
*/
protected function set_visiblevalue() {
parent::set_visiblevalue();
$value = $this->attributesvalues[$this->attributes['locked']];
$this->visiblevalue .= $this->delegation->extra_set_visiblevalue($value, 'locked');
}
}
@@ -0,0 +1,39 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

/**
* Used to validate a textarea used for ip addresses.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configiplist extends adminpresets_admin_setting_configtext {

protected function set_value($value) {
// Check ip format.
if ($this->settingdata->validate($value) !== true) {
$this->value = false;
return false;
}

$this->value = $value;
return true;
}
}
@@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

/**
* Class to be extended by multicheckbox settings.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configmulticheckbox extends adminpresets_admin_setting_configmultiselect {

public function set_behaviors() {
$this->behaviors['loadchoices'] = &$this->settingdata;
}
}
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

/**
* Extends the base class and lists the selected values separated by comma.
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configmultiselect extends adminpresets_setting {

/**
* Ensure that the $value values are setting choices.
*
* @param mixed $value Setting value
* @return mixed Returns false if wrong param value
*/
protected function set_value($value) {
if ($value) {
$options = explode(',', $value);
foreach ($options as $option) {

foreach ($this->settingdata->choices as $key => $choice) {

if ($key == $option) {
$this->value = $option;
return true;
}
}
}

$value = implode(',', $options);
}

$this->value = $value;
}

protected function set_visiblevalue() {
$values = explode(',', $this->value);
$visiblevalues = [];

foreach ($values as $value) {

if (!empty($this->settingdata->choices[$value])) {
$visiblevalues[] = $this->settingdata->choices[$value];
}
}

if (empty($visiblevalues)) {
$this->visiblevalue = '';
return false;
}

$this->visiblevalue = implode(', ', $visiblevalues);
}
}
@@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_adminpresets\local\setting;

/**
* Generalizes a configmultipleselect with load_choices().
*
* @package core_adminpresets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adminpresets_admin_setting_configmultiselect_with_loader extends adminpresets_admin_setting_configmultiselect {

public function set_behaviors() {
$this->behaviors['loadchoices'] = &$this->settingdata;
}
}

0 comments on commit 727f0d4

Please sign in to comment.