Skip to content

Commit 4ab6d1a

Browse files
committed
Internal - Refactor code
1 parent 3ef0908 commit 4ab6d1a

File tree

9 files changed

+27
-69
lines changed

9 files changed

+27
-69
lines changed

public/main/admin/course_edit.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@
169169
$courseTeacherNames = [];
170170

171171
foreach ($course_teachers as $courseTeacherId) {
172-
/** @var User $courseTeacher */
173-
$courseTeacher = UserManager::getRepository()->find($courseTeacherId);
172+
$courseTeacher = api_get_user_entity($courseTeacherId);
174173
$courseTeacherNames[$courseTeacher->getId()] = UserManager::formatUserFullName($courseTeacher, true);
175174
}
176175

public/main/cron/user_import/resend_email_with_new_password.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
$pass = api_substr($row['username'], 0, 4).rand(0, 9).rand(0, 9);
4343

4444
if ($user) {
45-
/** @var User $user */
46-
$user = $repository->find($row['user_id']);
45+
$user = api_get_user_entity($row['user_id']);
4746
$user->setPlainPassword($pass);
4847
$userManager->updateUser($user, true);
4948
} else {

public/main/inc/lib/api.lib.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Chamilo\CoreBundle\Entity\Session as SessionEntity;
99
use Chamilo\CoreBundle\Entity\SettingsCurrent;
1010
use Chamilo\CoreBundle\Entity\User;
11+
use Chamilo\CoreBundle\Entity\UserCourseCategory;
1112
use Chamilo\CoreBundle\Framework\Container;
1213
use Chamilo\CourseBundle\Entity\CGroup;
1314
use Chamilo\CourseBundle\Entity\CLp;
@@ -1842,7 +1843,7 @@ function api_get_user_info_from_entity(
18421843
return $result;
18431844
}
18441845

1845-
function api_get_lp_entity(int $id): ? CLp
1846+
function api_get_lp_entity(int $id): ?CLp
18461847
{
18471848
return Database::getManager()->getRepository(CLp::class)->find($id);
18481849
}
@@ -1858,10 +1859,7 @@ function api_get_user_entity(int $userId = 0): ?User
18581859
return $user;
18591860
}
18601861

