-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed error in JSON example and improved setting with JSON validation.
- Loading branch information
Showing
9 changed files
with
211 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Atto styles - Special settings. | ||
* | ||
* @package atto_styles | ||
* @copyright 2020 Kathrin Osswald, Ulm University <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace atto_styles; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Special setting for configtextarea with JSON validation. | ||
* | ||
* @package atto_styles | ||
* @copyright 2020 Kathrin Osswald, Ulm University <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class admin_setting_configtextarea_validatejson extends \admin_setting_configtextarea { | ||
|
||
/** | ||
* Validate the contents of the textarea to ensure it's valid JSON. | ||
* | ||
* @param string $data The JSON from text field. | ||
* @return mixed bool true for success or string:error on failure. | ||
*/ | ||
public function validate($data) { | ||
// If no entry is made validate true, otherwise the setting would always return an error and | ||
// could not be saved. | ||
if (empty($data)) { | ||
return true; | ||
} | ||
|
||
$jsonobject = json_decode($data); | ||
// If the JSON decoding was not possible (returns null) or encountered errors (returns false) | ||
// then return an error message. | ||
if (!$jsonobject) { | ||
return get_string('jsondecodemessage', 'atto_styles'); | ||
} | ||
|
||
// Return the result of the parent class' check of the data. | ||
return parent::validate($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Atto styles - Upgrade Script. | ||
* | ||
* @package atto_styles | ||
* @copyright 2020 Kathrin Osswald, Ulm University <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die; | ||
|
||
/** | ||
* Function to upgrade atto_styles | ||
* @param int $oldversion the version we are upgrading from | ||
* @return bool result | ||
*/ | ||
function xmldb_atto_styles_upgrade($oldversion) { | ||
|
||
if ($oldversion < 2020100200) { | ||
/* JSON validation has been added to the setting and now every entry must be valid JSON. | ||
Up to this point entries without [] brackets were accepted, so we need to add them now | ||
to the existent setting entries. */ | ||
|
||
$oldconfig = get_config('atto_styles', 'config'); | ||
|
||
/* Check if rules with multiple JSON heads exist like this | ||
{ | ||
"title": "Hero unit box", | ||
"type": "block", | ||
"classes": "hero-unit" | ||
},{ | ||
"title": "Muted Well", | ||
"type": "block", | ||
"classes": "well text-muted" | ||
} | ||
and add surrounding square brackets around it to provide valid JSON code in the setting. */ | ||
// First character (without leading whitespaces) in string should not be a '['. | ||
// Last character (without trailing whitespaces) in string should not be a ‘]‘. | ||
if (strpos($oldconfig, '},') !== false && | ||
(trim($oldconfig)[0] != '[') && | ||
(trim($oldconfig)[-1] != ']')) { | ||
$newconfig = '['.trim($oldconfig).']'; | ||
set_config('config', $newconfig, 'atto_styles'); | ||
} | ||
|
||
upgrade_plugin_savepoint(true, 2020100200, 'atto', 'styles'); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters