Completed
Push — master ( 1ea773...b7667b )
by Julito
63:46 queued 26:51
created

Auth::browseSessionsBySearch()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\ExtraField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ExtraField.

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...
5
6
/**
7
 * Class Auth
8
 * Auth can be used to instantiate objects or as a library to manage courses
9
 * This file contains a class used like library provides functions for auth tool.
10
 * It's also used like model to courses_controller (MVC pattern)
11
 * @author Christian Fasanando <[email protected]>
12
 *
13
 * @package chamilo.auth
14
 */
15
class Auth
16
{
17
    /**
18
     * Constructor
19
     */
20
    public function __construct()
21
    {
22
    }
23
24
    /**
25
     * retrieves all the courses that the user has already subscribed to
26
     * @param   int $user_id
27
     * @return  array an array containing all the information of the courses of the given user
28
     */
29
    public function get_courses_of_user($user_id)
30
    {
31
        $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
32
        $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
33
        $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_EXTRA_FIELD);
34
        $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
35
36
        $extraFieldType = ExtraField::COURSE_FIELD_TYPE;
37
        // get course list auto-register
38
        $sql = "SELECT item_id FROM $TABLE_COURSE_FIELD_VALUE tcfv
39
                INNER JOIN $TABLE_COURSE_FIELD tcf
40
                ON tcfv.field_id =  tcf.id
41
                WHERE
42
                    tcf.extra_field_type = $extraFieldType AND
43
                    tcf.variable = 'special_course' AND
44
                    tcfv.value = 1
45
                ";
46
47
        $result = Database::query($sql);
48
        $special_course_list = array();
49 View Code Duplication
        if (Database::num_rows($result) > 0) {
50
            while ($result_row = Database::fetch_array($result)) {
51
                $special_course_list[] = '"' . $result_row['item_id'] . '"';
52
            }
53
        }
54
        $without_special_courses = '';
55
        if (!empty($special_course_list)) {
56
            $without_special_courses = ' AND course.id NOT IN (' . implode(',', $special_course_list) . ')';
57
        }
58
59
        // Secondly we select the courses that are in a category (user_course_cat<>0) and sort these according to the sort of the category
60
        $user_id = intval($user_id);
61
        $sql = "SELECT
62
                    course.code k,
63
                    course.visual_code vc,
64
                    course.subscribe subscr,
65
                    course.unsubscribe unsubscr,
66
                    course.title i,
67
                    course.tutor_name t,
68
                    course.category_code cat,
69
                    course.directory dir,
70
                    course_rel_user.status status,
71
                    course_rel_user.sort sort,
72
                    course_rel_user.user_course_cat user_course_cat
73
                FROM $TABLECOURS course, $TABLECOURSUSER  course_rel_user
74
                WHERE
75
                    course.id = course_rel_user.c_id AND
76
                    course_rel_user.relation_type<>" . COURSE_RELATION_TYPE_RRHH . " AND
77
                    course_rel_user.user_id = '" . $user_id . "' $without_special_courses
78
                ORDER BY course_rel_user.sort ASC";
79
        $result = Database::query($sql);
80
        $courses = array();
81 View Code Duplication
        while ($row = Database::fetch_array($result)) {
82
            //we only need the database name of the course
83
            $courses[] = array(
84
                'code' => $row['k'],
85
                'visual_code' => $row['vc'],
86
                'title' => $row['i'],
87
                'directory' => $row['dir'],
88
                'status' => $row['status'],
89
                'tutor' => $row['t'],
90
                'subscribe' => $row['subscr'],
91
                'category' => $row['cat'],
92
                'unsubscribe' => $row['unsubscr'],
93
                'sort' => $row['sort'],
94
                'user_course_category' => $row['user_course_cat']
95
            );
96
        }
97
98
        return $courses;
99
    }
100
101
    /**
102
     * retrieves the user defined course categories
103
     * @return array containing all the IDs of the user defined courses categories, sorted by the "sort" field
104
     */
