Skip to content

Commit

Permalink
Add an additional settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
abias committed Nov 4, 2024
1 parent 1d571ba commit 398f115
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 3 deletions.
46 changes: 46 additions & 0 deletions classes/admin_externalpage_in_tab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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 theme_boost_union;

/**
* Theme Boost Union - External admin settings page which can be placed within a tab
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Class admin_externalpage_in_tab.
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_externalpage_in_tab extends \admin_externalpage {
/**
* Dummy function just to make /admin/settings.php happy.
* It returns just an empty string.
*
* @return string
*/
public function output_html() {
return '';
}
}
89 changes: 89 additions & 0 deletions classes/admin_externalpage_tabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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 theme_boost_union;

use moodle_url;
use tabobject;
use tabtree;

/**
* Theme Boost Union - Tabs to be shown within an external admin settings page
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Class admin_externalpage_tabs.
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_externalpage_tabs {

/**
* @var array Holds the tabs in this tab tree.
*/
private $tabs;


/**
* Create a tab tree.
*
* @return void
*/
public function _construct() {
// Initialize the tab tree.
$this->tabs = [];
}

/**
* Add a tab to the tab tree.
*
* @param string $name The (internal) tab name.
* @param moodle_url $url The tab URL.
* @param string $label The tab label.
* @return void
*/
public function add_tab(string $name, moodle_url $url, string $label) {
// Create a new tab.
$newtab = new tabobject($name, $url, $label);

// Add the tab to the tab tree.
$this->tabs[] = $newtab;
}

/**
* Render the tab tree.
*
* @param string $selected The selected tab's name.
* @return string The rendered tab tree.
*/
public function render_tabtree(string $selected) {
global $OUTPUT;

// Make a tabtree object from the added tabs.
$tabtree = new tabtree($this->tabs, $selected);

// Render and return the tab tree.
return $OUTPUT->render($tabtree);
}
}
118 changes: 118 additions & 0 deletions classes/admin_settingspage_tabs_with_external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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 theme_boost_union;

/**
* Theme Boost Union - Admin settings page with tabs as well as external pages within a tab
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* based on code 2016 Ryan Wyllie in class theme_boost_admin_settingspage_tabs
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Class admin_settingspage_tabs_with_external.
*
* This class is copied and modified from /theme/boost/classes/admin_settingspage_tabs.php
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* based on code 2016 Ryan Wyllie in class theme_boost_admin_settingspage_tabs
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_settingspage_tabs_with_external extends \theme_boost_admin_settingspage_tabs {
/**
* Add the page.
*
* This function is amended with a switch for the external tabs.
*
* @param $tab object A tab.
*/
public function add($tab) {
// If the tab is an external page, add it as external tab.
if ($tab instanceof \admin_externalpage) {
return $this->add_external_tab($tab);

// Otherwise, fall back to normal mode.
} else {
return $this->add_tab($tab);
}
}

/**
* Add an external tab.
*
* @param \admin_externalpage $tab An external tab.
*/
private function add_external_tab(\admin_externalpage $tab) {
$this->tabs[] = $tab;
return true;
}

