Browse Source

Fix document creation

Julio Montoya 6 years ago
parent
commit
e541846c12

+ 20 - 39
main/document/create_audio.php

@@ -21,6 +21,8 @@ $nameTools = get_lang('CreateAudio');
 api_protect_course_script();
 api_block_anonymous_users();
 
+$courseInfo = api_get_course_info();
+
 $groupRights = Session::read('group_member_with_upload_rights');
 $groupId = api_get_group_id();
 
@@ -74,18 +76,11 @@ if ($dir[strlen($dir) - 1] != '/') {
     $dir .= '/';
 }
 
-$filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
-
-if (!is_dir($filepath)) {
-    $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
-    $dir = '/';
-}
-
 //groups //TODO: clean
 if (!empty($groupId)) {
     $interbreadcrumb[] = [
-        "url" => "../group/group_space.php?".api_get_cidreq(),
-        "name" => get_lang('GroupSpace'),
+        'url' => '../group/group_space.php?'.api_get_cidreq(),
+        'name' => get_lang('GroupSpace'),
     ];
     $group = GroupManager:: get_group_properties($groupId);
     $path = explode('/', $dir);
@@ -95,8 +90,8 @@ if (!empty($groupId)) {
 }
 
 $interbreadcrumb[] = [
-    "url" => "./document.php?curdirpath=".urlencode($dir)."&".api_get_cidreq(),
-    "name" => get_lang('Documents'),
+    'url' => "./document.php?curdirpath=".urlencode($dir)."&".api_get_cidreq(),
+    'name' => get_lang('Documents'),
 ];
 
 if (!api_is_allowed_in_course()) {
@@ -113,7 +108,6 @@ if (!($is_allowed_to_edit || $groupRights ||
     api_not_allowed(true);
 }
 
-/*	Header */
 Event::event_access_tool(TOOL_DOCUMENT);
 
 $display_dir = $dir;
@@ -140,7 +134,7 @@ for ($i = 0; $i < $array_len; $i++) {
 $service = isset($_GET['service']) ? $_GET['service'] : 'google';
 
 if (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'google') {
-    downloadAudioGoogle($filepath, $dir);
+    downloadAudioGoogle($dir);
 }
 
 Display:: display_header($nameTools, 'Doc');
@@ -249,13 +243,13 @@ Display::display_footer();
  *
  * @version january 2011, chamilo 1.8.8
  */
-function downloadAudioGoogle($filepath, $dir)
+function downloadAudioGoogle($dir)
 {
     $location = 'create_audio.php?'.api_get_cidreq().'&id='.intval($_POST['id']).'&service=google';
 
     //security
     if (!isset($_POST['lang']) && !isset($_POST['text']) &&
-        !isset($_POST['title']) && !isset($filepath) && !isset($dir)
+        !isset($_POST['title']) && !isset($dir)
     ) {
         echo '<script>window.location.href="'.$location.'"</script>';
 
@@ -263,8 +257,6 @@ function downloadAudioGoogle($filepath, $dir)
     }
 
     $_course = api_get_course_info();
-    $_user = api_get_user_info();
-
     $clean_title = trim($_POST['title']);
     $clean_text = trim($_POST['text']);
     if (empty($clean_title) || empty($clean_text)) {
@@ -282,24 +274,10 @@ function downloadAudioGoogle($filepath, $dir)
     $extension = 'mp3';
     $audio_filename = $clean_title.'.'.$extension;
     $audio_title = str_replace('_', ' ', $clean_title);
-
-    //prevent duplicates
-    if (file_exists($filepath.'/'.$clean_title.'.'.$extension)) {
-        $i = 1;
-        while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) {
-            $i++;
-        }
-        $audio_filename = $clean_title.'_'.$i.'.'.$extension;
-        $audio_title = $clean_title.'_'.$i.'.'.$extension;
-        $audio_title = str_replace('_', ' ', $audio_title);
-    }
-
-    $documentPath = $filepath.'/'.$audio_filename;
     $clean_text = api_replace_dangerous_char($clean_text);
 
     // adding the file
     // add new file to disk
-
     $proxySettings = api_get_configuration_value('proxy_settings');
     $key = api_get_configuration_value('translate_app_google_key');
     $url = "https://www.googleapis.com/language/translate/v2?key=$key&".$clean_lang."&target=$clean_lang&q=".urlencode($clean_text)."";
@@ -320,20 +298,23 @@ function downloadAudioGoogle($filepath, $dir)
 
         return;
     }
-
-    file_put_contents(
-        $documentPath,
-        $content
-    );
-
+    
     // add document to database
     $relativeUrlPath = $dir;
     add_document(
         $_course,
         $relativeUrlPath.$audio_filename,
         'file',
-        filesize($documentPath),
-        $audio_title
+        '',
+        $audio_title,
+        null,
+        0,
+        null,
+        0,
+        0,
+        0,
+        true,
+        $content
     );
     echo Display::return_message(get_lang('DocumentCreated'), 'confirm');
     echo '<script>window.location.href="'.$location.'"</script>';

+ 1 - 1
main/document/document.php

@@ -1393,7 +1393,7 @@ if ($isAllowedToEdit ||
 
             if (!empty($newFolderData)) {
                 $message = Display::return_message(
-                    get_lang('DirCr').' '.$newFolderData['title'],
+                    get_lang('DirCr').' '.$newFolderData->getTitle(),
                     'confirmation'
                 );
             } else {

+ 56 - 51
main/document/document_quota.php

@@ -14,9 +14,7 @@ if (!api_is_allowed_to_edit(null, true)) {
 
 $current_course_tool = TOOL_DOCUMENT;
 $this_section = SECTION_COURSES;
-
 $tool_name = get_lang('DocumentQuota');
-
 $interbreadcrumb[] = ['url' => 'document.php', 'name' => get_lang('Documents')];
 
 $htmlHeadXtra[] = api_get_js('jqplot/jquery.jqplot.js');
@@ -24,40 +22,38 @@ $htmlHeadXtra[] = api_get_js('jqplot/plugins/jqplot.pieRenderer.js');
 $htmlHeadXtra[] = api_get_js('jqplot/plugins/jqplot.donutRenderer.js');
 $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/jqplot/jquery.jqplot.css');
 
-$course_code = api_get_course_id();
-$course_id = api_get_course_int_id();
-$session_id = api_get_session_id();
-$group_id = api_get_group_id();
-$user_id = api_get_user_id();
-$user_info = api_get_user_info($user_id);
-
+$courseId = api_get_course_int_id();
+$sessionId = api_get_session_id();
+$groupId = api_get_group_id();
+$userId = api_get_user_id();
+$userInfo = api_get_user_info($userId);
 $session = [];
-$user_name = $user_info['complete_name'];
+$user_name = $userInfo['complete_name'];
 
-$course_list = SessionManager::get_course_list_by_session_id($session_id);
-$session_list = SessionManager::get_session_by_course($course_id);
+$course_list = SessionManager::get_course_list_by_session_id($sessionId);
+$session_list = SessionManager::get_session_by_course($courseId);
 $total_quota_bytes = DocumentManager::get_course_quota();
-$quota_bytes = DocumentManager::documents_total_space($course_id, 0, 0);
-$quota_percentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
+$quota_bytes = DocumentManager::documents_total_space($courseId, 0, 0);
+$quotaPercentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
 
-$session[] = [get_lang('Course').' ('.format_file_size($quota_bytes).')', $quota_percentage];
+$session[] = [get_lang('Course').' ('.format_file_size($quota_bytes).')', $quotaPercentage];
 
 $used_quota_bytes = $quota_bytes;
 
 if (!empty($session_list)) {
     foreach ($session_list as $session_data) {
-        $quota_percentage = 0;
-        $quota_bytes = DocumentManager::documents_total_space($course_id, null, $session_data['id']);
+        $quotaPercentage = 0;
+        $quota_bytes = DocumentManager::documents_total_space($courseId, null, $session_data['id']);
         if (!empty($quota_bytes)) {
-            $quota_percentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
+            $quotaPercentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
         }
-        if ($session_id == $session_data['id']) {
+        if ($sessionId == $session_data['id']) {
             $session_data['name'] = $session_data['name'].' * ';
         }
         $used_quota_bytes += $quota_bytes;
         $session[] = [
             addslashes(get_lang('Session').': '.$session_data['name']).' ('.format_file_size($quota_bytes).')',
-            $quota_percentage,
+            $quotaPercentage,
         ];
     }
 }
@@ -65,34 +61,40 @@ $group_list = GroupManager::get_groups();
 
 if (!empty($group_list)) {
     foreach ($group_list as $group_data) {
-        $quota_percentage = 0;
+        $quotaPercentage = 0;
         $my_group_id = $group_data['id'];
-        $quota_bytes = DocumentManager::documents_total_space($course_id, $my_group_id, 0);
+        $quota_bytes = DocumentManager::documents_total_space($courseId, $my_group_id, 0);
         if (!empty($quota_bytes)) {
-            $quota_percentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
+            $quotaPercentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
         }
-        if ($group_id == $my_group_id) {
+        if ($groupId == $my_group_id) {
             $group_data['name'] = $group_data['name'].' * ';
         }
         $used_quota_bytes += $quota_bytes;
-        $session[] = [addslashes(get_lang('Group').': '.$group_data['name']).' ('.format_file_size($quota_bytes).')', $quota_percentage];
+        $session[] = [
+            addslashes(get_lang('Group').': '.$group_data['name']).' ('.format_file_size($quota_bytes).')',
+            $quotaPercentage,
+        ];
     }
 }
 // Showing weight of documents uploaded by user
-$document_list = DocumentManager::getAllDocumentData($_course);
+$document_list = DocumentManager::getAllDocumentData(api_get_course_info());
 if (!empty($document_list)) {
     foreach ($document_list as $document_data) {
-        if ($document_data['insert_user_id'] == api_get_user_id() && $document_data['filetype'] == 'file') {
+        if ($document_data['creator_id'] == api_get_user_id() && $document_data['filetype'] === 'file') {
             $quota_bytes += $document_data['size'];
         }
     }
     if ($quota_bytes != 0) {
-        $quota_percentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
+        $quotaPercentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
     }
 
-    $session[] = [addslashes(get_lang('Teacher').': '.$user_name).' ('.format_file_size($quota_bytes).')', $quota_percentage];
+    $session[] = [
+        addslashes(get_lang('Teacher').': '.$user_name).' ('.format_file_size($quota_bytes).')',
+        $quotaPercentage,
+    ];
     //if a sesson is active
-    if ($session_id != 0) {
+    if ($sessionId != 0) {
         if (!empty($course_list)) {
             $total_courses_quota = 0;
             $total_quota_bytes = 0;
@@ -102,34 +104,37 @@ if (!empty($document_list)) {
                 }
             }
             if ($quota_bytes != 0) {
-                $quota_percentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
+                $quotaPercentage = round($quota_bytes / $total_quota_bytes, 2) * 100;
             }
         }
-        $session[] = [addslashes(sprintf(get_lang('TeacherXInSession'), $user_name)), $quota_percentage];
+        $session[] = [addslashes(sprintf(get_lang('TeacherXInSession'), $user_name)), $quotaPercentage];
     }
 }
 
-$quota_percentage = round(($total_quota_bytes - $used_quota_bytes) / $total_quota_bytes, 2) * 100;
-$session[] = [addslashes(get_lang('ShowCourseQuotaUse')).' ('.format_file_size($total_quota_bytes - $used_quota_bytes).') ', $quota_percentage];
+$quotaPercentage = round(($total_quota_bytes - $used_quota_bytes) / $total_quota_bytes, 2) * 100;
+$session[] = [
+    addslashes(get_lang('ShowCourseQuotaUse')).' ('.format_file_size(
+        $total_quota_bytes - $used_quota_bytes
+    ).') ',
+    $quotaPercentage,
+];
 $quota_data = json_encode($session);
 
-$htmlHeadXtra[] = "
-<script>
-$(document).ready(function(){
-  var data = ".$quota_data.";
-  var plot1 = jQuery.jqplot ('chart1', [data], {
-      seriesDefaults: {
-        // Make this a pie chart
-        renderer: jQuery.jqplot.PieRenderer,
-        rendererOptions: {
-          // Put data labels on the pie slices.
-          // By default, labels show the percentage of the slice.
-          showDataLabels: true
-        }
-      },
-      legend: { show:true, location: 'e' }
-    }
-  );
+$htmlHeadXtra[] = "<script>
+$(document).ready(function() {
+    var data = ".$quota_data.";
+    var plot1 = jQuery.jqplot('chart1', [data], {
+        seriesDefaults: {
+            // Make this a pie chart
+            renderer: jQuery.jqplot.PieRenderer,
+            rendererOptions: {
+                // Put data labels on the pie slices.
+                // By default, labels show the percentage of the slice.
+                showDataLabels: true
+            }
+        },
+        legend: { show:true, location: 'e' }
+    });
 });
 </script>";
 

+ 9 - 8
main/document/edit_document.php

@@ -193,7 +193,7 @@ if (!api_is_allowed_to_edit()) {
     }
 }
 
-$document_info = api_get_item_property_info(
+/*$document_info = api_get_item_property_info(
     api_get_course_int_id(),
     'document',
     $document_id,
@@ -219,7 +219,7 @@ if (api_is_in_group()) {
         $document_info,
         true
     );
-}
+}*/
 
 /* MAIN TOOL CODE */
 /* Code to change the comment */
@@ -325,10 +325,11 @@ $path_info = pathinfo($document_data['path']);
 $filename = $path_info['filename'];
 $extension = $path_info['extension'] ?? '';
 
+$em = Database::getManager();
+/** @var \Chamilo\CoreBundle\Entity\Resource\ResourceNode $node */
+$node = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($document_data['resource_node_id']);
+
 if (in_array($extension, ['html', 'htm'])) {
-    $em = Database::getManager();
-    /** @var \Chamilo\CoreBundle\Entity\Resource\ResourceNode $node */
-    $node = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($document_data['resource_node_id']);
     $file = $node->getResourceFile();
 
     if ($file) {
@@ -343,9 +344,9 @@ if (in_array($extension, ['html', 'htm'])) {
 $nameTools = get_lang('EditDocument').': '.Security::remove_XSS($document_data['title']);
 Display::display_header($nameTools, 'Doc');
 
-$owner_id = $document_info['insert_user_id'];
-$last_edit_date = $document_info['lastedit_date'];
-$createdDate = $document_info['insert_date'];
+$owner_id = $node->getCreator()->getId();
+$last_edit_date = $node->getUpdatedAt();
+$createdDate = $node->getCreatedAt();
 $groupInfo = GroupManager::get_group_properties(api_get_group_id());
 
 if ($owner_id == api_get_user_id() ||

+ 2 - 3
main/document/record_audio.php

@@ -68,12 +68,11 @@ if ($dir[strlen($dir) - 1] != '/') {
     $dir .= '/';
 }
 
-$filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
-
+/*$filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
 if (!is_dir($filepath)) {
     $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
     $dir = '/';
-}
+}*/
 
 //groups //TODO: clean
 if (!empty($groupId)) {

+ 2 - 1
main/inc/lib/document.lib.php

@@ -522,7 +522,8 @@ class DocumentManager
                     creator_id,
                     visibility,
                     n.updated_at,
-                    n.created_at                                     
+                    n.created_at,
+                    n.creator_id                                     
                 FROM resource_node AS n
                 INNER JOIN $tblDocument AS docs
                 ON (docs.resource_node_id = n.id)

+ 5 - 5
main/inc/lib/fileUpload.lib.php

@@ -1708,8 +1708,8 @@ function search_img_from_html($html_file)
  * @param bool   $generateNewNameIfExists
  * @param bool   $sendNotification        depends in conf setting "send_notification_when_document_added"
  *
- * @return string actual directory name if it succeeds,
- *                boolean false otherwise
+ * @return CDocument|false
+ *
  */
 function create_unexisting_directory(
     $_course,
@@ -1857,10 +1857,10 @@ function create_unexisting_directory(
                     $session_id
                 );
 
-                /* This means the folder NOT exist in the filesystem
-                 (now this was created) but there is a record in the Database*/
+                $em = Database::getManager();
+                $document = $em->getRepository('ChamiloCourseBundle:CDocument')->find($documentData['iid']);
 
-                return $documentData;
+                return $document;
             }
         }
     }