/**
  * Copies a session with the same data to a new session.
  * The new copy is not assigned to the same promotion. @see suscribe_sessions_to_promotions() for that
  * @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
  * @param   bool    Whether to set exercises and learning paths in the new session to invisible by default
  * @return  int     The new session ID on success, 0 otherwise
  * @todo make sure the extra session fields are copied too
  */
 public static function copy($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false)
 {
     $id = intval($id);
     $s = self::fetch($id);
     // Check all dates before copying
     // Get timestamp for now in UTC - see http://php.net/manual/es/function.time.php#117251
     $now = time() - date('Z');
     // Timestamp in one month
     $inOneMonth = $now + 30 * 24 * 3600;
     $inOneMonth = api_get_local_time($inOneMonth);
     if (api_strtotime($s['access_start_date']) < $now) {
         $s['access_start_date'] = api_get_local_time($now);
     }
     if (api_strtotime($s['display_start_date']) < $now) {
         $s['display_start_date'] = api_get_local_time($now);
     }
     if (api_strtotime($s['coach_access_start_date']) < $now) {
         $s['coach_access_start_date'] = api_get_local_time($now);
     }
     if (api_strtotime($s['access_end_date']) < $now) {
         $s['access_end_date'] = $inOneMonth;
     }
     if (api_strtotime($s['display_end_date']) < $now) {
         $s['display_end_date'] = $inOneMonth;
     }
     if (api_strtotime($s['coach_access_end_date']) < $now) {
         $s['coach_access_end_date'] = $inOneMonth;
     }
     // Now try to create the session
     $sid = self::create_session($s['name'] . ' ' . get_lang('CopyLabelSuffix'), $s['access_start_date'], $s['access_end_date'], $s['display_start_date'], $s['display_end_date'], $s['coach_access_start_date'], $s['coach_access_end_date'], (int) $s['id_coach'], $s['session_category_id'], (int) $s['visibility'], true);
     if (!is_numeric($sid) || empty($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 = $new_short_courses = array();
         if (is_array($courses) && count($courses) > 0) {
             foreach ($courses as $course) {
                 $short_courses[] = $course;
             }
         }
         $courses = null;
         //We will copy the current courses of the session to new courses
         if (!empty($short_courses)) {
             if ($create_new_courses) {
                 //Just in case
                 if (function_exists('ini_set')) {
                     api_set_memory_limit('256M');
                     ini_set('max_execution_time', 0);
                 }
                 $params = array();
                 $params['skip_lp_dates'] = true;
                 foreach ($short_courses as $course_data) {
                     $course_info = CourseManager::copy_course_simple($course_data['title'] . ' ' . get_lang('CopyLabelSuffix'), $course_data['course_code'], $id, $sid, $params);
                     if ($course_info) {
                         //By default new elements are invisible
                         if ($set_exercises_lp_invisible) {
                             $list = new LearnpathList('', $course_info['code'], $sid);
                             $flat_list = $list->get_flat_list();
                             if (!empty($flat_list)) {
                                 foreach ($flat_list as $lp_id => $data) {
                                     api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0, 0, 0, 0, $sid);
                                 }
                             }
                             $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST);
                             $course_id = $course_info['real_id'];
                             //@todo check this query
                             $sql = "UPDATE {$quiz_table} SET active = 0 WHERE c_id = {$course_id} AND session_id = {$sid}";
                             $result = Database::query($sql);
                         }
                         $new_short_courses[] = $course_info['real_id'];
                     }
                 }
             } else {
                 foreach ($short_courses as $course_data) {
                     $new_short_courses[] = $course_data['id'];
                 }
             }
             $short_courses = $new_short_courses;
             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
         $users = self::get_users_by_session($id);
         $short_users = array();
         if (is_array($users) && count($users) > 0) {
             foreach ($users as $user) {
                 $short_users[] = $user['user_id'];
             }
         }
         $users = null;
         //Subscribing in read only mode
         self::suscribe_users_to_session($sid, $short_users, SESSION_VISIBLE_READ_ONLY, true);
         $short_users = null;
     }
     return $sid;
 }
 /**
  * @param $id
  * @param $copy_courses
  * @param $copy_users
  * @param $create_new_courses
  * @param $set_exercises_lp_invisible
  * @return int
  */
 public static function copy_session($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false)
 {
     $id = intval($id);
     $params = self::fetch($id);
     $params['name'] = $params['name'] . ' ' . get_lang('CopyLabelSuffix');
     $sid = self::add($params);
     if (!is_numeric($sid) || empty($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 = $new_short_courses = array();
         if (is_array($courses) && count($courses) > 0) {
             foreach ($courses as $course) {
                 $short_courses[] = $course;
             }
         }
         $courses = null;
         //We will copy the current courses of the session to new courses
         if (!empty($short_courses)) {
             if ($create_new_courses) {
                 //Just in case
                 if (function_exists('ini_set')) {
                     api_set_memory_limit('256M');
                     ini_set('max_execution_time', 0);
                 }
                 $params = array();
                 $params['skip_lp_dates'] = true;
                 foreach ($short_courses as $course_data) {
                     $course_info = CourseManager::copy_course_simple($course_data['title'] . ' ' . get_lang('CopyLabelSuffix'), $course_data['course_code'], $id, $sid, $params);
                     if ($course_info) {
                         //By default new elements are invisible
                         if ($set_exercises_lp_invisible) {
                             $list = new LearnpathList('', $course_info['code'], $sid);
                             $flat_list = $list->get_flat_list();
                             if (!empty($flat_list)) {
                                 foreach ($flat_list as $lp_id => $data) {
                                     api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0, 0, 0, 0, $sid);
                                     api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0, 0, 0, 0);
                                 }
                             }
                             $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST);
                             $course_id = $course_info['real_id'];
                             //@todo check this query
                             //Disabling quiz items
                             $sql = "UPDATE {$quiz_table} SET active = 0 WHERE c_id = {$course_id} ";
                             Database::query($sql);
                         }
                         $new_short_courses[] = $course_info['code'];
                     }
                 }
             } else {
                 foreach ($short_courses as $course_data) {
                     $new_short_courses[] = $course_data['id'];
                 }
             }
             $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
         $users = self::get_users_by_session($id);
         $short_users = array();
         if (is_array($users) && count($users) > 0) {
             foreach ($users as $user) {
                 $short_users[] = $user['user_id'];
             }
         }
         $users = null;
         //Subscribing in read only mode
         $res = self::suscribe_users_to_session($sid, $short_users, SESSION_VISIBLE_READ_ONLY, true, false);
         $short_users = null;
     }
     return $sid;
 }
    /**
     * Copies a session with the same data to a new session.
     * The new copy is not assigned to the same promotion. @see suscribe_sessions_to_promotions() for that
     * @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 static function copy_session($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false)
    {
        $id = intval($id);
        $s = self::fetch($id);
        $s['year_start'] = substr($s['date_start'], 0, 4);
        $s['month_start'] = substr($s['date_start'], 5, 2);
        $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);
        $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('CopyLabelSuffix'),
            $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'],
            $consider_start,
            $consider_end,
            true
        );

        if (!is_numeric($sid) || empty($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 = $new_short_courses = array();
            if (is_array($courses) && count($courses) > 0) {
                foreach ($courses as $course) {
                    $short_courses[] = $course;
                }
            }

            $courses = null;

            //We will copy the current courses of the session to new courses
            if (!empty($short_courses)) {
                if ($create_new_courses) {
                    //Just in case
                    if (function_exists('ini_set')) {
                        api_set_memory_limit('256M');
                        ini_set('max_execution_time', 0);
                    }
                    $params = array();
                    $params['skip_lp_dates'] = true;

                    foreach ($short_courses as $course_data) {
                        $course_info = CourseManager::copy_course_simple($course_data['title'] . ' ' . get_lang('CopyLabelSuffix'), $course_data['course_code'], $id, $sid, $params);
                        if ($course_info) {
                            //By default new elements are invisible
                            if ($set_exercises_lp_invisible) {
                                require_once api_get_path(SYS_CODE_PATH) . 'newscorm/learnpathList.class.php';
                                $list = new LearnpathList('', $course_info['code'], $sid);
                                $flat_list = $list->get_flat_list();
                                if (!empty($flat_list)) {
                                    foreach ($flat_list as $lp_id => $data) {
                                        api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0, 0, 0, 0, $sid);
                                        api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0, 0, 0, 0);
                                    }
                                }
                                $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST);
                                $course_id = $course_info['real_id'];
                                //@todo check this query
                                $sql = "UPDATE $quiz_table SET active = 0 WHERE c_id = $course_id ";
                                $result = Database::query($sql);
                            }
                            $new_short_courses[] = $course_info['code'];
                        }
                    }
                } else {
                    foreach ($short_courses as $course_data) {
                        $new_short_courses[] = $course_data['code'];
                    }
                }

                $short_courses = $new_short_courses;
                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
            $users = self::get_users_by_session($id);
            $short_users = array();
            if (is_array($users) && count($users) > 0) {
                foreach ($users as $user) {
                    $short_users[] = $user['user_id'];
                }
            }
            $users = null;
            //Subscribing in read only mode
            self::suscribe_users_to_session($sid, $short_users, SESSION_VISIBLE_READ_ONLY, true, false);
            $short_users = null;
        }
        return $sid;
    }