Skip to content

Commit 57b048d

Browse files
committed
Updated Book search with coding standards
1 parent e8b9d0a commit 57b048d

12 files changed

+458
-345
lines changed

.github/workflows/moodle-ci.yml

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
name: Moodle Plugin CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-22.04
8+
9+
services:
10+
postgres:
11+
image: postgres:14
12+
env:
13+
POSTGRES_USER: 'postgres'
14+
POSTGRES_HOST_AUTH_METHOD: 'trust'
15+
ports:
16+
- 5432:5432
17+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
18+
19+
mariadb:
20+
image: mariadb:10
21+
env:
22+
MYSQL_USER: 'root'
23+
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
24+
MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
25+
MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"
26+
ports:
27+
- 3306:3306
28+
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3
29+
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
include:
34+
- php: '8.1'
35+
# Main job. Run all checks that do not require setup and only need to be run once.
36+
runchecks: 'all'
37+
moodle-branch: 'MOODLE_401_STABLE'
38+
database: 'pgsql'
39+
- php: '7.4'
40+
moodle-branch: 'MOODLE_401_STABLE'
41+
database: 'mariadb'
42+
- php: '8.3'
43+
moodle-branch: 'MOODLE_405_STABLE'
44+
database: 'pgsql'
45+
- php: '8.1'
46+
moodle-branch: 'MOODLE_405_STABLE'
47+
database: 'mariadb'
48+
49+
steps:
50+
- name: Check out repository code
51+
uses: actions/checkout@v4
52+
with:
53+
path: plugin
54+
55+
- name: Setup PHP ${{ matrix.php }}
56+
uses: shivammathur/setup-php@v2
57+
with:
58+
php-version: ${{ matrix.php }}
59+
extensions: ${{ matrix.extensions }}
60+
ini-values: max_input_vars=5000
61+
# If you are not using code coverage, keep "none". Otherwise, use "pcov" (Moodle 3.10 and up) or "xdebug".
62+
# If you try to use code coverage with "none", it will fallback to phpdbg (which has known problems).
63+
coverage: none
64+
65+
- name: Initialise moodle-plugin-ci
66+
run: |
67+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
68+
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
69+
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
70+
sudo locale-gen en_AU.UTF-8
71+
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
72+
73+
- name: Install moodle-plugin-ci
74+
run: moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
75+
env:
76+
DB: ${{ matrix.database }}
77+
MOODLE_BRANCH: ${{ matrix.moodle-branch }}
78+
# Uncomment this to run Behat tests using the Moodle App.
79+
# MOODLE_APP: 'true'
80+
81+
- name: PHP Lint
82+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
83+
run: moodle-plugin-ci phplint
84+
85+
- name: PHP Mess Detector
86+
continue-on-error: true # This step will show errors but will not fail
87+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
88+
run: moodle-plugin-ci phpmd
89+
90+
- name: Moodle Code Checker
91+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
92+
run: moodle-plugin-ci phpcs --max-warnings 0
93+
94+
- name: Moodle PHPDoc Checker
95+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
96+
run: moodle-plugin-ci phpdoc --max-warnings 0
97+
98+
- name: Validating
99+
if: ${{ !cancelled() }}
100+
run: moodle-plugin-ci validate
101+
102+
- name: Check upgrade savepoints
103+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
104+
run: moodle-plugin-ci savepoints
105+
106+
- name: Mustache Lint
107+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
108+
run: moodle-plugin-ci mustache
109+
110+
- name: Grunt
111+
if: ${{ !cancelled() && matrix.runchecks == 'all' }}
112+
run: moodle-plugin-ci grunt --max-lint-warnings 0
113+
114+
- name: PHPUnit tests
115+
if: ${{ !cancelled() }}
116+
run: moodle-plugin-ci phpunit --fail-on-warning
117+
118+
- name: Behat features
119+
id: behat
120+
if: ${{ !cancelled() }}
121+
run: moodle-plugin-ci behat --profile chrome
122+
123+
- name: Upload Behat Faildump
124+
if: ${{ failure() && steps.behat.outcome == 'failure' }}
125+
uses: actions/upload-artifact@v4
126+
with:
127+
name: Behat Faildump (${{ join(matrix.*, ', ') }})
128+
path: ${{ github.workspace }}/moodledata/behat_dump
129+
retention-days: 7
130+
if-no-files-found: ignore
131+
132+
- name: Mark cancelled jobs as failed.
133+
if: ${{ cancelled() }}
134+
run: exit 1

