-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.php
238 lines (178 loc) · 6.26 KB
/
index.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
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
<?php
/**
* Search page (displays the search results)
*
* @package block_search
* @copyright Anthony Kuske <www.anthonykuske.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once '../../config.php';
require_login();
require_capability('block/search:search', context_system::instance());
require_once __DIR__ . '/classes/Block.php';
$searchBlock = new block_search\Block();
$q = optional_param('q', '', PARAM_RAW);
$escapedq = htmlentities($q);
$courseID = optional_param('courseID', 0, PARAM_INT);
$searchInCourse = optional_param('searchInCourse', false, PARAM_BOOL);
$pageNum = optional_param('page', 0, PARAM_INT);
$showHiddenResults = optional_param('showHiddenResults', false, PARAM_BOOL);
$perPage = (int)get_config('block_search', 'results_per_page');
if (!get_config('block_search', 'allow_no_access')) {
$showHiddenResults = false;
}
if ($courseID) {
$PAGE->set_context(context_course::instance($courseID));
} else {
$PAGE->set_context(context_system::instance());
}
$PAGE->requires->jquery();
$PAGE->set_url('/blocks/search', array(
'q' => $q,
'courseID' => $courseID,
'searchInCourse' => $searchInCourse,
'page' => $pageNum,
'showHiddenResults' => $showHiddenResults
));
$PAGE->requires->css('/blocks/search/assets/font-awesome-4.0.3/css/font-awesome.min.css');
$PAGE->requires->css('/blocks/search/assets/css/page.css?v=' . $searchBlock->version());
/**
* Page Title
*/
if (!empty($q)) {
$PAGE->set_title(get_string('search_results_for', 'block_search', $escapedq));
//Log the search (if logging is enabled)
if (get_config('block_search', 'log_searches') == 1) {
// FIXME: add_to_log() has been deprecated, please rewrite your code to the new events API
add_to_log($courseID, 'block_search', 'search', '/blocks/search', $q, 0, $USER->id);
}
} else {
$PAGE->set_title(get_string('pagetitle', $searchBlock->blockName));
}
$PAGE->set_heading(get_string('pagetitle', $searchBlock->blockName));
echo $OUTPUT->header();
echo html_writer::start_tag('div', array('id' => $searchBlock->blockName));
echo $searchBlock->display->showSearchBox($q, $courseID, $searchInCourse, $showHiddenResults);
if (!empty($q)) {
$removeHiddenResults = empty($showHiddenResults) ? true : false;
//Do the search
$results = $searchBlock->search($q, ($searchInCourse ? $courseID : false), $removeHiddenResults);
if (!empty($results['error'])) {
//Show an error
echo $OUTPUT->error_text($results['error']);
} elseif ($results['total'] < 1) {
//There were no results
$icon = html_writer::tag('i', '', array('class' => 'fa fa-info-cirlce'));
echo html_writer::tag(
'div',
$icon . ' ' . get_string('no_results', 'block_search'),
array('class' => 'noResults')
);
if ($courseID) {
//If searching in a course and there are no results,
//suggest trying a full site search.
$fullSearchURL = clone $PAGE->url;
$fullSearchURL->remove_params(array('searchInCourse'));
$icon = html_writer::tag('i', '', array('class' => 'fa fa-hand-o-right'));
$a = html_writer::tag(
'a',
$icon . ' ' . get_string('try_full_search', 'block_search'),
array(
'href' => $fullSearchURL
)
);
echo html_writer::tag(
'div',
$a,
array('class' => 'noResults')
);
}
/*
* Advanced Search Options
*/
echo $searchBlock->display->showAdvancedOptions();
} else {
$icon = html_writer::tag('i', '', array('class' => 'fa fa-list-ul'));
$offset = $pageNum * $perPage;
echo '<p id="showing">';
echo get_string(
'showing',
'block_search',
array(
'start' => number_format($offset + 1),
'end' => number_format(min(($offset + $perPage), $results['total'])),
'total' => number_format($results['total'])
)
);
echo '</p>';
echo html_writer::tag('h2', $icon . ' ' . get_string('search_results', 'block_search'));
/*
* Results menu (on the left)
*/
echo html_writer::start_tag('div', array('class' => 'col left'));
echo $searchBlock->display->showResultsNav($results, $pageNum);
// This is here so the leftcol still has content (and doesn't collapse)
// when the resultsNav becomes position:fixed when scrolling
echo ' ';
echo html_writer::end_tag('div');
/*
* Show results
*/
echo html_writer::start_tag('div', array('id' => 'results', 'class' => 'col right'));
/*
* Results pagination
*/
$pagingbar = new paging_bar($results['total'], $pageNum, $perPage, $PAGE->url);
echo $OUTPUT->render($pagingbar);
echo $searchBlock->display->showResults($results['results'], $pageNum);
echo $OUTPUT->render($pagingbar);
/*
* Advanced Search Options
*/
echo $searchBlock->display->showAdvancedOptions();
echo html_writer::end_tag('div');
}
echo html_writer::tag('div', '', array('class' => 'clear'));
} else {
/*
* Advanced Search Options
*/
echo $searchBlock->display->showAdvancedOptions();
}
/*
* Search Info
*/
if (!empty($results)) {
echo '<div class="searchInfo">';
if (!empty($results['userCached'])) {
echo '<br/>';
echo get_string('user_cached_results_generated', 'block_search', date('Y-m-d H:i:s', $results['generated']));
} else {
if (isset($results['searchTime'])) {
echo get_string('search_took', 'block_search', $results['searchTime']);
}
if (!empty($results['cached'])) {
echo '<br/>';
echo get_string('cached_results_generated', 'block_search', date('Y-m-d H:i:s', $results['generated']));
}
if (isset($results['filterTime'])) {
echo '<br/>';
echo get_string('filtering_took', 'block_search', $results['filterTime']);
}
}
if (!empty($searchBlock->display->displayTime)) {
echo '<br/>';
echo get_string('displaying_took', 'block_search', $searchBlock->display->displayTime);
}
echo '<br/>';
echo 'Memory used: <strong>' . number_format(memory_get_peak_usage() / 1024 / 1024) . 'MB</strong>';
echo '</div>';
}
echo html_writer::end_tag('div');
/*
* Javascript
*/
echo '<script src="' . $searchBlock->getFullURL() . 'assets/js/jquery.scrollTo.min.js?v=' . $searchBlock->version() . '"></script>';
echo '<script src="' . $searchBlock->getFullURL() . 'assets/js/jquery.localScroll.min.js?v=' . $searchBlock->version() . '"></script>';
echo '<script src="' . $searchBlock->getFullURL() . 'assets/js/block_search.js?v=' . $searchBlock->version() . '"></script>';
echo $OUTPUT->footer();