105
    public function get_user_course_categories()
106
    {
107
        $user_id = api_get_user_id();
108
        $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
109
        $sql = "SELECT * FROM " . $table_category . "
110
                WHERE user_id=$user_id
111
                ORDER BY sort ASC";
112
        $result = Database::query($sql);
113
        $output = array();
114
        while ($row = Database::fetch_array($result)) {
115
            $output[] = $row;
116
        }
117
118
        return $output;
119
    }
120
121
    /**
122
     * This function get all the courses in the particular user category;
123
     * @return string The name of the user defined course category
124
     */
125
    public function get_courses_in_category()
126
    {
127
        $user_id = api_get_user_id();
128
129
        // table definitions
130
        $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
131
        $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
132
        $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_EXTRA_FIELD);
133
        $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
134
135
        $extraFieldType = ExtraField::COURSE_FIELD_TYPE;
136
137
        // get course list auto-register
138
        $sql = "SELECT item_id
139
                FROM $TABLE_COURSE_FIELD_VALUE tcfv
140
                INNER JOIN $TABLE_COURSE_FIELD tcf
141
                ON tcfv.field_id =  tcf.id
142
                WHERE
143
                    tcf.extra_field_type = $extraFieldType AND
144
                    tcf.variable = 'special_course' AND
145
                    tcfv.value = 1 ";
146
147
        $result = Database::query($sql);
148
        $special_course_list = array();
149 View Code Duplication
        if (Database::num_rows($result) > 0) {
150
            while ($result_row = Database::fetch_array($result)) {
151
                $special_course_list[] = '"' . $result_row['item_id'] . '"';
152
            }
153
        }
154
155
        $without_special_courses = '';
156
        if (!empty($special_course_list)) {
157
            $without_special_courses = ' AND course.id NOT IN (' . implode(',', $special_course_list) . ')';
158
        }
159
160
        $sql = "SELECT
161
                    course.code, course.visual_code, course.subscribe subscr, course.unsubscribe unsubscr,
162
                    course.title title, course.tutor_name tutor, course.directory, course_rel_user.status status,
163
                    course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
164
                FROM $TABLECOURS course,
165
                $TABLECOURSUSER  course_rel_user
166
                WHERE
167
                    course.id = course_rel_user.c_id AND
168
                    course_rel_user.user_id = '" . $user_id . "' AND
169
                    course_rel_user.relation_type <> " . COURSE_RELATION_TYPE_RRHH . "
170
                    $without_special_courses
171
                ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
172
        $result = Database::query($sql);
173
        $data = array();
174
        while ($course = Database::fetch_array($result)) {
175
            $data[$course['user_course_cat']][] = $course;
176
        }
177
178
        return $data;
179
    }
180
181
    /**
182
     * stores  the changes in a course category
183
     * (moving a course to a different course category)
184
     * @param  int    $courseId
185
     * @param  int       Category id
186
     * @return bool      True if it success
187
     */
188
    public function updateCourseCategory($courseId, $newcategory)
189
    {
190
        $courseId = intval($courseId);
191
        $newcategory = intval($newcategory);
192
        $current_user = api_get_user_id();
193
194
        $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
195
        $max_sort_value = api_max_sort_value($newcategory, $current_user);
196
        $sql = "UPDATE $TABLECOURSUSER SET
197
                    user_course_cat='" . $newcategory . "',
198
                    sort='" . ($max_sort_value + 1) . "'
199
                WHERE
200
                    c_id ='" . $courseId . "' AND
201
                    user_id='" . $current_user . "' AND
202
                    relation_type<>" . COURSE_RELATION_TYPE_RRHH;
203
        $resultQuery = Database::query($sql);
204
205
        $result = false;
206
        if (Database::affected_rows($resultQuery)) {
207
            $result = true;
208
        }
209
210
        return $result;
211
    }
