Course: Use constants inside the Course entity instead of api lib

Refactor course edit/course add to load visibility from a function
pull/3904/head
Julio Montoya 4 years ago
parent 4919cdf0e0
commit 49dfe38186
  1. 13
      public/main/admin/course_add.php
  2. 8
      public/main/admin/course_edit.php
  3. 4
      public/main/inc/lib/add_course.lib.inc.php
  4. 20
      public/main/inc/lib/api.lib.php
  5. 75
      public/main/inc/lib/course.lib.php

@ -2,6 +2,8 @@
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Course;
$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
$this_section = SECTION_PLATFORM_ADMIN;
@ -129,14 +131,7 @@ if ('true' === api_get_setting('teacher_can_select_course_template')) {
$form->addElement('checkbox', 'exemplary_content', '', get_lang('Fill with demo content'));
$group = [];
$group[] = $form->createElement('radio', 'visibility', get_lang('Course access'), get_lang('Public - access allowed for the whole world'), COURSE_VISIBILITY_OPEN_WORLD);
$group[] = $form->createElement('radio', 'visibility', null, get_lang(' Open - access allowed for users registered on the platform'), COURSE_VISIBILITY_OPEN_PLATFORM);
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Private access (access authorized to group members only)'), COURSE_VISIBILITY_REGISTERED);
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Closed - the course is only accessible to the teachers'), COURSE_VISIBILITY_CLOSED);
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Hidden - Completely hidden to all users except the administrators'), COURSE_VISIBILITY_HIDDEN);
$form->addGroup($group, '', get_lang('Course access'));
CourseManager::addVisibilityOptions($form);
$group = [];
$group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
@ -180,7 +175,7 @@ $default_course_visibility = api_get_setting('courses_default_creation_visibilit
if (isset($default_course_visibility)) {
$values['visibility'] = api_get_setting('courses_default_creation_visibility');
} else {
$values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
$values['visibility'] = Course::OPEN_PLATFORM;
}
$values['subscribe'] = 1;
$values['unsubscribe'] = 0;

@ -234,13 +234,7 @@ $form->applyFilter('department_url', 'trim');
$form->addSelectLanguage('course_language', get_lang('Course language'));
$group = [];
$group[] = $form->createElement('radio', 'visibility', get_lang('Course access'), get_lang('Public - access allowed for the whole world'), COURSE_VISIBILITY_OPEN_WORLD);
$group[] = $form->createElement('radio', 'visibility', null, get_lang(' Open - access allowed for users registered on the platform'), COURSE_VISIBILITY_OPEN_PLATFORM);
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Private access (access authorized to group members only)'), COURSE_VISIBILITY_REGISTERED);
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Closed - the course is only accessible to the teachers'), COURSE_VISIBILITY_CLOSED);
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Hidden - Completely hidden to all users except the administrators'), COURSE_VISIBILITY_HIDDEN);
$form->addGroup($group, '', get_lang('Course access'));
CourseManager::addVisibilityOptions($form);
$group = [];
$group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);

@ -668,7 +668,7 @@ class AddCourse
if (isset($default_course_visibility)) {
$visibility = $default_course_visibility;
} else {
$visibility = COURSE_VISIBILITY_OPEN_PLATFORM;
$visibility = Course::OPEN_PLATFORM;
}
} else {
$visibility = $params['visibility'];
@ -678,7 +678,7 @@ class AddCourse
if (isset($params['subscribe'])) {
$subscribe = 1 === (int) $params['subscribe'];
} else {
if (COURSE_VISIBILITY_OPEN_PLATFORM == $visibility) {
if (Course::OPEN_PLATFORM == $visibility) {
$subscribe = true;
}
}

@ -1005,29 +1005,29 @@ function api_protect_course_script($print_headers = false, $allow_session_admins
if (isset($course_info) && isset($course_info['visibility'])) {
switch ($course_info['visibility']) {
default:
case COURSE_VISIBILITY_CLOSED:
case Course::CLOSED:
// Completely closed: the course is only accessible to the teachers. - 0
if ($isAllowedInCourse && api_get_user_id() && !api_is_anonymous()) {
$is_visible = true;
}
break;
case COURSE_VISIBILITY_REGISTERED:
case Course::REGISTERED:
// Private - access authorized to course members only - 1
if ($isAllowedInCourse && api_get_user_id() && !api_is_anonymous()) {
$is_visible = true;
}
break;
case COURSE_VISIBILITY_OPEN_PLATFORM:
case Course::OPEN_PLATFORM:
// Open - access allowed for users registered on the platform - 2
if ($isAllowedInCourse && api_get_user_id() && !api_is_anonymous()) {
$is_visible = true;
}
break;
case COURSE_VISIBILITY_OPEN_WORLD:
case Course::OPEN_WORLD:
//Open - access allowed for the whole world - 3
$is_visible = true;
break;
case COURSE_VISIBILITY_HIDDEN:
case Course::HIDDEN:
//Completely closed: the course is only accessible to the teachers. - 0
if (api_is_platform_admin()) {
$is_visible = true;
@ -5437,14 +5437,14 @@ function api_is_course_visible_for_user($userid = null, $cid = null)
}
switch ($visibility) {
case COURSE_VISIBILITY_OPEN_WORLD:
case Course::OPEN_WORLD:
return true;
case COURSE_VISIBILITY_OPEN_PLATFORM:
case Course::OPEN_PLATFORM:
return isset($userid);
case COURSE_VISIBILITY_REGISTERED:
case COURSE_VISIBILITY_CLOSED:
case Course::REGISTERED:
case Course::CLOSED:
return $is_platformAdmin || $is_courseMember || $is_courseAdmin;
case COURSE_VISIBILITY_HIDDEN:
case Course::HIDDEN:
return $is_platformAdmin;
}

@ -578,7 +578,7 @@ class CourseManager
if (in_array($visibility, [
COURSE_VISIBILITY_CLOSED,
//COURSE_VISIBILITY_REGISTERED,
//Course::REGISTERED,
COURSE_VISIBILITY_HIDDEN,
])) {
Display::addFlash(
@ -592,7 +592,7 @@ class CourseManager
}
// Private course can allow auto subscription
if (COURSE_VISIBILITY_REGISTERED === $visibility && false === $course->getSubscribe()) {
if (Course::REGISTERED === $visibility && false === $course->getSubscribe()) {
Display::addFlash(
Display::return_message(
get_lang('Subscribing not allowed'),
@ -4950,7 +4950,7 @@ class CourseManager
}
if ($access_link && in_array('enter', $access_link) ||
COURSE_VISIBILITY_OPEN_WORLD == $course_info['visibility']
Course::OPEN_WORLD == $course_info['visibility']
) {
$my_course['go_to_course_button'] = Display::url(
get_lang('Go to the course').' '.
@ -5118,7 +5118,7 @@ class CourseManager
if ($checkHidePrivate) {
$hidePrivateSetting = api_get_setting('course_catalog_hide_private');
if ('true' === $hidePrivateSetting) {
$visibilityCondition .= " AND $courseTableAlias.visibility <> ".COURSE_VISIBILITY_REGISTERED;
$visibilityCondition .= " AND $courseTableAlias.visibility <> ".Course::REGISTERED;
}
}
if ($hideClosed) {
@ -5182,8 +5182,8 @@ class CourseManager
// Register button
if (!api_is_anonymous($uid) &&
(
(COURSE_VISIBILITY_OPEN_WORLD == $course['visibility'] || COURSE_VISIBILITY_OPEN_PLATFORM == $course['visibility'])
//$course['visibility'] == COURSE_VISIBILITY_REGISTERED && $course['subscribe'] == SUBSCRIBE_ALLOWED
(Course::OPEN_WORLD == $course['visibility'] || Course::OPEN_PLATFORM == $course['visibility'])
//$course['visibility'] == Course::REGISTERED && $course['subscribe'] == SUBSCRIBE_ALLOWED
) &&
SUBSCRIBE_ALLOWED == $course['subscribe'] &&
(!in_array($course['real_id'], $user_courses) || empty($user_courses))
@ -5195,16 +5195,16 @@ class CourseManager
// Go To Course button (only if admin, if course public or if student already subscribed)
if ($is_admin ||
COURSE_VISIBILITY_OPEN_WORLD == $course['visibility'] && empty($course['registration_code']) ||
($isLogin && COURSE_VISIBILITY_OPEN_PLATFORM == $course['visibility'] && empty($course['registration_code'])) ||
Course::OPEN_WORLD == $course['visibility'] && empty($course['registration_code']) ||
($isLogin && Course::OPEN_PLATFORM == $course['visibility'] && empty($course['registration_code'])) ||
(in_array($course['real_id'], $user_courses) && COURSE_VISIBILITY_CLOSED != $course['visibility'])
) {
$options[] = 'enter';
}
if ($is_admin ||
COURSE_VISIBILITY_OPEN_WORLD == $course['visibility'] && empty($course['registration_code']) ||
($isLogin && COURSE_VISIBILITY_OPEN_PLATFORM == $course['visibility'] && empty($course['registration_code'])) ||
Course::OPEN_WORLD == $course['visibility'] && empty($course['registration_code']) ||
($isLogin && Course::OPEN_PLATFORM == $course['visibility'] && empty($course['registration_code'])) ||
(in_array($course['real_id'], $user_courses) && COURSE_VISIBILITY_CLOSED != $course['visibility'])
) {
$options[] = 'enter';
@ -6146,7 +6146,7 @@ class CourseManager
if (!empty($course_info)) {
if (in_array(
$course_info['visibility'],
[COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD]
[Course::OPEN_PLATFORM, Course::OPEN_WORLD]
)
) {
if (self::is_user_subscribed_in_course($userId, $course_info['code'])) {
@ -6537,9 +6537,9 @@ class CourseManager
public static function getCountOpenCourses()
{
$visibility = [
COURSE_VISIBILITY_REGISTERED,
COURSE_VISIBILITY_OPEN_PLATFORM,
COURSE_VISIBILITY_OPEN_WORLD,
Course::REGISTERED,
Course::OPEN_PLATFORM,
Course::OPEN_WORLD,
];
$table = Database::get_main_table(TABLE_MAIN_COURSE);
@ -6558,9 +6558,9 @@ class CourseManager
public static function getCountExercisesFromOpenCourse()
{
$visibility = [
COURSE_VISIBILITY_REGISTERED,
COURSE_VISIBILITY_OPEN_PLATFORM,
COURSE_VISIBILITY_OPEN_WORLD,
Course::REGISTERED,
Course::OPEN_PLATFORM,
Course::OPEN_WORLD,
];
$table = Database::get_main_table(TABLE_MAIN_COURSE);
@ -6779,4 +6779,45 @@ class CourseManager
$courseFieldValue = new ExtraFieldValue('course');
$courseFieldValue->saveFieldValues($params);
}
public static function addVisibilityOptions(FormValidator $form): void
{
$group = [];
$group[] = $form->createElement(
'radio',
'visibility',
get_lang('Course access'),
get_lang('Public - access allowed for the whole world'),
Course::OPEN_WORLD
);
$group[] = $form->createElement(
'radio',
'visibility',
null,
get_lang(' Open - access allowed for users registered on the platform'),
Course::OPEN_PLATFORM
);
$group[] = $form->createElement(
'radio',
'visibility',
null,
get_lang('Private access (access authorized to group members only)'),
Course::REGISTERED
);
$group[] = $form->createElement(
'radio',
'visibility',
null,
get_lang('Closed - the course is only accessible to the teachers'),
Course::CLOSED
);
$group[] = $form->createElement(
'radio',
'visibility',
null,
get_lang('Hidden - Completely hidden to all users except the administrators'),
Course::HIDDEN
);
$form->addGroup($group, '', get_lang('Course access'));
}
}

Loading…
Cancel
Save