|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
use Chamilo\CoreBundle\Framework\Container; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* This tool allows platform admins to create courses by uploading a CSV file |
|
8
|
|
|
* Copyright (c) 2005 Bart Mollet <[email protected]> |
|
9
|
|
|
* @package chamilo.admin |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Validates imported data. |
|
14
|
|
|
*/ |
|
15
|
|
|
function validate_courses_data($courses) |
|
16
|
|
|
{ |
|
17
|
|
|
$errors = array (); |
|
18
|
|
|
$coursecodes = array (); |
|
19
|
|
|
foreach ($courses as $index => $course) { |
|
20
|
|
|
$course['line'] = $index +1; |
|
21
|
|
|
|
|
22
|
|
|
// 1. Check whether mandatory fields are set. |
|
23
|
|
|
$mandatory_fields = array ('Code', 'Title', 'CourseCategory'); |
|
24
|
|
View Code Duplication |
foreach ($mandatory_fields as $field) { |
|
25
|
|
|
if (empty($course[$field])) { |
|
26
|
|
|
$course['error'] = get_lang($field.'Mandatory'); |
|
27
|
|
|
$errors[] = $course; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// 2. Check current course code. |
|
32
|
|
|
if (!empty($course['Code'])) { |
|
33
|
|
|
// 2.1 Check whether code has been already used by this CVS-file. |
|
34
|
|
|
if (isset($coursecodes[$course['Code']])) { |
|
35
|
|
|
$course['error'] = get_lang('CodeTwiceInFile'); |
|
36
|
|
|
$errors[] = $course; |
|
37
|
|
|
} else { |
|
38
|
|
|
// 2.2 Check whether course code has been occupied. |
|
39
|
|
|
$courseInfo = api_get_course_info($course['Code']); |
|
40
|
|
|
if (!empty($courseInfo)) { |
|
41
|
|
|
$course['error'] = get_lang('CodeExists'); |
|
42
|
|
|
$errors[] = $course; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
$coursecodes[$course['Code']] = 1; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// 3. Check whether teacher exists. |
|
49
|
|
|
$teacherList = getTeacherListInArray($course['Teacher']); |
|
50
|
|
|
|
|
51
|
|
|
if (!empty($teacherList)) { |
|
52
|
|
|
foreach ($teacherList as $teacher) { |
|
53
|
|
|
$teacherInfo = api_get_user_info_from_username($teacher); |
|
54
|
|
|
if (empty($teacherInfo)) { |
|
55
|
|
|
$course['error'] = get_lang('UnknownTeacher').' ('.$teacher.')'; |
|
56
|
|
|
$errors[] = $course; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// 4. Check whether course category exists. |
|
62
|
|
|
if (!empty($course['CourseCategory'])) { |
|
63
|
|
|
$categoryInfo = CourseCategory::getCategory($course['CourseCategory']); |
|
64
|
|
|
if (empty($categoryInfo)) { |
|
65
|
|
|
//@todo this is so bad even all lang variables are wrong ... |
|
66
|
|
|
CourseCategory::addNode($course['CourseCategory'], $course['CourseCategoryName'] ? $course['CourseCategoryName'] : $course['CourseCategory'], 'TRUE', null); |
|
67
|
|
|
} |
|
68
|
|
|
} else { |
|
69
|
|
|
$course['error'] = get_lang('NoCourseCategorySupplied'); |
|
70
|
|
|
$errors[] = $course; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $errors; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the teacher list |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $teachers |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
function getTeacherListInArray($teachers) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!empty($teachers)) { |
|
86
|
|
|
return explode('|', $teachers); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return array(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Saves imported data. |
|
94
|
|
|
* @param array $courses List of courses |
|
95
|
|
|
*/ |
|
96
|
|
|
function save_courses_data($courses) |
|
97
|
|
|
{ |
|
98
|
|
|
$msg = ''; |
|
99
|
|
|
foreach ($courses as $course) { |
|
100
|
|
|
$course_language = $course['Language']; |
|
101
|
|
|
$teachers = getTeacherListInArray($course['Teacher']); |
|
102
|
|
|
$teacherList = array(); |
|
103
|
|
|
$creatorId = api_get_user_id(); |
|
104
|
|
|
|
|
105
|
|
|
if (!empty($teachers)) { |
|
106
|
|
|
foreach ($teachers as $teacher) { |
|
107
|
|
|
$teacherInfo = api_get_user_info_from_username($teacher); |
|
108
|
|
|
if (!empty($teacherInfo)) { |
|
109
|
|
|
$teacherList[] = $teacherInfo; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$params = array(); |
|
115
|
|
|
$params['title'] = $course['Title']; |
|
116
|
|
|
$params['wanted_code'] = $course['Code']; |
|
117
|
|
|
$params['tutor_name'] = null; |
|
118
|
|
|
$params['course_category'] = $course['CourseCategory']; |
|
119
|
|
|
$params['course_language'] = $course_language; |
|
120
|
|
|
$params['user_id'] = $creatorId; |
|
121
|
|
|
|
|
122
|
|
|
$addMeAsTeacher = isset($_POST['add_me_as_teacher']) ? $_POST['add_me_as_teacher'] : false; |
|
123
|
|
|
$params['add_user_as_teacher'] = $addMeAsTeacher; |
|
124
|
|
|
|
|
125
|
|
|
$courseInfo = CourseManager::create_course($params); |
|
126
|
|
|
|
|
127
|
|
|
if (!empty($courseInfo)) { |
|
128
|
|
|
if (!empty($teacherList)) { |
|
129
|
|
|
foreach ($teacherList as $teacher) { |
|
130
|
|
|
CourseManager::add_user_to_course( |
|
131
|
|
|
$teacher['user_id'], |
|
132
|
|
|
$courseInfo['code'], |
|
133
|
|
|
COURSEMANAGER |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
$msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/"> |
|
138
|
|
|
'.$courseInfo['title'].'</a> '.get_lang('Created').'<br />'; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (!empty($msg)) { |
|
143
|
|
|
Display::display_normal_message($msg, false); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Read the CSV-file |
|
149
|
|
|
* @param string $file Path to the CSV-file |
|
150
|
|
|
* @return array All course-information read from the file |
|
151
|
|
|
*/ |
|
152
|
|
|
function parse_csv_courses_data($file) |
|
153
|
|
|
{ |
|
154
|
|
|
$courses = Import::csv_reader($file); |
|
155
|
|
|
return $courses; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$cidReset = true; |
|
159
|
|
|
|
|
160
|
|
|
//require '../inc/global.inc.php'; |
|
161
|
|
|
|
|
162
|
|
|
$this_section = SECTION_PLATFORM_ADMIN; |
|
163
|
|
|
api_protect_admin_script(); |
|
164
|
|
|
|
|
165
|
|
|
$defined_auth_sources[] = PLATFORM_AUTH_SOURCE; |
|
166
|
|
|
|
|
167
|
|
|
if (isset($extAuthSource) && is_array($extAuthSource)) { |
|
168
|
|
|
$defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource)); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$tool_name = get_lang('ImportCourses').' CSV'; |
|
172
|
|
|
|
|
173
|
|
|
$interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration'), 'name' => get_lang('PlatformAdmin')); |
|
174
|
|
|
|
|
175
|
|
|
set_time_limit(0); |
|
176
|
|
|
Display :: display_header($tool_name); |
|
177
|
|
|
|
|
178
|
|
|
if (isset($_POST['formSent']) && $_POST['formSent']) { |
|
179
|
|
|
if (empty($_FILES['import_file']['tmp_name'])) { |
|
180
|
|
|
$error_message = get_lang('UplUploadFailed'); |
|
181
|
|
|
Display :: display_error_message($error_message, false); |
|
182
|
|
|
} else { |
|
183
|
|
|
$allowed_file_mimetype = array('csv'); |
|
184
|
|
|
|
|
185
|
|
|
$ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1)); |
|
186
|
|
|
|
|
187
|
|
|
if (!in_array($ext_import_file, $allowed_file_mimetype)) { |
|
188
|
|
|
Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption')); |
|
189
|
|
|
} else { |
|
190
|
|
|
$courses = parse_csv_courses_data($_FILES['import_file']['tmp_name']); |
|
191
|
|
|
|
|
192
|
|
|
$errors = validate_courses_data($courses); |
|
193
|
|
|
if (count($errors) == 0) { |
|
194
|
|
|
save_courses_data($courses); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (isset($errors) && count($errors) != 0) { |
|
201
|
|
|
$error_message = '<ul>'; |
|
202
|
|
|
foreach ($errors as $index => $error_course) { |
|
203
|
|
|
$error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: '; |
|
204
|
|
|
$error_message .= get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')'; |
|
205
|
|
|
$error_message .= '</li>'; |
|
206
|
|
|
} |
|
207
|
|
|
$error_message .= '</ul>'; |
|
208
|
|
|
Display :: display_error_message($error_message, false); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$form = new FormValidator('import', 'post', api_get_self(), null, array('enctype' => 'multipart/form-data')); |
|
212
|
|
|
$form->addHeader($tool_name); |
|
213
|
|
|
$form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation')); |
|
214
|
|
|
$form->addElement('checkbox', 'add_me_as_teacher', null, get_lang('AddMeAsTeacherInCourses')); |
|
215
|
|
|
$form->addButtonImport(get_lang('Import'), 'save'); |
|
216
|
|
|
$form->addElement('hidden', 'formSent', 1); |
|
217
|
|
|
$form->display(); |
|
218
|
|
|
|
|
219
|
|
|
?> |
|
220
|
|
|
<div style="clear: both;"></div> |
|
221
|
|
|
<p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p> |
|
222
|
|
|
|
|
223
|
|
|
<blockquote> |
|
224
|
|
|
<pre> |
|
225
|
|
|
<strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;<strong>CourseCategoryName</strong>;Teacher;Language |
|
226
|
|
|
BIO0015;Biology;BIO;Science;teacher1;english |
|
227
|
|
|
BIO0016;Maths;MATH;Engineerng;teacher2|teacher3;english |
|
228
|
|
|
BIO0017;Language;LANG;;;english |
|
229
|
|
|
</pre> |
|
230
|
|
|
</blockquote> |
|
231
|
|
|
|
|
232
|
|
|
<?php |
|
233
|
|
|
Display :: display_footer(); |
|
234
|
|
|
|