From 082978f6d9df756f9b44a9bfb17b183244e1284c Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 26 Jan 2011 16:55:29 +0100 Subject: [PATCH] Adding usergroup feature see BT#1910 --- main/admin/add_courses_to_usergroup.php | 386 ++++++++++++++++++++++ main/admin/add_sessions_to_promotion.php | 168 +--------- main/admin/add_sessions_to_usergroup.php | 388 ++++++++++++++++++++++ main/admin/add_users_to_usergroup.php | 391 +++++++++++++++++++++++ main/admin/index.php | 2 +- main/admin/promotions.php | 3 +- main/admin/usergroups.php | 171 ++++++++++ main/inc/ajax/model.ajax.php | 58 ++-- main/inc/lib/course.lib.php | 76 +++-- main/inc/lib/database.lib.php | 6 + main/inc/lib/promotion.lib.php | 2 + main/inc/lib/sessionmanager.lib.php | 23 +- main/inc/lib/usergroup.lib.php | 210 ++++++++++++ 13 files changed, 1662 insertions(+), 222 deletions(-) create mode 100644 main/admin/add_courses_to_usergroup.php create mode 100644 main/admin/add_sessions_to_usergroup.php create mode 100644 main/admin/add_users_to_usergroup.php create mode 100644 main/admin/usergroups.php create mode 100644 main/inc/lib/usergroup.lib.php diff --git a/main/admin/add_courses_to_usergroup.php b/main/admin/add_courses_to_usergroup.php new file mode 100644 index 0000000000..ab3a6f459a --- /dev/null +++ b/main/admin/add_courses_to_usergroup.php @@ -0,0 +1,386 @@ +debugOn(); +$xajax->registerFunction('search'); + +// setting the section (for the tabs) +$this_section = SECTION_PLATFORM_ADMIN; + +// Access restrictions +api_protect_admin_script(true); + +// setting breadcrumbs +$interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin')); +$interbreadcrumb[]=array('url' => 'usergroups.php','name' => get_lang('UserGroups')); + +// Database Table Definitions + +// setting the name of the tool +$tool_name=get_lang('SubscribeSessionsToUserGroup'); + +$add_type = 'multiple'; +if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){ + $add_type = Security::remove_XSS($_REQUEST['add_type']); +} + +$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/'); +$htmlHeadXtra[] = ' +'; + + +$form_sent = 0; +$errorMsg = ''; +$sessions=array(); +$usergroup = new UserGroup(); +$id = intval($_GET['id']); +if($_POST['form_sent']) { + $form_sent = $_POST['form_sent']; + $elements_posted = $_POST['elements_in_name']; + if (!is_array($elements_posted)) { + $elements_posted=array(); + } + if ($form_sent == 1) { + //added a parameter to send emails when registering a user + $usergroup->subscribe_courses_to_usergroup($id, $elements_posted); + header('Location: usergroups.php'); + exit; + } +} +$data = $usergroup->get($id); +$course_list_in = $usergroup->get_courses_by_usergroup($id); +$course_list = CourseManager::get_courses_list(); + +//api_display_tool_title($tool_name.' ('.$session_info['name'].')'); +$elements_not_in = $elements_in= array(); + +if (!empty($course_list)) { + foreach($course_list as $item) { + if (in_array($item['id'], $course_list_in)) { + $elements_in[$item['id']] = $item['title']; + } else { + $elements_not_in[$item['id']] = $item['title']; + } + } +} +$ajax_search = $add_type == 'unique' ? true : false; + +//checking for extra field with filter on + +function search($needle,$type) { + global $tbl_user,$elements_in; + $xajax_response = new XajaxResponse(); + $return = ''; + if (!empty($needle) && !empty($type)) { + + // xajax send utf8 datas... datas in db can be non-utf8 datas + $charset = api_get_system_encoding(); + $needle = Database::escape_string($needle); + $needle = api_convert_encoding($needle, $charset, 'utf-8'); + + if ($type == 'single') { + // search users where username or firstname or lastname begins likes $needle + /* $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user + WHERE (username LIKE "'.$needle.'%" + OR firstname LIKE "'.$needle.'%" + OR lastname LIKE "'.$needle.'%") AND user.user_id<>"'.$user_anonymous.'" AND user.status<>'.DRH.''. + $order_clause. + ' LIMIT 11';*/ + } else { + $list = CourseManager::get_courses_list(0, 0, 1, 'ASC', -1, $needle); + } + $i=0; + if ($type=='single') { + /* + while ($user = Database :: fetch_array($rs)) { + $i++; + if ($i<=10) { + $person_name = api_get_person_name($user['firstname'], $user['lastname']); + $return .= ''.$person_name.' ('.$user['username'].')
'; + } else { + $return .= '...
'; + } + } + $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));*/ + } else { + $return .= ''; + $xajax_response -> addAssign('ajax_list_multiple','innerHTML',api_utf8_encode($return)); + } + } + return $xajax_response; +} +$xajax -> processRequests(); + +Display::display_header($tool_name); + +if ($add_type == 'multiple') { + $link_add_type_unique = ''.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').''; + $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple'); +} else { + $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique'); + $link_add_type_multiple = ''.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').''; +} + +echo '
'; +echo ''.Display::return_icon('back.png',get_lang('Back')).get_lang('Back').''; +echo '
'; + +echo '
'.$tool_name.' '.$data['name'].'

