<?php

/*************************************************************************/
/* DOCEBO LCMS - Learning Content Managment System                       */
/* ======================================================================*/
/* Docebo is the new name of SpaghettiLearning Project                   */
/*                                                                       */
/* Copyright (c) 2002 by Emanuele Sandri (esandri@tiscali.it)            */
/*                                                                       */
/* 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.        */
/*************************************************************************/

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

function organization_property(&$treeView, $idItem) {
	require_once( $GLOBALS['where_framework'].'/lib/lib.tab.php' );
	
	$tv = new TabView( 'organization_properties', '#' );
	$lang =& DoceboLanguage::createInstance('organization', 'lms');
	
	$tv->addTab( new TabElemDefault( 'prereqisites', $lang->def('_ORGPREREQUISITES'), getPathImage().'organizations/prerequisites.gif' ) );
	$tv->addTab( new TabElemDefault( 'settings', $lang->def('_ORGPROPERTIESTITLE'), getPathImage().'organizations/settings1.gif' ) );
	$tv->addTab( new TabElemDefault( 'catalogation', $lang->def('_CATINFO'), getPathImage().'standard/mod.gif' ) );
	
	$tv->parseInput( $_POST, $_POST );
	if( $tv->getActiveTab() == '' )
		$tv->setActiveTab('prereqisites');
	
	require_once( $GLOBALS['where_framework'].'/lib/lib.form.php' );
	$form = new Form();
	
	$GLOBALS['page']->add($form->openForm('properties' , 'index.php?modname=storage&amp;op=display'));
	
	$GLOBALS['page']->add($form->getHidden(	$tv->_getStateId(),
											$tv->_getStateId(),
											$tv->getActiveTab()));
	
	/* The form contains the tab buttons and all others input
	 * In the method extendedParsing of the Org_TreeView object 
	 * it's controlled the hidden input named 'stay_on_properties'.
	 * When this input was found the treeView set op to properties so
	 * system go back in this function. But after the control of
	 * 'stay_on_properties' it's controlled the org_properties_ok and
	 * org_properties_cancel; if one of this was found the op of the 
	 * treeView object is set to 'display' and visualization go back 
	 * to area
	 */
	$GLOBALS['page']->add( $form->getHidden('stay_on_properties', 'stay_on_properties', 'stay_on_properties') );
	
	/* when 'stay_on_properties' was found the contextId it's set to 
	 * 'idItem' input
	 */
	$GLOBALS['page']->add( $form->getHidden('idItem', 'idItem', $idItem) );
	
	$GLOBALS['page']->add( $tv->printTabView_Begin("",FALSE) );

	switch( $tv->getActiveTab() ) {
		case 'prereqisites':
			organization_property_prereqisites($treeView, $idItem, $form, $lang);
		break;
		case 'settings':
			$GLOBALS['page']->add($form->openElementSpace('padding05') );
			organization_property_settings($treeView, $idItem, $form, $lang);
			$GLOBALS['page']->add($form->closeElementSpace() );
		break;
		case 'catalogation':
			$GLOBALS['page']->add($form->openElementSpace('padding05') );
			organization_property_catalogation($treeView, $idItem, $form, $lang);
			$GLOBALS['page']->add($form->closeElementSpace() );
		break;
	}
	
	$GLOBALS['page']->add( $tv->printTabView_End() );
	$GLOBALS['page']->add($form->openButtonSpace());
	$GLOBALS['page']->add( Form::getButton("org_properties_ok","org_properties_ok",$lang->def( '_CONFIRM' )), 'content' );
	$GLOBALS['page']->add( Form::getButton("org_properties_cancel","org_properties_cancel",$lang->def( '_CANCEL' )), 'content' );
	$GLOBALS['page']->add($form->closeButtonSpace());
	$GLOBALS['page']->add($form->closeForm());
	
}

