Document - Add option to define if a document can be downloaded or not - refs BT#20418

pull/4482/head
Christian 3 years ago
parent 86d7590c25
commit 2a50f60422
  1. 18
      main/document/edit_document.php
  2. 67
      main/inc/lib/document.lib.php
  3. 3
      main/inc/lib/extra_field.lib.php
  4. 8
      main/install/configuration.dist.php
  5. 1
      src/Chamilo/CoreBundle/Entity/ExtraField.php

@ -334,6 +334,14 @@ if ($is_allowed_to_edit) {
}
}
if (true === api_get_configuration_value('documents_hide_download_icon')) {
$extraFieldValue = new ExtraFieldValue('document');
$fieldValues = [];
$fieldValues['item_id'] = $document_id;
$fieldValues['extra_can_be_downloaded'] = (int) $_REQUEST['download']['can_download'];
$extraFieldValue->saveFieldValues($fieldValues);
}
$url = 'document.php?id='.$document_data['parent_id'].'&'.api_get_cidreq().($is_certificate_mode ? '&curdirpath=/certificates&selectcat=1' : '');
$redirectToEditPage = isset($_POST['button_ck']) && 1 === (int) $_POST['button_ck'];
@ -459,6 +467,16 @@ if ($owner_id == api_get_user_id() ||
$form->addElement('textarea', 'comment', get_lang('Comment'), ['cols-size' => [2, 10, 0]]);
}
// Define if a document can be downloaded or not.
if (true === api_get_configuration_value('documents_hide_download_icon')) {
$itemId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$group = [];
$group[] = $form->createElement('radio', 'can_download', null, get_lang('Yes'), 1, ['id' => 'can_download_yes']);
$group[] = $form->createElement('radio', 'can_download', null, get_lang('No'), 0, ['id' => 'can_download_no']);
$form->addGroup($group, 'download', get_lang('DefineIfDocumentCanBeDownloaded'));
$defaults['download']['can_download'] = DocumentManager::getHideDownloadIcon($itemId);
}
if ($file_type != 'link') {
if ($owner_id == api_get_user_id() || api_is_platform_admin()) {
$checked = &$form->addElement('checkbox', 'readonly', null, get_lang('ReadOnly'));

@ -5328,31 +5328,41 @@ class DocumentManager
$document_data['file_extension'] = $extension;
if (!$show_as_icon) {
if ($filetype == 'folder') {
if ($isAllowedToEdit ||
api_is_platform_admin() ||
api_get_setting('students_download_folders') == 'true'
) {
// filter: when I am into a shared folder, I can only show "my shared folder" for donwload
if (self::is_shared_folder($curdirpath, $sessionId)) {
if (preg_match('/shared_folder\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) ||
preg_match('/shared_folder_session_'.$sessionId.'\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) ||
$isAllowedToEdit || api_is_platform_admin()
// to force download if a document can be downloaded or not
$hideDownloadIcon = false;
if (true === api_get_configuration_value('documents_hide_download_icon')) {
$hideDownloadIcon = true;
}
if (self::getHideDownloadIcon($document_data['id'])) {
$hideDownloadIcon = false;
}
if (!$hideDownloadIcon) {
if ($filetype == 'folder') {
if ($isAllowedToEdit ||
api_is_platform_admin() ||
api_get_setting('students_download_folders') == 'true'
) {
// filter: when I am into a shared folder, I can only show "my shared folder" for donwload
if (self::is_shared_folder($curdirpath, $sessionId)) {
if (preg_match('/shared_folder\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) ||
preg_match('/shared_folder_session_'.$sessionId.'\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) ||
$isAllowedToEdit || api_is_platform_admin()
) {
$force_download_html = $size == 0 ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.
Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
}
} elseif (!preg_match('/shared_folder/', urldecode($forcedownload_link)) ||
$isAllowedToEdit ||
api_is_platform_admin()
) {
$force_download_html = $size == 0 ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.
Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
}
} elseif (!preg_match('/shared_folder/', urldecode($forcedownload_link)) ||
$isAllowedToEdit ||
api_is_platform_admin()
) {
$force_download_html = $size == 0 ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.
Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
}
} else {
$force_download_html = $size == 0 ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.' download="'.$document_data['basename'].'">'.
Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
}
} else {
$force_download_html = $size == 0 ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.' download="'.$document_data['basename'].'">'.
Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
}
// Copy files to user's myfiles
@ -7461,4 +7471,23 @@ class DocumentManager
return $btn;
}
/**
* It gest extra value to define if download icon is visible or not.
*
* @param $documentId
*
* @return bool
*/
public static function getHideDownloadIcon($documentId)
{
$extraFieldValue = new ExtraFieldValue('document');
$extraValue = $extraFieldValue->get_values_by_handler_and_field_variable($documentId, 'can_be_downloaded');
$canBeDownloadedIcon = false;
if (!empty($extraValue) && isset($extraValue['value'])) {
$canBeDownloadedIcon = (bool) $extraValue['value'];
}
return $canBeDownloadedIcon;
}
}

@ -171,6 +171,9 @@ class ExtraField extends Model
case 'message':
$this->extraFieldType = EntityExtraField::MESSAGE_TYPE;
break;
case 'document':
$this->extraFieldType = EntityExtraField::DOCUMENT_TYPE;
break;
}
$this->pageUrl = 'extra_fields.php?type='.$this->type;

@ -2317,6 +2317,14 @@ INSERT INTO `extra_field` (`extra_field_type`, `field_type`, `variable`, `displa
// Disable links in gradebook view for students
// $_configuration['gradebook_hide_link_to_item_for_student'] = false;
// It adds option to define if a document can be downloaded or not.
// Requires DB changes:
/*
INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, default_value, field_order, visible_to_self, visible_to_others, changeable, filter, created_at) VALUES
(23, 1, 'can_be_downloaded', 'CanBeDownloaded', '', 0, 1, 0, 1, 0, NOW());
*/
// $_configuration['documents_hide_download_icon'] = false;
// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email

@ -41,6 +41,7 @@ class ExtraField extends BaseAttribute
public const LP_VIEW_TYPE = 20;
public const COURSE_ANNOUNCEMENT = 21;
public const MESSAGE_TYPE = 22;
public const DOCUMENT_TYPE = 23;
/**
* @var int

Loading…
Cancel
Save