Skip to content

Commit

Permalink
Version 3.2.0
Browse files Browse the repository at this point in the history
Updated the placement rules for the Block.
- Can no longer be placed on the site-index
- Can now be placed on the Course Modules
  • Loading branch information
MakinZeel authored Aug 19, 2024
2 parents c8c8756 + 3ae5510 commit 91365c1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 51 deletions.
158 changes: 111 additions & 47 deletions block_booksearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,34 @@ public function init() {
}


/**
* Allow multiple instances of this block
* @return bool Returns false
*/
public function instance_allow_multiple() {
return false;
}


/**
* Where can this Block be placed
* @return array Context level where this block can be placed
*/
public function applicable_formats() {
return [
'admin' => false,
'site-index' => false,
'course-view' => true,
'mod' => true,
'my' => true,
];
}


/**
* Describe Block Content.
*/
public function get_content() {
global $OUTPUT, $DB, $USER;

if ($this->content !== null) {
return $this->content;
}
Expand All @@ -61,21 +83,18 @@ public function get_content() {
$text = '';
$footer = '';

// Add the course target selection to the Block ui content if we are in the dashboard view.
if (!self::in_course_view()) {
list($text, $footer) = self::add_course_selection_ui($text, $footer);
}

// Add the booksearch ui to the Block ui content if we have a target course selected to search in.
if (self::is_course_selected()) {
list($isvalid, $course, $error) = block_booksearch_validate_course_access(self::get_selected_course_id(), $USER->id);

if ($isvalid) {
list($text, $footer) = self::add_search_and_results_ui($text, $footer);
} else {
$text .= get_string('error_message', get_class($this));
$footer .= $error;
}
switch ($this->page->context->contextlevel) {
case CONTEXT_USER:
self::handle_user_context($text, $footer);
break;
case CONTEXT_COURSE:
self::handle_course_context($text, $footer);
break;
case CONTEXT_MODULE:
self::handle_module_context($text, $footer);
break;
default:
break;
}

$this->content = new stdClass();
Expand All @@ -86,47 +105,96 @@ public function get_content() {


/**
* This function checks if we currently are in course view or on our dashboard.
* It checks if the Paramater 'id' is set, which is the case for course view but not for dashboard.
* @return bool True if we are in course view.
* Behavior of this block when on the dashboard (user context).
* @param string $text This is the main block ui.
* @param string $footer This is the footer of the Block ui.
*/
private function handle_user_context(&$text, &$footer) {
global $USER;

// As this context does not have a fixed course, display a course selection.
self::add_course_selection_ui($text);

// Get a selected courseid if one was selected, -1 if not.
$courseid = self::get_selected_course_id();

// If there is no active selected course, we do not display anything else.
if ($courseid < 0) {
return;
}

// Check if we have access to the selected course.
list($isvalid, $course, $error) = block_booksearch_validate_course_access($courseid, $USER->id);

// We do not have access, so we display an error message.
if (!$isvalid) {
$text .= get_string('error_message', get_class($this));
$footer .= $error;
return;
}

// We have valid access, so we display a search field and the results.
self::add_search_and_results_ui($text, $footer, $course->id);
}


/**
* Behavior of this block when on the main course view (course context).
* @param string $text This is the main block ui.
* @param string $footer This is the footer of the Block ui.
*/
private function in_course_view(): bool {
$currentcourseid = optional_param('id', 0, PARAM_INT);
return $currentcourseid != 0;
private function handle_course_context(&$text, &$footer) {
// Get the course id.
$courseid = $this->page->context->instanceid;

// Add the search field and results to the final ui.
self::add_search_and_results_ui($text, $footer, $courseid);
}


/**
* This function checks if there is a current selected target course to search.
* @return bool True, if there is a target course selected.
* Behavior of this block when viewing a course module (module context).
* @param string $text This is the main block ui.
* @param string $footer This is the footer of the Block ui.
*/
private function is_course_selected(): bool {
$currentcourseid = optional_param('id', 0, PARAM_INT);
$searchcourseid = optional_param(BLOCK_BOOKSEARCH_TARGET_ID_PARAM, 0, PARAM_INT);
// If at least one of the two parameters is not zero, there is a course selected.
return 0 < $currentcourseid + $searchcourseid;
private function handle_module_context(&$text, &$footer) {
global $DB;

// Get the module id.
$moduleid = $this->page->context->instanceid;

// Fetch the course module record from the database.
$cm = $DB->get_record('course_modules', ['id' => $moduleid], 'course');

// The course module for this module id could not be found, so we display an error message.
if (!$cm) {
$text .= get_string('database_error', get_class($this));
return;
}

// Get courseid from coursemodule.
$courseid = $cm->course;

// Add the search field and results to the final ui.
self::add_search_and_results_ui($text, $footer, $courseid);
}


/**
* This function returns the course id of the current booksearch target, 0 if there is no target.
* @return int Target course id.
* This function returns the course id of the current booksearch target, -1 if there is no target.
* @return int Selected courseid or -1 if none selected.
*/
private function get_selected_course_id(): int {
$searchcourseid = optional_param(BLOCK_BOOKSEARCH_TARGET_ID_PARAM, 0, PARAM_INT);
// If $currentcourseid is not set (which means we are not in a course), we use our custom $searchcourseid.
$currentcourseid = optional_param('id', $searchcourseid, PARAM_INT);
return $currentcourseid;
$courseid = optional_param(BLOCK_BOOKSEARCH_TARGET_ID_PARAM, -1, PARAM_INT);
return $courseid;
}


/**
* This function adds the ui elements regarding the course selection to the given strings and returns them.
* @param string $text This string has the main Block content UI.
* @param string $footer This string has the Blocks footer UI.
* @return array [$text, $footer] - The updated $text and $footer with the course selection elements.
*/
private function add_course_selection_ui(string $text, string $footer): array {
private function add_course_selection_ui(string &$text) {
global $OUTPUT;

// Display the drop down course selector.
Expand All @@ -135,22 +203,20 @@ private function add_course_selection_ui(string $text, string $footer): array {
'course_selector_param_name' => BLOCK_BOOKSEARCH_TARGET_ID_PARAM,
'course_selector_options' => self::get_select_course_options(self::get_selected_course_id()),
]);

return [$text, $footer];
}


/**
* This function adds the ui elements regarding the book search input and results to the given strings and returns them.
* @param string $text This string has the main Block content UI.
* @param string $footer This string has the Blocks footer UI.
* @return array [$text, $footer] - The updated $text and $footer with the search input and result elements.
* @param int $courseid This is the id of the course we want to search in.
*/
private function add_search_and_results_ui(string $text, string $footer): array {
private function add_search_and_results_ui(string &$text, string &$footer, int $courseid) {
global $OUTPUT;

// Info: $content has the attributes section, filename, page, bookurl, size, content.
list($content, $misconfiguredcontentinfo) = data::get_course_content(self::get_selected_course_id());
list($content, $misconfiguredcontentinfo) = data::get_course_content($courseid);

// Add a list of names of the misconfigured chapters to the block footer.
if (!empty($misconfiguredcontentinfo)) {
Expand All @@ -168,16 +234,14 @@ private function add_search_and_results_ui(string $text, string $footer): array
'chapter_label' => get_string('chapter', get_class($this)),
'course_content' => base64_encode(json_encode($content)),
]);

return [$text, $footer];
}

/**
* Create and Return an (id => fullname) array for all courses the current user can access.
* @param int $courseid ID of a course. The selected course is at the beginning of the array, else a selection method.
* @return array Array of courses the current user has access to. Position 1 is either selected course or selection message.
*/
private function get_select_course_options(int $courseid = 0) {
private function get_select_course_options(int $courseid = -1) {
$courses = [];

foreach (get_courses() as $course) {
Expand Down
2 changes: 1 addition & 1 deletion db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

'block/booksearch:addinstance' => [
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
'user' => CAP_PREVENT,
],
Expand Down
3 changes: 2 additions & 1 deletion lang/en/block_booksearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
$string['error_user_not_found'] = 'User does not exist.';
$string['error_course_not_found'] = 'Course not found.';
$string['error_course_access_denied'] = 'Access to course denied.';
$string['error_book_pdf_mismatch'] = 'There exists an mismatch of book and pdf.';
$string['error_book_pdf_mismatch'] = 'There is a mismatch between the book and the PDF.';
$string['error_module_database'] = 'This course module could not be found in the database.';

// External Parameter Description.
$string['parameter_course_id'] = 'Id of the course the user wants to access.';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024081800; // The current plugin version (Date: YYYYMMDDHH).
$plugin->version = 2024081900; // The current plugin version (Date: YYYYMMDDHH).
$plugin->requires = 2020061510; // Requires this Moodle version.
$plugin->component = 'block_booksearch'; // Full name of the plugin (used for diagnostics).
$plugin->release = '1.1.3';
$plugin->release = '3.2.0';
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 91365c1

Please sign in to comment.