Skip to content

Commit

Permalink
FUQT-Work in progress: added support to save uploaded file metadata a…
Browse files Browse the repository at this point in the history
…s a JSON string into the database

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev@8746 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
Amit Shanker committed May 23, 2010
1 parent 87fcb67 commit 2632cc3
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 98 deletions.
48 changes: 47 additions & 1 deletion common.php
Expand Up @@ -2421,7 +2421,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi

// Types "L", "!" , "O", "D", "G", "N", "X", "Y", "5","S","T","U"

if ($qtypes[$arow['type']]['subquestions']==0 && $arow['type'] != "R")
if ($qtypes[$arow['type']]['subquestions']==0 && $arow['type'] != "R" && $arow['type'] != "|")
{
$fieldname="{$arow['sid']}X{$arow['gid']}X{$arow['qid']}";
$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>"{$arow['type']}", 'sid'=>$surveyid, "gid"=>$arow['gid'], "qid"=>$arow['qid'], "aid"=>"");
Expand Down Expand Up @@ -2638,6 +2638,52 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
}
}
}
elseif ($arow['type'] == "|")
{
$abquery = "SELECT value FROM ".db_table_name('question_attributes')
." WHERE attribute='max_num_of_files' AND qid=".$arow['qid'];
$abresult = db_execute_assoc($abquery) or safe_die ("Couldn't get maximum
number of files that can be uploaded <br />$abquery<br />".$connect->ErrorMsg());
$abrow = $abresult->FetchRow();

for ($i = 1; $i <= $abrow['value']; $i++)
{
$fieldname="{$arow['sid']}X{$arow['gid']}X{$arow['qid']}"."_title_".$i;
$fieldmap[$fieldname]=array("fieldname"=>$fieldname,
'type'=>$arow['type'],
'sid'=>$surveyid,
"gid"=>$arow['gid'],
"qid"=>$arow['qid']
);
if ($style == "full")
{
$fieldmap[$fieldname]['title']=$arow['title'];
$fieldmap[$fieldname]['question']=$arow['question'];
$fieldmap[$fieldname]['subquestion']=$clang->gT("Comment");
$fieldmap[$fieldname]['group_name']=$arow['group_name'];
$fieldmap[$fieldname]['mandatory']=$arow['mandatory'];
$fieldmap[$fieldname]['hasconditions']=$conditions;
$fieldmap[$fieldname]['usedinconditions']=$usedinconditions;
}
$fieldname="{$arow['sid']}X{$arow['gid']}X{$arow['qid']}"."_comment_".$i;
$fieldmap[$fieldname]=array("fieldname"=>$fieldname,
'type'=>$arow['type'],
'sid'=>$surveyid,
"gid"=>$arow['gid'],
"qid"=>$arow['qid']
);
if ($style == "full")
{
$fieldmap[$fieldname]['title']=$arow['title'];
$fieldmap[$fieldname]['question']=$arow['question'];
$fieldmap[$fieldname]['subquestion']=$clang->gT("Comment");
$fieldmap[$fieldname]['group_name']=$arow['group_name'];
$fieldmap[$fieldname]['mandatory']=$arow['mandatory'];
$fieldmap[$fieldname]['hasconditions']=$conditions;
$fieldmap[$fieldname]['usedinconditions']=$usedinconditions;
}
}
}
else // Question types with subquestions and one answer per subquestion (M/A/B/C/E/F/H/P)
{
//MULTI ENTRY
Expand Down
177 changes: 97 additions & 80 deletions qanda.php
Expand Up @@ -651,6 +651,9 @@ function retrieveAnswers($ia, $notanswered=null, $notvalidated=null)
}
}
break;
case '|': //File Upload
$values=do_file_upload($ia);
break;
case 'Q': //MULTIPLE SHORT TEXT
$values=do_multipleshorttext($ia);
break;
Expand Down Expand Up @@ -702,9 +705,6 @@ function retrieveAnswers($ia, $notanswered=null, $notvalidated=null)
case '1': //Array (Flexible Labels) dual scale
$values=do_array_flexible_dual($ia);
break;
case '|': //File Upload
$values=do_file_upload($ia);
break;

} //End Switch