'; ?> + +
> + +0 ) { + echo '

'.get_lang('FilterUsers').'

'; + foreach ($new_field_list as $new_field) { + echo $new_field['name']; + $varname = 'field_'.$new_field['variable']; + echo ' '; + echo '  '; + } + echo ''; + echo '

'; + } + } +} +echo Display::input('hidden','id',$id); +echo Display::input('hidden','form_sent','1'); +echo Display::input('hidden','add_type',null); +if(!empty($errorMsg)) { + Display::display_normal_message($errorMsg); //main API +} +?> + + + + + + + + + + + + + + + + + + + + + + +
: + :
+ : + + 
+
+ + +
+ +
+ 'width:360px', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?> +
+ +
+
+ + + + +

+ + +





+
+'width:360px', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false ); + unset($sessionUsersList); +?> +
+
+ '.get_lang('SubscribeCoursesToGroup').''; + ?> +
+
+ + +getJavascript('../inc/lib/xajax/'); $htmlHeadXtra[] = ' '; -$form_sent=0; -$errorMsg=$firstLetterUser=$firstLetterSession=''; -$UserList=$SessionList=array(); -$users=$sessions=array(); -$noPHP_SELF=true; - +$form_sent = 0; +$errorMsg = ''; +$users =$sessions=array(); $promotion = new Promotion(); $id = intval($_GET['id']); -//echo '
';
 if($_POST['form_sent']) {
-    $form_sent          = $_POST['form_sent'];
-    $firstLetterUser    = $_POST['firstLetterUser'];
-    $firstLetterSession = $_POST['firstLetterSession'];
+    $form_sent          = $_POST['form_sent'];    
     $session_in_promotion_posted       = $_POST['session_in_promotion_name'];     
     if (!is_array($session_in_promotion_posted)) {
         $session_in_promotion_posted=array();
@@ -106,7 +99,7 @@ if($_POST['form_sent']) {
 }
 
 $promotion_data = $promotion->get($id);
-$session_list = SessionManager::get_sessions_list();
+$session_list   = SessionManager::get_sessions_list(array(), array('name'));
 
 //api_display_tool_title($tool_name.' ('.$session_info['name'].')');
 $session_not_in_promotion = $session_in_promotion= array();
@@ -149,12 +142,8 @@ function search_sessions($needle,$type) {
                 $order_clause.
                 ' LIMIT 11';*/
         } else {
-/*            $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user
-                    WHERE '.(api_sort_by_first_name() ? 'firstname' : 'lastname').' LIKE "'.$needle.'%" AND user.status<>'.DRH.' AND user.user_id<>"'.$user_anonymous.'"'.$cond_user_id.
-                    $order_clause;*/
-            $session_list = SessionManager::get_sessions_list(array('s.name LIKE' => "%$needle%"));
-        }
-     
+            $session_list = SessionManager::get_sessions_list(array('s.name LIKE' => "$needle%"));
+        }     
         $i=0;        
         if ($type=='single') {
             /*
@@ -169,9 +158,7 @@ function search_sessions($needle,$type) {
             }
             $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));*/
         } else {
-            global $nosessionUsersList;
-            $return .= '';            
             foreach ($session_list as $row ) {         
                 if (!in_array($row['id'], array_keys($session_in_promotion))) {       
                     $return .= '';
@@ -181,123 +168,12 @@ function search_sessions($needle,$type) {
             $xajax_response -> addAssign('ajax_list_multiple','innerHTML',api_utf8_encode($return));
         }
     }
-
     return $xajax_response;
 }
-
 $xajax -> processRequests();
 
-
 Display::display_header($tool_name);
 
-
-
-
-
-
-if ($ajax_search) {
-    $sql="SELECT user_id, lastname, firstname, username, id_session
-            FROM $tbl_user
-            INNER JOIN $tbl_session_rel_user
-                ON $tbl_session_rel_user.id_user = $tbl_user.user_id AND $tbl_session_rel_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
-                AND $tbl_session_rel_user.id_session = ".intval($id_session)."
-                WHERE status<>".DRH." $order_clause";
-
-    if ($_configuration['multiple_access_urls']) {
-        $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
-        $access_url_id = api_get_current_access_url_id();
-        if ($access_url_id != -1){
-            $sql="SELECT u.user_id, lastname, firstname, username, id_session
-            FROM $tbl_user u
-            INNER JOIN $tbl_session_rel_user
-                ON $tbl_session_rel_user.id_user = u.user_id AND $tbl_session_rel_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
-                AND $tbl_session_rel_user.id_session = ".intval($id_session)."
-                INNER JOIN $tbl_user_rel_access_url url_user ON (url_user.user_id=u.user_id)
-                WHERE access_url_id = $access_url_id AND u.status<>".DRH."
-                $order_clause";
-        }
-    }
-    $result=Database::query($sql);
-    $users=Database::store_result($result);
-    foreach ($users as $user) {
-        $sessionUsersList[$user['user_id']] = $user ;
-    }
-} else {
-          $sql="SELECT  user_id, lastname, firstname, username, id_session
-            FROM $tbl_user u
-            LEFT JOIN $tbl_session_rel_user
-            ON $tbl_session_rel_user.id_user = u.user_id AND $tbl_session_rel_user.id_session = '$id_session' AND $tbl_session_rel_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
-            WHERE u.status<>".DRH."
-        $order_clause";
-       
-        if ($_configuration['multiple_access_urls']) {
-            $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
-            $access_url_id = api_get_current_access_url_id();
-            if ($access_url_id != -1){
-                $sql="SELECT  u.user_id, lastname, firstname, username, id_session
-                FROM $tbl_user u
-                LEFT JOIN $tbl_session_rel_user
-                    ON $tbl_session_rel_user.id_user = u.user_id AND $tbl_session_rel_user.id_session = '$id_session' AND $tbl_session_rel_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
-                INNER JOIN $tbl_user_rel_access_url url_user ON (url_user.user_id=u.user_id)
-                WHERE access_url_id = $access_url_id  $where_filter AND u.status<>".DRH."
-            $order_clause";
-            }
-        }
-
-        $result=Database::query($sql);
-        $users=Database::store_result($result);
-        //var_dump($_REQUEST['id_session']);
-        foreach ($users as $user) {
-            if($user['id_session'] != $id_session)
-                $nosessionUsersList[$user['user_id']] = $user ;
-        }
-        $user_anonymous=api_get_anonymous_id();
-        if (count($nosessionUsersList) > 0) {
-            foreach($nosessionUsersList as $key_user_list =>$value_user_list) {
-                if ($nosessionUsersList[$key_user_list]['user_id']==$user_anonymous) {
-                    unset($nosessionUsersList[$key_user_list]);
-                }
-            }
-        }
-        //filling the correct users in list
-        $sql="SELECT  user_id, lastname, firstname, username, id_session
-            FROM $tbl_user u
-            LEFT JOIN $tbl_session_rel_user
-            ON $tbl_session_rel_user.id_user = u.user_id AND $tbl_session_rel_user.id_session = '$id_session' AND $tbl_session_rel_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
-            WHERE u.status<>".DRH." $order_clause";
-
-        if ($_configuration['multiple_access_urls']) {
-            $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
-            $access_url_id = api_get_current_access_url_id();
-            if ($access_url_id != -1){
-                $sql="SELECT  u.user_id, lastname, firstname, username, id_session
-                FROM $tbl_user u
-                LEFT JOIN $tbl_session_rel_user
-                    ON $tbl_session_rel_user.id_user = u.user_id AND $tbl_session_rel_user.id_session = '$id_session' AND $tbl_session_rel_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
-                INNER JOIN $tbl_user_rel_access_url url_user ON (url_user.user_id=u.user_id)
-                WHERE access_url_id = $access_url_id AND u.status<>".DRH."
-                $order_clause";
-            }
-        }
-    $result=Database::query($sql);
-    $users=Database::store_result($result);
-
-    foreach($users as $key_user_list =>$value_user_list) {
-        if ($users[$key_user_list]['user_id']==$user_anonymous) {
-            unset($users[$key_user_list]);
-            }
-        }
-
-    foreach ($users as $user) {
-        if($user['id_session'] == $id_session){
-            $sessionUsersList[$user['user_id']] = $user;
-            if (array_key_exists($user['user_id'],$nosessionUsersList))
-                unset($nosessionUsersList[$user['user_id']]);
-        }
-
-    }
-}
-
 if ($add_type == 'multiple') {
     $link_add_type_unique = ''.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'';
     $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
@@ -305,15 +181,12 @@ if ($add_type == 'multiple') {
     $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
     $link_add_type_multiple = ''.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'';
 }
-/*
-
-  |  -
-*/ -?> +echo ''; -
'.$tool_name.' '.$promotion_data['name'].'

'; ?> +echo '
'.$tool_name.' '.$promotion_data['name'].'

'; ?>
> @@ -353,7 +226,6 @@ if(!empty($errorMsg)) { ?> - @@ -364,7 +236,6 @@ if(!empty($errorMsg)) { @@ -418,7 +284,6 @@ if(!empty($errorMsg)) {
:
- :
-
'width:360px', 'multiple'=>'multiple','id'=>'session_not_in_promotion','size'=>'15px'),false); ?>
'width:360px', 'multiple'=>'multiple','id'=>'session_in_promotion','size'=>'15px'),false ); unset($sessionUsersList); ?> @@ -524,7 +389,6 @@ function makepost(select){ } --> - debugOn(); +$xajax->registerFunction('search_sessions'); + +// setting the section (for the tabs) +$this_section = SECTION_PLATFORM_ADMIN; + +// Access restrictions +api_protect_admin_script(true); + +// setting breadcrumbs +$interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin')); +$interbreadcrumb[]=array('url' => 'usergroups.php','name' => get_lang('UserGroups')); + +// Database Table Definitions + +// setting the name of the tool +$tool_name=get_lang('SubscribeSessionsToUserGroup'); + +$add_type = 'multiple'; +if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){ + $add_type = Security::remove_XSS($_REQUEST['add_type']); +} + +$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/'); +$htmlHeadXtra[] = ' +'; + + +$form_sent = 0; +$errorMsg = ''; +$sessions=array(); +$usergroup = new UserGroup(); +$id = intval($_GET['id']); +if($_POST['form_sent']) { + $form_sent = $_POST['form_sent']; + $elements_posted = $_POST['elements_in_name']; + if (!is_array($elements_posted)) { + $elements_posted = array(); + } + if ($form_sent == 1) { + //added a parameter to send emails when registering a user + $usergroup->subscribe_sessions_to_usergroup($id, $elements_posted); + header('Location: usergroups.php'); + exit; + } +} +$data = $usergroup->get($id); +$session_list_in = $usergroup->get_sessions_by_usergroup($id); +$session_list = SessionManager::get_sessions_list(array(), array('name')); + +//api_display_tool_title($tool_name.' ('.$session_info['name'].')'); +$elements_not_in = $elements_in= array(); + +if (!empty($session_list)) { + foreach($session_list as $session) { + if (in_array($session['id'], $session_list_in)) { + $elements_in[$session['id']] = $session['name']; + } else { + $elements_not_in[$session['id']] = $session['name']; + } + } +} + +$ajax_search = $add_type == 'unique' ? true : false; + +//checking for extra field with filter on + +function search_sessions($needle,$type) { + global $tbl_user,$elements_in; + $xajax_response = new XajaxResponse(); + $return = ''; + if (!empty($needle) && !empty($type)) { + + // xajax send utf8 datas... datas in db can be non-utf8 datas + $charset = api_get_system_encoding(); + $needle = Database::escape_string($needle); + $needle = api_convert_encoding($needle, $charset, 'utf-8'); + + if ($type == 'single') { + // search users where username or firstname or lastname begins likes $needle + /* $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user + WHERE (username LIKE "'.$needle.'%" + OR firstname LIKE "'.$needle.'%" + OR lastname LIKE "'.$needle.'%") AND user.user_id<>"'.$user_anonymous.'" AND user.status<>'.DRH.''. + $order_clause. + ' LIMIT 11';*/ + } else { + $session_list = SessionManager::get_sessions_list(array('s.name LIKE' => "$needle%")); + } + $i=0; + if ($type=='single') { + /* + while ($user = Database :: fetch_array($rs)) { + $i++; + if ($i<=10) { + $person_name = api_get_person_name($user['firstname'], $user['lastname']); + $return .= ''.$person_name.' ('.$user['username'].')
'; + } else { + $return .= '...
'; + } + } + $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));*/ + } else { + $return .= ''; + $xajax_response -> addAssign('ajax_list_multiple','innerHTML',api_utf8_encode($return)); + } + } + return $xajax_response; +} +$xajax -> processRequests(); + +Display::display_header($tool_name); + +if ($add_type == 'multiple') { + $link_add_type_unique = ''.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').''; + $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple'); +} else { + $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique'); + $link_add_type_multiple = ''.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').''; +} + +echo ''; + +echo '
'.$tool_name.' '.$data['name'].'