212
213
    /**
214
     * moves the course one place up or down
215
     * @param   string    Direction (up/down)
216
     * @param   string    Course code
217
     * @param   int       Category id
218
     * @return  bool      True if it success
219
     */
220
    public function move_course($direction, $course2move, $category)
221
    {
222
        // definition of tables
223
        $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
224
225
        $current_user_id = api_get_user_id();
226
        $all_user_courses = $this->get_courses_of_user($current_user_id);
227
228
        // we need only the courses of the category we are moving in
229
        $user_courses = array();
230
        foreach ($all_user_courses as $key => $course) {
231
            if ($course['user_course_category'] == $category) {
232
                $user_courses[] = $course;
233
            }
234
        }
235
236
        $target_course = array();
237
        foreach ($user_courses as $count => $course) {
238
            if ($course2move == $course['code']) {
239
                // source_course is the course where we clicked the up or down icon
240
                $source_course = $course;
241
                // target_course is the course before/after the source_course (depending on the up/down icon)
242
                if ($direction == 'up') {
243
                    $target_course = $user_courses[$count - 1];
244
                } else {
245
                    $target_course = $user_courses[$count + 1];
246
                }
247
                break;
248
            }
249
        }
250
251
        $result = false;
252
        if (count($target_course) > 0 && count($source_course) > 0) {
253
            $courseInfo = api_get_course_info($source_course['code']);
254
            $courseId = $courseInfo['real_id'];
255
256
            $targetCourseInfo = api_get_course_info($target_course['code']);
257
            $targetCourseId = $targetCourseInfo['real_id'];
258
259
            $sql = "UPDATE $table
260
                    SET sort='" . $target_course['sort'] . "'
261
                    WHERE
262
                        c_id = '" . $courseId . "' AND
263
                        user_id = '" . $current_user_id . "' AND
264
                        relation_type<>" . COURSE_RELATION_TYPE_RRHH;
265
266
            $result1 = Database::query($sql);
267
268
            $sql = "UPDATE $table SET sort='" . $source_course['sort'] . "'
269
                    WHERE
270
                        c_id ='" . $targetCourseId . "' AND
271
                        user_id='" . $current_user_id . "' AND
272
                        relation_type<>" . COURSE_RELATION_TYPE_RRHH;
273
274
            $result2 = Database::query($sql);
275
276
            if (Database::affected_rows($result1) && Database::affected_rows($result2)) {
277
                $result = true;
278
            }
279
        }
280
281
        return $result;
282
    }
283
284
    /**
285
     * Moves the course one place up or down
286
     * @param string    Direction up/down
287
     * @param string    Category id
288
     * @return bool     True If it success
289
     */
290
    public function move_category($direction, $category2move)
291
    {
292
        // the database definition of the table that stores the user defined course categories
293
        $table_user_defined_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
294
295
        $current_user_id = api_get_user_id();
296
        $user_coursecategories = $this->get_user_course_categories();
297
        $user_course_categories_info = $this->get_user_course_categories_info();
298
        $result = false;
299
300
        foreach ($user_coursecategories as $key => $category) {
301
            $category_id = $category['id'];
302
            if ($category2move == $category_id) {
303
                // source_course is the course where we clicked the up or down icon
304
                $source_category = $user_course_categories_info[$category2move];
305
                // target_course is the course before/after the source_course (depending on the up/down icon)
306
                if ($direction == 'up') {
307
                    $target_category = $user_course_categories_info[$user_coursecategories[$key - 1]['id']];
308
                } else {
309
                    $target_category = $user_course_categories_info[$user_coursecategories[$key + 1]['id']];
310
                }
311
            }
312
        }
313
314
        if (count($target_category) > 0 && count($source_category) > 0) {
315
            $sql_update1 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($target_category['sort']) . "'
316
                            WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $current_user_id . "'";
317
            $sql_update2 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($source_category['sort']) . "'
318
                            WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $current_user_id . "'";
319
320
            $result1 = Database::query($sql_update2);
321
            $result2 = Database::query($sql_update1);
322
            if (Database::affected_rows($result1) && Database::affected_rows($result2)) {
323
                $result = true;
324
            }
325
        }
326
        return $result;
327
    }
