Should fix bug when uploading a lot of files see #4503

skala
Julio Montoya 14 years ago
parent fc70ff7e6c
commit e29ab3348e
  1. 9
      main/inc/lib/document.lib.php
  2. 31
      main/inc/lib/fileUpload.lib.php
  3. 37
      main/newscorm/learnpath.class.php
  4. 46
      main/newscorm/learnpathItem.class.php
  5. 134
      main/newscorm/lp_add_audio.php
  6. 35
      main/newscorm/lp_controller.php

@ -2184,11 +2184,18 @@ class DocumentManager {
if (!empty($docid)) {
$table_document = Database::get_course_table(TABLE_DOCUMENT);
$params = array();
if (!empty($title)) {
$params['title'] = get_document_title($title);
} else {
$params['title'] = get_document_title($files['file']['name']);
if (isset($if_exists) && $if_exists == 'rename') {
$new_path = basename($new_path);
$params['title'] = get_document_title($new_path);
} else {
$params['title'] = get_document_title($files['file']['name']);
}
}
if (!empty($comment)) {
$params['comment'] = trim($comment);
}

@ -161,12 +161,15 @@ function process_uploaded_file($uploaded_file, $show_output = true) {
*/
function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upload_path, $user_id, $to_group_id = 0, $to_user_id = null, $maxFilledSpace = '', $unzip = 0, $what_if_file_exists = '', $output = true) {
if (!$user_id) die('Not a valid user.');
// Strip slashes
$uploaded_file['name'] = stripslashes($uploaded_file['name']);
// Add extension to files without one (if possible)
$uploaded_file['name'] = add_ext_on_mime($uploaded_file['name'], $uploaded_file['type']);
$uploaded_file['name'] = add_ext_on_mime($uploaded_file['name'], $uploaded_file['type']);
$current_session_id = api_get_session_id();
if (empty($maxFilledSpace)) {
$maxFilledSpace = DocumentManager::get_course_quota();
}
// Check if there is enough space to save the file
if (!DocumentManager::enough_space($uploaded_file['size'], $maxFilledSpace)) {
@ -174,7 +177,7 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
Display::display_error_message(get_lang('UplNotEnoughSpace'));
}
return false;
}
}
// If the want to unzip, check if the file has a .zip (or ZIP,Zip,ZiP,...) extension
if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
@ -185,14 +188,15 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
Display::display_error_message(get_lang('UplNotAZip')." ".get_lang('PleaseTryAgain'));
}
return false;
} else {
} else {
// Clean up the name, only ASCII characters should stay. (and strict)
$clean_name = replace_dangerous_char($uploaded_file['name'], 'strict');
// No "dangerous" files
$clean_name = disable_dangerous_file($clean_name);
$clean_name = disable_dangerous_file($clean_name);
if (!filter_extension($clean_name)) {
if ($output){
if ($output) {
Display::display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
}
return false;
@ -226,7 +230,7 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
$file_size = $uploaded_file['size'];
$files_perm = api_get_permissions_for_new_files();
// What to do if the target file exists
switch ($what_if_file_exists) {
// Overwrite the file if it exists
@ -268,7 +272,7 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
return $file_path;
}
} else {
if ($output){
if ($output) {
Display::display_error_message(get_lang('UplUnableToSaveFile'));
}
return false;
@ -276,11 +280,11 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
break;
// Rename the file if it exists
case 'rename':
case 'rename':
$new_name = unique_name($where_to_save, $clean_name);
$store_path = $where_to_save.$new_name;
$new_file_path = $upload_path.$new_name;
if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
chmod($store_path, $files_perm);
@ -299,13 +303,15 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
}
return $new_file_path;
} else {
Display::display_error_message(get_lang('UplUnableToSaveFile'));
if ($output) {
Display::display_error_message(get_lang('UplUnableToSaveFile'));
}
return false;
}
break;
// Only save the file if it doesn't exist or warn user if it does exist
default:
default:
if (file_exists($store_path)) {
if ($output) {
Display::display_error_message($clean_name.' '.get_lang('UplAlreadyExists'));
@ -316,6 +322,7 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
// Put the document data in the database
$document_id = add_document($_course, $file_path, 'file', $file_size, $document_name);
if ($document_id) {
// Update document item_property
api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $user_id, $to_group_id, $to_user_id, null, null, $current_session_id);

@ -4997,16 +4997,17 @@ class learnpath {
$return_audio .= '<td align="center">';
$audio = '';
/*
if (!$update_audio OR $update_audio <> 'true') {
if (!empty ($arrLP[$i]['audio'])) {
$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
$audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
$audio .= '<script type="text/javascript">
var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
s1.write("container' . $i . '");
</script>';
var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
s1.write("container' . $i . '");
</script>';
} else {
$audio .= '';
}
@ -5017,12 +5018,14 @@ class learnpath {
$audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br /><input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
}
}
}
}*/
$return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
$return_audio .= '</td>';
$move_icon = '';
$move_item_icon = '';
$edit_icon = '';
$delete_icon = '';
$audio_icon = '';$prerequisities_icon = '';
if ($is_allowed_to_edit) {
if (!$update_audio OR $update_audio <> 'true') {
@ -5046,18 +5049,15 @@ class learnpath {
$delete_icon .= '</a>';
$url = api_get_self() . '?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'chapter') {
if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
$prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('Prerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
$move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
$audio_icon = Display::url(Display::return_icon('audio.png', get_lang('Upload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
}
//if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'chapter') {
$moves_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
//}
}
if ($update_audio != 'true') {
$row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$moves_icon.$delete_icon, array('class'=>'button_actions'));
$row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
} else {
$row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
}
@ -5180,16 +5180,15 @@ class learnpath {
$return .= '<a href="lp_controller.php?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&action=build&lp_id=' . $this->lp_id . '">' . Display :: return_icon('home.png', get_lang('Build'),'',ICON_SIZE_MEDIUM).'</a>';
if ($is_allowed_to_edit) {
}
//$return .= '<a href="' . api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('BasicOverview') . '">' . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM).'</a>';
$return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('view_left_right.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
$return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=step&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewStep') . '">' . Display :: return_icon('new_learnigpath_object.png', get_lang('NewStep'),'',ICON_SIZE_MEDIUM).'</a>';
// echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=chapter&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewChapter') . '">' . Display :: return_icon('add_learnpath_section.png', get_lang('NewChapter'),'',ICON_SIZE_MEDIUM).'</a>';
$return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
//$return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
$return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;action=edit&amp;lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
$return .= '</div>';
echo $return;
@ -9070,6 +9069,8 @@ EOD;
$course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
$course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
}
}

@ -1521,8 +1521,7 @@ class learnpathItem {
} else {
if (self::debug > 1) { error_log('New LP - Prerequisite '.$prereqs_string.' complete', 0); }
}
//error_log('status of document'.$status);
//var_dump($returnstatus);
//$returnstatus = true;
if ($returnstatus && $this->prevent_reinit == 1) {
// I would prefer check in the database.
@ -1541,7 +1540,6 @@ class learnpathItem {
$status_array = Database :: fetch_row($rs_lp);
$status = $status_array[0];
//var_dump($status);
$returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
if (!$returnstatus && empty($this->prereq_alert)){
$this->prereq_alert = get_lang('_prereq_not_complete');
@ -2592,4 +2590,46 @@ class learnpathItem {
if (self::debug > 2) { error_log('New LP - End of learnpathItem::write_to_db()', 0); }
return true;
}
function add_audio() {
$course_info = api_get_course_info();
$filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/';
if (!is_dir($filepath.'audio')) {
mkdir($filepath.'audio', api_get_permissions_for_new_directories());
$audio_id = add_document($course_info, '/audio', 'folder', 0, 'audio');
api_item_property_update($course_info, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
}
$key = 'file';
if (!isset($_FILES[$key]['name']) || !isset($_FILES[$key]['tmp_name'])) {
return false;
}
$result = DocumentManager::upload_document($_FILES, '/audio', null, null, 0, 'rename', false, false);
$file_path = null;
if ($result) {
$file_path = basename($result['path']);
// Store the mp3 file in the lp_item table.
$tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
$sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string($file_path)."'
WHERE c_id = {$course_info['real_id']} AND id = '".Database::escape_string($this->db_id)."'";
Database::query($sql_insert_audio);
}
return $file_path;
}
function remove_audio() {
$tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
$course_id = api_get_course_int_id();
if (empty($this->db_id)) {
return false;
}
$sql = "UPDATE $tbl_lp_item SET audio = '' WHERE c_id = $course_id AND id IN (".$this->db_id.")";
Database::query($sql);
}
}

@ -0,0 +1,134 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This is a learning path creation and player tool in Chamilo - previously
* learnpath_handler.php
*
* @author Patrick Cool
* @author Denes Nagy
* @author Roan Embrechts, refactoring and code cleaning
* @author Yannick Warnier <ywarnier@beeznest.org> - cleaning and update
* @author Julio Montoya - Improving the list of templates
* @package chamilo.learnpath
*/
/**
* INIT SECTION
*/
$this_section = SECTION_COURSES;
api_protect_course_script();
include 'learnpath_functions.inc.php';
include 'resourcelinker.inc.php';
$language_file = 'learnpath';
/* Constants and variables */
$is_allowed_to_edit = api_is_allowed_to_edit(null, true);
$isStudentView = (int) $_REQUEST['isStudentView'];
$learnpath_id = (int) $_REQUEST['lp_id'];
$submit = $_POST['submit_button'];
$type = isset($_GET['type']) ? $_GET['type'] : null;
$action = isset($_GET['action']) ? $_GET['action'] : null;
// Using the resource linker as a tool for adding resources to the learning path.
if ($action == 'add' && $type == 'learnpathitem') {
$htmlHeadXtra[] = "<script language='JavaScript' type='text/javascript'> window.location=\"../resourcelinker/resourcelinker.php?source_id=5&action=$action&learnpath_id=$learnpath_id&chapter_id=$chapter_id&originalresource=no\"; </script>";
}
if ((!$is_allowed_to_edit) || ($isStudentView)) {
error_log('New LP - User not authorized in lp_add_item.php');
header('location:lp_controller.php?action=view&lp_id='.$learnpath_id);
exit;
}
/* SHOWING THE ADMIN TOOLS */
if (isset($_SESSION['gradebook'])) {
$gradebook = $_SESSION['gradebook'];
}
if (!empty($gradebook) && $gradebook == 'view') {
$interbreadcrumb[] = array (
'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
'name' => get_lang('ToolGradebook')
);
}
$interbreadcrumb[] = array('url' => 'lp_controller.php?action=list', 'name' => get_lang('LearningPaths'));
$interbreadcrumb[] = array('url' => api_get_self()."?action=build&lp_id=$learnpath_id", 'name' => $_SESSION['oLP']->get_name());
switch ($type) {
case 'chapter':
$interbreadcrumb[]= array ('url' => 'lp_controller.php?action=add_item&type=step&lp_id='.$_SESSION['oLP']->get_id(), 'name' => get_lang('NewStep'));
$interbreadcrumb[]= array ('url' => '#', 'name' => get_lang('NewChapter'));
break;
case 'document':
$interbreadcrumb[]= array ('url' => 'lp_controller.php?action=add_item&type=step&lp_id='.$_SESSION['oLP']->get_id(), 'name' => get_lang('NewStep'));
break;
default:
$interbreadcrumb[]= array ('url' => '#', 'name' => get_lang('NewStep'));
break;
}
if ($action == 'add_item' && $type == 'document' ) {
$interbreadcrumb[]= array ('url' => '#', 'name' => get_lang('NewDocumentCreated'));
}
// Theme calls.
$show_learn_path = true;
$lp_item_id = isset($_GET['id']) ? intval($_GET['id']) : null;
if (empty($lp_item_id)) {
api_not_allowed();
}
Display::display_header(null, 'Path');
$suredel = trim(get_lang('AreYouSureToDelete'));
/* DISPLAY SECTION */
echo $_SESSION['oLP']->build_action_menu();
echo '<div class="row-fluid" style="overflow:hidden">';
echo '<div id="lp_sidebar" class="span4">';
echo $_SESSION['oLP']->return_new_tree(null, true);
// Show the template list.
echo '</div>';
echo '<div id="doc_form" class="span8">';
$form = new FormValidator('add_audio', 'post', api_get_self().'?action=add_audio&id='.$lp_item_id, null, array('enctype' => 'multipart/form-data'));
$form->addElement('header', get_lang('Upload'));
$form->addElement('file', 'file', get_lang('File'));
$form->addElement('hidden', 'id', $lp_item_id);
$lp_item = new learnpathItem($lp_item_id);
if (isset($lp_item->audio) && !empty($lp_item->audio)) {
$form->addElement('checkbox', 'delete_file', null, get_lang('RemoveAudio'));
$player = '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
$player .= '<div id="preview"></div><script type="text/javascript">
var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $lp_item->audio . '");
s1.write("preview");
</script>';
$form->addElement('label', get_lang('Preview'), $player);
}
$form->addElement('button', 'submit', get_lang('Edit'));
//RemoveAudio
$form->display();
echo '</div>';
echo '</div>';
/* FOOTER */
Display::display_footer();

@ -267,9 +267,42 @@ switch ($action) {
require 'lp_add_item.php';
}
}
break;
case 'add_audio':
if (!$is_allowed_to_edit) {
api_not_allowed(true);
}
if ($debug > 0) error_log('New LP - add audio action triggered', 0);
if (!$lp_found) {
//check if the learnpath ID was defined, otherwise send back to list
if ($debug > 0) error_log('New LP - No learnpath given for add audio', 0);
require 'lp_list.php';
} else {
$_SESSION['refresh'] = 1;
if (isset($_POST['id'])) {
$lp_item_obj = new learnpathItem($_POST['id']);
if (isset($_POST['delete_file']) && $_POST['delete_file'] == 1) {
$lp_item_obj->remove_audio();
}
if (isset($_FILES['file']) && !empty($_FILES['file'])) {
//Updating the lp.modified_on
$_SESSION['oLP']->set_modified_on();
$lp_item_obj->add_audio();
}
// Display.
require 'lp_add_audio.php';
} else {
require 'lp_add_audio.php';
}
}
break;
case 'add_lp':
if (!$is_allowed_to_edit) {
api_not_allowed(true);

Loading…
Cancel
Save