function organization_property_prereqisites(&$treeView, $idItem, &$form, &$lang) {
	$folder = $treeView->tdb->getFolderById( $idItem );
	$values = organization_property_common($treeView, $idItem, $form, $lang, $folder);
	
	// print tree with check
	$GLOBALS['page']->add( $form->getOpenFieldset($lang->def('_ORGPREREQUISITES')));
	$treeView->selector_mode = TRUE;
	$treeView->simple_selector = TRUE;
	$treeView->itemDisabled = array($idItem);
	$GLOBALS['page']->add( $treeView->load() );
	$GLOBALS['page']->add( $form->getCloseFieldset() );

	$GLOBALS['page']->add( $form->getHidden('selfPrerequisites', 'selfPrerequisites', $values['selfPrerequisites']) );
	$GLOBALS['page']->add( $form->getHidden('isTerminator', 'isTerminator', $values['isTerminator']) );
	$GLOBALS['page']->add( $form->getHidden('visibility', 'visibility', $values['visibility']) );
	$GLOBALS['page']->add( $form->getHidden('milestone', 'milestone', $values['milestone']) );
	$GLOBALS['page']->add( $form->getHidden('version', 'version', $values['version']) );
	$GLOBALS['page']->add( $form->getHidden('difficult', 'difficult', $values['difficult']) );
	$GLOBALS['page']->add( $form->getHidden('description', 'description', $values['description']) );
	$GLOBALS['page']->add( $form->getHidden('language', 'language', $values['language']) );
	$GLOBALS['page']->add( $form->getHidden('resource', 'resource', $values['resource']) );
	$GLOBALS['page']->add( $form->getHidden('objective', 'objective', $values['objective']) );
	
	$GLOBALS['page']->add( $form->getHidden('obj_width', 'obj_width', $values['obj_width']) );
	$GLOBALS['page']->add( $form->getHidden('obj_height', 'obj_height', $values['obj_height']) );
	$GLOBALS['page']->add( $form->getHidden('publish_from', 'publish_from', $values['publish_from']) );
	$GLOBALS['page']->add( $form->getHidden('publish_to', 'publish_to', $values['publish_to']) );

}

