Skip to content

Commit 91c0dbd

Browse files
committed
OWORK-1032 move user allocation actions into extra menu
1 parent 7ea69d8 commit 91c0dbd

11 files changed

+186
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
16+
17+
namespace enrol_programs\hook\extra_menu;
18+
19+
/**
20+
* Extra menu in user allocation tab for program.
21+
*
22+
* @package enrol_programs
23+
* @copyright 2024 Open LMS
24+
* @author Petr Skoda
25+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
final class management_program_users extends \local_openlms\hook\extra_menu {
28+
/** @var \stdClass program record */
29+
protected $program;
30+
31+
/**
32+
* Create hook for extra menu.
33+
*
34+
* @param \stdClass $program
35+
*/
36+
public function __construct(\stdClass $program) {
37+
$dropdown = new \local_openlms\output\extra_menu\dropdown(
38+
get_string('extra_menu_management_program_users', 'enrol_programs'));
39+
parent::__construct($dropdown);
40+
$this->program = $program;
41+
}
42+
43+
/**
44+
* Returns program of user allocation page.
45+
*
46+
* @return \stdClass program record
47+
*/
48+
public function get_program(): \stdClass {
49+
return $this->program;
50+
}
51+
52+
/**
53+
* Hook purpose description in Markdown format
54+
* used on Hooks overview page.
55+
*
56+
* @return string
57+
*/
58+
public static function get_hook_description(): string {
59+
return 'Additions to "Users actions" extra menu in Users tab in program.';
60+
}
61+
}

classes/local/source/base.php

