Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/cc/cc_lib/cc_convert_moodle2.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00023 if (!extension_loaded('fileinfo')) {
00024     die('You must install fileinfo extension!');
00025 }
00026 
00027 abstract class cc_convert_moodle2 {
00028 
00037     public static function convert($packagedir, $outdir) {
00038         $dir = realpath($packagedir);
00039         if (empty($dir)) {
00040             throw new InvalidArgumentException('Directory does not exist!');
00041         }
00042         $odir = realpath($outdir);
00043         if (empty($odir)) {
00044             throw new InvalidArgumentException('Directory does not exist!');
00045         }
00046         $coursefile = $dir.DIRECTORY_SEPARATOR.'course'.DIRECTORY_SEPARATOR.'course.xml';
00047         $doc = new XMLGenericDocument();
00048         if ($doc->load($coursefile)) {
00049             $course_name = $doc->nodeValue('/course/fullname');
00050             $course_desc = $doc->nodeValue('/course/summary');
00051             $course_language = $doc->nodeValue('/course/lang');
00052             $course_language = empty($course_language) ? 'en' : $course_language;
00053             $course_category = $doc->nodeValue('/course/category/name');
00054 
00055             //Initialize the manifest metadata class
00056             $meta = new cc_metadata_manifest();
00057 
00058             //Package metadata
00059             $metageneral = new cc_metadata_general();
00060             $metageneral->set_language($course_language);
00061             $metageneral->set_title($course_name, $course_language);
00062             $metageneral->set_description($course_desc, $course_language);
00063             $metageneral->set_catalog('category');
00064             $metageneral->set_entry($course_category);
00065             $meta->add_metadata_general($metageneral);
00066 
00067             // Create the manifest
00068             $manifest = new cc_manifest(cc_version::v11);
00069 
00070             $manifest->add_metadata_manifest($meta);
00071 
00072             $organization = null;
00073 
00074             //Package structure - default organization and resources
00075             //Get the course structure - this will be transformed into organization
00076             //Step 1 - Get the list and order of sections/topics
00077             $moodle_backup = $dir . DIRECTORY_SEPARATOR . 'moodle_backup.xml';
00078             $secp = new XMLGenericDocument();
00079             $docp = new XMLGenericDocument();
00080             if ($docp->load($moodle_backup)) {
00081                 //sections
00082                 $sections = array();
00083                 $coursef = new XMLGenericDocument();
00084                 $course_file = $dir . DIRECTORY_SEPARATOR .'course' . DIRECTORY_SEPARATOR . 'course.xml';
00085                 $coursef->load($course_file);
00086                 $numsections = (int)$coursef->nodeValue('/course/numsections');
00087                 $section_list = $docp->nodeList('/moodle_backup/information/contents/sections/section');
00088                 if (!empty($section_list)) {
00089                     $count = 0;
00090                     foreach ($section_list as $node) {
00091                         if ($count > $numsections) {
00092                             break;
00093                         }
00094                         $sectionid    = $docp->nodeValue('sectionid', $node);
00095                         $sectiontitle = $docp->nodeValue('title'    , $node);
00096                         $sectionpath  = $docp->nodeValue('directory', $node);
00097                         $sequence = array();
00098                         //Get section stuff
00099                         $section_file = $dir .
00100                         DIRECTORY_SEPARATOR .
00101                         $sectionpath .
00102                         DIRECTORY_SEPARATOR .
00103                         'section.xml';
00104                         if ($secp->load($section_file)) {
00105                             $rawvalue = $secp->nodeValue('/section/sequence');
00106                             if ($rawvalue != '$@NULL@$') {
00107                                 $sequence = explode(',', $rawvalue);
00108                             }
00109                         }
00110                         $sections[$sectionid] = array($sectiontitle, $sequence);
00111                         $count++;
00112                     }
00113                 }
00114                 //organization title
00115                 $organization = new cc_organization();
00116                 //Add section/topic items
00117                 foreach ($sections as $sectionid => $values) {
00118                     $item = new cc_item();
00119                     $item->title = $values[0];
00120                     self::process_sequence($item, $manifest, $values[1], $dir, $odir);
00121                     $organization->add_item($item);
00122                 }
00123                 $manifest->put_nodes();
00124             }
00125 
00126             if (!empty($organization)) {
00127                 $manifest->add_new_organization($organization);
00128             }
00129 
00130             $manifestpath = $outdir.DIRECTORY_SEPARATOR.'imsmanifest.xml';
00131             $manifest->saveTo($manifestpath);
00132         }
00133 
00134     }
00135 
00144     protected static function process_sequence(cc_i_item &$item, cc_i_manifest &$manifest, array $sequence, $packageroot, $outdir) {
00145         $moodle_backup = $packageroot . DIRECTORY_SEPARATOR . 'moodle_backup.xml';
00146         $doc = new XMLGenericDocument();
00147         if(!$doc->load($moodle_backup)) {
00148             return;
00149         }
00150         $activities = $doc->nodeList('/moodle_backup/information/contents/activities/activity');
00151         if (!empty($activities)) {
00152             $dpp = new XMLGenericDocument();
00153             foreach ($activities as $activity) {
00154                 $moduleid = $doc->nodeValue('moduleid', $activity);
00155                 if (in_array($moduleid, $sequence)) {
00156                     //detect activity type
00157                     $directory = $doc->nodeValue('directory', $activity);
00158                     $path = $packageroot . DIRECTORY_SEPARATOR . $directory;
00159                     $module_file = $path . DIRECTORY_SEPARATOR . 'module.xml';
00160                     if ($dpp->load($module_file)) {
00161                         $activity_type = $dpp->nodeValue('/module/modulename');
00162                         $activity_indentation = $dpp->nodeValue('/module/indent');
00163                         $aitem = self::item_indenter($item, $activity_indentation);
00164                         $caller = "cc_converter_{$activity_type}";
00165                         if (class_exists($caller)) {
00166                             $obj = new $caller($aitem, $manifest, $packageroot, $path);
00167                             if (!$obj->convert($outdir)) {
00168                                 throw new RuntimeException("failed to convert {$activity_type}");
00169                             }
00170                         }
00171                     }
00172                 }
00173             }
00174         }
00175     }
00176 
00177     protected static function item_indenter(cc_i_item &$item, $level = 0) {
00178         $indent = (int)$level;
00179         $indent = ($indent) <= 0 ? 0 : $indent;
00180         $nprev = null;
00181         $nfirst = null;
00182         for ($pos = 0, $size = $indent; $pos < $size; $pos++) {
00183             $nitem = new cc_item();
00184             $nitem->title = '';
00185             if (empty($nfirst)) {
00186                 $nfirst = $nitem;
00187             }
00188             if (!empty($nprev)) {
00189                 $nprev->add_child_item($nitem);
00190             }
00191             $nprev = $nitem;
00192         }
00193         $result = $item;
00194         if (!empty($nfirst)) {
00195             $item->add_child_item($nfirst);
00196             $result = $nprev;
00197         }
00198         return $result;
00199     }
00200 
00201 }
 All Data Structures Namespaces Files Functions Variables Enumerations