328
329
    /**
330
     * Retrieves the user defined course categories and all the info that goes with it
331
     * @return array containing all the info of the user defined courses categories with the id as key of the array
332
     */
333 View Code Duplication
    public function get_user_course_categories_info()
334
    {
335
        $current_user_id = api_get_user_id();
336
        $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
337
        $sql = "SELECT * FROM " . $table_category . "
338
                WHERE user_id='" . $current_user_id . "'
339
                ORDER BY sort ASC";
340
        $result = Database::query($sql);
341
        while ($row = Database::fetch_array($result)) {
342
            $output[$row['id']] = $row;
343
        }
344
        return $output;
345
    }
346
347
    /**
348
     * Updates the user course category in the chamilo_user database
349
     * @param   string  Category title
350
     * @param   int     Category id
351
     * @return  bool    True if it success
352
     */
353 View Code Duplication
    public function store_edit_course_category($title, $category_id)
354
    {
355
        // protect data
356
        $title = Database::escape_string($title);
357
        $category_id = intval($category_id);
358
        $result = false;
359
        $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
360
        $sql = "UPDATE $tucc
361
                SET title='" . api_htmlentities($title, ENT_QUOTES, api_get_system_encoding()) . "'
362
                WHERE id='" . $category_id . "'";
363
        $resultQuery = Database::query($sql);
364
        if (Database::affected_rows($resultQuery)) {
365
            $result = true;
366
        }
367
        return $result;
368
    }
369
370
    /**
371
     * deletes a course category and moves all the courses that were in this category to main category
372
     * @param   int     Category id
373
     * @return  bool    True if it success
374
     */
375
    public function delete_course_category($category_id)
376
    {
377
        $current_user_id = api_get_user_id();
378
        $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
379
        $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
380
        $category_id = intval($category_id);
381
        $result = false;
382
        $sql = "DELETE FROM $tucc
383
                WHERE 
384
                    id='" . $category_id . "' AND 
385
                    user_id='" . $current_user_id . "'";
386
        $resultQuery = Database::query($sql);
387
       if (Database::affected_rows($resultQuery)) {
388
            $result = true;
389
        }
390
        $sql = "UPDATE $TABLECOURSUSER
391
                SET user_course_cat='0'
392
                WHERE
393
                    user_course_cat='" . $category_id . "' AND
394
                    user_id='" . $current_user_id . "' AND
395
                    relation_type<>" . COURSE_RELATION_TYPE_RRHH . " ";
396
        Database::query($sql);
397
398
        return $result;
399
    }
400
401
    /**
402
     * Search the courses database for a course that matches the search term.
403
     * The search is done on the code, title and tutor field of the course table.
404
     * @param string $search_term The string that the user submitted, what we are looking for
405
     * @param array $limit
406
     * @param boolean $justVisible search only on visible courses in the catalogue
407
     * @return array An array containing a list of all the courses matching the the search term.
408
     */
409
    public function search_courses($search_term, $limit, $justVisible = false)
