Skip to content

Commit

Permalink
Fixed issue #06154: File upload questions not mandatory anymore
Browse files Browse the repository at this point in the history
Dev EM now manages validation of uploaded file counts, including dynamic display of validation messages
  • Loading branch information
TMSWhite committed Jun 2, 2012
1 parent a28d082 commit cbff12b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 29 deletions.
6 changes: 4 additions & 2 deletions application/controllers/UploaderController.php
Expand Up @@ -276,13 +276,15 @@ function run()

$fn = $param['fieldname'];
$qid = $param['qid'];
$minfiles = sanitize_int($param['minfiles']);
$maxfiles = sanitize_int($param['maxfiles']);
$qidattributes=getQuestionAttributeValues($qid);

$body = '
<div id="notice"></div>
<input type="hidden" id="ia" value="'.$fn.'" />
<input type="hidden" id="'.$fn.'_minfiles" value="'.$qidattributes['min_num_of_files'].'" />
<input type="hidden" id="'.$fn.'_maxfiles" value="'.$qidattributes['max_num_of_files'].'" />
<input type="hidden" id="'.$fn.'_minfiles" value="'.$minfiles.'" />
<input type="hidden" id="'.$fn.'_maxfiles" value="'.$maxfiles.'" />
<input type="hidden" id="'.$fn.'_maxfilesize" value="'.$qidattributes['max_filesize'].'" />
<input type="hidden" id="'.$fn.'_allowed_filetypes" value="'.$qidattributes['allowed_filetypes'].'" />
<input type="hidden" id="preview" value="'.Yii::app()->session['preview'].'" />
Expand Down
7 changes: 2 additions & 5 deletions application/helpers/common_helper.php
Expand Up @@ -2573,8 +2573,6 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
elseif ($arow['type'] == "|")
{
$qidattributes= getQuestionAttributeValues($arow['qid']);
for ($i = 1; $i <= $qidattributes['max_num_of_files']; $i++)
{
$fieldname="{$arow['sid']}X{$arow['gid']}X{$arow['qid']}";
$fieldmap[$fieldname]=array("fieldname"=>$fieldname,
'type'=>$arow['type'],
Expand Down Expand Up @@ -2614,7 +2612,6 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
$fieldmap[$fieldname]['questionSeq']=$questionSeq;
$fieldmap[$fieldname]['groupSeq']=$groupSeq;
}
}
}
else // Question types with subquestions and one answer per subquestion (M/A/B/C/E/F/H/P)
{
Expand Down Expand Up @@ -4071,7 +4068,7 @@ function questionAttributes($returnByName=false)
"types"=>"|",
'category'=>$clang->gT('Other'),
'sortorder'=>130,
"inputtype"=>"integer",
"inputtype"=>"text",
'default'=>1,
"help"=>$clang->gT("Maximum number of files that the participant can upload for this question"),
"caption"=>$clang->gT("Max number of files"));
Expand All @@ -4080,7 +4077,7 @@ function questionAttributes($returnByName=false)
"types"=>"|",
'category'=>$clang->gT('Other'),
'sortorder'=>132,
"inputtype"=>"integer",
"inputtype"=>"text",
'default'=>0,
"help"=>$clang->gT("Minimum number of files that the participant must upload for this question"),
"caption"=>$clang->gT("Min number of files"));
Expand Down
105 changes: 104 additions & 1 deletion application/helpers/expressions/em_manager_helper.php
Expand Up @@ -1855,6 +1855,76 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
$multiflexible_max='';
}

// min_num_of_files
// Validation:= sq_filecount >= value (which could be an expression).
if (isset($qattr['min_num_of_files']) && trim($qattr['min_num_of_files']) != '')
{
$min_num_of_files = $qattr['min_num_of_files'];
$eqn='';
$sgqa = $qinfo['sgqa'];
switch ($type)
{
case '|': //List - dropdown
$eqn = "(" . $sgqa . "_filecount >= (" . $min_num_of_files . "))";
break;
default:
break;
}
if ($eqn != '')
{
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'min_num_of_files',
'class' => 'num_answers',
'eqn' => $eqn,
'qid' => $questionNum,
);
}
}
else
{
$min_num_of_files = '';
}

