Skip to content

Commit

Permalink
Fix query when using config "allow_base_course_category" BT#16794
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Mar 6, 2020
1 parent 9e310d7 commit dc16e8c
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions main/inc/lib/course_category.lib.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/* For licensing terms, see /license.txt */

/**
Expand Down Expand Up @@ -68,7 +69,6 @@ public static function getCategories($category = '')
$tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
$category = Database::escape_string($category);
$conditions = null;

$table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
$conditions = " INNER JOIN $table a ON (t1.id = a.course_category_id)";
Expand Down Expand Up @@ -109,9 +109,8 @@ public static function getCategories($category = '')
ORDER BY t1.tree_pos";

$result = Database::query($sql);
$categories = Database::store_result($result, 'ASSOC');

return $categories;
return Database::store_result($result, 'ASSOC');
}

/**
Expand All @@ -135,25 +134,25 @@ public static function getAllCategories()
}

$sql = "SELECT
t1.id,
t1.name,
t1.code,
t1.parent_id,
t1.tree_pos,
t1.children_count,
COUNT(DISTINCT t3.code) AS number_courses
FROM $tbl_category t1
t1.id,
t1.name,
t1.code,
t1.parent_id,
t1.tree_pos,
t1.children_count,
COUNT(DISTINCT t3.code) AS number_courses
FROM $tbl_category t1
$conditions
LEFT JOIN $tbl_course t3
LEFT JOIN $tbl_course t3
ON t3.category_code=t1.code
WHERE 1=1
$whereCondition
$whereCondition
GROUP BY
t1.name,
t1.code,
t1.parent_id,
t1.tree_pos,
t1.children_count
t1.name,
t1.code,
t1.parent_id,
t1.tree_pos,
t1.children_count
ORDER BY t1.parent_id, t1.tree_pos";

$result = Database::query($sql);
Expand Down Expand Up @@ -302,9 +301,9 @@ public static function editNode($code, $name, $canHaveCourses, $old_code)

$code = CourseManager::generate_course_code($code);
// Updating category
$sql = "UPDATE $tbl_category SET
name='$name',
code='$code',
$sql = "UPDATE $tbl_category SET
name='$name',
code='$code',
auth_course_child = '$canHaveCourses'
WHERE code = '$old_code'";
Database::query($sql);
Expand Down Expand Up @@ -394,14 +393,14 @@ public static function courseCategoryChildrenCount($categoryId)
if (empty($categoryId)) {
return 0;
}
$sql = "SELECT id, code FROM $table
$sql = "SELECT id, code FROM $table
WHERE parent_id = $categoryId";
$result = Database::query($sql);
while ($row = Database::fetch_array($result)) {
$count += self::courseCategoryChildrenCount($row['id']);
}
$sql = "UPDATE $table SET
children_count = $count
$sql = "UPDATE $table SET
children_count = $count
WHERE id = $categoryId";
Database::query($sql);

Expand All @@ -417,7 +416,7 @@ public static function getChildren($categoryCode)
{
$table = Database::get_main_table(TABLE_MAIN_CATEGORY);
$categoryCode = Database::escape_string($categoryCode);
$sql = "SELECT code, id FROM $table
$sql = "SELECT code, id FROM $table
WHERE parent_id = '$categoryCode'";
$result = Database::query($sql);
$children = [];
Expand All @@ -443,7 +442,7 @@ public static function getParents($categoryCode)

$table = Database::get_main_table(TABLE_MAIN_CATEGORY);
$categoryCode = Database::escape_string($categoryCode);
$sql = "SELECT code, parent_id
$sql = "SELECT code, parent_id
FROM $table
WHERE code = '$categoryCode'";

Expand Down Expand Up @@ -642,15 +641,15 @@ public static function countCoursesInCategory($category_code = '', $searchTerm =
$searchFilter = '';
if (!empty($searchTerm)) {
$searchFilter = ' AND (
code LIKE "%'.$searchTerm.'%" OR
title LIKE "%'.$searchTerm.'%" OR
code LIKE "%'.$searchTerm.'%" OR
title LIKE "%'.$searchTerm.'%" OR
tutor_name LIKE "%'.$searchTerm.'%"
) ';
}

$urlCondition = ' access_url_id = '.api_get_current_access_url_id().' AND';
$tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$sql = "SELECT count(*) as count
$sql = "SELECT count(*) as count
FROM $tbl_course as course
INNER JOIN $tbl_url_rel_course as url_rel_course
ON (url_rel_course.c_id = course.id)
Expand Down Expand Up @@ -713,6 +712,11 @@ public static function searchCategoryByKeyword($keyword)
$conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)";
$whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id();

$allowBaseCategories = api_get_configuration_value('allow_base_course_category');
if ($allowBaseCategories) {
$whereCondition = " AND (a.access_url_id = ".api_get_current_access_url_id()." OR a.access_url_id = 1) ";
}

$keyword = Database::escape_string($keyword);

$sql = "SELECT c.*, c.name as text
Expand Down

0 comments on commit dc16e8c

Please sign in to comment.