<?php
// $Id: subscribe_user2class.php 10811 2007-01-22 08:26:40Z elixir_julian $
/*
==============================================================================
	Dokeos - elearning and course management software

	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2003 Ghent University (UGent)
	Copyright (c) 2001 Universite catholique de Louvain (UCL)
	Copyright (c) Olivier Brouckaert

	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	See the GNU General Public License for more details.

	Contact: Zhong 	poopsoft@163.com
==============================================================================
*/
/**
============================================================================== 
*	@package iilearn.admin
============================================================================== 
*/
// name of the language file that needs to be included
$language_file = 'admin';

$cidReset = true;

require ('../inc/global.inc.php');
require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
api_protect_admin_script();

$course = $_GET['course'];
$class_id = intval($_GET['idclass']);
$form_sent = 0;
$error_message = '';
$first_letter_left = '';
$first_letter_right = '';
$left_user_list = array();
$right_user_list = array ();

// Database table definitions
$tbl_class 		= Database :: get_main_table(TABLE_MAIN_CLASS);
$tbl_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
$tbl_user 		= Database :: get_main_table(TABLE_MAIN_USER);

$sql = "SELECT name FROM $tbl_class WHERE id='$class_id'";
$result = api_sql_query($sql, __FILE__, __LINE__);

if (!list ($class_name) = mysql_fetch_row($result))
{
	header('Location: class_list.php?filtreCours='.urlencode($course));
	exit ();
}

$this_section=SECTION_PLATFORM_ADMIN;
$interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
$interbreadcrumb[] = array ("url" => "class_list.php?filtreCours=".urlencode($course), "name" => get_lang('AdminClasses'));
$tool_name = get_lang('AddUsersToAClass').' ('.$class_name.')';
Display :: display_header($tool_name);

if ($_POST['formSent'])
{
	$form_sent = $_POST['formSent'];
	$first_letter_left = $_POST['firstLetterLeft'];
	$first_letter_right = $_POST['firstLetterRight'];
	$left_user_list = is_array($_POST['LeftUserList']) ? $_POST['LeftUserList'] : array();
	$right_user_list = is_array($_POST['RightUserList']) ? $_POST['RightUserList'] : array();
	$add_to_class = empty ($_POST['addToClass']) ? 0 : 1;
	$remove_from_class = empty ($_POST['removeFromClass']) ? 0 : 1;
	if ($form_sent == 1)
	{
		if ($add_to_class)
		{
			if (count($left_user_list) == 0)
			{
				$error_message = get_lang('AtLeastOneUser');
			}
			else
			{
				foreach ($left_user_list as $user_id)
				{
					ClassManager :: add_user($user_id, $class_id);
				}
				Display :: display_normal_message(get_lang('UsersAreSubscibedToClass'));
			}
		}
		elseif ($remove_from_class)
		{
			if (count($right_user_list) == 0)
				$error_message = get_lang('AtLeastOneUser');
			else
			{
				foreach ($right_user_list as $index => $user_id)
				{
					ClassManager :: unsubscribe_user($user_id, $class_id);
				}
				Display :: display_normal_message(get_lang('UsersAreUnSubscibedToClass'));
			}
		}
	}
}

//$sql = "SELECT u.user_id,lastname,firstname,username FROM $tbl_user u LEFT JOIN $tbl_class_user cu ON u.user_id=cu.user_id AND class_id='$class_id' WHERE lastname LIKE '".$first_letter_left."%' AND class_id IS NULL ORDER BY ". (count($left_user_list) > 0 ? "(user_id IN(".implode(',', $left_user_list).")) DESC," : "")." lastname";
$sql = "SELECT u.user_id,lastname,firstname,username FROM $tbl_user u LEFT JOIN $tbl_class_user cu ON u.user_id=cu.user_id AND class_id='$class_id' WHERE firstname LIKE '".$first_letter_left."%' AND class_id IS NULL ORDER BY firstname";
$result = api_sql_query($sql, __FILE__, __LINE__);
$left_users = api_store_result($result);
//$sql = "SELECT u.user_id,lastname,firstname,username FROM $tbl_user u,$tbl_class_user cu WHERE cu.user_id=u.user_id AND class_id='$class_id' AND lastname LIKE '".$first_letter_right."%' ORDER BY ". (count($right_user_list) > 0 ? "(user_id IN(".implode(',', $right_user_list).")) DESC," : "")." lastname";
$sql = "SELECT u.user_id,lastname,firstname,username FROM $tbl_user u,$tbl_class_user cu WHERE cu.user_id=u.user_id AND class_id='$class_id' AND firstname LIKE '".$first_letter_right."%' ORDER BY firstname";
$result = api_sql_query($sql, __FILE__, __LINE__);
$right_users = api_store_result($result);

if (!empty ($error_message))
{
	Display :: display_error_message($error_message); 
}

?>
<form name="formulaire" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?course=<?php echo urlencode($course); ?>&amp;idclass=<?php echo $class_id; ?>" style="margin:0px;">
 <input type="hidden" name="formSent" value="1"/>
 <table border="0" cellpadding="5" cellspacing="0" align="center">
  <tr>
   <td align="center">
    <b><?php echo get_lang('UsersOutsideClass'); ?> </b>
    <?php echo get_lang('FirstLetterUser'); ?> : 
    <select name="firstLetterLeft" onchange="javascript:document.formulaire.formSent.value='2'; document.formulaire.submit();">
     <option value=""><?php echo get_lang('All'); ?></option>
      <?php
      echo Display :: get_alphabet_options($first_letter_left);
      ?>  
    </select>
   </td>
   <td width="20">&nbsp;</td>
   <td align="center">
    <b><?php echo get_lang('UsersInsideClass'); ?> :</b>
    <?php echo get_lang('FirstLetterUser'); ?> : 
    <select name="firstLetterRight" onchange="javascript:document.formulaire.formSent.value='2'; document.formulaire.submit();">
     <option value=""><?php echo get_lang('All'); ?></option>
<?php
echo Display :: get_alphabet_options($first_letter_right);
?>
   </select>
   </td>
  </tr>
  <tr>
   <td align="center">
    <select name="LeftUserList[]" multiple="multiple" size="20" style="width:300px;">
<?php
foreach ($left_users as $user)
{
?>
     <option value="<?php echo $user['user_id']; ?>" <?php if(in_array($user['user_id'],$left_user_list)) echo 'selected="selected"'; ?>><?php echo $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')'; ?></option>
<?php
}
?>
    </select>
   </td>
   <td valign="middle" align="center">
	<input type="submit" name="addToClass" class="inputSubmitShort" value="&gt;&gt;"/>
	<br/><br/>
	<input type="submit" name="removeFromClass" class="inputSubmitShort" value="&lt;&lt;"/>
   </td>
   <td align="center">
    <select name="RightUserList[]" multiple="multiple" size="20" style="width:300px;">
<?php
foreach ($right_users as $user)
{
?>
     <option value="<?php echo $user['user_id']; ?>" <?php if(in_array($user['user_id'],$right_user_list)) echo 'selected="selected"'; ?>><?php echo $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')'; ?></option>
<?php
}
?>
    </select>
   </td>
  </tr>
 </table>
 <br>
</form>
<?php
/*
==============================================================================
		FOOTER 
==============================================================================
*/
Display :: display_footer();
?>