// max_num_of_files
// Validation:= sq_filecount <= value (which could be an expression).
if (isset($qattr['max_num_of_files']) && trim($qattr['max_num_of_files']) != '')
{
$max_num_of_files = $qattr['max_num_of_files'];
$eqn='';
$sgqa = $qinfo['sgqa'];
switch ($type)
{
case '|': //List - dropdown
$eqn = "(" . $sgqa . "_filecount <= (" . $max_num_of_files . "))";
break;
default:
break;
}
if ($eqn != '')
{
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'max_num_of_files',
'class' => 'num_answers',
'eqn' => $eqn,
'qid' => $questionNum,
);
}
}
else
{
$max_num_of_files = '';
}

// other_comment_mandatory
// Validation:= sqN <= value (which could be an expression).
if (isset($qattr['other_comment_mandatory']) && trim($qattr['other_comment_mandatory']) == '1')
Expand Down Expand Up @@ -2261,6 +2331,37 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
")}";
}

// min/max num files
if ($min_num_of_files !='' || $max_num_of_files !='')
{
$_minA = (($min_num_of_files == '') ? "''" : $min_num_of_files);
$_maxA = (($max_num_of_files == '') ? "''" : $max_num_of_files );
// TODO - create em_num_files class so can sepately style num_files vs. num_answers
$qtips['num_answers']=
"{if((is_empty($_minA) && is_empty($_maxA)),".
"'',".
"if(is_empty($_maxA),".
"if(($_minA)==1,".
"'".$this->gT("Please upload at least one file")."',".
"sprintf('".$this->gT("Please upload at least %s files")."',fixnum($_minA))".
"),".
"if(is_empty($_minA),".
"if(($_maxA)==1,".
"'".$this->gT("Please upload at most one file")."',".
"sprintf('".$this->gT("Please upload at most %s files")."',fixnum($_maxA))".
"),".
"if(($_minA)==($_maxA),".
"if(($_minA)==1,".
"'".$this->gT("Please upload one file")."',".
"sprintf('".$this->gT("Please upload %s files")."',fixnum($_minA))".
"),".
"sprintf('".$this->gT("Please upload between %s and %s files")."',fixnum($_minA),fixnum($_maxA))".
")".
")".
")".
")}";
}