410
    {
411
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
412
        $extraFieldTable = Database :: get_main_table(TABLE_EXTRA_FIELD);
413
        $extraFieldValuesTable = Database :: get_main_table(TABLE_EXTRA_FIELD_VALUES);
414
415
        $limitFilter = CourseCategory::getLimitFilterFromArray($limit);
416
417
        // get course list auto-register
418
        $sql = "SELECT item_id
419
                FROM $extraFieldValuesTable tcfv
420
                INNER JOIN $extraFieldTable tcf ON tcfv.field_id =  tcf.id
421
                WHERE
422
                    tcf.variable = 'special_course' AND
423
                    tcfv.value = 1 ";
424
425
        $special_course_result = Database::query($sql);
426 View Code Duplication
        if (Database::num_rows($special_course_result) > 0) {
427
            $special_course_list = array();
428
            while ($result_row = Database::fetch_array($special_course_result)) {
429
                $special_course_list[] = '"' . $result_row['item_id'] . '"';
430
            }
431
        }
432
        $without_special_courses = '';
433
        if (!empty($special_course_list)) {
434
            $without_special_courses = ' AND course.code NOT IN (' . implode(',', $special_course_list) . ')';
435
        }
436
437
        $visibilityCondition = $justVisible ? CourseManager::getCourseVisibilitySQLCondition('course') : '';
438
439
        $search_term_safe = Database::escape_string($search_term);
440
        $sql_find = "SELECT * FROM $courseTable
441
                    WHERE (
442
                            code LIKE '%" . $search_term_safe . "%' OR
443
                            title LIKE '%" . $search_term_safe . "%' OR
444
                            tutor_name LIKE '%" . $search_term_safe . "%'
445
                        )
446
                        $without_special_courses
447
                        $visibilityCondition
448
                    ORDER BY title, visual_code ASC
449
                    $limitFilter
450
                    ";
451
452
        if (api_is_multiple_url_enabled()) {
453
            $url_access_id = api_get_current_access_url_id();
454
            if ($url_access_id != -1) {
455
                $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
456
                $sql_find = "SELECT *
457
                            FROM $courseTable as course
458
                            INNER JOIN $tbl_url_rel_course as url_rel_course
459
                            ON (url_rel_course.c_id = course.id)
460
                            WHERE
461
                                access_url_id = $url_access_id AND (
462
                                    code LIKE '%" . $search_term_safe . "%' OR
463
                                    title LIKE '%" . $search_term_safe . "%' OR
464
                                    tutor_name LIKE '%" . $search_term_safe . "%'
465
                                )
466
                                $without_special_courses
467
                                $visibilityCondition
468
                            ORDER BY title, visual_code ASC
469
                            $limitFilter
470
                            ";
471
            }
472
        }
473
        $result_find = Database::query($sql_find);
474
        $courses = array();
475
        while ($row = Database::fetch_array($result_find)) {
476
            $row['registration_code'] = !empty($row['registration_code']);
477
            $count_users = count(CourseManager::get_user_list_from_course_code($row['code']));
478
            $count_connections_last_month = Tracking::get_course_connections_count(
479
                    $row['id'], 0, api_get_utc_datetime(time() - (30 * 86400))
480
            );
481
482
            $point_info = CourseManager::get_course_ranking($row['id'], 0);
483
484
            $courses[] = array(
485
                'real_id' => $row['id'],
486
                'point_info' => $point_info,
487
                'code' => $row['code'],
488
                'directory' => $row['directory'],
489
                'visual_code' => $row['visual_code'],
490
                'title' => $row['title'],
491
                'tutor' => $row['tutor_name'],
492
                'subscribe' => $row['subscribe'],
493
                'unsubscribe' => $row['unsubscribe'],
494
                'registration_code' => $row['registration_code'],
495
                'creation_date' => $row['creation_date'],
496
                'visibility' => $row['visibility'],
497
                'count_users' => $count_users,
498
                'count_connections' => $count_connections_last_month
499
            );
500
        }
501
        return $courses;
502
    }
503
504
    /**
505
     * unsubscribe the user from a given course
506
     * @param   string  $course_code
507
     * @return  bool    True if it success
508
     */
509
    public function remove_user_from_course($course_code)
