|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
require_once __DIR__.'/../inc/global.inc.php'; |
|
6
|
|
|
require_once __DIR__.'/cm_webservice.php'; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Web services available for the Course module. This class extends the WS class. |
|
10
|
|
|
*/ |
|
11
|
|
|
class WSCMCourse extends WSCM |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Deletes a course. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string API secret key |
|
17
|
|
|
* @param string Course id field name |
|
18
|
|
|
* @param string Course id value |
|
19
|
|
|
*/ |
|
20
|
|
|
public function DeleteCourse( |
|
21
|
|
|
$secret_key, |
|
22
|
|
|
$course_id_field_name, |
|
23
|
|
|
$course_id_value |
|
24
|
|
|
) { |
|
25
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
26
|
|
|
if ($verifKey instanceof WSError) { |
|
27
|
|
|
$this->handleError($verifKey); |
|
28
|
|
|
} else { |
|
29
|
|
|
$result = $this->deleteCourseHelper( |
|
30
|
|
|
$course_id_field_name, |
|
31
|
|
|
$course_id_value |
|
32
|
|
|
); |
|
33
|
|
|
if ($result instanceof WSError) { |
|
|
|
|
|
|
34
|
|
|
$this->handleError($result); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Deletes multiple courses. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string API secret key |
|
43
|
|
|
* @param array Array of courses with elements of the form |
|
44
|
|
|
* array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') |
|
45
|
|
|
* |
|
46
|
|
|
* @return array Array with elements like |
|
47
|
|
|
* array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
|
48
|
|
|
* Note that if the result array contains a code different |
|
49
|
|
|
* than 0, an error occured |
|
50
|
|
|
*/ |
|
51
|
|
|
public function DeleteCourses($secret_key, $courses) |
|
52
|
|
|
{ |
|
53
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
54
|
|
|
if ($verifKey instanceof WSError) { |
|
55
|
|
|
$this->handleError($verifKey); |
|
56
|
|
|
} else { |
|
57
|
|
|
$results = []; |
|
58
|
|
|
foreach ($courses as $course) { |
|
59
|
|
|
$result_tmp = []; |
|
60
|
|
|
$result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']); |
|
61
|
|
|
$result_tmp['course_id_value'] = $course['course_id_value']; |
|
62
|
|
|
if ($result_op instanceof WSCMError) { |
|
63
|
|
|
// Return the error in the results |
|
64
|
|
|
$result_tmp['result'] = $result_op->toArray(); |
|
65
|
|
|
} else { |
|
66
|
|
|
$result_tmp['result'] = $this->getSuccessfulResult(); |
|
67
|
|
|
} |
|
68
|
|
|
$results[] = $result_tmp; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $results; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Creates a course. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string API secret key |
|
79
|
|
|
* @param string Title |
|
80
|
|
|
* @param string Category code |
|
81
|
|
|
* @param string Wanted code. If it's not defined, it will be generated automatically |
|
82
|
|
|
* @param string Tutor name |
|
83
|
|
|
* @param string Course admin user id field name |
|
84
|
|
|
* @param string Course admin user id value |
|
85
|
|
|
* @param string Course language |
|
86
|
|
|
* @param string Course id field name |
|
87
|
|
|
* @param string Course id value |
|
88
|
|
|
* @param array Course extra fields |
|
89
|
|
|
* |
|
90
|
|
|
* @return int Course id generated |
|
91
|
|
|
*/ |
|
92
|
|
|
public function CreateCourse( |
|
93
|
|
|
$secret_key, |
|
94
|
|
|
$title, |
|
95
|
|
|
$category_code, |
|
96
|
|
|
$wanted_code, |
|
97
|
|
|
$tutor_name, |
|
98
|
|
|
$course_admin_user_id_field_name, |
|
99
|
|
|
$course_admin_user_id_value, |
|
100
|
|
|
$language, |
|
101
|
|
|
$course_id_field_name, |
|
102
|
|
|
$course_id_value, |
|
103
|
|
|
$extras |
|
104
|
|
|
) { |
|
105
|
|
|
// First, verify the secret key |
|
106
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
107
|
|
|
if ($verifKey instanceof WSError) { |
|
108
|
|
|
$this->handleError($verifKey); |
|
109
|
|
|
} else { |
|
110
|
|
|
$result = $this->createCourseHelper( |
|
111
|
|
|
$title, |
|
112
|
|
|
$category_code, |
|
113
|
|
|
$wanted_code, |
|
114
|
|
|
$tutor_name, |
|
115
|
|
|
$course_admin_user_id_field_name, |
|
116
|
|
|
$course_admin_user_id_value, |
|
117
|
|
|
$language, |
|
118
|
|
|
$course_id_field_name, |
|
119
|
|
|
$course_id_value, |
|
120
|
|
|
$extras |
|
121
|
|
|
); |
|
122
|
|
|
if ($result instanceof WSError) { |
|
123
|
|
|
$this->handleError($result); |
|
124
|
|
|
} else { |
|
125
|
|
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Create multiple courses. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string API secret key |
|
134
|
|
|
* @param array Courses to be created, with elements following the structure presented in CreateCourse |
|
135
|
|
|
* |
|
136
|
|
|
* @return array Array with elements of the form |
|
137
|
|
|
* array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
|
138
|
|
|
*/ |
|
139
|
|
|
public function CreateCourses($secret_key, $courses) |
|
140
|
|
|
{ |
|
141
|
|
|
// First, verify the secret key |
|
142
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
143
|
|
|
if ($verifKey instanceof WSCMError) { |
|
144
|
|
|
$this->handleError($verifKey); |
|
145
|
|
|
} else { |
|
146
|
|
|
$results = []; |
|
147
|
|
|
foreach ($courses as $course) { |
|
148
|
|
|
$result_tmp = []; |
|
149
|
|
|
//reinitialize variables just in case |
|
150
|
|
|
$title = $category_code = $wanted_code = $tutor_name = $course_admin_user_id_field_name = $course_admin_user_id_value = $language = $course_id_field_name = $course_id_value = $extras = null; |
|
151
|
|
|
extract($course); |
|
152
|
|
|
$result = $this->createCourseHelper( |
|
153
|
|
|
$title, |
|
154
|
|
|
$category_code, |
|
155
|
|
|
$wanted_code, |
|
156
|
|
|
$tutor_name, |
|
157
|
|
|
$course_admin_user_id_field_name, |
|
158
|
|
|
$course_admin_user_id_value, |
|
159
|
|
|
$language, |
|
160
|
|
|
$course_id_field_name, |
|
161
|
|
|
$course_id_value, |
|
162
|
|
|
$extras |
|
163
|
|
|
); |
|
164
|
|
|
if ($result instanceof WSCMError) { |
|
165
|
|
|
$result_tmp['result'] = $result->toArray(); |
|
166
|
|
|
$result_tmp['course_id_value'] = $course_id_value; |
|
167
|
|
|
$result_tmp['course_id_generated'] = 0; |
|
168
|
|
|
} else { |
|
169
|
|
|
$result_tmp['result'] = $this->getSuccessfulResult(); |
|
170
|
|
|
$result_tmp['course_id_value'] = $course_id_value; |
|
171
|
|
|
$result_tmp['course_id_generated'] = $result; |
|
172
|
|
|
} |
|
173
|
|
|
$results[] = $result_tmp; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $results; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Edits a course. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string API secret key |
|
184
|
|
|
* @param string Course id field name |
|
185
|
|
|
* @param string Course id value |
|
186
|
|
|
* @param string Title |
|
187
|
|
|
* @param string Category code |
|
188
|
|
|
* @param string Department name |
|
189
|
|
|
* @param string Department url |
|
190
|
|
|
* @param string Course language |
|
191
|
|
|
* @param int Visibility |
|
192
|
|
|
* @param int Subscribe (0 = denied, 1 = allowed) |
|
193
|
|
|
* @param int Unsubscribe (0 = denied, 1 = allowed) |
|
194
|
|
|
* @param string Visual code |
|
195
|
|
|
* @param array Course extra fields |
|
196
|
|
|
*/ |
|
197
|
|
|
public function EditCourse( |
|
198
|
|
|
$secret_key, |
|
199
|
|
|
$course_id_field_name, |
|
200
|
|
|
$course_id_value, |
|
201
|
|
|
$title, |
|
202
|
|
|
$category_code, |
|
203
|
|
|
$department_name, |
|
204
|
|
|
$department_url, |
|
205
|
|
|
$language, |
|
206
|
|
|
$visibility, |
|
207
|
|
|
$subscribe, |
|
208
|
|
|
$unsubscribe, |
|
209
|
|
|
$visual_code, |
|
210
|
|
|
$extras |
|
211
|
|
|
) { |
|
212
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
213
|
|
|
if ($verifKey instanceof WSCMError) { |
|
214
|
|
|
$this->handleError($verifKey); |
|
215
|
|
|
} else { |
|
216
|
|
|
$result = $this->editCourseHelper( |
|
217
|
|
|
$course_id_field_name, |
|
218
|
|
|
$course_id_value, |
|
219
|
|
|
$title, |
|
220
|
|
|
$category_code, |
|
221
|
|
|
$department_name, |
|
222
|
|
|
$department_url, |
|
223
|
|
|
$language, |
|
224
|
|
|
$visibility, |
|
225
|
|
|
$subscribe, |
|
226
|
|
|
$unsubscribe, |
|
227
|
|
|
$visual_code, |
|
228
|
|
|
$extras |
|
229
|
|
|
); |
|
230
|
|
|
if ($result instanceof WSCMError) { |
|
231
|
|
|
$this->handleError($result); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* List courses. |
|
238
|
|
|
* |
|
239
|
|
|
* @param string API secret key |
|
240
|
|
|
* @param string Course id field name. Use "chamilo_course_id" to use internal id |
|
241
|
|
|
* |
|
242
|
|
|
* @return array An array with elements of the form |
|
243
|
|
|
* ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility', |
|
244
|
|
|
* 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id') |
|
245
|
|
|
*/ |
|
246
|
|
|
public function ListCourses($secret_key, $course_id_field_name) |
|
247
|
|
|
{ |
|
248
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
249
|
|
|
if ($verifKey instanceof WSError) { |
|
250
|
|
|
$this->handleError($verifKey); |
|
251
|
|
|
} else { |
|
252
|
|
|
$courses_result = []; |
|
253
|
|
|
$category_names = []; |
|
254
|
|
|
|
|
255
|
|
|
$courses = CourseManager::get_courses_list(); |
|
256
|
|
|
foreach ($courses as $course) { |
|
257
|
|
|
$course_tmp = []; |
|
258
|
|
|
$course_tmp['id'] = $course['id']; |
|
259
|
|
|
$course_tmp['code'] = $course['code']; |
|
260
|
|
|
$course_tmp['title'] = $course['title']; |
|
261
|
|
|
$course_tmp['language'] = $course['course_language']; |
|
262
|
|
|
$course_tmp['visibility'] = $course['visibility']; |
|
263
|
|
|
|
|
264
|
|
|
// Determining category name |
|
265
|
|
|
if ($category_names[$course['category_code']]) { |
|
266
|
|
|
$course_tmp['category_name'] = $category_names[$course['category_code']]; |
|
267
|
|
|
} else { |
|
268
|
|
|
$category = CourseManager::get_course_category($course['category_code']); |
|
269
|
|
|
$category_names[$course['category_code']] = $category['name']; |
|
270
|
|
|
$course_tmp['category_name'] = $category['name']; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
// Determining number of students registered in course |
|
274
|
|
|
$user_list = CourseManager::get_user_list_from_course_code($course['code']); |
|
275
|
|
|
$course_tmp['number_students'] = count($user_list); |
|
276
|
|
|
|
|
277
|
|
|
// Determining external course id |
|
278
|
|
|
$course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_id_field_name, $course['code']); |
|
279
|
|
|
$courses_result[] = $course_tmp; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
return $courses_result; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Subscribe user to a course. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string API secret key |
|
290
|
|
|
* @param string Course id field name. Use "chamilo_course_id" to use internal id |
|
291
|
|
|
* @param string course id value |
|
292
|
|
|
* @param string User id field name. Use "chamilo_user_id" to use internal id |
|
293
|
|
|
* @param string User id value |
|
294
|
|
|
* @param int Status (1 = Teacher, 5 = Student) |
|
295
|
|
|
*/ |
|
296
|
|
|
public function SubscribeUserToCourse( |
|
297
|
|
|
$secret_key, |
|
298
|
|
|
$course_id_field_name, |
|
299
|
|
|
$course_id_value, |
|
300
|
|
|
$user_id_field_name, |
|
301
|
|
|
$user_id_value, |
|
302
|
|
|
$status |
|
303
|
|
|
) { |
|
304
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
305
|
|
|
if ($verifKey instanceof WSError) { |
|
306
|
|
|
$this->handleError($verifKey); |
|
307
|
|
|
} else { |
|
308
|
|
|
$result = $this->changeUserSubscription( |
|
309
|
|
|
$course_id_field_name, |
|
310
|
|
|
$course_id_value, |
|
311
|
|
|
$user_id_field_name, |
|
312
|
|
|
$user_id_value, |
|
313
|
|
|
1, |
|
314
|
|
|
$status |
|
315
|
|
|
); |
|
316
|
|
|
if ($result instanceof WSError) { |
|
317
|
|
|
$this->handleError($result); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Unsusbscribe user from course. |
|
324
|
|
|
* |
|
325
|
|
|
* @param string API secret key |
|
326
|
|
|
* @param string Course id field name. Use "chamilo_course_id" to use internal id |
|
327
|
|
|
* @param string course id value |
|
328
|
|
|
* @param string User id field name. Use "chamilo_user_id" to use internal id |
|
329
|
|
|
* @param string User id value |
|
330
|
|
|
*/ |
|
331
|
|
|
public function UnsubscribeUserFromCourse( |
|
332
|
|
|
$secret_key, |
|
333
|
|
|
$course_id_field_name, |
|
334
|
|
|
$course_id_value, |
|
335
|
|
|
$user_id_field_name, |
|
336
|
|
|
$user_id_value |
|
337
|
|
|
) { |
|
338
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
339
|
|
|
if ($verifKey instanceof WSError) { |
|
340
|
|
|
$this->handleError($verifKey); |
|
341
|
|
|
} else { |
|
342
|
|
|
$result = $this->changeUserSubscription( |
|
343
|
|
|
$course_id_field_name, |
|
344
|
|
|
$course_id_value, |
|
345
|
|
|
$user_id_field_name, |
|
346
|
|
|
$user_id_value, |
|
347
|
|
|
0 |
|
348
|
|
|
); |
|
349
|
|
|
if ($result instanceof WSError) { |
|
350
|
|
|
$this->handleError($result); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Returns the descriptions of a course, along with their id. |
|
357
|
|
|
* |
|
358
|
|
|
* @param string API secret key |
|
359
|
|
|
* @param string Course id field name |
|
360
|
|
|
* @param string Course id value |
|
361
|
|
|
* |
|
362
|
|
|
* @return array Returns an array with elements of the form |
|
363
|
|
|
* array('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content') |
|
364
|
|
|
*/ |
|
365
|
|
|
public function GetCourseDescriptions( |
|
366
|
|
|
$secret_key, |
|
367
|
|
|
$course_id_field_name, |
|
368
|
|
|
$course_id_value |
|
369
|
|
|
) { |
|
370
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
371
|
|
|
if ($verifKey instanceof WSError) { |
|
372
|
|
|
$this->handleError($verifKey); |
|
373
|
|
|
} else { |
|
374
|
|
|
$course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
|
375
|
|
|
if ($course_id instanceof WSError) { |
|
376
|
|
|
return $course_id; |
|
377
|
|
|
} else { |
|
378
|
|
|
// Course exists, get its descriptions |
|
379
|
|
|
$descriptions = CourseDescription::get_descriptions($course_id); |
|
380
|
|
|
$results = []; |
|
381
|
|
|
foreach ($descriptions as $description) { |
|
382
|
|
|
$results[] = ['course_desc_id' => $description->get_description_type(), |
|
383
|
|
|
'course_desc_title' => $description->get_title(), |
|
384
|
|
|
'course_desc_content' => $description->get_content(), ]; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
return $results; |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Edit course description. |
|
394
|
|
|
* |
|
395
|
|
|
* @param string API secret key |
|
396
|
|
|
* @param string Course id field name |
|
397
|
|
|
* @param string Course id value |
|
398
|
|
|
* @param int Category id from course description |
|
399
|
|
|
* @param string Description title |
|
400
|
|
|
* @param string Course description content |
|
401
|
|
|
*/ |
|
402
|
|
|
public function EditCourseDescription( |
|
403
|
|
|
$secret_key, |
|
404
|
|
|
$course_id_field_name, |
|
405
|
|
|
$course_id_value, |
|
406
|
|
|
$course_desc_id, |
|
407
|
|
|
$course_desc_title, |
|
408
|
|
|
$course_desc_content |
|
409
|
|
|
) { |
|
410
|
|
|
$verifKey = $this->verifyKey($secret_key); |
|
411
|
|
|
if ($verifKey instanceof WSError) { |
|
412
|
|
|
$this->handleError($verifKey); |
|
413
|
|
|
} else { |
|
414
|
|
|
$course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
|
415
|
|
|
if ($course_id instanceof WSError) { |
|
416
|
|
|
return $course_id; |
|
417
|
|
|
} else { |
|
418
|
|
|
// Create the new course description |
|
419
|
|
|
$cd = new CourseDescription(); |
|
420
|
|
|
$cd->set_description_type($course_desc_id); |
|
421
|
|
|
$cd->set_title($course_desc_title); |
|
422
|
|
|
$cd->set_content($course_desc_content); |
|
423
|
|
|
$cd->set_session_id(0); |
|
424
|
|
|
|
|
425
|
|
|
// Get course info |
|
426
|
|
|
$course_info = CourseManager::get_course_information( |
|
427
|
|
|
CourseManager::get_course_code_from_course_id($course_id) |
|
428
|
|
|
); |
|
429
|
|
|
// Check if this course description exists |
|
430
|
|
|
$descriptions = CourseDescription::get_descriptions($course_id); |
|
431
|
|
|
$exists = false; |
|
432
|
|
|
foreach ($descriptions as $description) { |
|
433
|
|
|
if ($description->get_description_type() == $course_desc_id) { |
|
434
|
|
|
$exists = true; |
|
435
|
|
|
} |
|
436
|
|
|
} |
|
437
|
|
|
$cd->set_course_id($course_info['real_id']); |
|
438
|
|
|
if (!$exists) { |
|
439
|
|
|
$cd->set_progress(0); |
|
440
|
|
|
$cd->insert(); |
|
441
|
|
|
} else { |
|
442
|
|
|
$cd->update(); |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
/*public function unreadMessage($username, $password) |
|
449
|
|
|
{ |
|
450
|
|
|
if ("valid" == $this->verifyUserPass($username, $password)) { |
|
451
|
|
|
$table_message = Database::get_main_table(TABLE_MESSAGE); |
|
452
|
|
|
$user_id = UserManager::get_user_id_from_username($username); |
|
453
|
|
|
$condition_msg_status = ' msg_status = 1 '; |
|
454
|
|
|
|
|
455
|
|
|
$sql_query = "SELECT COUNT(*) as number_messages |
|
456
|
|
|
FROM $table_message |
|
457
|
|
|
WHERE $condition_msg_status AND user_receiver_id=".$user_id; |
|
458
|
|
|
|
|
459
|
|
|
$sql_result = Database::query($sql_query); |
|
460
|
|
|
$result = Database::fetch_array($sql_result); |
|
461
|
|
|
|
|
462
|
|
|
return $result['number_messages']; |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
return "0"; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
public function get_message_data($username, $password) |
|
469
|
|
|
{ |
|
470
|
|
|
if ("valid" == $this->verifyUserPass($username, $password)) { |
|
471
|
|
|
$user_id = get_user_id_from_username($username); |
|
472
|
|
|
} |
|
473
|
|
|
}*/ |
|
474
|
|
|
|
|
475
|
|
|
public function nada($username, $password) |
|
476
|
|
|
{ |
|
477
|
|
|
if ("valid" == $this->verifyUserPass($username, $password)) { |
|
478
|
|
|
return $username.$password; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
return $username; |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Deletes a course (helper method). |
|
486
|
|
|
* |
|
487
|
|
|
* @param string Course id field name |
|
488
|
|
|
* @param string Course id value |
|
489
|
|
|
* |
|
490
|
|
|
* @return mixed True if the course was successfully deleted, WSError otherwise |
|
491
|
|
|
*/ |
|
492
|
|
|
protected function deleteCourseHelper( |
|
493
|
|
|
$course_id_field_name, |
|
494
|
|
|
$course_id_value |
|
495
|
|
|
) { |
|
496
|
|
|
$course_id = $this->getCourseId( |
|
497
|
|
|
$course_id_field_name, |
|
498
|
|
|
$course_id_value |
|
499
|
|
|
); |
|
500
|
|
|
if ($course_id instanceof WSCMError) { |
|
501
|
|
|
return $course_id; |
|
502
|
|
|
} else { |
|
503
|
|
|
$course_code = CourseManager::get_course_code_from_course_id( |
|
504
|
|
|
$course_id |
|
505
|
|
|
); |
|
506
|
|
|
CourseManager::delete_course($course_code); |
|
507
|
|
|
|
|
508
|
|
|
return true; |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
/** |
|
513
|
|
|
* Creates a course (helper method). |
|
514
|
|
|
* |
|
515
|
|
|
* @param string Title |
|
516
|
|
|
* @param string Category code |
|
517
|
|
|
* @param string Wanted code. If it's not defined, it will be generated automatically |
|
518
|
|
|
* @param string Tutor name |
|
519
|
|
|
* @param string Course admin user id field name |
|
520
|
|
|
* @param string Course admin user id value |
|
521
|
|
|
* @param string Course language |
|
522
|
|
|
* @param string Course id field name |
|
523
|
|
|
* @param string Course id value |
|
524
|
|
|
* @param array Course extra fields |
|
525
|
|
|
* |
|
526
|
|
|
* @return mixed Generated id if creation was successful, WSError otherwise |
|
527
|
|
|
*/ |
|
528
|
|
|
protected function createCourseHelper( |
|
529
|
|
|
$title, |
|
530
|
|
|
$category_code, |
|
531
|
|
|
$wanted_code, |
|
532
|
|
|
$tutor_name, |
|
533
|
|
|
$course_admin_user_id_field_name, |
|
534
|
|
|
$course_admin_user_id_value, |
|
535
|
|
|
$language, |
|
536
|
|
|
$course_id_field_name, |
|
537
|
|
|
$course_id_value, |
|
538
|
|
|
$extras |
|
539
|
|
|
) { |
|
540
|
|
|
// Add the original course id field name and value to the extra fields if needed |
|
541
|
|
|
$extras_associative = []; |
|
542
|
|
|
if ("chamilo_course_id" != $course_id_field_name) { |
|
543
|
|
|
$extras_associative[$course_id_field_name] = $course_id_value; |
|
544
|
|
|
} |
|
545
|
|
|
foreach ($extras as $extra) { |
|
546
|
|
|
$extras_associative[$extra['field_name']] = $extra['field_value']; |
|
547
|
|
|
} |
|
548
|
|
|
$course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value); |
|
549
|
|
|
if ($course_admin_id instanceof WSError) { |
|
550
|
|
|
return $course_admin_id; |
|
551
|
|
|
} |
|
552
|
|
|
if ('' == $wanted_code) { |
|
553
|
|
|
$wanted_code = CourseManager::generate_course_code($title); |
|
554
|
|
|
} |
|
555
|
|
|
$result = create_course( |
|
556
|
|
|
$wanted_code, |
|
557
|
|
|
$title, |
|
558
|
|
|
$tutor_name, |
|
559
|
|
|
$category_code, |
|
560
|
|
|
$language, |
|
561
|
|
|
$course_admin_id, |
|
562
|
|
|
$this->_configuration['db_prefix'], |
|
563
|
|
|
0 |
|
564
|
|
|
); |
|
565
|
|
|
if (!$result) { |
|
566
|
|
|
return new WSError(202, 'There was an error creating the course'); |
|
567
|
|
|
} else { |
|
568
|
|
|
// Update extra fields |
|
569
|
|
|
foreach ($extras_associative as $fname => $fvalue) { |
|
570
|
|
|
CourseManager::update_course_extra_field_value($result, $fname, $fvalue); |
|
571
|
|
|
} |
|
572
|
|
|
// Get course id |
|
573
|
|
|
$course_info = CourseManager::get_course_information($result); |
|
574
|
|
|
|
|
575
|
|
|
return $course_info['real_id']; |
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Edits a course (helper method). |
|
581
|
|
|
* |
|
582
|
|
|
* @param string Course id field name |
|
583
|
|
|
* @param string Course id value |
|
584
|
|
|
* @param string Title |
|
585
|
|
|
* @param string Category code |
|
586
|
|
|
* @param string Department name |
|
587
|
|
|
* @param string Department url |
|
588
|
|
|
* @param string Course language |
|
589
|
|
|
* @param int Visibility |
|
590
|
|
|
* @param int Subscribe (0 = denied, 1 = allowed) |
|
591
|
|
|
* @param int Unsubscribe (0 = denied, 1 = allowed) |
|
592
|
|
|
* @param string Visual code |
|
593
|
|
|
* @param array Course extra fields |
|
594
|
|
|
* |
|
595
|
|
|
* @return mixed True in case of success, WSError otherwise |
|
596
|
|
|
*/ |
|
597
|
|
|
protected function editCourseHelper( |
|
598
|
|
|
$course_id_field_name, |
|
599
|
|
|
$course_id_value, |
|
600
|
|
|
$title, |
|
601
|
|
|
$category_code, |
|
602
|
|
|
$department_name, |
|
603
|
|
|
$department_url, |
|
604
|
|
|
$language, |
|
605
|
|
|
$visibility, |
|
606
|
|
|
$subscribe, |
|
607
|
|
|
$unsubscribe, |
|
608
|
|
|
$visual_code, |
|
609
|
|
|
$extras |
|
610
|
|
|
) { |
|
611
|
|
|
$course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
|
612
|
|
|
if ($course_id instanceof WSCMError) { |
|
613
|
|
|
return $course_id; |
|
614
|
|
|
} else { |
|
615
|
|
|
$attributes = []; |
|
616
|
|
|
if (!empty($title)) { |
|
617
|
|
|
$attributes['title'] = $title; |
|
618
|
|
|
} |
|
619
|
|
|
if (!empty($category_code)) { |
|
620
|
|
|
$attributes['category_code'] = $category_code; |
|
621
|
|
|
} |
|
622
|
|
|
if (!empty($department_name)) { |
|
623
|
|
|
$attributes['department_name'] = $department_name; |
|
624
|
|
|
} |
|
625
|
|
|
if (!empty($department_url)) { |
|
626
|
|
|
$attributes['department_url'] = $department_url; |
|
627
|
|
|
} |
|
628
|
|
|
if (!empty($language)) { |
|
629
|
|
|
$attributes['course_language'] = $language; |
|
630
|
|
|
} |
|
631
|
|
|
if ('' != $visibility) { |
|
632
|
|
|
$attributes['visibility'] = (int) $visibility; |
|
633
|
|
|
} |
|
634
|
|
|
if ('' != $subscribe) { |
|
635
|
|
|
$attributes['subscribe'] = (int) $subscribe; |
|
636
|
|
|
} |
|
637
|
|
|
if ('' != $unsubscribe) { |
|
638
|
|
|
$attributes['unsubscribe'] = (int) $unsubscribe; |
|
639
|
|
|
} |
|
640
|
|
|
if (!empty($visual_code)) { |
|
641
|
|
|
$attributes['visual_code'] = $visual_code; |
|
642
|
|
|
} |
|
643
|
|
|
if (!empty($attributes)) { |
|
644
|
|
|
CourseManager::update_attributes($course_id, $attributes); |
|
645
|
|
|
} |
|
646
|
|
|
if (!empty($extras)) { |
|
647
|
|
|
$course_code = CourseManager::get_course_code_from_course_id($course_id); |
|
648
|
|
|
$extras_associative = []; |
|
649
|
|
|
foreach ($extras as $extra) { |
|
650
|
|
|
$extras_associative[$extra['field_name']] = $extra['field_value']; |
|
651
|
|
|
} |
|
652
|
|
|
foreach ($extras_associative as $fname => $fvalue) { |
|
653
|
|
|
CourseManager::update_extra_field_value($course_code, $fname, $fvalue); |
|
654
|
|
|
} |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
return true; |
|
658
|
|
|
} |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
/** |
|
662
|
|
|
* Subscribe or unsubscribe user to a course (helper method). |
|
663
|
|
|
* |
|
664
|
|
|
* @param string Course id field name. Use "chamilo_course_id" to use internal id |
|
665
|
|
|
* @param string course id value |
|
666
|
|
|
* @param string User id field name. Use "chamilo_user_id" to use internal id |
|
667
|
|
|
* @param string User id value |
|
668
|
|
|
* @param int Set to 1 to subscribe, 0 to unsubscribe |
|
669
|
|
|
* @param int Status (STUDENT or TEACHER) Used for subscription only |
|
670
|
|
|
* |
|
671
|
|
|
* @return mixed True if subscription or unsubscription was successful, false otherwise |
|
672
|
|
|
*/ |
|
673
|
|
|
protected function changeUserSubscription( |
|
674
|
|
|
$course_id_field_name, |
|
675
|
|
|
$course_id_value, |
|
676
|
|
|
$user_id_field_name, |
|
677
|
|
|
$user_id_value, |
|
678
|
|
|
$state, |
|
679
|
|
|
$status = STUDENT |
|
680
|
|
|
) { |
|
681
|
|
|
$course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
|
682
|
|
|
if ($course_id instanceof WSError) { |
|
683
|
|
|
return $course_id; |
|
684
|
|
|
} else { |
|
685
|
|
|
$user_id = $this->getUserId($user_id_field_name, $user_id_value); |
|
686
|
|
|
if ($user_id instanceof WSError) { |
|
687
|
|
|
return $user_id; |
|
688
|
|
|
} else { |
|
689
|
|
|
$course_code = CourseManager::get_course_code_from_course_id($course_id); |
|
690
|
|
|
if (0 == $state) { |
|
691
|
|
|
// Unsubscribe user |
|
692
|
|
|
CourseManager::unsubscribe_user($user_id, $course_code); |
|
693
|
|
|
|
|
694
|
|
|
return true; |
|
695
|
|
|
} else { |
|
696
|
|
|
// Subscribe user |
|
697
|
|
|
if (CourseManager::subscribeUser($user_id, $course_id, $status)) { |
|
698
|
|
|
return true; |
|
699
|
|
|
} else { |
|
700
|
|
|
return new WSError(203, 'An error occurred subscribing to this course'); |
|
701
|
|
|
} |
|
702
|
|
|
} |
|
703
|
|
|
} |
|
704
|
|
|
} |
|
705
|
|
|
} |
|
706
|
|
|
} |
|
707
|
|
|
|