improve queries, format code

pull/2487/head
jmontoyaa 8 years ago
parent 47b7a94f49
commit 76501eb534
  1. 55
      main/inc/lib/course.lib.php
  2. 3
      main/inc/lib/sessionmanager.lib.php
  3. 21
      main/inc/lib/usermanager.lib.php

@ -2956,8 +2956,8 @@ class CourseManager
/** /**
* Gets the value of a course extra field. Returns null if it was not found * Gets the value of a course extra field. Returns null if it was not found
* *
* @param string Name of the extra field * @param string $variable Name of the extra field
* @param string Course code * @param string $code Course code
* *
* @return string Value * @return string Value
*/ */
@ -3023,7 +3023,8 @@ class CourseManager
public static function get_course_category($code) public static function get_course_category($code)
{ {
$table_categories = Database::get_main_table(TABLE_MAIN_CATEGORY); $table_categories = Database::get_main_table(TABLE_MAIN_CATEGORY);
$sql = "SELECT * FROM $table_categories WHERE code = '$code';"; $code = Database::escape_string($code);
$sql = "SELECT * FROM $table_categories WHERE code = '$code'";
return Database::fetch_array(Database::query($sql)); return Database::fetch_array(Database::query($sql));
} }
@ -3052,7 +3053,7 @@ class CourseManager
* @return int $course_id The number of rows in the given table. * @return int $course_id The number of rows in the given table.
* @deprecated * @deprecated
*/ */
public static function count_rows_course_table($table, $session_id = '', $course_id = null) public static function count_rows_course_table($table, $session_id = '', $course_id = 0)
{ {
$condition_session = ''; $condition_session = '';
if ($session_id !== '') { if ($session_id !== '') {
@ -3269,6 +3270,7 @@ class CourseManager
$courses[$row['code']] = $row; $courses[$row['code']] = $row;
} }
} }
return $courses; return $courses;
} }
@ -3397,6 +3399,7 @@ class CourseManager
$html .= '<div class="pull-right course-box-actions">' . $params['right_actions'] . '</div>'; $html .= '<div class="pull-right course-box-actions">' . $params['right_actions'] . '</div>';
$html .= '</div>'; $html .= '</div>';
$html .= '</div>'; $html .= '</div>';
return $html; return $html;
} }
@ -3432,8 +3435,8 @@ class CourseManager
* *
* Special courses are courses that stick on top of the list and are "auto-registerable" * Special courses are courses that stick on top of the list and are "auto-registerable"
* in the sense that any user clicking them is registered as a student * in the sense that any user clicking them is registered as a student
* @param int User id * @param int $user_id User id
* @param bool Whether to show the document quick-loader or not * @param bool $load_dirs Whether to show the document quick-loader or not
* @return string * @return string
*/ */
public static function returnSpecialCourses($user_id, $load_dirs = false) public static function returnSpecialCourses($user_id, $load_dirs = false)
@ -3441,9 +3444,7 @@ class CourseManager
$user_id = intval($user_id); $user_id = intval($user_id);
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
$tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$special_course_list = self::get_special_course_list(); $special_course_list = self::get_special_course_list();
$with_special_courses = ''; $with_special_courses = '';
if (!empty($special_course_list)) { if (!empty($special_course_list)) {
$with_special_courses = ' course.code IN ("' . implode('","', $special_course_list) . '")'; $with_special_courses = ' course.code IN ("' . implode('","', $special_course_list) . '")';
@ -3462,8 +3463,9 @@ class CourseManager
course_rel_user.user_id course_rel_user.user_id
FROM $tbl_course course FROM $tbl_course course
LEFT JOIN $tbl_course_user course_rel_user LEFT JOIN $tbl_course_user course_rel_user
ON course.id = course_rel_user.c_id AND course_rel_user.user_id = '$user_id' ON (course.id = course_rel_user.c_id)
WHERE $with_special_courses group by course.code"; WHERE course_rel_user.user_id = '$user_id' $with_special_courses
GROUP BY course.code";
$rs_special_course = Database::query($sql); $rs_special_course = Database::query($sql);
$number_of_courses = Database::num_rows($rs_special_course); $number_of_courses = Database::num_rows($rs_special_course);
@ -3566,7 +3568,7 @@ class CourseManager
while ($row = Database::fetch_array($result)) { while ($row = Database::fetch_array($result)) {
// We simply display the title of the category. // We simply display the title of the category.
$courseInCategory = self:: returnCoursesCategories( $courseInCategory = self::returnCoursesCategories(
$row['id'], $row['id'],
$load_dirs $load_dirs
); );
@ -3612,22 +3614,21 @@ class CourseManager
$without_special_courses = ' AND course.code NOT IN ("' . implode('","', $special_course_list) . '")'; $without_special_courses = ' AND course.code NOT IN ("' . implode('","', $special_course_list) . '")';
} }
//AND course_rel_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
$sql = "SELECT $sql = "SELECT
course.id, course.id,
course.title, course.title,
course.code, course.code,
course.subscribe subscr, course.subscribe subscr,
course.unsubscribe unsubscr, course.unsubscribe unsubscr,
course_rel_user.status status, course_rel_user.status status,
course_rel_user.sort sort, course_rel_user.sort sort,
course_rel_user.user_course_cat user_course_cat course_rel_user.user_course_cat user_course_cat
FROM $TABLECOURS course, FROM $TABLECOURS course
$TABLECOURSUSER course_rel_user, INNER JOIN $TABLECOURSUSER course_rel_user
$TABLE_ACCESS_URL_REL_COURSE url ON (course.id = course_rel_user.c_id)
INNER JOIN $TABLE_ACCESS_URL_REL_COURSE url
ON (url.c_id = course.id)
WHERE WHERE
course.id = course_rel_user.c_id AND
url.c_id = course.id AND
course_rel_user.user_id = '" . $user_id . "' AND course_rel_user.user_id = '" . $user_id . "' AND
course_rel_user.user_course_cat = '" . $user_category_id . "' course_rel_user.user_course_cat = '" . $user_category_id . "'
$without_special_courses "; $without_special_courses ";
@ -3999,7 +4000,9 @@ class CourseManager
// Display the "what's new" icons // Display the "what's new" icons
$notifications = ''; $notifications = '';
if ($course_visibility != COURSE_VISIBILITY_CLOSED && $course_visibility != COURSE_VISIBILITY_HIDDEN) { if ($course_visibility != COURSE_VISIBILITY_CLOSED &&
$course_visibility != COURSE_VISIBILITY_HIDDEN
) {
$notifications .= Display:: show_notification($course_info); $notifications .= Display:: show_notification($course_info);
} }

@ -3141,6 +3141,7 @@ class SessionManager
* @param bool $getOnlySessionId * @param bool $getOnlySessionId
* @param bool $getSql * @param bool $getSql
* @param string $orderCondition * @param string $orderCondition
* @param string $keyword
* @param string $description * @param string $description
* *
* @return array sessions * @return array sessions
@ -3383,7 +3384,7 @@ class SessionManager
$sql = "SELECT $sqlSelect $sql = "SELECT $sqlSelect
FROM $tbl_course c FROM $tbl_course c
INNER JOIN $tbl_session_rel_course src INNER JOIN $tbl_session_rel_course src
ON c.id = src.c_id ON (c.id = src.c_id)
WHERE src.session_id = '$session_id' "; WHERE src.session_id = '$session_id' ";
if (!empty($course_name)) { if (!empty($course_name)) {

@ -2617,8 +2617,8 @@ class UserManager
s.coachAccessStartDate AS coach_access_start_date, s.coachAccessStartDate AS coach_access_start_date,
s.coachAccessEndDate AS coach_access_end_date s.coachAccessEndDate AS coach_access_end_date
FROM ChamiloCoreBundle:Session AS s FROM ChamiloCoreBundle:Session AS s
INNER JOIN ChamiloCoreBundle:SessionRelCourseRelUser AS scu WITH scu.session = s
LEFT JOIN ChamiloCoreBundle:SessionCategory AS sc WITH s.category = sc LEFT JOIN ChamiloCoreBundle:SessionCategory AS sc WITH s.category = sc
LEFT JOIN ChamiloCoreBundle:SessionRelCourseRelUser AS scu WITH scu.session = s
WHERE scu.user = :user OR s.generalCoach = :user WHERE scu.user = :user OR s.generalCoach = :user
ORDER BY sc.name, s.name"; ORDER BY sc.name, s.name";
@ -2992,7 +2992,6 @@ class UserManager
/* This query is very similar to the query below, but it will check the /* This query is very similar to the query below, but it will check the
session_rel_course_user table if there are courses registered session_rel_course_user table if there are courses registered
to our user or not */ to our user or not */
$sql = "SELECT DISTINCT $sql = "SELECT DISTINCT
c.visibility, c.visibility,
c.id as real_id, c.id as real_id,
@ -3058,12 +3057,12 @@ class UserManager
} }
if (api_is_drh()) { if (api_is_drh()) {
$session_list = SessionManager::get_sessions_followed_by_drh($user_id); $sessionList = SessionManager::get_sessions_followed_by_drh($user_id);
$session_list = array_keys($session_list); $sessionList = array_keys($sessionList);
if (in_array($session_id, $session_list)) { if (in_array($session_id, $sessionList)) {
$course_list = SessionManager::get_course_list_by_session_id($session_id); $courseList = SessionManager::get_course_list_by_session_id($session_id);
if (!empty($course_list)) { if (!empty($courseList)) {
foreach ($course_list as $course) { foreach ($courseList as $course) {
if (!in_array($course['id'], $courses)) { if (!in_array($course['id'], $courses)) {
$personal_course_list[] = $course; $personal_course_list[] = $course;
} }
@ -3074,9 +3073,9 @@ class UserManager
//check if user is general coach for this session //check if user is general coach for this session
$sessionInfo = api_get_session_info($session_id); $sessionInfo = api_get_session_info($session_id);
if ($sessionInfo['id_coach'] == $user_id) { if ($sessionInfo['id_coach'] == $user_id) {
$course_list = SessionManager::get_course_list_by_session_id($session_id); $courseList = SessionManager::get_course_list_by_session_id($session_id);
if (!empty($course_list)) { if (!empty($courseList)) {
foreach ($course_list as $course) { foreach ($courseList as $course) {
if (!in_array($course['id'], $courses)) { if (!in_array($course['id'], $courses)) {
$personal_course_list[] = $course; $personal_course_list[] = $course;
} }

Loading…
Cancel
Save