510
    {
511
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
512
513
        // protect variables
514
        $current_user_id = api_get_user_id();
515
        $course_code = Database::escape_string($course_code);
516
        $result = true;
517
518
        $courseInfo = api_get_course_info($course_code);
519
        $courseId = $courseInfo['real_id'];
520
521
        // we check (once again) if the user is not course administrator
522
        // because the course administrator cannot unsubscribe himself
523
        // (s)he can only delete the course
524
        $sql = "SELECT * FROM $tbl_course_user
525
                WHERE
526
                    user_id='" . $current_user_id . "' AND
527
                    c_id ='" . $courseId . "' AND
528
                    status='1' ";
529
        $result_check = Database::query($sql);
530
        $number_of_rows = Database::num_rows($result_check);
531
        if ($number_of_rows > 0) {
532
            $result = false;
533
        }
534
535
        CourseManager::unsubscribe_user($current_user_id, $course_code);
536
537
        return $result;
538
    }
539
540
    /**
541
     * stores the user course category in the chamilo_user database
542
     * @param   string  Category title
543
     * @return  bool    True if it success
544
     */
545
    public function store_course_category($category_title)
546
    {
547
        $tucc = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
548
549
        // protect data
550
        $current_user_id = api_get_user_id();
551
        $category_title = Database::escape_string($category_title);
552
        $result = false;
553
554
        // step 1: we determine the max value of the user defined course categories
555
        $sql = "SELECT sort FROM $tucc 
556
                WHERE user_id='" . $current_user_id . "' 
557
                ORDER BY sort DESC";
558
        $rs_sort = Database::query($sql);
559
        $maxsort = Database::fetch_array($rs_sort);
560
        $nextsort = $maxsort['sort'] + 1;
561
562
        // step 2: we check if there is already a category with this name, if not we store it, else we give an error.
563
        $sql = "SELECT * FROM $tucc 
564
                WHERE 
565
                    user_id='" . $current_user_id . "' AND 
566
                    title='" . $category_title . "'
567
                ORDER BY sort DESC";
568
        $rs = Database::query($sql);
569
        if (Database::num_rows($rs) == 0) {
570
            $sql = "INSERT INTO $tucc (user_id, title,sort)
571
                           VALUES ('" . $current_user_id . "', '" . api_htmlentities($category_title, ENT_QUOTES, api_get_system_encoding()) . "', '" . $nextsort . "')";
572
            $resultQuery = Database::query($sql);
573
            if (Database::affected_rows($resultQuery)) {
574
                $result = true;
575
            }
576
        } else {
577
            $result = false;
578
        }
579
580
        return $result;
581
    }
582
583
    /**
584
     * Counts the number of courses in a given course category
585
     * @param   string $categoryCode Category code
586
     * @param $searchTerm
587
     * @return  int     Count of courses
588
     */
589
    public function count_courses_in_category($categoryCode, $searchTerm = '')
590
    {
591
        return CourseCategory::countCoursesInCategory($categoryCode, $searchTerm);
592
    }
593
594
    /**
595
     * get the browsing of the course categories (faculties)
596
     * @return array    array containing a list with all the categories and subcategories(if needed)
597
     */
598
    public function browse_course_categories()
599
    {
600
        return CourseCategory::browseCourseCategories();
601
    }
602
603
    /**
604
     * Display all the courses in the given course category. I could have used a parameter here
605
     * @param string $categoryCode Category code
606
     * @param int $randomValue
607
     * @param array $limit will be used if $random_value is not set.
608
     * This array should contains 'start' and 'length' keys
609
     * @return array Courses data
610
     */
611
    public function browse_courses_in_category($categoryCode, $randomValue = null, $limit = array())
612
    {
613
        return CourseCategory::browseCoursesInCategory($categoryCode, $randomValue, $limit);
614
    }
615
616
    /**
617
     * Subscribe the user to a given course
618
     * @param string $course_code Course code
619
     * @return string  Message about results
620
     */
621
    public function subscribe_user($course_code)
