1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Auth |
7
|
|
|
* Auth can be used to instantiate objects or as a library to manage courses |
8
|
|
|
* This file contains a class used like library provides functions for auth tool. |
9
|
|
|
* It's also used like model to courses_controller (MVC pattern). |
10
|
|
|
* |
11
|
|
|
* @author Christian Fasanando <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Auth |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor. |
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This function get all the courses in the particular user category. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getCoursesInCategory() |
28
|
|
|
{ |
29
|
|
|
$user_id = api_get_user_id(); |
30
|
|
|
|
31
|
|
|
// table definitions |
32
|
|
|
$TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE); |
33
|
|
|
$TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
34
|
|
|
$avoidCoursesCondition = CoursesAndSessionsCatalog::getAvoidCourseCondition(); |
35
|
|
|
$visibilityCondition = CourseManager::getCourseVisibilitySQLCondition('course', true); |
36
|
|
|
|
37
|
|
|
$sql = "SELECT |
38
|
|
|
course.id as real_id, |
39
|
|
|
course.code, course.visual_code, course.subscribe subscr, course.unsubscribe unsubscr, |
40
|
|
|
course.title title, course.tutor_name tutor, course.directory, course_rel_user.status status, |
41
|
|
|
course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat |
42
|
|
|
FROM $TABLECOURS course, |
43
|
|
|
$TABLECOURSUSER course_rel_user |
44
|
|
|
WHERE |
45
|
|
|
course.id = course_rel_user.c_id AND |
46
|
|
|
course_rel_user.user_id = '".$user_id."' AND |
47
|
|
|
course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH." |
48
|
|
|
$avoidCoursesCondition |
49
|
|
|
$visibilityCondition |
50
|
|
|
ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC"; |
51
|
|
|
$result = Database::query($sql); |
52
|
|
|
$data = []; |
53
|
|
|
while ($course = Database::fetch_array($result)) { |
54
|
|
|
$data[$course['user_course_cat']][] = $course; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* stores the changes in a course category |
62
|
|
|
* (moving a course to a different course category). |
63
|
|
|
* |
64
|
|
|
* @param int $courseId |
65
|
|
|
* @param int Category id |
66
|
|
|
* |
67
|
|
|
* @return bool True if it success |
68
|
|
|
*/ |
69
|
|
|
public function updateCourseCategory($courseId, $newcategory) |
70
|
|
|
{ |
71
|
|
|
$courseId = (int) $courseId; |
72
|
|
|
$newcategory = (int) $newcategory; |
73
|
|
|
$current_user = api_get_user_id(); |
74
|
|
|
|
75
|
|
|
$table = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
76
|
|
|
$max_sort_value = api_max_sort_value($newcategory, $current_user); |
77
|
|
|
$sql = "UPDATE $table SET |
78
|
|
|
user_course_cat='".$newcategory."', |
79
|
|
|
sort='".($max_sort_value + 1)."' |
80
|
|
|
WHERE |
81
|
|
|
c_id ='".$courseId."' AND |
82
|
|
|
user_id='".$current_user."' AND |
83
|
|
|
relation_type<>".COURSE_RELATION_TYPE_RRHH; |
84
|
|
|
$resultQuery = Database::query($sql); |
85
|
|
|
|
86
|
|
|
$result = false; |
87
|
|
|
if (Database::affected_rows($resultQuery)) { |
88
|
|
|
$result = true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* moves the course one place up or down. |
96
|
|
|
* |
97
|
|
|
* @param string Direction (up/down) |
98
|
|
|
* @param string Course code |
99
|
|
|
* @param int Category id |
100
|
|
|
* |
101
|
|
|
* @return bool True if it success |
102
|
|
|
*/ |
103
|
|
|
public function move_course($direction, $course2move, $category) |
104
|
|
|
{ |
105
|
|
|
// definition of tables |
106
|
|
|
$table = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
107
|
|
|
|
108
|
|
|
$current_user_id = api_get_user_id(); |
109
|
|
|
$all_user_courses = CourseManager::getCoursesByUserCourseCategory($current_user_id); |
110
|
|
|
|
111
|
|
|
// we need only the courses of the category we are moving in |
112
|
|
|
$user_courses = []; |
113
|
|
|
foreach ($all_user_courses as $key => $course) { |
114
|
|
|
if ($course['user_course_category'] == $category) { |
115
|
|
|
$user_courses[] = $course; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$target_course = []; |
120
|
|
|
foreach ($user_courses as $count => $course) { |
121
|
|
|
if ($course2move == $course['code']) { |
122
|
|
|
// source_course is the course where we clicked the up or down icon |
123
|
|
|
$source_course = $course; |
124
|
|
|
// target_course is the course before/after the source_course (depending on the up/down icon) |
125
|
|
|
if ($direction == 'up') { |
126
|
|
|
$target_course = $user_courses[$count - 1]; |
127
|
|
|
} else { |
128
|
|
|
$target_course = $user_courses[$count + 1]; |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$result = false; |
135
|
|
|
if (count($target_course) > 0 && count($source_course) > 0) { |
|
|
|
|
136
|
|
|
$courseInfo = api_get_course_info($source_course['code']); |
137
|
|
|
$courseId = $courseInfo['real_id']; |
138
|
|
|
|
139
|
|
|
$targetCourseInfo = api_get_course_info($target_course['code']); |
140
|
|
|
$targetCourseId = $targetCourseInfo['real_id']; |
141
|
|
|
|
142
|
|
|
$sql = "UPDATE $table |
143
|
|
|
SET sort='".$target_course['sort']."' |
144
|
|
|
WHERE |
145
|
|
|
c_id = '".$courseId."' AND |
146
|
|
|
user_id = '".$current_user_id."' AND |
147
|
|
|
relation_type<>".COURSE_RELATION_TYPE_RRHH; |
148
|
|
|
|
149
|
|
|
$result1 = Database::query($sql); |
150
|
|
|
|
151
|
|
|
$sql = "UPDATE $table SET sort='".$source_course['sort']."' |
152
|
|
|
WHERE |
153
|
|
|
c_id ='".$targetCourseId."' AND |
154
|
|
|
user_id='".$current_user_id."' AND |
155
|
|
|
relation_type<>".COURSE_RELATION_TYPE_RRHH; |
156
|
|
|
|
157
|
|
|
$result2 = Database::query($sql); |
158
|
|
|
|
159
|
|
|
if (Database::affected_rows($result1) && Database::affected_rows($result2)) { |
160
|
|
|
$result = true; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $result; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Moves the course one place up or down. |
169
|
|
|
* |
170
|
|
|
* @param string $direction Direction up/down |
171
|
|
|
* @param string $category2move Category id |
172
|
|
|
* |
173
|
|
|
* @return bool True If it success |
174
|
|
|
*/ |
175
|
|
|
public function move_category($direction, $category2move) |
176
|
|
|
{ |
177
|
|
|
$userId = api_get_user_id(); |
178
|
|
|
$userCategories = CourseManager::get_user_course_categories($userId); |
179
|
|
|
$categories = array_values($userCategories); |
180
|
|
|
|
181
|
|
|
$previous = null; |
182
|
|
|
$target_category = []; |
183
|
|
|
foreach ($categories as $key => $category) { |
184
|
|
|
$category_id = $category['id']; |
185
|
|
|
if ($category2move == $category_id) { |
186
|
|
|
// source_course is the course where we clicked the up or down icon |
187
|
|
|
$source_category = $userCategories[$category2move]; |
188
|
|
|
// target_course is the course before/after the source_course (depending on the up/down icon) |
189
|
|
|
if ($direction === 'up') { |
190
|
|
|
if (isset($categories[$key - 1])) { |
191
|
|
|
$target_category = $userCategories[$categories[$key - 1]['id']]; |
192
|
|
|
} |
193
|
|
|
} else { |
194
|
|
|
if (isset($categories[$key + 1])) { |
195
|
|
|
$target_category = $userCategories[$categories[$key + 1]['id']]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$result = false; |
202
|
|
|
if (count($target_category) > 0 && count($source_category) > 0) { |
|
|
|
|
203
|
|
|
$table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
204
|
|
|
$sql = "UPDATE $table SET |
205
|
|
|
sort = '".Database::escape_string($target_category['sort'])."' |
206
|
|
|
WHERE id='".intval($source_category['id'])."' AND user_id='".$userId."'"; |
207
|
|
|
$resultFirst = Database::query($sql); |
208
|
|
|
$sql = "UPDATE $table SET |
209
|
|
|
sort = '".Database::escape_string($source_category['sort'])."' |
210
|
|
|
WHERE id='".intval($target_category['id'])."' AND user_id='".$userId."'"; |
211
|
|
|
$resultSecond = Database::query($sql); |
212
|
|
|
if (Database::affected_rows($resultFirst) && Database::affected_rows($resultSecond)) { |
213
|
|
|
$result = true; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $result; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Updates the user course category in the chamilo_user database. |
222
|
|
|
* |
223
|
|
|
* @param string Category title |
224
|
|
|
* @param int Category id |
225
|
|
|
* |
226
|
|
|
* @return bool True if it success |
227
|
|
|
*/ |
228
|
|
|
public function store_edit_course_category($title, $category_id) |
229
|
|
|
{ |
230
|
|
|
$title = Database::escape_string($title); |
231
|
|
|
$category_id = (int) $category_id; |
232
|
|
|
$result = false; |
233
|
|
|
$table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
234
|
|
|
$sql = "UPDATE $table |
235
|
|
|
SET title='".api_htmlentities($title, ENT_QUOTES, api_get_system_encoding())."' |
236
|
|
|
WHERE id='".$category_id."'"; |
237
|
|
|
$resultQuery = Database::query($sql); |
238
|
|
|
if (Database::affected_rows($resultQuery)) { |
239
|
|
|
$result = true; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $result; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* deletes a course category and moves all the courses that were in this category to main category. |
247
|
|
|
* |
248
|
|
|
* @param int Category id |
249
|
|
|
* |
250
|
|
|
* @return bool True if it success |
251
|
|
|
*/ |
252
|
|
|
public function delete_course_category($category_id) |
253
|
|
|
{ |
254
|
|
|
$current_user_id = api_get_user_id(); |
255
|
|
|
$tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
256
|
|
|
$TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
257
|
|
|
$category_id = (int) $category_id; |
258
|
|
|
$result = false; |
259
|
|
|
$sql = "DELETE FROM $tucc |
260
|
|
|
WHERE |
261
|
|
|
id='".$category_id."' AND |
262
|
|
|
user_id='".$current_user_id."'"; |
263
|
|
|
$resultQuery = Database::query($sql); |
264
|
|
|
if (Database::affected_rows($resultQuery)) { |
265
|
|
|
$result = true; |
266
|
|
|
} |
267
|
|
|
$sql = "UPDATE $TABLECOURSUSER |
268
|
|
|
SET user_course_cat='0' |
269
|
|
|
WHERE |
270
|
|
|
user_course_cat='".$category_id."' AND |
271
|
|
|
user_id='".$current_user_id."' AND |
272
|
|
|
relation_type<>".COURSE_RELATION_TYPE_RRHH." "; |
273
|
|
|
Database::query($sql); |
274
|
|
|
|
275
|
|
|
return $result; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param int $categoryId |
280
|
|
|
* |
281
|
|
|
* @return array|mixed |
282
|
|
|
*/ |
283
|
|
|
public function getUserCourseCategory($categoryId) |
284
|
|
|
{ |
285
|
|
|
$userId = api_get_user_id(); |
286
|
|
|
$tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
287
|
|
|
$categoryId = (int) $categoryId; |
288
|
|
|
|
289
|
|
|
$sql = "SELECT * FROM $tucc |
290
|
|
|
WHERE |
291
|
|
|
id= $categoryId AND |
292
|
|
|
user_id= $userId"; |
293
|
|
|
$resultQuery = Database::query($sql); |
294
|
|
|
|
295
|
|
|
return Database::fetch_array($resultQuery, 'ASSOC'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* unsubscribe the user from a given course. |
300
|
|
|
* |
301
|
|
|
* @param string $course_code |
302
|
|
|
* |
303
|
|
|
* @return bool True if it success |
304
|
|
|
*/ |
305
|
|
|
public function remove_user_from_course($course_code) |
306
|
|
|
{ |
307
|
|
|
$tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
308
|
|
|
|
309
|
|
|
// protect variables |
310
|
|
|
$current_user_id = api_get_user_id(); |
311
|
|
|
$course_code = Database::escape_string($course_code); |
312
|
|
|
$result = true; |
313
|
|
|
|
314
|
|
|
$courseInfo = api_get_course_info($course_code); |
315
|
|
|
$courseId = $courseInfo['real_id']; |
316
|
|
|
|
317
|
|
|
// we check (once again) if the user is not course administrator |
318
|
|
|
// because the course administrator cannot unsubscribe himself |
319
|
|
|
// (s)he can only delete the course |
320
|
|
|
$sql = "SELECT * FROM $tbl_course_user |
321
|
|
|
WHERE |
322
|
|
|
user_id='".$current_user_id."' AND |
323
|
|
|
c_id ='".$courseId."' AND |
324
|
|
|
status='1' "; |
325
|
|
|
$result_check = Database::query($sql); |
326
|
|
|
$number_of_rows = Database::num_rows($result_check); |
327
|
|
|
if ($number_of_rows > 0) { |
328
|
|
|
$result = false; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
CourseManager::unsubscribe_user($current_user_id, $course_code); |
332
|
|
|
|
333
|
|
|
return $result; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* stores the user course category in the chamilo_user database. |
338
|
|
|
* |
339
|
|
|
* @param string Category title |
340
|
|
|
* |
341
|
|
|
* @return bool True if it success |
342
|
|
|
*/ |
343
|
|
|
public function store_course_category($category_title) |
344
|
|
|
{ |
345
|
|
|
$table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
346
|
|
|
|
347
|
|
|
// protect data |
348
|
|
|
$current_user_id = api_get_user_id(); |
349
|
|
|
$category_title = Database::escape_string($category_title); |
350
|
|
|
|
351
|
|
|
// step 1: we determine the max value of the user defined course categories |
352
|
|
|
$sql = "SELECT sort FROM $table |
353
|
|
|
WHERE user_id='".$current_user_id."' |
354
|
|
|
ORDER BY sort DESC"; |
355
|
|
|
$rs_sort = Database::query($sql); |
356
|
|
|
$maxsort = Database::fetch_array($rs_sort); |
357
|
|
|
$nextsort = $maxsort['sort'] + 1; |
358
|
|
|
|
359
|
|
|
// step 2: we check if there is already a category with this name, |
360
|
|
|
// if not we store it, else we give an error. |
361
|
|
|
$sql = "SELECT * FROM $table |
362
|
|
|
WHERE |
363
|
|
|
user_id='".$current_user_id."' AND |
364
|
|
|
title='".$category_title."' |
365
|
|
|
ORDER BY sort DESC"; |
366
|
|
|
$rs = Database::query($sql); |
367
|
|
|
|
368
|
|
|
$result = false; |
369
|
|
|
if (Database::num_rows($rs) == 0) { |
370
|
|
|
$sql = "INSERT INTO $table (user_id, title,sort) |
371
|
|
|
VALUES ('".$current_user_id."', '".api_htmlentities( |
372
|
|
|
$category_title, |
373
|
|
|
ENT_QUOTES, |
374
|
|
|
api_get_system_encoding() |
375
|
|
|
)."', '".$nextsort."')"; |
376
|
|
|
$resultQuery = Database::query($sql); |
377
|
|
|
if (Database::affected_rows($resultQuery)) { |
378
|
|
|
$result = true; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return $result; |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|