Browse Source

[svn r19948] Restore course fixed see FS#3981

Julio Montoya 16 years ago
parent
commit
6afd711c20

+ 12 - 15
main/coursecopy/classes/Course.class.php

@@ -1,4 +1,4 @@
-<?php // $Id: Course.class.php 16630 2008-10-27 19:11:45Z ivantcholakov $
+<?php // $Id: Course.class.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ============================================================================== 
 	Dokeos - elearning and course management software
@@ -52,22 +52,19 @@ class Course
 	 */
 	function is_linked_resource(& $resource_to_check)
 	{
-		foreach($this->resources as $type => $resources)
-		{
-			foreach($resources as $id => $resource)
-			{
-				if( $resource->links_to($resource_to_check) )
-				{
-					return true;	
-				}
-				if( $type == RESOURCE_LEARNPATH && get_class($resource)=='Learnpath')
-				{
-					if($resource->has_item($resource_to_check))
-					{
+		foreach($this->resources as $type => $resources) {
+			if (is_array($resources)) {
+				foreach($resources as $id => $resource) {
+					if( $resource->links_to($resource_to_check) ) {
 						return true;	
-					}	
+					}
+					if( $type == RESOURCE_LEARNPATH && get_class($resource)=='Learnpath') {
+						if($resource->has_item($resource_to_check)) {
+							return true;	
+						}	
+					}
 				}
-			}	
+			}
 		}
 		return false;
 	}

+ 2 - 2
main/coursecopy/classes/CourseBuilder.class.php

@@ -1,4 +1,4 @@
-<?php // $Id: CourseBuilder.class.php 18549 2009-02-17 18:08:58Z cfasanando $
+<?php // $Id: CourseBuilder.class.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ==============================================================================
 	Dokeos - elearning and course management software
@@ -128,7 +128,7 @@ class CourseBuilder
 	{
 		$table_doc = Database :: get_course_table(TABLE_DOCUMENT);
 		$table_prop = Database :: get_course_table(TABLE_ITEM_PROPERTY);	
-		echo $this->course->type;			
+			
         if (!empty($this->course->type) && $this->course->type=='partial')        	
         	$sql = 'SELECT * FROM '.$table_doc.' d, '.$table_prop.' p WHERE tool = \''.TOOL_DOCUMENT.'\' AND p.ref = d.id AND p.visibility != 2 AND path NOT LIKE \'/images/gallery%\' ORDER BY path';
         else

+ 91 - 84
main/coursecopy/classes/CourseRestorer.class.php

@@ -1,4 +1,4 @@
-<?php // $Id: CourseRestorer.class.php 19321 2009-03-25 20:15:33Z juliomontoya $
+<?php // $Id: CourseRestorer.class.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ==============================================================================
 	Dokeos - elearning and course management software
@@ -82,14 +82,11 @@ class CourseRestorer
 	 */
 	function restore($destination_course_code = '')
 	{
-		if ($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
-		{
+		} else {
 			$course_info = Database :: get_course_info($destination_course_code);
 			$this->course->destination_db = $course_info['database'];
 			$this->course->destination_path = $course_info['directory'];
@@ -108,33 +105,33 @@ class CourseRestorer
 		$this->restore_student_publication();
 		// 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)
+		foreach ($this->course->resources as $type => $resources) {
+			if (is_array($resources)) {
+				foreach ($resources as $id => $resource) {
+					foreach ($resource->item_properties as $property)
 					{
-						// 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__);
+						// 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__);
+						}
 					}
 				}
 			}
@@ -143,61 +140,74 @@ class CourseRestorer
 		$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)
+			if (is_array($resources)) 
+				foreach ($resources as $id => $resource)
 				{
-					foreach ($to_ids as $index => $to_id)
+					$linked_resources = $resource->get_linked_resources();
+					foreach ($linked_resources as $to_type => $to_ids)
 					{
-						$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__);
+						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))
-		{
+	{		
+		if ($this->course->has_resources(RESOURCE_DOCUMENT)) {			
 			$table = Database :: get_course_table(TABLE_DOCUMENT, $this->course->destination_db);
 			$resources = $this->course->resources;
 			$destination_course['dbName']= $this->course->destination_db;
-			
-			foreach ($resources[RESOURCE_DOCUMENT] as $id => $document)
-			{	
+			/* echo '<pre>'; echo $this->course->backup_path; echo '<br>'; */
+			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);				
-				$dirs = explode('/', dirname($document->path));
-				
-				$my_temp = '';
-				for ($i=1; $i<count($dirs); $i++) {					
-					$my_temp .= $dirs[$i];					
-					if (!is_dir($path.'document/'.$my_temp)) {						
-						$sql = "SELECT id FROM ".$table." WHERE path='/".Database::escape_string($my_temp)."'";
-						$res = api_sql_query($sql, __FILE__, __LINE__);									
-						$num_result = Database::num_rows($res);
-						if ($num_result==0) {							
-							$created_dir = create_unexisting_directory($destination_course,api_get_user_id(),0, 0 ,$path.'document','/'.$my_temp,basename($my_temp));
-						}						
-					}	
-					$my_temp .= '/';																
-				}
-				
-				
-				if ($document->file_type == DOCUMENT)
-				{
-					if (file_exists($path.$document->path))
-					{
-						switch ($this->file_option)
-						{
-							case FILE_OVERWRITE :
+			    $perm = octdec(!empty($perm)?$perm:0770);
+			    $dirs = explode('/', dirname($document->path));	
+			    if (count($dirs)==1) {
+			    	if ($this->file_type==FOLDER) {
+			    		$new = substr($document->path, 8);
+			    		$created_dir = create_unexisting_directory($destination_course,api_get_user_id(),0, 0 ,$path.'document',$new,basename($new));
+			    	}
+			    } else {			    	    								
+					$my_temp = '';		
+					for ($i=1; $i<=count($dirs); $i++) {			
+						$my_temp .= $dirs[$i];					
+						if (!is_dir($path.'document/'.$my_temp)) {											
+							$sql = "SELECT id FROM ".$table." WHERE path='/".Database::escape_string($my_temp)."'"; 
+							//echo '<br>';							
+							$res = api_sql_query($sql, __FILE__, __LINE__);							
+							$num_result = Database::num_rows($res);					
+							if ($num_result==0) {
+								$created_dir = create_unexisting_directory($destination_course,api_get_user_id(),0, 0 ,$path.'document','/'.$my_temp,basename($my_temp));
+							}
+						}
+						$my_temp .= '/';																
+					}
+			    }
+			    /*
+				echo '<br>';
+				echo '------------------------';
+				echo '<br>';
+				echo '$doculent:';echo '<br>';
+				echo print_r($document); echo '<br>';
+				echo 'documlent->path'.$path.$document->path;echo '<br>';
+				echo 'file option:'.$this->file_option; echo '<br>';
+				echo 'filetype:'.$document->file_type ;
+				echo '<br>';
+				*/
+				if ($document->file_type == DOCUMENT) {
+					if (file_exists($path.$document->path)) {	
+						switch ($this->file_option) {
+							case FILE_OVERWRITE :								
+								$this->course->backup_path.'/'.$document->path;
 								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__);
@@ -215,21 +225,17 @@ class CourseRestorer
 							case FILE_RENAME :
 								$i = 1;
 								$ext = explode('.', basename($document->path));
-								if (count($ext) > 1)
-								{
+								if (count($ext) > 1) {
 									$ext = array_pop($ext);
 									$file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
 									$ext = '.'.$ext;
-								}
-								else
-								{
+								} 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)
-								{
+								while ($file_exists) {
 									$i ++;
 									$new_file_name = $file_name_no_ext.'_'.$i.$ext;
 									$file_exists = file_exists($path.$new_file_name);
@@ -253,6 +259,7 @@ class CourseRestorer
 					else
 					{
 						//make sure the source file actually exists
+						//echo $this->course->backup_path.'/'.$document->path;
 						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);
@@ -276,10 +283,10 @@ class CourseRestorer
 							}
 						}
 					} // end file doesn't exist
-				}
+				}				
 				else
 				{
-					$sql = "SELECT id FROM ".$table." WHERE path = '/".Database::escape_string(substr($document->path, 9))."'";
+					/*$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)
 					{
@@ -291,7 +298,7 @@ class CourseRestorer
 						$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
 		}
@@ -328,7 +335,7 @@ class CourseRestorer
 						case FILE_SKIP :
 							break;
 						case FILE_RENAME :
-							$i = 1;
+							$i = 1; 
 
 							$ext = explode('.', basename($document->path));
 
@@ -1082,7 +1089,7 @@ class CourseRestorer
 /**
  * copy all directory and sub directory
  */
-	function api_create_all_directory($source, $dest, $overwrite = false){
+	function api_create_all_directory($source, $dest, $overwrite = false){		
    		if(!is_dir($dest)) {
     		mkdir($dest);
     	}

+ 128 - 124
main/coursecopy/classes/CourseSelectForm.class.php

@@ -1,5 +1,5 @@
 <?php
-// $Id: CourseSelectForm.class.php 19321 2009-03-25 20:15:33Z juliomontoya $
+// $Id: CourseSelectForm.class.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ============================================================================== 
 	Dokeos - elearning and course management software
@@ -34,9 +34,10 @@ class CourseSelectForm
 	/**
 	 * Display the form
 	 * @param array $hidden_fiels Hidden fields to add to the form.
+	 * @param boolean the document array will be serialize. This is used in the course_copy.php file
 	 */
-	function display_form($course, $hidden_fields = null)
-	{
+	function display_form($course, $hidden_fields = null, $avoid_serialize=false)
+	{		
 		$resource_titles[RESOURCE_EVENT] = get_lang('Events');
 		$resource_titles[RESOURCE_ANNOUNCEMENT] = get_lang('Announcements');
 		$resource_titles[RESOURCE_DOCUMENT] = get_lang('Documents');
@@ -91,32 +92,26 @@ class CourseSelectForm
 		<?php
 
 		//get destination course title
-		if(!empty($hidden_fields['destination_course']))
-		{
+		if(!empty($hidden_fields['destination_course'])) {
 			require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
 			$course_infos = CourseManager::get_course_information($hidden_fields['destination_course']);
 			echo '<h3>';
-			echo get_lang('DestinationCourse').' : '.$course_infos['title'];
+				echo get_lang('DestinationCourse').' : '.$course_infos['title'];
 			echo '</h3>';
 		}
-
+		
 		echo '<p>';
 		echo get_lang('SelectResources');
 		echo '</p>';
 		
 		echo '<script language="javascript" src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>';
 		echo '<script type="text/javascript">var myUpload = new upload(1000);</script>';
-		
 		echo '<form method="post" id="upload_form" name="course_select_form" onsubmit="myUpload.start(\'dynamic_div\',\''.api_get_path(WEB_CODE_PATH).'img/progress_bar.gif\',\''.get_lang('PleaseStandBy').'\',\'upload_form\')">';
-		
 		echo '<input type="hidden" name="action" value="course_select_form"/>';
-
-		foreach ($course->resources as $type => $resources)
-		{
-			if (count($resources) > 0)
-			{
-				switch ($type)
-				{
+		
+		foreach ($course->resources as $type => $resources) {
+			if (count($resources) > 0) {
+				switch ($type) {
 					case RESOURCE_LINKCATEGORY :
 					case RESOURCE_FORUMCATEGORY :
 					case RESOURCE_FORUMPOST :
@@ -127,14 +122,13 @@ class CourseSelectForm
 					case RESOURCE_SCORM:
 						break;
 					default :
-						echo ' <img id="img_'.$type.'" src="../img/1.gif" onclick="javascript:exp('."'$type'".');" />';
-						echo ' <b  onclick="javascript:exp('."'$type'".');" >'.$resource_titles[$type].'</b><br />';
+						echo '<img id="img_'.$type.'" src="../img/1.gif" onclick="javascript:exp('."'$type'".');" />';
+						echo '<b  onclick="javascript:exp('."'$type'".');" >'.$resource_titles[$type].'</b><br />';
 						echo '<div id="div_'.$type.'">';
 						echo '<blockquote>';
 						echo "[<a href=\"#\" onclick=\"javascript:setCheckbox('$type',true);\" >".get_lang('All')."</a> - <a href=\"#\" onclick=\"javascript:setCheckbox('$type',false);\" >".get_lang('None')."</a>]";
-						echo '<br />';
-						foreach ($resources as $id => $resource)
-						{
+						echo '<br />'; 
+						foreach ($resources as $id => $resource) {
 							echo '<input type="checkbox" name="resource['.$type.']['.$id.']" id="resource['.$type.']['.$id.']"/>';
 							echo ' <label for="resource['.$type.']['.$id.']">';
 							$resource->show();
@@ -149,20 +143,22 @@ class CourseSelectForm
 			}
 		}
 		
-		//Documents are avoided due the huge moun of memory that  serialize function eats (when there are directories with hundred of files)
-		$course->resources['document']= null; 
+		if ($avoid_serialize) {
+			//Documents are avoided due the huge amount of memory that the serialize php function "eats" (when there are directories with hundred/thousand of files)
+			// this is a known issue of serialize
+			$course->resources['document']= null;
+		} 
+		
 		echo '<input type="hidden" name="course" value="'.base64_encode(serialize($course)).'"/>';
 		
-		if (is_array($hidden_fields))
-		{
-			foreach ($hidden_fields as $key => $value)
-			{
+		if (is_array($hidden_fields)) {
+			foreach ($hidden_fields as $key => $value) {
 				echo "\n";
 				echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
 			}
 		}
 		echo '<br /><button class="save" type="submit" onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'.get_lang('Ok').'</button>';
-		
+
 		CourseSelectForm :: display_hidden_quiz_questions($course);
 		CourseSelectForm :: display_hidden_scorm_directories($course);
 		echo '</form>';
@@ -171,17 +167,12 @@ class CourseSelectForm
 	
 	
 	function display_hidden_quiz_questions($course)
-	{
-	
-		foreach ($course->resources as $type => $resources)
-		{
-			if (count($resources) > 0)
-			{
-				switch ($type)
-				{
+	{	
+		foreach ($course->resources as $type => $resources) {
+			if (count($resources) > 0) {
+				switch ($type) {
 					case RESOURCE_QUIZQUESTION:
-						foreach ($resources as $id => $resource)
-						{
+						foreach ($resources as $id => $resource) {
 							echo '<input type="hidden" name="resource['.RESOURCE_QUIZQUESTION.']['.$id.'] id="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" value="On" />';
 						}
 						break;
@@ -190,121 +181,134 @@ class CourseSelectForm
 			}
 		}
 	}
+	
 	function display_hidden_scorm_directories($course)
 	{
-		foreach ($course->resources as $type => $resources)
-		{
-			if(count($resources) > 0)
-			{
-				switch($type)
-				{
+		foreach ($course->resources as $type => $resources) {
+			if(count($resources) > 0) {
+				switch($type) {
 					case RESOURCE_SCORM:
-						foreach ($resources as $id=>$resource)
-						{
+						foreach ($resources as $id=>$resource) {
 							echo '<input type="hidden" name="resource['.RESOURCE_SCORM.']['.$id.'] id="resource['.RESOURCE_SCORM.']['.$id.']" value="On" />';
 						}
 						break;
 				}
 			}
 		}
-	}
-	
+	}	
 	
 	/**
-	 * Get the posted course
+	 * Get the posted course 
+	 * @param string who calls the function? It can be copy_course, create_backup, import_backup or recycle_course
 	 * @return course The course-object with all resources selected by the user
 	 * in the form given by display_form(...)
 	 */
-	function get_posted_course()
+	function get_posted_course($from='')
 	{
-		$course = unserialize(base64_decode($_POST['course']));
-		
+		$course = unserialize(base64_decode($_POST['course']));				
 		//Create the resource DOCUMENT objects		
 		//Loading the results from the checkboxes of the javascript
 		$resource = $_POST['resource'][RESOURCE_DOCUMENT];
 		$table_doc = Database :: get_course_table(TABLE_DOCUMENT);
 		$table_prop = Database :: get_course_table(TABLE_ITEM_PROPERTY);				
-		$resource = array_keys($resource);	
-		foreach	($resource as $resource_item) {			
-				$sql = 'SELECT * FROM '.$table_doc.' d, '.$table_prop.' p WHERE tool = \''.TOOL_DOCUMENT.'\' AND p.ref = d.id AND p.visibility != 2 AND id = '.$resource_item.' ORDER BY path';				
-				$db_result = api_sql_query($sql, __FILE__, __LINE__);
-				while ($obj = Database::fetch_object($db_result)) {
-					$doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);									
-					$course->add_resource($doc);
 					
-					// adding item property										
-					$sql = "SELECT * FROM $table_prop WHERE TOOL = '".RESOURCE_DOCUMENT."' AND ref='".$resource_item."'";					
-					$res = api_sql_query($sql,__FILE__,__LINE__);
-					$all_properties = array ();
-					while ($item_property = Database::fetch_array($res,'ASSOC')) {						
-						$all_properties[] = $item_property;
-					}
-					$course->resources[RESOURCE_DOCUMENT][$resource_item]->item_properties = $all_properties;
-				}						
-		} 
-		
+		// Searching the documents resource that have been set to null because $avoid_serialize is true in the display_form() function 
+		if ($from=='copy_course') {			
+			if (is_array($resource)) {
+				$resource = array_keys($resource);
+				foreach	($resource as $resource_item) {				
+					$sql = 'SELECT * FROM '.$table_doc.' d, '.$table_prop.' p WHERE tool = \''.TOOL_DOCUMENT.'\' AND p.ref = d.id AND p.visibility != 2 AND id = '.$resource_item.' ORDER BY path';
+					$db_result = api_sql_query($sql, __FILE__, __LINE__);
+					while ($obj = Database::fetch_object($db_result)) {
+						$doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);									
+						$course->add_resource($doc);						
+						// adding item property										
+						$sql = "SELECT * FROM $table_prop WHERE TOOL = '".RESOURCE_DOCUMENT."' AND ref='".$resource_item."'";					
+						$res = api_sql_query($sql,__FILE__,__LINE__);
+						$all_properties = array ();
+						while ($item_property = Database::fetch_array($res,'ASSOC')) {						
+							$all_properties[] = $item_property;
+						}
+						$course->resources[RESOURCE_DOCUMENT][$resource_item]->item_properties = $all_properties;
+					}						
+				}
+			}
+		}	
 		
-		foreach ($course->resources as $type => $resources)
-		{
-			switch ($type)
-			{
-				case RESOURCE_SURVEYQUESTION:
-					foreach($resources as $id => $obj)
-					{
-					    if(!in_array($obj->survey_id,array_keys($_POST['resource'][RESOURCE_SURVEY])))
-						{
-							unset ($course->resources[$type][$id]);
-					    }
+		/*else {			
+			$documents = $_POST['resource'][RESOURCE_DOCUMENT];	
+			//print_r($course->resources );
+			foreach	($resource as $resource_item) {
+				echo $resource_item;					
+				foreach($documents as $obj) {
+					print_r($obj);
+					
+					if ($obj->id==$resource_item) {
+						$doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
+						print_r($doc);									
+						$course->add_resource($doc);		
 					}
+				}
+			}			
+		}*/
+		
+		if (is_array($course->resources)) {
+			foreach ($course->resources as $type => $resources) {	
+				switch ($type) {
+					case RESOURCE_SURVEYQUESTION:
+						foreach($resources as $id => $obj) {
+						    if(!in_array($obj->survey_id,array_keys($_POST['resource'][RESOURCE_SURVEY]))) {
+								unset ($course->resources[$type][$id]);
+						    }
+						}
 					break;
-				case RESOURCE_LINKCATEGORY :
-				case RESOURCE_FORUMCATEGORY :
-				case RESOURCE_FORUMPOST :
-				case RESOURCE_FORUMTOPIC :
-				case RESOURCE_QUIZQUESTION :
-				case RESOURCE_DOCUMENT:
-					// Mark folders to import which are not selected by the user to import,
-					// but in which a document was selected.
-					$documents = $_POST['resource'][RESOURCE_DOCUMENT];
-					foreach($resources as $id => $obj)
-					{
-						if( $obj->file_type == 'folder' && ! isset($_POST['resource'][RESOURCE_DOCUMENT][$id]) && is_array($documents))
-						{
-							foreach($documents as $id_to_check => $post_value)
-							{
-								$obj_to_check = $resources[$id_to_check];
-								$shared_path_part = substr($obj_to_check->path,0,strlen($obj->path));
-								if($id_to_check != $id && $obj->path == $shared_path_part)
+					case RESOURCE_LINKCATEGORY :
+					case RESOURCE_FORUMCATEGORY :
+					case RESOURCE_FORUMPOST :
+					case RESOURCE_FORUMTOPIC :
+					case RESOURCE_QUIZQUESTION :
+					case RESOURCE_DOCUMENT:
+						// Mark folders to import which are not selected by the user to import,
+						// but in which a document was selected.
+						$documents = $_POST['resource'][RESOURCE_DOCUMENT];
+						if (is_array($resources))						
+							foreach($resources as $id => $obj) {							
+								if( $obj->file_type == 'folder' && ! isset($_POST['resource'][RESOURCE_DOCUMENT][$id]) && is_array($documents)) {								
+									foreach($documents as $id_to_check => $post_value) {									
+										$obj_to_check = $resources[$id_to_check];
+										$shared_path_part = substr($obj_to_check->path,0,strlen($obj->path));
+										if($id_to_check != $id && $obj->path == $shared_path_part) {
+											$_POST['resource'][RESOURCE_DOCUMENT][$id] = 1;	
+											break;
+										}	
+									}	
+								}						
+							}						
+					default :		
+						if (is_array($resources)) {							 			
+							foreach ($resources as $id => $obj) {
+								$resource_is_used_elsewhere = $course->is_linked_resource($obj);
+								// check if document is in a quiz (audio/video)
+								if( $type == RESOURCE_DOCUMENT && $course->has_resources(RESOURCE_QUIZ))
 								{
-									$_POST['resource'][RESOURCE_DOCUMENT][$id] = 1;	
-									break;
-								}	
-							}	
-						}
-					}
-				default :
-					foreach ($resources as $id => $obj)
-					{
-						$resource_is_used_elsewhere = $course->is_linked_resource($obj);
-						// check if document is in a quiz (audio/video)
-						if( $type == RESOURCE_DOCUMENT && $course->has_resources(RESOURCE_QUIZ))
-						{
-							foreach($course->resources[RESOURCE_QUIZ] as $qid => $quiz)
-							{
-								if($quiz->media == $id)
+									foreach($course->resources[RESOURCE_QUIZ] as $qid => $quiz)
+									{
+										if($quiz->media == $id)
+										{
+											$resource_is_used_elsewhere = true;	
+										}	
+									}
+								}
+								if (!isset ($_POST['resource'][$type][$id]) && !$resource_is_used_elsewhere)
 								{
-									$resource_is_used_elsewhere = true;	
-								}	
+									unset ($course->resources[$type][$id]);
+								}
 							}
-						}
-						if (!isset ($_POST['resource'][$type][$id]) && !$resource_is_used_elsewhere)
-						{
-							unset ($course->resources[$type][$id]);
-						}
-					}
+						}											
+				}
 			}
 		}
-		return $course;
+		return $course;		
 	}
 }
 ?>

+ 13 - 27
main/coursecopy/copy_course.php

@@ -1,5 +1,5 @@
 <?php
-// $Id: copy_course.php 18925 2009-03-10 14:09:33Z ndieschburg $
+// $Id: copy_course.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ============================================================================== 
 	Dokeos - elearning and course management software
@@ -67,14 +67,10 @@ Display::display_header($nameTools);
 ==============================================================================
 */ 
 // If a CourseSelectForm is posted or we should copy all resources, then copy them
-if ((isset ($_POST['action']) && $_POST['action'] == 'course_select_form') || (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy'))
-{
-	if (isset ($_POST['action']) && $_POST['action'] == 'course_select_form')
-	{
-		$course = CourseSelectForm :: get_posted_course();
-	}
-	else
-	{
+if ((isset ($_POST['action']) && $_POST['action'] == 'course_select_form') || (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')) {
+	if (isset ($_POST['action']) && $_POST['action'] == 'course_select_form') {
+		$course = CourseSelectForm :: get_posted_course('copy_course');		
+	} else {
 		$cb = new CourseBuilder();
 		$course = $cb->build();
 	}
@@ -82,10 +78,8 @@ if ((isset ($_POST['action']) && $_POST['action'] == 'course_select_form') || (i
 	$cr->set_file_option($_POST['same_file_name_option']);
 	$cr->restore($_POST['destination_course']);
 	Display::display_normal_message(get_lang('CopyFinished'));
-}
-// Else, if a CourseSelectForm is requested, show it
-elseif (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'select_items')
-{
+} elseif (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
+	// Else, if a CourseSelectForm is requested, show it
 	Display::display_normal_message(get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'));
 	$cb = new CourseBuilder();
 	$course = $cb->build();
@@ -93,35 +87,27 @@ elseif (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'select_items'
 	//echo '<br/><br/>';
 	$hidden_fields['same_file_name_option'] = $_POST['same_file_name_option'];
 	$hidden_fields['destination_course'] = $_POST['destination_course'];
-	CourseSelectForm :: display_form($course, $hidden_fields);
-}
-// Else, show the default page
-else
-{
+	CourseSelectForm :: display_form($course, $hidden_fields, true);
+} else {	
 	$table_c = Database :: get_main_table(TABLE_MAIN_COURSE);
 	$table_cu = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
 	$user_info = api_get_user_info();
 	$course_info = api_get_course_info();
 	$sql = 'SELECT * FROM '.$table_c.' c, '.$table_cu.' cu WHERE cu.course_code = c.code';
-	if( ! api_is_platform_admin())
-	{
+	if (!api_is_platform_admin()) {
 		$sql .= ' AND cu.status=1 ';
 	}
 	$sql .= ' AND target_course_code IS NULL AND cu.user_id = '.$user_info['user_id'].' AND c.code != '."'".$course_info['sysCode']."'".' ORDER BY title ASC';
 	$res = api_sql_query($sql,__FILE__,__LINE__);
-	if( mysql_num_rows($res) == 0)
-	{
+	if( mysql_num_rows($res) == 0) {
 		Display::display_normal_message(get_lang('NoDestinationCoursesAvailable'));	
-	}
-	else
-	{
+	} else {
 ?>
 	<form method="post" action="copy_course.php">
 	<?php
 	echo get_lang('SelectDestinationCourse');
 	echo ' <select name="destination_course"/>';
-	while ($obj = mysql_fetch_object($res))
-	{
+	while ($obj = mysql_fetch_object($res)) {
 		echo '<option value="'.$obj->code.'">'.$obj->title.'</option>';
 	}
 	echo '</select>';

+ 29 - 54
main/coursecopy/import_backup.php

@@ -1,4 +1,4 @@
-<?php // $Id: import_backup.php 18925 2009-03-10 14:09:33Z ndieschburg $
+<?php // $Id: import_backup.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ============================================================================== 
 	Dokeos - elearning and course management software
@@ -74,105 +74,80 @@ require_once('classes/CourseSelectForm.class.php');
 // Display the tool title
 api_display_tool_title($nameTools);
 
+
 /*
 ==============================================================================
 		MAIN CODE
 ==============================================================================
 */ 
-if(  (isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['import_option']) && $_POST['import_option'] == 'full_backup' ) )
-{
+
+if((isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['import_option']) && $_POST['import_option'] == 'full_backup' )) {
 	$error=false;	
-	if(isset($_POST['action']) && $_POST['action'] == 'course_select_form' )
-	{
+	if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
+		// partial backup here we recover the documents posted
 		$course = CourseSelectForm::get_posted_course();
-	}
-	else
-	{
-		if( $_POST['backup_type'] == 'server')
-		{
+						
+	} else {
+		if( $_POST['backup_type'] == 'server') {
 			$filename = $_POST['backup_server'];	
 			$delete_file = false;
-		}
-		else
-		{
+		} else {
 			if($_FILES['backup']['error']==0){
 				$filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
-				if ($filename === false)
-                {
+				if ($filename === false) {
                 	$error = true;
-                }
-                else
-                {
+                } else                {
                     $delete_file = true;
                 }
-			}
-			else{
+			} else {
 				$error=true;
 			}
 		}
-        if(!$error)
-        {
+        if(!$error) {
+		  // full backup
 		  $course = CourseArchiver::read_course($filename,$delete_file);
         }
 	}
-	if(!$error && $course->has_resources())
-	{
-		$cr = new CourseRestorer($course);
+	
+	if(!$error && $course->has_resources()) {		
+		$cr = new CourseRestorer($course);		
 		$cr->set_file_option($_POST['same_file_name_option']);
 		$cr->restore();
 		Display::display_normal_message(get_lang('ImportFinished').
                 //'<a class="bottom-link" href="../course_home/course_home.php?'.api_get_cidreq().'">&gt;&gt; '.get_lang('CourseHomepage').'</a>',false); // This is not the preferable way to go to the course homepage.
                 '<a class="bottom-link" href="'.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php">&lt;&lt; '.get_lang('CourseHomepage').'</a>',false);
-	}
-	else
-	{
+	} else {
 		if(!$error){
 			Display::display_warning_message(get_lang('NoResourcesInBackupFile').
                 '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
-		}
-		elseif ($filename === false)
-        {
+		} elseif ($filename === false) {
             Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin').
                 '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
-        }
-        else
-        {
+        } else {
 			Display::display_error_message(ucfirst(get_lang('UploadError')).
                 '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
 		}
 	}
 	CourseArchiver::clean_backup_dir();
-}
-elseif (isset($_POST['import_option']) && $_POST['import_option'] == 'select_items')
-{
-	if( $_POST['backup_type'] == 'server')
-	{
+} elseif (isset($_POST['import_option']) && $_POST['import_option'] == 'select_items') {	
+	if( $_POST['backup_type'] == 'server') {
 		$filename = $_POST['backup_server'];
 		$delete_file = false;	
-	}
-	else
-	{
+	} else {
 		$filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
 		$delete_file = true;
 	}
-	$course = CourseArchiver::read_course($filename,$delete_file);
-	if ($course->has_resources() && ($filename !== false))
-	{
+	$course = CourseArchiver::read_course($filename,$delete_file);	
+	if ($course->has_resources() && ($filename !== false)) {
 		CourseSelectForm::display_form($course,array('same_file_name_option'=>$_POST['same_file_name_option']));
-	}
-    elseif ($filename === false)
-    {
+	} elseif ($filename === false) {
     	Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin').
                 '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
-    }
-	else
-	{
+    } else {
 		Display::display_warning_message(get_lang('NoResourcesInBackupFile').	
                 '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
 	}
-}
-else
-{
+} else {
 	$user = api_get_user_info();
 	$backups = CourseArchiver::get_available_backups($is_platformAdmin?null:$user['user_id']);
 	$backups_available = (count($backups)>0);

+ 10 - 22
main/coursecopy/recycle_course.php

@@ -1,4 +1,4 @@
-<?php // $Id: recycle_course.php 18925 2009-03-10 14:09:33Z ndieschburg $
+<?php // $Id: recycle_course.php 19948 2009-04-21 17:27:59Z juliomontoya $
 /*
 ============================================================================== 
 	Dokeos - elearning and course management software
@@ -38,8 +38,7 @@ $language_file = array ('admin','course_info','coursebackup');
 include ('../inc/global.inc.php');
 
 // Check access rights (only teachers are allowed here)
-if( ! api_is_allowed_to_edit())
-{
+if( ! api_is_allowed_to_edit()) {
 	api_not_allowed(true);	
 }
 // section for the tabs
@@ -66,38 +65,27 @@ api_display_tool_title($nameTools);
 		MAIN CODE
 ==============================================================================
 */ 
-if(  (isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['recycle_option']) && $_POST['recycle_option'] == 'full_backup' ) )
-{
-	if(isset($_POST['action']) && $_POST['action'] == 'course_select_form' )
-	{
+if(  (isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['recycle_option']) && $_POST['recycle_option'] == 'full_backup' ) ) {
+	if(isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) {
 		$course = CourseSelectForm::get_posted_course();
-	}
-	else
-	{
+	} else {
 		$cb = new CourseBuilder();
 		$course = $cb->build();	
 	}
 	$cr = new CourseRecycler($course);
 	$cr->recycle();
 	Display::display_confirmation_message(get_lang('RecycleFinished'));
-}
-elseif( isset($_POST['recycle_option']) && $_POST['recycle_option'] == 'select_items')
-{
+} elseif( isset($_POST['recycle_option']) && $_POST['recycle_option'] == 'select_items') {
 	$cb = new CourseBuilder();
 	$course = $cb->build();	
 	CourseSelectForm::display_form($course);
-}
-else
-{
+} else {
 	$cb = new CourseBuilder();
 	$course = $cb->build();
-	if( ! $course->has_resources())
-	{
+	if( ! $course->has_resources()) {
 		echo get_lang('NoResourcesToRecycle');	
-	}	
-	else
-	{
-	Display::display_warning_message(get_lang('RecycleWarning'),false);
+	} else {
+		Display::display_warning_message(get_lang('RecycleWarning'),false);
 	?>
 	<form method="post" action="recycle_course.php">
 	<input type="radio" class="checkbox" id="recycle_option_1" name="recycle_option" value="full_backup" checked="checked"/>