📄 courserestorer.class.php
字号:
<?php // $Id: CourseRestorer.class.php 15301 2008-05-16 03:21:42Z yannoo $/*============================================================================== Dokeos - elearning and course management software Copyright (c) 2008 Dokeos SPRL Copyright (c) 2003 Ghent University (UGent) Copyright (c) 2001 Universite catholique de Louvain (UCL) Copyright (c) Bart Mollet (bart.mollet@hogent.be) For a full list of contributors, see "credits.txt". The full license can be read in "license.txt". This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the GNU General Public License for more details. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium Mail: info@dokeos.com==============================================================================*/require_once ('Course.class.php');require_once ('Event.class.php');require_once ('Link.class.php');require_once ('ToolIntro.class.php');require_once ('LinkCategory.class.php');require_once ('ForumCategory.class.php');require_once ('Forum.class.php');require_once ('ForumTopic.class.php');require_once ('ForumPost.class.php');require_once ('CourseDescription.class.php');require_once ('Learnpath.class.php');require_once ('Survey.class.php');require_once ('SurveyQuestion.class.php');require_once ('mkdirr.php');require_once ('rmdirr.php');define('FILE_SKIP', 1);define('FILE_RENAME', 2);define('FILE_OVERWRITE', 3);/** * Class to restore items from a course object to a Dokeos-course * @author Bart Mollet <bart.mollet@hogent.be> * @package dokeos.backup */class CourseRestorer{ /** * The course-object */ var $course; /** * What to do with files with same name (FILE_SKIP, FILE_RENAME or * FILE_OVERWRITE) */ var $file_option; /** * Create a new CourseRestorer */ function CourseRestorer($course) { $this->course = $course; $this->file_option = FILE_RENAME; } /** * Set the file-option * @param constant $options What to do with files with same name (FILE_SKIP, * FILE_RENAME or FILE_OVERWRITE) */ function set_file_option($option) { $this->file_option = $option; } /** * Restore a course. * @param string $destination_course_code The code of the Dokeos-course in * which the resources should be stored. Default: Current Dokeos-course. */ function restore($destination_course_code = '') { if ($destination_course_code == '') { $course_info = api_get_course_info(); $this->course->destination_db = $course_info['dbName']; $this->course->destination_path = $course_info['path']; } else { $course_info = Database :: get_course_info($destination_course_code); $this->course->destination_db = $course_info['database']; $this->course->destination_path = $course_info['directory']; } $this->restore_links(); $this->restore_tool_intro(); $this->restore_events(); $this->restore_announcements(); $this->restore_documents(); $this->restore_scorm_documents(); $this->restore_course_descriptions(); //$this->restore_forums(); $this->restore_quizzes(); // after restore_documents! (for correct import of sound/video) $this->restore_learnpaths(); $this->restore_surveys(); // Restore the item properties $table = Database :: get_course_table(TABLE_ITEM_PROPERTY, $this->course->destination_db); foreach ($this->course->resources as $type => $resources) { foreach ($resources as $id => $resource) { foreach ($resource->item_properties as $property) { // First check if there isn't allready a record for this resource $sql = "SELECT * FROM $table WHERE tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'"; $res = api_sql_query($sql,__FILE__,__LINE__); if( Database::num_rows($res) == 0) { // The to_group_id and to_user_id are set to default values as users/groups possibly not exist in the target course $sql = "INSERT INTO $table SET tool = '".$property['tool']."', insert_user_id = '".$property['insert_user_id']."', insert_date = '".$property['insert_date']."', lastedit_date = '".$property['lastedit_date']."', ref = '".$resource->destination_id."', lastedit_type = '".$property['lastedit_type']."', lastedit_user_id = '".$property['lastedit_user_id']."', visibility = '".$property['visibility']."', start_visible = '".$property['start_visible']."', end_visible = '".$property['end_visible']."', to_user_id = '".$property['to_user_id']."', to_group_id = '0'"; ; api_sql_query($sql, __FILE__, __LINE__); } } } } // Restore the linked-resources $table = Database :: get_course_table(TABLE_LINKED_RESOURCES, $this->course->destination_db); foreach ($this->course->resources as $type => $resources) { foreach ($resources as $id => $resource) { $linked_resources = $resource->get_linked_resources(); foreach ($linked_resources as $to_type => $to_ids) { foreach ($to_ids as $index => $to_id) { $to_resource = $this->course->resources[$to_type][$to_id]; $sql = "INSERT INTO ".$table." SET source_type = '".$type."', source_id = '".$resource->destination_id."', resource_type='".$to_type."', resource_id='".$to_resource->destination_id."' "; api_sql_query($sql, __FILE__, __LINE__); } } } } } /** * Restore documents */ function restore_documents() { if ($this->course->has_resources(RESOURCE_DOCUMENT)) { $table = Database :: get_course_table(TABLE_DOCUMENT, $this->course->destination_db); $resources = $this->course->resources; foreach ($resources[RESOURCE_DOCUMENT] as $id => $document) { $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/'; $perm = api_get_setting('permissions_for_new_directories'); $perm = octdec(!empty($perm)?$perm:'0770'); mkdirr(dirname($path.$document->path),$perm); if ($document->file_type == DOCUMENT) { if (file_exists($path.$document->path)) { switch ($this->file_option) { case FILE_OVERWRITE : copy($this->course->backup_path.'/'.$document->path, $path.$document->path); $sql = "SELECT id FROM ".$table." WHERE path='/".substr($document->path, 9)."'"; $res = api_sql_query($sql, __FILE__, __LINE__); $obj = Database::fetch_object($res); $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id; $sql = "UPDATE ".$table." SET comment = '".Database::escape_string($document->comment)."', title='".Database::escape_string($document->title)."', size='".$document->size."' WHERE id = '".$obj->id."'"; api_sql_query($sql, __FILE__, __LINE__); break; case FILE_SKIP : $sql = "SELECT id FROM ".$table." WHERE path='/".Database::escape_string(substr($document->path, 9))."'"; $res = api_sql_query($sql, __FILE__, __LINE__); $obj = Database::fetch_object($res); $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id; break; case FILE_RENAME : $i = 1; $ext = explode('.', basename($document->path)); if (count($ext) > 1) { $ext = array_pop($ext); $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1)); $ext = '.'.$ext; } else { $ext = ''; $file_name_no_ext = $document->path; } $new_file_name = $file_name_no_ext.'_'.$i.$ext; $file_exists = file_exists($path.$new_file_name); while ($file_exists) { $i ++; $new_file_name = $file_name_no_ext.'_'.$i.$ext; $file_exists = file_exists($path.$new_file_name); } copy($this->course->backup_path.'/'.$document->path, $path.$new_file_name); $sql = "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($new_file_name, 9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'"; api_sql_query($sql, __FILE__, __LINE__); $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::get_last_insert_id(); //also insert into item_property /* api_item_property_update( array('dbName'=>$this->course->destination_db, TOOL_DOCUMENT, $this->course->resource[RESOURCE_DOCUMENT][$id]->destination_id, 'DocumentAdded', ); */ break; } // end switch } // end if file exists else { //make sure the source file actually exists if(is_file($this->course->backup_path.'/'.$document->path) && is_readable($this->course->backup_path.'/'.$document->path) && is_dir(dirname($path.$document->path)) && is_writeable(dirname($path.$document->path))) { copy($this->course->backup_path.'/'.$document->path, $path.$document->path); $sql = "INSERT INTO ".$table." SET path = '/".substr($document->path, 9)."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'"; api_sql_query($sql, __FILE__, __LINE__); $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::get_last_insert_id(); } else { if(is_file($this->course->backup_path.'/'.$document->path) && is_readable($this->course->backup_path.'/'.$document->path)) { error_log('Course copy generated an ignoreable error while trying to copy '.$this->course->backup_path.'/'.$document->path.': file not found'); } if(!is_dir(dirname($path.$document->path))) { error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not found'); } if(!is_writeable(dirname($path.$document->path))) { error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not writeable'); } } } // end file doesn't exist } else { $sql = "SELECT id FROM ".$table." WHERE path = '/".Database::escape_string(substr($document->path, 9))."'"; $res = api_sql_query($sql,__FILE__,__LINE__); if( Database::num_rows($res)> 0) { $obj = Database::fetch_object($res); $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id; } else { $sql = "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($document->path, 9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'"; api_sql_query($sql, __FILE__, __LINE__); $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::get_last_insert_id(); } } // end folder } // end for each } } /** * Restore scorm documents * TODO @TODO check that the restore function with renaming doesn't break the scorm structure! */ function restore_scorm_documents() { if ($this->course->has_resources(RESOURCE_SCORM)) { $resources = $this->course->resources; foreach ($resources[RESOURCE_SCORM] as $id => $document) { $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/'; $perm = api_get_setting('permissions_for_new_directories'); $perm = octdec(!empty($perm)?$perm:'0770'); mkdirr(dirname($path.$document->path),$perm);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -