Skip to content

Commit

Permalink
Merge pull request #18 from SE-Stuttgart/dev
Browse files Browse the repository at this point in the history
Plugin rename to block_slidefinder
  • Loading branch information
MakinZeel authored May 7, 2024
2 parents 6f7476a + cf588a9 commit cd89977
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 247 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

**German version please see below**

The plugin was developed to provide a search field for text search in Moodle books with image-based slides. The plugin requires that a PDF of (almost) the same name is present in the same course section. The pages of the PDF need to correspond 1-1 to the pages in the book. Names of book and PDF need to be identical, except for text specified in brackets inside their names.
The plugin was developed to provide a search field for text search in Moodle books with image-based chapters. The plugin requires that a PDF of (almost) the same name is present in the same course section. The pages of the PDF need to correspond 1-1 to the pages in the book. Names of book and PDF need to be identical, except for text specified in brackets inside their names.

The plugin searches all PDFs for the text and then displays the search results as a list of links. Clicking on a link shows the respective page with the corresponding number inside the book.

Expand All @@ -15,7 +15,7 @@ More details on functionality and configuration of this plugin can be found in t

## German Description

Das Plugin wurde entwickelt, um ein Suchfeld für die Textsuche nach Begriffen in Moodle-Büchern mit bildbasierten Folien bereitzustellen. Es erfordert, dass zu jedem Buch ein durchsuchbares PDF mit (fast) gleichen Namens bereitgestellt wird, dessen Seiten 1:1 zu den Seiten im Buch passen. Das PDF muss im selben Abschnitt wie das Buch liegen. Die Namen von Buch und PDF müssen identisch sein, ausgenommen ist in Klammern spezifizierter Text innerhalb der Namen.
Das Plugin wurde entwickelt, um ein Suchfeld für die Textsuche nach Begriffen in Moodle-Büchern mit bildbasierten Kapiteln bereitzustellen. Es erfordert, dass zu jedem Buch ein durchsuchbares PDF mit (fast) gleichen Namens bereitgestellt wird, dessen Seiten 1:1 zu den Seiten im Buch passen. Das PDF muss im selben Abschnitt wie das Buch liegen. Die Namen von Buch und PDF müssen identisch sein, ausgenommen ist in Klammern spezifizierter Text innerhalb der Namen.

Das Plugin sucht in allen PDFs nach den Begriffen und zeigt die Resultate als eine Liste von Links an. Durch Klicken auf den gewünschten Link wird die Seite mit der entsprechenden Seitennummer im Buch angezeigt.

Expand Down Expand Up @@ -49,21 +49,23 @@ This involves the following steps:
2. Enable protocols: Here, enable the REST protocol if not already enabled.
3. Create a new specific user.
4. Check user capability: The specified user has to have at least the __webservice/rest:use__ capability. If the user does not have the permission, you can add the permission to a role and then add the user to that role.
5. Select a service: Add the "Book Search" to custom services.
6. Add functions: Add the "block_booksearch_get_searched_locations" function to the "Book Search" service.
5. Select a service: Add the "Booksearch" to custom services.
6. Add functions: Add the "block_booksearch_get_searched_locations" function to the "Booksearch" service.
7. Select a specific user: Add the web services user as an authorised user.
8. Create a token for a user: Create a token for the web services user.
8. The authorized user needs the rights to access the course content.
9. Create a token for a user: Create a token for the web services user.


Test it by sending an http GET request to
'http://[yourmoodle]/webservice/rest/server.php?wstoken=[user-token]&wsfunction=block_booksearch_get_searched_locations&moodlewsrestformat=json&search_string=[search_string]&course_id=[course_id]&context_length=[context_length]'
'http://[yourmoodle]/webservice/rest/server.php?wstoken=[user_token]&wsfunction=block_booksearch_get_searched_locations&moodlewsrestformat=json&userid=[user_id]&courseid=[course_id]&searchstring=[search_string]&contextlength=[context_length]'
where
- yourmoodle: domain of your moodle installation (as developer: probably localhost)
- user-token: token received from moodle for a user which is allowed to use the web service
- search_string: the search string which is used to search in moodle books and pdfs
- course_id: the id of the course the string is searched in
- context_length: the number of word before and after each found string
- user_token: token received from moodle for a user which is allowed to use the web service.
- user_id: the id of the user that wants to access the webservice (This user needs to have access to the course content).
- course_id: the id of the course the string is searched in.
- search_string: the search string which is used to search in moodle books and pdfs.
- context_length: the number of word before and after each found string.

