Skip to content

Commit

Permalink
[GSOC-FUQT] AJAX uploader: changed behavior of 'Save and exit' button…
Browse files Browse the repository at this point in the history
… to prevent the participant from getting locked on the uploader screen.

Added facility to download individual file off the browse screen.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev@8975 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
Amit Shanker committed Jul 23, 2010
1 parent 1dca4c8 commit fa110d2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 73 deletions.
79 changes: 63 additions & 16 deletions admin/browse.php
Expand Up @@ -252,6 +252,9 @@
{
$browseoutput .= "<img align='left' hspace='0' border='0' src='$imagefiles/delete_disabled.png' alt='".$clang->gT("You don't have permission to delete this entry.")."'/>";
}
$browseoutput .= "<a href='#' title='".$clang->gTview("Download files for this entry")."' onclick=\" ".get2post($scriptname.'?action=browse&amp;subaction=all&amp;downloadfile='.$id.'&amp;sid='.$surveyid)."\" >"
."<img align='left' hspace='0' border='0' src='$imagefiles/download.png' alt='".$clang->gT("Download files for this entry")."' /></a>\n";

//Export this response
$browseoutput .= "<a href='$scriptname?action=exportresults&amp;sid=$surveyid&amp;id=$id'" .
"title='".$clang->gTview("Export this Response")."' >" .
Expand Down Expand Up @@ -296,9 +299,11 @@
if (isset($phparray[$index]))
{
if ($metadata === "size")
$phparray[$index][$metadata] = ((int)($phparray[$index][$metadata]))." KB";

$browseoutput .= rawurldecode($phparray[$index][$metadata]);
$browseoutput .= rawurldecode(((int)($phparray[$index][$metadata]))." KB");
else if ($metadata === "name")
$browseoutput .= "<a href='#' onclick=\" ".get2post($scriptname.'?action=browse&amp;subaction=all&amp;downloadindividualfile='.$phparray[$index][$metadata].'&amp;fieldname='.$fnames[$i][0].'&amp;id='.$id.'&amp;sid='.$surveyid)."\" >".rawurldecode($phparray[$index][$metadata])."</a>";
else
$browseoutput .= rawurldecode($phparray[$index][$metadata]);
}
else
$browseoutput .= "";
Expand Down Expand Up @@ -362,7 +367,7 @@
$connect->execute($query) or safe_die("Could not delete response<br />$dtquery<br />".$connect->ErrorMsg()); // checked
}
}
// Download all the marked file bundles - checked
// Download all files for all marked responses - checked
else if (isset($_POST['downloadfile']) && $_POST['downloadfile'] === 'marked')
{
$fieldmap = createFieldMap($surveyid, 'full');
Expand Down Expand Up @@ -397,23 +402,24 @@
$metadata = array();
while ($metadata = $filearray->FetchRow())
{
$filecount = 0;
foreach ($metadata as $data)
{
$phparray = json_decode($data, true);
for ($i = 0; $i < count($phparray); $i++)
{
$filelist[$i]['filename'] = $phparray[$i]['filename'];
$filelist[$i]['name'] = rawurldecode($phparray[$i]['name']);
$filelist[$filecount]['filename'] = $phparray[$i]['filename'];
$filelist[$filecount]['name'] = rawurldecode($phparray[$i]['name']);
$filecount++;
}

}
}
}
// Now, zip all the files in the filelist
$tmpdir = getcwd()."/../upload/surveys/" . $surveyid . "/files/";

$zip = new ZipArchive();
$zipfilename = "uploadedfiles.zip";
$zipfilename = "Responses_for_survey_" . $surveyid . ".zip";
if (file_exists($tmpdir."/".$zipfilename))
unlink($tmpdir."/".$zipfilename);

