Skip to content

Commit 16aca44

Browse files
committed
Make new code from user adpe more readable
This commit mostly breaks out the changes from Github user 'adpe' into multiple functions to make it easier to read. Some effort was put into reducing code duplication, especially when it comes to writing out select menus and hyperlinks since their logic is almost identical. Finally, updated styles.css to prevent unordered list discs ("bullets") from displaying next to the block's elements.
1 parent 55b1b0f commit 16aca44

File tree

2 files changed

+164
-99
lines changed

2 files changed

+164
-99
lines changed

block_massaction.php

+160-97
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function instance_allow_multiple() {
7878
* @return The HTML content of the block.
7979
*/
8080
public function get_content() {
81-
global $COURSE, $OUTPUT, $PAGE;
81+
global $COURSE, $PAGE;
8282

8383
if ($this->content !== null) {
8484
return $this->content;
@@ -98,111 +98,35 @@ public function get_content() {
9898
* modules and the ids of its modules.
9999
*/
100100
$PAGE->requires->js_call_amd('block_massaction/block_massaction', 'init', array($jsdata));
101-
102-
$formhtml = $this->get_form_html(
103-
$COURSE->id,
104-
$COURSE->format,
105-
$this->instance->id,
106-
$_SERVER['REQUEST_URI']
107-
);
108-
109-
$this->content->text .= html_writer::start_tag('div',
110-
array('class' => 'block-massaction-jsenabled'));
111-
$this->content->text .= html_writer::start_tag('div',
112-
array('class' => 'block-massaction-select'));
113-
$this->content->text .= html_writer::start_tag('ul');
114-
$this->content->text .= html_writer::start_tag('li');
115-
$this->content->text .= html_writer::start_tag('a',
116-
array('id' => 'block-massaction-selectall',
117-
'href' => 'javascript:void(0);',
118-
'title' => get_string('selectall', 'block_massaction')));
119-
$this->content->text .= get_string('selectall', 'block_massaction');
120-
$this->content->text .= html_writer::end_tag('a');
121-
$this->content->text .= html_writer::end_tag('li');
122-
$this->content->text .= html_writer::start_tag('li');
123-
$this->content->text .= html_writer::start_tag('select',
124-
array('id' => 'block-massaction-selectsome'));
125-
$this->content->text .= html_writer::start_tag('option',
126-
array('value' => 'all'));
127-
$this->content->text .= get_string('allitems', 'block_massaction');
128-
$this->content->text .= html_writer::end_tag('option');
129-
$this->content->text .= html_writer::end_tag('select');
130-
$this->content->text .= html_writer::end_tag('li');
131-
132-
$this->content->text .= html_writer::start_tag('li');
133-
$this->content->text .= html_writer::start_tag('a',
134-
array('id' => 'block-massaction-selectnone',
135-
'href' => 'javascript:void(0);',
136-
'title' => get_string('selectnone', 'block_massaction')));
137-
$this->content->text .= get_string('selectnone', 'block_massaction');
138-
$this->content->text .= html_writer::end_tag('a');
139-
$this->content->text .= html_writer::end_tag('li');
140-
$this->content->text .= html_writer::end_tag('ul');
141-
$this->content->text .= html_writer::end_tag('div');
142-
143-
$this->content->text .= html_writer::start_tag('div',
144-
array('class' => 'block-massaction-action'));
145-
$this->content->text .= html_writer::start_tag('ul');
146-
$this->content->text .= html_writer::start_tag('li');
147-
$this->content->text .= get_string('withselected', 'block_massaction').':';
148-
$this->content->text .= html_writer::end_tag('li');
149-
150-
// Print the action links.
151-
$actionicons = array(
152-
'outdent' => 't/left',
153-
'indent' => 't/right',
154-
'hide' => 't/show',
155-
'show' => 't/hide',
156-
'delete' => 't/delete'
157-
);
158-
159-
foreach ($actionicons as $action => $iconpath) {
160-
$this->content->text .= html_writer::start_tag('li');
161-
$this->content->text .= html_writer::start_tag('a',
162-
array('id' => 'block-massaction-' .$action,
163-
'class' => 'massaction-action',
164-
'href' => 'javascript:void(0);'));
165-
$this->content->text .= $OUTPUT->pix_icon($iconpath, get_string('action_'.$action, 'block_massaction'));
166-
$this->content->text .= get_string('action_'.$action, 'block_massaction');
167-
$this->content->text .= html_writer::end_tag('a');
168-
$this->content->text .= html_writer::end_tag('li');
169-
}
170-
$this->content->text .= html_writer::end_tag('ul');
171-
$this->content->text .= html_writer::end_tag('div');
172101

173-
// Print the action links.
102+
// Open main content div for the plugin.
103+
// Having a div with a manually assigned class allows us to style it.
104+
$this->content->text .= html_writer::start_tag('div',
105+
array('class' => 'block-massaction-jsenabled'));
106+
107+
// Get select elements.
108+
$this->content->text .= $this->get_select_section();
109+
110+
// Get action links.
111+
$this->content->text .= $this->get_action_section();
112+
113+
// Get operation menus.
114+
// The actions in this array have a topic or section in the course as their target.
174115
$actions = array(
175116
'move',
176117
'clone'
177118
);
178119

179-
$this->content->text .= html_writer::start_tag('div',
180-
array('class' => 'block-massaction-operation'));
181-
$this->content->text .= html_writer::start_tag('ul');
182-
foreach ($actions as $action) {
183-
$this->content->text .= html_writer::start_tag('li');
184-
$this->content->text .= html_writer::start_tag('select',
185-
array('id' => 'block-massaction-' . $action));
186-
$this->content->text .= html_writer::start_tag('option',
187-
array('value' => ''));
188-
$this->content->text .= get_string('action_' . $action, 'block_massaction');
189-
$this->content->text .= html_writer::end_tag('option');
190-
$this->content->text .= html_writer::end_tag('select');
191-
$this->content->text .= html_writer::end_tag('li');
192-
}
193-
$this->content->text .= html_writer::end_tag('ul');
194-
$this->content->text .= html_writer::end_tag('div');
120+
$this->content->text .= $this->get_operation_section($actions);
195121

196-
$this->content->text .= <<< EOB
197-
{$formhtml}
198-
EOB;
122+
// Get hidden form html.
123+
$this->content->text .= $this->get_form_html($COURSE->id, $COURSE->format, $this->instance->id,
124+
$_SERVER['REQUEST_URI']);
125+
126+
// Get help section.
127+
$this->content->text .= $this->get_help_section();
199128

200-
$this->content->text .= html_writer::start_tag('div',
201-
array('id' => 'block-massaction-help-icon'));
202-
$this->content->text .= $OUTPUT->help_icon('usage', 'block_massaction');
203129
$this->content->text .= html_writer::end_tag('div');
204-
205-
$this->content->text .= html_writer::end_tag('div');
206130
}
207131

208132
return $this->content;
@@ -248,6 +172,145 @@ private function get_section_data($course) {
248172
return $jsdata;
249173
}
250174

175+
/**
176+
* Gets the html content for the "Select All" and "Deselect All" links,
177+
* and the "Select All in Section" menu.
178+
*
179+
* @return string The html for this section.
180+
*/
181+
private function get_select_section() {
182+
$selecttext = html_writer::start_tag('div', array('class' => 'block-massaction-select'));
183+
$selecttext .= html_writer::start_tag('ul');
184+
185+
// Create "Select All" link.
186+
$selecttext .= $this->get_item_content('a', 'selectall',
187+
array('id' => 'block-massaction-selectall',
188+
'href' => 'javascript:void(0);',
189+
'title' => get_string('selectall', 'block_massaction')));
190+
191+
// Open "Select All" menu.
192+
$selecttext .= $this->get_item_content('select', 'allitems',
193+
array('id' => 'block-massaction-selectsome'), array('value' => 'all'));
194+
195+
// Create "Deselect All" link.
196+
$selecttext .= $this->get_item_content('a', 'selectnone',
197+
array('id' => 'block-massaction-selectnone',
198+
'href' => 'javascript:void(0);',
199+
'title' => get_string('selectnone', 'block_massaction')));
200+
201+
$selecttext .= html_writer::end_tag('ul');
202+
$selecttext .= html_writer::end_tag('div');
203+
204+
return $selecttext;
205+
}
206+
207+
/**
208+
* Gets the html content for the action links, e.g. "Outdent", "Indent", etc.
209+
*
210+
* @return string The html for this section.
211+
*/
212+
private function get_action_section() {
213+
$actiontext = html_writer::start_tag('div', array('class' => 'block-massaction-action'));
214+
$actiontext .= html_writer::start_tag('ul');
215+
$actiontext .= html_writer::start_tag('li');
216+
$actiontext .= get_string('withselected', 'block_massaction').':';
217+
$actiontext .= html_writer::end_tag('li');
218+
219+
// Print the action links.
220+
$actionicons = array(
221+
'outdent' => 't/left',
222+
'indent' => 't/right',
223+
'hide' => 't/show',
224+
'show' => 't/hide',
225+
'delete' => 't/delete'
226+
);
227+
228+
foreach ($actionicons as $action => $iconpath) {
229+
$actiontext .= $this->get_item_content('a', 'action_' . $action,
230+
array('id' => 'block-massaction-' . $action,
231+
'class' => 'massaction-action',
232+
'href' => 'javascript:void(0);'),
233+
array(), $iconpath);
234+
}
235+
236+
$actiontext .= html_writer::end_tag('ul');
237+
$actiontext .= html_writer::end_tag('div');
238+
239+
return $actiontext;
240+
}
241+
242+
/**
243+
* Gets the html content for the operations menus.
244+
*
245+
* @param array $actions The operation actions that have a topic or section as their target.
246+
*
247+
* @return string The html for this section.
248+
*/
249+
private function get_operation_section($actions) {
250+
$opstext = html_writer::start_tag('div', array('class' => 'block-massaction-operation'));
251+
$opstext .= html_writer::start_tag('ul');
252+
253+
foreach ($actions as $action) {
254+
$opstext .= $this->get_item_content('select', 'action_' . $action,
255+
array('id' => 'block-massaction-' . $action));
256+
}
257+
258+
$opstext .= html_writer::end_tag('ul');
259+
$opstext .= html_writer::end_tag('div');
260+
261+
return $opstext;
262+
}
263+
264+
/**
265+
* Gets the html content for the help section.
266+
*
267+
* @return string The html for this section.
268+
*/
269+
private function get_help_section() {
270+
global $OUTPUT;
271+
272+
$helptext = html_writer::start_tag('div', array('id' => 'block-massaction-help-icon'));
273+
$helptext .= $OUTPUT->help_icon('usage', 'block_massaction');
274+
$helptext .= html_writer::end_tag('div');
275+
276+
return $helptext;
277+
}
278+
279+
/**
280+
* Encapsulates the logic required to write the individual links and drop menus for the block.
281+
*
282+
* @param string $itemtag The element's html tag, e.g. 'select', 'a', etc.
283+
* @param string $itemstr The element's language string identifier, e.g. 'action_delete', etc.
284+
* @param array $itemattrs The element's id and class, if any.
285+
* @param array $itemval The value for the select menu's first option.
286+
* @param string $iconpath The pix_icon path for the link.
287+
*
288+
* @return string HTML content for the requested element.
289+
*/
290+
private function get_item_content($itemtag, $itemstr, $itemattrs = array(), $itemval = array('value' => ''), $iconpath = '') {
291+
global $OUTPUT;
292+
293+
$itemtext = html_writer::start_tag('li');
294+
$itemtext .= html_writer::start_tag($itemtag, $itemattrs);
295+
296+
if ($itemtag == 'select') {
297+
$itemtext .= html_writer::start_tag('option', $itemval);
298+
$itemtext .= get_string($itemstr, 'block_massaction');
299+
$itemtext .= html_writer::end_tag('option');
300+
} else {
301+
if ($iconpath != '') {
302+
$itemtext .= $OUTPUT->pix_icon($iconpath, get_string($itemstr, 'block_massaction'));
303+
}
304+
305+
$itemtext .= get_string($itemstr, 'block_massaction');
306+
}
307+
308+
$itemtext .= html_writer::end_tag($itemtag);
309+
$itemtext .= html_writer::end_tag('li');
310+
311+
return $itemtext;
312+
}
313+
251314
/**
252315
* Creates the form html for the hidden form submitted when the user chooses the action to apply
253316
* to the selected modules.

styles.css

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
margin-left: -16px;
33
}
44

5-
.block-massaction-jsenabled li {
6-
list-style: none ;
5+
.block-massaction-select > ul,
6+
.block-massaction-action > ul,
7+
.block-massaction-operation > ul {
8+
list-style-type: none ;
79
}
810

911
.block-massaction-action a .icon {

0 commit comments

Comments
 (0)