24
24
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
25
*/
26
26
27
- defined ( ' MOODLE_INTERNAL ' ) || die () ;
27
+ use block_search_books \ helper ;
28
28
29
+ /**
30
+ * Search books block
31
+ */
29
32
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 ' );
32
40
}
33
41
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
+ }
35
50
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.
41
66
}
42
67
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 ;
48
75
49
- if ($ this ->content !== NULL ) {
76
+ if ($ this ->content !== null ) {
50
77
return $ this ->content ;
51
78
}
52
79
53
80
if ($ COURSE ->id == $ this ->page ->course ->id ) {
54
81
$ course = $ COURSE ;
55
82
} else {
56
- $ course = $ DB ->get_record ('course ' , array ( 'id ' => $ this ->page ->course ->id ) );
83
+ $ course = $ DB ->get_record ('course ' , [ 'id ' => $ this ->page ->course ->id ] );
57
84
}
58
85
59
- // Course not found, we won't do anything in the block
86
+ // Course not found, we won't do anything in the block.
60
87
if (empty ($ course )) {
61
- return '' ;
88
+ return null ;
62
89
}
63
90
64
91
$ this ->content = new stdClass ;
@@ -69,64 +96,36 @@ function get_content() {
69
96
return $ this ->content ;
70
97
}
71
98
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 );
97
100
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
+ }
118
126
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 );
123
128
124
129
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
-
131
130
}
132
131
}
0 commit comments