Skip to content

Commit 9577c71

Browse files
committed
add missing builtin snippets to database
1 parent 5b8c3de commit 9577c71

File tree

7 files changed

+135
-48
lines changed

7 files changed

+135
-48
lines changed

classes/snippets.php

+87-37
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,48 @@ class snippets {
4141
const SNIPPET_HEADERS = [
4242
'Snippet Title',
4343
'Goal',
44-
'Domain',
4544
'Description',
4645
'Scope',
4746
];
4847

48+
/**
49+
* Base path CSS snippets that are shipped with boost_union.
50+
* @var string
51+
*/
52+
const BUILTIN_SNIPPETS_BASE_PATH = '/theme/boost_union/snippets/builtin/';
53+
4954
/**
5055
* Gets the snippet file based on the meta information.
51-
* @param mixed $key
56+
*
57+
* @param mixed $path
5258
* @param mixed $source
53-
* @param mixed $domain
59+
*
5460
* @return string|null
5561
*/
56-
public static function get_snippet_file($key, $source, $domain) {
62+
public static function get_snippet_file($path, $source) {
5763
global $CFG;
64+
// Get the snippet file based on the different sources.
5865
if ('theme_boost_union' === $source) {
59-
$file = $CFG->dirroot . sprintf('/theme/boost_union/snippets/builtin/%s.scss', $key);
66+
// Builtin CSS SNippets.
67+
$file = $CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . $path;
6068
} else {
61-
// TODO: other sources.
69+
// Other snippet sources.
6270
return null;
6371
}
64-
6572
return is_readable($file) ? $file : null;
6673
}
6774

6875
/**
6976
* Loads the Snippets SCSS content.
7077
*
71-
* @param mixed $key
78+
* @param mixed $path
7279
* @param mixed $source
73-
* @param mixed $domain
80+
*
7481
* @return boolean|string
7582
*/
76-
public static function get_snippet_scss($key, $source, $domain = '') {
77-
// Get the snippets file, based on the source and domain.
78-
$file = self::get_snippet_file($key, $source, $domain);
83+
public static function get_snippet_scss($path, $source) {
84+
// Get the snippets file, based on the source.
85+
$file = self::get_snippet_file($path, $source);
7986

8087
if (is_null($file)) {
8188
return null;
@@ -86,16 +93,17 @@ public static function get_snippet_scss($key, $source, $domain = '') {
8693
}
8794

8895
/**
89-
* Get a snippet defined in the code based on key and domain.
90-
* @param string $key
91-
* @param string $domain
96+
* Get a snippet defined in the code based on path.
97+
*
98+
* @param string $path
99+
*
92100
* @return mixed
93101
*/
94-
public static function get_snippet_meta($key, $source, $domain = '') {
102+
public static function get_snippet_meta($path, $source) {
95103
global $CFG;
96104

97-
// Get the snippets file, based on the source and domain.
98-
$file = self::get_snippet_file($key, $source, $domain);
105+
// Get the snippets file, based on the source.
106+
$file = self::get_snippet_file($path, $source);
99107

100108
if (is_null($file)) {
101109
return;
@@ -119,14 +127,16 @@ public static function get_snippet_meta($key, $source, $domain = '') {
119127

120128
/**
121129
* Compose snippets file data to record.
130+
*
122131
* @param mixed $data
132+
*
123133
* @return array
124134
*/
125135
public static function compose_snippets_data($snippetrecordset) {
126136
$snippets = [];
127137

128138
foreach ($snippetrecordset as $snippetrecord) {
129-
$snippet = self::get_snippet_meta($snippetrecord->key, $snippetrecord->source);
139+
$snippet = self::get_snippet_meta($snippetrecord->path, $snippetrecord->source);
130140
if ($snippet) {
131141
$snippets[] = (object) array_merge((array) $snippetrecord, (array) $snippet);
132142
}
@@ -137,6 +147,7 @@ public static function compose_snippets_data($snippetrecordset) {
137147

138148
/**
139149
* Checks which snippets are active and returns their css.
150+
*
140151
* @return string
141152
*/
142153
public static function get_enabled_snippet_css() {
@@ -151,21 +162,22 @@ public static function get_enabled_snippet_css() {
151162
// Get records.
152163
$data = $DB->get_recordset_sql($sql);
153164

154-
$css = '';
165+
$scss = '';
155166

156167
foreach ($data as $snippet) {
157-
if ($snippet->enabled) {
158-
$css .= self::get_snippet_scss($snippet->key, $snippet->domain)['css'] . ' ';
159-
}
168+
$scss .= self::get_snippet_scss($snippet->path, $snippet->source);
160169
}
161170

162-
return $css;
171+
$data->close();
172+
173+
return $scss;
163174
}
164175

165176
/**
166177
* Strips close comment and close php tags from file headers.
167178
*
168179
* @param string $str Header comment to clean up.
180+
*
169181
* @return string
170182
*/
171183
private static function cleanup_header_comment($str) {
@@ -186,7 +198,7 @@ private static function cleanup_header_comment($str) {
186198
*
187199
* @return string[] Array of file header values keyed by header name.
188200
*/
189-
public static function get_snippet_meta_from_file($file,) {
201+
public static function get_snippet_meta_from_file($file) {
190202
// Pull only the first 8 KB of the file in.
191203
$filedata = file_get_contents( $file, false, null, 0, 8 * self::KB_IN_BYTES );
192204

@@ -211,17 +223,55 @@ public static function get_snippet_meta_from_file($file,) {
211223
return $headers;
212224
}
213225

214-
// /**
215-
// * Checks if there are any snippets not tracked in the database.
216-
// *
217-
// * If they are not they will be added to the database.
218-
// *
219-
// * @param moodle_recordset $data The snippets dataset currently present in the DB.
220-
// *
221-
// * @return void
222-
// */
223-
// public static function check_for_missing_snippets_int_the_database($data) {
224-
225-
// }
226+
/**
227+
* Retrieve all builtin CSS snippets via the actual scss files.
228+
*
229+
* @return string[]
230+
*/
231+
private static function get_builtin_snippet_paths() {
232+
global $CFG;
233+
// Get an array of all .scss files in the directory.
234+
$files = glob($CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . '*.scss');
235+
236+
// Get the basenames.
237+
$basenames = array_map(fn($file) => basename($file), $files);
238+
239+
return $basenames;
240+
}
241+
242+
/**
243+
* Make sure builtin snippets are in the database.
244+
*
245+
* @return void
246+
*/
247+
public static function add_builtin_snippets() {
248+
global $DB;
249+
$paths = self::get_builtin_snippet_paths();
250+
251+
$present = $DB->get_records(
252+
'theme_boost_union_snippets',
253+
['source' => 'theme_boost_union'],
254+
'sortorder DESC',
255+
'id,path,sortorder',
256+
0,
257+
0,
258+
);
259+
260+
$sortorder = empty($present) ? 0 : $present[0]->sortorder + 1;
261+
262+
foreach ($paths as $path) {
263+
if (!array_key_exists($path, $present)) {
264+
$DB->insert_record(
265+
'theme_boost_union_snippets',
266+
[
267+
'path' => $path,
268+
'source' => 'theme_boost_union',
269+
'sortorder' => $sortorder,
270+
]
271+
);
272+
$sortorder += 1;
273+
}
274+
}
275+
}
226276

227277
}

classes/table/snippets_overview.php

-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ public function query_db($pagesize, $useinitialsbar = true) {
262262
// Get records.
263263
$data = $DB->get_recordset_sql($sql);
264264

265-
// snippets::check_for_missing_snippets_int_the_database($data);
266-
267265
$this->rawdata = snippets::compose_snippets_data($data);
268266
}
269267

db/install.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Install script for Boost Union
19+
*
20+
* Documentation: {@link https://moodledev.io/docs/guides/upgrade}
21+
*
22+
* @package theme_boost_union
23+
* @copyright 2024 University of Graz
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
27+
use theme_boost_union\snippets;
28+
29+
/**
30+
* Executed on installation of Boost Union
31+
*
32+
* @return bool
33+
*/
34+
function xmldb_theme_boost_union_install() {
35+
snippets::add_builtin_snippets();
36+
return true;
37+
}

db/install.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@
103103
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
104104
<FIELD NAME="sortorder" TYPE="int" LENGTH="18" NOTNULL="false" SEQUENCE="false"/>
105105
<FIELD NAME="source" TYPE="char" LENGTH="255" NOTNULL="false" DEFAULT="theme_boost_union" SEQUENCE="false"/>
106-
<FIELD NAME="domain" TYPE="char" LENGTH="255" NOTNULL="false" DEFAULT="" SEQUENCE="false"/>
107-
<FIELD NAME="key" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
106+
<FIELD NAME="path" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
108107
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
109108
</FIELDS>
110109
<KEYS>

db/upgrade.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2424
*/
2525

26+
use theme_boost_union\snippets;
27+
2628
/**
2729
* Function to upgrade theme_boost_union
2830
* @param int $oldversion the version we are upgrading from
@@ -324,16 +326,15 @@ function xmldb_theme_boost_union_upgrade($oldversion) {
324326
upgrade_plugin_savepoint(true, 2023102027, 'theme', 'boost_union');
325327
}
326328

327-
if ($oldversion < 2024060101) {
329+
if ($oldversion < 2024060102) {
328330
// Define table theme_boost_union_snippets to be created.
329331
$table = new xmldb_table('theme_boost_union_snippets');
330332

331333
// Adding fields to table theme_boost_union_snippets.
332334
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
333-
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '18', null, null, null, null);
335+
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '18', null, XMLDB_NOTNULL, null, null);
334336
$table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, 'theme_boost_union');
335-
$table->add_field('domain', XMLDB_TYPE_CHAR, '255', null, null, null, '');
336-
$table->add_field('key', XMLDB_TYPE_CHAR, '255', null, null, null, null);
337+
$table->add_field('path', XMLDB_TYPE_CHAR, '255', null, null, null, null);
337338
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
338339

339340
// Adding keys to table theme_boost_union_snippets.
@@ -345,8 +346,10 @@ function xmldb_theme_boost_union_upgrade($oldversion) {
345346
}
346347

347348
// Boost_union savepoint reached.
348-
upgrade_plugin_savepoint(true, 2024060101, 'theme', 'boost_union');
349+
upgrade_plugin_savepoint(true, 2024060102, 'theme', 'boost_union');
349350
}
350351

352+
snippets::add_builtin_snippets();
353+
351354
return true;
352355
}

snippets/builtin/bigger_title.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
*/
1313

1414
h1 {
15-
font-size: 70px;
15+
color: violet;
1616
}

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'theme_boost_union';
28-
$plugin->version = 2024060101;
28+
$plugin->version = 2024060109;
2929
$plugin->release = 'v4.4-r1';
3030
$plugin->requires = 2024042200;
3131
$plugin->supported = [404, 404];

0 commit comments

Comments
 (0)