Fixing error when deleting document see BT#6642

1.9.x
Julio Montoya 12 years ago
parent c423fc5a8a
commit 138e5e43b2
  1. 69
      main/document/document.php
  2. 168
      main/inc/lib/document.lib.php

@ -84,8 +84,6 @@ $(function() {
// Create directory certificates // Create directory certificates
DocumentManager::create_directory_certificate_in_course(api_get_course_id()); DocumentManager::create_directory_certificate_in_course(api_get_course_id());
if (empty($course_info)) { if (empty($course_info)) {
api_not_allowed(true); api_not_allowed(true);
} }
@ -169,7 +167,7 @@ switch ($action) {
// Launch event // Launch event
event_download($document_data['url']); event_download($document_data['url']);
// Check visibility of document and paths // Check visibility of document and paths
if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible_by_id($document_id, $course_info, api_get_session_id(), api_get_user_id())) { if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible_by_id($document_id, $course_info, $session_id, api_get_user_id())) {
api_not_allowed(true); api_not_allowed(true);
} }
$full_file_name = $base_work_dir.$document_data['path']; $full_file_name = $base_work_dir.$document_data['path'];
@ -268,8 +266,8 @@ if (isset($document_id) && empty($action)) {
if ($document_data) { if ($document_data) {
$parent_id = $document_data['parent_id']; $parent_id = $document_data['parent_id'];
//$visibility = DocumentManager::is_visible_by_id($document_id, $course_info, api_get_session_id(), api_get_user_id()); //$visibility = DocumentManager::is_visible_by_id($document_id, $course_info, $session_id, api_get_user_id());
$visibility = DocumentManager::check_visibility_tree($document_id, api_get_course_id(), api_get_session_id(), api_get_user_id()); $visibility = DocumentManager::check_visibility_tree($document_id, api_get_course_id(), $session_id, api_get_user_id());
if (!empty($document_data['filetype']) && $document_data['filetype'] == 'file') { if (!empty($document_data['filetype']) && $document_data['filetype'] == 'file') {
if ($visibility && api_is_allowed_to_session_edit()) { if ($visibility && api_is_allowed_to_session_edit()) {
@ -307,7 +305,6 @@ if (isset($document_id) && empty($action)) {
$curdirpath = '/'; $curdirpath = '/';
} }
$curdirpathurl = urlencode($curdirpath); $curdirpathurl = urlencode($curdirpath);
// Check the path // Check the path
@ -409,7 +406,7 @@ if ($to_group_id != 0 && $curdirpath == '/') {
//if (!$is_allowed_to_edit || api_is_coach()) { before //if (!$is_allowed_to_edit || api_is_coach()) { before
if (!$is_allowed_to_edit && api_is_coach()) { if (!$is_allowed_to_edit && api_is_coach()) {
if ($curdirpath != '/' && !(DocumentManager::is_visible($curdirpath, $_course, api_get_session_id(), 'folder'))) { if ($curdirpath != '/' && !(DocumentManager::is_visible($curdirpath, $_course, $session_id, 'folder'))) {
api_not_allowed(true); api_not_allowed(true);
} }
} }
@ -422,7 +419,7 @@ if ($session_id == 0) {
$usf_dir_name = '/shared_folder'; $usf_dir_name = '/shared_folder';
$to_group_id = 0; $to_group_id = 0;
$visibility = 0; $visibility = 0;
create_unexisting_directory($_course, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility); create_unexisting_directory($_course, api_get_user_id(), $session_id, $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
} }
// Create dynamic user shared folder // Create dynamic user shared folder
if (!file_exists($base_work_dir.'/shared_folder/sf_user_'.api_get_user_id())) { if (!file_exists($base_work_dir.'/shared_folder/sf_user_'.api_get_user_id())) {
@ -430,7 +427,7 @@ if ($session_id == 0) {
$usf_dir_name = '/shared_folder/sf_user_'.api_get_user_id(); $usf_dir_name = '/shared_folder/sf_user_'.api_get_user_id();
$to_group_id = 0; $to_group_id = 0;
$visibility = 1; $visibility = 1;
create_unexisting_directory($_course, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility); create_unexisting_directory($_course, api_get_user_id(), $session_id, $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
} }
} else { } else {
//Create shared folder session //Create shared folder session
@ -439,7 +436,7 @@ if ($session_id == 0) {
$usf_dir_name = '/shared_folder_session_'.$session_id; $usf_dir_name = '/shared_folder_session_'.$session_id;
$to_group_id = 0; $to_group_id = 0;
$visibility = 0; $visibility = 0;
create_unexisting_directory($_course, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility); create_unexisting_directory($_course, api_get_user_id(), $session_id, $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
} }
//Create dynamic user shared folder into a shared folder session //Create dynamic user shared folder into a shared folder session
if (!file_exists($base_work_dir.'/shared_folder_session_'.$session_id.'/sf_user_'.api_get_user_id())) { if (!file_exists($base_work_dir.'/shared_folder_session_'.$session_id.'/sf_user_'.api_get_user_id())) {
@ -447,7 +444,7 @@ if ($session_id == 0) {
$usf_dir_name = '/shared_folder_session_'.$session_id.'/sf_user_'.api_get_user_id(); $usf_dir_name = '/shared_folder_session_'.$session_id.'/sf_user_'.api_get_user_id();
$to_group_id = 0; $to_group_id = 0;
$visibility = 1; $visibility = 1;
create_unexisting_directory($_course, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility); create_unexisting_directory($_course, api_get_user_id(), $session_id, $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
} }
} }
@ -521,7 +518,7 @@ if (!empty($docs_and_folders)) {
if ($extension == 'ogg') { if ($extension == 'ogg') {
$extension = 'oga'; $extension = 'oga';
} }
//$("#jplayer_inspector_'.$count.'").jPlayerInspector({jPlayer:$("#jquery_jplayer_'.$count.'")});
$params = array('url' => $document_data['direct_url'], $params = array('url' => $document_data['direct_url'],
'extension' => $extension, 'extension' => $extension,
'count' => $count 'count' => $count
@ -573,7 +570,7 @@ if (
$my_get_move = intval($_REQUEST['move']); $my_get_move = intval($_REQUEST['move']);
if (api_is_coach()) { if (api_is_coach()) {
if (!DocumentManager::is_visible_by_id($my_get_move, $course_info, api_get_session_id(), api_get_user_id())) { if (!DocumentManager::is_visible_by_id($my_get_move, $course_info, $session_id, api_get_user_id())) {
api_not_allowed(); api_not_allowed();
} }
} }
@ -618,7 +615,7 @@ if (
} }
if (api_is_coach()) { if (api_is_coach()) {
if (!DocumentManager::is_visible_by_id($_POST['move_file'], $_course, api_get_session_id(), api_get_user_id())) { if (!DocumentManager::is_visible_by_id($_POST['move_file'], $_course, $session_id, api_get_user_id())) {
api_not_allowed(); api_not_allowed();
} }
} }
@ -672,7 +669,7 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
if (isset($_GET['delete'])) { if (isset($_GET['delete'])) {
if (!$is_allowed_to_edit) { if (!$is_allowed_to_edit) {
if (api_is_coach()) { if (api_is_coach()) {
if (!DocumentManager::is_visible($_GET['delete'], $_course, api_get_session_id())) { if (!DocumentManager::is_visible($_GET['delete'], $_course, $session_id)) {
api_not_allowed(); api_not_allowed();
} }
} }
@ -685,10 +682,8 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
// Check whether the document is in the database // Check whether the document is in the database
if (!empty($document_data)) { if (!empty($document_data)) {
if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) { if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) {
if (isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id'] == strval(intval($_GET['delete_certificate_id']))) { $certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
$default_certificate_id = $_GET['delete_certificate_id']; DocumentManager::remove_attach_certificate(api_get_course_id(), $certificateId);
DocumentManager::remove_attach_certificate(api_get_course_id(), $default_certificate_id);
}
Display::display_confirmation_message(get_lang('DocDeleted')); Display::display_confirmation_message(get_lang('DocDeleted'));
} else { } else {
Display::display_error_message(get_lang('DocDeleteError')); Display::display_error_message(get_lang('DocDeleteError'));
@ -707,13 +702,12 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
continue; continue;
} else { } else {
$documentId = DocumentManager::get_document_id($_course, $path); $documentId = DocumentManager::get_document_id($_course, $path, $session_id);
$data = DocumentManager::get_document_data_by_id($documentId, $_course['code']); $data = DocumentManager::get_document_data_by_id($documentId, $_course['code'], false, $session_id);
switch ($_POST['action']) { switch ($_POST['action']) {
case 'set_invisible': case 'set_invisible':
$visibilityCommand = 'invisible'; $visibilityCommand = 'invisible';
if (api_item_property_update( if (api_item_property_update(
$_course, $_course,
TOOL_DOCUMENT, TOOL_DOCUMENT,
@ -751,18 +745,26 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
} }
break; break;
case 'delete': case 'delete':
foreach ($files as $checkPath) {
foreach ($files as $path) {
if (!$is_allowed_to_edit) { if (!$is_allowed_to_edit) {
if (DocumentManager::check_readonly($_course, api_get_user_id(), $path)) { if (DocumentManager::check_readonly(
$_course,
api_get_user_id(),
$checkPath,
null,
false,
$session_id
)
) {
Display::display_error_message(get_lang('CantDeleteReadonlyFiles')); Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
break 2; break 2;
} }
} }
} }
$delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir); $deleteDocument = DocumentManager::delete_document($_course, $path, $base_work_dir, $session_id);
if (!empty($delete_document)) {
if (!empty($deleteDocument)) {
Display::display_confirmation_message(get_lang('DocDeleted').': '.$data['path']); Display::display_confirmation_message(get_lang('DocDeleted').': '.$data['path']);
} }
break; break;
@ -795,7 +797,7 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
if (!is_dir($dir_check)) { if (!is_dir($dir_check)) {
$created_dir = create_unexisting_directory($_course, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name); $created_dir = create_unexisting_directory($_course, api_get_user_id(), $session_id, $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name);
if ($created_dir) { if ($created_dir) {
Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>', false); Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>', false);
@ -818,7 +820,6 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
} }
/* VISIBILITY COMMANDS */ /* VISIBILITY COMMANDS */
//Only teacher
if ($is_allowed_to_edit) { if ($is_allowed_to_edit) {
if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') { if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
// Make visible or invisible? // Make visible or invisible?
@ -832,7 +833,7 @@ if ($is_allowed_to_edit) {
if (!$is_allowed_to_edit) { if (!$is_allowed_to_edit) {
if (api_is_coach()) { if (api_is_coach()) {
if (!DocumentManager::is_visible_by_id($update_id, $_course, api_get_session_id(), api_get_user_id())) { if (!DocumentManager::is_visible_by_id($update_id, $_course, $session_id, api_get_user_id())) {
api_not_allowed(); api_not_allowed();
} }
} }
@ -953,7 +954,6 @@ if (api_get_group_id() != 0) {
$folders = DocumentManager::get_all_document_folders($_course, api_get_group_id(), $is_allowed_to_edit || $group_member_with_upload_rights); $folders = DocumentManager::get_all_document_folders($_course, api_get_group_id(), $is_allowed_to_edit || $group_member_with_upload_rights);
} }
//$folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights); //$folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
if ($folders === false) { if ($folders === false) {
$folders = array(); $folders = array();
@ -1020,7 +1020,6 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
<?php <?php
} }
// Record an image clip from my webcam // Record an image clip from my webcam
if (api_get_setting('enable_webcam_clip') == 'true') { if (api_get_setting('enable_webcam_clip') == 'true') {
?> ?>
@ -1082,7 +1081,6 @@ if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_fold
<?php Display::display_icon('new_folder.png', get_lang('CreateDir'), '', ICON_SIZE_MEDIUM); ?></a> <?php Display::display_icon('new_folder.png', get_lang('CreateDir'), '', ICON_SIZE_MEDIUM); ?></a>
<?php <?php
} }
} }
$table_footer = ''; $table_footer = '';
@ -1103,9 +1101,8 @@ if (isset($docs_and_folders) && is_array($docs_and_folders)) {
$row['id'] = $document_data['id']; $row['id'] = $document_data['id'];
$row['type'] = $document_data['filetype']; $row['type'] = $document_data['filetype'];
// If the item is invisible, wrap it in a span with class invisible // If the item is invisible, wrap it in a span with class invisible.
$is_visible = DocumentManager::is_visible_by_id($document_data['id'], $course_info, $session_id, api_get_user_id(), false);
$is_visible = DocumentManager::is_visible_by_id($document_data['id'], $course_info, api_get_session_id(), api_get_user_id(), false);
$invisibility_span_open = ($is_visible == 0) ? '<span class="muted">' : ''; $invisibility_span_open = ($is_visible == 0) ? '<span class="muted">' : '';
$invisibility_span_close = ($is_visible == 0) ? '</span>' : ''; $invisibility_span_close = ($is_visible == 0) ? '</span>' : '';

@ -765,11 +765,25 @@ class DocumentManager
* @param int $document_id in case you dont have the file path ,insert the id of the file here and leave $file in blank '' * @param int $document_id in case you dont have the file path ,insert the id of the file here and leave $file in blank ''
* @return boolean true/false * @return boolean true/false
* */ * */
public static function check_readonly($_course, $user_id, $file, $document_id = '', $to_delete = false) public static function check_readonly(
{ $_course,
$user_id,
$file,
$document_id = '',
$to_delete = false,
$sessionId = null
) {
if (empty($sessionId)) {
$sessionId = api_get_session_id();
} else {
$sessionId = intval($sessionId);
}
if (!(!empty($document_id) && is_numeric($document_id))) { if (!(!empty($document_id) && is_numeric($document_id))) {
$document_id = self::get_document_id($_course, $file); $document_id = self::get_document_id($_course, $file, $sessionId);
} }
$TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); $TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$course_id = $_course['real_id']; $course_id = $_course['real_id'];
@ -778,12 +792,14 @@ class DocumentManager
if (self::is_folder($_course, $document_id)) { if (self::is_folder($_course, $document_id)) {
if (!empty($file)) { if (!empty($file)) {
$path = Database::escape_string($file); $path = Database::escape_string($file);
$what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id FROM " . $TABLE_DOCUMENT . " td , $TABLE_PROPERTY tp $what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id
FROM $TABLE_DOCUMENT td , $TABLE_PROPERTY tp
WHERE td.c_id = $course_id AND WHERE td.c_id = $course_id AND
tp.c_id = $course_id AND tp.c_id = $course_id AND
td.session_id = $sessionId AND
tp.ref= td.id AND tp.ref= td.id AND
(path='" . $path . "' OR path LIKE BINARY '" . $path . "/%' ) "; (path='" . $path . "' OR path LIKE BINARY '" . $path . "/%' ) ";
//get all id's of documents that are deleted // Get all id's of documents that are deleted
$what_to_check_result = Database::query($what_to_check_sql); $what_to_check_result = Database::query($what_to_check_sql);
if ($what_to_check_result && Database::num_rows($what_to_check_result) != 0) { if ($what_to_check_result && Database::num_rows($what_to_check_result) != 0) {
@ -809,13 +825,14 @@ class DocumentManager
} }
if (!empty($document_id)) { if (!empty($document_id)) {
$sql = "SELECT a.insert_user_id, b.readonly FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b $sql = "SELECT a.insert_user_id, b.readonly
FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b
WHERE WHERE
a.c_id = $course_id AND a.c_id = $course_id AND
b.c_id = $course_id AND b.c_id = $course_id AND
a.ref = b.id and a.ref = $document_id LIMIT 1"; a.ref = b.id and a.ref = $document_id LIMIT 1";
$resultans = Database::query($sql); $result = Database::query($sql);
$doc_details = Database ::fetch_array($resultans, 'ASSOC'); $doc_details = Database ::fetch_array($result, 'ASSOC');
if ($doc_details['readonly'] == 1) { if ($doc_details['readonly'] == 1) {
return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin()); return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin());
@ -846,32 +863,50 @@ class DocumentManager
* @param int $session_id * @param int $session_id
* @param bool $remove_content_from_db * @param bool $remove_content_from_db
*/ */
public static function delete_document_from_db($document_id, $course_info = array(), $session_id = 0, $remove_content_from_db = false) public static function delete_document_from_db(
{ $document_id,
$course_info = array(),
$session_id = 0,
$remove_content_from_db = false
) {
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY); $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
// Deleting from the DB // Deleting from the DB
$user_id = api_get_user_id(); $user_id = api_get_user_id();
$document_id = intval($document_id);
if (empty($course_info)) { if (empty($course_info)) {
$course_info = api_get_course_info(); $course_info = api_get_course_info();
} }
if (empty($session_id)) { if (empty($session_id)) {
$session_id = api_get_session_id(); $session_id = api_get_session_id();
} }
// Soft DB delete // Soft DB delete
api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'delete', $user_id, null, null, null, null, $session_id); api_item_property_update(
$course_info,
TOOL_DOCUMENT,
$document_id,
'delete',
$user_id,
null,
null,
null,
null,
$session_id
);
self::delete_document_from_search_engine($course_info['code'], $document_id); self::delete_document_from_search_engine($course_info['code'], $document_id);
self::unset_document_as_template($document_id, $course_info['code'], $user_id); self::unset_document_as_template($document_id, $course_info['code'], $user_id);
//Hard DB delete //Hard DB delete
if ($remove_content_from_db) { if ($remove_content_from_db) {
$sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = " . $document_id . " AND tool='" . TOOL_DOCUMENT . "'"; $sql = "DELETE FROM $TABLE_ITEMPROPERTY
WHERE c_id = {$course_info['real_id']} AND ref = ".$document_id." AND tool='".TOOL_DOCUMENT."'";
Database::query($sql); Database::query($sql);
$sql = "DELETE FROM " . $TABLE_DOCUMENT . " WHERE c_id = {$course_info['real_id']} AND id = " . $document_id; $sql = "DELETE FROM $TABLE_DOCUMENT WHERE c_id = {$course_info['real_id']} AND id = ".$document_id;
Database::query($sql); Database::query($sql);
self::delete_document_metadata($document_id); self::delete_document_metadata($document_id);
@ -900,11 +935,11 @@ class DocumentManager
* *
* @param array $_course * @param array $_course
* @param string $path, path stored in the database * @param string $path, path stored in the database
* @param string ,$base_work_dir, path to the documents folder * @param string $base_work_dir, path to the documents folder
* @return boolean true/false * @return boolean true/false
* @todo now only files/folders in a folder get visibility 2, we should rename them too. * @todo now only files/folders in a folder get visibility 2, we should rename them too.
*/ */
public static function delete_document($_course, $path, $base_work_dir) public static function delete_document($_course, $path, $base_work_dir, $sessionId = null)
{ {
$TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT); $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
@ -914,63 +949,89 @@ class DocumentManager
$course_id = $_course['real_id']; $course_id = $_course['real_id'];
//first, delete the actual document... if (empty($course_id)) {
$document_id = self :: get_document_id($_course, $path); return false;
}
if (empty($sessionId)) {
$sessionId = api_get_session_id();
} else {
$sessionId = intval($sessionId);
}
// First, delete the actual document.
$document_id = self::get_document_id($_course, $path, $sessionId);
if (empty($document_id)) {
return false;
}
$document_exists_in_disk = file_exists($base_work_dir.$path); $document_exists_in_disk = file_exists($base_work_dir.$path);
$new_path = $path.'_DELETED_'.$document_id; $new_path = $path.'_DELETED_'.$document_id;
$current_session_id = api_get_session_id();
$file_deleted_from_db = false; $file_deleted_from_db = false;
$file_deleted_from_disk = false; $file_deleted_from_disk = false;
$file_renamed_from_disk = false; $file_renamed_from_disk = false;
if ($document_id) { if ($document_id) {
self::delete_document_from_db($document_id); self::delete_document_from_db($document_id, $_course, $sessionId);
// Checking // Checking
// $file_exists_in_db = self::get_document_data_by_id($document_id, $_course['code']); // $file_exists_in_db = self::get_document_data_by_id($document_id, $_course['code']);
$file_deleted_from_db = true; $file_deleted_from_db = true;
} }
if ($document_exists_in_disk) { if ($document_exists_in_disk) {
if (api_get_setting('permanently_remove_deleted_files') == 'true') { if (api_get_setting('permanently_remove_deleted_files') == 'true') {
//Deleted files are *really* deleted
// Deleted files are *really* deleted.
$sql = "SELECT id FROM $TABLE_DOCUMENT $sql = "SELECT id FROM $TABLE_DOCUMENT
WHERE c_id = $course_id AND path='".$path."' OR path LIKE BINARY '".$path."/%'"; WHERE
// Get all id's of documents that are deleted c_id = $course_id AND
session_id = $sessionId AND
(path = '".$path."' OR path LIKE BINARY '".$path."/%') ";
// Get all id's of documents that are deleted.
$result = Database::query($sql); $result = Database::query($sql);
if ($result && Database::num_rows($result) != 0) { if ($result && Database::num_rows($result) != 0) {
// Delete all item_property entries // Delete all item_property entries
while ($row = Database::fetch_array($result)) { while ($row = Database::fetch_array($result)) {
// Query to delete from item_property table (hard way) // Query to delete from item_property table (hard way)
self::delete_document_from_db($row['id'], $_course, $current_session_id, true); self::delete_document_from_db($row['id'], $_course, $sessionId, true);
}
} }
// Delete documents, do it like this so metadata gets deleted too // Delete documents, do it like this so metadata gets deleted too
my_delete($base_work_dir.$path); my_delete($base_work_dir.$path);
$file_deleted_from_disk = true; $file_deleted_from_disk = true;
}
} else { } else {
// Set visibility to 2 and rename file/folder to xxx_DELETED_#id (soft delete) // Set visibility to 2 and rename file/folder to xxx_DELETED_#id (soft delete)
if (is_file($base_work_dir.$path) || is_dir($base_work_dir.$path)) { if (is_file($base_work_dir.$path) || is_dir($base_work_dir.$path)) {
if (rename($base_work_dir.$path, $base_work_dir.$new_path)) { if (rename($base_work_dir.$path, $base_work_dir.$new_path)) {
$sql = "UPDATE $TABLE_DOCUMENT set path='".$new_path."' WHERE c_id = $course_id AND id='".$document_id."'"; $sql = "UPDATE $TABLE_DOCUMENT SET path='".$new_path."'
WHERE c_id = $course_id AND session_id = $sessionId AND id = '".$document_id."'";
Database::query($sql); Database::query($sql);
$sql = "SELECT id, path FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '".$path."/%'"; $sql = "SELECT id, path FROM $TABLE_DOCUMENT
WHERE
c_id = $course_id AND
session_id = $sessionId AND
(path = '".$path."' OR path LIKE BINARY '".$path."/%') ";
$result = Database::query($sql); $result = Database::query($sql);
if ($result && Database::num_rows($result) > 0) { if ($result && Database::num_rows($result) > 0) {
while ($deleted_items = Database::fetch_array($result, 'ASSOC')) { while ($deleted_items = Database::fetch_array($result, 'ASSOC')) {
self::delete_document_from_db($deleted_items['id']); self::delete_document_from_db($deleted_items['id'], $_course, $sessionId);
// Change path of sub folders and documents in database. // Change path of sub folders and documents in database.
$old_item_path = $deleted_items['path']; $old_item_path = $deleted_items['path'];
$new_item_path = $new_path.substr($old_item_path, strlen($path)); $new_item_path = $new_path.substr($old_item_path, strlen($path));
$sql = "UPDATE $TABLE_DOCUMENT set path = '" . $new_item_path . "' WHERE c_id = $course_id AND id = " . $deleted_items['id']; $sql = "UPDATE $TABLE_DOCUMENT
SET path = '".$new_item_path."'
WHERE c_id = $course_id AND session_id = $sessionId AND id = ".$deleted_items['id'];
Database::query($sql); Database::query($sql);
} }
} }
@ -1032,17 +1093,24 @@ class DocumentManager
/** /**
* Gets the id of a document with a given path * Gets the id of a document with a given path
* *
* @param array $_course * @param array $courseInfo
* @param string $path * @param string $path
* @return int id of document / false if no doc found * @return int id of document / false if no doc found
*/ */
public static function get_document_id($course_info, $path) public static function get_document_id($courseInfo, $path, $sessionId = null)
{ {
$TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT); $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
$course_id = $course_info['real_id']; $course_id = $courseInfo['real_id'];
if (empty($sessionId)) {
$sessionId = api_get_session_id();
} else {
$sessionId = intval($sessionId);
}
$path = Database::escape_string($path); $path = Database::escape_string($path);
if (!empty($course_id) && !empty($path)) { if (!empty($course_id) && !empty($path)) {
$sql = "SELECT id FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '$path' LIMIT 1"; $sql = "SELECT id FROM $TABLE_DOCUMENT
WHERE c_id = $course_id AND path LIKE BINARY '$path' AND session_id = $sessionId LIMIT 1";
$result = Database::query($sql); $result = Database::query($sql);
if ($result && Database::num_rows($result)) { if ($result && Database::num_rows($result)) {
$row = Database::fetch_array($result); $row = Database::fetch_array($result);
@ -1060,7 +1128,7 @@ class DocumentManager
* @param bool load parents? * @param bool load parents?
* @return array document content * @return array document content
*/ */
public static function get_document_data_by_id($id, $course_code, $load_parents = false) public static function get_document_data_by_id($id, $course_code, $load_parents = false, $session_id = null)
{ {
$course_info = api_get_course_info($course_code); $course_info = api_get_course_info($course_code);
$course_id = $course_info['real_id']; $course_id = $course_info['real_id'];
@ -1068,11 +1136,18 @@ class DocumentManager
if (empty($course_info)) { if (empty($course_info)) {
return false; return false;
} }
if (empty($session_id)) {
$session_id = intval($session_id);
} else {
$session_id = api_get_session_id();
}
$www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document'; $www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document';
$TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT); $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
$id = intval($id); $id = intval($id);
$sql = "SELECT * FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id = $id "; $sql = "SELECT * FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND session_id = $session_id AND id = $id ";
$result = Database::query($sql); $result = Database::query($sql);
if ($result && Database::num_rows($result) == 1) { if ($result && Database::num_rows($result) == 1) {
$row = Database::fetch_array($result, 'ASSOC'); $row = Database::fetch_array($result, 'ASSOC');
@ -1093,15 +1168,13 @@ class DocumentManager
if (dirname($row['path']) == '.') { if (dirname($row['path']) == '.') {
$row['parent_id'] = '0'; $row['parent_id'] = '0';
} else { } else {
$row['parent_id'] = self::get_document_id($course_info, dirname($row['path'])); $row['parent_id'] = self::get_document_id($course_info, dirname($row['path']), $session_id);
} }
$parents = array(); $parents = array();
//Use to generate parents (needed for the breadcrumb) //Use to generate parents (needed for the breadcrumb)
//@todo sorry but this for is here because there's not a parent_id in the document table so we parsed the path!! //@todo sorry but this for is here because there's not a parent_id in the document table so we parsed the path!!
$visibility = true;
if ($load_parents) { if ($load_parents) {
$dir_array = explode('/', $row['path']); $dir_array = explode('/', $row['path']);
$dir_array = array_filter($dir_array); $dir_array = array_filter($dir_array);
@ -1113,13 +1186,12 @@ class DocumentManager
$real_dir .= '/' . $dir_array[$i]; $real_dir .= '/' . $dir_array[$i];
$parent_id = self::get_document_id($course_info, $real_dir); $parent_id = self::get_document_id($course_info, $real_dir);
if (!empty($parent_id)) { if (!empty($parent_id)) {
$sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false); $sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false, $session_id);
//@todo add visibility here //@todo add visibility here
$parents[] = $sub_document_data; $parents[] = $sub_document_data;
} }
} }
} }
//$row['visibility_for_user'] = $visibility;
$row['parents'] = $parents; $row['parents'] = $parents;
return $row; return $row;
} }
@ -1523,8 +1595,12 @@ class DocumentManager
* @param int The document id of the default certificate * @param int The document id of the default certificate
* @return void() * @return void()
*/ */
function remove_attach_certificate($course_id, $default_certificate_id) public static function remove_attach_certificate($course_id, $default_certificate_id)
{ {
if (empty($default_certificate_id)) {
return false;
}
$default_certificate = self::get_default_certificate_id($course_id); $default_certificate = self::get_default_certificate_id($course_id);
if ((int) $default_certificate == (int) $default_certificate_id) { if ((int) $default_certificate == (int) $default_certificate_id) {
$tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
@ -1548,7 +1624,7 @@ class DocumentManager
* @param string The course id * @param string The course id
* @return void() * @return void()
*/ */
static function create_directory_certificate_in_course($course_id) public static function create_directory_certificate_in_course($course_id)
{ {
$course_info = api_get_course_info($course_id); $course_info = api_get_course_info($course_id);
if (!empty($course_info)) { if (!empty($course_info)) {
@ -1575,7 +1651,7 @@ class DocumentManager
* @param string The course id * @param string The course id
* @return int The document id of the directory certificate * @return int The document id of the directory certificate
*/ */
static function get_document_id_of_directory_certificate() public static function get_document_id_of_directory_certificate()
{ {
$tbl_document = Database::get_course_table(TABLE_DOCUMENT); $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
@ -1590,7 +1666,7 @@ class DocumentManager
* @param string path of directory * @param string path of directory
* @return bool true if is a certificate or false otherwise * @return bool true if is a certificate or false otherwise
*/ */
static function is_certificate_mode($dir) public static function is_certificate_mode($dir)
{ {
// I'm in the certification module? // I'm in the certification module?
$is_certificate_mode = false; $is_certificate_mode = false;
@ -1615,7 +1691,7 @@ class DocumentManager
* @return array List of file paths. An additional field containing 'local' or 'remote' helps determine * @return array List of file paths. An additional field containing 'local' or 'remote' helps determine
* if the file should be copied into the zip or just linked * if the file should be copied into the zip or just linked
*/ */
static function get_resources_from_source_html($source_html, $is_file = false, $type = null, $recursivity = 1) public static function get_resources_from_source_html($source_html, $is_file = false, $type = null, $recursivity = 1)
{ {
$max = 5; $max = 5;
$attributes = array(); $attributes = array();
@ -1900,7 +1976,7 @@ class DocumentManager
* @return array An associative array of attributes * @return array An associative array of attributes
* @author Based on a function from the HTML_Common2 PEAR module * * @author Based on a function from the HTML_Common2 PEAR module *
*/ */
static function parse_HTML_attributes($attrString, $wanted = array(), $explode_variables = array()) public static function parse_HTML_attributes($attrString, $wanted = array(), $explode_variables = array())
{ {
$attributes = array(); $attributes = array();
$regs = array(); $regs = array();

Loading…
Cancel
Save