Browse Source

Add option to update scorm files only see BT#10885

jmontoyaa 7 years ago
parent
commit
c209282cc4
4 changed files with 195 additions and 96 deletions
  1. 11 1
      main/lp/lp_list.php
  2. 78 0
      main/lp/lp_update_scorm.php
  3. 0 80
      main/lp/lp_upload.php
  4. 106 15
      main/lp/scorm.class.php

+ 11 - 1
main/lp/lp_list.php

@@ -385,6 +385,7 @@ foreach ($categories as $item) {
             $copy = null;
             $lp_auto_launch_icon = null;
             $actionSeriousGame = null;
+            $actionUpdateScormFile = '';
 
             if ($is_allowed_to_edit) {
                 // EDIT LP
@@ -618,7 +619,7 @@ foreach ($categories as $item) {
                 }
 
                 /*  DEBUG  */
-                if ($test_mode == 'test' or api_is_platform_admin()) {
+                if ($test_mode == 'test' || api_is_platform_admin()) {
                     if ($details['lp_scorm_debug'] == 1) {
                         $dsp_debug = Display::url(
                             Display::return_icon(
@@ -769,6 +770,14 @@ foreach ($categories as $item) {
                     }
                 }
 
+                if ($details['lp_type'] == 2) {
+                    $url = api_get_path(WEB_CODE_PATH)."lp/lp_update_scorm.php?".api_get_cidreq()."&lp_id=$id";
+                    $actionUpdateScormFile = Display::url(
+                        Display::return_icon('upload_file.png', get_lang('Update')),
+                        $url
+                    );
+                }
+
                 if ($is_allowed_to_edit) {
                     $start_time = $start_time;
                     $end_time = $end_time;
@@ -852,6 +861,7 @@ foreach ($categories as $item) {
                 'action_order' => $dsp_order,
                 'action_serious_game' => $actionSeriousGame,
                 'action_subscribe_users' => $subscribeUsers,
+                'action_update_scorm' => $actionUpdateScormFile
             ];
 
             $lpIsShown = true;

+ 78 - 0
main/lp/lp_update_scorm.php

@@ -0,0 +1,78 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * This is a learning path creation and player tool in Chamilo - previously
+ * @author Julio Montoya  - Improving the list of templates
+ * @package chamilo.learnpath
+ */
+
+$this_section = SECTION_COURSES;
+
+require_once __DIR__.'/../inc/global.inc.php';
+
+api_protect_course_script();
+$allow = api_is_allowed_to_edit(null, true);
+$lpId = !empty($_GET['lp_id']) ? intval($_GET['lp_id']) : 0;
+
+if (!$allow || empty($lpId)) {
+    api_not_allowed(true);
+}
+
+$lp = new learnpath(api_get_course_id(), $lpId, api_get_user_id());
+
+if (api_is_in_gradebook()) {
+    $interbreadcrumb[] = [
+        'url' => Category::getUrl(),
+        'name' => get_lang('ToolGradebook')
+    ];
+}
+
+$interbreadcrumb[] = [
+    'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
+    'name' => get_lang('LearningPaths')
+];
+$interbreadcrumb[] = [
+    'url' => api_get_self()."?action=build&lp_id=$lpId&".api_get_cidreq(),
+    'name' => $lp->get_name()
+];
+
+$form = new FormValidator(
+    '',
+    'POST',
+    api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId,
+    '',
+    [
+        'id' => "upload_form",
+        'enctype' => "multipart/form-data"
+    ]
+);
+$form->addHeader(get_lang('UpdateFile'));
+$form->addHtml(Display::return_message(get_lang('TheScormPackageWillBeUpdatedYouMustUploadTheFileWithTheSameName')));
+$form->addLabel(null, Display::return_icon('scorm_logo.jpg', null, ['style' => 'width:230px;height:100px']));
+$form->addElement('hidden', 'curdirpath', '');
+$form->addElement('file', 'user_file', get_lang('FileToUpload'));
+$form->addRule('user_file', get_lang('ThisFieldIsRequired'), 'required');
+$form->addButtonUpload(get_lang('Upload'));
+
+if ($form->validate()) {
+    $oScorm = new scorm();
+    $manifest = $oScorm->import_package(
+        $_FILES['user_file'],
+        '',
+        api_get_course_info(),
+        true,
+        $lp
+    );
+    if ($manifest) {
+        Display::addFlash(Display::return_message(get_lang('Updated')));
+    }
+    header('Location: '.api_get_path(WEB_CODE_PATH).'lp/lp_list.php?'.api_get_cidreq());
+    exit;
+}
+
+$content = $form->returnForm();
+
+$tpl = new Template(null);
+$tpl->assign('content', $content);
+$tpl->display_one_col_template();

+ 0 - 80
main/lp/lp_upload.php

@@ -65,86 +65,6 @@ if (isset($_POST) && $is_error) {
             $manifest = $oScorm->import_package($_FILES['user_file'], $current_dir);
             if (!empty($manifest)) {
                 $oScorm->parse_manifest($manifest);
-                $fixTemplate = api_get_configuration_value('learnpath_fix_xerte_template');
-                $proxyPath = api_get_configuration_value('learnpath_proxy_url');
-                if ($fixTemplate && !empty($proxyPath)) {
-                    // Check organisations:
-                    if (isset($oScorm->manifest['organizations'])) {
-                        foreach ($oScorm->manifest['organizations'] as $data) {
-                            if (strpos(strtolower($data), 'xerte') !== false) {
-                                // Check if template.xml exists:
-                                $templatePath = str_replace('imsmanifest.xml', 'template.xml', $manifest);
-                                if (file_exists($templatePath) && is_file($templatePath)) {
-                                    $templateContent = file_get_contents($templatePath);
-
-                                    $find = [
-                                        'href="www.',
-                                        'href="https://',
-                                        'href="http://',
-                                        'url="www.',
-                                        'pdfs/download.php?'
-                                    ];
-
-                                    $replace = [
-                                        'href="http://www.',
-                                        'target = "_blank" href="'.$proxyPath.'?type=link&src=https://',
-                                        'target = "_blank" href="'.$proxyPath.'?type=link&src=http://',
-                                        'url="http://www.',
-                                        'pdfs/download.php&'
-                                    ];
-                                    $templateContent = str_replace($find, $replace, $templateContent);
-                                    file_put_contents($templatePath, $templateContent);
-                                }
-
-                                // Fix link generation:
-                                $linkPath = str_replace('imsmanifest.xml', 'models_html5/links.html', $manifest);
-
-                                if (file_exists($linkPath) && is_file($linkPath)) {
-                                    $linkContent = file_get_contents($linkPath);
-                                    $find = [
-                                        ':this.getAttribute("url")'
-                                    ];
-                                    $replace = [
-                                        ':"'.$proxyPath.'?type=link&src=" + this.getAttribute("url")'
-                                    ];
-                                    $linkContent = str_replace($find, $replace, $linkContent);
-                                    file_put_contents($linkPath, $linkContent);
-                                }
-
-                                // Fix iframe generation
-                                $framePath = str_replace('imsmanifest.xml', 'models_html5/embedDiv.html', $manifest);
-
-                                if (file_exists($framePath) && is_file($framePath)) {
-                                    $content = file_get_contents($framePath);
-                                    $find = [
-                                        '$iFrameHolder.html(iFrameTag);'
-                                    ];
-                                    $replace = [
-                                        'iFrameTag = \'<a target ="_blank" href="'.$proxyPath.'?type=link&src=\'+ pageSrc + \'">Open website. <img src="'.api_get_path(WEB_CODE_PATH).'img/link-external.png"></a>\'; $iFrameHolder.html(iFrameTag); '
-                                    ];
-                                    $content = str_replace($find, $replace, $content);
-                                    file_put_contents($framePath, $content);
-                                }
-
-                                // Fix new window generation
-                                $newWindowPath = str_replace('imsmanifest.xml', 'models_html5/newWindow.html', $manifest);
-
-                                if (file_exists($newWindowPath) && is_file($newWindowPath)) {
-                                    $content = file_get_contents($newWindowPath);
-                                    $find = [
-                                        'var src = x_currentPageXML'
-                                    ];
-                                    $replace = [
-                                        'var src = "'.$proxyPath.'?type=link&src=" + x_currentPageXML'
-                                    ];
-                                    $content = str_replace($find, $replace, $content);
-                                    file_put_contents($newWindowPath, $content);
-                                }
-                            }
-                        }
-                    }
-                }
-
                 $oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
                 Display::addFlash(Display::return_message(get_lang('UplUploadSucceeded')));
             }

+ 106 - 15
main/lp/scorm.class.php

@@ -85,8 +85,10 @@ class scorm extends learnpath
             if ($this->debug > 0) {
                 error_log('In scorm::parse_manifest() - Parsing using PHP5 method');
             }
-            //$this->manifest_encoding = api_detect_encoding_xml($xml); // This is the usual way for reading the encoding.
-            // This method reads the encoding, it tries to be correct even in cases of wrong or missing encoding declarations.
+            // $this->manifest_encoding = api_detect_encoding_xml($xml);
+            // This is the usual way for reading the encoding.
+            // This method reads the encoding, it tries to be correct even in cases
+            // of wrong or missing encoding declarations.
             $this->manifest_encoding = self::detect_manifest_encoding($xml);
 
             // UTF-8 is supported by DOMDocument class, this is for sure.
@@ -227,6 +229,85 @@ class scorm extends learnpath
             return null;
         }
 
+        $fixTemplate = api_get_configuration_value('learnpath_fix_xerte_template');
+        $proxyPath = api_get_configuration_value('learnpath_proxy_url');
+        if ($fixTemplate && !empty($proxyPath)) {
+            // Check organisations:
+            if (isset($this->manifest['organizations'])) {
+                foreach ($this->manifest['organizations'] as $data) {
+                    if (strpos(strtolower($data), 'xerte') !== false) {
+                        // Check if template.xml exists:
+                        $templatePath = str_replace('imsmanifest.xml', 'template.xml', $file);
+                        if (file_exists($templatePath) && is_file($templatePath)) {
+                            $templateContent = file_get_contents($templatePath);
+                            $find = [
+                                'href="www.',
+                                'href="https://',
+                                'href="http://',
+                                'url="www.',
+                                'pdfs/download.php?'
+                            ];
+
+                            $replace = [
+                                'href="http://www.',
+                                'target = "_blank" href="'.$proxyPath.'?type=link&src=https://',
+                                'target = "_blank" href="'.$proxyPath.'?type=link&src=http://',
+                                'url="http://www.',
+                                'pdfs/download.php&'
+                            ];
+                            $templateContent = str_replace($find, $replace, $templateContent);
+                            file_put_contents($templatePath, $templateContent);
+                        }
+
+                        // Fix link generation:
+                        $linkPath = str_replace('imsmanifest.xml', 'models_html5/links.html', $file);
+                        if (file_exists($linkPath) && is_file($linkPath)) {
+                            $linkContent = file_get_contents($linkPath);
+                            $find = [
+                                ':this.getAttribute("url")'
+                            ];
+                            $replace = [
+                                ':"'.$proxyPath.'?type=link&src=" + this.getAttribute("url")'
+                            ];
+                            $linkContent = str_replace($find, $replace, $linkContent);
+                            file_put_contents($linkPath, $linkContent);
+                        }
+
+                        // Fix iframe generation
+                        $framePath = str_replace('imsmanifest.xml', 'models_html5/embedDiv.html', $file);
+
+                        if (file_exists($framePath) && is_file($framePath)) {
+                            $content = file_get_contents($framePath);
+                            $find = [
+                                '$iFrameHolder.html(iFrameTag);'
+                            ];
+                            $replace = [
+                                'iFrameTag = \'<a target ="_blank" href="'.$proxyPath.'?type=link&src=\'+ pageSrc + \'">Open website. <img src="'.api_get_path(WEB_CODE_PATH).'img/link-external.png"></a>\'; $iFrameHolder.html(iFrameTag); '
+                            ];
+                            $content = str_replace($find, $replace, $content);
+                            file_put_contents($framePath, $content);
+                        }
+
+                        // Fix new window generation
+                        $newWindowPath = str_replace('imsmanifest.xml', 'models_html5/newWindow.html', $file);
+
+                        if (file_exists($newWindowPath) && is_file($newWindowPath)) {
+                            $content = file_get_contents($newWindowPath);
+                            $find = [
+                                'var src = x_currentPageXML'
+                            ];
+                            $replace = [
+                                'var src = "'.$proxyPath.'?type=link&src=" + x_currentPageXML'
+                            ];
+                            $content = str_replace($find, $replace, $content);
+                            file_put_contents($newWindowPath, $content);
+                        }
+                    }
+                }
+            }
+        }
+
+
         // TODO: Close the DOM handler.
         return $this->manifest;
     }
@@ -527,29 +608,34 @@ class scorm extends learnpath
      * @param  string    Current path (optional)
      * @return string    Absolute path to the imsmanifest.xml file or empty string on error
      */
-    public function import_local_package($file_path, $current_dir = '')
+    public function import_local_package($file_path, $currentDir = '')
     {
         // TODO: Prepare info as given by the $_FILES[''] vector.
-        $file_info = [];
-        $file_info['tmp_name'] = $file_path;
-        $file_info['name'] = basename($file_path);
+        $fileInfo = [];
+        $fileInfo['tmp_name'] = $file_path;
+        $fileInfo['name'] = basename($file_path);
         // Call the normal import_package function.
-        return $this->import_package($file_info, $current_dir);
+        return $this->import_package($fileInfo, $currentDir);
     }
 
     /**
      * Imports a zip file into the Chamilo structure
-     * @param  string $zip_file_info Zip file info as given by $_FILES['userFile']
-     * @param  string
-     * @param  array
+     * @param string $zip_file_info Zip file info as given by $_FILES['userFile']
+     * @param string $current_dir
+     * @param array $courseInfo
+     * @param bool $updateDirContents
+     * @param learnpath $lpToCheck
      *
      * @return string    $current_dir Absolute path to the imsmanifest.xml file or empty string on error
      */
     public function import_package(
         $zip_file_info,
         $current_dir = '',
-        $courseInfo = []
+        $courseInfo = [],
+        $updateDirContents = false,
+        $lpToCheck = null
     ) {
+        $this->debug = 100;
         if ($this->debug > 0) {
             error_log('In scorm::import_package('.print_r($zip_file_info, true).',"'.$current_dir.'") method', 0);
         }
@@ -587,15 +673,12 @@ class scorm extends learnpath
         }
 
         $zipFile = new PclZip($zip_file_path);
-
         // Check the zip content (real size and file extension).
         $zipContentArray = $zipFile->listContent();
-
         $package_type = '';
         $at_root = false;
         $manifest = '';
         $manifest_list = [];
-
         // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
         $realFileSize = 0;
         foreach ($zipContentArray as $thisContent) {
@@ -656,6 +739,14 @@ class scorm extends learnpath
             return false;
         }
 
+        if ($updateDirContents && $lpToCheck) {
+            $originalPath = str_replace('/.', '', $lpToCheck->path);
+            if ($originalPath != $new_dir) {
+                Display::addFlash(Display::return_message(get_lang('FileError')));
+                return false;
+            }
+        }
+
         // It happens on Linux that $new_dir sometimes doesn't start with '/'
         if ($new_dir[0] != '/') {
             $new_dir = '/'.$new_dir;
@@ -760,7 +851,7 @@ class scorm extends learnpath
                 }
             }
         } else {
-            return '';
+            return false;
         }
 
         return $course_sys_dir.$new_dir.$manifest;