Skip to content

Commit

Permalink
- Updated course_access checks using moodles can_access_course instea…
Browse files Browse the repository at this point in the history
…d of custom one (addressing #8)

- updated strings addressing #6

- added the customary content check for the block addressing #4

- updated thirdpartylibs.xml addressing #3

- updated error handling & management

- other bugfixes
  • Loading branch information
st143971 committed Sep 16, 2023
1 parent a9f44d6 commit a13c83c
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 176 deletions.
112 changes: 112 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Moodle Plugin CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-22.04

services:
postgres:
image: postgres:13
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3

mariadb:
image: mariadb:10
env:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3

strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
moodle-branch: ['MOODLE_401_STABLE']
database: [pgsql, mariadb]

steps:
- name: Check out repository code
uses: actions/checkout@v3
with:
path: plugin

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ matrix.extensions }}
ini-values: max_input_vars=5000
# If you are not using code coverage, keep "none". Otherwise, use "pcov" (Moodle 3.10 and up) or "xdebug".
# If you try to use code coverage with "none", it will fallback to phpdbg (which has known problems).
coverage: none

- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
- name: Install moodle-plugin-ci
run: |
moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
env:
DB: ${{ matrix.database }}
MOODLE_BRANCH: ${{ matrix.moodle-branch }}

- name: PHP Lint
if: ${{ always() }}
run: moodle-plugin-ci phplint

- name: PHP Copy/Paste Detector
continue-on-error: true # This step will show errors but will not fail
if: ${{ always() }}
run: moodle-plugin-ci phpcpd

- name: PHP Mess Detector
continue-on-error: true # This step will show errors but will not fail
if: ${{ always() }}
run: moodle-plugin-ci phpmd

- name: Moodle Code Checker
if: ${{ always() }}
run: moodle-plugin-ci phpcs --max-warnings 0

- name: Moodle PHPDoc Checker
if: ${{ always() }}
run: moodle-plugin-ci phpdoc --max-warnings 0

- name: Validating
if: ${{ always() }}
run: moodle-plugin-ci validate

- name: Check upgrade savepoints
if: ${{ always() }}
run: moodle-plugin-ci savepoints

- name: Mustache Lint
if: ${{ always() }}
run: moodle-plugin-ci mustache

- name: Grunt
if: ${{ always() }}
run: moodle-plugin-ci grunt --max-lint-warnings 0

- name: PHPUnit tests
if: ${{ always() }}
run: moodle-plugin-ci phpunit --fail-on-warning

- name: Behat features
if: ${{ always() }}
run: moodle-plugin-ci behat --profile chrome
130 changes: 71 additions & 59 deletions block_slidefinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,89 +35,101 @@ function init() {
}

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


// Context
$systemcontext = context_system::instance();
$usercontext = context_user::instance($USER->id);

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

$course = null; // Course to search in.
$course_id = 0; // Course ID of searched course.

$view_course_selection = false; // Are we displaying the course selection?
$view_selected_course = false; // Are we displaying the search field? Did we select a course? Are we allowed to search said course?
// Get Course and CourseID by parameter
if ($course = $DB->get_record('course', array('id' => $lrf_cid))) {
$course_id = $course->id;
$coursecontext = context_course::instance($course->id);
if ($this->content !== null) {
return $this->content;
}

// Renderer needed to use templates
$renderer = $PAGE->get_renderer(get_class($this));

$view_course_selection = !$cid;
$view_selected_course = $course_id ? can_access_course($course) : false;
// Params.
$cid = optional_param('id', 0, PARAM_INT); // Do we have a set course id? Or are we on our dashboard (default).
$lrf_cid = optional_param('lrf_cid', 0, PARAM_INT); // Selected course ID (by our course selection).
$search = optional_param('search', '', PARAM_TEXT); // Searched pattern (search hook).

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

if ($view_course_selection) {
$text .= $renderer->render_from_template('block_slidefinder/lrf_drop_down', [
'action' => $PAGE->url,
'course_selector_param_name' => 'lrf_cid',
'course_selector_options' => block_lrf_select_course_options($course_id),
]);
}
if ($view_selected_course) {
$text .= $renderer->render_from_template('block_slidefinder/lrf_search', [
try {
// Get all current params.
$hiddenparams = $_GET;

// Filter out 'lrf_cid' as a param we use and change.
$hiddenparams = array_filter($hiddenparams, function ($key) {
return $key !== 'lrf_cid';
}, 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 array("name" => $name, "value" => $value);
}, array_keys($hiddenparams), $hiddenparams);

if ($cid == 0) { // My Moodle Page.
if ($lrf_cid != 0) {
// Course.
if (!$course = $DB->get_record('course', array('id' => $lrf_cid))) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
}
// 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'));
}
} else {
$course = null;
}
$text .= $OUTPUT->render_from_template('block_slidefinder/lrf_drop_down', [
'action' => $PAGE->url,
'course_selector_param_name' => 'lrf_cid',
'course_selector_options' => block_slidefinder_select_course_options($lrf_cid),
'hidden_params' => $hiddenparams
]);
} else { // Course Page.
// Course.
if (!$course = $DB->get_record('course', array('id' => $cid))) {
throw new moodle_exception(get_string('error_course_not_found', 'block_slidefinder'));
}
// 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'));
}
}

$data = [[], []];
if (!is_null($course)) {
$data = block_slidefinder_get_content_as_chapters_for_all_book_pdf_matches_from_course($course->id, $USER->id);
if (!empty($data[1])) {
$footer .= get_string('misconfigured_info', get_class($this));
foreach ($data[1] as $key => $value) {
$footer .= '<br>';
$footer .= $value;
}
}
}

$text .= $OUTPUT->render_from_template('block_slidefinder/lrf_search', [
'action' => $PAGE->url,
'cid' => $cid,
'lrf_cid' => $lrf_cid,
'course_selector_param_name' => 'lrf_cid',
'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($this->get_pdfs_content_from_course($course)))
'content' => base64_encode(json_encode($data[0])),
'hidden_params' => $hiddenparams
]);
} catch (\Throwable $th) {
debugging($th);
$text .= get_string('error_message', get_class($this));
$footer .= $th;
}

$this->content = new stdClass();
$this->content->text = $text;
$this->content->footer = $footer;
return $this->content;
}

/**
* Returns the PDFs and their content (splitted in pages) for all eligable PDFs in the given course.
*
* @param mixed $course course to search in.
*
* @return array array of objects each holding one pdf page on content and some metadata
*/
function get_pdfs_content_from_course($course): array {
if ($course == null) return [];

$chapters = array();

$matches = block_lrf_get_all_book_pdf_matches_from_course($course);

foreach ($matches as $match) {
$chapters = array_merge($chapters, block_lrf_get_content_as_chapters($match));
}

return $chapters;
}
}
Loading

0 comments on commit a13c83c

Please sign in to comment.