Skip to content

Commit

Permalink
Fixed issue #09643: Date/Time question with drop down can break DB sa…
Browse files Browse the repository at this point in the history
…ve with pgsql

Fixed issue : Add an empty default tip for all date question
Dev: Fix "INVALID" only when saving in DB, keep the session
Dev: Use EM to validate in PHP: LEMval JS return empty string for "INVALID" : do it manually after EM
  • Loading branch information
Shnoulle committed Dec 17, 2015
1 parent 9a09b69 commit 9f6703c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 60 deletions.
110 changes: 71 additions & 39 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -1465,30 +1465,9 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
);
}
break;
case 'D': // dropdown box: validate that a complete date is entered
// TODO: generic validation as to dateformat[SGQA].value
if ($hasSubqs) {
$subqs = $qinfo['subqs'];
$sq_equs=array();

foreach($subqs as $sq)
{
$sq_name = ($this->sgqaNaming)?$sq['rowdivid'].".NAOK":$sq['varName'].".NAOK";
$sq_equs[] = '('.$sq_name.'!="INVALID")';
}
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'default',
'class' => 'default',
'eqn' => implode(' and ',$sq_equs),
'qid' => $questionNum,
);
}
break;
case 'D':
// TODO: generic validation as to dateformat[SGQA].value : BUT not same in PHP and JS
break;
default:
break;
}
Expand Down Expand Up @@ -1546,6 +1525,55 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
}
}

// dropdown_dates
// dropdown box: validate that a complete date is entered
if (isset($qattr['dropdown_dates']) && $qattr['dropdown_dates'])
{
$dropdown_dates = $qattr['dropdown_dates'];
if ($hasSubqs) {
$subqs = $qinfo['subqs'];
$sq_names = array();
$subqValidEqns = array();
foreach ($subqs as $sq) {
$sq_name = NULL;
switch ($type)
{
case 'D': //DATE QUESTION TYPE
$sq_name = ($this->sgqaNaming)?$sq['rowdivid'].".NAOK":$sq['varName'].".NAOK";
$sq_name = '('.$sq_name.'!="INVALID")';
$subqValidSelector = '';
break;
default:
break;
}
if (!is_null($sq_name)) {
$sq_names[] = $sq_name;
$subqValidEqns[$subqValidSelector] = array(
'subqValidEqn' => $sq_name,
'subqValidSelector' => $subqValidSelector,
);
}
}
if (count($sq_names) > 0) {
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'dropdown_dates',
'class' => 'dropdown_dates',
'eqn' => implode(' && ', $sq_names),
'qid' => $questionNum,
'subqValidEqns' => $subqValidEqns,
);
}
}
}
else
{
$dropdown_dates='';
}
// date_min
// Maximum date allowed in date question
if (isset($qattr['date_min']) && trim($qattr['date_min']) != '')
Expand Down Expand Up @@ -3092,15 +3120,13 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
case 'R':
$qtips['default']=$this->gT("All your answers must be different and you must rank in order.");
break;
// Helptext is added in qanda_help.php
/* case 'D':
$qtips['default']=$this->gT("Please complete all parts of the date.");
break;
*/
default:
break;
}

