diff --git a/public/main/inc/lib/course.lib.php b/public/main/inc/lib/course.lib.php index b812b10a61..1b1b868f62 100644 --- a/public/main/inc/lib/course.lib.php +++ b/public/main/inc/lib/course.lib.php @@ -1058,7 +1058,7 @@ class CourseManager * @return array an array with the course info of all the courses (real and virtual) * of which the current user is course admin */ - public static function get_course_list_of_user_as_course_admin($userId, $keyword = '') + public static function get_course_list_of_user_as_course_admin($userId, $keyword = ''): array { $user = api_get_user_entity($userId); @@ -1067,10 +1067,9 @@ class CourseManager } $url = api_get_url_entity(); - $user = api_get_user_entity($userId); - $repo = Container::getUserRepository(); + $repo = Container::getCourseRepository(); - return $repo->getCourses($user, $url, COURSEMANAGER, $keyword); + return $repo->getCoursesInfoByUser($user, $url, COURSEMANAGER, $keyword); } /** diff --git a/public/main/my_space/current_courses.php b/public/main/my_space/current_courses.php index a6d5b21f3e..74b5ae5103 100644 --- a/public/main/my_space/current_courses.php +++ b/public/main/my_space/current_courses.php @@ -25,9 +25,9 @@ $i = 0; $session_id = 0; if (!empty($my_courses)) { foreach ($my_courses as $course) { - $course_code = $course['code']; - $course_id = $course['real_id']; - $course_info = api_get_course_info($course_code); + $course_info = api_get_course_info_by_id($course['id']); + $course_id = $course['id']; + $course_code = $course_info['code']; //Only show open courses if (0 == $course_info['visibility']) { @@ -43,7 +43,7 @@ if (!empty($my_courses)) { } } - $tmp_students = CourseManager :: get_student_list_from_course_code($course_code, false); + $tmp_students = CourseManager::get_student_list_from_course_code($course_code, false); //Cleaning students only REAL students $students = []; @@ -56,8 +56,9 @@ if (!empty($my_courses)) { } $t_lp = Database::get_course_table(TABLE_LP_MAIN); - $sql_lp = "SELECT lp.title, lp.id FROM $t_lp lp - WHERE c_id = $course_id AND lp.session_id = 0"; + $sql_lp = "SELECT lp.title, lp.iid FROM $t_lp lp + INNER JOIN resource_link li ON lp.resource_node_id = li.id + WHERE li.c_id = $course_id AND li.session_id = 0"; $rs_lp = Database::query($sql_lp); $t_lpi = Database::get_course_table(TABLE_LP_ITEM); $t_news = Database::get_course_table(TABLE_ANNOUNCEMENT); @@ -74,7 +75,7 @@ if (!empty($my_courses)) { if (Database :: num_rows($rs_lp) > 0) { while ($learnpath = Database :: fetch_array($rs_lp)) { - $lp_id = $learnpath['id']; + $lp_id = $learnpath['iid']; $lp_items = $array[$i]['lp'] = ''.$learnpath['title'].''; @@ -213,12 +214,14 @@ $headers = [ ]; if (isset($_GET['export'])) { - $list = [ - 0 => $headers, - 1 => $array[0], - ]; - Export::arrayToXls($list, $filename); - exit; + if (!empty($array[0])) { + $list = [ + 0 => $headers, + 1 => $array[0], + ]; + Export::arrayToXls($list, $filename); + exit; + } } $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Reporting')]; diff --git a/src/CoreBundle/Repository/Node/CourseRepository.php b/src/CoreBundle/Repository/Node/CourseRepository.php index 4bffacc847..4bb3d74fd9 100644 --- a/src/CoreBundle/Repository/Node/CourseRepository.php +++ b/src/CoreBundle/Repository/Node/CourseRepository.php @@ -84,6 +84,44 @@ class CourseRepository extends ResourceRepository return $query->getResult(); } + /** + * Get info from courses where the user has the given role. + * @param User $user + * @param AccessUrl $url + * @param int $status + * @param string $keyword + * @return Course[] + */ + public function getCoursesInfoByUser(User $user, AccessUrl $url, int $status, string $keyword = ''): array + { + $qb = $this->getEntityManager()->createQueryBuilder(); + + $qb + ->select('DISTINCT c.id') + ->from(Course::class, 'c') + ->innerJoin(CourseRelUser::class, 'courseRelUser') + ->innerJoin('c.urls', 'accessUrlRelCourse') + ->where('accessUrlRelCourse.url = :url') + ->andWhere('courseRelUser.user = :user') + ->andWhere('courseRelUser.status = :status') + ->setParameters([ + 'user' => $user, + 'url' => $url, + 'status' => $status + ]) + ; + if (!empty($keyword)) { + $qb + ->andWhere('c.title like = :keyword OR c.code like = :keyword') + ->setParameter('keyword', $keyword) + ; + } + + $query = $qb->getQuery(); + + return $query->getResult(); + } + /** * Get all users that are registered in the course. No matter the status. * diff --git a/src/CoreBundle/Repository/Node/UserRepository.php b/src/CoreBundle/Repository/Node/UserRepository.php index d58874cd55..7cd5c9f4fe 100644 --- a/src/CoreBundle/Repository/Node/UserRepository.php +++ b/src/CoreBundle/Repository/Node/UserRepository.php @@ -242,48 +242,6 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter return $qb->getQuery()->getResult(); } - /** - * Get course user relationship based in the course_rel_user table. - * - * @return Course[] - */ - public function getCourses(User $user, AccessUrl $url, int $status, string $keyword = '') - { - $qb = $this->createQueryBuilder('u'); - - $qb - // ->select('DISTINCT course') - ->innerJoin('u.courses', 'courseRelUser') - ->innerJoin('courseRelUser.course', 'course') - ->innerJoin('course.urls', 'accessUrlRelCourse') - ->innerJoin('accessUrlRelCourse.url', 'url') - ->where('url = :url') - ->andWhere('courseRelUser.user = :user') - ->andWhere('courseRelUser.status = :status') - ->setParameters( - [ - 'user' => $user, - 'url' => $url, - 'status' => $status, - ] - ) - // ->addSelect('courseRelUser') - ; - - if (!empty($keyword)) { - $qb - ->andWhere('course.title like = :keyword OR course.code like = :keyword') - ->setParameter('keyword', $keyword) - ; - } - - $qb->orderBy('course.title', Criteria::DESC); - - $query = $qb->getQuery(); - - return $query->getResult(); - } - /** * Get the coaches for a course within a session. *