Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #10

Merged
merged 2 commits into from
Dec 23, 2023
Merged

Dev #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions block_slidefinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public function get_content() {

// Restructure (for mustache) the name=>value list into a list of array objects having the name and value attribute.
$hiddenparams = array_map(function ($name, $value) {
return array("name" => $name, "value" => $value);
return ["name" => $name, "value" => $value];
}, array_keys($hiddenparams), $hiddenparams);

if ($cid == 0) { // My Moodle Page.
if ($slidefinderid != 0) {
// Course.
if (!$course = $DB->get_record('course', array('id' => $slidefinderid))) {
if (!$course = $DB->get_record('course', ['id' => $slidefinderid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
}
// Does the user have access to the course?
Expand All @@ -90,11 +90,11 @@ public function get_content() {
'action' => $this->page->url,
'course_selector_param_name' => BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM,
'course_selector_options' => block_slidefinder_select_course_options($slidefinderid),
'hidden_params' => $hiddenparams
'hidden_params' => $hiddenparams,
]);
} else { // Course Page.
// Course.
if (!$course = $DB->get_record('course', array('id' => $cid))) {
if (!$course = $DB->get_record('course', ['id' => $cid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
}
// Does the user have access to the course?
Expand Down Expand Up @@ -125,7 +125,7 @@ public function get_content() {
'search_term' => $search,
'chapter_label' => get_string('chapter', get_class($this)),
'content' => base64_encode(json_encode($data[0])),
'hidden_params' => $hiddenparams
'hidden_params' => $hiddenparams,
]);
} catch (\Throwable $th) {
debugging($th);
Expand Down
24 changes: 12 additions & 12 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
*/
defined('MOODLE_INTERNAL') || die();

$capabilities = array(
'block/slidefinder:myaddinstance' => array(
$capabilities = [
'block/slidefinder:myaddinstance' => [
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'archetypes' => [
'guest' => CAP_ALLOW,
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
],
'clonepermissionsfrom' => 'moodle/my:manageblocks',
],

'block/slidefinder:addinstance' => array(
'block/slidefinder:addinstance' => [
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'archetypes' => [
'user' => CAP_PREVENT,
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
);
],
'clonepermissionsfrom' => 'moodle/site:manageblocks',
],
];
10 changes: 5 additions & 5 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
defined('MOODLE_INTERNAL') || die();

$functions = array(
$functions = [
// Info: local_PLUGINNAME_FUNCTIONNAME is the name of the web service function that the client will call.
'block_slidefinder_get_searched_locations' => array(
'block_slidefinder_get_searched_locations' => [

// Class containing the external function.
'classname' => 'block_slidefinder_external',
Expand Down Expand Up @@ -55,6 +55,6 @@
// Optional, only available for Moodle 3.1 onwards.
// List of built-in services (by shortname) where the function will be included.
// Services created manually via the Moodle interface are not supported.
'services' => array()
)
);
'services' => [],
],
];
86 changes: 48 additions & 38 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class block_slidefinder_external extends external_api {
*/
public static function get_searched_locations_parameters() {
return new external_function_parameters(
array(
[
'userid' => new external_value(
PARAM_INT,
'Id of the user using the webservice',
Expand All @@ -56,8 +56,8 @@ public static function get_searched_locations_parameters() {
'contextlength' => new external_value(
PARAM_INT,
'Number of words surrounding the found query word in each direction'
)
)
),
]
);
}

Expand All @@ -67,7 +67,7 @@ public static function get_searched_locations_parameters() {
* @param int $userid id of the user who initiates the search
* @param int $courseid id of the course to search in
* @param string $searchstring the string to search for
* @param int $contextlength the size of the context snippet on each side of the found $seach_string occurences in words
* @param int $contextlength the size of the context snippet on each side of the found $search_string occurences in words
*
* @return string json encoded array of arrays holding the 'filename', 'page_number', 'book_chapter_url' and 'context'
* of each chapter/pdf-page the $searchterm was found
Expand All @@ -80,23 +80,21 @@ public static function get_searched_locations($userid, $courseid, $searchstring,
// Validate parameter.
$params = self::validate_parameters(
self::get_searched_locations_parameters(),
array(
[
'userid' => $userid,
'courseid' => $courseid,
'searchstring' => $searchstring,
'contextlength' => $contextlength
)
'contextlength' => $contextlength,
]
);

$transaction = $DB->start_delegated_transaction();

try {
// User.
if (!$user = $DB->get_record('user', array('id' => $userid))) {
if (!$user = $DB->get_record('user', ['id' => $userid])) {
throw new moodle_exception(get_string('error_user_not_found', 'block_slidefinder'));
}
// Course.
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
}
// Does the user have access to the course?
Expand All @@ -115,15 +113,15 @@ public static function get_searched_locations($userid, $courseid, $searchstring,
block_slidefinder_get_content_as_chapters_for_all_book_pdf_matches_from_course($courseid, $userid);

// Get Search Results & Context for PDFs.
$results = array();
$results = [];
foreach ($chapters as $chapter) {
$result = self::search_content($chapter, $searchstring, $contextlength);
if ($result) {
$results[] = [
'filename' => $result->filename,
'page_number' => $result->page,
'book_chapter_url' => $result->bookurl,
'context_snippet' => $result->context
'context_snippet' => $result->context,
];
}
}
Expand Down Expand Up @@ -151,46 +149,58 @@ public static function get_searched_locations_returns() {
* @return stdClass|null the given $page object with the additional $page->context or null if nothing was found
*/
private static function search_content($page, $searchterm, $contextlength) {
$content = ' ' . $page->content . ' ';
$content = $page->content;

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

// Create a String with all occurences & context.
$context = '';
// Split the text into words.
$words = preg_split('/\s+/', $content);

$index = self::index_of($content, $searchterm, 0);
$finalendindex = -1;
$snippets = [];
$snippetindex = 0;

// For all $searchterm occurances.
while (0 <= $index) {
$startindex = $index;
$tempendindex = $index;
// Iterate through the words to find occurrences of the search word.
// Save the context snippet indices.
for ($i = 0; $i < count($words); $i++) {
if (stristr($words[$i], $searchterm)) {
// Calculate start and end indices for the context.
$start = max(0, $i - $contextlength);
$end = min(count($words) - 1, $i + $contextlength);

// Get Context Words.
for ($i = 0; $i < $contextlength; $i++) {
$startindex = self::lastindex_of($content, ' ', $startindex - 1);
$tempendindex = self::index_of($content, ' ', $tempendindex + 1);
if ($tempendindex < $startindex) {
$tempendindex = strlen($content) - 1;
if ($snippetindex > 0 && $start - $snippets[$snippetindex - 1][1] < $contextlength) {
$snippets[$snippetindex - 1][1] = $end;
} else {
$snippets[] = [$start, $end];
$snippetindex++;
}
}
}

// Turn the snippet indices into actual text snippets.
for ($i = 0; $i < count($snippets); $i++) {
[$start, $end] = $snippets[$i];
// Extract the context around the search word.
$snippet = implode(' ', array_slice($words, $start, $end - $start + 1));

// Do the contexti have overlap or are they apart?
if ($startindex > $finalendindex) {
$context .= '...';
$context .= self::substring($content, $startindex, $tempendindex);
} else {
$context .= self::substring($content, $finalendindex + 1, $tempendindex);
// Add "..." at the beginning if not at the start of the text.
if ($start > 0) {
$snippet = '...' . $snippet;
}

// Next $searchterm occurance.
$finalendindex = $tempendindex;
$index = self::index_of($content, $searchterm, $index + 1);
// Add "..." at the end if not at the end of the text.
if ($end < count($words) - 1) {
$snippet .= '...';
}

// Update snippet with text.
$snippets[$i] = $snippet;
}
$context .= '...';

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

$page->context = $context;
return $page;
Expand Down
1 change: 1 addition & 0 deletions lang/de/block_slidefinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$string['chapter'] = 'Kapitel';
$string['misconfigured_info'] = 'Die folgenden Dateien sind zwar als übereinstimmend gekennzeichnet, '
. 'wurden aber nicht korrekt eingerichtet. Vielleicht stimmt die Kapitelanzahl zwischen Buch und pdf nicht überein?';
$string['pdf_replace'] = ' (Buch)';

// Search Field.
$string['search'] = 'Suche...';
Expand Down
1 change: 1 addition & 0 deletions lang/en/block_slidefinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
$string['chapter'] = 'Chapter';
$string['misconfigured_info'] = 'The following files are flagged as matching but have not been set up correctly. '
. 'Maybe there is a chapter count mismatch between book and pdf?';
$string['pdf_replace'] = ' (Book)';

// Search Field.
$string['search'] = 'Search keyword...';
Expand Down
20 changes: 10 additions & 10 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
function block_slidefinder_get_content_as_chapters_for_all_book_pdf_matches_from_course($courseid, $userid) {
global $DB;

$coursechapters = array();
$misconfiguredcoursechapters = array();
$coursechapters = [];
$misconfiguredcoursechapters = [];

try {
// Course.
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
}
// Does the user have access to the course?
Expand Down Expand Up @@ -78,7 +78,7 @@ function block_slidefinder_get_content_as_chapters_for_all_book_pdf_matches_from
function block_slidefinder_get_all_book_pdf_matches_from_course($course) {
// Get all PDFs from course.
$fs = get_file_storage();
$pdfs = array();
$pdfs = [];
foreach (get_all_instances_in_course('resource', $course) as $resource) {
// Get all resources.
$cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
Expand Down Expand Up @@ -113,15 +113,15 @@ function block_slidefinder_get_all_book_pdf_matches_from_course($course) {
}

// Get all books from course.
$sectionedbooks = array();
$sectionedbooks = [];
$books = get_all_instances_in_course('book', $course);
foreach ($books as $book) {
$sectionedbooks[$book->section][$book->id] =
trim(preg_replace('/\s*\[[^]]*\](?![^[]*\[)/', '', preg_replace('/\s*\([^)]*\)(?![^(]*\()/', '', $book->name)));
}

// Get all book-PDF matches.
$matches = array();
$matches = [];
foreach ($pdfs as $pdf) {
if (!isset($sectionedbooks[$pdf->section])) {
continue;
Expand All @@ -143,7 +143,7 @@ function block_slidefinder_get_all_book_pdf_matches_from_course($course) {
* @return array list of objects containing the content and some metadata of one PDF page.
*/
function block_slidefinder_get_content_as_chapters($match) {
$chapters = array();
$chapters = [];

try {
$fs = get_file_storage();
Expand All @@ -163,7 +163,7 @@ function block_slidefinder_get_content_as_chapters($match) {

for ($i = 0; $i < $pdfdetails['Pages']; $i++) {
$chapter = new stdClass();
$chapter->filename = $match->filename;
$chapter->filename = str_replace('.pdf', get_string('pdf_replace', 'block_slidefinder'), $match->filename);
$chapter->section = $match->section;
$chapter->page = $i + 1;
$chapter->content = $pages[$i]->getText();
Expand Down Expand Up @@ -206,7 +206,7 @@ function block_slidefinder_get_book_chapter_url($bookid, $pagenum) {
* @return array Array of courses the current user has access to. Position 1 is either selected course or selection message.
*/
function block_slidefinder_select_course_options(int $cid = 0) {
$courses = array();
$courses = [];

foreach (get_courses() as $course) {
if (can_access_course($course)) {
Expand All @@ -222,7 +222,7 @@ function block_slidefinder_select_course_options(int $cid = 0) {
}
} catch (\Throwable $th) {
throw $th;
return array();
return [];
}
} else {
array_unshift($courses, (object)['id' => 0, 'value' => get_string('select_course', 'block_slidefinder')]);
Expand Down
Loading