Skip to content

Commit

Permalink
Admin: Add setting course_category_code_to_use_as_model BT#18083
Browse files Browse the repository at this point in the history
Allow to group course templates by course category code.
  • Loading branch information
jmontoyaa committed Nov 25, 2020
1 parent 992d35a commit e5ebc9e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 25 deletions.
15 changes: 10 additions & 5 deletions main/create_course/add_course.php
@@ -1,4 +1,5 @@
<?php

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

use Chamilo\CoreBundle\Entity\CourseCategory;
Expand All @@ -9,10 +10,6 @@
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @author Roan Embrechts, refactoring
*
* @package chamilo.create_course
* "Course validation" feature:
*
* @author Jose Manuel Abuin Mosquera <chema@cesga.es>, Centro de Supercomputacion de Galicia
* "Course validation" feature, technical adaptation for Chamilo 1.8.8:
* @author Ivan Tcholakov <ivantcholakov@gmail.com>
Expand Down Expand Up @@ -115,10 +112,18 @@ function setFocus(){
api_get_configuration_value('allow_base_course_category')
);
$categoriesOptions = [null => get_lang('None')];
$categoryToAvoid = '';
if (!api_is_platform_admin()) {
$categoryToAvoid = api_get_configuration_value('course_category_code_to_use_as_model');
}

/** @var CourseCategory $category */
foreach ($categories as $category) {
$categoriesOptions[$category->getCode()] = $category->__toString();
$categoryCode = $category->getCode();
if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) {
continue;
}
$categoriesOptions[$categoryCode] = $category->__toString();
}

$form->addSelect(
Expand Down
42 changes: 32 additions & 10 deletions main/inc/ajax/course.ajax.php
@@ -1,4 +1,5 @@
<?php

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

use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
Expand Down Expand Up @@ -121,11 +122,21 @@
break;
}

$categoryToAvoid = '';
if (!api_is_platform_admin()) {
$categoryToAvoid = api_get_configuration_value('course_category_code_to_use_as_model');
}

$list = [];
foreach ($categories as $item) {
$categoryCode = $item['code'];
if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) {
continue;
}

$list['items'][] = [
'id' => $item['code'],
'text' => '('.$item['code'].') '.strip_tags($item['name']),
'id' => $categoryCode,
'text' => '('.$categoryCode.') '.strip_tags($item['name']),
];
}

Expand All @@ -145,30 +156,41 @@
//TODO change this function to search not only courses STARTING with $_GET['q']
if (api_is_platform_admin()) {
$courseList = CourseManager::get_courses_list(
0, //offset
0, //howMany
1, //$orderby = 1
0,
0,
1,
'ASC',
-1, //visibility
-1,
$_GET['q'],
null, //$urlId
true //AlsoSearchCode
null,
true
);
} elseif (api_is_teacher()) {
$courseList = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id(), $_GET['q']);
$category = api_get_configuration_value('course_category_code_to_use_as_model');
if (!empty($category)) {
$alreadyAdded = [];
if (!empty($courseList)) {
$alreadyAdded = array_column($courseList, 'id');
}
$coursesInCategory = CourseCategory::getCoursesInCategory($category, $_GET['q']);
foreach ($coursesInCategory as $course) {
if (!in_array($course['id'], $alreadyAdded)) {
$courseList[] = $course;
}
}
}
}
}

$results = [];

if (empty($courseList)) {
echo json_encode([]);
break;
}