function organization_property_settings(&$treeView, $idItem, &$form, &$lang) {
	$folder = $treeView->tdb->getFolderById( $idItem );
	
	$values = organization_property_common($treeView, $idItem, $form, $lang, $folder);
	
	$GLOBALS['page']->add( $treeView->printState() );
	$GLOBALS['page']->add( $form->getHidden('version', 'version', $values['version']) );
	$GLOBALS['page']->add( $form->getHidden('difficult', 'difficult', $values['difficult']) );
	$GLOBALS['page']->add( $form->getHidden('description', 'description', $values['description']) );
	$GLOBALS['page']->add( $form->getHidden('language', 'language', $values['language']) );
	$GLOBALS['page']->add( $form->getHidden('resource', 'resource', $values['resource']) );
	$GLOBALS['page']->add( $form->getHidden('objective', 'objective', $values['objective']) );

	$GLOBALS['page']->add( 
		$form->getRadioSet( $lang->def( '_ORGVISIBILITY' ), 
							'visibility', 
							'visibility', 
							array( 	$lang->def( '_NO' ) => "1",
									$lang->def( '_YES' ) => "0"),
							$values['visibility']) );
	
	
	$isFolder = ($values['objectType'] === '');
	if( !$isFolder ) {
		
		
		$GLOBALS['page']->add( $form->getDatefield( $lang->def( '_PUBLISH_FROM' ), 
											"publish_from", 
											"publish_from", 
											isset($values['publish_from'])?$values['publish_from']:"") 
						);
	
		$GLOBALS['page']->add( $form->getDatefield( $lang->def( '_PUBLISH_TO' ), 
											"publish_to", 
											"publish_to", 
											isset($values['publish_to'])?$values['publish_to']:"") 
						);
		
		
		$orgselfprerequisites = array( $lang->def( '_ORGPRETOCOMPLETE' ) => "incomplete",
											$lang->def( '_ORGPREINFINITE' ) => "*",
											$lang->def( '_ORGPREONLYONE' ) => "NULL" );
		
		$GLOBALS['page']->add($form->getOpenCombo( $lang->def( '_ORGSELFPREREQUISITES' ) ) );
		foreach($orgselfprerequisites as $name => $id ) {
			
			$GLOBALS['page']->add(
				$form->getRadio( $name, 
									'selfPrerequisites_'.$id, 
									'selfPrerequisites', 
									$id,
									($values['selfPrerequisites'] == $id)) 
			);
		}
		$GLOBALS['page']->add($form->getCloseCombo() );
		/*$GLOBALS['page']->add( 
				$form->getRadioSet( $lang->def( '_ORGSELFPREREQUISITES' ), 
									'selfPrerequisites', 
									'selfPrerequisites', 
									array( $lang->def( '_ORGPRETOCOMPLETE' ) => "incomplete",
											$lang->def( '_ORGPREINFINITE' ) => "*",
											$lang->def( '_ORGPREONLYONE' ) => "NULL"	 ),
									$values['selfPrerequisites']) );
									*/
		$GLOBALS['page']->add( $form->getBreakRow() );
		
		// ------------------- terminator
		$GLOBALS['page']->add( 
				$form->getRadioSet( $lang->def( '_ORGISTERMINATOR' ), 
									'isTerminator', 
									'isTerminator', 
									array( 	$lang->def( '_NO' ) => "0",
											$lang->def( '_YES' ) => "1"),
									$values['isTerminator']) );

		$GLOBALS['page']->add( $form->getBreakRow() );
	
		// ---- custom LO parameters
		$lo = createLO(	$values['objectType'], 
						$values['idResource'], 
						$values['idParam'], 
						array() );
		if( $lo->canBeMilestone() ) {
			if( $values['milestone'] == '' ) 
				$values['milestone'] = '-';
			$arr_milestones = array( 	$lang->def( '_NO') => '-',
										$lang->def( '_ORGMILESTONE_START') => 'start',
										$lang->def( '_ORGMILESTONE_END') => 'end' );
			$GLOBALS['page']->add( 
					$form->getRadioSet( $lang->def( '_ORGMILESTONE' ), 
										'milestone', 
										'milestone', 
										$arr_milestones , 
										$values['milestone']) );
			$GLOBALS['page']->add( '<br />' );

			$startFolder = $treeView->tdb->getMilestone( 'start', (int)$_SESSION['idCourse'] );
			$endFolder = $treeView->tdb->getMilestone( 'end', (int)$_SESSION['idCourse'] );
			$jsOut = '';
			if(  $startFolder !== FALSE && $startFolder->id != $folder->id ) {
				$GLOBALS['page']->add( '<div class="form_line_l">'."\n"
										.'<div class="label_effect">'.$lang->def( '_ORGMILESTONE_PREVSTART').'</div>'
										.'<div class="grouping">' 
										.$startFolder->otherValues[REPOFIELDTITLE]
										.'</div></div>'
										);
				$GLOBALS['page']->add( '<br />' );
				$jsOut .= 'if( milestone_value == "start" )'
							.'if( !window.confirm("'
							.unhtmlentities(addslashes(str_replace( '%LOTitle%', 
										$startFolder->otherValues[REPOFIELDTITLE], 
										$lang->def( '_ORGMILESTONE_PREVSTART_REMOVED'))))
							.'") )'."\n" 
							.'		return false;'."\n";
			}
			if(  $endFolder !== FALSE && $endFolder->id != $folder->id) {
				$GLOBALS['page']->add( '<div class="form_line_l">'."\n"
										.'<div class="label_effect">'.$lang->def( '_ORGMILESTONE_PREVEND').'</div>'
										.'<div class="grouping">' 
										.$endFolder->otherValues[REPOFIELDTITLE]
										.'</div></div>'
										);
				$GLOBALS['page']->add( '<br />' );
				$jsOut .= 'if( milestone_value == "end" )'
							.'if( !window.confirm("'
							.unhtmlentities(addslashes(str_replace( '%LOTitle%', 
										$endFolder->otherValues[REPOFIELDTITLE], 
										$lang->def( '_ORGMILESTONE_PREVEND_REMOVED'))))
							.'") )'."\n" 
							.'		return false;'."\n";
			}	
			/*$GLOBALS['page']->add( '<script type="text/javascript">'."\n"
									.'//<![CDATA['."\n"
									.'var form_prop = null;'."\n"
									.'var milestone_field = null;'."\n"
									.'var milestone_prev_index = 0;'."\n"
									.'window.onload = function() {'."\n"
									.'	form_prop = document.getElementById( "properties" );'."\n"
									.'	milestone_field = form_prop.milestone;'."\n"
									.'	for( var i = 0; i < milestone_field.length; i++ ) {'."\n"
									.'		if( milestone_field[i].checked ) '."\n"
									.'			milestone_prev_index = i;'."\n"
									.'  	milestone_field[i].onchange = function() {'
									.'	}'."\n"
									.'	form_prop.onsubmit = function() {'."\n"
									.'		var milestone_field = form_prop.milestone;'."\n"
									.'		var milestone_value = "-";'."\n"
									.'		for( var i = 0; i < milestone_field.length; i++ ) {'."\n"
									.'			if( milestone_field[i].checked ) '."\n"
									.'				milestone_value = milestone_field[i].value;'."\n"
									.'		}'."\n"
									.'  //alert( milestone_value ); ');
			$GLOBALS['page']->add( $jsOut );
			$GLOBALS['page']->add( '		return true;'."\n"
									.'	}'."\n"
									.'}'."\n"
									.'//]]>'."\n"
									.'</script>'."\n" );*/
		} else {
			$GLOBALS['page']->add( ' <input type="hidden" value="-" id="milestone" name="milestone" />' );
		}
		
		$arrParamsInfo = $lo->getParamInfo();
		if( $arrParamsInfo !== FALSE ) {
			require_once($GLOBALS['where_lms'].'/lib/lib.param.php');
			$param_values = getLOParamArray( $values['idParam'] );
			if( is_callable( array($lo,'renderCustomSettings') ) ) {
				$GLOBALS['page']->add( $lo->renderCustomSettings($param_values,
																 $form,
																 $lang
																)
										);
			} else {
				while( $param = current( $arrParamsInfo ) ) {
					//$GLOBALS['page']->add( '<label for="'.$param['param_name'].'">'.$param['label'].'</label>' );
					if( isset($_POST[$param['param_name']])) {
						$pval = $_POST[$param['param_name']];
					} else if ( isset($param_values[$param['param_name']]) ) {
						$pval = $param_values[$param['param_name']];
					} else {
						$pval = '';
					}
					$GLOBALS['page']->add( $form->getTextfield( $param['label'], 
																$param['param_name'], 
																$param['param_name'], 
																'255', 
																$pval ) );				
					//$GLOBALS['page']->add( ' <input type="text" value="'.$pval.'" name="'.$param['param_name'].'" /><br />' );
					next( $arrParamsInfo );
				}
			}
			$GLOBALS['page']->add( ' <input type="hidden" value="1" name="customParam" /><br />' );			
		}
	} else {
		$GLOBALS['page']->add( ' <input type="hidden" value="'.$values['isTerminator'].'" id="isTerminator" name="isTerminator" />' );
		$GLOBALS['page']->add( ' <input type="hidden" value="" id="selfPrerequisites" name="selfPrerequisites" />' );
		$GLOBALS['page']->add( ' <input type="hidden" value="-" id="milestone" name="milestone" />' );
	}
	
	if(isset($lo) && get_class($lo) == "Learning_ScormOrg") {
		$GLOBALS['page']->add( $form->getTextfield( $lang->def( '_WIDTH' ), 
												"obj_width", 
												"obj_width", 
												4, 
												isset($values['obj_width'])?$values['obj_width']:"") 
							);
		
		$GLOBALS['page']->add( $form->getTextfield( $lang->def( '_HEIGHT' ), 
												"obj_height", 
												"obj_height", 
												4, 
												isset($values['obj_height'])?$values['obj_height']:"") 
							);
	}
}

