Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/admin/course_add.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\CourseCategory;
0 ignored issues
show
This use statement conflicts with another class in this namespace, CourseCategory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Chamilo\CoreBundle\Entity\Repository\CourseCategoryRepository;
7
8
$cidReset = true;
9
require_once __DIR__.'/../inc/global.inc.php';
10
$this_section = SECTION_PLATFORM_ADMIN;
11
12
api_protect_admin_script();
13
14
$tool_name = get_lang('AddCourse');
15
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
16
$interbreadcrumb[] = ['url' => 'course_list.php', 'name' => get_lang('CourseList')];
17
18
$em = Database::getManager();
19
/** @var CourseCategoryRepository $courseCategoriesRepo */
20
$courseCategoriesRepo = $em->getRepository('ChamiloCoreBundle:CourseCategory');
21
// Get all possible teachers.
22
$accessUrlId = api_get_current_access_url_id();
23
24
// Build the form.
25
$form = new FormValidator('update_course');
26
$form->addElement('header', $tool_name);
27
28
// Title
29
$form->addText(
30
    'title',
31
    get_lang('Title'),
32
    true,
33
    [
34
        'aria-label' => get_lang('Title'),
35
    ]
36
);
37
$form->applyFilter('title', 'html_filter');
38
$form->applyFilter('title', 'trim');
39
40
// Code
41
if (!api_get_configuration_value('course_creation_form_hide_course_code')) {
42
    $form->addText(
43
        'visual_code',
44
        [
45
            get_lang('CourseCode'),
46
            get_lang('OnlyLettersAndNumbers'),
47
        ],
48
        false,
49
        [
50
            'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE,
51
            'pattern' => '[a-zA-Z0-9]+',
52
            'title' => get_lang('OnlyLettersAndNumbers'),
53
            'id' => 'visual_code',
54
        ]
55
    );
56
57
    $form->applyFilter('visual_code', 'api_strtoupper');
58
    $form->applyFilter('visual_code', 'html_filter');
59
60
    $form->addRule(
61
        'visual_code',
62
        get_lang('Max'),
63
        'maxlength',
64
        CourseManager::MAX_COURSE_LENGTH_CODE
65
    );
66
}
67
68
$countCategories = $courseCategoriesRepo->countAllInAccessUrl(
69
    $accessUrlId,
70
    api_get_configuration_value('allow_base_course_category')
71
);
72
73
if ($countCategories >= 100) {
74
    // Category code
75
    $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
76
77
    $form->addElement(
78
        'select_ajax',
79
        'category_code',
80
        get_lang('CourseFaculty'),
81
        null,
82
        ['url' => $url]
83
    );
84
} else {
85
    $categories = $courseCategoriesRepo->findAllInAccessUrl(
86
        $accessUrlId,
87
        api_get_configuration_value('allow_base_course_category')
88
    );
89
    $categoriesOptions = [null => get_lang('None')];
90
    /** @var CourseCategory $category */
91
    foreach ($categories as $category) {
92
        $categoriesOptions[$category->getCode()] = (string) $category;
93
    }
94
    $form->addSelect(
95
        'category_code',
96
        get_lang('CourseFaculty'),
97
        $categoriesOptions
98
    );
99
}
100
101
if (api_get_configuration_value('course_creation_form_set_course_category_mandatory')) {
102
    $form->addRule('category_code', get_lang('ThisFieldIsRequired'), 'required');
103
}
104
105
$currentTeacher = api_get_user_entity(api_get_user_id());
106
107
$form->addSelectAjax(
108
    'course_teachers',
109
    get_lang('CourseTeachers'),
110
    [$currentTeacher->getId() => UserManager::formatUserFullName($currentTeacher, true)],
111
    [
112
        'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=teacher_to_basis_course',
113
        'id' => 'course_teachers',
114
        'multiple' => 'multiple',
115
    ]
116
);
117
$form->applyFilter('course_teachers', 'html_filter');
118
119
// Course department
120
$form->addText(
121
    'department_name',
122
    get_lang('CourseDepartment'),
123
    false,
124
    ['size' => '60', 'id' => 'department_name']
125
);
126
$form->applyFilter('department_name', 'html_filter');
127
$form->applyFilter('department_name', 'trim');
128
129
// Department URL
130
$form->addText(
131
    'department_url',
132
    get_lang('CourseDepartmentURL'),
133
    false,
134
    ['size' => '60', 'id' => 'department_url']
135
);
136
$form->applyFilter('department_url', 'html_filter');
137
138
// Course language.
139
$languages = api_get_languages();
140
if (count($languages['name']) === 1) {
141
    // If there's only one language available, there's no point in asking
142
    $form->addElement('hidden', 'course_language', $languages['folder'][0]);
143
} else {
144
    $form->addSelectLanguage(
145
        'course_language',
146
        get_lang('Ln'),
147
        [],
148
        ['style' => 'width:150px']
149
    );
150
}
151
152
if (api_get_setting('teacher_can_select_course_template') === 'true') {
153
    $form->addElement(
154
        'select_ajax',
155
        'course_template',
156
        [
157
            get_lang('CourseTemplate'),
158
            get_lang('PickACourseAsATemplateForThisNewCourse'),
159
        ],
160
        null,
161
        ['url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course']
162
    );
163
}
164
165
$form->addElement('checkbox', 'exemplary_content', '', get_lang('FillWithExemplaryContent'));
166
167
$group = [];
168
$group[] = $form->createElement('radio', 'visibility', get_lang('CourseAccess'), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
169
$group[] = $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
170
$group[] = $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
171
$group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
172
$group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
173
174
$form->addGroup($group, '', get_lang('CourseAccess'));
175
176
$group = [];
177
$group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
178
$group[] = $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
179
$form->addGroup($group, '', get_lang('Subscription'));
180
181
$group = [];
182
$group[] = $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
183
$group[] = $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
184
$form->addGroup($group, '', get_lang('Unsubscription'));
185
186
$form->addElement('text', 'disk_quota', [get_lang('CourseQuota'), null, get_lang('MB')], [
187
    'id' => 'disk_quota',
188
]);
189
$form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
190
191
$obj = new GradeModel();
192
$obj->fill_grade_model_select_in_form($form);
193
194
//Extra fields
195
$extra_field = new ExtraField('course');
196
$extra = $extra_field->addElements($form);
197
198
$htmlHeadXtra[] = '
199
<script>
200
201
$(function() {
202
    '.$extra['jquery_ready_content'].'
203
});
204
</script>';
205
206
$form->addProgress();
207
$form->addButtonCreate(get_lang('CreateCourse'));
208
209
// Set some default values.
210
$values['course_language'] = api_get_setting('platformLanguage');
211
$values['disk_quota'] = round(api_get_setting('default_document_quotum') / 1024 / 1024, 1);
212
213
$default_course_visibility = api_get_setting('courses_default_creation_visibility');
214
215
if (isset($default_course_visibility)) {
216
    $values['visibility'] = api_get_setting('courses_default_creation_visibility');
217
} else {
218
    $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
219
}
220
$values['subscribe'] = 1;
221
$values['unsubscribe'] = 0;
222
$values['course_teachers'] = [$currentTeacher->getId()];
223
224
$form->setDefaults($values);
225
226
// Validate the form
227
if ($form->validate()) {
228
    $course = $form->exportValues();
229
230
    $course_teachers = isset($course['course_teachers']) ? $course['course_teachers'] : null;
231
    $course['disk_quota'] = $course['disk_quota'] * 1024 * 1024;
232
    $course['exemplary_content'] = empty($course['exemplary_content']) ? false : true;
233
    $course['teachers'] = $course_teachers;
234
    $course['wanted_code'] = isset($course['visual_code']) ? $course['visual_code'] : '';
235
    $course['gradebook_model_id'] = isset($course['gradebook_model_id']) ? $course['gradebook_model_id'] : null;
236
    // Fixing category code
237
    $course['course_category'] = isset($course['category_code']) ? $course['category_code'] : '';
238
239
    include_once api_get_path(SYS_CODE_PATH).'lang/english/trad4all.inc.php';
240
    $file_to_include = api_get_path(SYS_CODE_PATH).'lang/'.$course['course_language'].'/trad4all.inc.php';
241
242
    if (file_exists($file_to_include)) {
243
        include $file_to_include;
244
    }
245
246
    $courseInfo = CourseManager::create_course($course);
247
    if ($courseInfo && isset($courseInfo['course_public_url'])) {
248
        Display::addFlash(
249
            Display::return_message(
250
                sprintf(
251
                    get_lang('CourseXAdded'),
252
                    Display::url($courseInfo['title'], $courseInfo['course_public_url'])
253
                ),
254
                'confirmation',
255
                false
256
            )
257
        );
258
    }
259
260
    header('Location: course_list.php');
261
    exit;
262
}
263
264
// Display the form.
265
$content = $form->returnForm();
266
267
$tpl = new Template($tool_name);
268
$tpl->assign('content', $content);
269
$tpl->display_one_col_template();
270