<?php

/************************************************************************/
/* DOCEBO CORE - Framework												*/
/* ============================================							*/
/*																		*/
/* Copyright (c) 2006													*/
/* http://www.docebo.com												*/
/*																		*/
/* 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.		*/
/************************************************************************/

/**
 * @package DoceboLms
 * @subpackage dimdim
 * @category driver with external
 * @author Fabio Pirovano
 * @version $Id:$
 * @since 3.5.0
 * 
 * ( editor = Eclipse 3.2.0[phpeclipse,subclipse,WTP], tabwidth = 4 ) 
 */

if(!defined('IN_DOCEBO')) die('You cannot access this file directly');

require_once($GLOBALS['where_scs'].'/lib/lib.dimdim.php');

function dimdim(&$url) {
	checkPerm('view');
	//$mod_perm = checkPerm('mod');
	
	$lang =& DoceboLanguage::createInstance('dimdim', 'lms');
	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_DIMDIM'), 'dimdim')
		.'<div class="std_block">', 'content');
	
	if(checkPerm('mod', true)) {
		$GLOBALS['page']->add('<p><a href="'.$url->getUrl('op=dimdimstartnewconf').'">'.$lang->def('_CREATE_CONFERENCE').'</a></p>'
		, 'content');
	}
	
	$dimdim = new DimDim_Manager();
	$dimdim->deleteOldRoom($_SESSION['idCourse'],fromDatetimeToTimestamp(date("Y-m-d H:i:s")));
	$re_room 		= $dimdim->roomActive($_SESSION['idCourse'], fromDatetimeToTimestamp(date("Y-m-d H:i:s")));
	$room_number 	= $dimdim->totalRoom($re_room);
	
	if($room_number == 0) {
		// no rooms
		$GLOBALS['page']->add('<strong id="noroom">'.$lang->def('_NO_ROOM_AVAILABLE').'</strong>', 'content');
	} else {
		
		// list rooms active in this moment
		$tb = new TypeOne(0, $lang->def('_ROOMS_AVAILABLE'), $lang->def('_SUMMARY_ROOM_AVAILABLE'));
		$tb->setTableStyle('soft-table active');

		$cont_h = array($lang->def('_ROOM_TITLE'), 
						$lang->def('_START_DATE'),
						$lang->def('_MEETING_HOURS'), 
						$lang->def('_ROOM_LOGIN'),
						""
						
		);

		$type_h = array('table_main_colum', 'align_center nowrap', 'align_center nowrap', 'align_center');

		$tb->setColsStyle($type_h);
		$tb->addHead($cont_h);
		
		$acl_manager =& $GLOBALS['current_user']->getAclManager();
		$display_name = $GLOBALS['current_user']->getUserName();
		$u_info = $acl_manager->getUser(getLogUserId(), false);
		$user_email=$u_info[ACL_INFO_EMAIL];
		
		while($room = $dimdim->nextRow($re_room)) {

			$room_id = $room["id"];
	
			$cont = array();
			$cont[]=$room["name"];	
			$start_date=$GLOBALS['regset']->databaseToRegional(date("Y-m-d H:i:s",$room["starttime"]), 'datetime');
			$cont[]=$start_date;
			$cont[]=$room["meetinghours"];		
			
			$returnurl="http://".$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]."?modname=dimdim&amp;op=dimdim";
			
			if ($room["audiovideosettings"]=="audio") {
				$av="audio";
			} else {
				$av="av";
			}
			
			
			$now=time();
			
			if ($room["endtime"]>=$now and $room["starttime"]<=$now) {
				if (getLogUserId()==$room["idSt"]) {
					$url = "http://".$dimdim->server.":".$dimdim->port."/dimdim/html/signin/signin.action?action=host&"."email=".$room["emailuser"]."&confKey=".$room["confkey"]."&displayName=".$room["displayname"]."&confName=".$confname."&lobby=false&networkProfile=1&meetingHours=".$room["meetinghours"]."&meetingMinutes=0&maxParticipants=".$room["maxparticipants"]."&presenterAV=".$av."&attendees=&maxAttendeeMikes=".$room["maxmikes"]."&returnUrl=".$returnurl."&submitFormOnLoad=true";
					$label=$lang->def('_START_CONFERENCE');
				} else {
					$url = "http://".$dimdim->server.":".$dimdim->port."/dimdim/html/signin/signin.action?action=join&"."email=".$user_email."&confKey=".$room["confkey"]."&displayName=".$display_name."&submitFormOnLoad=true";
					$label=$lang->def('_JOIN_CONFERENCE');
				}
				$cont[]="<a href=\"$url\">".$label."</a>";
			
			} else {
				$cont[]="";
			}
			
			if (getLogUserId()==$room["idSt"]) {
				$cont[]="<a href=\"index.php?modname=dimdim&amp;op=dimdimdelconf&id=".$room["id"]."\">".$lang->def('_DELETE_ROOM')."</a>";
			} else {
				$cont[]="";
			}
			
			$tb->addBody($cont);
			
			
		} // end while
		
		$GLOBALS['page']->add($tb->getTable(), 'content');
	}
	
	$GLOBALS['page']->add('</div>'
		, 'content');
}