Expand Down Expand Up @@ -444,7 +450,7 @@
}
}
}
// Download individual file bundle via the row download icon - checked
// Download all files for this entry - checked
else if (isset($_POST['downloadfile']) && $_POST['downloadfile'] != '' && $_POST['downloadfile'] !== true)
{
$_POST['downloadfile'] = (int) $_POST['downloadfile'];
Expand Down Expand Up @@ -473,13 +479,15 @@
$filearray = db_execute_assoc($query) or safe_die("Could not download response<br />$query<br />".$connect->ErrorMsg());
while ($metadata = $filearray->FetchRow())
{
$filecount = 0;
foreach ($metadata as $data)
{
$phparray = json_decode($data, true);
for ($i = 0; isset($phparray[$i]); $i++)
{
$filelist[$i]['filename'] = $phparray[$i]['filename'];
$filelist[$i]['name'] = rawurldecode($phparray[$i]['name']);
$filelist[$filecount]['filename'] = $phparray[$i]['filename'];
$filelist[$filecount]['name'] = rawurldecode($phparray[$i]['name']);
$filecount++;
}
}
}
Expand All @@ -488,7 +496,7 @@
$tmpdir = getcwd()."/../upload/surveys/" . $surveyid . "/files/";

$zip = new ZipArchive();
$zipfilename = "uploadedfiles.zip";
$zipfilename = "LS_Responses_for_" . $_POST['downloadfile'] . ".zip";
if (file_exists($tmpdir ."/". $zipfilename))
unlink($tmpdir . "/" . $zipfilename);

Expand Down Expand Up @@ -517,6 +525,43 @@
exit;
}
}
else if (isset($_POST['downloadindividualfile']) && $_POST['downloadindividualfile'] != '')
{
$id = (int)$_POST['id'];
$downloadindividualfile = $_POST['downloadindividualfile'];
$fieldname = $_POST['fieldname'];

$query = "SELECT $fieldname FROM $surveytable WHERE id={$id}";
$result=db_execute_num($query);
$row=$result->FetchRow();
$phparray = json_decode($row[0]);

for ($i = 0; $i < count($phparray); $i++)
{
if ($phparray[$i]->name == $downloadindividualfile)
{
$dir = dirname(getcwd());
$file = $dir."/upload/surveys/" . $surveyid . "/files/" . $phparray[$i]->filename;

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $phparray[$i]->name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
//unlink($file);
exit;
}
break;
}
}
}

$fields=createFieldMap($surveyid,'full');

Expand Down Expand Up @@ -785,7 +830,7 @@
."<td align='center'>
<a href='$scriptname?action=browse&amp;sid=$surveyid&amp;subaction=id&amp;id={$dtrow['id']}'><img src='$imagefiles/token_viewanswer.png' alt='".$clang->gT('View response details')."'/></a>
<a href='$scriptname?action=dataentry&amp;sid=$surveyid&amp;subaction=edit&amp;id={$dtrow['id']}'><img src='$imagefiles/token_edit.png' alt='".$clang->gT('Edit this response')."'/></a>
<a><img id='downloadfile_{$dtrow['id']}' src='$imagefiles/down.png' alt='".$clang->gT('Download these files')."' class='downloadfile'/></a>
<a><img id='downloadfile_{$dtrow['id']}' src='$imagefiles/down.png' alt='".$clang->gT('Download all files in this response as a zip file')."' class='downloadfile'/></a>
<a><img id='deleteresponse_{$dtrow['id']}' src='$imagefiles/token_delete.png' alt='".$clang->gT('Delete this response')."' class='deleteresponse'/></a></td>\n";

$i = 0;
Expand Down Expand Up @@ -829,9 +874,11 @@
if (isset($phparray[$index]))
{
if ($metadata === "size")
$phparray[$index][$metadata] = ((int)($phparray[$index][$metadata]))." KB";

$browseoutput .= "<td align='center'>".rawurldecode($phparray[$index][$metadata])."</td>\n";
$browseoutput .= "<td align='center'>".rawurldecode(((int)($phparray[$index][$metadata]))." KB")."</td>\n";
else if ($metadata === "name")
$browseoutput .= "<td align='center'><a href='#' onclick=\" ".get2post($scriptname.'?action=browse&amp;subaction=all&amp;downloadindividualfile='.$phparray[$index][$metadata].'&amp;fieldname='.$fnames[$i][0].'&amp;id='.$dtrow['id'].'&amp;sid='.$surveyid)."\" >".rawurldecode($phparray[$index][$metadata])."</a></td>\n";
else
$browseoutput .= "<td align='center'>".rawurldecode($phparray[$index][$metadata])."</td>\n";
}
else
$browseoutput .= "<td align='center'>&nbsp;</td>\n";
Expand Down
6 changes: 3 additions & 3 deletions common.php
Expand Up @@ -4251,7 +4251,7 @@ function questionAttributes($returnByName=false)
'sortorder'=>128,
"inputtype"=>"integer",
'default'=>10240,
"help"=>$clang->gT("The surveyee cannot upload a single file larger than this size"),
"help"=>$clang->gT("The participant cannot upload a single file larger than this size"),
"caption"=>$clang->gT("Maximum file size allowed (in KB)"));

$qattributes["max_num_of_files"]=array(
Expand All @@ -4260,7 +4260,7 @@ function questionAttributes($returnByName=false)
'sortorder'=>130,
"inputtype"=>"integer",
'default'=>1,
"help"=>$clang->gT("Maximum number of files that the surveyee can upload for this question"),
"help"=>$clang->gT("Maximum number of files that the participant can upload for this question"),
"caption"=>$clang->gT("Max number of files"));

