Skip to content

Commit

Permalink
Add delivering styles from database
Browse files Browse the repository at this point in the history
  • Loading branch information
PhMemmel committed Sep 11, 2024
1 parent 2fba150 commit 217e523
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 4 deletions.
35 changes: 35 additions & 0 deletions classes/local/hook_callbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 tiny_c4l\local;

use core\hook\output\before_http_headers;

/**
* Class containing the hook callbacks for tiny_c4l.
*
* @package tiny_c4l
* @copyright 2024 ISB Bayern
* @author Philipp Memmel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class hook_callbacks {

public static function add_c4l_stylesheet_to_dom(\core\hook\output\before_http_headers $beforehttpheadershook): void {
$pluginfileurl = \moodle_url::make_pluginfile_url(SYSCONTEXTID, 'tiny_c4l', '', null, '', 'tiny_c4l_styles.css');
$beforehttpheadershook->renderer->get_page()->requires->css($pluginfileurl);
}
}
35 changes: 35 additions & 0 deletions classes/local/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
class utils {

public const TINY_C4L_CACHE_AREA = 'tiny_c4l_css';

public const TINY_C4L_CSS_CACHE_KEY = 'css';

public const TINY_C4L_CSS_CACHE_REV= 'cssrev';

public static function get_all_components(): array {
global $DB;
$componentrecords = $DB->get_records('tiny_c4l_component');
Expand Down Expand Up @@ -60,4 +66,33 @@ public static function get_all_flavors(): array {
$flavors = $DB->get_records('tiny_c4l_flavor');
return array_values($flavors);
}

public static function get_complete_css_as_string(): array {
global $DB;
$cache = \cache::make('tiny_c4l', self::TINY_C4L_CACHE_AREA);
$css = '';
if (!$cache->has(self::TINY_C4L_CSS_CACHE_REV)) {
$componentcssentries = $DB->get_fieldset('tiny_c4l_component', 'css');
$categorycssentries = $DB->get_fieldset('tiny_c4l_compcat', 'css');
$flavorcssentries = $DB->get_fieldset('tiny_c4l_flavor', 'css');
$cssentries = array_merge($categorycssentries, $componentcssentries, $flavorcssentries);
$css = array_reduce($cssentries, fn($current, $add) => $current . PHP_EOL . $add, '');
if (empty($css)) {
$css = '// This file contains the stylesheet for the tiny_c4l plugin.';
}
$clock = \core\di::get(\core\clock::class);
$rev = $clock->time();
$cache->set(self::TINY_C4L_CSS_CACHE_KEY, $css);
$cache->set(self::TINY_C4L_CSS_CACHE_REV, $rev);
} else {
$css = $cache->get(self::TINY_C4L_CSS_CACHE_KEY);
$rev = $cache->get(self::TINY_C4L_CSS_CACHE_REV);
}
return [$css, $rev];
}

public static function purge_css_cache(): void {
$cache = \cache::make('tiny_c4l', self::TINY_C4L_CACHE_AREA);
$cache->delete(self::TINY_C4L_CSS_CACHE_REV);
}
}
32 changes: 32 additions & 0 deletions db/caches.php
Original file line number Diff line number Diff line change
@@ -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/>.

/**
* Cache definitions for tiny_c4l.
*
* @package tiny_c4l
* @copyright 2024 ISB Bayern
* @author Philipp Memmel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

$definitions = [
\tiny_c4l\local\utils::TINY_C4L_CACHE_AREA => [
'mode' => cache_store::MODE_APPLICATION,
],
];
32 changes: 32 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
@@ -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/>.

/**
* Hook callbacks for tiny_c4l.
*
* @package tiny_c4l
* @copyright 2024 ISB Bayern
* @author Philipp Memmel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

$callbacks = [
[
'hook' => \core\hook\output\before_http_headers::class,
'callback' => \tiny_c4l\local\hook_callbacks::class . '::add_c4l_stylesheet_to_dom',
],
];
33 changes: 30 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use tiny_c4l\local\utils;

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

/**
Expand All @@ -33,10 +35,35 @@ function tiny_c4l_user_preferences() {
$preferences = [];

$preferences['c4l_components_variants'] = array(
'type' => PARAM_RAW,
'null' => NULL_NOT_ALLOWED,
'default' => ''
'type' => PARAM_RAW,
'null' => NULL_NOT_ALLOWED,
'default' => ''
);

return $preferences;
}

/**
* Serve the requested file for the tiny_c4l plugin.
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if the file not found, just send the file otherwise and do not return anything
*/
function tiny_c4l_pluginfile(
$course,
$cm,
$context,
string $filearea,
array $args,
bool $forcedownload,
array $options
): bool {
[$css, $rev] = utils::get_complete_css_as_string();
send_file($css, 'tiny_c4l_styles.css?rev=' . $rev, null, 0, true, false, 'text/css');
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
$plugin->release = '2.1.1';
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_STABLE;
$plugin->version = 2024090302;
$plugin->version = 2024091101;

0 comments on commit 217e523

Please sign in to comment.