/** * insert or update course data * * @return boolean success */ public function save() { if (empty($this->courseId)) { // Insert $keys = define_course_keys($this->officialCode, '', get_conf('dbNamePrefix')); $courseSysCode = trim($keys['currentCourseId']); $courseDbName = trim($keys['currentCourseDbName']); $courseDirectory = trim($keys['currentCourseRepository']); if (empty($courseSysCode) || empty($courseDbName) || empty($courseDirectory)) { throw new Exception("Error missing data for course {$this->officialCode}"); } if (!$this->useExpirationDate) { $this->expirationDate = 'NULL'; } // Session courses are created without categories links: // so we duplicate the source course's categories links /*if ( !is_null($this->sourceCourseId) && !empty($this->sourceCourseId) ) { $sourceCourse = new claroCourse(); $sourceCourse->load(claroCourse::getCodeFromId($this->sourceCourseId)); $this->categories = $sourceCourse->categories; }*/ if (!is_null($this->sourceCourseId) && !empty($this->sourceCourseId)) { $sourceCourse = new claroCourse(); $sourceCourse->load(claroCourse::getCodeFromId($this->sourceCourseId)); if ($sourceCourse->sourceCourseId) { throw new Exception('You cannot create a course session from another course session'); } } if (prepare_course_repository($courseDirectory, $courseSysCode) && register_course($courseSysCode, $this->officialCode, $this->sourceCourseId, $courseDirectory, $courseDbName, $this->titular, $this->email, $this->categories, $this->title, $this->language, $GLOBALS['_uid'], $this->access, $this->registration, $this->registrationKey, $this->visibility, $this->departmentName, $this->extLinkUrl, $this->publicationDate, $this->expirationDate, $this->status, $this->userLimit) && install_course_database($courseDbName) && install_course_tools($courseDbName, $this->language, $courseDirectory)) { $courseObj = new Claro_Course($courseSysCode); $courseObj->load(); $courseRegistration = new Claro_CourseUserRegistration(AuthProfileManager::getUserAuthProfile($GLOBALS['_uid']), $courseObj, null, null); $courseRegistration->ignoreRegistrationKeyCheck(); $courseRegistration->ignoreCategoryRegistrationCheck(); $courseRegistration->setCourseAdmin(); $courseRegistration->setCourseTutor(); $courseRegistration->forceSuperUser(); if ($courseRegistration->addUser()) { // Set course id $this->courseId = $courseSysCode; // Notify event manager $args['courseSysCode'] = $courseSysCode; $args['courseDbName'] = $courseDbName; $args['courseDirectory'] = $courseDirectory; $args['courseCategory'] = $this->categories; $GLOBALS['eventNotifier']->notifyEvent("course_created", $args); return true; } else { $this->backlog->failure($courseRegistration->getErrorMessage()); return false; } } else { $lastFailure = claro_failure::get_last_failure(); $this->backlog->failure('Error : ' . $lastFailure); return false; } } else { // Update $tbl_mdb_names = claro_sql_get_main_tbl(); $tbl_course = $tbl_mdb_names['course']; $tbl_cdb_names = claro_sql_get_course_tbl(); $tbl_course_properties = $tbl_cdb_names['course_properties']; if (!$this->useExpirationDate) { $this->expirationDate = null; } $sqlExpirationDate = is_null($this->expirationDate) ? 'NULL' : 'FROM_UNIXTIME(' . claro_sql_escape($this->expirationDate) . ')'; $sqlCreationDate = is_null($this->publicationDate) ? 'NULL' : 'FROM_UNIXTIME(' . claro_sql_escape($this->publicationDate) . ')'; $sql = "UPDATE `" . $tbl_course . "`\n SET `intitule` = '" . claro_sql_escape($this->title) . "',\n `titulaires` = '" . claro_sql_escape($this->titular) . "',\n `administrativeNumber` = '" . claro_sql_escape($this->officialCode) . "',\n `language` = '" . claro_sql_escape($this->language) . "',\n `extLinkName` = '" . claro_sql_escape($this->departmentName) . "',\n `extLinkUrl` = '" . claro_sql_escape($this->extLinkUrl) . "',\n `email` = '" . claro_sql_escape($this->email) . "',\n `visibility` = '" . ($this->visibility ? 'visible' : 'invisible') . "',\n `access` = '" . claro_sql_escape($this->access) . "',\n `registration` = '" . claro_sql_escape($this->registration) . "',\n `registrationKey` = '" . claro_sql_escape($this->registrationKey) . "',\n `lastEdit` = NOW(),\n `creationDate` = " . $sqlCreationDate . ",\n `expirationDate` = " . $sqlExpirationDate . ",\n `status` = '" . claro_sql_escape($this->status) . "',\n `userLimit` = '" . (int) $this->userLimit . "'\n WHERE code='" . claro_sql_escape($this->courseId) . "'"; // Handle categories // 1/ Remove all links in database $this->unlinkCategories(); // 2/ Link new categories selection $this->linkCategories($this->categories); // If it's a source course, do the same for all its session courses if ($this->isSourceCourse) { $sql2 = "SELECT cours_id FROM `" . $tbl_course . "`\n WHERE sourceCourseId = " . $this->id; $sessionCourses = claro_sql_query_fetch_all_rows($sql2); foreach ($sessionCourses as $sessionCourse) { unlink_course_categories($sessionCourse['cours_id']); link_course_categories($sessionCourse['cours_id'], $this->categories); } } return claro_sql_query($sql); } }
/** * This function creates a virtual course. * It assumes all parameters have been checked and are not empty. * It checks wether a course with the $wanted_course_code already exists. * * Users of this library should consider this function private, * please call attempt_create_virtual_course instead of this one. * * note: The virtual course 'owner' id (the first course admin) is set to the CURRENT user id. * @param string Course code * @param string Course title * @param string Wanted course code * @param string Course language * @param string Course category * @return true if the course creation succeeded, false otherwise * @todo research: expiration date of a course * @deprecated virtual course feature is not supported */ public static function create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category) { global $firstExpirationDelay; $user_id = api_get_user_id(); $real_course_info = Database::get_course_info($real_course_code); $real_course_code = $real_course_info['system_code']; //check: virtual course creation fails if another course has the same //code, real or fake. if (self::course_code_exists($wanted_course_code)) { Display::display_error_message($wanted_course_code . ' - ' . get_lang('CourseCodeAlreadyExists')); return false; } //add data to course table, course_rel_user $course_sys_code = $wanted_course_code; $course_screen_code = $wanted_course_code; $course_repository = $real_course_info['directory']; $course_db_name = $real_course_info['db_name']; $responsible_teacher = $real_course_info['tutor_name']; $faculty_shortname = $course_category; // $course_title = $course_title; // $course_language = $course_language; $teacher_id = $user_id; //HACK ---------------------------------------------------------------- $expiration_date = time() + $firstExpirationDelay; //END HACK ------------------------------------------------------------ register_course($course_sys_code, $course_screen_code, $course_repository, $course_db_name, $responsible_teacher, $faculty_shortname, $course_title, $course_language, $teacher_id, $expiration_date); //above was the normal course creation table update call, //now one more thing: fill in the target_course_code field Database::query("UPDATE " . Database::get_main_table(TABLE_MAIN_COURSE) . " SET target_course_code = '{$real_course_code}'\n WHERE code = '" . Database::escape_string($course_sys_code) . "' LIMIT 1 "); return true; }