Add dropbox drag n drop see BT#10896

1.10.x
jmontoya 10 years ago
parent a9322ed1b1
commit 2fb41be35e
  1. 1
      main/document/upload.php
  2. 121
      main/dropbox/dropbox_functions.inc.php
  3. 192
      main/dropbox/dropbox_init.inc.php
  4. 5
      main/dropbox/index.php
  5. 25
      main/inc/ajax/document.ajax.php
  6. 74
      main/inc/ajax/dropbox.ajax.php
  7. 26
      main/inc/ajax/work.ajax.php
  8. 3
      main/work/upload.php

@ -204,7 +204,6 @@ $(function () {
</script>";
// This needs cleaning!
if (!empty($groupId)) {
// If the group id is set, check if the user has the right to be here

@ -222,9 +222,9 @@ function store_move($id, $target, $part)
$dropbox_cnf = getDropboxConf();
$course_id = api_get_course_int_id();
if ((isset($id) AND $id != '') AND
(isset($target) AND $target != '') AND
(isset($part) AND $part != '')
if ((isset($id) && $id != '') &&
(isset($target) && $target != '') &&
(isset($part) && $part != '')
) {
if ($part == 'received') {
@ -469,7 +469,7 @@ function display_addcategory_form($category_name = '', $id = '', $action)
$course_id = api_get_course_int_id();
$title = get_lang('AddNewCategory');
if (isset($id) AND $id != '') {
if (isset($id) && $id != '') {
// retrieve the category we are editing
$sql = "SELECT * FROM ".$dropbox_cnf['tbl_category']."
WHERE c_id = $course_id AND cat_id = ".intval($id)."";
@ -693,7 +693,53 @@ function display_add_form($dropbox_unid, $viewReceivedCategory, $viewSentCategor
)
);
$form->addButtonUpload(get_lang('Upload'), 'submitWork');
$form->display();
$headers = array(
get_lang('Upload'),
get_lang('Upload').' ('.get_lang('Simple').')',
);
$multipleForm = new FormValidator(
'sent_multiple',
'post',
'#',
null,
array('enctype' => 'multipart/form-data', 'id' => 'fileupload')
);
$multipleForm->addSelect(
'recipients',
get_lang('SendTo'),
$options,
array(
'multiple' => 'multiple',
'size' => '10',
'id' => 'recipient_form'
)
);
$multipleForm->addHtml('
<div id="multiple_form" style="display:none">
<div class="description-upload">'.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'</div>
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>'.get_lang('AddFiles').'</span>
<!-- The file input field used as target for the file upload widget -->
<input id="input_fileupload" type="file" name="files[]" multiple>
</span>
<br />
<br />
<!-- The global progress bar -->
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
<div id="files" class="files"></div>
</div>
');
echo Display::tabs($headers, array($multipleForm->returnForm(), $form->returnForm()), 'tabs');
}
/**
@ -863,14 +909,20 @@ function dropbox_cnf($variable)
}
/**
* @param array $file
*
* @return array|null|string
*/
function store_add_dropbox()
function store_add_dropbox($file = [])
{
$_course = api_get_course_info();
$_user = api_get_user_info();
$dropbox_cnf = getDropboxConf();
if (empty($file)) {
$file = isset($_FILES['file']) ? $_FILES['file'] : null;
}
// Validating the form data
// there are no recipients selected
@ -886,28 +938,34 @@ function store_add_dropbox()
} elseif ($rec == 'upload') {
$thisIsJustUpload = true;
} elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) {
return get_lang('InvalideUserDetected');
Display::addFlash(Display::return_message(get_lang('InvalideUserDetected'), 'warning'));
return false;
} elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) {
return get_lang('InvalideGroupDetected');
Display::addFlash(Display::return_message(get_lang('InvalideGroupDetected'), 'warning'));
return false;
}
}
}
// we are doing a mailing but an additional recipient is selected
if ($thisIsAMailing && (count($_POST['recipients']) != 1)) {
return get_lang('MailingSelectNoOther');
Display::addFlash(Display::return_message(get_lang('MailingSelectNoOther'), 'warning'));
return false;
}
// we are doing a just upload but an additional recipient is selected.
// note: why can't this be valid? It is like sending a document to
// yourself AND to a different person (I do this quite often with my e-mails)
if ($thisIsJustUpload && (count($_POST['recipients']) != 1)) {
return get_lang('MailingJustUploadSelectNoOther');
Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning'));
return false;
}
if (empty($_FILES['file']['name'])) {
$error = true;
return get_lang('NoFileSpecified');
if (empty($file['name'])) {
Display::addFlash(Display::return_message(get_lang('NoFileSpecified'), 'warning'));
return false;
}
// are we overwriting a previous file or sending a new one
@ -919,23 +977,26 @@ function store_add_dropbox()
// doing the upload
$dropbox_filename = $_FILES['file']['name'];
$dropbox_filesize = $_FILES['file']['size'];
$dropbox_filetype = $_FILES['file']['type'];
$dropbox_filetmpname = $_FILES['file']['tmp_name'];
$dropbox_filename = $file['name'];
$dropbox_filesize = $file['size'];
$dropbox_filetype = $file['type'];
$dropbox_filetmpname = $file['tmp_name'];
// check if the filesize does not exceed the allowed size.
if ($dropbox_filesize <= 0 || $dropbox_filesize > $dropbox_cnf['maxFilesize']) {
return get_lang('DropboxFileTooBig');
// TODO: The "too big" message does not fit in the case of uploading zero-sized file.
Display::addFlash(Display::return_message(get_lang('DropboxFileTooBig'), 'warning'));
return false;
}
// check if the file is actually uploaded
if (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
return get_lang('TheFileIsNotUploaded');
Display::addFlash(Display::return_message(get_lang('TheFileIsNotUploaded'), 'warning'));
return false;
}
$upload_ok = process_uploaded_file($_FILES['file'], true);
$upload_ok = process_uploaded_file($file, true);
if (!$upload_ok) {
return null;
@ -950,7 +1011,8 @@ function store_add_dropbox()
//filter extension
if (!filter_extension($dropbox_filename)) {
return get_lang('UplUnableToSaveFileFilteredExtension');
Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'warning'));
return false;
}
// set title
@ -973,10 +1035,13 @@ function store_add_dropbox()
foreach ($dropbox_person->sentWork as $w) {
if ($w->title == $dropbox_filename) {
if (($w->recipients[0]['id'] > dropbox_cnf('mailingIdBase')) xor $thisIsAMailing) {
return get_lang('MailingNonMailingError');
Display::addFlash(Display::return_message(get_lang('MailingNonMailingError'), 'warning'));
return false;
}
if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
return get_lang('MailingJustUploadSelectNoOther');
Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning'));
return false;
}
$dropbox_filename = $w->filename;
$found = true; // note: do we still need this?
@ -1046,7 +1111,7 @@ function store_add_dropbox()
}
}
new Dropbox_SentWork(
$result = new Dropbox_SentWork(
$_user['user_id'],
$dropbox_title,
isset($_POST['description']) ? $_POST['description'] : '',
@ -1057,11 +1122,13 @@ function store_add_dropbox()
);
Security::clear_token();
return get_lang('FileUploadSucces');
Display::addFlash(Display::return_message(get_lang('FileUploadSucces')));
return $result;
}
/**
* this function transforms the array containing all the feedback into something visually attractive.
* Transforms the array containing all the feedback into something visually attractive.
*
* @param an array containing all the feedback about the given message.
*

@ -113,15 +113,10 @@ Version 1.4 (Yannick Warnier)
* @package chamilo.dropbox
*/
/**
* Code
*/
/* INIT SECTION */
use ChamiloSession as Session;
// including the basic Chamilo initialisation file
require_once '../inc/global.inc.php';
require_once __DIR__.'/../inc/global.inc.php';
$is_allowed_in_course = api_is_allowed_in_course();
$is_courseTutor = api_is_course_tutor();
$is_courseAdmin = api_is_course_admin();
@ -135,7 +130,6 @@ Session::write('dropbox_conf', $dropbox_cnf);
// the dropbox file that contains additional functions
require_once 'dropbox_functions.inc.php';
// protecting the script
api_protect_course_script();
@ -182,39 +176,39 @@ if ($action == 'add') {
}
/* Create javascript and htmlHeaders */
$javascript = "<script type=\"text/javascript\">
function confirmsend ()
{
if (confirm(\"".get_lang('MailingConfirmSend', '')."\")){
return true;
} else {
return false;
}
return true;
}
$javascript = "<script>
function confirmsend()
{
if (confirm(\"".get_lang('MailingConfirmSend', '')."\")){
return true;
} else {
return false;
}
return true;
}
function confirmation (name)
{
if (confirm(\"".get_lang('ConfirmDelete', '')." : \"+ name )){
return true;
} else {
return false;
}
return true;
}
function confirmation (name)
{
if (confirm(\"".get_lang('ConfirmDelete', '')." : \"+ name )){
return true;
} else {
return false;
}
return true;
}
function checkForm (frm)
{
if (frm.elements['recipients[]'].selectedIndex < 0){
alert(\"".get_lang('NoUserSelected', '')."\");
return false;
} else if (frm.file.value == '') {
alert(\"".get_lang('NoFileSpecified', '')."\");
return false;
} else {
return true;
}
}
function checkForm (frm)
{
if (frm.elements['recipients[]'].selectedIndex < 0){
alert(\"".get_lang('NoUserSelected', '')."\");
return false;
} else if (frm.file.value == '') {
alert(\"".get_lang('NoFileSpecified', '')."\");
return false;
} else {
return true;
}
}
";
if (dropbox_cnf('allowOverwrite')) {
@ -294,11 +288,133 @@ $htmlHeadXtra[] = '<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="-1">';
$checked_files = false;
if (!$view || $view == 'received') {
$part = 'received';
} elseif ($view = 'sent') {
$part = 'sent';
$htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
$url = api_get_path(WEB_AJAX_PATH).'dropbox.ajax.php?'.api_get_cidreq().'&a=upload_file&id=';
$htmlHeadXtra[] = "<script>
$(function () {
'use strict';
var url = '".$url."';
var uploadButton = $('<button/>')
.addClass('btn btn-primary')
.prop('disabled', true)
.text('".get_lang('Loading')."')
.on('click', function () {
var \$this = $(this),
data = \$this.data();
\$this
.off('click')
.text('".get_lang('Cancel')."')
.on('click', function () {
\$this.remove();
data.abort();
});
data.submit().always(function () {
\$this.remove();
});
});
$('#recipient_form').on('change', function() {
$('#multiple_form').show();
});
$('#fileupload').bind('fileuploaddone', function (e, data) {
console.log('fileuploaddone');
event.preventDefault();
return false;
});
$('#fileupload').submit(function() {
return false;
});
$('#fileupload').fileupload({
url: url,
dataType: 'json',
autoUpload: false,
// Enable image resizing, except for Android and Opera,
// which actually support image resizing, but fail to
// send Blob objects via XHR requests:
disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
previewMaxWidth: 100,
previewMaxHeight: 100,
previewCrop: true,
complete: function (e, data) {
}
}).on('fileuploadadd', function (e, data) {
data.context = $('<div/>').appendTo('#files');
$.each(data.files, function (index, file) {
var node = $('<p/>').append($('<span/>').text(file.name));
if (!index) {
node
.append('<br>')
.append(uploadButton.clone(true).data(data));
}
node.appendTo(data.context);
});
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index,
file = data.files[index],
node = $(data.context.children()[index]);
if (file.preview) {
node
.prepend('<br>')
.prepend(file.preview);
}
if (file.error) {
node
.append('<br>')
.append($('<span class=\"text-danger\"/>').text(file.error));
}
if (index + 1 === data.files.length) {
data.context.find('button')
.text('Upload')
.prop('disabled', !!data.files.error);
}
}).on('fileuploadprogressall', function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}).on('fileuploaddone', function (e, data) {
$.each(data.result.files, function (index, file) {
if (file.url) {
var link = $('<a>')
//.attr('target', '_blank')
.prop('href', file.url);
$(data.context.children()[index]).wrap(link);
} else if (file.error) {
var error = $('<span class=\"text-danger\"/>').text(file.error);
$(data.context.children()[index])
.append('<br>')
.append(error);
}
});
}).on('fileuploadfail', function (e, data) {
$.each(data.files, function (index) {
var error = $('<span class=\"text-danger\"/>').text('File upload failed.');
$(data.context.children()[index])
.append('<br>')
.append(error);
});
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>";
} else {
header('location: index.php?view='.$view.'&error=Error');
exit;

@ -63,10 +63,7 @@ if ($action == 'add') {
if (isset($_POST['submitWork'])) {
$check = Security::check_token();
if ($check) {
$message = store_add_dropbox();
if (!empty($message)) {
Display :: display_confirmation_message($message);
}
store_add_dropbox();
}
}

@ -62,27 +62,24 @@ switch ($action) {
);
$json = array();
$json['name'] = Display::url(
api_htmlentities($result['title']),
api_htmlentities($result['url']),
array('target'=>'_blank')
);
$json['url'] = $result['url'];
if (!empty($result) && is_array($result)) {
$json['name'] = Display::url(
api_htmlentities($result['title']),
api_htmlentities($result['url']),
array('target'=>'_blank')
);
$json['size'] = format_file_size($file['size']);
$json['type'] = api_htmlentities($file['type']);
$json['url'] = $result['url'];
$json['size'] = format_file_size($file['size']);
$json['type'] = api_htmlentities($file['type']);
if (!empty($result) && is_array($result)) {
$json['result'] = Display::return_icon(
'accept.png',
get_lang('Uploaded')
);
} else {
$json['result'] = Display::return_icon(
'exclamation.png',
get_lang('Error')
);
$json['url'] = '';
$json['error'] = get_lang('Error');
}
$resultList[] = $json;
}

@ -0,0 +1,74 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Responses to AJAX calls for the document upload
*/
require_once '../global.inc.php';
require_once api_get_path(SYS_CODE_PATH).'dropbox/dropbox_functions.inc.php';
$action = $_REQUEST['a'];
switch ($action) {
case 'upload_file':
api_protect_course_script(true);
// User access same as upload.php
$is_allowed_to_edit = api_is_allowed_to_edit(null, true);
$recipients = isset($_POST['recipients']) ? $_POST['recipients'] : '';
if (empty($recipients)) {
$resultList[] = ['error' => get_lang('YouMustSelectAtLeastOneDestinee')];
echo json_encode(['files' => $resultList]);
exit;
}
if (!empty($_FILES)) {
$files = $_FILES['files'];
$fileList = [];
foreach ($files as $name => $array) {
$counter = 0;
foreach ($array as $data) {
$fileList[$counter][$name] = $data;
$counter++;
}
}
$resultList = [];
foreach ($fileList as $file) {
$globalFile = [];
$globalFile['files'] = $file;
/** @var Dropbox_SentWork $result */
$result = store_add_dropbox($file);
$json = array();
if (!empty($result)) {
$json['name'] = Display::url(
api_htmlentities($result->title),
api_htmlentities(api_get_path(WEB_CODE_PATH).'dropbox/index.php?'.api_get_cidreq()),
array('target' => '_blank')
);
$json['url'] = api_get_path(WEB_CODE_PATH).'dropbox/index.php?'.api_get_cidreq();
$json['size'] = format_file_size($result->filesize);
$json['type'] = api_htmlentities($file['type']);
$json['result'] = Display::return_icon(
'accept.png',
get_lang('Uploaded')
);
} else {
$json['result'] = Display::return_icon(
'exclamation.png',
get_lang('Error')
);
}
$resultList[] = $json;
}
echo json_encode(['files' => $resultList]);
}
exit;
break;
}
exit;

@ -44,27 +44,23 @@ switch ($action) {
$result = processWorkForm($workInfo, $values, $courseInfo, $sessionId, 0, $userId, $file);
$json = array();
$json['name'] = Display::url(
api_htmlentities($result['title']),
api_htmlentities($result['view_url']),
array('target' => '_blank')
);
$json['url'] = $result['view_url'];
//$json['size'] = format_file_size($result['filesize']);
$json['size'] = '';
$json['type'] = api_htmlentities($result['filetype']);
if (!empty($result) && is_array($result) && empty($result['error'])) {
$json['name'] = Display::url(
api_htmlentities($result['title']),
api_htmlentities($result['view_url']),
array('target' => '_blank')
);
$json['url'] = $result['view_url'];
$json['size'] = '';
$json['type'] = api_htmlentities($result['filetype']);
$json['result'] = Display::return_icon(
'accept.png',
get_lang('Uploaded')
);
} else {
$json['result'] = Display::return_icon(
'exclamation.png',
get_lang('Error')
);
$json['url'] = '';
$json['error'] = get_lang('Error');
}
$resultList[] = $json;
}

@ -120,8 +120,7 @@ $url = api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_
$htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
$htmlHeadXtra[] = to_javascript_work();
$htmlHeadXtra[] = "
<script>
$htmlHeadXtra[] = "<script>
$(function () {
'use strict';
var url = '".$url."';

Loading…
Cancel
Save