dokeos-backup
[ class tree: dokeos-backup ] [ index: dokeos-backup ] [ all elements ]

Source for file CourseArchiver.class.php

Documentation is available at CourseArchiver.class.php

  1. <?php
  2. // $Id: CourseArchiver.class.php 15429 2008-05-26 20:34:37Z yannoo $
  3. /*
  4. ==============================================================================
  5.     Dokeos - elearning and course management software
  6.  
  7.     Copyright (c) 2004-2008 Dokeos SPRL
  8.     Copyright (c) 2003 Ghent University (UGent)
  9.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  10.     Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  11.  
  12.     For a full list of contributors, see "credits.txt".
  13.     The full license can be read in "license.txt".
  14.  
  15.     This program is free software; you can redistribute it and/or
  16.     modify it under the terms of the GNU General Public License
  17.     as published by the Free Software Foundation; either version 2
  18.     of the License, or (at your option) any later version.
  19.  
  20.     See the GNU General Public License for more details.
  21.  
  22.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  23.     Mail: info@dokeos.com
  24. ==============================================================================
  25. */
  26. require_once ('Course.class.php');
  27. require_once ('mkdirr.php');
  28. require_once ('rmdirr.php');
  29. require_once (api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  30. /**
  31.  * Some functions to write a course-object to a zip-file and to read a course-
  32.  * object from such a zip-file.
  33.  * @author Bart Mollet <bart.mollet@hogent.be>
  34.  * @package dokeos.backup
  35.  *
  36.  * @todo Use archive-folder of Dokeos?
  37.  */
  38. {
  39.     /**
  40.      * Delete old temp-dirs
  41.      */
  42.     function clean_backup_dir()
  43.     {
  44.         $dir api_get_path(SYS_ARCHIVE_PATH);
  45.         if ($handle opendir($dir))
  46.         {
  47.             while (($file readdir($handle)) !== false)
  48.             {
  49.                 if ($file != "." && $file != ".." && strpos($file,'CourseArchiver_'== && is_dir($dir.'/'.$file))
  50.                 {
  51.                     rmdirr($dir.'/'.$file);
  52.                 }
  53.             }
  54.             closedir($handle);
  55.         }
  56.     }
  57.     /**
  58.      * Write a course and all its resources to a zip-file.
  59.      * @return string A pointer to the zip-file
  60.      */
  61.     function write_course($course)
  62.     {
  63.         CourseArchiver::clean_backup_dir();
  64.         // Create a temp directory
  65.         $tmp_dir_name 'CourseArchiver_'.uniqid('');
  66.         $backup_dir api_get_path(SYS_ARCHIVE_PATH).''.$tmp_dir_name.'/';
  67.         // All course-information will be stored in course_info.dat
  68.         $course_info_file $backup_dir.'course_info.dat';
  69.         $zip_dir api_get_path(SYS_ARCHIVE_PATH).'';
  70.         $user api_get_user_info();
  71.         $zip_file $user['user_id'].'_'.$course->code.'_'.date("YmdHis").'.zip';
  72.         $php_errormsg '';
  73.         $res @mkdir($backup_dir0755);
  74.         if($res == false)
  75.         {
  76.             //TODO set and handle an error message telling the user to review the permissions on the archive directory
  77.                   error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors')!=false?$php_errormsg:'error not recorded because track_errors is off in your php.ini').' - This error, occuring because your archive directory will not let this script write data into it, will prevent courses backups to be created',0);
  78.         
  79.         // Write the course-object to the file
  80.         $fp @fopen($course_info_file'w');
  81.         if($fp == false)
  82.         {
  83.                   error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors')!=false?$php_errormsg:'error not recorded because track_errors is off in your php.ini'),0);
  84.         
  85.         $res @fwrite($fpbase64_encode(serialize($course)));
  86.         if($res == false)
  87.         {
  88.                   error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors')!=false?$php_errormsg:'error not recorded because track_errors is off in your php.ini'),0);
  89.         
  90.         $res @fclose($fp);
  91.         if($res == false)
  92.         {
  93.                   error_log(__FILE__.' line '.__LINE__.': '.(ini_get('track_errors')!=false?$php_errormsg:'error not recorded because track_errors is off in your php.ini'),0);
  94.         
  95.  
  96.         // Copy all documents to the temp-dir
  97.         ifis_array($course->resources[RESOURCE_DOCUMENT]))
  98.         {
  99.             foreach ($course->resources[RESOURCE_DOCUMENTas $id => $document)
  100.             {
  101.                 if ($document->file_type == DOCUMENT)
  102.                 {
  103.                     $doc_dir $backup_dir.$document->path;
  104.                     mkdirr(dirname($doc_dir)0755);
  105.                     copy($course->path.$document->path$doc_dir);
  106.                 }
  107.                 else
  108.                 {
  109.                     mkdirr($backup_dir.$document->path0755);
  110.                 }
  111.             }
  112.         }
  113.  
  114.         // Copy all scorm documents to the temp-dir
  115.         ifis_array($course->resources[RESOURCE_SCORM]))
  116.         {
  117.             foreach ($course->resources[RESOURCE_SCORMas $id => $document)
  118.             {
  119.                 $doc_dir=dirname($backup_dir.$document->path);
  120.  
  121.                 mkdirr($doc_dir,0755);
  122.  
  123.                 copyDirTo($course->path.$document->path$doc_dirfalse);
  124.             }
  125.         }
  126.  
  127.         // Zip the course-contents
  128.         $zip new PclZip($zip_dir.$zip_file);
  129.         $zip->create($zip_dir.$tmp_dir_namePCLZIP_OPT_REMOVE_PATH$zip_dir.$tmp_dir_name.'/');
  130.         //$zip->deleteByIndex(0);
  131.         // Remove the temp-dir.
  132.         rmdirr($backup_dir);
  133.         return ''.$zip_file;
  134.     }
  135.     /**
  136.      *
  137.      */
  138.     function get_available_backups($user_id null)
  139.     {
  140.         global $dateTimeFormatLong;
  141.         $backup_files array();
  142.         $dirname api_get_path(SYS_ARCHIVE_PATH).'';
  143.         if ($dir opendir($dirname)) {
  144.               while (($file readdir($dir)) !== false{
  145.                    $file_parts explode('_',$file);
  146.                    if(count($file_parts== 3)
  147.                    {
  148.                        $owner_id $file_parts[0];
  149.                        $course_code $file_parts[1];
  150.                        $file_parts explode('.',$file_parts[2]);
  151.                        $date $file_parts[0];
  152.                        $ext $file_parts[1];
  153.                        if($ext == 'zip' && ($user_id != null && $owner_id == $user_id || $user_id == null) )
  154.                        {
  155.                            $date substr($date,0,4).'-'.substr($date,4,2).'-'.substr($date,6,2).' '.substr($date,8,2).':'.substr($date,10,2).':'.substr($date,12,2);
  156.                            $backup_files[array('file' => $file'date' => $date'course_code' => $course_code);
  157.                        }
  158.                    }
  159.               }
  160.               closedir($dir);
  161.         }
  162.         return $backup_files;
  163.     }
  164.     /**
  165.      *
  166.      */
  167.     function import_uploaded_file($file)
  168.     {
  169.         $new_filename uniqid('').'.zip';
  170.         $new_dir api_get_path(SYS_ARCHIVE_PATH);
  171.         if(is_dir($new_dir&& is_writable($new_dir))
  172.         {
  173.           move_uploaded_file($file,api_get_path(SYS_ARCHIVE_PATH).''.$new_filename);
  174.           return $new_filename;
  175.         }
  176.         return false;
  177.     }
  178.     /**
  179.      * Read a course-object from a zip-file
  180.      * @return course The course
  181.      * @param boolean $delete Delete the file after reading the course?
  182.      * @todo Check if the archive is a correct Dokeos-export
  183.      */
  184.     function read_course($filename,$delete false)
  185.     {
  186.         CourseArchiver::clean_backup_dir();
  187.         // Create a temp directory
  188.         $tmp_dir_name 'CourseArchiver_'.uniqid('');
  189.         $unzip_dir api_get_path(SYS_ARCHIVE_PATH).''.$tmp_dir_name;
  190.         mkdirr($unzip_dir,0755);
  191.         @copy(api_get_path(SYS_ARCHIVE_PATH).''.$filename,$unzip_dir.'/backup.zip');
  192.         // unzip the archive
  193.         $zip new PclZip($unzip_dir.'/backup.zip');
  194.         @chdir($unzip_dir);
  195.         $zip->extract();
  196.         // remove the archive-file
  197.         if($delete)
  198.         {
  199.             @unlink(api_get_path(SYS_ARCHIVE_PATH).''.$filename);
  200.         }
  201.         // read the course
  202.         if(!is_file('course_info.dat'))
  203.         {
  204.             return new Course();
  205.         }
  206.         $fp @fopen('course_info.dat'"r");
  207.         $contents @fread($fpfilesize('course_info.dat'));
  208.         @fclose($fp);
  209.         $course unserialize(base64_decode($contents));
  210.         ifget_class($course!= 'Course')
  211.         {
  212.             return new Course();
  213.         }
  214.         $course->backup_path $unzip_dir;
  215.         return $course;
  216.     }
  217. }
  218. ?>

Documentation generated on Thu, 12 Jun 2008 13:11:10 -0500 by phpDocumentor 1.4.1