622
    {
623
        $user_id = api_get_user_id();
624
        $all_course_information = CourseManager::get_course_information($course_code);
625
626
        if (
627
            $all_course_information['registration_code'] == '' ||
628
            (
629
                isset($_POST['course_registration_code']) &&
630
                $_POST['course_registration_code'] == $all_course_information['registration_code']
631
            )
632
        ) {
633
            if (api_is_platform_admin()) {
634
                $status_user_in_new_course = COURSEMANAGER;
635
            } else {
636
                $status_user_in_new_course = null;
637
            }
638
            if (CourseManager::add_user_to_course($user_id, $course_code, $status_user_in_new_course)) {
639
                $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course_code);
640
                if ($send == 1) {
641
                    CourseManager::email_to_tutor($user_id, $all_course_information['real_id'], $send_to_tutor_also = false);
642
                } else if ($send == 2) {
643
                    CourseManager::email_to_tutor($user_id, $all_course_information['real_id'], $send_to_tutor_also = true);
644
                }
645
                $url = Display::url($all_course_information['title'], api_get_course_url($course_code));
646
                $message = sprintf(get_lang('EnrollToCourseXSuccessful'), $url);
647
            } else {
648
                $message = get_lang('ErrorContactPlatformAdmin');
649
            }
650
            return array('message' => $message);
651
        } else {
652
            if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) {
653
                return false;
654
            }
655
            $message = get_lang('CourseRequiresPassword') . '<br />';
656
            $message .= $all_course_information['title'].' ('.$all_course_information['visual_code'].') ';
657
658
            $action  = api_get_path(WEB_CODE_PATH) . "auth/courses.php?action=subscribe_user_with_password&sec_token=" . $_SESSION['sec_token'];
659
            $form = new FormValidator('subscribe_user_with_password', 'post', $action);
660
            $form->addElement('hidden', 'sec_token', $_SESSION['sec_token']);
661
            $form->addElement('hidden', 'subscribe_user_with_password', $all_course_information['code']);
662
            $form->addElement('text', 'course_registration_code');
663
            $form->addButton('submit', get_lang('SubmitRegistrationCode'));
664
            $content = $form->returnForm();
665
666
            return array('message' => $message, 'content' => $content);
667
        }
668
    }
669
670
    /**
671
     * List the sessions
672
     * @param string $date (optional) The date of sessions
673
     * @param array $limit
674
     * @return array The session list
675
     */
676
    public function browseSessions($date = null, $limit = array())
677
    {
678
        $em = Database::getManager();
679
        $qb = $em->createQueryBuilder();
680
681
        $_sessions = $qb->select('s')->from('ChamiloCoreBundle:Session', 's');
682
683
        if (!empty($limit)) {
684
            $_sessions->setFirstResult($limit['start'])
685
                ->setMaxResults($limit['length']);
686
        }
687
688
        $_sessions->where(
689
            $qb->expr()->gt('s.nbrCourses', 0)
690
        );
691
692
        if (!is_null($date)) {
693
            $_sessions
694
                ->andWhere(
695
                    $qb->expr()->orX(
696
                        $qb->expr()->between(':date', 's.accessStartDate', 's.accessEndDate'),
697
                        $qb->expr()->isNull('s.accessEndDate'),
698
                        $qb->expr()->andX(
699
                            $qb->expr()->isNull('s.accessStartDate'),
700
                            $qb->expr()->isNotNull('s.accessEndDate'),
701
                            $qb->expr()->gt('s.accessEndDate', ':date')
702
                        )
703
                    )
704
                )
705
                ->setParameter('date', $date);
706
        }
707
708
        return $_sessions->getQuery()->getResult();
709
    }
710
711
    /**
712
     * Return a COUNT from Session table
713
     * @param string $date in Y-m-d format
714
     * @return int
715
     */
716
    public function countSessions($date = null)
