Adding select_ajax when loading course categories see BT# 7048

1.9.x
Julio Montoya 11 years ago
parent b315e0d20a
commit 66ead5edad
  1. 25
      main/admin/course_add.php
  2. 20
      main/admin/course_edit.php
  3. 17
      main/inc/ajax/course.ajax.php
  4. 7
      main/inc/lib/course.lib.php
  5. 55
      main/inc/lib/course_category.lib.php

@ -17,13 +17,13 @@ $this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script();
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
$tool_name = get_lang('AddCourse');
$interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
$interbreadcrumb[] = array('url' => 'course_list.php', 'name' => get_lang('CourseList'));
/* MAIN CODE */
global $_configuration;
// Get all possible teachers.
@ -61,17 +61,22 @@ $form->applyFilter('visual_code', 'api_strtoupper');
$form->applyFilter('visual_code', 'html_filter');
$form->addRule('visual_code', get_lang('Max'), 'maxlength', CourseManager::MAX_COURSE_LENGTH_CODE);
//$form->addElement('select', 'tutor_id', get_lang('CourseTitular'), $teachers, array('style' => 'width:350px', 'class'=>'chzn-select', 'id'=>'tutor_id'));
//$form->applyFilter('tutor_id', 'html_filter');
$form->addElement('select', 'course_teachers', get_lang('CourseTeachers'), $teachers, ' id="course_teachers" class="chzn-select" style="width:350px" multiple="multiple" ');
$form->applyFilter('course_teachers', 'html_filter');
$categories_select = $form->addElement('select', 'category_code', get_lang('CourseFaculty'), $categories, array('style' => 'width:350px', 'class'=>'chzn-select', 'id'=>'category_code'));
$categories_select->addOption('-','');
$form->applyFilter('category_code', 'html_filter');
//This function fills the category_code select ...
CourseManager::select_and_sort_categories($categories_select);
// Category code
$url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
$form->addElement(
'select_ajax',
'category_code',
get_lang('CourseFaculty'),
null,
array(
'url' => $url
// 'formatResult' => 'function(item) { return item.name + "'" +item.code; }'
)
);
// Course department
$form->add_textfield('department_name', get_lang('CourseDepartment'), false, array ('size' => '60'));
@ -128,8 +133,6 @@ if (isset($default_course_visibility)) {
}
$values['subscribe'] = 1;
$values['unsubscribe'] = 0;
reset($teachers);
//$values['course_teachers'] = key($teachers);
$form->setDefaults($values);

@ -14,6 +14,7 @@ $this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script();
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
$course_table = Database::get_main_table(TABLE_MAIN_COURSE);
$course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
@ -180,15 +181,16 @@ if (!empty($coursesInSession)) {
}
}
$categories_select = $form->addElement(
'select',
'category_code',
get_lang('CourseFaculty'),
array(),
array('style'=>'width:350px','id'=>'category_code_id', 'class'=>'chzn-select')
);
$categories_select->addOption('-', '');
CourseManager::select_and_sort_categories($categories_select);
// Category code
$url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
$categoryList = array();
if (!empty($course['category_code'])) {
$data = getCategory($course['category_code']);
$categoryList[] = array('id' => $course['category_code'], 'text' => $data['name']);
}
$form->addElement('select_ajax', 'category_code', get_lang('CourseFaculty'), null, array('url' => $url, 'defaults' => $categoryList));
$form->add_textfield('department_name', get_lang('CourseDepartment'), false, array('size' => '60'));
$form->applyFilter('department_name', 'html_filter');

@ -8,7 +8,6 @@ $language_file[] = 'admin';
require_once '../global.inc.php';
$action = $_REQUEST['a'];
$user_id = api_get_user_id();
switch ($action) {
@ -40,6 +39,22 @@ switch ($action) {
}
}
break;
case 'search_category':
require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
if (api_is_platform_admin()) {
$results = searchCategoryByKeyword($_REQUEST['q']);
if (!empty($results)) {
foreach ($results as &$item) {
$item['id'] = $item['code'];
}
echo json_encode($results);
} else {
echo json_encode(array());
}
}
break;
default:
echo '';
}