You should get a response with your search results in the JSON format.


## Installing via Uploaded ZIP File ##
Expand Down
53 changes: 19 additions & 34 deletions block_slidefinder.php → block_booksearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
/**
* Block core and UI
*
* @package block_slidefinder
* @package block_booksearch
* @copyright 2022 Universtity of Stuttgart <kasra.habib@iste.uni-stuttgart.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/locallib.php');

define('BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM', 'slidefinderid');
define('BLOCK_BOOKSEARCH_BOOKSEARCH_PARAM', 'booksearchid');

/**
* The slidefinder block class.
* The booksearch block class.
*
* Used to create the slidefinder block. Base of all slidefinder block functionality & UI.
* Used to create the booksearch block. Base of all booksearch block functionality & UI.
*/
class block_slidefinder extends block_base {
class block_booksearch extends block_base {
/**
* Set the initial properties for the block
*/
Expand All @@ -52,61 +52,47 @@ public function get_content() {

// Params.
$cid = optional_param('id', 0, PARAM_INT); // Do we have a set course id? Or are we on our dashboard (default).
$slidefinderid = optional_param(BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM, 0, PARAM_INT); // Selected course ID.
$booksearchid = optional_param(BLOCK_BOOKSEARCH_BOOKSEARCH_PARAM, 0, PARAM_INT); // Selected course ID.
$search = optional_param('search', '', PARAM_TEXT); // Searched pattern (search hook).

// Main Content (text) and Footer of the block.
$text = '';
$footer = '';

try {
// Get all current params.
$hiddenparams = $_GET;

// Filter out BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM as a param we use and change.
$hiddenparams = array_filter($hiddenparams, function ($key) {
return $key !== BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM;
}, ARRAY_FILTER_USE_KEY);

// 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 ["name" => $name, "value" => $value];
}, array_keys($hiddenparams), $hiddenparams);

if ($cid == 0) { // My Moodle Page.
if ($slidefinderid != 0) {
if ($booksearchid != 0) {
// Course.
if (!$course = $DB->get_record('course', ['id' => $slidefinderid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
if (!$course = $DB->get_record('course', ['id' => $booksearchid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_booksearch'));
}
// Does the user have access to the course?
if (!can_access_course($course)) {
throw new moodle_exception(get_string('error_course_access_denied', 'block_slidefinder'));
throw new moodle_exception(get_string('error_course_access_denied', 'block_booksearch'));
}
} else {
$course = null;
}
$text .= $OUTPUT->render_from_template('block_slidefinder/lrf_drop_down', [
$text .= $OUTPUT->render_from_template('block_booksearch/block_booksearch_drop_down', [
'action' => $this->page->url,
'course_selector_param_name' => BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM,
'course_selector_options' => block_slidefinder_select_course_options($slidefinderid),
'hidden_params' => $hiddenparams,
'course_selector_param_name' => BLOCK_BOOKSEARCH_BOOKSEARCH_PARAM,
'course_selector_options' => block_booksearch_select_course_options($booksearchid),
]);
} else { // Course Page.
// Course.
if (!$course = $DB->get_record('course', ['id' => $cid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
throw new moodle_exception(get_string('error_course_not_found', 'block_booksearch'));
}
// Does the user have access to the course?
if (!can_access_course($course)) {
throw new moodle_exception(get_string('error_course_access_denied', 'block_slidefinder'));
throw new moodle_exception(get_string('error_course_access_denied', 'block_booksearch'));
}
}

// Info: data[0] has the attributes section, filename, page, bookurl, size, content.
$data = [[], []];
if (!is_null($course)) {
$data = block_slidefinder_get_all_content_of_course_as_sections_with_metadata($course->id, $USER->id);
$data = block_booksearch_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 All @@ -116,17 +102,16 @@ public function get_content() {
}
}

$text .= $OUTPUT->render_from_template('block_slidefinder/lrf_search', [
$text .= $OUTPUT->render_from_template('block_booksearch/block_booksearch_search', [
'action' => $this->page->url,
'cid' => $slidefinderid,
'course_selector_param_name' => BLOCK_SLIDEFINDER_SLIDEFINDER_PARAM,
'cid' => $booksearchid,
'course_selector_param_name' => BLOCK_BOOKSEARCH_BOOKSEARCH_PARAM,
'search_term_param_name' => 'search',
'search_term_placeholder' => get_string('search', get_class($this)),
'search_term_label' => get_string('search_term', get_class($this)),
'search_term' => $search,
'chapter_label' => get_string('chapter', get_class($this)),
'content' => base64_encode(json_encode($data[0])),
'hidden_params' => $hiddenparams,
]);
} catch (\Throwable $th) {
debugging($th);
Expand Down
43 changes: 43 additions & 0 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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/>.

/**
* Privacy Information
*
* @package block_booksearch
* @copyright 2022 Universtity of Stuttgart <kasra.habib@iste.uni-stuttgart.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_booksearch\privacy;

/**
* This class provides information to the user about how their personal data is handled.
*/
class provider implements
// This plugin does not store any personal user data.
\core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
6 changes: 3 additions & 3 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
/**
* Capability definitions for this module.
*
* @package block_slidefinder
* @package block_booksearch
* @copyright 2022 Universtity of Stuttgart <kasra.habib@iste.uni-stuttgart.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

$capabilities = [
'block/slidefinder:myaddinstance' => [
'block/booksearch:myaddinstance' => [
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => [
Expand All @@ -33,7 +33,7 @@
'clonepermissionsfrom' => 'moodle/my:manageblocks',
],

'block/slidefinder:addinstance' => [
'block/booksearch:addinstance' => [
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
Expand Down
8 changes: 4 additions & 4 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
/**
* Describes WebServices
*
* @package block_slidefinder
* @package block_booksearch
* @copyright 2022 Universtity of Stuttgart <kasra.habib@iste.uni-stuttgart.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

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

// Class containing the external function.
'classname' => 'block_slidefinder_external',
'classname' => 'block_booksearch_external',

// External function name.
'methodname' => 'get_searched_locations',

// File containing the class/external function - not required if using namespaced auto-loading classes.
// Defaults to the service's externalib.php.
'classpath' => 'blocks/slidefinder/externallib.php',
'classpath' => 'blocks/booksearch/externallib.php',

// This documentation will be displayed in the generated API documentation.
// Administration > Plugins > Webservices > API documentation.
Expand Down
20 changes: 10 additions & 10 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Function for the WebService
*
* @package block_slidefinder
* @package block_booksearch
* @copyright 2022 Universtity of Stuttgart <kasra.habib@iste.uni-stuttgart.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand All @@ -26,11 +26,11 @@
require_once("$CFG->libdir/externallib.php");

/**
* External class for the slidefinder block.
* External class for the booksearch block.
*
* Let's a webservice use the slidefinder functionality.
* Let's a webservice use the booksearch functionality.
*/
class block_slidefinder_external extends external_api {
class block_booksearch_external extends external_api {
/**
* Returns description of method parameter
* @return external_function_parameters
Expand Down Expand Up @@ -91,15 +91,15 @@ public static function get_searched_locations($userid, $courseid, $searchstring,
try {
// User.
if (!$user = $DB->get_record('user', ['id' => $userid])) {
throw new moodle_exception(get_string('error_user_not_found', 'block_slidefinder'));
throw new moodle_exception(get_string('error_user_not_found', 'block_booksearch'));
}
// Course.
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
throw new moodle_exception(get_string('error_course_not_found', 'block_booksearch'));
}
// Does the user have access to the course?
if (!can_access_course($course, $user)) {
throw new moodle_exception(get_string('error_course_access_denied', 'block_slidefinder'));
// Does the webservice and user have access to the course?
if (!can_access_course($course) && !can_access_course($course, $user)) {
throw new moodle_exception(get_string('error_course_access_denied', 'block_booksearch'));
}
} catch (\Throwable $th) {
debugging($th);
Expand All @@ -110,7 +110,7 @@ public static function get_searched_locations($userid, $courseid, $searchstring,
self::validate_context($coursecontext);

// Get all searchable content.
$sections = block_slidefinder_get_all_content_of_course_as_sections_with_metadata($courseid, $userid)[0];
$sections = block_booksearch_get_all_content_of_course_as_sections_with_metadata($courseid, $userid)[0];

// Get Search Results & Context for PDFs.
$data = [];
Expand Down
Loading

0 comments on commit cd89977

Please sign in to comment.