Commit e99cb702 authored by Lisa Seeto's avatar Lisa Seeto Committed by Robert Lyon

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>
parent 2d14b440
...@@ -320,17 +320,17 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype { ...@@ -320,17 +320,17 @@ class PluginBlocktypeAnnotation extends MaharaCoreBlocktype {
if (!empty($values['smartevidence'])) { if (!empty($values['smartevidence'])) {
// Check that the new smartevidence standard we are changing to is not alreay covered by another annotation block // Check that the new smartevidence standard we are changing to is not alreay covered by another annotation block
$block = $form->get_element('blockconfig'); $block = $form->get_element_option('blockconfig', 'value');
$view = $form->get_element('id'); $viewid = $form->get_element_option('id', 'value');
require_once('view.php'); require_once('view.php');
$view = new View($view['value']); $view = new View($viewid);
$collection = $view->get('collection'); $collection = $view->get('collection');
if (is_object($collection) && $collection->get('framework')) { if (is_object($collection) && $collection->get('framework')) {
$annotationid = get_field('framework_evidence', 'annotation', $annotationid = get_field('framework_evidence', 'annotation',
'view', $view->get('id'), 'view', $view->get('id'),
'framework', $collection->get('framework'), 'framework', $collection->get('framework'),
'element', $values['smartevidence']); 'element', $values['smartevidence']);
if ($annotationid && $annotationid != $block['value']) { if ($annotationid && ($annotationid != $block)) {
$result['message'] = get_string('annotationclash', 'module.framework'); $result['message'] = get_string('annotationclash', 'module.framework');
$form->set_error('smartevidence', $result['message']); $form->set_error('smartevidence', $result['message']);
$form->reply(PIEFORM_ERR, $result); $form->reply(PIEFORM_ERR, $result);
......
...@@ -121,15 +121,13 @@ exit; ...@@ -121,15 +121,13 @@ exit;
function newblog_submit(Pieform $form, $values) { function newblog_submit(Pieform $form, $values) {
global $USER; global $USER;
$data = $form->get_element('institution'); if ($institution = $form->get_element_option('institution', 'value')) {
$group = $form->get_element('group');
if ($data['value'] != false) {
ArtefactTypeBlog::new_blog(null, $values); 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); ArtefactTypeBlog::new_blog(null, $values);
redirect('/artefact/blog/index.php?group=' . $group['value']); redirect('/artefact/blog/index.php?group=' . $group);
} }
else { else {
ArtefactTypeBlog::new_blog($USER, $values); ArtefactTypeBlog::new_blog($USER, $values);
...@@ -141,13 +139,11 @@ function newblog_submit(Pieform $form, $values) { ...@@ -141,13 +139,11 @@ function newblog_submit(Pieform $form, $values) {
* This function gets called to cancel a submission. * This function gets called to cancel a submission.
*/ */
function newblog_cancel_submit(Pieform $form) { function newblog_cancel_submit(Pieform $form) {
$data = $form->get_element('institution'); if ($institution = $form->get_element_option('institution', 'value')) {
$group = $form->get_element('group'); redirect('/artefact/blog/index.php?institution=' . $institution);
if ($data['value'] != false) {
redirect('/artefact/blog/index.php?institution=' . $data['value']);
} }
if ($group['value'] != false) { if ($group = $form->get_element_option('group', 'value')) {
redirect('/artefact/blog/index.php?group=' . $group['value']); redirect('/artefact/blog/index.php?group=' . $group);
} }
else { else {
redirect('/artefact/blog/index.php'); redirect('/artefact/blog/index.php');
......
...@@ -123,13 +123,11 @@ function editblog_submit(Pieform $form, $values) { ...@@ -123,13 +123,11 @@ function editblog_submit(Pieform $form, $values) {
global $USER; global $USER;
ArtefactTypeBlog::edit_blog($USER, $values); ArtefactTypeBlog::edit_blog($USER, $values);
$institution = $form->get_element('institution'); if ($institution = $form->get_element_option('institution', 'value')) {
$group = $form->get_element('group'); redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&institution=' . $institution);
if (!empty($institution['value'])) {
redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&institution=' . $institution['value']);
} }
else if (!empty($group['value'])) { else if ($group = $form->get_element_option('group', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&group=' . $group['value']); redirect('/artefact/blog/view/index.php?id=' . $values['id'] . '&group=' . $group);
} }
else { else {
redirect('/artefact/blog/view/index.php?id=' . $values['id']); redirect('/artefact/blog/view/index.php?id=' . $values['id']);
...@@ -142,11 +140,11 @@ function editblog_submit(Pieform $form, $values) { ...@@ -142,11 +140,11 @@ function editblog_submit(Pieform $form, $values) {
*/ */
function editblog_cancel_submit(Pieform $form) { function editblog_cancel_submit(Pieform $form) {
$id = param_integer('id'); $id = param_integer('id');
if ($data = $form->get_element('institution')) { if ($institution = $form->get_element_option('institution', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $id . '&institution=' . $data['value']); redirect('/artefact/blog/view/index.php?id=' . $id . '&institution=' . $institution);
} }
else if ($data = $form->get_element('group')) { else if ($group = $form->get_element_option('group', 'value')) {
redirect('/artefact/blog/view/index.php?id=' . $id . '&group=' . $data['value']); redirect('/artefact/blog/view/index.php?id=' . $id . '&group=' . $group);
} }
else { else {
redirect('/artefact/blog/view/index.php?id=' . $id); redirect('/artefact/blog/view/index.php?id=' . $id);
......
...@@ -941,6 +941,25 @@ class Pieform {/*{{{*/ ...@@ -941,6 +941,25 @@ class Pieform {/*{{{*/
throw new PieformException('Element "' . $name . '" cannot be found'); 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 * Sends a message back to a form
*/ */
......
...@@ -45,8 +45,13 @@ function pieform_element_checkbox(Pieform $form, $element) {/*{{{*/ ...@@ -45,8 +45,13 @@ function pieform_element_checkbox(Pieform $form, $element) {/*{{{*/
$checked = true; $checked = true;
} }
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST; $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if ($form->is_submitted() && isset($global[$element['name']])) { if ($form->is_submitted()) {
$checked = true; 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'])) { else if (!$form->is_submitted() && !empty($element['defaultvalue'])) {
$checked = true; $checked = true;
...@@ -80,6 +85,9 @@ function pieform_element_checkbox_get_value(Pieform $form, $element) {/*{{{*/ ...@@ -80,6 +85,9 @@ function pieform_element_checkbox_get_value(Pieform $form, $element) {/*{{{*/
if(isset($global[$name])) { if(isset($global[$name])) {
return true; return true;
} }
else if (isset($element['disabled']) && $element['disabled']) {
return (bool) $element['defaultvalue'];
}
return false; return false;
} }
......
...@@ -4585,17 +4585,17 @@ function format_class($extra, $column, $class = 'search-results-sort-column') { ...@@ -4585,17 +4585,17 @@ function format_class($extra, $column, $class = 'search-results-sort-column') {
} }
function reportconfigform_cancel_submit(Pieform $form) { function reportconfigform_cancel_submit(Pieform $form) {
$submitelement = $form->get_element('submit'); $goto = $form->get_element_option('submit', 'goto');
redirect($submitelement['goto']); redirect($goto);
} }
function reportconfigform_submit(Pieform $form, $values) { function reportconfigform_submit(Pieform $form, $values) {
global $SESSION; global $SESSION;
$submitelement = $form->get_element('submit'); $goto = $form->get_element_option('submit', 'goto');
// Get the type/subtype values from select field // Get the type/subtype values from select field
list($type, $subtype) = explode('_', $values['typesubtype']); list($type, $subtype) = explode('_', $values['typesubtype']);
$submitelement['goto'] .= '&type=' . $type . '&subtype=' . $subtype; $goto .= '&type=' . $type . '&subtype=' . $subtype;
$SESSION->set('columnsforstats', null); $SESSION->set('columnsforstats', null);
$extra = array(); $extra = array();
...@@ -4615,7 +4615,7 @@ function reportconfigform_submit(Pieform $form, $values) { ...@@ -4615,7 +4615,7 @@ function reportconfigform_submit(Pieform $form, $values) {
$form->reply(PIEFORM_OK, array( $form->reply(PIEFORM_OK, array(
'message' => get_string('applyingfilters', 'statistics'), 'message' => get_string('applyingfilters', 'statistics'),
'goto' => $submitelement['goto'], 'goto' => $goto,
) )
); );
} }
......
...@@ -553,13 +553,13 @@ function designskinform_submit(Pieform $form, $values) { ...@@ -553,13 +553,13 @@ function designskinform_submit(Pieform $form, $values) {
$viewskin['type'] = $values['viewskin_access']; $viewskin['type'] = $values['viewskin_access'];
$viewskin['viewskin'] = $skin; $viewskin['viewskin'] = $skin;
$submitelement = $form->get_element('submitform'); $subgoto = $form->get_element_option('submitform', 'goto');
Skin::create($viewskin); Skin::create($viewskin);
if ($form->submitted_by_js()) { if ($form->submitted_by_js()) {
$result = array( $result = array(
'error' => false, 'error' => false,
'message' => get_string('skinsaved', 'skin'), 'message' => get_string('skinsaved', 'skin'),
'goto' => $submitelement['goto'], 'goto' => $subgoto,
); );
// Redirect back to the page from within the iframe // Redirect back to the page from within the iframe
$SESSION->add_ok_msg($result['message']); $SESSION->add_ok_msg($result['message']);
......
...@@ -117,7 +117,7 @@ $smarty->display('view/delete.tpl'); ...@@ -117,7 +117,7 @@ $smarty->display('view/delete.tpl');
function deleteview_submit(Pieform $form, $values) { function deleteview_submit(Pieform $form, $values) {
global $SESSION, $USER, $viewid, $groupid, $institution; global $SESSION, $USER, $viewid, $groupid, $institution;
$submitelement = $form->get_element('submit'); $goto = $form->get_element_option('submit', 'goto');
$view = new View($viewid, null); $view = new View($viewid, null);
if (View::can_remove_viewtype($view->get('type')) || $USER->get('admin')) { if (View::can_remove_viewtype($view->get('type')) || $USER->get('admin')) {
$collectionid = $view->collection_id(); $collectionid = $view->collection_id();
...@@ -130,5 +130,5 @@ function deleteview_submit(Pieform $form, $values) { ...@@ -130,5 +130,5 @@ function deleteview_submit(Pieform $form, $values) {
else { else {
$SESSION->add_error_msg(get_string('cantdeleteview', 'view')); $SESSION->add_error_msg(get_string('cantdeleteview', 'view'));
} }
redirect( $submitelement['goto']); redirect($goto);
} }
...@@ -78,8 +78,8 @@ if ($delete) { ...@@ -78,8 +78,8 @@ if ($delete) {
} }
function allocate_client_connection_cancel_submit(Pieform $form) { function allocate_client_connection_cancel_submit(Pieform $form) {
$institution = $form->get_element('i'); $institution = $form->get_element_option('i', 'value');
redirect(get_config('wwwroot') . 'webservice/admin/connections.php?i=' . $institution['value']); redirect(get_config('wwwroot') . 'webservice/admin/connections.php?i=' . $institution);
} }
function allocate_client_connection_validate(Pieform $form, $values) { 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