717
    {
718
        $count = 0;
719
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
720
        $date = Database::escape_string($date);
721
        $dateFilter = '';
722
        if (!empty($date)) {
723
            $dateFilter = <<<SQL
724
                AND ('$date' BETWEEN s.access_start_date AND s.access_end_date)
725
                OR (s.access_end_date IS NULL)
726
                OR (s.access_start_date IS NULL AND
727
                s.access_end_date IS NOT NULL AND s.access_end_date > '$date')
728
SQL;
729
        }
730
        $sql = "SELECT COUNT(*) FROM $sessionTable s WHERE 1 = 1 $dateFilter";
731
        $res = Database::query($sql);
732
        if ($res !== false && Database::num_rows($res) > 0) {
733
            $count = current(Database::fetch_row($res));
734
        }
735
736
        return $count;
737
    }
738
739
    /**
740
     * Search sessions by the tags in their courses
741
     * @param string $termTag Term for search in tags
742
     * @param array $limit Limit info
743
     * @return array The sessions
744
     */
745
    public function browseSessionsByTags($termTag, array $limit)
746
    {
747
        $em = Database::getManager();
748
        $qb = $em->createQueryBuilder();
749
750
        $sessions = $qb->select('s')
751
            ->distinct(true)
752
            ->from('ChamiloCoreBundle:Session', 's')
753
            ->innerJoin(
754
                'ChamiloCoreBundle:SessionRelCourse',
755
                'src',
756
                \Doctrine\ORM\Query\Expr\Join::WITH,
757
                's.id = src.session'
758
            )
759
            ->innerJoin(
760
                'ChamiloCoreBundle:ExtraFieldRelTag',
761
                'frt',
762
                \Doctrine\ORM\Query\Expr\Join::WITH,
763
                'src.course = frt.itemId'
764
            )
765
            ->innerJoin(
766
                'ChamiloCoreBundle:Tag',
767
                't',
768
                \Doctrine\ORM\Query\Expr\Join::WITH,
769
                'frt.tagId = t.id'
770
            )
771
            ->innerJoin(
772
                'ChamiloCoreBundle:ExtraField',
773
                'f',
774
                \Doctrine\ORM\Query\Expr\Join::WITH,
775
                'frt.fieldId = f.id'
776
            )
777
            ->where(
778
                $qb->expr()->like('t.tag', ":tag")
779
            )
780
            ->andWhere(
781
                $qb->expr()->eq('f.extraFieldType', ExtraField::COURSE_FIELD_TYPE)
782
            )
783
            ->setFirstResult($limit['start'])
784
            ->setMaxResults($limit['length'])
785
            ->setParameter('tag', "$termTag%")
786
            ->getQuery()
787
            ->getResult();
788
789
        $sessionsToBrowse = [];
790
        foreach ($sessions as $session) {
791
            if ($session->getNbrCourses() === 0) {
792
                continue;
793
            }
794
            $sessionsToBrowse[] = $session;
795
        }
796
797
        return $sessionsToBrowse;
798
    }
799
800
    /**
801
     * Search sessions by searched term by session name
802
     * @param string $queryTerm Term for search
803
     * @param array $limit Limit info
804
     * @return array The sessions
805
     */
806
    public function browseSessionsBySearch($queryTerm, array $limit)
807
    {
808
        $sessionsToBrowse = [];
809
810
        $criteria = Doctrine\Common\Collections\Criteria::create()
811
            ->where(
812
                Doctrine\Common\Collections\Criteria::expr()->contains('name', $queryTerm)
813
            )
814
            ->setFirstResult($limit['start'])
815
            ->setMaxResults($limit['length']);
816
817
        $sessions = Database::getManager()
818
                ->getRepository('ChamiloCoreBundle:Session')
819
                ->matching($criteria);
820
821
        foreach ($sessions as $session) {
822
            if ($session->getNbrCourses() === 0) {
823
                continue;
824
            }
825
826
            $sessionsToBrowse[] = $session;
827
        }
828
829
        return $sessionsToBrowse;
830
    }
831
}
832