<?php //  $Id: subscribe_user.php,v 1.31.2.3 2006/02/01 10:51:33 bmol Exp $
/*
============================================================================== 
	Dokeos - elearning and course management software
	
	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2003 University of Ghent (UGent)
	Copyright (c) 2001 Universite catholique de Louvain (UCL)
	Copyright (c) Elie harfouche elie at harfdesign dot com
	Copyright (c) Roan Embrechts (VUB)
	Copyright (c) Wolfgang Schneider
	Copyright (c) Bart Mollet
	
	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: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
============================================================================== 
*/
/**
============================================================================== 
*	This script allows teachers to subscribe existing users
*	to their course.
*
*	@package dokeos.user
============================================================================== 
*/
/*
============================================================================== 
		INIT SECTION
============================================================================== 
*/
$langFile = 'registration';
include ("../inc/claro_init_global.inc.php");
require_once(api_get_library_path().'/course.lib.php');
/*
-----------------------------------------------------------
	Libraries
-----------------------------------------------------------
*/

/*
============================================================================== 
		DISPLAY FUNCTIONS
============================================================================== 
*/
/*
============================================================================== 
		DATABASE FUNCTIONS
============================================================================== 
*/

/*
============================================================================== 
		MAIN CODE
============================================================================== 
*/
//when not in a course, don't display this page
if (!isset ($_cid))
{
	header("location: ".$rootWeb);
}
//only admins allowed here
if (!($is_courseAdmin || $is_platformAdmin)) api_not_allowed();

$currentCourseID = $_course['sysCode'];
$currentCourseName = $_course['official_code'];
$table_user = Database::get_main_table(MAIN_USER_TABLE);
$table_course_user = Database::get_main_table(MAIN_COURSE_USER_TABLE);
$currentCourse = $currentCourseID;
//oh my god...
$i = 0;
$sqlUserOfCoursea = "SELECT user.user_id FROM ".$table_user;
$sqlUserOfCourseb = "SELECT user.user_id
               FROM ".$table_course_user.", ".$table_user."
                  WHERE course_code='$currentCourseID'
                     AND course_rel_user.user_id = user.user_id";
$result_numba = api_sql_query($sqlUserOfCoursea);
$countUsera = mysql_num_rows($result_numba);
$result_numbb = api_sql_query($sqlUserOfCourseb);
$countUserb = mysql_num_rows($result_numbb);
$countUser = $countUsera - $countUserb;
/*
-----------------------------------------------------------
	Header
-----------------------------------------------------------
*/
$nameTools = get_lang("SubscribeUserToCourse")." (".get_lang("UserNumber").": $countUser)";
//extra entries in breadcrumb
$interbredcrump[] = array ("url" => "user.php", "name" => get_lang("Users"));
Display::display_header($nameTools,"User");

api_display_tool_title($nameTools);

/*
============================================================================== 
		ADMIN SQL FUNCTIONS
============================================================================== 
*/
if (isset ($_GET['register']))
{
	//$result = api_sql_query("INSERT INTO ".$table_course_user." (course_code, user_id, status) VALUES ('$currentCourse','".addslashes($_GET['user_id'])."', '5')");
	CourseManager::subscribe_user($_GET['user_id'],$currentCourse);
}
	if( isset($_POST['action']))
	{
		switch($_POST['action'])
		{
			case 'subscribe':
				if( is_array($_POST['user']))
				{
					foreach($_POST['user'] as $index => $user_id)
					{
						//$sql = "INSERT INTO ".Database :: get_main_table(MAIN_COURSE_USER_TABLE)." SET user_id = $user_id, course_code = '".$_course['sysCode']."'";
						//api_sql_query($sql,__FILE__,__LINE__);
						CourseManager::subscribe_user($user_id,$_course['sysCode']);
					}
				}
			break;	
		}	
	}

/*
============================================================================== 
		SHOW LIST OF USERS
============================================================================== 
*/
$properties["width"] = "100%";
//table column titles
$column_header[] = array ('', false, '');
$column_header[] = array (get_lang("Lastname"), true, '');
$column_header[] = array (get_lang("Firstname"), true, '');
$column_header[] = array (get_lang("Email"), true, '');
$column_header[] = array (get_lang("reg"), false, '');

/*
============================================================================== 
		SELECT USER INFO
============================================================================== 
*/
$sql_query = "SELECT u.user_id, u.lastname, u.firstname, u.email
				FROM $table_user u
				LEFT JOIN $table_course_user cu on u.user_id = cu.user_id and course_code='$currentCourseID'
				WHERE cu.user_id IS NULL
				";
$sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
$user_data = array ();
while ($myrow = mysql_fetch_array($sql_result))
{
	$row = array ();
	$row[] = '<input type="checkbox" name="user[]" value="'.$myrow['user_id'].'" '.(isset($_GET['selectall']) ? ' checked="checked" ' : '').'/>';
	$row[] = $myrow["lastname"];
	$row[] = $myrow["firstname"];
	$row[] = Display::encrypted_mailto_link($myrow["email"]);
	//Link to register users
	if ($myrow["user_id"] != $HTTP_SESSION_VARS["uid"])
	{
		$row[] = "<a href=\"".$_SERVER['PHP_SELF']."?register=yes&user_id=".$myrow[user_id]."\">".get_lang("reg")."</a>";
	}
	else
	{
		$row[] = '';
	}
	$user_data[] = $row;
}
echo '<form method="post" action="subscribe_user.php">';
Display :: display_sortable_table($column_header, $user_data);
$link = '';
$link .= isset ($_GET['page_nr']) ? 'page_nr='.$_GET['page_nr'].'&amp;' : '';
$link .= isset ($_GET['per_page']) ? 'per_page='.$_GET['per_page'].'&amp;' : '';
$link .= isset ($_GET['column']) ? 'column='.$_GET['column'].'&amp;' : '';
$link .= isset ($_GET['direction']) ? 'direction='.$_GET['direction'].'&amp;' : '';;
echo '<a href="subscribe_user.php?'.$link.'selectall">'.get_lang('SelectAll').'</a> - ';
echo '<a href="subscribe_user.php?'.$link.'">'.get_lang('UnSelectAll').'</a>  ';
echo get_lang('WithSelected').' : ';
echo '<select name="action">';
echo '<option value="subscribe">'.get_lang('reg').'</option>';
echo '</select>';
echo '<input type="submit" value="'.get_lang('Ok').'"/>';
echo '</form>';

/*
============================================================================== 
		FOOTER 
============================================================================== 
*/
Display::display_footer();
?>