'; ?> + +> + +0 ) { + echo '

'.get_lang('FilterUsers').'

'; + foreach ($new_field_list as $new_field) { + echo $new_field['name']; + $varname = 'field_'.$new_field['variable']; + echo ' '; + echo '  '; + } + echo ''; + echo '

'; + } + } +} +echo Display::input('hidden','id',$id); +echo Display::input('hidden','form_sent','1'); +echo Display::input('hidden','add_type',null); +if(!empty($errorMsg)) { + Display::display_normal_message($errorMsg); //main API +} +?> + + + + + + + + + + + + + + + + + + + + + + +
: + :
+ : + + 
+
+ + +
+ +
+ 'width:360px', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?> +
+ +
+
+ + + + +

+ + +





+
+'width:360px', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false ); + unset($sessionUsersList); +?> +
+
+ '.get_lang('SubscribeSessionsToGroup').''; + ?> +
+ + + +debugOn(); +$xajax->registerFunction('search'); + +// setting the section (for the tabs) +$this_section = SECTION_PLATFORM_ADMIN; + +// Access restrictions +api_protect_admin_script(true); + +// setting breadcrumbs +$interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin')); +$interbreadcrumb[]=array('url' => 'usergroups.php','name' => get_lang('UserGroups')); + +// Database Table Definitions + +// setting the name of the tool +$tool_name=get_lang('SubscribeUsersToUserGroup'); + +$add_type = 'multiple'; +if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){ + $add_type = Security::remove_XSS($_REQUEST['add_type']); +} + +$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/'); +$htmlHeadXtra[] = ' +'; + + +$form_sent = 0; +$errorMsg = ''; +$sessions=array(); +$usergroup = new UserGroup(); +$id = intval($_GET['id']); +if($_POST['form_sent']) { + $form_sent = $_POST['form_sent']; + $elements_posted = $_POST['elements_in_name']; + if (!is_array($elements_posted)) { + $elements_posted=array(); + } + if ($form_sent == 1) { + //added a parameter to send emails when registering a user + $usergroup->subscribe_users_to_usergroup($id, $elements_posted); + header('Location: usergroups.php'); + exit; + } +} +$data = $usergroup->get($id); +$list_in = $usergroup->get_users_by_usergroup($id); +$user_list = UserManager::get_user_list(); + +//api_display_tool_title($tool_name.' ('.$session_info['name'].')'); +$elements_not_in = $elements_in = array(); + +if (!empty($user_list)) { + foreach($user_list as $item) { + $person_name = api_get_person_name($item['firstname'], $item['lastname']); + if (in_array($item['user_id'], $list_in)) { + $elements_in[$item['user_id']] = $person_name; + } else { + $elements_not_in[$item['user_id']] = $person_name; + } + } +} + + +$ajax_search = $add_type == 'unique' ? true : false; + +//checking for extra field with filter on + +function search($needle,$type) { + global $tbl_user,$elements_in; + $xajax_response = new XajaxResponse(); + $return = ''; + if (!empty($needle) && !empty($type)) { + + // xajax send utf8 datas... datas in db can be non-utf8 datas + $charset = api_get_system_encoding(); + $needle = Database::escape_string($needle); + $needle = api_convert_encoding($needle, $charset, 'utf-8'); + + if ($type == 'single') { + // search users where username or firstname or lastname begins likes $needle + /* $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user + WHERE (username LIKE "'.$needle.'%" + OR firstname LIKE "'.$needle.'%" + OR lastname LIKE "'.$needle.'%") AND user.user_id<>"'.$user_anonymous.'" AND user.status<>'.DRH.''. + $order_clause. + ' LIMIT 11';*/ + } else { + $list = UserManager::get_user_list_like(array('firstname'=>$needle)); + } + $i=0; + if ($type=='single') { + /* + while ($user = Database :: fetch_array($rs)) { + $i++; + if ($i<=10) { + $person_name = api_get_person_name($user['firstname'], $user['lastname']); + $return .= ''.$person_name.' ('.$user['username'].')
'; + } else { + $return .= '...
'; + } + } + $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));*/ + } else { + $return .= ''; + $xajax_response -> addAssign('ajax_list_multiple','innerHTML',api_utf8_encode($return)); + } + } + return $xajax_response; +} +$xajax -> processRequests(); + +Display::display_header($tool_name); + +if ($add_type == 'multiple') { + $link_add_type_unique = ''.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').''; + $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple'); +} else { + $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique'); + $link_add_type_multiple = ''.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').''; +} + +echo ''; + +echo '
'.$tool_name.' '.$data['name'].'

