<?php
/**
 * unit_itstdadd_view.php - itstd view file
 *
 * @author $Author: dtong $
 * @version $Id: unit_itstdadd_view.php,v 1.42 2011/03/28 04:47:36 dtong Exp $
 * @copyright Copyright (c) 2009, Tiller Software Co., Ltd.
*/
  	$view_divid = $template->getval('view_divid');
  	//$_divid = $template->getval('_divid');
	$col_checkbox = $template->getval('_col_checkbox');

	$_title = lang('target_ittitle');
	if ($template->getval('_title') != FALSE )
		$_title = $template->getval('_title');

	$select_array["icon1"] = $template->getval('_select1');
	$select_array["icon2"] = $template->getval('_select2');

	$printsep=TRUE;
	$typeid = $template->type();

	$resourcetypeid = $template->getval('_resourcetypeid');
	if (!isset($schoolid))
		$schoolid = 0;

	if (!isset($deptid))
		$deptid = 0;

	if ($upload_flag) {
  	 	$itstdaddurl = base_url() . "unit/itstd/sample/$typeid/$resourcetypeid/$schoolid/{$role_type}";
  	 //	echo 'khon it'.$itstdaddurl;
	} else {
  		$itstdaddurl = base_url() . "unit/itstd/addo/$typeid/$resourcetypeid/$unitid";
  	//	echo 'teacher'.$itstdaddurl;
	}

	$update_check = base_url() ."unit/itstd/update_check/";

  	$itstdupdateurl = base_url() . "unit/itstd/update/";

	$unitreadurl = base_url() ."unit/unit/read/";
	$resourceurladd = base_url() . "unit/resource/addo/$resourcetypeid/_TARGETID_/$schoolid/{$role_type}";
	$resourceurledit = base_url() ."unit/resource/editit/$resourcetypeid/_RESID_/$schoolid/{$role_type}";
	$resourceurldelete = base_url() ."unit/resource/deleteo/_RESID_/$type";
/*
	$open = "var reswin=dhtmlwindow.open('dhtmlwin_resource','iframe','_OPENURL_','Upload Resource', 'width=580px,height=650px,resize=0,scrolling=0,center=1', 'recal'); ".
	"; reswin.onclose= function(){ alert('$itstdupdateurl'); ".
	"  parent.ajaxGetpage('$view_divid', '_REFRESH_', false); ".
	"  return TRUE; };  ";
*/	//fix bug in unit page (resource)
	$open = "newIframeWindow('_OPENURL_','Upload Resource',580,700,'resource'); return false;";

	//" ; ";
	$edit_script  = str_replace("_OPENURL_", $resourceurledit ,$open);
	$edit_script  = str_replace("_REFRESH_", $itstdaddurl ,$edit_script);

	$edit_delete  = "<div class=\"unitbutton\"><a href='{$resourceurldelete}' class=\"buttonalink\" " .
						 "onclick=\"return delUnitItem('{$resourceurldelete}','{$itstdaddurl}','{$view_divid}'," .
						 "'Are you sure to delete this Sample upload resource?')\" >" .
						 "<img title=\"Delete\" src=\"" . osa_imagepath() . "x.gif\" border=\"0\" /></a></div>&nbsp;";
	$edit_delete .= "<div class=\"unitbutton\"><a href='{$resourceurledit}'   class=\"buttonalink\" " .
						 "onclick=\"$edit_script return false; \" ><img title=\"Edit\" src=\"" . osa_imagepath() .  "edit.gif\" border=\"0\" /></a></div>";

	$add_script  = str_replace("_OPENURL_", $resourceurladd ,$open);
	$add_script  = str_replace("_REFRESH_", $itstdaddurl ,$add_script);

	$add_button  = "<div class=\"unitbutton\" ><a href='{$resourceurladd}'    class=\"buttonalink\" onclick=\"$add_script return false;\"><img align=top title=\"Add Upload/URL Resource\" src=\"" . osa_imagepath() . "up.gif\" border=\"0\"></a></div>";

	$onclick   = "newDhtmlWindow('_UNITID_','Units',700,700,'unitread'); return false; ";
	$unit_view = "<a style=\"color:blue;\" align=\"top\" href=\"_UNITID_\"  ".
				 "onclick=\"$onclick \" >_UNITTITLE_</a>";
	//Div popup
	$_popup_div = $template->getval('_popup_div');

