Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechecker #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,38 @@ class filter_tabs extends moodle_text_filter {
/**
* Placeholder for tabs.
*/
const PLACEHOLDER_PATTERN = '/\{%:([^}]*)\}(.*?)\{%\}/s';
const PLACEHOLDER_PATTERN = '/\{%:([^}]*)}(.*?)\{%}/s';

/**
* Page.
*
* @var moodle_page $page.
*/
private $page;
private moodle_page $page;

/**
* Plugin renderer.
*
* @var renderer $renderer.
*/
private $renderer;
private static renderer $renderer;

/**
* Insert JS scripts to page using amd.
*
* @param moodle_page $page The current page.
* @param context $context The current context.
*/
public function setup($page, $context) {
public function setup($page, $context): void {
$this->page = $page;
$page->requires->js_call_amd('filter_tabs/tabs', 'init');

static $initialised = false;

if (!$initialised) {
$page->requires->js_call_amd('filter_tabs/tabs', 'init');
$initialised = true;
}

}

/**
Expand All @@ -66,7 +73,7 @@ public function setup($page, $context) {
* @param string $text The text to filter.
* @param array $options The filter options.
*/
public function filter($text, array $options = array() ) {
public function filter($text, array $options = [] ): string {
if (!($matches = $this->get_matches($text))) {
return $text;
}
Expand All @@ -80,8 +87,8 @@ public function filter($text, array $options = array() ) {
* @param string $text
* @return null|array
*/
private function get_matches(string $text) {
if (!is_string($text) || empty($text) || strpos($text, '{%') === false ||
private function get_matches(string $text): ?array {
if (!is_string($text) || empty($text) || !str_contains($text, '{%') ||
!preg_match_all(self::PLACEHOLDER_PATTERN, $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
return null;
}
Expand All @@ -95,14 +102,12 @@ private function get_matches(string $text) {
* @param array $matches
* @return string
*/
private function replace_text(string $text, array $matches) {
private function replace_text(string $text, array $matches): string {
$textbefore = substr($text, 0, strpos($text, "{%:"));
$textafter = substr($text, strpos($text, "{%:"));
$text = preg_replace(self::PLACEHOLDER_PATTERN, "", $textbefore)
return preg_replace(self::PLACEHOLDER_PATTERN, "", $textbefore)
. $this->generate_tabs($matches) .
preg_replace(self::PLACEHOLDER_PATTERN, "", $textafter);

return $text;
}

/**
Expand All @@ -111,14 +116,14 @@ private function replace_text(string $text, array $matches) {
* @param array $matches
* @return string
*/
private function generate_tabs(array $matches) {
if ($this->renderer === null) {
$this->renderer = $this->page->get_renderer('filter_tabs');
private function generate_tabs(array $matches): string {
if (!isset(self::$renderer)) {
self::$renderer = $this->page->get_renderer('filter_tabs');
}

$config = config::create(get_config('filter_tabs'));
$tabs = tab::from_matches($matches);

return $this->renderer->render(new renderable($config->get_template(), $tabs));
return self::$renderer->render(new renderable($config->get_template(), $tabs));
}
}
24 changes: 14 additions & 10 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use filter_tabs\config;
use filter_tabs\helper;

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

if ($ADMIN->fulltree) {
Expand All @@ -37,32 +40,33 @@
'')
);

$tabsconfigsoptions = array(
\filter_tabs\config::YUI_TABS => get_string('enableyui', 'filter_tabs', null, true),
\filter_tabs\config::BOOTSTRAP_2_TABS => get_string('enablebootstrap2', 'filter_tabs', null, true),
\filter_tabs\config::BOOTSTRAP_4_TABS => get_string('enablebootstrap4', 'filter_tabs', null, true)
);
$tabsconfigsoptions = [
config::YUI_TABS => get_string('enableyui', 'filter_tabs', null, true),
config::BOOTSTRAP_2_TABS => get_string('enablebootstrap2', 'filter_tabs', null, true),
config::BOOTSTRAP_4_TABS => get_string('enablebootstrap4', 'filter_tabs', null, true),
];

if (($bootstrapversion = \filter_tabs\helper::get_bootstrap_version())) {
$version = null;
if (($bootstrapversion = helper::get_bootstrap_version())) {
$version = substr($bootstrapversion, 0, 1);
}

$settings->add(new admin_setting_configselect(
'filter_tabs/enablebootstrap',
get_string('selecttabs', 'filter_tabs', null, true),
get_string('selecttabs_desc', 'filter_tabs', null, true),
'4' === $version ? \filter_tabs\config::BOOTSTRAP_4_TABS : \filter_tabs\config::BOOTSTRAP_2_TABS,
'4' === $version ? config::BOOTSTRAP_4_TABS : config::BOOTSTRAP_2_TABS,
$tabsconfigsoptions)
);

if ($bootstrapversion) {
$suggestedoption = '';
if ($version !== '4') {
$suggestedoption .= '<br /><small>' . get_string('suggestedoption', 'filter_tabs', null, true) .
": [ \"{$tabsconfigsoptions[\filter_tabs\config::BOOTSTRAP_2_TABS]}\" ]</small>";
": [ \"{$tabsconfigsoptions[config::BOOTSTRAP_2_TABS]}\" ]</small>";
} else {
$suggestedoption .= '<br /><small>' . get_string('suggestedoption', 'filter_tabs', null, true) .
": [ \"{$tabsconfigsoptions[\filter_tabs\config::BOOTSTRAP_4_TABS]}\" ]</small>";
": [ \"{$tabsconfigsoptions[config::BOOTSTRAP_4_TABS]}\" ]</small>";
}

// Bootstrap suggestion.
Expand All @@ -74,7 +78,7 @@

// Preview.
$context = context_system::instance();
$filtertabs = new filter_tabs($context, array());
$filtertabs = new filter_tabs($context, []);
$filtertabs->setup($PAGE, $context);
$tabsfilteredtext = $filtertabs->filter('{%:First tab}Some text{%}{%:Second tab}Another text{%}');
$settings->add(new admin_setting_heading(
Expand Down