Moodle Global Search inside Books
Saturday, September 4th, 2010In order to inculude Books in the Global Search in Moodle complete the following steps:
add the following lines to /search/lib.php just after the initial Global declerations
define('SEARCH_TYPE_BOOK', 'book');
define('PATH_FOR_SEARCH_TYPE_BOOK', 'mod/book');
Then copy /search/documents/data_document.php and rename to /search/documents/book_document.php
replace all variable names of “data” with “label” (watch out for plurals)
Need to use moodles get_context_instance_by_id function in order to return the correct instance ID so that you can link directly to the resource
add the following line to the __construct function
$context = get_context_instance_by_id($context_id);
and then change the $doc->url line to this:
$doc->url = book_make_link($context->instanceid);
This will return the instance id of the book so that the user can click directly into the book, further work needs to be done in order to return the subchapter of the book so that the user can click directly into the actual page / subchapter within the book to see the results.
To get a full search in all content create a new function like below and use its return to put content into the $doc->contents variable.
function get_book_content($book_id){
$sql_get_book_contents = "SELECT bc.title, bc.content FROM mdl_book_chapters bc,
mdl_book b WHERE
bc.bookid = $book_id AND
bc.hidden = 0
ORDER BY bc.bookid, bc.pagenum";
$book_contents_array = get_records_sql($sql_get_book_contents);
foreach ($book_contents_array as $book_temp){
foreach ($book_temp as $book_temp_item){
$book_full_content .= $book_temp_item;
}
}
return strip_tags($book_full_content);
}
test that your new document class is ok by running /search/tests/index.php
if all ok then rebuild index /search/indexersplash.php then go and put the kettle on as it can take over 10 mins depending on size of your moodle
then run a few tests seeing if any books are returned!
Let me know if this works for you and any tweaks to the code are more than welcome.