Explorar el Código

When using the promotion copy session tool, a new session is genetared as well as the courses see BT#2048

Julio Montoya hace 14 años
padre
commit
26b5570c4b

+ 3 - 3
main/inc/lib/add_course.lib.inc.php

@@ -2480,8 +2480,8 @@ function register_course($course_sys_code, $course_screen_code, $course_reposito
         $ok_to_register_course = false;
     }
     if (empty($titular)) {
-        $error_msg[] = 'titular is missing';
-        $ok_to_register_course = false;
+        //$error_msg[] = 'titular is missing';
+        //$ok_to_register_course = false;
     }
     if (empty($title)) {
         $error_msg[] = 'title is missing';
@@ -2511,7 +2511,7 @@ function register_course($course_sys_code, $course_screen_code, $course_reposito
         $titular = addslashes($titular);
 
         // Here we must add 2 fields.
-        $sql = "INSERT INTO ".$TABLECOURSE . " SET
+       $sql = "INSERT INTO ".$TABLECOURSE . " SET
                     code = '".Database :: escape_string($course_sys_code) . "',
                     db_name = '".Database :: escape_string($course_db_name) . "',
                     directory = '".Database :: escape_string($course_repository) . "',

+ 137 - 0
main/inc/lib/course.lib.php

@@ -3078,6 +3078,143 @@ class CourseManager {
     
         return $output;
     }
+    
+    /**
+     *
+     
+     * @param	string	source course code
+     * @param 	int		source session id
+     * @param	string	destination course code
+     * @param 	int		destination session id
+     * @return bool	
+     */
+    function copy_course($source_course_code, $source_session_id, $destination_course_code, $destination_session_id) {
+        require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseBuilder.class.php';
+        require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseRestorer.class.php';
+        require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseSelectForm.class.php';
+
+        $course_info = api_get_course_info($source_course_code);
+        
+        if (!empty($course_info)) {
+            $cb = new CourseBuilder('', $course_info);
+		    $course = $cb->build($source_session_id,$source_session_id['code']);		    		
+    		$course_restorer = new CourseRestorer($course);
+    		$course_restorer->restore($destination_course_code, $destination_session_id);
+    		return true;		
+        }
+        return false;
+    }
+    
+    
+    /**
+     * A simpler version of the copy_course, the function creates an empty course
+     * 
+     * @param	string	course title
+     * @param	string	source course code
+     * @param 	int		source session id
+     * @return 	
+     */
+    function copy_course_simple($new_title, $source_course_code, $source_session_id = 0) {
+        $source_course_info = api_get_course_info($source_course_code);        
+        if (!empty($source_course_info)) {
+            $new_course_code = self::generate_nice_next_course_code($source_course_code); 
+            if ($new_course_code) {
+                $new_course_info = self::create_course($new_title, $new_course_code, false);
+                if (!empty($new_course_info['code'])) {                    
+                    $result = self::copy_course($source_course_code, $source_session_id, $new_course_info['code'], 0);
+                    if ($result) {
+                        return $new_course_info;
+                    }                   
+                }
+            }
+        }
+        return false;
+    }
+    
+    
+    
+    /**
+     * Creates a new course code based in given code
+     * 
+     * @param string	wanted code
+     * @example	$wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3
+     * if the course code doest not exist in the DB the same course code will be returned
+     * @return string	wanted unused code
+     */
+	function generate_nice_next_course_code($wanted_code) {
+        require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
+        $course_code_ok = !self::course_code_exists($wanted_code);
+        if (!$course_code_ok) {            
+           $wanted_code = generate_course_code($wanted_code);
+           $table = Database::get_main_table(TABLE_MAIN_COURSE);
+           $wanted_code = Database::escape_string($wanted_code);
+           $sql = "SELECT count(*) as count FROM $table WHERE code LIKE '$wanted_code%'";           
+           $result = Database::query($sql);
+           if (Database::num_rows($result) > 0 ) {
+		       $row = Database::fetch_array($result);
+		       $count = $row['count'] + 1;
+		       $wanted_code = $wanted_code.'_'.$count;
+		       $result = api_get_course_info($wanted_code);
+		       if (empty($result)) {
+		           return $wanted_code;
+		       }
+           }       
+           return false;     
+        }     
+        return $wanted_code;
+    }
+        
+    /**
+     * Creates a course very fast (the function doesn't need a lot of parameters)
+     * 
+     * @param	string	course title
+     * @param	bool	add example content or not
+     * @param	mixed	false if the course was not created, array with the course info
+     * 
+		Coded found in create_course/add_course.php
+		
+     */
+    function create_course($title, $wanted_code  = '', $exemplary_content = false) {
+        require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
+        global $_configuration, $firstExpirationDelay;
+        if (empty($title)) {
+            return false;
+        }        
+        if (empty($wanted_code)) {
+            $wanted_code = $title;    
+            // Check whether the requested course code has already been occupied.       
+            $dbnamelength = strlen($_configuration['db_prefix']);
+            // Ensure the database prefix + database name do not get over 40 characters.
+            $maxlength = 40 - $dbnamelength;
+            $wanted_code = generate_course_code(api_substr($title, 0, $maxlength));            
+        }
+ 
+        // Create the course immediately
+        $keys = define_course_keys($wanted_code, '', $_configuration['db_prefix']);
+
+        if (count($keys)) {
+            $visual_code = $keys['currentCourseCode'];
+            $code        = $keys['currentCourseId'];
+            $db_name     = $keys['currentCourseDbName'];
+            $directory   = $keys['currentCourseRepository'];
+                
+            $code_info   = api_get_course_info($code);
+            if (empty($code_info)) {            
+                $expiration_date = time() + $firstExpirationDelay;
+                prepare_course_repository($directory, $code);
+                update_Db_course($db_name);
+                $pictures_array = fill_course_repository($directory,  $exemplary_content);
+                fill_Db_course($db_name, $directory, $course_language, $pictures_array, $exemplary_content);
+                $course_language = api_get_setting('platformLanguage');
+                $result = register_course($code, $visual_code, $directory, $db_name, '', '', $title, $course_language, api_get_user_id(), $expiration_date);
+                $course_info = api_get_course_info($code);
+                if (!empty($course_info)) {
+                    return $course_info;    
+                }
+            }
+        }
+        return false;            
+    } 
 
     
 } //end class CourseManager

+ 6 - 2
main/inc/lib/database.lib.php

@@ -1338,6 +1338,7 @@ class Database {
     /**
      * Experimental useful database finder 
      * @todo lot of stuff to do here
+     * @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
     */
     
     public static function select($columns = '*' , $table_name,  $conditions = array(), $type_result = 'all', $option = 'ASSOC') {        
@@ -1379,6 +1380,7 @@ class Database {
     
     /**
      * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC'))
+     * @todo known issues, it doesn't work when using LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
      * @param   array   
      * @todo lot of stuff to do here
     */
@@ -1402,9 +1404,11 @@ class Database {
                             $value_array = Database::escape_string($value_array);
                             $clean_values = "'$value_array'";
                         }
-                        if (!empty($condition) && !empty($clean_values)) {    
-                            $condition = str_replace('?','%s', $condition); //we treat everything as string                
+                        if (!empty($condition) && !empty($clean_values)) {
+                            //$condition = str_replace('%','@percentage@', $condition);  
+                            $condition = str_replace('?','%s', $condition); //we treat everything as string
                             $condition = vsprintf($condition, $clean_values);
+                            //$condition = str_replace('@percentage@','%', $condition);  
                             $where_return .= $condition;                            
                         }
                     }

+ 8 - 3
main/inc/lib/promotion.lib.php

@@ -146,12 +146,17 @@ class Promotion extends Model {
         }
         $pid = $this->save($new);
         if ($copy_sessions) {
-            //Now also copy each session of the promotion as a new session and register it inside the promotion
+            /**
+             * When copying a session we do:
+             * 1. Copy a new session from the source
+             * 2. Copy all courses from the session (no user data, no user list)              
+             */
             require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
-            $session_list   = SessionManager::get_all_sessions_by_promotion($id);    
+            $session_list   = SessionManager::get_all_sessions_by_promotion($id); 
+        
             if (!empty($session_list)) {
                 foreach($session_list  as $item) {
-                    $sid = SessionManager::copy_session($item['id'], true, true);
+                    $sid = SessionManager::copy_session($item['id'], true, false, true);
                     if ($sid != 0) {
                         SessionManager::suscribe_sessions_to_promotion($pid,array($sid));
                     }

+ 41 - 9
main/inc/lib/sessionmanager.lib.php

@@ -66,8 +66,7 @@ class SessionManager {
 		$id_session_category = intval($id_session_category);
 		$id_visibility   = intval($id_visibility);
 		$tbl_user		= Database::get_main_table(TABLE_MAIN_USER);
-		$tbl_session	= Database::get_main_table(TABLE_MAIN_SESSION);
-        
+		$tbl_session	= Database::get_main_table(TABLE_MAIN_SESSION);        
     
 		if(is_int($coach_username)) {
 			$id_coach = $coach_username;
@@ -1311,10 +1310,11 @@ class SessionManager {
      * @param   int     Session ID
      * @param   bool    Whether to copy the relationship with courses
      * @param   bool    Whether to copy the relationship with users
+     * @param	bool	New courses will be created
      * @return  int     The new session ID on success, 0 otherwise
      * @todo make sure the extra session fields are copied too
      */
-    public function copy_session($id, $copy_courses=true, $copy_users=true) {
+    public function copy_session($id, $copy_courses=true, $copy_users=true, $create_new_courses = false) {
         $id = (int)$id;
         $s = self::fetch($id);
         $s['year_start']    = substr($s['date_start'],0,4);
@@ -1322,28 +1322,60 @@ class SessionManager {
         $s['day_start']     = substr($s['date_start'],8,2);
         $s['year_end']      = substr($s['date_end'],0,4);
         $s['month_end']     = substr($s['date_end'],5,2);
-        $s['day_end']       = substr($s['date_end'],8,2);
+        $s['day_end']       = substr($s['date_end'],8,2);        
+        $consider_start = true;
+        if ($s['year_start'].'-'.$s['month_start'].'-'.$s['day_start'] == '0000-00-00') {
+            $consider_start = false;
+        }
+        $consider_end = true;
+        if ($s['year_end'].'-'.$s['month_end'].'-'.$s['day_end'] == '0000-00-00') {
+            $consider_end = false;
+        }
+        
         $sid = self::create_session($s['name'].' '.get_lang('Copy'),
              $s['year_start'], $s['month_start'], $s['day_start'],
              $s['year_end'],$s['month_end'],$s['day_end'],
              $s['nb_days_acess_before_beginning'],$s['nb_days_acess_after_end'],
              false,(int)$s['id_coach'], $s['session_category_id'],
-             (int)$s['visibility'],true, true);
+             (int)$s['visibility'],$consider_start, $consider_end);             
         if (!is_numeric($sid)) {
         	return false;
         }
         if ($copy_courses) {
             // Register courses from the original session to the new session
             $courses = self::get_course_list_by_session_id($id);
-            $short_courses = array();
+            
+            $short_courses = $new_short_courses = array();
             if (is_array($courses) && count($courses)>0) {
             	foreach ($courses as $course) {
-            		$short_courses[] = $course['course_code'];
+            		$short_courses[] = $course;
             	}
             }
             $courses = null;
-            $res = self::add_courses_to_session($sid, $short_courses, true);
-            $short_courses = null;
+            //We will copy the current courses of the session to new courses
+            if (!empty($short_courses)) {
+                if ($create_new_courses) {                    
+                    require_once api_get_path(LIBRARY_PATH).'course.lib.php';
+                    //Just in case
+                    if (function_exists('ini_set')) {
+                    	ini_set('memory_limit','256M');
+                    	ini_set('max_execution_time',0);
+                    }                    
+                    foreach($short_courses as $course_data) {
+                        $course_info = CourseManager::copy_course_simple($course_data['title'].' '.get_lang('Copy'), $course_data['course_code'], $id);
+                        if ($course_info) {
+                            $new_short_courses[] = $course_info['code'];
+                        }                             
+                    }                    
+                } else {
+                    foreach($short_courses as $course_data) {
+                         $new_short_courses[] = $course_data['code'];
+                    }
+                }
+                $short_courses = $new_short_courses;
+                $res = self::add_courses_to_session($sid, $short_courses, true);
+                $short_courses = null;
+            }
         }
         if ($copy_users) {
             // Register users from the original session to the new session