示例#1
0
 /**
  * Creates a course
  * @param   array $params columns in the main.course table
  *
  * @return  mixed  false if the course was not created, array with the course info
  */
 public static function create_course($params, $extraFields = array())
 {
     global $_configuration;
     // Check portal limits
     $access_url_id = 1;
     if (api_get_multiple_access_url()) {
         $access_url_id = api_get_current_access_url_id();
     }
     if (isset($_configuration[$access_url_id]) && is_array($_configuration[$access_url_id])) {
         if (isset($_configuration[$access_url_id]['hosting_limit_courses']) && $_configuration[$access_url_id]['hosting_limit_courses'] > 0) {
             $num = self::count_courses($access_url_id);
             if ($num >= $_configuration[$access_url_id]['hosting_limit_courses']) {
                 api_warn_hosting_contact('hosting_limit_courses');
                 return api_set_failure(get_lang('PortalCoursesLimitReached'));
             }
         }
         if (isset($_configuration[$access_url_id]['hosting_limit_active_courses']) && $_configuration[$access_url_id]['hosting_limit_active_courses'] > 0) {
             $num = self::countActiveCourses($access_url_id);
             if ($num >= $_configuration[$access_url_id]['hosting_limit_active_courses']) {
                 api_warn_hosting_contact('hosting_limit_active_courses');
                 return api_set_failure(get_lang('PortalActiveCoursesLimitReached'));
             }
         }
     }
     if (empty($params['title'])) {
         return false;
     }
     if (empty($params['wanted_code'])) {
         $params['wanted_code'] = $params['title'];
         // Check whether the requested course code has already been occupied.
         $params['wanted_code'] = CourseManager::generate_course_code(api_substr($params['title'], 0, self::MAX_COURSE_LENGTH_CODE));
     }
     // Create the course keys
     $keys = AddCourse::define_course_keys($params['wanted_code']);
     $params['exemplary_content'] = isset($params['exemplary_content']) ? $params['exemplary_content'] : false;
     if (count($keys)) {
         $params['code'] = $keys['currentCourseCode'];
         $params['visual_code'] = $keys['currentCourseId'];
         $params['directory'] = $keys['currentCourseRepository'];
         $course_info = api_get_course_info($params['code']);
         if (empty($course_info)) {
             $course_id = AddCourse::register_course($params);
             $course_info = api_get_course_info_by_id($course_id);
             if (!empty($course_info)) {
                 AddCourse::prepare_course_repository($course_info['directory'], $course_info['code']);
                 AddCourse::fill_db_course($course_id, $course_info['directory'], $course_info['course_language'], $params['exemplary_content']);
                 if (api_get_setting('gradebook.gradebook_enable_grade_model') == 'true') {
                     //Create gradebook_category for the new course and add
                     // a gradebook model for the course
                     if (isset($params['gradebook_model_id']) && !empty($params['gradebook_model_id']) && $params['gradebook_model_id'] != '-1') {
                         GradebookUtils::create_default_course_gradebook($course_info['code'], $params['gradebook_model_id']);
                     }
                 }
                 // If parameter defined, copy the contents from a specific
                 // template course into this new course
                 $template = api_get_setting('course.course_creation_use_template');
                 if (!empty($template)) {
                     // Include the necessary libraries to generate a course copy
                     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';
                     // Call the course copy object
                     $originCourse = api_get_course_info_by_id($template);
                     $originCourse['official_code'] = $originCourse['code'];
                     $cb = new CourseBuilder(null, $originCourse);
                     $course = $cb->build(null, $originCourse['code']);
                     $cr = new CourseRestorer($course);
                     $cr->set_file_option();
                     $cr->restore($course_info['id']);
                     //course_info[id] is the course.code value (I know...)
                 }
                 $params['course_code'] = $course_info['code'];
                 $params['item_id'] = $course_info['real_id'];
                 $courseFieldValue = new ExtraFieldValue('course');
                 $courseFieldValue->saveFieldValues($params);
                 return $course_info;
             }
         }
     }
     return false;
 }