function dimdim_startnewconf($url) {
	checkPerm('view');
	$mod_perm = checkPerm('mod');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	
	$lang =& DoceboLanguage::createInstance('dimdim', 'lms');
	
	if(isset($_POST['create_conf'])) {
		
		$acl_manager =& $GLOBALS['current_user']->getAclManager();
		
		$dimdim = new DimDim_Manager();
		
		$display_name = $GLOBALS['current_user']->getUserName();
		$u_info = $acl_manager->getUser(getLogUserId(), false);
		$user_email=$u_info[ACL_INFO_EMAIL];
			
		$conf_key = $dimdim->generateConfKey();
		
		$start_date = $GLOBALS['regset']->regionalToDatabase($_POST['start_date'], 'date');
		$start_date = substr($start_date, 0, 10);

		$start_time = ( strlen($_POST['start_time']['hour']) == 1 ? '0' : '' ).$_POST['start_time']['hour'].':'
			.( strlen($_POST['start_time']['minute']) == 1 ? '0' : '' ).$_POST['start_time']['minute'].':00';
		
		$start_timestamp = fromDatetimeToTimestamp($start_date.' '.$start_time);
			
		$conference_name=(trim($_POST["conference_name"]))?(trim($_POST["conference_name"])):($lang->def('_ROOM'));
		
		$meetinghours=(int)$_POST["meetinghours"];	
		
		$end_timestamp = $start_timestamp + $meetinghours * 3600;
		
		
		$maxparticipants=(int)$_POST["maxparticipants"];	
		
		$audiovideosettings=(int)$_POST["audiovideosettings"];	
		$maxmikes=(int)$_POST["maxmikes"];	
		
		$idCourse=$_SESSION['idCourse'];
		$dimdim->insert_room($idCourse,$conference_name,getLogUserId(),$user_email,$display_name,$start_timestamp,$end_timestamp,$conf_key,$meetinghours,$maxparticipants,$audiovideosettings,$maxmikes);
		jumpTo('index.php?modname=dimdim&amp;op=dimdim');
	}
		
	$avsettings=array("audio","audio-video");
	$start_time['hour'] 	= date('H');
	$start_time['minute'] 	= date('i');
	$start_date = importVar('start_date', false, date("Y-m-d H:i:s"));
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_DIMDIM'), 'dimdim')
		.'<div class="std_block">'
	, 'content');
	
	$GLOBALS['page']->add(
		Form::openForm('create_dimdim_conference', $url->getUrl('op=dimdimstartnewconf'))
		.Form::openElementSpace()
		.Form::getTextfield(	$lang->def('_CONFERENCE_NAME'), 
								'conference_name', 
								'conference_name', 
								255, 
								importVar('conference_name') )
		
							
		.Form::getDatefield($lang->def('_START_DATE'), 	'start_date', 'start_date',
			$GLOBALS['regset']->databaseToRegional($start_date, 'date') )
			
		.Form::getLineBox(
			$lang->def('_AT_HOUR'),
			Form::getInputDropdown('', 'start_time_hour', 'start_time[hour]', range(0, 23)
				, importVar('start_time_hour', false, date("H"))
				, '' )
			.' : '
			.Form::getInputDropdown('', 'start_time_minute', 'start_time[minute]', range(0, 59)
				, importVar('start_time_hour', false, date("i"))
				, '' )
		)
		
		.Form::getLineBox(
			$lang->def('_MEETING_HOURS'),
			Form::getInputDropdown('', 'meetinghours', 'meetinghours', range(0, 5)
				, importVar('meetinghours', false, 2)
				, '' )
								
		)	
		
		.Form::getLineBox(
			$lang->def('_MAX_PARTICIPANTS'),
			Form::getInputDropdown('', 'maxparticipants', 'maxparticipants', range(0, 30)
				, importVar('max_participants', false, 20)
				, '' )
								
		)	
		
		.Form::getLineBox(
			$lang->def('_AUDIO_VIDEO'),
			Form::getInputDropdown('', 'audiovideosettings', 'audiovideosettings', $avsettings
				, importVar('audiovideosettings', false, 0)
				, '' )
								
		)	
		
		.Form::getLineBox(
			$lang->def('_MAX_MIKES'),
			Form::getInputDropdown('', 'maxmikes', 'maxmikes', range(0,5)
				, importVar('maxmikes', false, 2)
				, '' )
								
		)	
		
		
		.Form::closeElementSpace()
		
		.Form::openButtonSpace()
		.Form::getButton('create_conf', 'create_conf', $lang->def('_CREATE_CONF'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		
		.'</div>'
	, 'content');
}

function dimdim_delconf() {
	checkPerm('mod');
	
	$id=importVar('id');
	$dimdim = new DimDim_Manager();
	
	$room=$dimdim->roomInfo($id);
	
	$lang =& DoceboLanguage::createInstance('dimdim', 'lms');
	if( isset($_GET['confirm']) ) {
		
		$dimdim->deleteRoom($id);
		jumpTo('index.php?modname=dimdim&amp;op=dimdim');
	} else {
		$title_page = array(
			'index.php?modname=dimdim&amp;op=dimdim' => $lang->def('_DIMDIM'), 
			$lang->def('_DELETE_ROOM')
		);
		$GLOBALS['page']->add( 
			getTitleArea($title_page, 'dimdim', $lang->def('_DIMDIM'))
			.'<div class="std_block">'
			.getDeleteUi(	$lang->def('_AREYOUSURE'),
							'<span>'.$lang->def('_CONFERENCE_NAME').' : </span>'.$room["name"],
							true, 
							'index.php?modname=dimdim&amp;op=dimdimdelconf&amp;id='.$id.'&amp;confirm=1',
							'index.php?modname=dimdim&amp;op=dimdim' )
			.'</div>', 'content');
	
	}
}




// =================================================================== //
// dimdim dispatch
// =================================================================== //

function dispatchDimdim($op) {
	
	require_once($GLOBALS['where_framework'].'/lib/lib.urlmanager.php');
	$url =& UrlManager::getInstance();
	$url->setStdQuery('modname=dimdim&op=dimdim');
	
	if(isset($_POST['undo'])) $op = 'dimdim';
	
	switch($op) {
		case "dimdim" : {
			dimdim($url);
		};break;
		case "dimdimstartnewconf" : {
			dimdim_startnewconf($url);
		};break;
		case "dimdimdelconf" : {
			dimdim_delconf();
		};break;
	}
}

?>