forked from reskit/moodle-tiny_c4l
-
Notifications
You must be signed in to change notification settings - Fork 2
/
management.php
163 lines (148 loc) · 6.17 KB
/
management.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?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/>.
/**
* Management site: create, import and edit components.
*
* @package tiny_c4l
* @copyright 2024 Tobias Garske, ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tiny_c4l\local\utils;
require('../../../../../config.php');
require_login();
$url = new moodle_url('/lib/editor/tiny/plugins/c4l/management.php', []);
$PAGE->set_url($url);
$PAGE->set_context(context_system::instance());
$PAGE->set_heading(get_string('menuitem_c4l', 'tiny_c4l') . ' ' . get_string('management', 'tiny_c4l'));
$compcatactive = optional_param('compcat', '', PARAM_ALPHANUMEXT);
require_capability('tiny/c4l:manage', context_system::instance());
echo $OUTPUT->header();
// Get all c4l components.
$dbcompcats = $DB->get_records('tiny_c4l_compcat');
$dbflavor = $DB->get_records('tiny_c4l_flavor');
$dbcompflavor = $DB->get_records('tiny_c4l_comp_flavor');
$dbcomponent = $DB->get_records('tiny_c4l_component');
$dbvariant = $DB->get_records('tiny_c4l_variant');
$dbcompvariant = $DB->get_records('tiny_c4l_comp_variant');
// Use array_values so mustache can parse it.
$compcats = array_values($dbcompcats);
$flavor = array_values($dbflavor);
$component = array_values($dbcomponent);
$variant = array_values($dbvariant);
// Check if there items without valid component.
// Also already begin formatting as selectors.
$sqlflavor = "SELECT CONCAT('.', f.name, '.flavor') FROM {tiny_c4l_flavor} f
LEFT JOIN {tiny_c4l_comp_flavor} cf ON f.name = cf.flavorname
LEFT JOIN {tiny_c4l_component} c ON cf.componentname = c.name
WHERE cf.id IS NULL OR c.id IS NULL";
$loneflavors = $DB->get_fieldset_sql($sqlflavor);
$sqlvariant = "SELECT CONCAT('.', v.name, '.variant')
FROM {tiny_c4l_variant} v
LEFT JOIN {tiny_c4l_comp_variant} cpv
ON v.name = cpv.variant
WHERE cpv.id IS NULL";
$lonevariants = $DB->get_fieldset_sql($sqlvariant);
$sqlcomponent = "SELECT CONCAT('.', c.name, '.component') FROM {tiny_c4l_component} c
LEFT JOIN {tiny_c4l_compcat} cc ON c.compcat = cc.id
WHERE cc.id IS NULL";
$lonecomponents = $DB->get_fieldset_sql($sqlcomponent);
// Add a compcat to make these accessible.
if ($loneflavors || $lonevariants || $lonecomponents) {
$foundcompcat = [
'id' => 'false',
'name' => 'found-items',
'displayname' => get_string('foundcompcat', 'tiny_c4l'),
'loneflavors' => implode(',', $loneflavors),
'lonevariants' => implode(',', $lonevariants),
'lonecomponents' => implode(',', $lonecomponents),
];
array_unshift($compcats, $foundcompcat);
}
// Add matching compcats to variants.
foreach ($variant as $key => $value) {
$vcompcats = [];
// Select the matching compcats.
$sql = "SELECT compcat
FROM {tiny_c4l_component} c
JOIN {tiny_c4l_comp_variant} cpv
ON c.id = cpv.component
WHERE cpv.variant = :variant";
$vcomps = $DB->get_fieldset_sql($sql, ['variant' => $value->name]);
if (!empty($vcomps)) {
$vcomps = array_unique($vcomps);
// Extract names and write as classes.
[$insql, $inparams] = $DB->get_in_or_equal($vcomps);
$vcompcats = $DB->get_fieldset_select('tiny_c4l_compcat', 'name', "id $insql", $inparams);
$variant[$key]->compcatmatches = implode(' ', $vcompcats);
}
}
// Build component preview images for management, also add compcat.
foreach ($component as $key => $value) {
// Add corresponding flavors.
$flavorsarr = [];
$flavorexamplesarr = [];
foreach ($dbcompflavor as $val) {
if ($val->componentname == $value->name) {
array_push($flavorsarr, $val->flavorname);
if (!empty($val->iconurl)) {
array_push($flavorexamplesarr, utils::replace_pluginfile_urls($val->iconurl, true));
}
}
}
$component[$key]->flavorsarr = $flavorsarr;
if (empty($flavorexamplesarr)) {
$component[$key]->flavorexamplesarr = [utils::replace_pluginfile_urls($value->iconurl, true)];
} else {
$component[$key]->flavorexamplesarr = $flavorexamplesarr;
}
// Keep only the first two entries.
if (count($component[$key]->flavorexamplesarr) > 2) {
$component[$key]->flavorexamplesarr = array_slice($component[$key]->flavorexamplesarr, 0, 2);
}
// Add the compcat name as js selector.
$component[$key]->compcatname = $dbcompcats[$value->compcat]->name;
}
// Add flavor previews.
foreach ($flavor as $key => $value) {
// Look for an example in comp_flavor.
foreach ($dbcompflavor as $val) {
if ($val->flavorname == $value->name) {
if (!empty($val->iconurl)) {
$flavor[$key]->example = utils::replace_pluginfile_urls($val->iconurl, true);
continue;
}
}
}
}
// Use empty array to create an add item.
$addentry = [];
array_push($compcats, $addentry);
array_push($flavor, $addentry);
array_push($component, $addentry);
array_push($variant, $addentry);
// Add exportlink.
$exportlink = \moodle_url::make_pluginfile_url(SYSCONTEXTID, 'tiny_c4l', 'export', null, '/', 'tiny_c4l_export.xml')->out();
$params = new \stdClass();
$params->compcatactive = $compcatactive;
$PAGE->requires->js_call_amd('tiny_c4l/management', 'init', [$params]);
echo($OUTPUT->render_from_template('tiny_c4l/management', [
'compcats' => $compcats,
'flavor' => $flavor,
'component' => $component,
'variant' => $variant,
'exportlink' => $exportlink,
]));
echo $OUTPUT->footer();