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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
<?php
namespace app\models\cms;
use \Registry;
class Course_model {
/** COURSES
* -------------------------------------------------------------------------
*/
/** get all categories
*
* raw table data (simplest SELECT)
*
*/
public static function get_categories()
{
return Registry::use('database')->runQuery(
"SELECT * FROM course ORDER BY label",
[]
);
}
/** courses struct
*
* a super array with almost any info needed about courses
*
* @return (array) ['tree' => ..., 'breadcrumbs' => ... ]
*/
public static function courses_struct()
{
$tree = self::category_tree();
return [
'tree' => $tree,
'breadcrumbs' => self::breadcrumbs($tree)
];
}
/** constuct a category_tree
*
* returns a tree representation of the categories
*
* NOTE:
* category_tree() is an expensive method;
* it calls 2 other methods implementing recursive algorithms
* thus it uses many sources to run (particularly RAM).
* Caching the result is strogly recommended.
*
*/
public static function category_tree()
{
$categories = self::get_categories(); // get all categories
$tree = self::to_tree($categories); // format to a tree
$tree_wParents = self::tree_parents($tree); // add parents section for each tree-node
return $tree_wParents;
}
/** to_tree
*
* constructs a tree from raw-table data;
* this is a private method and uses a recursive algorithm
*
* @param $dataset (array): flar array of records with id/parent-id pairs
* @return $root (array): id of root category
*
* (**) each node has 2 parts:
* .... .. rec : all record attributes/data as passed into $dataset
* .... .. childs : array of (children) nodes
*/
private static function to_tree($dataset, $root = 0)
{
$return = [];
// loop data ; search for direct children of root
foreach($dataset as $key => $rec) {
$child = $rec['id'];
$parent = $rec['parent_id'];
if ($parent == $root) { // a direct child is found
unset($dataset[$key]); // remove item (no need to traverse again)
// Append the child into result array ; parse its children
$return[] = [
'rec' => [
'id' => $rec['id'],
'label' => $rec['label'],
'order' => $rec['order'],
'parent' => $rec['parent_id']
],
'childs' => self::to_tree($dataset, $child) // recursively
];
}
}
return empty($return) ? [] : $return;
}
/** tree_parents
*
* adds a section to each tree node with all parents of each node
*
* @param $tree (array) : nodes array (each node has `rec` and `childs` sections )
* @param $parents (array); DO NOT SET IT (takes values automaticaly)
* @return array of nodes with an extra node[parents] section
*
*/
private static function tree_parents($tree, $parents = [])
{
$tree_with_parents = [];
foreach($tree as $key => $node) {
// parents to be pushed for node's children
$push_parents = $parents; // parents so far
$push_parents[] = $node['rec']; // this record will be a new parent
$tree_with_parents[$key] = [
'rec' => $node['rec'],
'parents' => $parents,
'childs' => ($node['childs'] == [])
? []
: self::tree_parents($node['childs'], $push_parents)
];
}
return $tree_with_parents;
}
/** all_breadcrumbs
* -------------------------------------------------------------------------
*
* returns an array of all breadcrumbs
* where array-key of each record is category[id]
*
* NOTE:
* ---
* Course_model::all_breadcrumbs returns an indexed super-array;
* each array item includes a banch of information: [
* breadcrumb,
* rec: [ id , title ],
* parents: [ [id, title] , ... ]
* childs: [ [id, title] , ... ],
* level
* ]
*
* Use Cases:
* ---
* as a super-array, the output can be used in many cases
* for example...
* into <select-option> form elements
* .. while selecting category for a post
* .. or editing a category
* or directry referring to category's parents/childs
*
* Arguments:
* ---
* @param $tree (array) : category tree (with childs and parents parts)
* @param $detimiter (string, optional) : string to split breadcrumb's path-nodes
* @param $exception (int, optional) : id of category to exclude (subcategories shall be excluded too)
* @param $l (int, not-pass) : depth level of the node; DO NOT SET (takes values automaticaly)
* @return array of breadcrumbs
* -------------------------------------------------------------------------
*/
static public function breadcrumbs($tree, $delimiter = " / ", $exception = 0, $l = 0)
{
$all = []; // results array
foreach($tree as $node) { // loop through all nodes
if (intval($node['rec']['id']) != $exception) { // if node is not exception
// construct breadcrumb html of node
// --- -- -- - - -
$breadcrumb = "";
foreach($node['parents'] as $par) { // first: join path titles
$breadcrumb .= $par['label'] . $delimiter;
}
$breadcrumb .= $node['rec']['label']; // last: append title
// make a new super record
// --- -- -- - - -
$all[$node['rec']['id']] = [ // set record is as key
'breadcrumb' => $breadcrumb, // add breadcrump to results
'rec' => $node['rec'], // + node info
'parents' => $node['parents'], // + parents array
'childs' => self::first_level_childs($node), // + direct childs
'level' => $l // + level
];
// recursively traverse children nodes
// --- -- -- - - -
if (isset($node['childs']) && $node['childs'] != []) {
$child_breadcrumbs = self::breadcrumbs(
$node['childs'],
$delimiter,
$exception,
$l+1
);
$all = $all + $child_breadcrumbs; // concatenate arrays (keep array-keys)
}
}
}
return $all;
}
/** first_level_childs
* --- -- -- - - -
* used by all_breadcrumbs()
*/
static private function first_level_childs($node)
{
$childs = [];
if ($node['childs'] == []) {
return [];
}
foreach($node['childs'] as $key => $kid) {
$childs[] = [
'id' => $kid['rec']['id'],
'label' => $kid['rec']['label']
];
}
return $childs;
}
/** LESSONS
* -------------------------------------------------------------------------
*/
/** lessons of course
*
* @param $id (int) : course_id
*/
public static function lessons_of_course($id)
{
// TODO: order results in some way
return Registry::use('database')->runQuery(
"SELECT lesson.*, lesson_privilege.privilege_id FROM lesson
LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id
WHERE lesson.status = 1 AND lesson.course_id = :id
ORDER BY lesson_privilege.privilege_id ASC",
[':id' => $id]
);
}
/** lesson
*
* @param $id (int) : lesson id
*/
public static function lesson($id)
{
// TODO: order results in some way
return Registry::use('database')->query(
"SELECT lesson.*, lesson_privilege.privilege_id FROM lesson
LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id
WHERE lesson.status = 1 AND lesson.id = :id",
[':id' => $id]
)->getFirst();
}
/** files
* return all files
* @param void
* @return array
*/
public static function files()
{
return Registry::use('database')->runQuery("SELECT * from media", []);
}
}
|