if($dropdown_dates)
{
$qtips['dropdown_dates']=$this->gT("Please complete all parts of the date.");
}
if($commented_checkbox)
{
switch ($commented_checkbox)
Expand Down Expand Up @@ -4841,12 +4867,13 @@ static function StartSurvey($surveyid,$surveyMode='group',$aSurveyOptions=NULL,$
switch ($knownVar['type'])
{
case 'D': //DATE
if (trim($value)=="" | $value=='INVALID')
if (trim($value)=="")
{
$value = NULL;
}
else
{
// We don't really validate date here, anyone can send anything : forced too
$dateformatdatat=getDateFormatData($LEM->surveyOptions['surveyls_dateformat']);
$datetimeobj = new Date_Time_Converter($value, $dateformatdatat['phpdate']);
$value=$datetimeobj->convert("Y-m-d H:i");
Expand Down Expand Up @@ -5342,7 +5369,7 @@ private function _UpdateValuesInDatabase($updatedValues, $finished=false)
switch($type)
{
case 'D': //DATE
if (trim($val)=='')
if (trim($val)=='' || $val=="INVALID")
{
$val=NULL; // since some databases can't store blanks in date fields
}
Expand Down Expand Up @@ -6701,7 +6728,6 @@ function _ValidateQuestion($questionSeq,$force=false)
}
}


//////////////////////////////////////////////////////////////////////////
// STORE METADATA NEEDED FOR SUBSEQUENT PROCESSING AND DISPLAY PURPOSES //
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -8496,20 +8522,26 @@ static function ProcessCurrentResponses()
switch($type)
{
case 'D': //DATE
if (isset($_POST['qattribute_answer'.$sq])) // push validation message (see qanda_helper) to $_SESSION
{
$_SESSION[$LEM->sessid]['qattribute_answer'.$sq]=($_POST['qattribute_answer'.$sq]);
}
$value=trim($value);
if ($value!="" && $value!='INVALID')
if ($value!="" && $value!="INVALID")
{
$aAttributes=$LEM->getQuestionAttributesForEM($LEM->sid, $qid,$_SESSION['LEMlang']);
if (!isset($aAttributes[$qid])) {
$aAttributes[$qid]=array();
}
$aDateFormatData=getDateFormatDataForQID($aAttributes[$qid],$LEM->surveyOptions);
// We don't really validate date here : if date is invalid : return 1999-12-01 00:00
$oDateTimeConverter = new Date_Time_Converter(trim($value), $aDateFormatData['phpdate']);
$value=$oDateTimeConverter->convert("Y-m-d H:i"); // TODO : control if inverse function original value
$newValue=$oDateTimeConverter->convert("Y-m-d H:i");
$oDateTimeConverter = new Date_Time_Converter($newValue, "Y-m-d H:i");
if($value==$oDateTimeConverter->convert($aDateFormatData['phpdate'])) // control if inverse function original value
{
$value=$newValue;
}
else
{
$value="";// Or $value="INVALID" ? : dropdown is OK with this not default.
}
}
break;
# case 'N': //NUMERICAL QUESTION TYPE
Expand Down
27 changes: 10 additions & 17 deletions application/helpers/qanda_helper.php
Expand Up @@ -127,13 +127,6 @@ function retrieveAnswers($ia)
break;
case 'D': //DATE
$values = do_date($ia);
// if a drop box style date was answered incompletely (dropbox), print an error/help message
if (($_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['step'] != $_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['maxstep']) ||
($_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['step'] == $_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['prevstep']))
{
if (isset($_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['qattribute_answer'.$ia[1]]))
$question_text['help'] = '<span class="error">'.$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['qattribute_answer'.$ia[1]].'</span>';
}
break;
case 'L': //LIST drop-down/radio-button list
$values = do_list_radio($ia);
Expand Down Expand Up @@ -931,7 +924,6 @@ function do_date($ia)
$aQuestionAttributes=getQuestionAttributeValues($ia[0],$ia[4]);
$sDateLangvarJS=" translt = {
alertInvalidDate: '" . gT('Date entered is invalid!','js') . "',
infoCompleteAll: '" . gT('Please complete all parts of the date!','js') . "'
};";
App()->getClientScript()->registerScript("sDateLangvarJS",$sDateLangvarJS,CClientScript::POS_HEAD);
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig("generalscripts").'date.js');
Expand Down Expand Up @@ -997,12 +989,15 @@ function do_date($ia)
$currentdate = $datetimeobj->days;
$currenthour = $datetimeobj->hours;
$currentminute = $datetimeobj->minutes;
} else {
$currentdate='';
$currentmonth='';
$currentyear='';
$currenthour = '';
$currentminute = '';
}
else
{
// If date is invalid get the POSTED value
$currentdate = App()->request->getPost("day{$ia[1]}",'');
$currentmonth = App()->request->getPost("month{$ia[1]}",'');
$currentyear = App()->request->getPost("year{$ia[1]}",'');
$currenthour = App()->request->getPost("hour{$ia[1]}",'');
$currentminute = App()->request->getPost("minute{$ia[1]}",'');
}

$dateorder = preg_split('/([-\.\/ :])/', $dateformatdetails['phpdate'],-1,PREG_SPLIT_DELIM_CAPTURE );
Expand Down Expand Up @@ -1205,9 +1200,7 @@ function do_date($ia)

$answer .= '<input class="text" type="text" size="10" name="'.$ia[1].'" style="display: none" id="answer'.$ia[1].'" value="'.htmlspecialchars($dateoutput,ENT_QUOTES,'utf-8').'" maxlength="10" alt="'.gT('Answer').'" onchange="'.$checkconditionFunction.'(this.value, this.name, this.type)" title="'.sprintf(gT('Date in the format : %s'),$dateformatdetails['dateformat']).'" />
</p>';
$answer .= '
<input type="hidden" id="qattribute_answer'.$ia[1].'" name="qattribute_answer'.$ia[1].'" value="'.$ia[1].'"/>
<input type="hidden" id="dateformat'.$ia[1].'" value="'.$dateformatdetails['jsdate'].'"/>';
$answer .= '<input type="hidden" id="dateformat'.$ia[1].'" value="'.$dateformatdetails['jsdate'].'"/>';
App()->getClientScript()->registerScript("doDropDownDate{$ia[0]}","doDropDownDate({$ia[0]});",CClientScript::POS_HEAD);
// MayDo:
// add js code to
Expand Down
7 changes: 3 additions & 4 deletions scripts/date.js
Expand Up @@ -32,6 +32,7 @@ function doDropDownDate(qId){
$(document).on("change",'#question'+qId+' select',dateUpdater);
$(document).ready(function(){
$("#question"+qId+" select").filter(':first').trigger("change");
//dateUpdater();
});
}
/* This function is called each time shortly before the picker pops up.
Expand Down Expand Up @@ -181,15 +182,13 @@ function dateUpdater() {
{
thisid=this.id.substr(6);
}

if ((!$('#year'+thisid).length || $('#year'+thisid).val()=='') &&
(!$('#month'+thisid).length || $('#month'+thisid).val()=='') &&
(!$('#day'+thisid).length || $('#day'+thisid).val()=='') &&
(!$('#hour'+thisid).length || $('#hour'+thisid).val()=='') &&
(!$('#minute'+thisid).length || $('#minute'+thisid).val()==''))
{
//nothing filled in
$('#qattribute_answer'+thisid).val('');
$('#answer'+thisid).val('');
$('#answer'+thisid).change();
}
Expand All @@ -200,9 +199,10 @@ function dateUpdater() {
($('#minute'+thisid).length && $('#minute'+thisid).val()==''))
{
//incomplete
$('#qattribute_answer'+thisid).val(translt.infoCompleteAll);
$('#answer'+thisid).val('INVALID');
$('#answer'+thisid).change();
// QCODE.NAOK return "" if real value is INVALID (because can be shown to user), then do it manually (line 721 em_javascript)
$("#vmsg_22_dropdown_dates").removeClass('good').addClass('error');
}
else
{
Expand Down Expand Up @@ -251,7 +251,6 @@ function dateUpdater() {
parseddate=parseddate.format($('#dateformat'+thisid).val());
$('#answer'+thisid).val(parseddate);
$('#answer'+thisid).change();
$('#qattribute_answer'+thisid).val('');
}
}

Expand Down

0 comments on commit 9f6703c

Please sign in to comment.