1861-
/**
1862-
* @return User|null
1863-
*/
1864-
function api_get_current_user()
1862+
function api_get_current_user(): ?User
18651863
{
18661864
$isLoggedIn = Container::$container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED');
18671865
if (false === $isLoggedIn) {
@@ -2302,10 +2300,8 @@ function api_get_group_entity($id = 0): ?CGroup
23022300

23032301
/**
23042302
* @param int $id
2305-
*
2306-
* @return AccessUrl
23072303
*/
2308-
function api_get_url_entity($id = 0)
2304+
function api_get_url_entity($id = 0): ?AccessUrl
23092305
{
23102306
if (empty($id)) {
23112307
$id = api_get_current_access_url_id();
@@ -4576,29 +4572,17 @@ function api_get_themes($getOnlyThemeFromVirtualInstance = false)
45764572
* This function is used when we are moving a course to a different category
45774573
* and also when a user subscribes to courses (the new course is added at the end of the main category.
45784574
*
4579-
* @author Patrick Cool <[email protected]>, Ghent University
4580-
*
4581-
* @param int $user_course_category the id of the user_course_category
4582-
* @param int $user_id
4575+
* @param int $courseCategoryId the id of the user_course_category
4576+
* @param int $userId
45834577
*
45844578
* @return int the value of the highest sort of the user_course_category
45854579
*/
4586-
function api_max_sort_value($user_course_category, $user_id)
4580+
function api_max_sort_value($courseCategoryId, $userId)
45874581
{
4588-
$tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
4589-
$sql = "SELECT max(sort) as max_sort FROM $tbl_course_user
4590-
WHERE
4591-
user_id='".intval($user_id)."' AND
4592-
relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
4593-
user_course_cat='".intval($user_course_category)."'";
4594-
$result_max = Database::query($sql);
4595-
if (1 == Database::num_rows($result_max)) {
4596-
$row_max = Database::fetch_array($result_max);
4582+
$user = api_get_user_entity($userId);
4583+
$userCourseCategory = Database::getManager()->getRepository(UserCourseCategory::class)->find($courseCategoryId);
45974584

4598-
return $row_max['max_sort'];
4599-
}
4600-
4601-
return 0;
4585+
return null === $user ? 0 : $user->getMaxSortValue($userCourseCategory);
46024586
}
46034587

46044588
/**

public/main/inc/lib/course.lib.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
652652
$userId = api_get_user_id();
653653

654654
if (api_get_configuration_value('catalog_course_subscription_in_user_s_session')) {
655-
/** @var \Chamilo\CoreBundle\Entity\User $user */
656-
$user = UserManager::getRepository()->find($userId);
655+
$user = api_get_user_entity($userId);
657656
$sessions = $user->getCurrentlyAccessibleSessions();
658657
if (empty($sessions)) {
659658
// user has no accessible session

public/main/inc/lib/sessionmanager.lib.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4370,24 +4370,6 @@ public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin
43704370
return Database::store_result($result, 'ASSOC');
43714371
}
43724372

4373-
/**
4374-
* @param int $user_id
4375-
*
4376-
* @return array
4377-
*
4378-
* @deprecated use get_sessions_by_general_coach()
4379-
*/
4380-
public static function get_sessions_by_coach($user_id)
4381-
{
4382-
$session_table = Database::get_main_table(TABLE_MAIN_SESSION);
4383-
4384-
return Database::select(
4385-
'*',
4386-
$session_table,
4387-
['where' => ['id_coach = ?' => $user_id]]
4388-
);
4389-
}
4390-
43914373
/**
43924374
* @param int $user_id
43934375
* @param int $courseId

public/main/inc/lib/usermanager.lib.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ public static function isPasswordValid($encoded, $raw, $salt)
111111
*/
112112
public static function updatePassword($userId, $password)
113113
{
114-
$repository = self::getRepository();
115-
/** @var User $user */
116-
$user = $repository->find($userId);
114+
$user = api_get_user_entity($userId);
117115
$userManager = self::getManager();
118116
$user->setPlainPassword($password);
119117
$userManager->updateUser($user, true);

public/main/session/add_users_to_session.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ function search_users($needle, $type)
174174
WHERE
175175
access_url_id = '$access_url_id' AND
176176
(
177-
username LIKE '$needle%' OR
178-
lastname LIKE '$needle%' OR
177+
username LIKE '$needle%' OR
178+
lastname LIKE '$needle%' OR
179179
firstname LIKE '$needle%'
180-
) AND
181-
user.status <> 6 AND
180+
) AND
181+
user.status <> 6 AND
182182
user.status <> ".DRH."
183183
$order_clause LIMIT 11
184184
";
@@ -275,7 +275,7 @@ function add_user_to_session (code, content) {
275275
}
276276
destination.options[destination.length] = new Option(content,code);
277277
destination.selectedIndex = -1;
278-
278+
279279
$("#remove_user").show();
280280
sortOptions(destination.options);
281281
}
@@ -371,8 +371,6 @@ function change_select(val) {
371371
continue;
372372
}
373373
}
374-
375-
unset($users); //clean to free memory
376374
} else {
377375
// Filter by Extra Fields
378376
$extra_field_result = [];

public/main/session/index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
$userId = api_get_user_id();
4444
$session_info = SessionManager::fetch($session_id);
45-
$session_list = SessionManager::get_sessions_by_coach(api_get_user_id());
45+
$session_list = SessionManager::get_sessions_by_general_coach(api_get_user_id());
4646
$courseList = SessionManager::get_course_list_by_session_id($session_id);
4747
$userIsGeneralCoach = SessionManager::user_is_general_coach($userId, $session_id);
4848

@@ -70,7 +70,8 @@
7070
if (!empty($exerciseList)) {
7171
// Exercises
7272
foreach ($exerciseList as $exerciseInfo) {
73-
$exerciseId = $exerciseInfo['id'];
73+
// @todo check visibility
74+
/*$exerciseId = $exerciseInfo['id'];
7475
$visibility = api_get_item_visibility(
7576
$course,
7677
TOOL_QUIZ,
@@ -79,7 +80,7 @@
7980
);
8081
if (0 == $visibility) {
8182
continue;
82-
}
83+
}*/
8384
$exerciseListNew[] = $exerciseInfo;
8485
}
8586
}

public/main/session/resume_session.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Chamilo\CoreBundle\Entity\SequenceResource;
55
use Chamilo\CoreBundle\Entity\Session;
6+
use Chamilo\CoreBundle\Entity\Promotion;
67
use Chamilo\CoreBundle\Entity\SessionRelCourse;
78
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
89
use Chamilo\CoreBundle\Repository\SequenceRepository;
@@ -47,10 +48,7 @@
4748

4849
$em = Database::getManager();
4950
$sessionInfo = api_get_session_info($sessionId);
50-
/** @var SessionRepository $sessionRepository */
51-
$sessionRepository = $em->getRepository('ChamiloCoreBundle:Session');
52-
/** @var Session $session */
53-
$session = $sessionRepository->find($sessionId);
51+
$session = api_get_session_entity($sessionId);
5452
$sessionCategory = $session->getCategory();
5553

5654
$action = isset($_GET['action']) ? $_GET['action'] : null;
@@ -376,7 +374,7 @@
376374
}
377375

378376
/** @var SequenceRepository $repo */
379-
$repo = $em->getRepository('ChamiloCoreBundle:SequenceResource');
377+
$repo = $em->getRepository(SequenceResource::class);
380378
$requirementAndDependencies = $repo->getRequirementAndDependencies(
381379
$sessionId,
382380
SequenceResource::SESSION_TYPE
@@ -395,7 +393,7 @@
395393

396394
$promotion = null;
397395
if (!empty($sessionInfo['promotion_id'])) {
398-
$promotion = $em->getRepository('ChamiloCoreBundle:Promotion');
396+
$promotion = $em->getRepository(Promotion::class);
399397
$promotion = $promotion->find($sessionInfo['promotion_id']);
400398
}
401399

0 commit comments

Comments
 (0)