// equals_num_value
if ($equals_num_value!='')
{
Expand Down Expand Up @@ -2903,7 +3004,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
break;
}
if (!is_null($rowdivid) || $type == 'L' || $type == 'N' || $type == '!' || !is_null($preg)
|| $type == 'S' || $type == 'T' || $type == 'U') {
|| $type == 'S' || $type == 'T' || $type == 'U' || $type == '|') {
if (!isset($q2subqInfo[$questionNum])) {
$q2subqInfo[$questionNum] = array(
'qid' => $questionNum,
Expand Down Expand Up @@ -7537,6 +7638,8 @@ static public function ShowSurveyLogicFile($sid, $gid=NULL, $qid=NULL,$LEMdebugL
case 'min_answers':
case 'min_num_value':
case 'min_num_value_n':
case 'min_num_of_files':
case 'max_num_of_files':
case 'multiflexible_max':
case 'multiflexible_min':
$value = '{' . $value . '}';
Expand Down
42 changes: 21 additions & 21 deletions application/helpers/qanda_helper.php
Expand Up @@ -243,15 +243,6 @@ function retrieveAnswers($ia)
break;
case '|': //File Upload
$values=do_file_upload($ia);
if ($aQuestionAttributes['min_num_of_files'] != 0)
{
if (trim($aQuestionAttributes['min_num_of_files']) != 0)
{
$qtitle .= "<br />\n<span class = \"questionhelp\">"
.sprintf($clang->gT("At least %d files must be uploaded for this question"), $aQuestionAttributes['min_num_of_files'])."<span>";
$question_text['help'] .= ' '.sprintf($clang->gT("At least %d files must be uploaded for this question"), $aQuestionAttributes['min_num_of_files']);
}
}
break;
case 'Q': //MULTIPLE SHORT TEXT
$values=do_multipleshorttext($ia);
Expand Down Expand Up @@ -2975,17 +2966,26 @@ function do_file_upload($ia)
$questgrppreview = 0;
}

$uploadbutton = "<h2><a id='upload_".$ia[1]."' class='upload' href='{$scriptloc}?sid=".Yii::app()->getConfig('surveyID')."&amp;fieldname={$ia[1]}&amp;qid={$ia[0]}&amp;preview="
."{$questgrppreview}&amp;show_title={$aQuestionAttributes['show_title']}&amp;show_comment={$aQuestionAttributes['show_comment']}&amp;pos=".($pos?1:0)."'>" .$clang->gT('Upload files'). "</a></h2>";

$answer = "<script type='text/javascript'>
var translt = {
title: '" . $clang->gT('Upload your files','js') . "',
returnTxt: '" . $clang->gT('Return to survey','js') . "',
headTitle: '" . $clang->gT('Title','js') . "',
headComment: '" . $clang->gT('Comment','js') . "',
headFileName: '" . $clang->gT('File name','js') . "'
};
$uploadbutton = "<h2><a id='upload_".$ia[1]."' class='upload' ";
$uploadbutton .= " href='#' onclick='javascript:upload_$ia[1]();'";
$uploadbutton .=">" .$clang->gT('Upload files'). "</a></h2>";

$answer = "<script type='text/javascript'>
function upload_$ia[1]() {
var uploadurl = '{$scriptloc}?sid=".Yii::app()->getConfig('surveyID')."&amp;fieldname={$ia[1]}&amp;qid={$ia[0]}';
uploadurl += '&amp;preview={$questgrppreview}&amp;show_title={$aQuestionAttributes['show_title']}';
uploadurl += '&amp;show_comment={$aQuestionAttributes['show_comment']}&amp;pos=".($pos?1:0)."';
uploadurl += '&amp;minfiles=' + LEMval('{$aQuestionAttributes['min_num_of_files']}');
uploadurl += '&amp;maxfiles=' + LEMval('{$aQuestionAttributes['max_num_of_files']}');
$('#upload_$ia[1]').attr('href',uploadurl);
}
var translt = {
title: '" . $clang->gT('Upload your files','js') . "',
returnTxt: '" . $clang->gT('Return to survey','js') . "',
headTitle: '" . $clang->gT('Title','js') . "',
headComment: '" . $clang->gT('Comment','js') . "',
headFileName: '" . $clang->gT('File name','js') . "',
};
</script>\n";

header_includes(Yii::app()->getConfig('generalscripts')."modaldialog.js");
Expand Down Expand Up @@ -3032,7 +3032,7 @@ function do_file_upload($ia)
var i;
var jsonstring = "[";
for (i = 1, filecount = 0; i <= '.$aQuestionAttributes['max_num_of_files'].'; i++)
for (i = 1, filecount = 0; i <= LEMval("'.$aQuestionAttributes['max_num_of_files'].'"); i++)
{
if ($("#'.$ia[1].'_"+i).val() == "")
continue;
Expand Down
1 change: 1 addition & 0 deletions scripts/modaldialog.js
Expand Up @@ -35,6 +35,7 @@ $(document).ready(function() {
$(this).dialog('close');
$('iframe#uploader').remove();
$(this).dialog('destroy');
checkconditions();
}
};

Expand Down

0 comments on commit cbff12b

Please sign in to comment.