function organization_property_catalogation(&$treeView, $idItem, &$form, &$lang) {
	$folder = $treeView->tdb->getFolderById( $idItem );
	$values = organization_property_common($treeView, $idItem, $form, $lang, $folder);

	$GLOBALS['page']->add( $treeView->printState() );
	$GLOBALS['page']->add( $form->getHidden('selfPrerequisites', 'selfPrerequisites', $values['selfPrerequisites']) );
	$GLOBALS['page']->add( $form->getHidden('isTerminator', 'isTerminator', $values['isTerminator']) );
	$GLOBALS['page']->add( $form->getHidden('visibility', 'visibility', $values['visibility']) );
	$GLOBALS['page']->add( $form->getHidden('description', 'description', $values['description']) );
	$GLOBALS['page']->add( $form->getHidden('milestone', 'milestone', $values['milestone']) );
	
	$GLOBALS['page']->add( $form->getHidden('obj_width', 'obj_width', $values['obj_width']) );
	$GLOBALS['page']->add( $form->getHidden('obj_height', 'obj_height', $values['obj_height']) );
	$GLOBALS['page']->add( $form->getHidden('publish_from', 'publish_from', $values['publish_from']) );
	$GLOBALS['page']->add( $form->getHidden('publish_to', 'publish_to', $values['publish_to']) );

	$GLOBALS['page']->add( $form->getTextfield( $lang->def( '_VERSION' ), 
												"version", 
												"version", 
												8, 
												isset($values['version'])?$values['version']:"1.0"
												) 
							);
	
	$arr_diff = array( 	'1'	=>	$lang->def( '_VERYEASY' ),
						'2'	=>	$lang->def( '_EASY' ),
						'3'	=>	$lang->def( '_MEDIUM' ),
						'4'	=>	$lang->def( '_DIFFICULT2'),
						'5'	=>	$lang->def( '_VERYDIFFICULT')   
					);
	$selDiff = isset($values['difficult'])?$values['difficult']:"";
	
	$GLOBALS['page']->add( $form->getDropdown(	$lang->def( '_DIFFICULT' ), 
												"difficult", 
												"difficult", 
												$arr_diff , 
												$selDiff 
											)				
						);

						
	$langArray = $GLOBALS['globLangManager']->getAllLangCode();
	if( isset($values['language']) )
		$selLang = $values['language'];
	else
		$selLang = getDefaultLang();

	$GLOBALS['page']->add( $form->getDropdown(	$lang->def( '_LANGUAGE' ), 
												"language", 
												"language", 
												$langArray , 
												$selLang 
											)				
						);
						
	
	$GLOBALS['page']->add( $form->getTextfield( $lang->def( '_RESOURCE' ), 
												"resource", 
												"resource", 
												255, 
												isset($values['resource'])?$values['resource']:"http://"
												) 
							);

	$GLOBALS['page']->add( $form->getSimpleTextarea(	$lang->def( '_OBJECTIVE' ), 
														"objective",
														"objective", 
														isset($values['objective'])?$values['objective']:""
													) 
						);

}