$qattributes["min_num_of_files"]=array(
Expand All @@ -4269,7 +4269,7 @@ function questionAttributes($returnByName=false)
'sortorder'=>132,
"inputtype"=>"integer",
'default'=>0,
"help"=>$clang->gT("Minimum number of files that the surveyee must upload for this question"),
"help"=>$clang->gT("Minimum number of files that the participant must upload for this question"),
"caption"=>$clang->gT("Min number of files"));

$qattributes["allowed_filetypes"]=array(
Expand Down
Binary file added images/download.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 11 additions & 43 deletions save.php
Expand Up @@ -474,55 +474,23 @@ function createinsertquery()
; // get out of here as this has already been saved into the filesystem
else
{
/* $count = 0;
// ajax, move files from temp to files directory
$tmp = "upload/tmp/";
$target = "upload/surveys/". $thissurvey['sid'] . "/files/";

$query = "SELECT attribute, value FROM ".db_table_name("question_attributes")." WHERE qid = ".$fieldexists['qid'];
$result = db_execute_assoc($query) or safe_die("Failed to fetch question attributes");
while ($row = $result->FetchRow())
$qAttributes[$row['attribute']] = $row['value'];
$validExtensions = explode(",", $qAttributes['allowed_filetypes']);
// for the HTML form version
for ($i = 1; $i <= $qAttributes['max_num_of_files']; $i++)
for ($i = 0; $i < count($phparray); $i++)
{
if (isset($_FILES[$fieldname."_file_".$i]) && $_FILES[$fieldname."_file_".$i]['size'] != 0)
{
$basic = true;
$pathinfo = pathinfo($_FILES[$fieldname."_file_".$i]['name']);
if (!in_array($pathinfo['extension'], $validExtensions))
continue;
$phparray[$count]->name = $_FILES[$fieldname."_file_".$i]['name'];
$phparray[$count]->filename = randomkey(20);
$phparray[$count]->size = $_FILES[$fieldname."_file_".$i]['size'];
$phparray[$count]->ext = $pathinfo['extension'];
$phparray[$i]->filename = randomkey(20);
if (!rename($tmp . rawurldecode($phparray[$i]->name), $target . $phparray[$i]->filename))
echo "Error Moving file to its destination";

if (!@move_uploaded_file($_FILES[$fieldname."_file_".$i]['tmp_name'], $target . $phparray[$count]->filename))
echo "error uploading";
else
$count++;
}
}
*/
// for the AJAX version
if (!isset($basic) || !$basic)
{ // ajax, move files from temp to files directory
$tmp = "upload/tmp/";
$target = "upload/surveys/". $thissurvey['sid'] . "/files/";

for ($i = 0; $i < count($phparray); $i++)
{
$phparray[$i]->filename = randomkey(20);
if (!rename($tmp . rawurldecode($phparray[$i]->name), $target . $phparray[$i]->filename))
echo "Error Moving file to its destination";

$_SESSION[$value] = json_encode($phparray);
}
$_SESSION[$value] = json_encode($phparray);
}
}
$values[] = $connect->qstr($_SESSION[$value], get_magic_quotes_gpc());
// filename is changed from undefined to a random value
// update uses $_POST for saving responses
$_POST[$value] = $_SESSION[$value];
}

else
Expand Down
19 changes: 8 additions & 11 deletions uploader.php
Expand Up @@ -309,19 +309,16 @@ function saveAndExit() {

if (minfiles != 0 && filecount < minfiles)
{
alert("Please upload " + (minfiles-filecount) + " more files");
return false;
var confirmans = confirm("You need to upload " + (minfiles - filecount) + " more files for this question.\n\Are you sure you want to exit ?")
if (confirmans)
return true
else
return false;
}
else
{
var pass = confirm ('Would you like to exit?');
if (pass)
{
passJSON();
return true;
}
else
return false;
passJSON();
return true;
}
}

Expand Down Expand Up @@ -369,7 +366,7 @@ function deletefile(i) {

<!-- The upload button -->
<div align="center" class="upload-div">
<button id="button1" class="upload-button" type="button">Select file to upload</button>
<button id="button1" class="upload-button" type="button">Select file</button>
</div>

<p class="uploadmsg">You can upload <?php echo $_GET['allowed_filetypes']; ?> under <?php echo $_GET['maxfilesize']; ?> KB each</p>
Expand Down

0 comments on commit fa110d2

Please sign in to comment.