foreach ($courseList as $course) {
$title = $course['title'];

if (!empty($course['category_code'])) {
$parents = CourseCategory::getParentsToString($course['category_code']);
$title = $parents.$course['title'];
Expand Down
19 changes: 14 additions & 5 deletions main/inc/lib/CoursesAndSessionsCatalog.class.php
Expand Up @@ -181,17 +181,26 @@ public static function getCourseCategoriesTree()
];

$allCategories = CourseCategory::getAllCategories();
$categoryToAvoid = '';
if (api_is_student()) {
$categoryToAvoid = api_get_configuration_value('course_category_code_to_use_as_model');
}
foreach ($allCategories as $category) {
$categoryCode = $category['code'];
if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) {
continue;
}

if (empty($category['parent_id'])) {
$list[$category['code']] = $category;
$list[$category['code']]['level'] = 0;
list($subList, $childrenCount) = self::buildCourseCategoryTree($allCategories, $category['code'], 0);
$list[$categoryCode] = $category;
$list[$categoryCode]['level'] = 0;
list($subList, $childrenCount) = self::buildCourseCategoryTree($allCategories, $categoryCode, 0);
foreach ($subList as $item) {
$list[$item['code']] = $item;
}
// Real course count
$countCourses = CourseCategory::countCoursesInCategory($category['code']);
$list[$category['code']]['number_courses'] = $childrenCount + $countCourses;
$countCourses = CourseCategory::countCoursesInCategory($categoryCode);
$list[$categoryCode]['number_courses'] = $childrenCount + $countCourses;
}
}

Expand Down
37 changes: 32 additions & 5 deletions main/inc/lib/course_category.lib.php
Expand Up @@ -576,9 +576,18 @@ public static function getCategoriesCanBeAddedInCourse($categoryCode)
ORDER BY tree_pos";
$res = Database::query($sql);

$categoryToAvoid = '';
if (!api_is_platform_admin()) {
$categoryToAvoid = api_get_configuration_value('course_category_code_to_use_as_model');
}

$categories[''] = '-';
while ($cat = Database::fetch_array($res)) {
$categories[$cat['code']] = '('.$cat['code'].') '.$cat['name'];
$categoryCode = $cat['code'];
if (!empty($categoryToAvoid) && $categoryToAvoid == $categoryCode) {
continue;
}
$categories[$categoryCode] = '('.$categoryCode.') '.$cat['name'];
ksort($categories);
}

Expand All @@ -593,7 +602,16 @@ public static function getCategoriesCanBeAddedInCourse($categoryCode)
*
* @return int
*/
public static function countCoursesInCategory($category_code = '', $keyword = '', $avoidCourses = true, $conditions = [])
public static function countCoursesInCategory(
$category_code = '',
$keyword = '',
$avoidCourses = true,
$conditions = []
) {
return self::getCoursesInCategory($category_code, $keyword, $avoidCourses, $conditions, true);
}

public static function getCoursesInCategory($category_code = '', $keyword = '', $avoidCourses = true, $conditions = [], $getCount = false)
{
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
$categoryCode = Database::escape_string($category_code);
Expand Down Expand Up @@ -635,7 +653,11 @@ public static function countCoursesInCategory($category_code = '', $keyword = ''

$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(DISTINCT course.id) as count
$select = " DISTINCT course.id, course.code, course.title, course.category_code ";
if ($getCount) {
$select = "count(DISTINCT course.id) as count";
}
$sql = "SELECT $select
FROM $tbl_course as course
INNER JOIN $tbl_url_rel_course as url_rel_course
ON (url_rel_course.c_id = course.id)
Expand All @@ -653,9 +675,14 @@ public static function countCoursesInCategory($category_code = '', $keyword = ''
";

$result = Database::query($sql);
$row = Database::fetch_array($result);

return (int) $row['count'];
if ($getCount) {
$row = Database::fetch_array($result);

return (int) $row['count'];
}

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

/**
Expand Down
3 changes: 3 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -1770,6 +1770,9 @@
// Enable a "Previous question" button in surveys
// $_configuration['survey_backwards_enable'] = false;

// All courses with category MY_CATEGORY will be used as course templates BT#18083
// $_configuration['course_category_code_to_use_as_model'] = 'MY_CATEGORY';

// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email
Expand Down

0 comments on commit e5ebc9e

Please sign in to comment.