Skip to content

Commit

Permalink
Add OnlyofficeTools::getPathToView() to enable viewer in the learning…
Browse files Browse the repository at this point in the history
… path - fixes #41
  • Loading branch information
ywarnier committed Oct 18, 2024
1 parent a0c4eb6 commit e0e9598
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
17 changes: 15 additions & 2 deletions editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@
$config = json_decode(json_encode($config), true);
$isMobileAgent = $configService->isMobileAgent($_SERVER['HTTP_USER_AGENT']);

$showHeaders = true;
$headerHeight = 'calc(100% - 140px)';
if (!empty($_GET['nh'])) {
$showHeaders = false;
$headerHeight = '100%';
}

?>
<title>ONLYOFFICE</title>
<style>
#app > iframe {
height: calc(100% - 140px);
height: <?php echo $headerHeight; ?>;
}
body {
height: 100%;
Expand Down Expand Up @@ -172,4 +179,10 @@
}

</script>
<?php echo Display::display_header(); ?>
<?php
if ($showHeaders) {
echo Display::display_header();
} else {
echo Display::display_reduced_header();
}
?>
60 changes: 60 additions & 0 deletions lib/onlyofficeTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,64 @@ public static function getButtonCreateNew(): string
$urlToCreate
);
}

/**
* Return path to OnlyOffice viewer for a given file.
* @param int $documentId The ID from c_document.iid
* @param bool $showHeaders Whether to show Chamilo headers on top of the OnlyOffice frame or not
*
* @return string A link to open the OnlyOffice viewer
*/
public static function getPathToView(int $documentId, bool $showHeaders = true): string
{
$plugin = OnlyofficePlugin::create();
$appSettings = new OnlyofficeAppsettings($plugin);
$documentManager = new OnlyofficeDocumentManager($appSettings, []);

$isEnable = 'true' === $plugin->get('enable_onlyoffice_plugin');
if (!$isEnable) {
return '';
}

$urlToEdit = api_get_path(WEB_PLUGIN_PATH).'onlyoffice/editor.php';

$sessionId = api_get_session_id();
$courseInfo = api_get_course_info();
$userId = api_get_user_id();

$docInfo = DocumentManager::get_document_data_by_id($documentId, $courseInfo['code'], false, $sessionId);

$extension = strtolower(pathinfo($docInfo['path'], PATHINFO_EXTENSION));
$canView = null !== $documentManager->getFormatInfo($extension) ? $documentManager->getFormatInfo($extension)->isViewable() : false;

$isGroupAccess = false;
$groupId = api_get_group_id();
if (!empty($groupId)) {
$groupProperties = GroupManager::get_group_properties($groupId);
$docInfoGroup = api_get_item_property_info(api_get_course_int_id(), 'document', $documentId, $sessionId);
$isGroupAccess = GroupManager::allowUploadEditDocument($userId, $courseInfo['code'], $groupProperties, $docInfoGroup);

$urlToEdit = $urlToEdit.'?groupId='.$groupId.'&';
} else {
$urlToEdit = $urlToEdit.'?';
}
error_log(__LINE__.' '.$urlToEdit);

$isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo['absolute_parent_path'], $sessionId);

$accessRights = $isMyDir || $isGroupAccess;

$urlToEdit = $urlToEdit.'docId='.$documentId;
if (false === $showHeaders) {
$urlToEdit .= '&nh=1';
}

if ($canView && !$accessRights) {
error_log(__LINE__.' '.$urlToEdit);

return $urlToEdit;
}

return '';
}
}

0 comments on commit e0e9598

Please sign in to comment.