/** * stores the changes in a course category * (moving a course to a different course category) * @param int $courseId * @param int Category id * @return bool True if it success */ public function updateCourseCategory($courseId, $newcategory) { $courseId = intval($courseId); $newcategory = intval($newcategory); $current_user = api_get_user_id(); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $max_sort_value = api_max_sort_value($newcategory, $current_user); $sql = "UPDATE {$TABLECOURSUSER} SET\n user_course_cat='" . $newcategory . "',\n sort='" . ($max_sort_value + 1) . "'\n WHERE\n c_id ='" . $courseId . "' AND\n user_id='" . $current_user . "' AND\n relation_type<>" . COURSE_RELATION_TYPE_RRHH; $resultQuery = Database::query($sql); $result = false; if (Database::affected_rows($resultQuery)) { $result = true; } return $result; }
/** * Subscribe a user $user_id to a course defined by $courseCode. * @author Hugues Peeters * @author Roan Embrechts * * @param int $user_id the id of the user * @param string $courseCode the course code * @param int $status (optional) The user's status in the course * @param int The user category in which this subscription will be classified * * @return boolean true if subscription succeeds, boolean false otherwise. * @assert ('', '') === false */ public static function add_user_to_course($user_id, $courseCode, $status = STUDENT, $userCourseCategoryId = 0) { $debug = false; $user_table = Database::get_main_table(TABLE_MAIN_USER); $course_table = Database::get_main_table(TABLE_MAIN_COURSE); $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER); $status = $status == STUDENT || $status == COURSEMANAGER ? $status : STUDENT; if (empty($user_id) || empty($courseCode) || $user_id != strval(intval($user_id))) { return false; } $courseCode = Database::escape_string($courseCode); $courseInfo = api_get_course_info($courseCode); $courseId = $courseInfo['real_id']; // Check in advance whether the user has already been registered on the platform. $sql = "SELECT status FROM " . $user_table . " WHERE user_id = {$user_id} "; if (Database::num_rows(Database::query($sql)) == 0) { if ($debug) { error_log('The user has not been registered to the platform'); } return false; // The user has not been registered to the platform. } // Check whether the user has already been subscribed to this course. $sql = "SELECT * FROM {$course_user_table}\n WHERE\n user_id = {$user_id} AND\n relation_type <> " . COURSE_RELATION_TYPE_RRHH . " AND\n c_id = {$courseId}"; if (Database::num_rows(Database::query($sql)) > 0) { if ($debug) { error_log('The user has been already subscribed to the course'); } return false; // The user has been subscribed to the course. } if (!api_is_course_admin()) { // Check in advance whether subscription is allowed or not for this course. $sql = "SELECT code, visibility FROM {$course_table}\n WHERE id = {$courseId} AND subscribe = '" . SUBSCRIBE_NOT_ALLOWED . "'"; if (Database::num_rows(Database::query($sql)) > 0) { if ($debug) { error_log('Subscription is not allowed for this course'); } return false; // Subscription is not allowed for this course. } } // Ok, subscribe the user. $max_sort = api_max_sort_value('0', $user_id); $params = ['c_id' => $courseId, 'user_id' => $user_id, 'status' => $status, 'sort' => $max_sort + 1, 'user_course_cat' => $userCourseCategoryId]; $insertId = Database::insert($course_user_table, $params); return $insertId; }
/** * Function register_course to create a record in the course table of the main database * @param array Course details (see code for details) * @return int Created course ID * @todo use an array called $params instead of lots of params * @assert (null) === false */ public static function register_course($params) { global $error_msg, $firstExpirationDelay; $title = $params['title']; $code = $params['code']; $visual_code = $params['visual_code']; $directory = $params['directory']; $tutor_name = isset($params['tutor_name']) ? $params['tutor_name'] : null; //$description = $params['description']; $category_code = isset($params['course_category']) ? $params['course_category'] : ''; $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : api_get_setting('language.platform_language'); $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']); $department_name = isset($params['department_name']) ? $params['department_name'] : null; $department_url = isset($params['department_url']) ? $params['department_url'] : null; $disk_quota = isset($params['disk_quota']) ? $params['disk_quota'] : null; if (!isset($params['visibility'])) { $default_course_visibility = api_get_setting('course.courses_default_creation_visibility'); if ($default_course_visibility != '') { $visibility = $default_course_visibility; } else { $visibility = COURSE_VISIBILITY_OPEN_PLATFORM; } } else { $visibility = $params['visibility']; } $subscribe = isset($params['subscribe']) ? intval($params['subscribe']) : ($visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0); $unsubscribe = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0; $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null; $teachers = isset($params['teachers']) ? $params['teachers'] : null; $status = isset($params['status']) ? $params['status'] : null; $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $ok_to_register_course = true; // Check whether all the needed parameters are present. if (empty($code)) { $error_msg[] = 'courseSysCode is missing'; $ok_to_register_course = false; } if (empty($visual_code)) { $error_msg[] = 'courseScreenCode is missing'; $ok_to_register_course = false; } if (empty($directory)) { $error_msg[] = 'courseRepository is missing'; $ok_to_register_course = false; } if (empty($title)) { $error_msg[] = 'title is missing'; $ok_to_register_course = false; } if (empty($expiration_date)) { $expiration_date = api_get_utc_datetime(time() + $firstExpirationDelay); } else { $expiration_date = api_get_utc_datetime($expiration_date); } if ($visibility < 0 || $visibility > 4) { $error_msg[] = 'visibility is invalid'; $ok_to_register_course = false; } if (empty($disk_quota)) { $disk_quota = api_get_setting('document.default_document_quotum'); } $time = api_get_utc_datetime(); if (stripos($department_url, 'http://') === false && stripos($department_url, 'https://') === false) { $department_url = 'http://' . $department_url; } //just in case if ($department_url == 'http://') { $department_url = ''; } $course_id = 0; if ($ok_to_register_course) { // Here we must add 2 fields. $sql = "INSERT INTO " . $TABLECOURSE . " SET\n code = '" . Database::escape_string($code) . "',\n directory = '" . Database::escape_string($directory) . "',\n course_language = '" . Database::escape_string($course_language) . "',\n title = '" . Database::escape_string($title) . "',\n description = '" . self::lang2db(get_lang('CourseDescription')) . "',\n category_code = '" . Database::escape_string($category_code) . "',\n visibility = '" . $visibility . "',\n show_score = '1',\n disk_quota = '" . intval($disk_quota) . "',\n creation_date = '{$time}',\n expiration_date = '" . $expiration_date . "',\n last_edit = '{$time}',\n last_visit = NULL,\n tutor_name = '" . Database::escape_string($tutor_name) . "',\n department_name = '" . Database::escape_string($department_name) . "',\n department_url = '" . Database::escape_string($department_url) . "',\n subscribe = '" . intval($subscribe) . "',\n unsubscribe = '" . intval($unsubscribe) . "',\n visual_code = '" . Database::escape_string($visual_code) . "'"; Database::query($sql); $course_id = Database::insert_id(); if ($course_id) { $sort = api_max_sort_value('0', api_get_user_id()); // Default true $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true; if ($addTeacher) { $i_course_sort = CourseManager::userCourseSort($user_id, $code); if (!empty($user_id)) { $sql = "INSERT INTO " . $TABLECOURSUSER . " SET\n c_id = '" . $course_id . "',\n user_id = '" . intval($user_id) . "',\n status = '1',\n is_tutor = '0',\n sort = '" . $i_course_sort . "',\n user_course_cat = '0'"; Database::query($sql); } } if (!empty($teachers)) { if (!is_array($teachers)) { $teachers = array($teachers); } foreach ($teachers as $key) { //just in case if ($key == $user_id) { continue; } if (empty($key)) { continue; } $sql = "INSERT INTO " . $TABLECOURSUSER . " SET\n c_id = '" . Database::escape_string($course_id) . "',\n user_id = '" . Database::escape_string($key) . "',\n status = '1',\n is_tutor = '0',\n sort = '" . ($sort + 1) . "',\n user_course_cat = '0'"; Database::query($sql); } } // Adding the course to an URL. if (api_is_multiple_url_enabled()) { $url_id = 1; if (api_get_current_access_url_id() != -1) { $url_id = api_get_current_access_url_id(); } UrlManager::add_course_to_url($course_id, $url_id); } else { UrlManager::add_course_to_url($course_id, 1); } // Add event to the system log. $user_id = api_get_user_id(); Event::addEvent(LOG_COURSE_CREATE, LOG_COURSE_CODE, $code, api_get_utc_datetime(), $user_id, $course_id); $send_mail_to_admin = api_get_setting('course.send_email_to_admin_when_create_course'); // @todo Improve code to send to all current portal administrators. if ($send_mail_to_admin == 'true') { $siteName = api_get_setting('platform.site_name'); $recipient_email = api_get_setting('admin.administrator_email'); $recipient_name = api_get_person_name(api_get_setting('admin.administrator_name'), api_get_setting('admin.administrator_surname')); $iname = api_get_setting('platform.institution'); $subject = get_lang('NewCourseCreatedIn') . ' ' . $siteName . ' - ' . $iname; $message = get_lang('Dear') . ' ' . $recipient_name . ",\n\n" . get_lang('MessageOfNewCourseToAdmin') . ' ' . $siteName . ' - ' . $iname . "\n"; $message .= get_lang('CourseName') . ' ' . $title . "\n"; $message .= get_lang('Category') . ' ' . $category_code . "\n"; $message .= get_lang('Tutor') . ' ' . $tutor_name . "\n"; $message .= get_lang('Language') . ' ' . $course_language; $userInfo = api_get_user_info($user_id); $additionalParameters = array('smsType' => SmsPlugin::NEW_COURSE_BEEN_CREATED, 'userId' => $user_id, 'courseName' => $title, 'creatorUsername' => $userInfo['username']); api_mail_html($recipient_name, $recipient_email, $subject, $message, $siteName, $recipient_email, null, null, null, $additionalParameters); } } } return $course_id; }
/** * function register_course to create a record in the course table of the main database * @param string $course_sys_code * @param string $course_screen_code * @param string $course_repository * @param string $course_db_name * @param string $tutor_name * @param string $category * @param string $title complete name of course * @param string $course_language lang for this course * @param string $uid uid of owner * @param integer Expiration date in unix time representation * @param array Optional array of teachers' user ID * @return int 0 * @todo use an array called $params instead of lots of params */ static function register_course($params) { global $error_msg, $firstExpirationDelay; $title = $params['title']; $code = $params['code']; $visual_code = $params['visual_code']; $directory = isset($params['directory']) ? $params['directory'] : null; $tutor_name = isset($params['tutor_name']) ? $params['tutor_name'] : null; $category_code = isset($params['category_code']) ? $params['category_code'] : null; $defaultLanguage = Container::getTranslator()->getLocale(); $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : $defaultLanguage; $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']); $department_name = isset($params['department_name']) ? $params['department_name'] : null; $department_url = isset($params['department_url']) ? $params['department_url'] : null; $disk_quota = isset($params['disk_quota']) ? $params['disk_quota'] : null; if (!isset($params['visibility'])) { $default_course_visibility = api_get_setting('course.courses_default_creation_visibility'); if (isset($default_course_visibility)) { $visibility = $default_course_visibility; } else { $visibility = COURSE_VISIBILITY_OPEN_PLATFORM; } } else { $visibility = $params['visibility']; } $subscribe = isset($params['subscribe']) ? intval($params['subscribe']) : ($visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0); $unsubscribe = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0; $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null; $teachers = isset($params['teachers']) ? $params['teachers'] : null; $status = isset($params['status']) ? $params['status'] : null; $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $ok_to_register_course = true; // Check whether all the needed parameters are present. if (empty($code)) { $error_msg[] = 'courseSysCode is missing'; $ok_to_register_course = false; } if (empty($visual_code)) { $error_msg[] = 'courseScreenCode is missing'; $ok_to_register_course = false; } if (empty($directory)) { $error_msg[] = 'courseRepository is missing'; $ok_to_register_course = false; } if (empty($title)) { $error_msg[] = 'title is missing'; $ok_to_register_course = false; } if (empty($expiration_date)) { $expiration_date = api_get_utc_datetime(time() + $firstExpirationDelay); } else { $expiration_date = api_get_utc_datetime($expiration_date); } if ($visibility < 0 || $visibility > 3) { $error_msg[] = 'visibility is invalid'; $ok_to_register_course = false; } if (empty($disk_quota)) { $disk_quota = api_get_setting('document.default_document_quotum'); } $time = api_get_utc_datetime(); if (stripos($department_url, 'http://') === false && stripos($department_url, 'https://') === false) { $department_url = 'http://' . $department_url; } //just in case if ($department_url == 'http://') { $department_url = ''; } $course_id = 0; if ($ok_to_register_course) { /** @var Course $course */ $course = self::getCourseManager()->create(); $course->setCode($code)->setDirectory($directory)->setCourseLanguage($course_language)->setTitle($title)->setDescription(get_lang('CourseDescription'))->setCategoryCode($category_code)->setVisibility($visibility)->setShowScore(1)->setDiskQuota($disk_quota)->setCreationDate(new \DateTime())->setExpirationDate(new \DateTime($expiration_date))->setDepartmentName($department_name)->setDepartmentUrl($department_url)->setSubscribe($subscribe)->setUnsubscribe($unsubscribe)->setVisualCode($visual_code); self::getCourseManager()->save($course, true); $course_id = $course->getId(); /*// Here we must add 2 fields. $sql = "INSERT INTO ".$TABLECOURSE . " SET code = '".Database :: escape_string($code) . "', directory = '".Database :: escape_string($directory) . "', course_language = '".Database :: escape_string($course_language) . "', title = '".Database :: escape_string($title) . "', description = '".Database::escape_string(get_lang('CourseDescription')) . "', category_code = '".Database :: escape_string($category_code) . "', visibility = '".$visibility . "', show_score = '1', disk_quota = '".intval($disk_quota) . "', creation_date = '$time', expiration_date = '".$expiration_date . "', last_edit = '$time', last_visit = NULL, tutor_name = '".Database :: escape_string($tutor_name) . "', department_name = '".Database :: escape_string($department_name) . "', department_url = '".Database :: escape_string($department_url) . "', subscribe = '".intval($subscribe) . "', unsubscribe = '".intval($unsubscribe) . "', visual_code = '".Database :: escape_string($visual_code) . "'"; Database::query($sql); $course_id = Database::insert_id();*/ //$course->addUsers() if ($course_id) { $settingsManager = Container::getCourseSettingsManager(); $schemas = $settingsManager->getSchemas(); $schemas = array_keys($schemas); /** * @var string $key * @var \Sylius\Bundle\SettingsBundle\Schema\SchemaInterface $schema */ foreach ($schemas as $schema) { $settings = $settingsManager->loadSettings($schema); $settingsManager->setCourse($course); $settingsManager->saveSettings($schema, $settings); } $sort = api_max_sort_value('0', api_get_user_id()); $i_course_sort = CourseManager::userCourseSort($user_id, $code); if (!empty($user_id)) { $sql = "INSERT INTO " . $TABLECOURSUSER . " SET\n c_id = '" . Database::escape_string($course_id) . "',\n user_id = '" . intval($user_id) . "',\n status = '1',\n tutor_id = '0',\n sort = '" . $i_course_sort . "',\n user_course_cat = '0'"; Database::query($sql); } if (!empty($teachers)) { if (!is_array($teachers)) { $teachers = array($teachers); } foreach ($teachers as $key) { //just in case if ($key == $user_id) { continue; } if (empty($key)) { continue; } $sql = "INSERT INTO " . $TABLECOURSUSER . " SET\n c_id = '" . Database::escape_string($course_id) . "',\n user_id = '" . Database::escape_string($key) . "',\n status = '1',\n role = '',\n tutor_id = '0',\n sort = '" . ($sort + 1) . "',\n user_course_cat = '0'"; Database::query($sql); } } // Adding the course to an URL if (api_is_multiple_url_enabled()) { $url_id = 1; if (api_get_current_access_url_id() != -1) { $url_id = api_get_current_access_url_id(); } UrlManager::add_course_to_url($course_id, $url_id); } else { UrlManager::add_course_to_url($course_id, 1); } // Add event to the system log. $user_id = api_get_user_id(); Event::addEvent(LOG_COURSE_CREATE, LOG_COURSE_CODE, $code, api_get_utc_datetime(), $user_id, $code); $send_mail_to_admin = api_get_setting('course.send_email_to_admin_when_create_course'); // @todo Improve code to send to all current portal administrators. if ($send_mail_to_admin == 'true') { $siteName = api_get_setting('platform.site_name'); $recipient_email = api_get_setting('platform.administrator_email'); $recipient_name = api_get_person_name(api_get_setting('platform.administrator_name'), api_get_setting('platform.administrator_surname')); $iname = api_get_setting('platform.institution'); $subject = get_lang('NewCourseCreatedIn') . ' ' . $siteName . ' - ' . $iname; $body = get_lang('Dear') . ' ' . $recipient_name . ",\n\n" . get_lang('MessageOfNewCourseToAdmin') . ' ' . $siteName . ' - ' . $iname . "\n"; $body .= get_lang('CourseName') . ' ' . $title . "\n"; $body .= get_lang('Category') . ' ' . $category_code . "\n"; $body .= get_lang('Tutor') . ' ' . $tutor_name . "\n"; $body .= get_lang('Language') . ' ' . $course_language; //api_mail_html($recipient_name, $recipient_email, $subject, $message, $siteName, $recipient_email); $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($recipient_email)->setTo($recipient_email)->setBody(Container::getTemplate()->render('ChamiloCoreBundle:Mailer:Course/new_course.html.twig', array('recipient_name' => $recipient_name, 'sitename' => $siteName, 'institution' => $iname, 'course_name' => $title, 'category' => $category_code, 'tutor' => $tutor_name, 'language' => $course_language))); Container::getMailer()->send($message); } } } return $course_id; }
/** * stores the changes in a course category (moving a course to a different course category) * @param int Course id * @param int Category id * @return bool True if it success */ public function store_changecoursecategory($courseId, $newcategory) { $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $courseId = Database::escape_string($courseId); $newcategory = intval($newcategory); $current_user = api_get_user_id(); $max_sort_value = api_max_sort_value($newcategory, $current_user); // max_sort_value($newcategory); $result = Database::query("UPDATE {$TABLECOURSUSER} SET user_course_cat='" . $newcategory . "', sort='" . ($max_sort_value + 1) . "'\n WHERE c_id ='" . $courseId . "' AND user_id='" . $current_user . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . ""); if (Database::affected_rows($result)) { $result = true; } return $result; }
/** * Subscribe a user $user_id to a course $course_code. * @author Hugues Peeters * @author Roan Embrechts * * @param int $user_id the id of the user * @param string $course_code the course code * @param string $status (optional) The user's status in the course * * @return boolean true if subscription succeeds, boolean false otherwise. * @assert ('', '') === false */ public static function add_user_to_course($user_id, $course_code, $status = STUDENT) { $debug = false; $user_table = Database::get_main_table(TABLE_MAIN_USER); $course_table = Database::get_main_table(TABLE_MAIN_COURSE); $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER); $status = $status == STUDENT || $status == COURSEMANAGER ? $status : STUDENT; if (empty($user_id) || empty($course_code) || $user_id != strval(intval($user_id))) { return false; } $course_code = Database::escape_string($course_code); // Check in advance whether the user has already been registered on the platform. $sql = "SELECT status FROM " . $user_table . " WHERE user_id = '{$user_id}' "; if (Database::num_rows(Database::query($sql)) == 0) { if ($debug) { error_log('The user has not been registered to the platform'); } return false; // The user has not been registered to the platform. } // Check whether the user has already been subscribed to this course. $sql = "SELECT * FROM {$course_user_table}\n WHERE\n user_id = '{$user_id}' AND\n relation_type<>" . COURSE_RELATION_TYPE_RRHH . " AND\n course_code = '{$course_code}'"; if (Database::num_rows(Database::query($sql)) > 0) { if ($debug) { error_log('The user has been already subscribed to the course'); } return false; // The user has been subscribed to the course. } // Check in advance whether subscription is allowed or not for this course. $sql = "SELECT code, visibility FROM {$course_table}\n WHERE code = '{$course_code}' AND subscribe = '" . SUBSCRIBE_NOT_ALLOWED . "'"; if (Database::num_rows(Database::query($sql)) > 0) { if ($debug) { error_log('Subscription is not allowed for this course'); } return false; // Subscription is not allowed for this course. } // Ok, subscribe the user. $max_sort = api_max_sort_value('0', $user_id); return (bool) Database::query("INSERT INTO " . $course_user_table . "\n SET course_code = '{$course_code}',\n user_id = '{$user_id}',\n status = '" . $status . "',\n sort = '" . ($max_sort + 1) . "'"); }