Expand Down Expand Up @@ -3385,6 +3385,100 @@ function do_multiplechoice_withcomments($ia)



// ---------------------------------------------------------------
function do_file_upload($ia)
{
global $clang, $js_header_includes;

if ($ia[8] == 'Y')
$checkconditionFunction = "checkconditions";
else
$checkconditionFunction = "noop_checkconditions";

$qidattributes=getQuestionAttributes($ia[0]);

if (trim($qidattributes['max_num_of_files'])!='')
$maxfiles=$qidattributes['max_num_of_files'];
else
{
//TODO: use the global settings for maximum no. of files
}

if (trim($qidattributes['min_num_of_files'])!='')
$minfiles=$qidattributes['min_num_of_files'];
else
{
//TODO: use the global settings for minimum no. of files
}

if (trim($qidattributes['max_filesize'])!='')
$maxfilesize=$qidattributes['max_filesize'];
else
{
//TODO: use the global settings for maximum size of file
}

if (trim($qidattributes['allowed_filetypes'])!='')
$allowed_filetypes=$qidattributes['allowed_filetypes'];
else
{
//TODO: use the global settings for allowed file types
}

//TODO: check the max no. of files that can be uploaded,
// accordingly display those many upload buttons

//TODO: use a javascript to ensure that the size of file
// is not more than $max_filesize

// --> START NEW FEATURE - SAVE
$answer = '<table border="0" cellpadding="10" cellspacing="10" align="center">
<tr>
<th align="center"><b>Title</b></th>
<th>&nbsp;&nbsp;</th>
<th align="center"><b>Comment</b></th>
<th>&nbsp;&nbsp;</th>
<th align="center"><b>Select file</b></th>
</tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tbody>';

for ($i = 1; $i <= $maxfiles; $i++) {
$answer .= '<tr>
<td>
<input class="text" type="text" size="30" name="'.$ia[1].'_title_'.$i
.'" id="answer'.$ia[1].'_title_'.$i.'" value="'.$_SESSION[$ia[1]]
.'" maxlength="100" />
</td>
<td>&nbsp;&nbsp;</td>
<td>
<input type="textarea" name="'.$ia[1].'_comment_'.$i
.'" id="answer'.$ia[1].'_comment_'.$i.'" value="'.$_SESSION[$ia[1]]
.'" maxlength="100" />
</td>
<td>&nbsp;&nbsp;</td>
<td>'
.' <input class="file" '
.'type="file" name="the_file_'.$i.'" id="answer'.$ia[1].'_'.$i.'" size="25" alt="'
.$clang->gT("Answer").'" ></input></td>
</tr>'
.'<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
}
// --> END NEW FEATURE - SAVE

$answer .= '</tbody></table>';

$inputnames[] = $ia[1];
for ($i = 1; $i <= $maxfiles; $i++)
{
$inputnames[]=$ia[1].'_title_'.$i;
$inputnames[]=$ia[1].'_comment_'.$i;
}
return array($answer, $inputnames);
}




// ---------------------------------------------------------------
function do_multipleshorttext($ia)
Expand Down Expand Up @@ -6695,83 +6789,6 @@ function do_array_flexible_dual($ia)



// ---------------------------------------------------------------
function do_file_upload($ia)
{
global $clang, $js_header_includes;

if ($ia[8] == 'Y')
{
$checkconditionFunction = "checkconditions";
}
else
{
$checkconditionFunction = "noop_checkconditions";
}

$qidattributes=getQuestionAttributes($ia[0]);

if (trim($qidattributes['max_num_of_files'])!='')
{
$maxfiles=$qidattributes['max_num_of_files'];
}
else
{
//TODO: use the global settings for maximum no. of files
}

if (trim($qidattributes['min_num_of_files'])!='')
{
$minfiles=$qidattributes['min_num_of_files'];
}
else
{
//TODO: use the global settings for minimum no. of files
}

if (trim($qidattributes['max_filesize'])!='')
{
$maxfilesize=$qidattributes['max_filesize'];
}
else
{
//TODO: use the global settings for maximum size of file
}

if (trim($qidattributes['allowed_filetypes'])!='')
{
$allowed_filetypes=$qidattributes['allowed_filetypes'];
}
else
{
//TODO: use the global settings for allowed file types
}


//TODO: check the max no. of files that can be uploaded,
// accordingly display those many upload buttons

//TODO: use a javascript to ensure that the size of file
// is not more than $max_filesize

//TODO: have a javascript that actually copies all the metadata into a
//JSON string and then saves it into the $_SESSION['qid'] field

// --> START NEW FEATURE - SAVE
for ($i = 1, $answer = ""; $i <= $maxfiles; $i++) {
$answer .= $clang->gT("Select file for uploading").' <input class="file" '
.'type="file" name="the_file_'.$i.'" id="answer'.$ia[1].'_'.$i.'" size="25" alt="'
.$clang->gT("Answer").'" ></input> <br><br>'."\n";
}
// --> END NEW FEATURE - SAVE

$inputnames[]=$ia[1];
return array($answer, $inputnames);
}




