Skip to content

Commit

Permalink
Added pre and postprocessing to Search results to make them more read…
Browse files Browse the repository at this point in the history
…able and neat.
  • Loading branch information
st143971 committed Feb 12, 2024
1 parent 94de320 commit 2fd8510
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 78 deletions.
3 changes: 2 additions & 1 deletion block_slidefinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ public function get_content() {
}
}

// data[0] = array([section, filename, page, bookurl, size, content]).
$data = [[], []];
if (!is_null($course)) {
$data = block_slidefinder_get_content_as_chapters_for_all_book_pdf_matches_from_course($course->id, $USER->id);
$data = block_slidefinder_get_all_content_of_course_as_sections_with_metadata($course->id, $USER->id);
if (!empty($data[1])) {
$footer .= get_string('misconfigured_info', get_class($this));
foreach ($data[1] as $key => $value) {
Expand Down
58 changes: 36 additions & 22 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,24 @@ public static function get_searched_locations($userid, $courseid, $searchstring,
$coursecontext = context_course::instance($course->id);
self::validate_context($coursecontext);

[$chapters, $misconfiguredchapters] =
block_slidefinder_get_content_as_chapters_for_all_book_pdf_matches_from_course($courseid, $userid);
// Get all searchable content.
[$sections, $_] = block_slidefinder_get_all_content_of_course_as_sections_with_metadata($courseid, $userid);

// Get Search Results & Context for PDFs.
$data = [];
foreach ($sections as $section) {
$data = self::search_content($data, $section, $searchstring, $contextlength);
}

// Format results.
$results = [];
foreach ($chapters as $chapter) {
$result = self::search_content($chapter, $searchstring, $contextlength);
if ($result) {
foreach ($data as $file) {
foreach ($file as $chapter) {
$results[] = [
'filename' => $result->filename,
'page_number' => $result->page,
'book_chapter_url' => $result->bookurl,
'context_snippet' => $result->context,
'filename' => $chapter->filename,
'page_number' => $chapter->page,
'book_chapter_url' => $chapter->bookurl,
'context_snippet' => $chapter->context,
];
}
}
Expand All @@ -131,29 +136,29 @@ public static function get_searched_locations($userid, $courseid, $searchstring,
}

/**
* Returns description of the method return values
* Returns description of the method return values.
* @return external_value
*/
public static function get_searched_locations_returns() {
return new external_value(PARAM_TEXT, 'Search results', VALUE_REQUIRED);
}

/**
* Searches for the $searchterm in the given $page->content and
* returns the page with a $page->context context snippet if it was found. returns null if not.
* Searches for the $searchterm in the given $section->content and populates the given $results array.
*
* @param stdClass $page object that holds the $page->content and gets returned containing the $page->context
* @param string $searchterm the string to seach for in the $page->content
* @param int $contextlength word count returned as context snippet on each side of the found $searchterm
* @param array $results the results so far.
* @param stdClass $section object that holds the $section->content.
* @param string $searchterm the string to seach for in the $section->content.
* @param int $contextlength word count returned as context snippet on each side of the found $searchterm.
*
* @return stdClass|null the given $page object with the additional $page->context or null if nothing was found
* @return array $results returns the updated $results array with the new data.
*/
private static function search_content($page, $searchterm, $contextlength) {
$content = $page->content;
private static function search_content($results, $section, $searchterm, $contextlength) {
$content = $section->content;

// Is the searched word in this page?
// Is the searched word in this section?
if (!stristr($content, $searchterm)) {
return;
return $results;
}

// Split the text into words.
Expand Down Expand Up @@ -201,9 +206,18 @@ private static function search_content($page, $searchterm, $contextlength) {

// Create a String with all occurences & context.
$context = implode(' ... ', $snippets);
$section->context = $context;

if (!array_key_exists($section->filename, $results)) {
$results[$section->filename] = [];
}
if (!array_key_exists($section->page, $results[$section->filename])) {
$results[$section->filename][$section->page] = $section;
} else {
$results[$section->filename][$section->page]->context .= " ... " . $section->context;
}

$page->context = $context;
return $page;
return $results;
}

/**
Expand Down
Loading

0 comments on commit 2fd8510

Please sign in to comment.