@ -2138,15 +2138,15 @@ class CourseManager
/**
* create recursively all categories as option of the select passed in parameter.
*
* @param FormValidator $form
* @param HTML_QuickForm_Element $element
* @param string $defaultCode the option value to select by default (used mainly for edition of courses)
* @param string $parentCode the parent category of the categories added (default=null for root category)
* @param string $padding the indent param (you shouldn't indicate something here)
*/
public static function select_and_sort_categories($form, $defaultCode = null, $parentCode = null , $padding = null)
public static function select_and_sort_categories($element, $defaultCode = null, $parentCode = null , $padding = null)
{
require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
setCategoriesInForm($form, $defaultCode, $parentCode , $padding);
setCategoriesInForm($element, $defaultCode, $parentCode , $padding);
}
/**
@ -4211,4 +4211,5 @@ class CourseManager
$result = Database::query($sql);
return Database::num_rows($result) > 0;
}
}

@ -580,12 +580,12 @@ function browseCoursesInCategory($category_code, $random_value = null)
/**
* create recursively all categories as option of the select passed in parameter.
*
* @param FormValidator $form
* @param HTML_QuickForm_Element $element
* @param string $defaultCode the option value to select by default (used mainly for edition of courses)
* @param string $parentCode the parent category of the categories added (default=null for root category)
* @param string $padding the indent param (you shouldn't indicate something here)
*/
function setCategoriesInForm($form, $defaultCode = null, $parentCode = null , $padding = null)
function setCategoriesInForm($element, $defaultCode = null, $parentCode = null, $padding = null)
{
$tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
$conditions = null;
@ -609,9 +609,9 @@ function setCategoriesInForm($form, $defaultCode = null, $parentCode = null , $p
$params .= ($cat['code'] == $defaultCode) ? ' selected' : '';
$option = $padding.' '.$cat['name'].' ('.$cat['code'].')';
$form->addOption($option, $cat['code'], $params);
$element->addOption($option, $cat['code'], $params);
if ($cat['auth_cat_child'] == 'TRUE') {
setCategoriesInForm($form, $defaultCode, $cat['code'], $padding.' - ');
setCategoriesInForm($element, $defaultCode, $cat['code'], $padding.' - ');
}
}
}
@ -635,7 +635,54 @@ function getCourseCategoryNotInList($list)
return Database::store_result($result, 'ASSOC');
}
/**
* @param $keyword
* @return array|null
*/
function searchCategoryByKeyword($keyword)
{
if (empty($keyword)) {
return null;
}
$tableCategory = Database::get_main_table(TABLE_MAIN_CATEGORY);
$conditions = null;
$whereCondition = null;
if (isMultipleUrlSupport()) {
$table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
$conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)";
$whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id();
}
$sql = "SELECT c.*, c.name as text FROM $tableCategory c $conditions
WHERE c.code LIKE '%$keyword%' AND name LIKE '%$keyword%' AND auth_course_child = 'TRUE' $whereCondition ";
$result = Database::query($sql);
return Database::store_result($result, 'ASSOC');
}
function searchCategoryById($list)
{
if (empty($list)) {
return array();
} else {
$list = array_map('intval', $list);
$list = implode("','", $list);
}
$tableCategory = Database::get_main_table(TABLE_MAIN_CATEGORY);
$conditions = null;
$whereCondition = null;
if (isMultipleUrlSupport()) {
$table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
$conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)";
$whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id();
}
$sql = "SELECT c.*, c.name as text FROM $tableCategory c $conditions
WHERE c.id IN $list $whereCondition";
$result = Database::query($sql);
return Database::store_result($result, 'ASSOC');
}
/**
CREATE TABLE IF NOT EXISTS access_url_rel_course_category (access_url_id int unsigned NOT NULL, course_category_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, course_category_id));

Loading…
Cancel
Save