'; ?> + +
> + +0 ) { + echo '

'.get_lang('FilterUsers').'

'; + foreach ($new_field_list as $new_field) { + echo $new_field['name']; + $varname = 'field_'.$new_field['variable']; + echo ' '; + echo '  '; + } + echo ''; + echo '

'; + } + } +} +echo Display::input('hidden','id',$id); +echo Display::input('hidden','form_sent','1'); +echo Display::input('hidden','add_type',null); +if(!empty($errorMsg)) { + Display::display_normal_message($errorMsg); //main API +} +?> + + + + + + + + + + + + + + + + + + + + + + +
: + :
+ : + + 
+
+ + +
+ +
+ 'width:360px', 'multiple'=>'multiple','id'=>'elements_not_in','size'=>'15px'),false); ?> +
+ +
+
+ + + + +

+ + +





+
+'width:360px', 'multiple'=>'multiple','id'=>'elements_in','size'=>'15px'),false ); + unset($sessionUsersList); +?> +
+
+ '.get_lang('SubscribeUsersToGroup').''; + ?> +
+
+ + +
  • diff --git a/main/admin/promotions.php b/main/admin/promotions.php index a240afcb99..eccb74eade 100644 --- a/main/admin/promotions.php +++ b/main/admin/promotions.php @@ -24,7 +24,6 @@ $htmlHeadXtra[] = api_get_jqgrid_js(); // The header. Display::display_header($tool_name); - // Tool name if (isset($_GET['action']) && $_GET['action'] == 'add') { $tool = 'Add'; @@ -49,7 +48,7 @@ $extra_params['autowidth'] = 'true'; //use the width of the parent $extra_params['height'] = 'auto'; //use the width of the parent //With this function we can add actions to the jgrid $action_links = 'function action_formatter (cellvalue, options, rowObject) { - return \' \'; + return \' \'; }'; ?> diff --git a/main/admin/usergroups.php b/main/admin/usergroups.php new file mode 100644 index 0000000000..4f7b2a6df4 --- /dev/null +++ b/main/admin/usergroups.php @@ -0,0 +1,171 @@ + api_get_self(), 'name' => get_lang('Group')); +} +if (isset($_GET['action']) && $_GET['action'] == 'editnote') { + $tool = 'Modify'; + $interbreadcrumb[] = array ('url' => api_get_self(), 'name' => get_lang('Group')); +} + +//jqgrid will use this URL to do the selects + +$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups'; + +//The order is important you need to check the the $column variable in the model.ajax.php file +$columns = array(get_lang('Name'),get_lang('Description'),get_lang('Actions')); + +//Column config +$column_model = array(array('name'=>'name', 'index'=>'name', 'width'=>'80', 'align'=>'left'), + array('name'=>'description', 'index'=>'description', 'width'=>'500', 'align'=>'left'), + array('name'=>'actions', 'index'=>'actions', 'formatter'=>'action_formatter','width'=>'100', 'align'=>'left'), + ); +//Autowidth +$extra_params['autowidth'] = 'true'; +//height auto +$extra_params['height'] = 'auto'; + +//With this function we can add actions to the jgrid +$action_links = 'function action_formatter (cellvalue, options, rowObject) { + return \'' + .'' + .'' + .'' + .'\'; + }'; +?> + +addElement('header', '', get_lang('Add')); + $form->addElement('text', 'name', get_lang('name'), array('size' => '95', 'id' => 'name')); + //$form->applyFilter('note_title', 'html_filter'); + $form->addElement('html_editor', 'description', get_lang('Description'), null); + $form->addElement('style_submit_button', 'submit', get_lang('Add'), 'class="add"'); + + // Setting the rules + $form->addRule('name', '
    '.get_lang('ThisFieldIsRequired'), 'required'); + + // The validation or display + if ($form->validate()) { + $check = Security::check_token('post'); + if ($check) { + $values = $form->exportValues(); + $res = $usergroup->save($values); + if ($res) { + Display::display_confirmation_message(get_lang('Added')); + } + } + Security::clear_token(); + $usergroup->display(); + } else { + echo ''; + $token = Security::get_token(); + $form->addElement('hidden', 'sec_token'); + $form->setConstants(array('sec_token' => $token)); + $form->display(); + } +}// Action handling: Editing a note +elseif (isset($_GET['action']) && $_GET['action'] == 'edit' && is_numeric($_GET['id'])) { + // Initialize the object + $form = new FormValidator('career', 'post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.Security::remove_XSS($_GET['id'])); + // Settting the form elements + $form->addElement('header', '', get_lang('Modify')); + $form->addElement('hidden', 'id',intval($_GET['id'])); + $form->addElement('text', 'name', get_lang('Name'), array('size' => '100')); + $form->addElement('html_editor', 'description', get_lang('description'), null); + $form->addElement('style_submit_button', 'submit', get_lang('Modify'), 'class="save"'); + + // Setting the defaults + $defaults = $usergroup->get($_GET['id']); + $form->setDefaults($defaults); + + // Setting the rules + $form->addRule('name', '
    '.get_lang('ThisFieldIsRequired'), 'required'); + + // The validation or display + if ($form->validate()) { + $check = Security::check_token('post'); + if ($check) { + $values = $form->exportValues(); + $res = $usergroup->update($values); + if ($res) { + Display::display_confirmation_message(get_lang('Updated')); + } + } + Security::clear_token(); + $usergroup->display(); + } else { + echo ''; + $token = Security::get_token(); + $form->addElement('hidden', 'sec_token'); + $form->setConstants(array('sec_token' => $token)); + $form->display(); + } +} +// Action handling: deleting a note +elseif (isset($_GET['action']) && $_GET['action'] == 'delete' && is_numeric($_GET['id'])) { + $res = $usergroup->delete(Security::remove_XSS($_GET['id'])); + if ($res) { + Display::display_confirmation_message(get_lang('Deleted')); + } + $usergroup->display(); +} else { + $usergroup->display(); +} + +Display :: display_footer(); \ No newline at end of file diff --git a/main/inc/ajax/model.ajax.php b/main/inc/ajax/model.ajax.php index 586ae2e175..554b6ece93 100644 --- a/main/inc/ajax/model.ajax.php +++ b/main/inc/ajax/model.ajax.php @@ -1,44 +1,47 @@ get_count(); - break; + break; case 'get_promotions': require_once $libpath.'promotion.lib.php'; $obj = new Promotion(); $count = $obj->get_count(); - break; + break; + case 'get_usergroups': + require_once $libpath.'usergroup.lib.php'; + $obj = new UserGroup(); + $count = $obj->get_count(); + break; default: exit; } + +//3. Calculating first, end, etc $total_pages = 0; if ($count >0) { @@ -46,42 +49,47 @@ if ($count >0) { $total_pages = ceil($count/$limit); } } - if ($page > $total_pages) { $page = $total_pages; } $start = $limit * $page - $limit; -//2. Querying the DB +//4. Deleting an element if the user wants to +if ($_REQUEST['oper'] == 'del') { + $obj->delete($_REQUEST['id']); +} + +//4. Querying the DB for the elements $columns = array(); switch ($action) { - case 'get_careers': - if ($_REQUEST['oper'] == 'del') { - $obj->delete($_REQUEST['id']); - } + case 'get_careers': $columns = array('name', 'description', 'actions'); if(!in_array($sidx, $columns)) { $sidx = 'name'; } $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit")); break; - case 'get_promotions': - if ($_REQUEST['oper'] == 'del') { - $obj->delete($_REQUEST['id']); - } + case 'get_promotions': $columns = array('name', 'career', 'description', 'actions'); if(!in_array($sidx, $columns)) { $sidx = 'name'; } $result = Database::select('p.id,p.name, p.description, c.name as career', "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c ON c.id = p.career_id ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit")); break; - default: + case 'get_usergroups': + $columns = array('name', 'description', 'actions'); + if(!in_array($sidx, $columns)) { + $sidx = 'name'; + } + $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit")); + break; + default: exit; } //echo '
    ';
     
    -if (in_array($action, array('get_careers','get_promotions'))) {
    -    //3. Creating an obj to return a json
    +//5. Creating an obj to return a json
    +if (in_array($action, array('get_careers','get_promotions','get_usergroups'))) { 
         $responce = new stdClass();           
         $responce->page     = $page; 
         $responce->total    = $total_pages; 
    diff --git a/main/inc/lib/course.lib.php b/main/inc/lib/course.lib.php
    index 64206db7d7..dcafc7bd6a 100755
    --- a/main/inc/lib/course.lib.php
    +++ b/main/inc/lib/course.lib.php
    @@ -158,7 +158,7 @@ class CourseManager {
     
             $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." ";
             if (!empty($startwith)) {
    -            $sql .= "WHERE LIKE title '".Database::escape_string($startwith)."%' ";
    +            $sql .= "WHERE title LIKE '".Database::escape_string($startwith)."%' ";
                 if ($visibility !== -1 && $visibility == strval(intval($visibility))) {
                     $sql .= " AND visibility = $visibility ";
                 }
    @@ -191,7 +191,7 @@ class CourseManager {
             } else {
                 $sql .= ' OFFSET 0';
             }
    -
    +        
             return Database::store_result(Database::query($sql));
         }
     
    @@ -228,10 +228,13 @@ class CourseManager {
     
         /**
          * Unsubscribe one or more users from a course
    -     * @param int|array $user_id
    -     * @param string $course_code
    +     * 
    +     * @param   mixed   user_id or an array with user ids 
    +     * @param   int     session id
    +     * @param   string  course code
    +     * 
          */
    -    public static function unsubscribe_user($user_id, $course_code) {
    +    public static function unsubscribe_user($user_id, $course_code, $session_id = 0) {
     
             if (!is_array($user_id)) {
                 $user_id = array($user_id);
    @@ -240,6 +243,12 @@ class CourseManager {
                 return;
             }
             $table_user = Database :: get_main_table(TABLE_MAIN_USER);
    +                
    +        if (!empty($session_id)) {
    +            $session_id = intval($session_id);
    +        } else {        
    +        	$session_id = intval($_SESSION['id_session']);
    +        }
     
             //Cleaning the $user_id variable
             if (is_array($user_id)) {
    @@ -294,38 +303,34 @@ class CourseManager {
     
     
             // Unsubscribe user from the course.
    -        if (!empty($_SESSION['id_session'])) { // We suppose the session is safe!
    +        if (!empty($session_id)) {
                 // Delete in table session_rel_course_rel_user
    -            $my_session_id = intval ($_SESSION['id_session']);
                 Database::query("DELETE FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
    -                    WHERE id_session ='".$my_session_id."'
    -                        AND course_code = '".Database::escape_string($_SESSION['_course']['id'])."'
    -                        AND id_user IN ($user_ids)");
    +                    WHERE id_session ='".$session_id."' AND course_code = '".Database::escape_string($_SESSION['_course']['id'])."' AND id_user IN ($user_ids)");
     
                 foreach ($user_id as $uid) {
                     // check if a user is register in the session with other course
    -                $sql = "SELECT id_user FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session='$my_session_id' AND id_user='$uid'";
    +                $sql = "SELECT id_user FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session='$session_id' AND id_user='$uid'";
                     $rs = Database::query($sql);
                     if (Database::num_rows($rs) == 0) {
                         // Delete in table session_rel_user
                         Database::query("DELETE FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
    -                                     WHERE id_session ='".$my_session_id."'
    -                                     AND id_user='$uid' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."");
    +                                     WHERE id_session ='".$session_id."' AND id_user='$uid' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."");
                     }
     
                 }
     
                 // Update the table session
                 $row = Database::fetch_array(Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
    -                    WHERE id_session = '".$my_session_id."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."  "));
    +                    WHERE id_session = '".$session_id."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."  "));
                 $count = $row[0]; // number of users by session
                 $result = Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION)." SET nbr_users = '$count'
    -                    WHERE id = '".$my_session_id."'");
    +                    WHERE id = '".$session_id."'");
     
                 // Update the table session_rel_course
    -            $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session = '$my_session_id' AND course_code = '$course_code' AND status<>2" ));
    +            $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session = '$session_id' AND course_code = '$course_code' AND status<>2" ));
                 $count = $row[0]; // number of users by session and course
    -            $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE)." SET nbr_users = '$count' WHERE id_session = '$my_session_id' AND course_code = '$course_code' ");
    +            $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE)." SET nbr_users = '$count' WHERE id_session = '$session_id' AND course_code = '$course_code' ");
     
             } else {
     
    @@ -348,7 +353,7 @@ class CourseManager {
          * @return  bool    True on success, false on failure
          * @see add_user_to_course
          */
    -    public static function subscribe_user($user_id, $course_code, $status = STUDENT) {
    +    public static function subscribe_user($user_id, $course_code, $status = STUDENT, $session_id = 0) {
     
             if ($user_id != strval(intval($user_id))) {
                 return false; //detected possible SQL injection
    @@ -358,6 +363,12 @@ class CourseManager {
             if (empty ($user_id) || empty ($course_code)) {
                 return false;
             }
    +        
    +        if (!empty($session_id)) {
    +            $session_id = intval($session_id);
    +        } else {        
    +            $session_id = intval($_SESSION['id_session']);
    +        }
     
             $status = ($status == STUDENT || $status == COURSEMANAGER) ? $status : STUDENT;
             $role_id = ($status == COURSEMANAGER) ? COURSE_ADMIN : NORMAL_COURSE_MEMBER;
    @@ -369,30 +380,30 @@ class CourseManager {
             }
     
             // Check whether the user has not been already subscribed to the course.
    -        if (empty($_SESSION['id_session'])) {
    +        if (empty($session_id)) {
                 if (Database::num_rows(@Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
                         WHERE user_id = '$user_id' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." AND course_code = '$course_code'")) > 0) {
                     return false; // The user has been already subscribed to the course.
                 }
             }
     
    -        if (!empty($_SESSION['id_session'])) {
    +        if (!empty($session_id)) {
     
                 // Check whether the user has not already been stored in the session_rel_course_user table
                 if (Database::num_rows(@Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
                         WHERE course_code = '".$_SESSION['_course']['id']."'
    -                    AND id_session ='".$_SESSION['id_session']."'
    +                    AND id_session ='".$session_id."'
                         AND id_user = '".$user_id."'")) > 0) {
                     return false;
                 }
     
                 // check if the user is registered in the session with other course
    -            $sql = "SELECT id_user FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session='".$_SESSION['id_session']."' AND id_user='$user_id'";
    +            $sql = "SELECT id_user FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session='".$session_id."' AND id_user='$user_id'";
                 $rs = Database::query($sql);
                 if (Database::num_rows($rs) == 0) {
                     // Check whether the user has not already been stored in the session_rel_user table
                     if (Database::num_rows(@Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
    -                        WHERE id_session ='".$_SESSION['id_session']."'
    +                        WHERE id_session ='".$session_id."'
                             AND id_user = '".$user_id."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH." ")) > 0) {
                         return false;
                     }
    @@ -400,28 +411,26 @@ class CourseManager {
     
                 // Add him/her in the table session_rel_course_rel_user
                 @Database::query("INSERT INTO ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
    -                    SET id_session ='".$_SESSION['id_session']."',
    +                    SET id_session ='".$session_id."',
                         course_code = '".$_SESSION['_course']['id']."',
                         id_user = '".$user_id."'");
     
                 // Add him/her in the table session_rel_user
                 @Database::query("INSERT INTO ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
    -                    SET id_session ='".$_SESSION['id_session']."',
    +                    SET id_session ='".$session_id."',
                         id_user = '".$user_id."'");
     
                 // Update the table session
    -            $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)." WHERE id_session = '".$_SESSION['id_session']."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH.""));
    +            $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)." WHERE id_session = '".$session_id."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH.""));
                 $count = $row[0]; // number of users by session
    -            $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION)." SET nbr_users = '$count' WHERE id = '".$_SESSION['id_session']."'");
    +            $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION)." SET nbr_users = '$count' WHERE id = '".$session_id."'");
     
                 // Update the table session_rel_course
    -            $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session = '".$_SESSION['id_session']."' AND course_code = '$course_code' AND status<>2" ));
    +            $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session = '".$session_id."' AND course_code = '$course_code' AND status<>2" ));
                 $count = $row[0]; // number of users by session
    -            $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE)." SET nbr_users = '$count' WHERE id_session = '".$_SESSION['id_session']."' AND course_code = '$course_code' ");
    -
    +            $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE)." SET nbr_users = '$count' WHERE id_session = '".$session_id."' AND course_code = '$course_code' ");
     
             } else {
    -
                 $course_sort = self::userCourseSort($user_id, $course_code);
                 $result = @Database::query("INSERT INTO ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
                         SET course_code = '$course_code',
    @@ -434,7 +443,6 @@ class CourseManager {
                 $user_id = api_get_user_id();
                 event_system(LOG_SUBSCRIBE_USER_TO_COURSE, LOG_COURSE_CODE, $course_code, $time, $user_id);
             }
    -
             return (bool)$result;
         }
     
    @@ -3060,5 +3068,7 @@ class CourseManager {
             }
         
             return $output;
    -    }    
    +    }
    +
    +    
     } //end class CourseManager
    diff --git a/main/inc/lib/database.lib.php b/main/inc/lib/database.lib.php
    index 425478a661..9e067d34fd 100755
    --- a/main/inc/lib/database.lib.php
    +++ b/main/inc/lib/database.lib.php
    @@ -280,6 +280,12 @@ define('TABLE_THEMATIC_ADVANCE','thematic_advance');
     define('TABLE_CAREER',      'career');
     define('TABLE_PROMOTION',   'promotion');
     
    +define('TABLE_USERGROUP',               'usergroup');
    +define('TABLE_USERGROUP_REL_USER',      'usergroup_rel_user');
    +define('TABLE_USERGROUP_REL_COURSE',    'usergroup_rel_course');
    +define('TABLE_USERGROUP_REL_SESSION',   'usergroup_rel_session');
    +
    +
     
     /*		DATABASE CLASS
             The class and its methods
    diff --git a/main/inc/lib/promotion.lib.php b/main/inc/lib/promotion.lib.php
    index 5d536565a8..80d9129a6b 100644
    --- a/main/inc/lib/promotion.lib.php
    +++ b/main/inc/lib/promotion.lib.php
    @@ -21,6 +21,8 @@ class Promotion extends Model {
         function get_all_promotions_by_career_id($career_id) {        
             return Database::select('*', $this->table, array('where'=>array('career_id = ?'=>$career_id)));
         }    
    +    
    +
        
         /**
          * Displays the title + grid
    diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php
    index 43abf92e4e..254ae6a252 100755
    --- a/main/inc/lib/sessionmanager.lib.php
    +++ b/main/inc/lib/sessionmanager.lib.php
    @@ -955,12 +955,12 @@ class SessionManager {
     
     	/**
          * Get a list of sessions of which the given conditions match with an = 'cond'
    -	 * @param array $conditions a list of condition (exemple : status=>STUDENT)
    -	 * @param array $order_by a list of fields on which sort
    +	 * @param  array $conditions a list of condition (exemple : array('status =' =>STUDENT) or array('s.name LIKE' => "%$needle%")
    +	 * @param  array $order_by a list of fields on which sort
     	 * @return array An array with all sessions of the platform.
    -	 * @todo optional course code parameter, optional sorting parameters...
    +	 * @todo   optional course code parameter, optional sorting parameters...
     	*/
    -	public static function get_sessions_list ($conditions = array(), $order_by = array()) {
    +	public static function get_sessions_list($conditions = array(), $order_by = array()) {
     
     		$session_table =Database::get_main_table(TABLE_MAIN_SESSION);
     		$session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
    @@ -978,12 +978,13 @@ class SessionManager {
     			foreach ($conditions as $field=>$value) {
                     $field = Database::escape_string($field);
                     $value = Database::escape_string($value);
    -				$sql_query .= $field.' = '.$value;
    +				$sql_query .= $field." '".$value."'";
     			}
     		}
     		if (count($order_by)>0) {
     			$sql_query .= ' ORDER BY '.Database::escape_string(implode(',',$order_by));
     		}
    +        //echo $sql_query;
     		$sql_result = Database::query($sql_query);
             if (Database::num_rows($sql_result)>0) {
         		while ($result = Database::fetch_array($sql_result)) {
    @@ -1266,10 +1267,14 @@ class SessionManager {
             Database::update($t, $params, array('promotion_id = ?'=>$promotion_id));
             
             $params['promotion_id'] = $promotion_id;
    -        foreach ($list as $session_id) {
    -            $session_id= intval($session_id);
    -            Database::update($t, $params, array('id = ?'=>$session_id));
    -        }                
    +        if (!empty($list)) {
    +            foreach ($list as $session_id) {
    +                $session_id= intval($session_id);
    +                Database::update($t, $params, array('id = ?'=>$session_id));
    +            }                
    +        }
         }
         
    +    
    +    
     }
    diff --git a/main/inc/lib/usergroup.lib.php b/main/inc/lib/usergroup.lib.php
    new file mode 100644
    index 0000000000..6c78e537f8
    --- /dev/null
    +++ b/main/inc/lib/usergroup.lib.php
    @@ -0,0 +1,210 @@
    +table                        =  Database::get_main_table(TABLE_USERGROUP);
    +        $this->usergroup_rel_user_table     =  Database::get_main_table(TABLE_USERGROUP_REL_USER);
    +        $this->usergroup_rel_course_table   =  Database::get_main_table(TABLE_USERGROUP_REL_COURSE);
    +        $this->usergroup_rel_session_table  =  Database::get_main_table(TABLE_USERGROUP_REL_SESSION);
    +	}
    +    
    +    /**
    +     * Displays the title + grid
    +     */
    +    function display() {
    +        // action links
    +        echo '';   
    +        echo Display::grid_html('usergroups');  
    +    } 
    +    
    +    
    +    public function get_courses_by_usergroup($id) {        
    +        $results = Database::select('*',$this->usergroup_rel_course_table, array('where'=>array('usergroup_id = ?'=>$id)));
    +        $array = array();
    +        if (!empty($results)) {    
    +            foreach($results as $row) {
    +                $array[]= $row['course_id'];            
    +            }
    +        }                       
    +        return $array;
    +    }
    +    
    +    public function get_sessions_by_usergroup($id) {
    +        $results = Database::select('*',$this->usergroup_rel_session_table, array('where'=>array('usergroup_id = ?'=>$id)));
    +        $array = array();
    +        if (!empty($results)) {    
    +            foreach($results as $row) {
    +                $array[]= $row['session_id'];            
    +            }
    +        }                
    +        return $array;
    +    }      
    +    
    +    public function get_users_by_usergroup($id) {
    +        $results = Database::select('*',$this->usergroup_rel_user_table, array('where'=>array('usergroup_id = ?'=>$id)));
    +        $array = array();
    +        if (!empty($results)) {    
    +            foreach($results as $row) {
    +                $array[]= $row['user_id'];            
    +            }
    +        }                       
    +        return $array; 	
    +    }
    +    
    +    
    +    /**
    +     * Subscribes sessions to a group  (also adding the members of the group in the session and course)
    +     * @param   int     usergroup id
    +     * @param   array   list of session ids
    +    */
    +    function subscribe_sessions_to_usergroup($usergroup_id, $list) {
    +        require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
    +        
    +        $t = Database::get_main_table(TABLE_USERGROUP_REL_SESSION);        
    +        //Deleting relationships
    +  
    +        $current_list = self::get_sessions_by_usergroup($usergroup_id);
    +        $user_list    = self::get_users_by_usergroup($usergroup_id);
    +     
    +        $delete_items = $new_items = array();
    +        if (!empty($list)) {                
    +            foreach ($list as $session_id) {
    +                if (!in_array($session_id, $current_list)) {
    +                	$new_items[] = $session_id;
    +                }           	
    +            }
    +        }            
    +        if (!empty($current_list)) {  
    +            foreach($current_list as $session_id) {
    +        	   if (!in_array($session_id, $list)) {
    +                    $delete_items[] = $session_id;
    +                }  
    +            }
    +        }
    +
    +        //Deleting items
    +        if (!empty($delete_items)) {
    +            foreach($delete_items as $session_id) {
    +                foreach($user_list as $user_id) {
    +                    SessionManager::unsubscribe_user_from_session($session_id, $user_id);
    +                    /*foreach ($course_list as $course_data) {
    +                        foreach($user_list as $user_id) {
    +                            CourseManager::subscribe_user($user_id, $course_data['code'], $session_id);
    +                        }
    +                    }*/
    +                }
    +                Database::delete($t, array('usergroup_id = ? AND session_id = ?'=>array($usergroup_id, $session_id)));
    +            }
    +        }
    +     
    +        
    +        //Addding new relationships
    +        if (!empty($new_items)) {
    +            foreach($new_items as $id) {                
    +                $params = array('session_id'=>$id, 'usergroup_id'=>$usergroup_id);
    +                Database::insert($t, $params);                
    +                SessionManager::suscribe_users_to_session($session_id, $user_list);
    +                /*
    +                $course_list = SessionManager::get_course_list_by_session_id($id);
    +                foreach ($course_list as $course_data) {
    +                    foreach($user_list as $user_id) {
    +                        CourseManager::subscribe_user($user_id, $course_data['code'], $id);
    +                    }
    +                }*/
    +            }
    +        }
    +    }
    +    
    +    /**
    +     * Subscribes courses to a group (also adding the members of the group in the course)
    +     * @param   int     usergroup id
    +     * @param   array   list of course ids
    +     */
    +    function subscribe_courses_to_usergroup($usergroup_id, $list) {
    +        require_once api_get_path(LIBRARY_PATH).'course.lib.php';
    +        
    +        $t = Database::get_main_table(TABLE_USERGROUP_REL_COURSE);        
    +        //Deleting relationships
    +  
    +        $current_list = self::get_courses_by_usergroup($usergroup_id);
    +        $user_list    = self::get_users_by_usergroup($usergroup_id);
    +     
    +        $delete_items = $new_items = array();
    +        if (!empty($list)) {                
    +            foreach ($list as $id) {
    +                if (!in_array($id, $current_list)) {
    +                    $new_items[] = $id;
    +                }               
    +            }
    +        }
    +        if (!empty($current_list)) {         
    +            foreach($current_list as $id) {
    +                if (!in_array($id, $list)) {
    +                    $delete_items[] = $id;
    +                }  
    +            }
    +        }
    +        
    +        //Deleting items
    +        if (!empty($delete_items)) {
    +            foreach($delete_items as $course_id) {
    +                $course_info = api_get_course_info_by_id($course_id);     
    +                foreach($user_list as $user_id) {                                   
    +                    CourseManager::unsubscribe_user($user_id, $course_info['code']);                    
    +                }
    +                Database::delete($t, array('usergroup_id = ? AND course_id = ?'=>array($usergroup_id, $course_id)));
    +            }
    +        }
    +        
    +        //Addding new relationships
    +        if (!empty($new_items)) {
    +            foreach($new_items as $course_id) {                
    +                $course_info = api_get_course_info_by_id($course_id);    
    +                
    +                foreach($user_list as $user_id) {         
    +                    CourseManager::subscribe_user($user_id, $course_info['code']);
    +                }
    +                 
    +                $params = array('course_id'=>$id, 'usergroup_id'=>$usergroup_id);
    +                Database::insert($t, $params);
    +            }
    +        }
    +    }   
    +    
    +     /**
    +     * Subscribes users to a group
    +     * @param   int     usergroup id
    +     * @param   array   list of user ids
    +     */
    +    function subscribe_users_to_usergroup($usergroup_id, $list) {
    +        $t = Database::get_main_table(TABLE_USERGROUP_REL_USER);            
    +        $user_list = self::get_users_by_usergroup($usergroup_id);        
    +            
    +        //Deleting relationships
    +        Database::delete($t, array('usergroup_id = ?'=>$usergroup_id));
    +        
    +        //Adding new relationships
    +        if (!empty($list)) {
    +            foreach($list as $id) {
    +                $params = array('user_id'=>$id, 'usergroup_id'=>$usergroup_id);
    +                Database::insert($t, $params);
    +            }
    +        }
    +    }
    +    
    +}
    \ No newline at end of file