Skip to content

Commit

Permalink
Default select category in topup form
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Jun 23, 2024
1 parent a0e1447 commit 987095b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions classes/form/topup_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function definition() {

if (count($catoptions) > 1) {
$mform->addElement('select', 'category', $categorytitle, $catoptions);
$this->set_default_category();
} else {
$mform->addElement('hidden', 'category');
$mform->setType('category', PARAM_INT);
Expand Down Expand Up @@ -191,4 +192,32 @@ public function definition() {
$this->add_action_buttons(false, get_string('topup', 'enrol_wallet'));
$this->set_display_vertical();
}

/**
* Default select the category option according to the
* context of the page.
*/
protected function set_default_category() {
global $PAGE;
$mform = $this->_form;
if (empty($PAGE->context)) {
return;
}
if ($PAGE->context->contextlevel == CONTEXT_COURSECAT) {
$mform->setDefault('category', $PAGE->context->instanceid);
return;
}
if ($coursectx = $PAGE->context->get_course_context(false)) {
$mform->setDefault('category', $coursectx->get_parent_context()->instanceid);
return;
}
if (!empty($PAGE->category)) {
$mform->setDefault('category', $PAGE->category);
return;
}
if (!empty($PAGE->course->category)) {
$mform->setDefault('category', $PAGE->course->category);
return;
}
}
}

0 comments on commit 987095b

Please sign in to comment.