function organization_property_common(&$treeView, $idItem, &$form, &$lang, &$folder) {

	// extract info from POST data
	$values = array();
	$data = $folder->otherValues;
	$values['title'] = ( isset($_POST['title']) ? stripslashes($_POST['title']) : $data[REPOFIELDTITLE] );
	
	if( isset($_POST['stay_on_properties']) ) {
		$values['selfPrerequisites'] = $_POST['selfPrerequisites'];
		$values['isTerminator'] = $_POST['isTerminator'];
		$values['objectType'] = $_POST['objectType'];
		$values['idResource'] = $_POST['idResource'];
		$values['idParam'] = $_POST['idParam'];
		$values['visibility'] = $_POST['visibility'];
		$values['milestone'] = $_POST['milestone'];
		$values['version'] = $_POST['version'];
		$values['difficult'] = $_POST['difficult'];
		$values['description'] = $_POST['description'];
		$values['language'] = $_POST['language'];
		$values['resource'] = $_POST['resource'];
		$values['objective'] = $_POST['objective'];	
			
		$values['obj_width'] = $_POST['obj_width'];	
		$values['obj_height'] = $_POST['obj_height'];
		$values['publish_from'] = $_POST['publish_from'];	
		$values['publish_to'] = $_POST['publish_to'];	
	} else {
		$arrPre = $data[ORGFIELDPREREQUISITES];
		$values['selfPrerequisites'] = $treeView->tdb->extractSelfPrerequisites( $idItem, $arrPre );
	
		$prerequisites = $treeView->tdb->extractPrerequisites( $idItem, $data[ORGFIELDPREREQUISITES] );
		$treeView->itemSelectedMulti = explode(',',$prerequisites);
		
		$values['title'] = $data[REPOFIELDTITLE];
		$values['isTerminator'] = $data[ORGFIELDISTERMINATOR];	
		$values['objectType'] = $data[REPOFIELDOBJECTTYPE];
		$values['idResource'] = $data[REPOFIELDIDRESOURCE];
		$values['idParam'] = $data[ORGFIELDIDPARAM];
		$values['visibility'] = $data[ORGFIELDVISIBLE];
		$values['milestone'] = $data[ORGFIELDMILESTONE];
		$values['version'] = $data[REPOFIELDVERSION];
		$values['difficult'] = $data[REPOFIELDDIFFICULT];
		$values['description'] = $data[REPOFIELDDESCRIPTION];
		$values['language'] = $data[REPOFIELDLANGUAGE];
		$values['resource'] = $data[REPOFIELDRESOURCE];
		$values['objective'] = $data[REPOFIELDOBJECTIVE];
		
		$values['obj_width'] = $data[ORGFIELD_WIDTH];
		$values['obj_height'] = $data[ORGFIELD_HEIGHT];
		$values['publish_from'] = $GLOBALS['regset']->databaseToRegional($data[ORGFIELD_PUBLISHFROM], 'date');	
		$values['publish_to'] = $GLOBALS['regset']->databaseToRegional($data[ORGFIELD_PUBLISHTO], 'date');
	}
	
	$GLOBALS['page']->add( $form->getHidden('objectType', 'objectType', $values['objectType']) );
	$GLOBALS['page']->add( $form->getHidden('idResource', 'idResource', $values['idResource']) );
	$GLOBALS['page']->add( $form->getHidden('idParam', 'idParam', $values['idParam']) );
	
	if($data[REPOFIELDOBJECTTYPE] == 'scormorg' || $data[REPOFIELDOBJECTTYPE] == '') {
		
		$GLOBALS['page']->add( $form->getTextfield( $lang->def('_TITLE'), 
													'title', 
													'title', 
													'255', 
													$values['title'] ) );	
	} else {
		
		$GLOBALS['page']->add( $form->getLineBox( $lang->def('_TITLE'),	$values['title'] ) );
	}
	return $values;
}