amd/build/checkbox.min.js

-1
This file was deleted.

amd/src/checkbox.js

-46
This file was deleted.

block_search_books.php

+72-73
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,68 @@
2424
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2525
*/
2626

27-
defined('MOODLE_INTERNAL') || die();
27+
use block_search_books\helper;
2828

29+
/**
30+
* Search books block
31+
*/
2932
class block_search_books extends block_base {
30-
function init() {
31-
$this->title = get_string('pluginname','block_search_books');
33+
/**
34+
* Initialise the block
35+
*
36+
* @return void
37+
*/
38+
public function init() {
39+
$this->title = get_string('pluginname', 'block_search_books');
3240
}
3341

34-
function has_config() {return false;}
42+
/**
43+
* And local config settings for the teacher?
44+
*
45+
* @return bool
46+
*/
47+
public function has_config(): bool {
48+
return false;
49+
}
3550

36-
function applicable_formats() {
37-
// SSU_AMEND START - BOOK SEARCH
38-
//return (array('site-index' => true, 'course-view-weeks' => true, 'course-view-topics' => true));
39-
return (array('site-index' => true, 'course-view-weeks' => true, 'course-view-topics' => true, 'course-view-nonumbers' => true, 'onetopic' => true));
40-
// SSU_AMEND END
51+
/**
52+
* List of formats the block appears on
53+
*
54+
* @return array
55+
*/
56+
public function applicable_formats(): array {
57+
// SSU_AMEND_START: Include our course format.
58+
return [
59+
'site-index' => true,
60+
'course-view-weeks' => true,
61+
'course-view-topics' => true,
62+
'course-view-nonumbers' => true,
63+
'course-view-onetopic' => true,
64+
];
65+
// SSU_AMEND_END.
4166
}
4267

43-
function get_content() {
44-
global $CFG, $USER, $COURSE, $DB, $OUTPUT;
45-
// SSU_AMEND START - BOOK SEARCH
46-
$this->page->requires->js_call_amd('block_search_books/checkbox', 'init');
47-
// SSU_AMEND END
68+
/**
69+
* Get block content.
70+
*
71+
* @return stdClass|null
72+
*/
73+
public function get_content(): ?stdClass {
74+
global $DB, $COURSE, $OUTPUT;
4875

49-
if ($this->content !== NULL) {
76+
if ($this->content !== null) {
5077
return $this->content;
5178
}
5279

5380
if ($COURSE->id == $this->page->course->id) {
5481
$course = $COURSE;
5582
} else {
56-
$course = $DB->get_record('course', array('id' => $this->page->course->id));
83+
$course = $DB->get_record('course', ['id' => $this->page->course->id]);
5784
}
5885

59-
// Course not found, we won't do anything in the block
86+
// Course not found, we won't do anything in the block.
6087
if (empty($course)) {
61-
return '';
88+
return null;
6289
}
6390

6491
$this->content = new stdClass;
@@ -69,64 +96,36 @@ function get_content() {
6996
return $this->content;
7097
}
7198

72-
$searchbooks = get_string('bookssearch', 'block_search_books');
73-
74-
// SSU_AMEND START - BOOK SEARCH
75-
$books = get_all_instances_in_course('book', $course);
76-
77-
// $this->content->text = '<div class="searchform">';
78-
// $this->content->text .= '<form action="' . $CFG->wwwroot . '/blocks/search_books/search_books.php" style="display:inline">';
79-
// $this->content->text .= '<fieldset class="invisiblefieldset">';
80-
// $this->content->text .= '<input name="courseid" type="hidden" value="' . $course->id . '" />';
81-
// $this->content->text .= '<input name="page" type="hidden" value="0" />';
82-
// $this->content->text .= '<label class="accesshide" for="searchbooksquery">' . $searchbooks . '</label>';
83-
// $this->content->text .= '<input id="searchbooksquery" name="bsquery" size="20" maxlength="255" value="" />';
84-
// $this->content->text .= '<br /><input type="submit" name="submit" value="' . $searchbooks . '"/>';
85-
// $this->content->text .= '</fieldset></form></div>';
86-
87-
// return $this->content;
88-
89-
if(count($books) > 0){
90-
// SSU_AMEND END
91-
92-
$this->content->text = '<div class="searchform">';
93-
$this->content->text .= '<form action="' . $CFG->wwwroot . '/blocks/search_books/search_books.php" method="post" style="display:inline">';
94-
$this->content->text .= '<fieldset class="invisiblefieldset">';
95-
$this->content->text .= '<input name="courseid" type="hidden" value="' . $course->id . '" />';
96-
$this->content->text .= '<input name="page" type="hidden" value="0" />';
99+
$books = helper::get_readable_books($course);
97100

98-
// SSU_AMEND START - BOOK SEARCH
99-
$this->content->text .= '<div style="text-align:left">
100-
<h3><a id="toggle" href="#">Advanced search...</a></h3>';
101-
$this->content->text .= '<div id="checkholder">';
102-
$this->content->text .= '<p id="intro">Select individual books to narrow your search results:</p>';
103-
104-
foreach ($books as $book) {
105-
$cm = get_coursemodule_from_instance("book", $book->id, $course->id);
106-
$context = context_module::instance($cm->id);
107-
if ($cm->visible || has_capability('moodle/course:viewhiddenactivities', $context)) {
108-
if (has_capability('mod/book:read', $context)) {
109-
$bookids[] = $book->id;
110-
$this->content->text .= '<label><input type="checkbox" class="checkbox1" name="check_book[]" value="'. $book->id . '"/><a href="'.$CFG->wwwroot.'/mod/book/view.php?id='.$cm->id.'" target="_blank">' . $book->name . '</a></label><br />';
111-
}
112-
}
113-
}
114-
115-
$this->content->text .= '<label><input type="checkbox" name="check" id="check">Select/unselect all</label><br />';
116-
$this->content->text .= '</div>';
117-
$this->content->text .= '</div>';
101+
if (count($books) == 0) {
102+
$this->content->text .= '<p id="intro">There are no books in this course</p>';
103+
return $this->content;
104+
}
105+
$data = new stdClass();
106+
$data->courseid = $course->id;
107+
$formurl = new moodle_url('/blocks/search_books/search_books.php');
108+
$data->formurl = $formurl->out();
109+
$data->books = [];
110+
foreach ($books as $book) {
111+
$cm = get_coursemodule_from_instance("book", $book->id, $course->id);
112+
$context = context_module::instance($cm->id);
113+
if ($cm->visible || has_capability('moodle/course:viewhiddenactivities', $context)) {
114+
if (has_capability('mod/book:read', $context)) {
115+
$bookitem = new stdClass();
116+
$bookitem->bookid = $book->id;
117+
$url = new moodle_url('/mod/book/view.php', [
118+
'id' => $cm->id,
119+
]);
120+
$bookitem->url = $url->out();
121+
$bookitem->name = s($book->name);
122+
$data->books[] = $bookitem;
123+
}
124+
}
125+
}
118126

119-
$this->content->text .= '<label class="accesshide" for="searchbooksquery">' . $searchbooks . '</label>';
120-
$this->content->text .= $OUTPUT->image_icon('icon', get_string('pluginname', 'book'), 'book') . '<input type="text" id="searchbooksquery" name="bsquery" size="20" maxlength="255" value="" />';
121-
$this->content->text .= '<br /><input type="submit" name="submit" value="' . $searchbooks . '"/>';
122-
$this->content->text .= '</fieldset></form></div>';
127+
$this->content->text = $OUTPUT->render_from_template('block_search_books/searchbox', $data);
123128

124129
return $this->content;
125-
126-
}else{
127-
$this->content->text .= '<p id="intro">There are no books in this course</p>';
128-
}
129-
// SSU_AMEND END
130-
131130
}
132131
}

0 commit comments

Comments
 (0)