Adding search form see BT#7400

1.9.x
Julio Montoya 11 years ago
parent ed9ff62eff
commit 225c5d9509
  1. 86
      main/admin/add_courses_to_usergroup.php
  2. 31
      main/inc/lib/course.lib.php
  3. 44
      main/inc/lib/usergroup.lib.php

@ -42,7 +42,6 @@ $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
$htmlHeadXtra[] = '
<script>
function add_user_to_session (code, content) {
document.getElementById("user_to_add").value = "";
document.getElementById("ajax_list_users_single").innerHTML = "";
@ -93,21 +92,58 @@ if (isset($_POST['form_sent']) && $_POST['form_sent']) {
}
}
// Filters
$filters = array(
array('type' => 'text', 'name' => 'code', 'label' => get_lang('CourseCode')),
array('type' => 'text', 'name' => 'title', 'label' => get_lang('Title')),
/*array('type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')),
array('type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')),
array('type' => 'text', 'name' => 'email', 'label' => get_lang('Email'))*/
);
$searchForm = new FormValidator('search', 'get', api_get_self().'?id='.$id);
$searchForm->add_header(get_lang('AdvancedSearch'));
$renderer =& $searchForm->defaultRenderer();
$searchForm->addElement('hidden', 'id', $id);
foreach ($filters as $param) {
$searchForm->addElement($param['type'], $param['name'], $param['label']);
}
$searchForm->addElement('button', 'submit', get_lang('Search'));
$filterData = array();
if ($searchForm->validate()) {
$filterData = $searchForm->getSubmitValues();
}
$conditions = array();
if (!empty($filters) && !empty($filterData)) {
foreach ($filters as $filter) {
if (isset($filter['name']) && isset($filterData[$filter['name']])) {
$value = $filterData[$filter['name']];
if (!empty($value)) {
$conditions[$filter['name']] = $value;
}
}
}
}
$data = $usergroup->get($id);
$course_list_in = $usergroup->get_courses_by_usergroup($id);
$course_list = CourseManager::get_courses_list(0, 0, 'title', 'asc', -1, null, api_get_current_access_url_id());
$course_list_in = $usergroup->get_courses_by_usergroup($id, true);
$course_list = CourseManager::get_courses_list(0, 0, 'title', 'asc', -1, null, api_get_current_access_url_id(), false, $conditions);
$elements_not_in = $elements_in= array();
$elements_not_in = $elements_in = array();
foreach ($course_list_in as $course) {
$elements_in[$course['id']] = $course['title']." (".$course['visual_code'].")";
}
if (!empty($course_list)) {
foreach ($course_list as $item) {
if (in_array($item['id'], $course_list_in)) {
$elements_in[$item['id']] = $item['title']." (".$item['visual_code'].")";
} else {
$elements_not_in[$item['id']] = $item['title']." (".$item['visual_code'].")";
}
}
}
$ajax_search = $add_type == 'unique' ? true : false;
//checking for extra field with filter on
@ -178,40 +214,18 @@ if ($add_type == 'multiple') {
echo '<div class="actions">';
echo '<a href="usergroups.php">'.Display::return_icon('back.png',get_lang('Back'), array(), ICON_SIZE_MEDIUM).'</a>';
echo Display::url(get_lang('AdvancedSearch'), '#', array('class' => 'advanced_options', 'id' => 'advanced_search'));
echo '</div>';
echo '<div id="advanced_search_options" style="display:none">';
$searchForm->display();
echo '</div>';
?>
<form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?id=<?php echo $id; if(!empty($_GET['add'])) echo '&add=true' ; ?>" style="margin:0px;" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
<?php echo '<legend>'.$data['name'].': '.$tool_name.'</legend>';
$extra_field_list = array();
if ($add_type == 'multiple') {
if (is_array($extra_field_list) && !empty($extra_field_list)) {
if (is_array($new_field_list) && count($new_field_list) > 0) {
echo '<h3>'.get_lang('FilterUsers').'</h3>';
foreach ($new_field_list as $new_field) {
echo $new_field['name'];
$varname = 'field_'.$new_field['variable'];
echo '&nbsp;<select name="'.$varname.'">';
echo '<option value="0">--'.get_lang('Select').'--</option>';
foreach ($new_field['data'] as $option) {
$checked='';
if (isset($_POST[$varname])) {
if ($_POST[$varname]==$option[1]) {
$checked = 'selected="true"';
}
}
echo '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
}
echo '</select>';
echo '&nbsp;&nbsp;';
}
echo '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
echo '<br /><br />';
}
}
}
echo Display::input('hidden', 'id', $id);
echo Display::input('hidden', 'form_sent', '1');
echo Display::input('hidden', 'add_type', null);

@ -150,6 +150,7 @@ class CourseManager
* @param string $startwith If defined, only return results for which the course *title* begins with this string
* @param string $urlId The Access URL ID, if using multiple URLs
* @param string $alsoSearchCode An extension option to indicate that we also want to search for course codes (not *only* titles)
* @param array $conditions
* @return array
*/
public static function get_courses_list(
@ -160,7 +161,8 @@ class CourseManager
$visibility = -1,
$startwith = '',
$urlId = null,
$alsoSearchCode = false
$alsoSearchCode = false,
$conditionsLike = array()
) {
$sql = "SELECT course.* FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." course ";
@ -191,6 +193,33 @@ class CourseManager
$sql .= " AND access_url_id= $urlId";
}
$allowedFields = array(
'title',
'code'
);
if (count($conditionsLike) > 0) {
$sql .= ' AND ';
$temp_conditions = array();
foreach ($conditionsLike as $field => $value) {
if (!in_array($field, $allowedFields)) {
continue;
}
$field = Database::escape_string($field);
$value = Database::escape_string($value);
$simple_like = false;
if ($simple_like) {
$temp_conditions[] = $field." LIKE '$value%'";
} else {
$temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
}
}
$condition = ' AND ';
if (!empty($temp_conditions)) {
$sql .= implode(' '.$condition.' ', $temp_conditions);
}
}
if (!empty($orderby)) {
$sql .= " ORDER BY ".Database::escape_string($orderby)." ";
} else {

@ -147,31 +147,63 @@ class UserGroup extends Model
/**
* Gets a list of course ids by user group
* @param int user group id
* @param array $conditionsLike
* @return array
*/
public function get_courses_by_usergroup($id)
public function get_courses_by_usergroup($id, $loadCourseData = false)
{
if ($this->useMultipleUrl) {
$urlId = api_get_current_access_url_id();
$from = $this->usergroup_rel_course_table." c
INNER JOIN {$this->access_url_rel_usergroup} a ON (a.usergroup_id = c.usergroup_id) ";
$where = array('where' => array('a.usergroup_id = ? AND access_url_id = ? ' => array($id, $urlId)));
INNER JOIN {$this->access_url_rel_usergroup} a
ON (a.usergroup_id = c.usergroup_id) ";
$whereConditionSql = 'a.usergroup_id = ? AND access_url_id = ? ';
$whereConditionValues = array($id, $urlId);
} else {
$whereConditionSql = 'usergroup_id = ?';
$whereConditionValues = array($id);
$from = $this->usergroup_rel_course_table." c ";
}
if ($loadCourseData) {
$from .= " INNER JOIN {$this->table_course} as course ON c.course_id = course.id";
}
/*
if (!empty($conditionsLike)) {
$from .= " INNER JOIN {$this->table_course} as course ON c.course_id = course.id";
$conditionSql = array();
foreach ($conditionsLike as $field => $value) {
$conditionSql[] = $field.' LIKE %?%';
$whereConditionValues[] = $value;
}
$whereConditionSql .= ' AND '.implode(' AND ', $conditionSql);
}*/
$where = array('where' => array($whereConditionSql => $whereConditionValues));
if ($loadCourseData) {
$select = 'course.*';
} else {
$from = $this->usergroup_rel_course_table;
$where = array('where' => array('usergroup_id = ?' => $id));
$select = 'course_id';
}
$results = Database::select(
'course_id',
$select,
$from,
$where
);
$array = array();
if (!empty($results)) {
foreach ($results as $row) {
if ($loadCourseData) {
$array[$row['id']] = $row;
} else {
$array[] = $row['course_id'];
}
}
}
return $array;
}

Loading…
Cancel
Save