From 000263132270c0ac7ad1f6f7ba444118f4e1cb27 Mon Sep 17 00:00:00 2001 From: jmontoya Date: Mon, 29 Feb 2016 16:19:06 +0100 Subject: [PATCH] Add work drag and drop form see BT#10893 --- main/document/upload.php | 1 - main/inc/ajax/document.ajax.php | 5 +- main/inc/ajax/work.ajax.php | 62 +++++++++++ main/inc/lib/document.lib.php | 73 +++++++------ main/work/upload.php | 148 +++++++++++++++++++++++-- main/work/work.lib.php | 187 +++++++++++++++++--------------- main/work/work_list_all.php | 5 +- 7 files changed, 345 insertions(+), 136 deletions(-) diff --git a/main/document/upload.php b/main/document/upload.php index 58d5ce4cbb..b37144f4f7 100755 --- a/main/document/upload.php +++ b/main/document/upload.php @@ -42,7 +42,6 @@ require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php'; $htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload')); // Variables - $is_allowed_to_edit = api_is_allowed_to_edit(null, true); $_course = api_get_course_info(); $groupId = api_get_group_id(); diff --git a/main/inc/ajax/document.ajax.php b/main/inc/ajax/document.ajax.php index a56ff69a3a..6968643af4 100755 --- a/main/inc/ajax/document.ajax.php +++ b/main/inc/ajax/document.ajax.php @@ -19,10 +19,7 @@ switch ($action) { } else { exit; } - } elseif ( - $is_allowed_to_edit || - DocumentManager::is_my_shared_folder(api_get_user_id(), $_POST['curdirpath'], api_get_session_id()) - ) { + } elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $_POST['curdirpath'], api_get_session_id())) { // ?? } else { // No course admin and no group member... diff --git a/main/inc/ajax/work.ajax.php b/main/inc/ajax/work.ajax.php index ad4a71028b..3a15685ad0 100755 --- a/main/inc/ajax/work.ajax.php +++ b/main/inc/ajax/work.ajax.php @@ -11,6 +11,68 @@ $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null; $isAllowedToEdit = api_is_allowed_to_edit(); switch ($action) { + case 'upload_file': + api_protect_course_script(true); + $workId = isset($_REQUEST['id']) ? $_REQUEST['id'] : ''; + + $workInfo = get_work_data_by_id($workId); + $courseInfo = api_get_course_info(); + $sessionId = api_get_session_id(); + $userId = api_get_user_id(); + + 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; + + $values = [ + 'contains_file' => 1, + 'title' => $file['name'], + 'description' => '' + ]; + $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['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]); + } + + break; case 'delete_work': if ($isAllowedToEdit) { if (empty($_REQUEST['id'])) { diff --git a/main/inc/lib/document.lib.php b/main/inc/lib/document.lib.php index f2022247b7..167d963746 100755 --- a/main/inc/lib/document.lib.php +++ b/main/inc/lib/document.lib.php @@ -328,13 +328,14 @@ class DocumentManager $len = filesize($full_file_name); // Fixing error when file name contains a "," $filename = str_replace(',', '', $filename); - global $_configuration; + + $sendFileHeaders = api_get_configuration_value('enable_x_sendfile_headers'); if ($forced) { // Force the browser to save the file instead of opening it - if (isset($_configuration['enable_x_sendfile_headers']) && - !empty($_configuration['enable_x_sendfile_headers'])) { + if (isset($sendFileHeaders) && + !empty($sendFileHeaders)) { header("X-Sendfile: $filename"); } @@ -361,6 +362,7 @@ class DocumentManager //no forced download, just let the browser decide what to do according to the mimetype $content_type = self::file_get_mime_type($filename); + $lpFixedEncoding = api_get_configuration_value('lp_fixed_encoding'); header('Expires: Wed, 01 Jan 1990 00:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); @@ -369,7 +371,7 @@ class DocumentManager //header('Pragma: no-cache'); switch ($content_type) { case 'text/html': - if (isset($_configuration['lp_fixed_encoding']) && $_configuration['lp_fixed_encoding'] === 'true') { + if (isset($lpFixedEncoding) && $lpFixedEncoding === 'true') { $content_type .= '; charset=UTF-8'; } else { $encoding = @api_detect_encoding_html(file_get_contents($full_file_name)); @@ -379,7 +381,7 @@ class DocumentManager } break; case 'text/plain': - if (isset($_configuration['lp_fixed_encoding']) && $_configuration['lp_fixed_encoding'] === 'true') { + if (isset($lpFixedEncoding) && $lpFixedEncoding === 'true') { $content_type .= '; charset=UTF-8'; } else { $encoding = @api_detect_encoding(strip_tags(file_get_contents($full_file_name))); @@ -1586,7 +1588,7 @@ class DocumentManager if (Database::num_rows($result) > 0) { $row = Database::fetch_array($result, 'ASSOC'); if ($row['visibility'] == 1) { - $is_visible = $_SESSION['is_allowed_in_course'] || api_is_platform_admin(); + $is_visible = api_is_allowed_in_course() || api_is_platform_admin(); } } @@ -2797,7 +2799,8 @@ class DocumentManager $unzip = 0, $if_exists = null, $index_document = false, - $show_output = false + $show_output = false, + $fileKey = 'file' ) { $course_info = api_get_course_info(); $sessionId = api_get_session_id(); @@ -2805,14 +2808,14 @@ class DocumentManager $sys_course_path = api_get_path(SYS_COURSE_PATH); $base_work_dir = $sys_course_path . $course_dir; - if (isset($files['file'])) { - $upload_ok = process_uploaded_file($files['file'], $show_output); + if (isset($files[$fileKey])) { + $upload_ok = process_uploaded_file($files[$fileKey], $show_output); if ($upload_ok) { // File got on the server without problems, now process it $new_path = handle_uploaded_document( $course_info, - $files['file'], + $files[$fileKey], $base_work_dir, $path, api_get_user_id(), @@ -2898,11 +2901,13 @@ class DocumentManager false, $sessionId ); + return $documentData; } } } } + return false; } @@ -3106,8 +3111,8 @@ class DocumentManager * on the base of a maximum directory size allowed * * @author Bert Vanderkimpen - * @param int file_size size of the file in byte - * @param int max_dir_space maximum size + * @param int $file_size size of the file in byte + * @param int $max_dir_space maximum size * @return boolean true if there is enough space, false otherwise * * @see enough_space() uses documents_total_space() function @@ -3123,31 +3128,33 @@ class DocumentManager } /** - * @param array parameters: count, url, extension + * @param array $params count, url, extension * @return string */ static function generate_jplayer_jquery($params = array()) { $js_path = api_get_path(WEB_LIBRARY_PATH) . 'javascript/'; - $js = ' $("#jquery_jplayer_' . $params['count'] . '").jPlayer({ - ready: function() { - $(this).jPlayer("setMedia", { - ' . $params['extension'] . ' : "' . $params['url'] . '" - }); - }, - play: function() { // To avoid both jPlayers playing together. - $(this).jPlayer("pauseOthers"); - }, - //errorAlerts: true, - //warningAlerts: true, - swfPath: "' . $js_path . 'jquery-jplayer/jplayer/", - //supplied: "m4a, oga, mp3, ogg, wav", - supplied: "' . $params['extension'] . '", - wmode: "window", - solution: "flash, html", // Do not change this setting - cssSelectorAncestor: "#jp_container_' . $params['count'] . '", - }); ' . "\n\n"; + $js = ' + $("#jquery_jplayer_' . $params['count'] . '").jPlayer({ + ready: function() { + $(this).jPlayer("setMedia", { + ' . $params['extension'] . ' : "' . $params['url'] . '" + }); + }, + play: function() { // To avoid both jPlayers playing together. + $(this).jPlayer("pauseOthers"); + }, + //errorAlerts: true, + //warningAlerts: true, + swfPath: "' . $js_path . 'jquery-jplayer/jplayer/", + //supplied: "m4a, oga, mp3, ogg, wav", + supplied: "' . $params['extension'] . '", + wmode: "window", + solution: "flash, html", // Do not change this setting + cssSelectorAncestor: "#jp_container_' . $params['count'] . '", + }); ' . "\n\n"; + return $js; } @@ -5315,8 +5322,8 @@ class DocumentManager 'style' => 'float: left;' ] ) - . $force_download_html . $send_to . $copy_to_myfiles - . $open_in_new_window_link . $pdf_icon; + . $force_download_html . $send_to . $copy_to_myfiles + . $open_in_new_window_link . $pdf_icon; } else { // For PDF Download the file. $pdfPreview = null; diff --git a/main/work/upload.php b/main/work/upload.php index f04bce65dd..44a5bf12f5 100755 --- a/main/work/upload.php +++ b/main/work/upload.php @@ -32,7 +32,11 @@ protectWork($course_info, $work_id); $workInfo = get_work_data_by_id($work_id); -$is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_id, $session_id); +$is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course( + $user_id, + $course_id, + $session_id +); $is_course_member = $is_course_member || api_is_platform_admin(); if ($is_course_member == false || api_is_invitee()) { @@ -80,7 +84,9 @@ $form = new FormValidator( '', array('enctype' => "multipart/form-data") ); + setWorkUploadForm($form, $workInfo['allow_text_assignment']); + $form->addElement('hidden', 'id', $work_id); $form->addElement('hidden', 'sec_token', $token); @@ -92,7 +98,7 @@ if ($form->validate()) { if ($student_can_edit_in_session && $check) { $values = $form->getSubmitValues(); // Process work - $error_message = processWorkForm( + processWorkForm( $workInfo, $values, $course_info, @@ -104,9 +110,6 @@ if ($form->validate()) { if ($is_allowed_to_edit) { $script = 'work_list_all.php'; } - if (!empty($error_message)) { - Session::write('error_message', $error_message); - } header('Location: '.api_get_path(WEB_CODE_PATH).'work/'.$script.'?'.api_get_cidreq().'&id='.$work_id); exit; } else { @@ -115,19 +118,150 @@ if ($form->validate()) { } } +$url = api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_file&id='.$work_id; + +$htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload')); $htmlHeadXtra[] = to_javascript_work(); +$htmlHeadXtra[] = " +"; + Display :: display_header(null); +$headers = array( + get_lang('Upload'), + get_lang('Upload').' ('.get_lang('Simple').')', +); + +$multiple_form = '
'.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'
'; +$multiple_form .= ' + + + '.get_lang('AddFiles').' + + + + +
+
+ +
+
+
+
+'; + +$tabs = Display::tabs($headers, array($multiple_form, $form->returnForm()), 'tabs'); + if (!empty($work_id)) { echo $validationStatus['message']; if ($is_allowed_to_edit) { if (api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION)) { echo Display::display_warning_message(get_lang('ResourceLockedByGradebook')); } else { - $form->display(); + echo $tabs; } } elseif ($student_can_edit_in_session && $validationStatus['has_ended'] == false) { - $form->display(); + echo $tabs; } else { Display::display_error_message(get_lang('ActionNotAllowed')); } diff --git a/main/work/work.lib.php b/main/work/work.lib.php index dce487d64d..66f63f9c48 100755 --- a/main/work/work.lib.php +++ b/main/work/work.lib.php @@ -240,6 +240,7 @@ function get_work_data_by_id($id, $courseId = null, $sessionId = null) $work['download_url'] = api_get_path(WEB_CODE_PATH).'work/download.php?id='.$work['id'].'&'.api_get_cidreq(); $work['view_url'] = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&'.api_get_cidreq(); $work['show_url'] = api_get_path(WEB_CODE_PATH).'work/show_file.php?id='.$work['id'].'&'.api_get_cidreq(); + $work['show_content'] = ''; if ($work['contains_file']) { $fileInfo = pathinfo($work['title']); if (is_array($fileInfo) && @@ -279,7 +280,7 @@ function get_work_count_by_student($user_id, $work_id) $result = Database::query($sql); $return = 0; if (Database::num_rows($result)) { - $return = Database::fetch_row($result,'ASSOC'); + $return = Database::fetch_row($result, 'ASSOC'); $return = intval($return[0]); } @@ -535,7 +536,6 @@ function getUniqueStudentAttempts( function showStudentWorkGrid() { $courseInfo = api_get_course_info(); - $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student&'.api_get_cidreq(); $columns = array( @@ -1078,96 +1078,94 @@ function count_dir($path_dir, $recurse) */ function to_javascript_work() { - $origin = isset($_REQUEST['origin']) && !empty($_REQUEST['origin']) ? api_get_tools_lists($_REQUEST['origin']) : ''; - $js = ''; return $js; @@ -2099,7 +2097,7 @@ function get_work_user_list( $correction .= "