/
search_books.php
210 lines (170 loc) · 8.27 KB
/
search_books.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
<?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/>.
/**
* Search books main script.
*
* @package block_search_books
* @copyright 2009 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/glossary/lib.php');
require_once($CFG->libdir.'/searchlib.php');
define('BOOKMAXRESULTSPERPAGE', 100); // Limit results per page.
$courseid = required_param('courseid', PARAM_INT);
$query = required_param('bsquery', PARAM_NOTAGS);
$page = optional_param('page', 0, PARAM_INT);
$bookids = optional_param_array ('check_book', array(), PARAM_INT);
//print_object($bookids);
function search($query, $course, &$bookids, $offset, &$countentries) {
global $CFG, $USER, $DB;
// Perform the search only in books fulfilling mod/book:read and (visible or moodle/course:viewhiddenactivities)
if(empty($bookids)){
$bookids = book_search_get_readble_books( $course );
}
// transform the search query into safe SQL queries
$searchterms = explode(" ",$query);
$parser = new search_parser();
$lexer = new search_lexer( $parser );
if ( $lexer->parse( $query ) ) {
$parsearray = $parser->get_parsed_array();
list($messagesearch, $msparams) =
search_generate_SQL($parsearray, 'bc.title', 'bc.content', null, null, null, null, null, null );
}
// Main query, only to allowed books and not hidden chapters.
$selectsql = "SELECT DISTINCT bc.*";
$fromsql = " FROM {book_chapters} bc, {book} b";
list( $insql, $inparams ) = $DB->get_in_or_equal($bookids, SQL_PARAMS_NAMED );
$params = array_merge( Array( 'courseid' => $course->id ),
$inparams, $msparams);
$wheresql = " WHERE b.course = :courseid
AND b.id $insql
AND bc.bookid = b.id
AND bc.hidden = 0
AND $messagesearch ";
$ordersql = " ORDER BY bc.bookid, bc.pagenum";
// Set page limits.
$limitfrom = $offset;
$limitnum = 0;
if ( $offset >= 0 ) {
$limitnum = BOOKMAXRESULTSPERPAGE;
}
$countentries = $DB->count_records_sql("select count(*) $fromsql $wheresql", $params);
$allentries = $DB->get_records_sql("$selectsql $fromsql $wheresql $ordersql", $params, $limitfrom, $limitnum);
return $allentries;
}
/**
* return a list of book ids for the books which can be read/viewed
*
* @param stdClass $course course object
* @return array of book ids
*/
function book_search_get_readble_books( $course ) {
$bookids = Array();
if (! $books = get_all_instances_in_course('book', $course)) {
notice(get_string('thereareno', 'moodle', get_string('modulenameplural', 'book')), "../../course/view.php?id=$course->id");
die;
}
foreach ($books as $book) {
$cm = get_coursemodule_from_instance("book", $book->id, $course->id);
$context = context_module::instance($cm->id);
if ($cm->visible || has_capability('moodle/course:viewhiddenactivities', $context)) {
if (has_capability('mod/book:read', $context)) {
$bookids[] = $book->id;
}
}
}
return $bookids;
}
//////////////////////////////////////////////////////////
// The main part of this script
$PAGE->set_pagelayout('standard');
$PAGE->set_url($FULLME);
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourseid');
}
require_course_login($course);
$strbooks = get_string('modulenameplural', 'book');
$searchbooks = get_string('bookssearch', 'block_search_books');
$searchresults = get_string('searchresults', 'block_search_books');
$strresults = get_string('results', 'block_search_books');
$ofabout = get_string('ofabout', 'block_search_books');
$for = get_string('for', 'block_search_books');
$seconds = get_string('seconds', 'block_search_books');
$PAGE->navbar->add($strbooks, new moodle_url('/mod/book/index.php', array('id' => $course->id)));
$PAGE->navbar->add($searchresults);
$PAGE->set_title($searchresults);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
$start = (BOOKMAXRESULTSPERPAGE * $page);
// Process the query.
$query = trim(strip_tags($query));
// Launch the SQL quey.
$bookresults = search( $query, $course, $bookids, $start, $countentries);
$coursefield = '<input type="hidden" name="courseid" value="'.$course->id.'"/>';
$pagefield = '<input type="hidden" name="page" value="0"/>';
$searchbox = '<input type="text" name="bsquery" size="20" maxlength="255" value="'.s($query).'"/>';
$submitbutton = '<input type="submit" name="submit" value="'.$searchbooks.'"/>';
$content = $coursefield.$pagefield.$searchbox.$submitbutton;
$form = '<form method="get" action="'.$CFG->wwwroot.'/blocks/search_books/search_books.php" name="form" id="form">'.$content.'</form>';
echo '<div style="margin-left: auto; margin-right: auto; width: 100%; text-align: center">' . $form . '</div>';
// Process $bookresults, if present.
$startindex = $start;
$endindex = $start + count($bookresults);
$countresults = $countentries;
// Print results page tip.
$page_bar = glossary_get_paging_bar($countresults, $page, BOOKMAXRESULTSPERPAGE, "search_books.php?bsquery=".urlencode(stripslashes($query))."&courseid=$course->id&");
// //Iterate over results.
// if (!empty($bookresults)) {
// // Print header
// echo '<p style="text-align: right">'.$strresults.' <b>'.($startindex+1).'</b> - <b>'.$endindex.'</b> '.$ofabout.'<b> '.$countresults.' </b>'.$for.'<b> "'.s($query).'"</b></p>';
// echo $page_bar;
// // Prepare each entry (hilight, footer...)
// echo '<ul>';
// foreach ($bookresults as $entry) {
// $book = $DB->get_record('book', array('id' => $entry->bookid));
// $cm = get_coursemodule_from_instance("book", $book->id, $course->id);
// //To show where each entry belongs to
// $result = "<li><a href=\"$CFG->wwwroot/mod/book/view.php?id=$cm->id\">".format_string($book->name,true)."</a> » <a href=\"$CFG->wwwroot/mod/book/view.php?id=$cm->id&chapterid=$entry->id\">".format_string($entry->title,true)."</a></li>";
// echo $result;
// }
// echo '</ul>';
// echo $page_bar;
// } else {
// echo '<br />';
// echo $OUTPUT->box(get_string("norecordsfound","block_search_books"),'CENTER');
// }
// Iterate over results.
if (!empty($bookresults)) {
// Print header
echo '<span style="float: left">'.$strresults.' <b>'.($startindex+1).'</b> - <b>'.$endindex.'</b> '.$ofabout.'<b> '.$countresults.' </b>'.$for.'<b> "'.s($query).'" </b> from<b> ' . count($bookids) . ' </b>selected book(s)</b></span><br /><br />';
echo $page_bar;
// Prepare each entry (hilight, footer...)
echo '<table id="search_results"><tr><th style="width=70%;">Book</th><th></th><th style="width=70%;">Chapter</th></tr>'; //amendment ul to table
foreach ($bookresults as $entry) {
$book = $DB->get_record('book', array('id' => $entry->bookid));
$cm = get_coursemodule_from_instance("book", $book->id, $course->id);
//To show where each entry belongs to
$result = "<tr><td><a href=\"$CFG->wwwroot/mod/book/view.php?id=$cm->id\">".format_string($book->name,true)."</a></td><td> » </td><td style='padding-left20px;'><a href=\"$CFG->wwwroot/mod/book/view.php?id=$cm->id&chapterid=$entry->id\">".format_string($entry->title,true)."</a></td></tr>";
echo $result;
}
echo '</table>';
echo $page_bar;
} else {
echo '<br />';
echo $OUTPUT->box(get_string("norecordsfound","block_search_books"),'CENTER');
}
echo $OUTPUT->footer();