/**
* Generate the HTML output.
*
* This function is amended with a switch for the external tabs.
*
* @return string
*/
public function output_html() {
global $OUTPUT;

$activetab = optional_param('activetab', '', PARAM_TEXT);
$context = array('tabs' => array());
$havesetactive = false;

foreach ($this->get_tabs() as $tab) {
$active = false;

// Default to first tab it not told otherwise.
if (empty($activetab) && !$havesetactive) {
$active = true;
$havesetactive = true;
} else if ($activetab === $tab->name) {
$active = true;
}

$newtab = array(
'name' => $tab->name,
'displayname' => $tab->visiblename,
'html' => $tab->output_html(),
'active' => $active,
);
// If the tab is an external page.
if ($tab instanceof \admin_externalpage) {
// Add a flag for the mustache template.
$newtab['externaltab'] = true;
// And change the name (which is used as link in the mustache template)
// to hold the full URL of the external page..
$externalname = new \moodle_url('/theme/boost_union/snippets/overview.php');
$newtab['name'] = $externalname->out();
}
$context['tabs'][] = $newtab;
}

if (empty($context['tabs'])) {
return '';
}

return $OUTPUT->render_from_template('theme_boost/admin_setting_tabs', $context);
}
}
2 changes: 2 additions & 0 deletions lang/en/theme_boost_union.php
Original file line number Diff line number Diff line change
Expand Up @@ -1120,11 +1120,13 @@
$string['snippetsgoaleaseofuse'] = 'Ease of use';
$string['snippetsgoaleyecandy'] = 'Eye candy';
$string['snippetsnothingtodisplay'] = 'There aren\'t any CSS snippets which can be used.';
$string['snippetsoverview'] = 'Overview';
$string['snippetsoverview_desc'] = '<p>Boost Union\'s CSS snippets offer a possibility to add small (or slightly larger) amounts of CSS to the Moodle site. This can be particularly handy for fixing small visual glitches in Moodle core or for adding eye candy to your Moodle site.</p>Please note that the CSS snippets are added to the SCSS stack one after another. Thus, the order of the snippets on this page is key.</p><p>Please note as well that after each change which you make on this page, the theme cache is purged. This is necessary to make sure that the compiled SCSS code which is shipped to the browser is up-to-date.</p>';
$string['snippetsscope'] = 'Scope';
$string['snippetsscopecourse'] = 'Course';
$string['snippetsscopedashboard'] = 'Dashboard';
$string['snippetsscopeglobal'] = 'Global';
$string['snippetssettings'] = 'Settings';
$string['snippetsshowdetails'] = 'Show details';
$string['snippetssnippets'] = 'CSS snippets';
$string['snippetssource'] = 'Source';
Expand Down
32 changes: 31 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use theme_boost_union\admin_setting_configdatetime;
use theme_boost_union\admin_setting_configstoredfilealwayscallback;
use theme_boost_union\admin_setting_configtext_url;
use theme_boost_union\admin_settingspage_tabs_with_external;
use theme_boost_union\admin_externalpage_in_tab;
use core\di;
use core\hook\manager as hook_manager;

Expand Down Expand Up @@ -102,7 +104,7 @@

// Create CSS snippets settings page as external page
// (and allow users with the theme/boost_union:configure capability to access it).
$snippetspage = new admin_externalpage('theme_boost_union_snippets',
$snippetspage = new admin_externalpage('theme_boost_union_snippets_overview',
get_string('configtitlesnippets', 'theme_boost_union', null, true),
new moodle_url('/theme/boost_union/snippets/overview.php'),
'theme/boost_union:configure');
Expand Down Expand Up @@ -2819,6 +2821,34 @@

// Add settings page to the admin settings category.
$ADMIN->add('theme_boost_union', $page);


// Create CSS snippets settings page with tabs (and external pages).
// (and allow users with the theme/boost_union:configure capability to access it).
$page = new admin_settingspage_tabs_with_external('theme_boost_union_snippets',
get_string('configtitlesnippets', 'theme_boost_union', null, true),
'theme/boost_union:configure');

// Create CSS snippets overview tab
// (and allow users with the theme/boost_union:configure capability to access it).
$tab = new admin_externalpage_in_tab('theme_boost_union_snippets_overview',
get_string('snippetsoverview', 'theme_boost_union', null, true),
new moodle_url('/theme/boost_union/snippets/overview.php'),
'theme/boost_union:configure');

// Add tab to settings page.
$page->add($tab);


// Create general settings tab.
$tab = new admin_settingpage('theme_boost_union_snippets_settings',
get_string('snippetssettings', 'theme_boost_union', null, true));

// Add tab to settings page.
$page->add($tab);

// Add settings page to the admin settings category.
$ADMIN->add('theme_boost_union', $page);
}

// Add JS to remember the active admin tab to the page.
Expand Down
12 changes: 11 additions & 1 deletion snippets/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$context = context_system::instance();

// Access checks.
admin_externalpage_setup('theme_boost_union_snippets');
admin_externalpage_setup('theme_boost_union_snippets_overview');

// Prepare the page (to make sure that all necessary information is already set even if we just handle the actions as a start).
$PAGE->set_context($context);
Expand Down Expand Up @@ -128,6 +128,16 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('configtitlesnippets', 'theme_boost_union'));

// Create and render the tab tree.
$tabtree = new \theme_boost_union\admin_externalpage_tabs();
$tabtree->add_tab('snippetsoverview',
new \moodle_url('/theme/boost_union/snippets/overview.php'),
get_string('snippetsoverview', 'theme_boost_union'));
$tabtree->add_tab('snippetssettings',
new \moodle_url('/admin/settings.php', ['section' => 'theme_boost_union_snippets'], 'theme_boost_union_snippets_settings'),
get_string('snippetssettings', 'theme_boost_union'));
echo $tabtree->render_tabtree('snippetsoverview');

// Show snippets description.
echo get_string('snippetsoverview_desc', 'theme_boost_union');

Expand Down
Loading

0 comments on commit 398f115

Please sign in to comment.