-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
138 lines (113 loc) · 4.62 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
<?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/>.
/**
* @package local_extendedinfo
* @copyright Nicholas Yang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Returns JSON-decoded Extended Info for specified instance and ID, grabbing from cache if available there.
*
* @param $instance Specify: course, course, or module.
* @param $contextinstanceid ID instance.
* @param $textformat int Specify: FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_WIKI, FORMAT_MARKDOWN, or -1. -1 means to return the raw unformatted value.
* @return array Array of Extended Info values indexed by their names. Null if info for the instance is not found.
*/
function local_extendedinfo_get($instance, $contextinstanceid, $textformat = FORMAT_HTML) {
// Check cache. The key is defined as "instance/contextinstanceid".
$cache = cache::make('local_extendedinfo', 'extendedinfovars');
$cached = $cache->get($instance . '/' . $contextinstanceid);
if ($cached) {
$vars = subvars($cached);
}
// If no cached data found (i.e. caches were purged), then go to DB
else {
global $DB;
$record = $DB->get_record('local_extendedinfo', [ 'instance' => $instance, 'contextinstanceid' => $contextinstanceid ]);
if ($record) {
// Remember to update cache
$cache->set($record->instance . '/' . $record->contextinstanceid, $record->vars);
$vars = subvars($record->vars);
}
}
if (isset($vars)) {
$vars_array = json_decode($vars, true);
// Format (and translate) using format_text()
global $CFG;
if ($textformat != -1) {
foreach ($vars_array as $key => $value) {
$vars_array[$key] = format_text($value, $textformat);
}
}
return $vars_array;
}
return null;
}
/**
* Substitute in your custom variable placeholders.
*
* @param $content String to parse.
* @return $content String with substituted variables.
*/
function subvars($content) {
global $CFG;
$subnames = [ 'wwwroot' ];
for ($x = 0; $x < count($subnames); ++$x) {
$var = $subnames[$x];
// Add more if-else cases here to define more variables
if ($var === 'wwwroot') {
$subbed_var = $CFG->wwwroot;
}
$content = str_replace('[['. $var . ']]', $subbed_var, $content);
}
return $content;
}
/**
* Render a button that links to Extended Info settings page.
*
* @return HTML for a button that links to the associated Extended Info page.
*/
function local_extendedinfo_settings_button() {
if (!is_siteadmin()) {
return;
}
global $CFG, $PAGE;
$instance = '';
if ($PAGE->pagelayout === 'coursecategory') {
$instance = 'category';
// If no ID, then it's the /course/index.php page (list of categories)
$contextinstanceid = (is_object($PAGE->category) ? $PAGE->category->id : -1);
}
else if ($PAGE->pagelayout === 'course' || $PAGE->pagelayout === 'frontpage') {
$instance = 'course';
$contextinstanceid = (is_object($PAGE->course) ? $PAGE->course->id : -1);
}
else if ($PAGE->pagelayout === 'incourse') {
$instance = 'module';
$contextinstanceid = (is_object($PAGE->cm) ? $PAGE->cm->id : false);
}
else if ($PAGE->pagelayout === 'mydashboard') {
// Dashboard has no ID
$instance = 'dashboard';
$contextinstanceid = -1; // -1 allows $contextinstanceid to be true to create link below
}
if ($instance !== '' && $contextinstanceid && $PAGE->pagelayout !== 'admin') {
$eiurl = new moodle_url($CFG->wwwroot . '/local/extendedinfo/edit.php', [ 'instance' => $instance, 'contextinstanceid' => $contextinstanceid, 'returnurl' => $PAGE->url ]);
return '<div class="extendedinfo-btn""><a class="btn btn-primary" href="' . $eiurl . '"><i class="fa fa-list-alt"></i> ' . get_string('pluginname', 'local_extendedinfo') . '</a></div>';
}
return '';
}