Commit 96a36e92 authored by Lisa Seeto's avatar Lisa Seeto Committed by Cecilia Vela Gurovic

Bug 1853387: jsform field defaults to no when field is disabled

- alter the way checkbox disabled fields are processed if they have
default values supplied
- add get_element_option(name, option) to Pieform

behatnotneeded

Change-Id: I0b48cf148716acb70b865e3339a6b9fadfd762bb
Signed-off-by: default avatarLisa Seeto <lisaseeto@catalyst.net.nz>
(cherry picked from commit e99cb702)
parent a3aa7b0a
......@@ -320,17 +320,17 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
if (!empty($values['smartevidence'])) {
// Check that the new smartevidence standard we are changing to is not alreay covered by another annotation block
$block = $form->get_element('blockconfig');
$view = $form->get_element('id');
$block = $form->get_element_option('blockconfig', 'value');
$viewid = $form->get_element_option('id', 'value');
require_once('view.php');
$view = new View($view['value']);
$view = new View($viewid);
$collection = $view->get('collection');
if (is_object($collection) && $collection->get('framework')) {
$annotationid = get_field('framework_evidence', 'annotation',
'view', $view->get('id'),
'framework', $collection->get('framework'),
'element', $values['smartevidence']);
if ($annotationid && $annotationid != $block['value']) {
if ($annotationid && ($annotationid != $block)) {
$result['message'] = get_string('annotationclash', 'module.framework');
$form->set_error('smartevidence', $result['message']);
$form->reply(PIEFORM_ERR, $result);
......
......@@ -121,15 +121,13 @@ exit;
function newblog_submit(Pieform $form, $values) {
global $USER;
$data = $form->get_element('institution');
$group = $form->get_element('group');
if ($data['value'] != false) {
if ($institution = $form->get_element_option('institution', 'value')) {
ArtefactTypeBlog::new_blog(null, $values);
redirect('/artefact/blog/index.php?institution=' . $data['value']);
redirect('/artefact/blog/index.php?institution=' . $institution);
}
else if ($group['value'] != false) {
else if ($group = $form->get_element_option('group', 'value')) {
ArtefactTypeBlog::new_blog(null, $values);
redirect('/artefact/blog/index.php?group=' . $group['value']);
redirect('/artefact/blog/index.php?group=' . $group);
}
else {
ArtefactTypeBlog::new_blog($USER, $values);
......@@ -141,13 +139,11 @@ function newblog_submit(Pieform $form, $values) {
* This function gets called to cancel a submission.
*/
function newblog_cancel_submit(Pieform $form) {
$data = $form->get_element('institution');
$group = $form->get_element('group');
if ($data['value'] != false) {
redirect('/artefact/blog/index.php?institution=' . $data['value']);
if ($institution = $form->get_element_option('institution', 'value')) {
redirect('/artefact/blog/index.php?institution=' . $institution);
}
if ($group['value'] != false) {
redirect('/artefact/blog/index.php?group=' . $group['value']);
if ($group = $form->get_element_option('group', 'value')) {
redirect('/artefact/blog/index.php?group=' . $group);
}
else {
redirect('/artefact/blog/index.php');
......
......@@ -123,13 +123,11 @@ function editblog_submit(Pieform $form, $values) {
global $USER;
ArtefactTypeBlog::edit_blog($USER, $values);
$institution = $form->get_element('institution');
$group = $form->get_element('group');
if (!empty($institution['value'])) {
redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&institution=' . $institution['value']);
if ($institution = $form->get_element_option('institution', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&institution=' . $institution);
}
else if (!empty($group['value'])) {
redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&group=' . $group['value']);
else if ($group = $form->get_element_option('group', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&group=' . $group);
}
else {
redirect('/artefact/blog/view/index.php?id=' . $values['id']);
......@@ -142,11 +140,11 @@ function editblog_submit(Pieform $form, $values) {
*/
function editblog_cancel_submit(Pieform $form) {
$id = param_integer('id');
if ($data = $form->get_element('institution')) {
redirect('/artefact/blog/view/index.php?id=' . $id . '&institution=' . $data['value']);
if ($institution = $form->get_element_option('institution', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $id . '&institution=' . $institution);
}
else if ($data = $form->get_element('group')) {
redirect('/artefact/blog/view/index.php?id=' . $id . '&group=' . $data['value']);
else if ($group = $form->get_element_option('group', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $id . '&group=' . $group);
}
else {
redirect('/artefact/blog/view/index.php?id=' . $id);
......
......@@ -941,6 +941,25 @@ class Pieform {/*{{{*/
throw new PieformException('Element "' . $name . '" cannot be found');
}/*}}}*/
/**
* Returns the element's option with the given name. Throws a PieformException if the
* element's cannot be found.
*
* @param string $name The name of the element to find
* @param string $option The name of the option to find, i.e 'disabled'
* @return array The element's option
* @throws PieformException If the element could not be found
*/
public function get_element_option($name, $option) {/*{{{*/
if (!isset($this->elementrefs[$name])) {
throw new PieformException('Element "' . $name . '" cannot be found');
}
if (!isset($this->elementrefs[$name][$option])) {
throw new PieformException('Element "' . $name . '" option "' . $option . '" cannot be found');
}
return $this->elementrefs[$name][$option];
}/*}}}*/
/**
* Sends a message back to a form
*/
......
......@@ -45,8 +45,13 @@ function pieform_element_checkbox(Pieform $form, $element) {/*{{{*/
$checked = true;
}
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if ($form->is_submitted() && isset($global[$element['name']])) {
$checked = true;
if ($form->is_submitted()) {
if (isset($global[$element['name']])) {
$checked = true;
}
else if ((isset($element['disabled']) && $element['disabled']) && isset($element['defaultvalue'])) {
$checked = (bool) $element['defaultvalue'];
}
}
else if (!$form->is_submitted() && !empty($element['defaultvalue'])) {
$checked = true;
......@@ -80,6 +85,9 @@ function pieform_element_checkbox_get_value(Pieform $form, $element) {/*{{{*/
if(isset($global[$name])) {
return true;
}
else if (isset($element['disabled']) && $element['disabled']) {
return (bool) $element['defaultvalue'];
}
return false;
}
......
......@@ -4585,17 +4585,17 @@ function format_class($extra, $column, $class = 'search-results-sort-column') {
}
function reportconfigform_cancel_submit(Pieform $form) {
$submitelement = $form->get_element('submit');
redirect($submitelement['goto']);
$goto = $form->get_element_option('submit', 'goto');
redirect($goto);
}
function reportconfigform_submit(Pieform $form, $values) {
global $SESSION;
$submitelement = $form->get_element('submit');
$goto = $form->get_element_option('submit', 'goto');
// Get the type/subtype values from select field
list($type, $subtype) = explode('_', $values['typesubtype']);
$submitelement['goto'] .= '&type=' . $type . '&subtype=' . $subtype;
$goto .= '&type=' . $type . '&subtype=' . $subtype;
$SESSION->set('columnsforstats', null);
$extra = array();
......@@ -4615,7 +4615,7 @@ function reportconfigform_submit(Pieform $form, $values) {
$form->reply(PIEFORM_OK, array(
'message' => get_string('applyingfilters', 'statistics'),
'goto' => $submitelement['goto'],
'goto' => $goto,
)
);
}
......
......@@ -553,13 +553,13 @@ function designskinform_submit(Pieform $form, $values) {
$viewskin['type'] = $values['viewskin_access'];
$viewskin['viewskin'] = $skin;
$submitelement = $form->get_element('submitform');
$subgoto = $form->get_element_option('submitform', 'goto');
Skin::create($viewskin);
if ($form->submitted_by_js()) {
$result = array(
'error' => false,
'message' => get_string('skinsaved', 'skin'),
'goto' => $submitelement['goto'],
'goto' => $subgoto,
);
// Redirect back to the page from within the iframe
$SESSION->add_ok_msg($result['message']);
......
......@@ -117,7 +117,7 @@ $smarty->display('view/delete.tpl');
function deleteview_submit(Pieform $form, $values) {
global $SESSION, $USER, $viewid, $groupid, $institution;
$submitelement = $form->get_element('submit');
$goto = $form->get_element_option('submit', 'goto');
$view = new View($viewid, null);
if (View::can_remove_viewtype($view->get('type')) || $USER->get('admin')) {
$collectionid = $view->collection_id();
......@@ -130,5 +130,5 @@ function deleteview_submit(Pieform $form, $values) {
else {
$SESSION->add_error_msg(get_string('cantdeleteview', 'view'));
}
redirect( $submitelement['goto']);
redirect($goto);
}
......@@ -78,8 +78,8 @@ if ($delete) {
}
function allocate_client_connection_cancel_submit(Pieform $form) {
$institution = $form->get_element('i');
redirect(get_config('wwwroot') . 'webservice/admin/connections.php?i=' . $institution['value']);
$institution = $form->get_element_option('i', 'value');
redirect(get_config('wwwroot') . 'webservice/admin/connections.php?i=' . $institution);
}
function allocate_client_connection_validate(Pieform $form, $values) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment