Issues (2029)

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
$setExtraFieldsMandatory = api_get_configuration_value('course_creation_form_set_extra_fields_mandatory');
196
$fieldsRequired = [];
197
if (false !== $setExtraFieldsMandatory && !empty($setExtraFieldsMandatory['fields'])) {
198
    $fieldsRequired = $setExtraFieldsMandatory['fields'];
199
}
200
$extra_field = new ExtraField('course');
201
$extra = $extra_field->addElements(
202
    $form,
203
    0,
204
    [],
205
    false,
206
    false,
207
    [],
208
    [],
209
    [],
210
    false,
211
    false,
212
    [],
213
    [],
214
    false,
215
    [],
216
    $fieldsRequired
217
);
218
219
if (api_get_configuration_value('allow_course_multiple_languages')) {
220
    // Course Multiple language.
221
    $cbMultiLanguage = $form->getElementByName('extra_multiple_language');
222
    if (isset($cbMultiLanguage)) {
223
        foreach ($languages['folder'] as $langFolder) {
224
            $cbMultiLanguage->addOption(get_lang($langFolder), $langFolder);
225
        }
226
    }
227
}
228
229
$htmlHeadXtra[] = '
230
<script>
231
232
$(function() {
233
    '.$extra['jquery_ready_content'].'
234
});
235
</script>';
236
237
$form->addProgress();
238
$form->addButtonCreate(get_lang('CreateCourse'));
239
240
// Set some default values.
241
$values['course_language'] = api_get_setting('platformLanguage');
242
$values['disk_quota'] = round(api_get_setting('default_document_quotum') / 1024 / 1024, 1);
243
244
$default_course_visibility = api_get_setting('courses_default_creation_visibility');
245
246
if (isset($default_course_visibility)) {
247
    $values['visibility'] = api_get_setting('courses_default_creation_visibility');
248
} else {
249
    $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
250
}
251
$values['subscribe'] = 1;
252
$values['unsubscribe'] = 0;
253
$values['course_teachers'] = [$currentTeacher->getId()];
254
255
// Relation to prefill course extra field with user extra field
256
$fillExtraField = api_get_configuration_value('course_creation_user_course_extra_field_relation_to_prefill');
257
if (false !== $fillExtraField && !empty($fillExtraField['fields'])) {
258
    foreach ($fillExtraField['fields'] as $courseVariable => $userVariable) {
259
        $extraValue = UserManager::get_extra_user_data_by_field(api_get_user_id(), $userVariable);
260
        $values['extra_'.$courseVariable] = $extraValue[$userVariable];
261
    }
262
}
263
264
$form->setDefaults($values);
265
266
// Validate the form
267
if ($form->validate()) {
268
    $course = $form->exportValues();
269
270
    $course_teachers = isset($course['course_teachers']) ? $course['course_teachers'] : null;
271
    $course['disk_quota'] = $course['disk_quota'] * 1024 * 1024;
272
    $course['exemplary_content'] = empty($course['exemplary_content']) ? false : true;
273
    $course['teachers'] = $course_teachers;
274
    $course['wanted_code'] = isset($course['visual_code']) ? $course['visual_code'] : '';
275
    $course['gradebook_model_id'] = isset($course['gradebook_model_id']) ? $course['gradebook_model_id'] : null;
276
    // Fixing category code
277
    $course['course_category'] = isset($course['category_code']) ? $course['category_code'] : '';
278
279
    include_once api_get_path(SYS_CODE_PATH).'lang/english/trad4all.inc.php';
280
    $file_to_include = api_get_path(SYS_CODE_PATH).'lang/'.$course['course_language'].'/trad4all.inc.php';
281
282
    if (file_exists($file_to_include)) {
283
        include $file_to_include;
284
    }
285
286
    $courseInfo = CourseManager::create_course($course);
287
    if ($courseInfo && isset($courseInfo['course_public_url'])) {
288
        Display::addFlash(
289
            Display::return_message(
290
                sprintf(
291
                    get_lang('CourseXAdded'),
292
                    Display::url($courseInfo['title'], $courseInfo['course_public_url'])
293
                ),
294
                'confirmation',
295
                false
296
            )
297
        );
298
    }
299
300
    header('Location: course_list.php');
301
    exit;
302
}
303
304
// Display the form.
305
$content = $form->returnForm();
306
307
$tpl = new Template($tool_name);
308
$tpl->assign('content', $content);
309
$tpl->display_one_col_template();
310