function organization_access( &$treeView, $idItem ) {

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	require_once($GLOBALS['where_framework'].'/class.module/class.directory.php');
	$lang =& DoceboLanguage::createInstance('organization', 'lms');
	$folder = $treeView->tdb->getFolderById( $idItem );

	$directory = new Module_Directory();
	$aclManager = new DoceboACLManager();
	if( isset($_POST['okselector']) ) {
		
		$arr_selected = $directory->getSelection($_POST);
		$arr_unselected = $directory->getUnselected();
		$treeView->tdb->_setAccessUG($idItem, ACLKINDUSER, $arr_selected);
		jumpTo( 'index.php?modname=storage' );
	} elseif( isset($_POST['cancelselector']) ) {
		jumpTo( 'index.php?modname=storage' );
	} else {
		if( !isset($_GET['stayon']) ) {
			$directory->resetSelection($treeView->tdb->getAccessUsers($idItem));
		}
		$directory->show_user_selector = TRUE;
		$directory->show_group_selector = TRUE;
		$directory->show_orgchart_selector = FALSE;
		$directory->show_orgchart_simple_selector = TRUE;
		
		$arr_idstGroup = $aclManager->getGroupsIdstFromBasePath('/lms/course/'.(int)$_SESSION['idCourse'].'/subscribed/');
		$directory->setUserFilter('group',$arr_idstGroup);
		$directory->setGroupFilter('path', '/lms/course/'.$_SESSION['idCourse'].'/group');
		
		$directory->multi_choice = TRUE;
		$directory->loadSelector('index.php?modname=storage&amp;org_access=1&amp;idItem='.$idItem.'&amp;stayon=1',
						$lang->def( '_ORG_ACCESS' ),
						$lang->def( '_ORG_ACCESSDESCR' ).' : '
							.'<span class="text_bold">'.$folder->otherValues[REPOFIELDTITLE].'</span>',
						TRUE);
	}
	
}

?>
