Skip to content

Commit

Permalink
CONTRIB-3306 - remove reliance on data_submitted for passing data to…
Browse files Browse the repository at this point in the history
… question types - clean vars directly inside qusetion handling functions
  • Loading branch information
danmarsden committed Apr 16, 2012
1 parent a4445fd commit 52d0adf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
10 changes: 8 additions & 2 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ function view() {
/// If Survey was submitted with all required fields completed ($msg is empty),
/// then record the submittal.
$viewform = data_submitted($CFG->wwwroot."/mod/questionnaire/view.php");
if (isset($viewform->submit) && isset($viewform->submittype) &&
if (!empty($viewform->rid)) {
$viewform->rid = (int)$viewform->rid;
}
if (!empty($viewform->sec)) {
$viewform->sec = (int)$viewform->sec;
}
if (confirm_sesskey() && isset($viewform->submit) && isset($viewform->submittype) &&
($viewform->submittype == "Submit Survey") && empty($msg)) {

$this->response_delete($viewform->rid, $viewform->sec);
Expand Down Expand Up @@ -1552,7 +1558,7 @@ function response_insert($sid, $section, $rid, $userid, &$formdata) {

if (!empty($this->questionsbysec[$section])) {
foreach ($this->questionsbysec[$section] as $question) {
$question->insert_response($rid, $formdata);
$question->insert_response($rid);
}
}
return($rid);
Expand Down
91 changes: 44 additions & 47 deletions questiontypes/questiontypes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,56 +201,53 @@ function get_choices() {
/// The following methods are defined by the tables they use. Questions should call the
/// appropriate function based on its table.

function insert_response($rid, $formdata) {
function insert_response($rid) {
$method = 'insert_'.$this->response_table;
if (method_exists($this, $method)) {
return $this->$method($rid, $formdata);
return $this->$method($rid);
} else {
return false;
}
}

function insert_response_bool($rid, $formdata) {
function insert_response_bool($rid) {
global $DB;

if (isset($formdata->{'q'.$this->id})
&& !empty($formdata->{'q'.$this->id}) // if "no answer" then choice is empty (CONTRIB-846)
) {
$val = optional_param('q'.$this->id, '', PARAM_ALPHANUMEXT);
if (!empty($val)) { // if "no answer" then choice is empty (CONTRIB-846)
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
$record->choice_id = $formdata->{'q'.$this->id};
$record->choice_id = $val;
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
} else {
return false;
}
}

function insert_response_text($rid, $formdata) {
function insert_response_text($rid) {
global $DB;

$val = optional_param('q'.$this->id, '', PARAM_CLEAN);
// only insert if non-empty content
if($this->type_id == 10) { // numeric
$formdata->{'q'.$this->id} = ereg_replace("[^0-9.\-]*(-?[0-9]*\.?[0-9]*).*", '\1', $formdata->{'q'.$this->id});
$val = ereg_replace("[^0-9.\-]*(-?[0-9]*\.?[0-9]*).*", '\1', $val);
}

if(ereg("[^ \t\n]",$formdata->{'q'.$this->id})) {
if(ereg("[^ \t\n]",$val)) {
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
$record->response = $formdata->{'q'.$this->id};
$record->response = $val;
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
} else {
return false;
}
}

function insert_response_date($rid, $formdata) {
function insert_response_date($rid) {
global $DB;

$checkdateresult = '';
$checkdateresult = check_date($formdata->{'q'.$this->id});
$thisdate = $formdata->{'q'.$this->id};
$val = optional_param('q'.$this->id, '', PARAM_CLEAN);
$checkdateresult = check_date($val);
$thisdate = $val;
if (substr($checkdateresult,0,5) == 'wrong') {
return false;
}
Expand All @@ -263,70 +260,70 @@ function insert_response_date($rid, $formdata) {
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
}

function insert_resp_single($rid, $formdata) {
function insert_resp_single($rid) {
global $DB;

if(!empty($formdata->{'q'.$this->id})) {
$val = optional_param('q'.$this->id, null, PARAM_CLEAN);
if(!empty($val)) {
foreach ($this->choices as $cid => $choice) {
if (strpos($choice->content, '!other') === 0) {
if (!isset($formdata->{'q'.$this->id.'_'.$cid})) {
$other = optional_param('q'.$this->id.'_'.$cid, null, PARAM_CLEAN);
if (!isset($other)) {
continue;
}
$other = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if(ereg("[^ \t\n]",$other)) {
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
$record->choice_id = $cid;
$record->response = $other;
$resid = $DB->insert_record('questionnaire_response_other', $record);
$formdata->{'q'.$this->id} = $cid;
$val = $cid;
break;
}
}
}
}
if(ereg("other_q([0-9]+)", (isset($formdata->{'q'.$this->id})?$formdata->{'q'.$this->id}:''), $regs)) {
if(ereg("other_q([0-9]+)", (isset($val)?$val:''), $regs)) {
$cid=$regs[1];
if (!isset($formdata->{'q'.$this->id.'_'.$cid})) {
$other = optional_param('q'.$this->id.'_'.$cid, null, PARAM_CLEAN);
if (!isset($other)) {
break; // out of the case
}
$other = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if(ereg("[^ \t\n]",$other)) {
$record = new object;
$record->response_id = $rid;
$record->question_id = $this->id;
$record->choice_id = $cid;
$record->response = $other;
$resid = $DB->insert_record('questionnaire_response_other', $record);
$formdata->{'q'.$this->id} = $cid;
$val = $cid;
}
}
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
$record->choice_id = isset($formdata->{'q'.$this->id}) ? $formdata->{'q'.$this->id} : 0;
$record->choice_id = isset($val) ? $val : 0;
if ($record->choice_id) {// if "no answer" then choice_id is empty (CONTRIB-846)
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
} else {
return false;
}
}

function insert_resp_multiple($rid, $formdata) {
function insert_resp_multiple($rid) {
global $DB;

$val = optional_param('q'.$this->id, null, PARAM_CLEAN);
foreach ($this->choices as $cid => $choice) {
if (strpos($choice->content, '!other') === 0) {
if (!isset($formdata->{'q'.$this->id.'_'.$cid}) || empty($formdata->{'q'.$this->id.'_'.$cid})) {
$other = optional_param('q'.$this->id.'_'.$cid, '', PARAM_CLEAN);
if (empty($other)) {
continue;
}
if (!isset($formdata->{'q'.$this->id})) {
$formdata->{'q'.$this->id} = array($cid);
if (!isset($val)) {
$val = array($cid);
} else {
array_push($formdata->{'q'.$this->id}, $cid);
array_push($val, $cid);
}
$other = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if(ereg("[^ \t\n]",$other)) {
$record = new Object();
$record->response_id = $rid;
Expand All @@ -338,11 +335,11 @@ function insert_resp_multiple($rid, $formdata) {
}
}

if(!isset($formdata->{'q'.$this->id}) || count($formdata->{'q'.$this->id}) < 1) {
if(!isset($val) || count($val) < 1) {
return false;
}

foreach($formdata->{'q'.$this->id} as $cid) {
foreach($val as $cid) {
$cid = clean_param($cid, PARAM_CLEAN);
if ($cid != 0) { //do not save response if choice is empty
if(ereg("other_q[0-9]+", $cid))
Expand All @@ -357,20 +354,20 @@ function insert_resp_multiple($rid, $formdata) {
return $resid;
}

function insert_response_rank($rid, $formdata) {
function insert_response_rank($rid) {
global $DB;

$val = optional_param('q'.$this->id, null, PARAM_CLEAN);
if($this->type_id == 8) { // Rank
$resid = false;
foreach ($this->choices as $cid => $choice) {
if (!isset($formdata->{'q'.$this->id.'_'.$cid})) {
$other = optional_param('q'.$this->id.'_'.$cid, null, PARAM_CLEAN);
if (!isset($other)) {
continue;
}
$val = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if($val == get_string('notapplicable', 'questionnaire')) {
if($other == get_string('notapplicable', 'questionnaire')) {
$rank = -1;
} else {
$rank = intval($val);
$rank = intval($other);
}
$record = new Object();
$record->response_id = $rid;
Expand All @@ -381,11 +378,11 @@ function insert_response_rank($rid, $formdata) {
}
return $resid;
} else { // THIS SHOULD NEVER HAPPEN
$r = clean_param($formdata->{'q'.$this->id}, PARAM_CLEAN);
if($formdata->{'q'.$this->id} == get_string('notapplicable', 'questionnaire')) {
$r = $val;
if($val == get_string('notapplicable', 'questionnaire')) {
$rank = -1;
} else {
$rank = intval($formdata->{'q'.$this->id});
$rank = intval($val);
}
$record = new Object();
$record->response_id = $rid;
Expand Down

0 comments on commit 52d0adf

Please sign in to comment.