Skip to content

Commit

Permalink
Merge pull request #810 from catalyst/809-sql-input-field-width-adjus…
Browse files Browse the repository at this point in the history
…tment

[#809] fix: SQL input field width to improve UX for larger queries
  • Loading branch information
Peterburnett authored Jul 18, 2023
2 parents f18748b + fdeda38 commit 51ec159
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
18 changes: 18 additions & 0 deletions classes/form/step_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ public function definition() {
$this->add_action_buttons();
}

/**
* Allow steps to setup the form depending on current values.
*
* This method is called after definition(), data submission and set_data().
* All form setup that is dependent on form values should go in here.
*/
public function definition_after_data() {
$mform = $this->_form;
$data = $this->get_default_data();

$persistent = $this->get_persistent();
$type = $data->type ?? null;
if (isset($persistent->steptype) || (isset($type) && class_exists($type))) {
$steptype = $persistent->steptype ?? new $type();
$steptype->form_definition_after_data($mform, $data);
}
}

/**
* Prepares and returns an array of dependson options
*
Expand Down
12 changes: 12 additions & 0 deletions classes/local/step/base_step.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ public function form_setup(\MoodleQuickForm &$mform) {
public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
}

/**
* Allow steps to setup the form depending on current values.
*
* This method is called after definition(), data submission and set_data().
* All form setup that is dependent on form values should go in here.
*
* @param \MoodleQuickForm $mform
* @param \stdClass $data
*/
public function form_definition_after_data(\MoodleQuickForm &$mform, \stdClass $data) {
}

/**
* Sets the default types for each input field defined
*
Expand Down
31 changes: 30 additions & 1 deletion classes/local/step/flow_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,39 @@ public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
LIMIT 10";
$sqlexamples = \html_writer::tag('pre', trim($sqlexample, " \t\r\0\x0B"));
$mform->addElement('textarea', 'config_sql', get_string('flow_sql:sql', 'tool_dataflows'),
['cols' => 50, 'rows' => 7, 'style' => 'font: 87.5% monospace;']);
['max_rows' => 40, 'rows' => 5, 'style' => 'font: 87.5% monospace; width: 100%; max-width: 100%']);
$mform->addElement('static', 'config_sql_help', '', get_string('flow_sql:sql_help', 'tool_dataflows', $sqlexamples));
}

/**
* Allow steps to setup the form depending on current values.
*
* This method is called after definition(), data submission and set_data().
* All form setup that is dependent on form values should go in here.
*
* @param \MoodleQuickForm $mform
* @param \stdClass $data
*/
public function form_definition_after_data(\MoodleQuickForm &$mform, \stdClass $data) {
// Validate the data.
$sqllinecount = count(explode(PHP_EOL, trim($data->config_sql)));

// Get the element.
$element = $mform->getElement('config_sql');

// Update the element height based on min/max settings, but preserve
// other existing rules.
$attributes = $element->getAttributes();

// Set the rows at a minimum to the predefined amount in
// form_add_custom_inputs, and expand as content grows up to a maximum.
$attributes['rows'] = min(
$attributes['max_rows'],
max($attributes['rows'], $sqllinecount)
);
$element->setAttributes($attributes);
}

/**
* Execute configured query
*
Expand Down
31 changes: 30 additions & 1 deletion classes/local/step/reader_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
$sqlexamples = \html_writer::tag('pre', trim($sqlexamples, " \t\r\0\x0B"));

$mform->addElement('textarea', 'config_sql', get_string('reader_sql:sql', 'tool_dataflows'),
['cols' => 50, 'rows' => 7, 'style' => 'font: 87.5% monospace;']);
['max_rows' => 40, 'rows' => 5, 'style' => 'font: 87.5% monospace; width: 100%; max-width: 100%']);
$mform->addElement('static', 'config_sql_help', '', get_string('reader_sql:sql_help', 'tool_dataflows', $sqlexamples));

// Counter field.
Expand All @@ -220,6 +220,35 @@ public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
$mform->addElement('static', 'config_countervalue_help', '', get_string('reader_sql:countervalue_help', 'tool_dataflows'));
}

/**
* Allow steps to setup the form depending on current values.
*
* This method is called after definition(), data submission and set_data().
* All form setup that is dependent on form values should go in here.
*
* @param \MoodleQuickForm $mform
* @param \stdClass $data
*/
public function form_definition_after_data(\MoodleQuickForm &$mform, \stdClass $data) {
// Validate the data.
$sqllinecount = count(explode(PHP_EOL, trim($data->config_sql)));

// Get the element.
$element = $mform->getElement('config_sql');

// Update the element height based on min/max settings, but preserve
// other existing rules.
$attributes = $element->getAttributes();

// Set the rows at a minimum to the predefined amount in
// form_add_custom_inputs, and expand as content grows up to a maximum.
$attributes['rows'] = min(
$attributes['max_rows'],
max($attributes['rows'], $sqllinecount)
);
$element->setAttributes($attributes);
}

/**
* Step callback handler
*
Expand Down

0 comments on commit 51ec159

Please sign in to comment.