?>
<form name="itstd_form">
<div class="ltwin_body">
<center>
<?php if ($template->getval('_title') != FALSE ) { ?>
	<div class="ltwin_title"><center>
	<?php
	if ($upload_flag && $schoolname != '')
		echo "<b>".$_title." - ".$schoolname."</b>";
	else
		echo "<b>".$_title."</b>";
	?>
	</center></div>
<?php }?>

<?php if(!$upload_flag && $readonly != FALSE) { ?>
		<div class="unit_itstdadd_close">
		<input type="button" value="<?php echo lang('unit_itstdadd_close'); ?>" name="close" <?php osa_btn()?>
			onClick="closeDhtmlWindow('<?php echo $_popup_div;?>');" />

		</div>

<?php } ?>
<?php
if ( !$db ) {
		echo '<div style="font-size:16px;padding-top:50px;color:red;font-weight:bold">'.$template->getval('_nodatamsg').'</div>';
	}
	else{
	?>
<table cellspacing=0px;	cellpadding=0px;  class="itstd_table" width="97%" >
<?php
	foreach ( $db_struct as $strandid => $strand ) {
?>
	<?php
	if ($upload_flag == FALSE) {
		foreach ( $itstd_school as $school ) {
	    	if ( $strandid == $school->strandid ) {
	    	?><tr><td colspan=4 ><div class="itstd_school">
	    	<?php
	  			echo $school->schoolname;
	  			break;
	  		?></div></td></tr><?php
	    	}
	    }
	}
 	?>

	<tr><td colspan=4 >
		<div class="itstd_strand" style="padding-top:5	px;">
		<?php echo lang('target_strand').": {$strand[0]}"; ?></div>
		</td>
	</tr>
<?php
	foreach ( $strand as $standardid => $standard ) {
		if ($standardid!=0)  {
	?>
		<tr>
			<td colspan=4 >
			<div style="border-top:solid 1px;border-color:black;padding:3px;font-weight:bold;font-size:12px;" class="tedit_standard" >
			<?php echo lang('target_standard').": {$standard[0]}"; ?></div>
			</td>
		</tr>
		<tr>
			<td>
				<table cellspacing=0px; cellpadding=0px; width="100%" style="margin:0px;padding:0px;" border="0px" >
				<tr>
				<!--<th style="width:3%;  " class="itstd_th_title" ><?php echo "" ?></th> ?> change TL to checkbox	-->
				<?php
					if ($upload_flag == FALSE) {  // general teacher ?>
						<th style="width:25%; padding:3px;" class="itstd_th_title" ><?php echo lang('target_benchmark'); ?></th>
						<th style="width:35%; " class="itstd_th_title" ><?php echo lang('target_reverse'); ?></th>
						<th style="width:31%; " class="itstd_th_title" ><?php echo lang('target_samples');  ?></th>
						<?php
						if (isset($col_checkbox) && $col_checkbox != FALSE){
							foreach ($col_checkbox as $col){ ?>
								<th style="width:3%;  " class="itstd_th_title" ><?php echo $col; ?></th>
							<?php
							}
						}
					}
					else { 	// khon it ?>
						<th style="width:50%; padding:3px;" class="itstd_th_title" ><?php echo lang('target_benchmark'); ?></th>
						<?php
						if (isset($col_checkbox) && $col_checkbox != FALSE && count($col_checkbox) > 1 ) {
							foreach ($col_checkbox as $col){ ?>
								<th style="width:2%;  " class="itstd_th_title" ><?php echo $col; ?></th>
						   <?php
							}
						}
						?>
						<th style="width:40%; " class="itstd_th_title" ><?php echo lang('target_samples');  ?></th>
			<?php
					} ?>

					<!--	<th style="width:5%;  " class="itstd_th_title" ><?php echo lang('target_tlegends'); ?></th>
					<th style="width:5%;  " class="itstd_th_title" ><?php echo lang('target_llegends'); ?></th>  -->
				</tr>
				<?php
				foreach ( $standard as $benchmarkid => $benchmark ) {
					if ($printsep) { $class="style=\"background-color: #EEE;\""; $printsep=FALSE;} else {$class =""; $printsep = true;}
					if ($benchmarkid!=0)  {
						$libtype = '';

						//echo $benchmark->benchmarkid;
						if ( array_key_exists($benchmark->benchmarkid, $selectedids) ) {
							if ($selectedids[$benchmark->benchmarkid]->select1==1)
								$libtype = '<font class="text_red"">T</font>';
							if ($selectedids[$benchmark->benchmarkid]->select2==1)
								$libtype .= '<font class="text_blue"">L</font>';
						}
 				?>
				<tr <?php echo $class;?> >
				<td style="padding:3px; " valign="top">
				<?php
					//if (isset($unitid)==FALSE || $unitid === FALSE || $unitid==0) //ict/lib upload
					if ( !empty($libtype) )
						$libtype = ', ' . $libtype;
					if ($upload_flag == FALSE)
						 echo $benchmark->benchmark . $libtype;
					else
						 echo $benchmark->benchmark;
				?>
				</td>

				<?php // reverse link
					//echo $upload_flag;
					if (!$upload_flag == FALSE) {
						//khon it
						//echo $upload_flag;//'reverse link'  ;
					}
					else {
						//echo '<pre>'; print_r($benchmark); echo '</pre>';
						// general teacher ?>
						<td style="border-left:solid 1px;border-color:black; padding:3px;"  valign="top"><div >
							<?php
								$sep = "";
								$link = "";
								$tec = '<font class="text_red"">T</font>';
								$lib = '<font class="text_blue"">L</font>';
								if ( isset($reverseids) && (array_key_exists( $benchmark->benchmarkid, $reverseids)) ) {
									//$link .= count($reverseids);
									//echo '<pre>'; print_r($reverseids ); echo '</pre>';
									foreach ( $reverseids[$benchmark->benchmarkid] as $key => $record ) {
										 $link .= $sep ."&#8226 ". str_replace("_UNITID_",$unitreadurl. $record->unitid ,$unit_view);
								 	 	 $link  = str_replace("_UNITTITLE_", $record->title ,$link);
								 	 	 if (isset($record->code)) {

								 	 	 	$code = str_replace($record->unitid.'_','', $record->code);
								 	 	 //	$link .= CMS_LIBTEC_TYPE;
								 	 	 	switch($code) {
								 	 	 		case CMS_UNIT_LIBTEC_TYPE : $link .= '&nbsp;,&nbsp;'. $tec . $lib;
								 	 	 			break;
								 	 	 		case CMS_UNIT_LIB_TYPE : $link .= '&nbsp;,&nbsp;'.$lib;
								 	 	 			break;
								 	 	 		case CMS_UNIT_TEC_TYPE : $link .= '&nbsp;,&nbsp;'.$tec;
								 	 	 			break;
								 	 	 	}

								 	 	 	///$link .= str_replace($record->unitid.'_','', $record->code);
								 	 	 }
								 		 $sep   = "<br />";
									}
									//$link .= $record->unitid;
									// $link .= '5555555555555555';
								} else { $link = "&nbsp;" ; }
								echo $link;
							?></div>
						</td>
				<?php // reverse link
					}
					$showbigt = '';
					$showbigl = '';
					$checked1 = '';
					$checked2 = '';
					if ( isset($benchmarkids) && (in_array($benchmark->benchmarkid, $benchmarkids)) ) {
					 	if ($benchmark->select1 <= 0) {
					 		$showbigt =$select_array["icon1"][0];
					 	} else {
					 		$showbigt =$select_array["icon1"][$benchmark->select1];
					 	}
						if ($benchmark->select2 <= 0) {
					 		$showbigl =$select_array["icon2"][0];
					    } else {
					 		$showbigl =$select_array["icon2"][$benchmark->select2];
					 	}
					}

					if ( array_key_exists($benchmark->benchmarkid, $selectedids) ) {
						if ($selectedids[$benchmark->benchmarkid]->select1==1)
							$checked1='checked="checked"';
						if ($selectedids[$benchmark->benchmarkid]->select2==1)
							$checked2='checked="checked"';
					}
				?>

				<?php
					$disable = '';
					if ($readonly)
						$disable = 'disabled="disabled"';
					if ($upload_flag != FALSE) {
						if (isset($col_checkbox) && $col_checkbox != FALSE && count($col_checkbox) > 1 ) {
						$onclick = "onClick=\"update_checkbox('{$update_check}{$benchmark->id}')\"";
						?>
								<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center" valign="middle">
		 						<?php echo "<input type=\"checkbox\" name=\"itstd_1_{$benchmark->id}\" value=\"$benchmark->benchmarkid\" $checked1 $disable $onclick />";  ?>
								</td>
								<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center" valign="middle">
		 						<?php echo "<input type=\"checkbox\" name=\"itstd_2_{$benchmark->id}\" value=\"$benchmark->benchmarkid\" $checked2 $disable $onclick />";  ?>
								</td>
				<?php }
					} ?>

				   <td style="border-left:solid 1px;border-color:black; padding:3px; height:26px;" valign="top"  align="center">
				   <span style="position:relative;display:block;">
					<table border="0" width="100%">
					<?php // Upload Resource
						$link  = "";
						if ($upload_flag  && $readonly===FALSE) { 	// khon it
							$link .= '<div style="position:absolute; right:0px;">';
						 	//$link .= "<div class=\"unitbutton\" ><a href=\"{$resourceurladd}{$resourcetypeid}/{$benchmark->id}\" class=\"buttonalink\" ".
 				  			//	     " onclick=\"newIframeWindow('{$resourceurladd}{$resourcetypeid}/{$benchmark->id}','Upload Resource',480,500,'resource');return false;\">".
 				  			//	     "<img align=top title=\"Add Upload/URL Resource\" src=\"" . osa_imagepath() . "up.gif\" border=\"0\"></a></div>";

							$addbutton = str_replace("_TARGETID_","$benchmark->benchmarkid",$add_button);

							$link .= $addbutton;
				     	   	$link .= '</div>';
						}
						$spantd = "";
					 	$edbutton ="";

						if ( isset($resourceids) && is_array($resourceids)  ) {
 							if ( array_key_exists($benchmark->benchmarkid, $resourceids) ) {
						   		$resources = $resourceids[$benchmark->benchmarkid];
		   						$last_url ='';
		   						$last_resid ='';
		   						$span = 1;
		   						foreach ($resources as $uploads) {
		   							$ictlib = '';
		   							$link .= "<tr><td class=\"itemtext\" style='text-align:left;' >";
		   							if  ($last_resid != $uploads->resid) {
		   							 	/*
		   							 	if ( strlen($uploads->text1) > 20 ) {
		   								 	$link .= wordwrap($uploads->text1, 20, "<br />\n");;
		   							 	} else {
		   							 		$link .= $uploads->text1;
		   							 	} */
		   							 //	$link .= '1';//htmlspecialchars_decode($uploads->text1);
		   							 	if (isset($uploads->libtype) && $uploads->libtype != '')
												 $ictlib = ',&nbsp;'.$uploads->libtype;
												 //width="100%"
											$link .= '<table border="0" ><tr><td>'.htmlspecialchars_decode($uploads->text1).'</td><td >'.$ictlib.'</td></tr></table>';
											//multi url
											$last_url ='';
											if (isset($res_urls) && $res_urls != FALSE){
												if ( isset($res_urls[$uploads->resid]) ){
													$urls = $res_urls[$uploads->resid];
													$first_step = TRUE;
													foreach ($urls as $value ){
														$class = '';
														if ($first_step){
															$class= 'urlitem';
															$first_step = FALSE;
														}
														if ( !is_null($value->url) ) {
															$last_url .= '<div class='.$class.'>'; //. lang('unit_uploadedurl') . ': ';
							   								if ($unitid != 0 ) { $last_url .= lang('unit_uploadedurl') . ': '; }
															$last_url .= "<a target=\"_blank\" href=\"http://$value->url\">http://{$value->url}</a>";
														}
													}
												}

											}

								 	}

								 	$filesize = "";
									if ($uploads->size > 0)
										$filesize = ' (' . osa_filesizestr($uploads->size) . ')';

							 		$link .= '<div class="uploaditem">' ;  //. lang('unit_uploadedfile') . ': '
							 		//if ($unitid != 0 ) { $link .= lang('unit_uploadedfile') . ': '; }
									//$href= base_url() . "unit/resource/file/{$uploads->uploadid}/" . urlencode($uploads->filename);
									$href= base_url() . "unit/resource/file/{$uploads->uploadid}/" . osa_filename_hash($uploads->filename) . "/" .urlencode($uploads->filename);
									$link .= "<a onclick=\"return downloadWindow('{$href}')\" target=\"_blank\" href=\"" . $href . '">' .$uploads->filename. '</a>'.
									'<span class="uploadsize">' . $filesize . '</span></div>';

								 	if  ( ($last_resid != $uploads->resid) ) {
								 		if ($upload_flag && $readonly===FALSE) {   // khon it
											$edbutton = str_replace("_UNITID_","$unitid",$edit_delete);
											$edbutton = str_replace("_RESID_","$uploads->resid",$edbutton);
											$edbutton = str_replace("_TARGETID_","$benchmark->id",$edbutton);
								 		}
										$last_resid  = $uploads->resid;
										$link .= $last_url;
										$link .= '<td rowspan="span" valign="bottom" style="white-space: nowrap;">'.$edbutton.'</td>';
										$span += 0;
								  	}

								}
								$link = str_replace("rowspan=span","rowspan=$span",$link);
								$link .= "</td></tr>";
								$span = 1;
							}
						}
						if ($link == "") { $link = "<tr><td>&nbsp;</td></tr>"; }
						echo $link;
						// Upload Resource
					?>
					</table>
					</span>
					</td>
					<?php	/*
					<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center"><?php echo $showbigt; ?></td>
					<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center"><?php echo $showbigl; ?></td>
					*/
					?>
					<?php
						$checked1 = '';
						$checked2 = '';
						if (isset($unit_selecteds) && isset($unit_selecteds[$benchmark->benchmarkid])) {
							$uids = $unit_selecteds[$benchmark->benchmarkid];
							if (isset($uids->select1) && $uids->select1== 1)
								$checked1='checked="checked"';
							if (isset($uids->select1) && $uids->select2== 1)
								$checked2='checked="checked"';
							//echo '<pre>'; print_r($uids); echo '</pre>';
						}

						if ($upload_flag == FALSE) { // general teacher
							if (isset($col_checkbox) && $col_checkbox != FALSE && count($col_checkbox) > 1 ) {   // double checkbox
							?>
								<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center" valign="middle">
								<?php echo "<input type=\"checkbox\" name=\"itstd_1_{$benchmark->id}\" value=\"$benchmark->benchmarkid\" $checked1 $disable />";  ?>
								</td>
								<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center" valign="middle">
								<?php echo "<input type=\"checkbox\" name=\"itstd_2_{$benchmark->id}\" value=\"$benchmark->benchmarkid\" $checked2 $disable />"; ?>
								</td>
							<?php
							}
							elseif (isset($col_checkbox) && $col_checkbox != FALSE && count($col_checkbox) == 1 ) { ?>
								<td style="border-left:solid 1px;border-color:black; padding:3px; " align="center" valign="middle">
								<?php echo "<input type=\"checkbox\" name=\"itstd_1_{$benchmark->id}\" value=\"$benchmark->benchmarkid\" $checked1 $disable />";  ?>
								</td>
					<?php
							}
						}
					?>
					</tr>
				<?php
					}
				}
				?>
				</table>
			</td>
		</tr>
	<?php
		}
	}
}
?>
</table>
<div style="padding:10px"></div>
<?php

	if ($upload_flag) { // khon it
	/*
	 hide because in div content
	<div ><input type="button" value="Close" name="close" class="btn" onMouseover="if(this.className) this.className='btnhov';" onMouseout="if(this.className) this.className='btn';"
		         onClick="parent.closeDhtmlWindow('<?php echo $template->getval('_divid') ?>');return false;" />
	</div>
	 */
	?>
<?php
 	} else {
 	if($readonly==FALSE){?>
	<input type="button" value="<?php echo $template->getval('_cancel_text');?>" name="close" <?php osa_btn()?>
		onClick="cancelDhtml('<?php echo $template->getval('_divid') ?>');" />
	<span style="padding:20px">&nbsp;</span>
	<input type="button" value="<?php echo $template->getval('_save_text');?>" name="save" <?php osa_btn()?>
		onClick="submitDhtml('<?php echo $itstdupdateurl .$unitid . "/" . $typeid ?>', 'itstd_form',
		'<?php echo base_url() . "unit/unit/stage/" . $template->getval('_stage_num') . "/" .CMS_ACTION_EDIT . "/" . $template->unitid() ?>',
		'<?php echo $template->getval('_stage_div') ?>', '<?php echo $template->getval('_divid') ?>');" />
<?php }
	else{?>
	<div class="unit_itstdadd_close">
		<input type="button" value="<?php echo lang('unit_itstdadd_close'); ?>" name="close" <?php osa_btn()?>
				onClick="closeDhtmlWindow('<?php echo $_popup_div;?>');" /></div>
<?php
 	}
 	}
	} ?>

</center>
</div>
</form>