-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlib.php
155 lines (144 loc) · 5.16 KB
/
lib.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
<?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/>.
/**
* local pages
*
* @package local_pages
* @author Kevin Dibble
* @copyright 2017 LearningWorks Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
*
* Extend page navigation
*
* @param global_navigation $nav
*/
function local_pages_extends_navigation(global_navigation $nav) {
return local_pages_extend_navigation($nav);
}
/**
*
* Get saved files for the page
*
* @param mixed $course
* @param mixed $birecordorcm
* @param mixed $context
* @param mixed $filearea
* @param mixed $args
* @param bool $forcedownload
* @param array $options
*/
function local_pages_pluginfile($course, $birecordorcm, $context, $filearea, $args, $forcedownload, array $options = array()) {
$fs = get_file_storage();
$filename = array_pop($args);
$filepath = $args ? '/' . implode('/', $args) . '/' : '/';
if (!$file = $fs->get_file($context->id, 'local_pages', 'pagecontent', 0, $filepath, $filename) or $file->is_directory()) {
send_file_not_found();
}
\core\session\manager::write_close();
send_stored_file($file, null, 0, $forcedownload, $options);
}
/**
*
* Build the menu for the page
*
* @param navigation_node $nav
* @param mixed $parent
*/
function local_pages_build_menu(navigation_node $nav, $parent) {
global $DB;
$today = date('U');
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND onmenu=1 " .
"AND pagetype='page' AND pageparent=? AND pagedate <=? " .
"ORDER BY pageorder", array($parent, $today));
local_pages_process_records($records, $nav);
}
/**
*
* Process records for pages
*
* @param mixed $records
* @param mixed $nav
* @param bool $parent
*/
function local_pages_process_records($records, $nav, $parent = false) {
global $CFG;
if ($records) {
foreach ($records as $page) {
$canaccess = true;
if (isset($page->accesslevel) && stripos($page->accesslevel, ":") !== false) {
$canaccess = false;
$levels = explode(",", $page->accesslevel);
$context = context_system::instance();
foreach ($levels as $level) {
if ($canaccess != true) {
if (stripos($level, "!") !== false) {
$level = str_replace("!", "", $level);
$canaccess = has_capability(trim($level), $context) ? false : true;
} else {
$canaccess = has_capability(trim($level), $context) ? true : false;
}
}
}
}
if ($canaccess) {
$urllocation = new moodle_url($CFG->wwwroot . '/local/pages/', array('id' => $page->id));
if (get_config('local_pages', 'cleanurl_enabled') && trim($page->menuname) != '') {
$urllocation = new moodle_url($CFG->wwwroot . '/local/pages/' . $page->menuname);
}
$child = $nav->add(
$page->pagename,
$urllocation,
navigation_node::TYPE_CONTAINER,
new pix_icon('white-info-circle', $page->pagename, 'local_pages')
);
if ($parent) {
$child->set_parent($nav);
}
local_pages_build_menu($child, $page->id);
}
}
}
}
/**
*
* Extend navigation to show the pages in the navigation block
*
* @param global_navigation $nav
*/
function local_pages_extend_navigation(global_navigation $nav) {
global $CFG, $DB;
$context = context_system::instance();
$pluginname = get_string('pluginname', 'local_pages');
if (has_capability('local/pages:addpages', $context)) {
$mainnode = $nav->add(
get_string('pagesplugin', 'local_pages'),
new moodle_url($CFG->wwwroot . "/local/pages/pages.php"),
navigation_node::TYPE_CONTAINER,
'local_pages',
'local_pages',
new pix_icon('newspaper', $pluginname, 'local_pages')
);
$mainnode->nodetype = 0;
$mainnode->showinflatnavigation = true;
}
$today = date('U');
$records = $DB->get_records_sql("SELECT * FROM {local_pages} WHERE deleted=0 AND onmenu=1 " .
"AND pagetype='page' AND pageparent=0 AND pagedate <= ? ORDER BY pageorder", array($today));
local_pages_process_records($records, $nav, false, $nav);
}