// ---------------------------------------------------------------
function answer_replace($text)
{
Expand Down
51 changes: 34 additions & 17 deletions save.php
Expand Up @@ -387,7 +387,7 @@ function createinsertquery()
if (isset($_SESSION['insertarray']) && is_array($_SESSION['insertarray']))
{
$inserts=array_unique($_SESSION['insertarray']);

$colnames_hidden=Array();
foreach ($inserts as $value)
{
Expand All @@ -405,7 +405,14 @@ function createinsertquery()
}
}
//Only create column name and data entry if there is actually data!
$colnames[]=$value;
if ($fieldexists['type'] !='|')
$colnames[]=$value;
else if (!isset($colnames))
$colnames[] = substr($value, 0, strpos($value, "_"));
else if (isset($colnames) && (!in_array(substr($value, 0, strpos($value, "_")), $colnames)))
$colnames[] = substr($value, 0, strpos($value, "_"));


// most databases do not allow to insert an empty value into a datefield,
// therefore if no date was chosen in a date question the insert value has to be NULL
if ($deletenonvalues==1 && !checkconfield($value))
Expand All @@ -416,28 +423,38 @@ function createinsertquery()
{
$values[]='NULL';
}
else if ($_SESSION[$value]=='' && $fieldexists['type']=='|')
else if ($fieldexists['type']=='|')
{
//TODO: file upload
// save the files in an uploads directory
// save the JSON string into the database -> this is done by default
//TODO: remove the hardcoded path
$i = substr(strrchr($value, "_"), 1);

$tempdir = "/opt/lampp/htdocs/gsocls/upload/files";

for ($i = 1, $file_upload_error = FALSE; $i <= count($_FILES); $i++)
if ($_FILES['the_file_'.$i]['tmp_name'] != '')
{
if ($_FILES['the_file_'.$i]['name'] != '')
if (!isset($values)) // if this is the first file to be uploaded
$values[0] = '\'{[';
else if ((strrpos($values[0], ",") == strlen($values[0]) - 1) || (strpos($values[0], "]") == strlen($values[0]) - 1)) // last char is a comma or sq brkt
; // do nothing
else // remove the braces '}' and add a ',['
$values[0] = substr($values[0], 0, strlen($values[0]) - 1).',[';

if (strpos($value, "title") != false)
$values[0] .= '"title": "'.$_SESSION[$value].'",';
else if (strpos($value, "comment") != false)
{
$the_full_file_path = $tempdir . "/" . $_FILES['the_file_'.$i]['name'];
$values[0] .= '"comment": "'.$_SESSION[$value].'",';

$random_file_name = randomkey(20);

//TODO: remove the hardcoded path
$uploads_dir = "/opt/lampp/htdocs/gsocls/upload/files";

$the_full_file_path = $uploads_dir."/".$random_file_name;//.strrchr($_FILES['the_file_'.$i]['tmp_name'], ".");

if (!@move_uploaded_file($_FILES['the_file_'.$i]['tmp_name'], $the_full_file_path))
$file_upload_error = TRUE;
echo "error uploading";
else
$values[0] .= '"filename": "'.$random_file_name.'"]}\'';
}
}
if ($file_upload_error)
echo "error uploading";
else
echo "successs uploading file";
}
else
{
Expand Down

0 comments on commit 2632cc3

Please sign in to comment.