+12
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public static function allocation_delete_supported(stdClass $program, stdClass $
140140
/**
141141
* Allocation related buttons for program management page.
142142
*
143+
* @deprecated
144+
*
143145
* @param stdClass $program
144146
* @param stdClass $source
145147
* @return array
@@ -148,6 +150,16 @@ public static function get_management_program_users_buttons(\stdClass $program,
148150
return [];
149151
}
150152

153+
/**
154+
* Source related extra menu items for program allocation tab.
155+
*
156+
* @param \enrol_programs\hook\extra_menu\management_program_users $menu
157+
* @param stdClass $source source record
158+
*/
159+
public static function add_management_program_users_extra_actions(
160+
\enrol_programs\hook\extra_menu\management_program_users $menu, stdClass $source): void {
161+
}
162+
151163
/**
152164
* Returns list of actions available in Program catalogue.
153165
*

classes/local/source/manual.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -130,34 +130,33 @@ public static function is_allocation_possible(\stdClass $program, \stdClass $sou
130130
}
131131

132132
/**
133-
* Allocation related buttons for program management page.
133+
* Source related extra menu items for program allocation tab.
134134
*
135-
* @param stdClass $program
135+
* @param \enrol_programs\hook\extra_menu\management_program_users $menu
136136
* @param stdClass $source
137-
* @return array
138137
*/
139-
public static function get_management_program_users_buttons(\stdClass $program, \stdClass $source): array {
140-
global $PAGE;
138+
public static function add_management_program_users_extra_actions(
139+
\enrol_programs\hook\extra_menu\management_program_users $menu, stdClass $source): void {
141140

142-
/** @var \local_openlms\output\dialog_form\renderer $dialogformoutput */
143-
$dialogformoutput = $PAGE->get_renderer('local_openlms', 'dialog_form');
141+
$program = $menu->get_program();
142+
if ($program->id != $source->programid || $source->type !== self::get_type()) {
143+
throw new \coding_exception('Parameter mismatch detected');
144+
}
144145

145-
if ($source->type !== static::get_type()) {
146-
throw new \coding_exception('invalid instance');
146+
if (!self::is_allocation_possible($program, $source)) {
147+
return;
147148
}
148-
$enabled = self::is_allocation_possible($program, $source);
149+
149150
$context = \context::instance_by_id($program->contextid);
150-
$buttons = [];
151-
if ($enabled && has_capability('enrol/programs:allocate', $context)) {
151+
if (has_capability('enrol/programs:allocate', $context)) {
152152
$url = new \moodle_url('/enrol/programs/management/source_manual_allocate.php', ['sourceid' => $source->id]);
153-
$button = new \local_openlms\output\dialog_form\button($url, get_string('source_manual_allocateusers', 'enrol_programs'));
154-
$buttons[] = $dialogformoutput->render($button);
153+
$link = new \local_openlms\output\dialog_form\link($url, get_string('source_manual_allocateusers', 'enrol_programs'));
154+
$menu->add_dialog_form($link);
155155

156156
$url = new \moodle_url('/enrol/programs/management/source_manual_upload.php', ['sourceid' => $source->id]);
157-
$button = new \local_openlms\output\dialog_form\button($url, get_string('source_manual_uploadusers', 'enrol_programs'));
158-
$buttons[] = $dialogformoutput->render($button);
157+
$link = new \local_openlms\output\dialog_form\link($url, get_string('source_manual_uploadusers', 'enrol_programs'));
158+
$menu->add_dialog_form($link);
159159
}
160-
return $buttons;
161160
}
162161

163162
/**

lang/en/enrol_programs.php

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
$string['evidenceupload_updated'] = 'Completion evidence updated for {$a} users';
9797
$string['evidence_details'] = 'Details';
9898
$string['evidence_detailsdefault'] = 'Default details';
99+
$string['extra_menu_management_program_users'] = 'Users actions';
99100
$string['fixeddate'] = 'At a fixed date';
100101
$string['importallocationend'] = 'Allocation end ({$a})';
101102
$string['importallocationstart'] = 'Allocation start ({$a})';

management/program_users.php

+61-42
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,14 @@
7878
/** @var \enrol_programs\output\catalogue\renderer $catalogueoutput */
7979
$catalogueoutput = $PAGE->get_renderer('enrol_programs', 'catalogue');
8080

81+
$sources = $DB->get_records('enrol_programs_sources', ['programid' => $program->id]);
82+
/** @var \enrol_programs\local\source\base[] $sourceclasses */ // Type hack.
83+
$sourceclasses = \enrol_programs\local\allocation::get_source_classes();
84+
8185
echo $OUTPUT->header();
8286

8387
echo $managementoutput->render_management_program_tabs($program, 'users');
8488

85-
echo '<div class="allocation-filtering">';
86-
// Add search form.
87-
$data = [
88-
'action' => new moodle_url('/enrol/programs/management/program_users.php'),
89-
'inputname' => 'search',
90-
'searchstring' => get_string('search', 'search'),
91-
'query' => $searchquery,
92-
'hiddenfields' => [
93-
(object)['name' => 'id', 'value' => $program->id],
94-
(object)['name' => 'sort', 'value' => $sort],
95-
(object)['name' => 'dir', 'value' => $dir],
96-
(object)['name' => 'status', 'value' => $status],
97-
],
98-
'extraclasses' => 'mb-3'
99-
];
100-
echo $OUTPUT->render_from_template('core/search_input', $data);
101-
$changestatus = new moodle_url($currenturl);
102-
$statusoptions = [
103-
0 => get_string('programstatus_any', 'enrol_programs'),
104-
1 => get_string('programstatus_completed', 'enrol_programs'),
105-
2 => get_string('programstatus_future', 'enrol_programs'),
106-
3 => get_string('programstatus_failed', 'enrol_programs'),
107-
4 => get_string('programstatus_overdue', 'enrol_programs'),
108-
5 => get_string('programstatus_open', 'enrol_programs'),
109-
];
110-
if (!isset($statusoptions[$status])) {
111-
$status = 0;
112-
}
113-
echo $OUTPUT->single_select($currenturl, 'status', $statusoptions, $status, []);
114-
echo '</div>';
115-
echo '<div class="clearfix"></div>';
116-
11789
$allusernamefields = \core_user\fields::get_name_fields(true);
11890
$userfieldsapi = \core_user\fields::for_identity(\context_system::instance(), false)->with_userpic();
11991
$userfieldssql = $userfieldsapi->get_sql('u', false, 'user', 'userid2', false);
@@ -204,12 +176,65 @@
204176
WHERE a.programid = :programid $usersearch";
205177
$totalcount = $DB->count_records_sql($sql, $params);
206178

179+
180+
$extramenu = new \enrol_programs\hook\extra_menu\management_program_users($program);
181+
foreach ($sourceclasses as $sourceclass) {
182+
$sourcetype = $sourceclass::get_type();
183+
$sourcerecord = $DB->get_record('enrol_programs_sources', ['programid' => $program->id, 'type' => $sourcetype]);
184+
if (!$sourcerecord) {
185+
continue;
186+
}
187+
$sourceclass::add_management_program_users_extra_actions($extramenu, $sourcerecord);
188+
}
189+
$canmanageevidence = has_capability('enrol/programs:manageevidence', $context);
190+
if ($totalcount && !$program->archived && $canmanageevidence) {
191+
$url = new \moodle_url('/enrol/programs/management/program_evidence_upload.php', ['programid' => $id]);
192+
$link = new \local_openlms\output\dialog_form\link($url, get_string('evidenceupload', 'enrol_programs'));
193+
$extramenu->add_dialog_form($link);
194+
}
195+
\core\hook\manager::get_instance()->dispatch($extramenu);
196+
197+
echo '<div class="allocation-filtering">';
198+
// Add search form.
199+
$data = [
200+
'action' => new moodle_url('/enrol/programs/management/program_users.php'),
201+
'inputname' => 'search',
202+
'searchstring' => get_string('search', 'search'),
203+
'query' => $searchquery,
204+
'hiddenfields' => [
205+
(object)['name' => 'id', 'value' => $program->id],
206+
(object)['name' => 'sort', 'value' => $sort],
207+
(object)['name' => 'dir', 'value' => $dir],
208+
(object)['name' => 'status', 'value' => $status],
209+
],
210+
'extraclasses' => 'mb-3 float-left'
211+
];
212+
echo $OUTPUT->render_from_template('core/search_input', $data);
213+
$changestatus = new moodle_url($currenturl);
214+
$statusoptions = [
215+
0 => get_string('programstatus_any', 'enrol_programs'),
216+
1 => get_string('programstatus_completed', 'enrol_programs'),
217+
2 => get_string('programstatus_future', 'enrol_programs'),
218+
3 => get_string('programstatus_failed', 'enrol_programs'),
219+
4 => get_string('programstatus_overdue', 'enrol_programs'),
220+
5 => get_string('programstatus_open', 'enrol_programs'),
221+
];
222+
if (!isset($statusoptions[$status])) {
223+
$status = 0;
224+
}
225+
echo '&nbsp';
226+
echo $OUTPUT->single_select($currenturl, 'status', $statusoptions, $status, []);
227+
if ($extramenu->has_items()) {
228+
echo '<div class="float-right">';
229+
echo $OUTPUT->render($extramenu->get_dropdown());
230+
echo '</div>';
231+
}
232+
echo '</div>';
233+
echo '<div class="clearfix"></div>';
234+
207235
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $currenturl);
208236

209-
$sources = $DB->get_records('enrol_programs_sources', ['programid' => $program->id]);
210237
$sourcenames = \enrol_programs\local\allocation::get_source_names();
211-
/** @var \enrol_programs\local\source\base[] $sourceclasses */ // Type hack.
212-
$sourceclasses = \enrol_programs\local\allocation::get_source_classes();
213238
$dateformat = get_string('strftimedatetimeshort');
214239

215240
$data = [];
@@ -344,8 +369,8 @@
344369

345370
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $currenturl);
346371

372+
// Buttons are deprecated here.
347373
$buttons = [];
348-
349374
foreach ($sourceclasses as $sourceclass) {
350375
$sourcetype = $sourceclass::get_type();
351376
$sourcerecord = $DB->get_record('enrol_programs_sources', ['programid' => $program->id, 'type' => $sourcetype]);
@@ -355,12 +380,6 @@
355380
$buttons = array_merge_recursive($buttons, $sourceclass::get_management_program_users_buttons($program, $sourcerecord));
356381
}
357382

358-
$canmanageevidence = has_capability('enrol/programs:manageevidence', $context);
359-
if ($totalcount && !$program->archived && $canmanageevidence) {
360-
$url = new \moodle_url('/enrol/programs/management/program_evidence_upload.php', ['programid' => $id]);
361-
$button = new \local_openlms\output\dialog_form\button($url, get_string('evidenceupload', 'enrol_programs'));
362-
$buttons[] = $dialogformoutput->render($button);
363-
}
364383
if ($buttons) {
365384
$buttons = implode(' ', $buttons);
366385
echo $OUTPUT->box($buttons, 'buttons');

styles.css

-10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@
2121
background-color: rgba(0, 0, 0, 0.03);
2222
}
2323

24-
#page-enrol-programs-management-program_users .allocation-filtering .simplesearchform,
25-
#page-admin-enrol-programs-management-program_users .allocation-filtering .simplesearchform {
26-
float: left;
27-
}
28-
29-
#page-enrol-programs-management-program_users .allocation-filtering .singleselect,
30-
#page-admin-enrol-programs-management-program_users .allocation-filtering .singleselect {
31-
float: right;
32-
}
33-
3424
#page-enrol-programs-documentation #programs_docs img {
3525
width: 100%;
3626
height: 100%;

0 commit comments

Comments
 (0)