<?php
include_once($staticpath.'classes/InputButton.php');
$submitButton = new InputButton;
if(!$_REQUEST['servicefunction'] && $_REQUEST['search_servicefunction']!='list')
	unset($_SESSION['ScdMajorAddRequests.php']);
//die();
if($_REQUEST['servicefunction']=='save')
{
	if(count($_REQUEST['course_id']))
	{
	//var_dump($_REQUEST['course_id']);
		foreach($_REQUEST['course_id'] as $i=>$course_id)
		{
			$current_array = db_request(db_execute("SELECT STUDENT_ID FROM STUDENT_SCHEDULE_REQUESTS WHERE COURSE_ID='".$_REQUEST['course_id'][$i]."' AND COURSE_CREDIT_TITLE='".$_REQUEST['course_credits'][$i]."' AND SCHOOL_YEAR='".get_school_year()."'"),array(),array('STUDENT_ID'));
			//print_r($current_array);die('sss');
	//		var_dump($_REQUEST['student']);
			foreach($_REQUEST['student'] as $student_id=>$yes)
			{
				if(!$current_array[$student_id])
				{
					$sql = "INSERT INTO STUDENT_SCHEDULE_REQUESTS (REQUEST_ID,SCHOOL_YEAR,SCHOOL_ID,STUDENT_ID,SUBJECT_ID,COURSE_ID,COURSE_CREDIT_TITLE,SCHOOL_TERM_ID,WITH_TEACHER_ID,NOT_TEACHER_ID,WITH_SCHOOL_PERIOD_ID,NOT_SCHOOL_PERIOD_ID)
								values(".db_seq_nextval('STUDENT_SCHEDULE_REQUESTS_SEQ').",'".get_school_year()."','".get_school_id()."','".$student_id."','".$_SESSION['subject_id'][$i]."','".$_REQUEST['course_id'][$i]."','".$_REQUEST['course_credits'][$i]."',NULL,'".$_REQUEST['with_teacher_id']."','".$_REQUEST['without_teacher_id']."','".$_REQUEST['with_school_period_id']."','".$_REQUEST['without_school_period_id']."')";
		//	db_execute($sql);
		//echo $sql;
				}
			}
			unset($_REQUEST['servicefunction']);
		}
		if(is_array($_REQUEST['course_section_id'])==1)
			$note = _("This course request has been successfully added for the selected students.");
		else
			$note = _("These course requests have been successfully added for the selected students.");
	}
	else
		BackPrompt(_('You must choose a course'));
}


if($_REQUEST['servicefunction']!='choose_course')
{
	unset($_SESSION['ScdMajorAddRequests.php']);
	DisplayHeader(".:".ProgramTitle().":.");
	if($note)
		DisplayHeader('<IMG SRC=images/check.gif> '.$note);
	if($_REQUEST['search_servicefunction']=='list')
	{
		echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=save method=POST>";
		DisplayHeader('',$submitButton->createSButton(_('Add Request(s) to Selected Students')));		
                echo '<CENTER><DIV id="main_select_div" style="width:90%;"><fieldset style="border:1px solid gray;"><legend><B>'._('Request(s) to Add').'</B></legend>';
		echo '<TABLE border=1 style="width:100%;" id="cs_tb" bgcolor=#'.Preferences('HIGHLIGHT_BACKGROUND').' cellpadding=3>';
		echo '<TR><TD ALIGN='.ALIGN_LEFT.'>'._('Courses').'</TD><TD><B>'._('Include').'</B></TD><TD><B>'._('Exclude').'</B></TD></TR>';
                echo '</TABLE>';
/*
        	echo '<TD><fieldset style="border:1px solid gray;"><legend><B>'._('Include').'</B></legend><TABLE><TR><TD align=center  valign=top>';
		echo '<TABLE><TR><TD align='.ALIGN_LEFT.'>'.
		     _('Teacher Name').'</TD><TD><SELECT style="background-color:'.Preferences('HIGHLIGHT_BACKGROUND').'" name=with_teacher_id><OPTION value="">'._('N/A').'</OPTION>';
	

		$teachers_array = db_request(db_execute("SELECT USER_ID,LAST_NAME,FIRST_NAME,MIDDLE_NAME FROM USERS WHERE SCHOOLS LIKE '%,".get_school_id().",%' AND PROFILE='teacher' ORDER BY LAST_NAME,FIRST_NAME"));
		foreach($teachers_array as $teacher)
			echo '<OPTION value='.$teacher['USER_ID'].'>'.$teacher['LAST_NAME'].', '.$teacher['FIRST_NAME'].' '.$teacher['MIDDLE_NAME'].'</OPTION>';
		echo '</SELECT></TD></TR><TR><TD align='.ALIGN_RIGHT.'>'._('School Period').'</TD><TD><SELECT style="background-color:'.Preferences('HIGHLIGHT_BACKGROUND').'" name=with_school_period_id><OPTION value="">'._('N/A').'</OPTION>';
		$periods_array = db_request(db_execute("SELECT SCHOOL_PERIOD_ID,TITLE FROM SCHOOL_PERIODS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY SORT_ORDER"));
		foreach($periods_array as $period)
			echo '<OPTION value='.$period['SCHOOL_PERIOD_ID'].'>'.$period['TITLE'].'</OPTION>';
		echo '</SELECT></TD></TR></TABLE>';
		echo '</fieldset></table></TD>';


//		echo '<TR><TD colspan=2  background=themes/'.Preferences('HIGHLIGHT_FRAME').'/bottom_shadow.png height=20><font color=black></TR>';

//		echo '<TR><TD align='.ALIGN_LEFT.'  valign=top>'._('Include').'</TD><TD>';
//		echo _('Include:').'<TABLE><TR><TD align='.ALIGN_LEFT.'>'._('Teacher').'</TD><TD><SELECT name=with_teacher_id><OPTION value="">'._('N/A').'</OPTION>';

		//	echo '<TR><TD colspan=2  background=themes/'.Preferences('HIGHLIGHT_FRAME').'/bottom_shadow.png height=20><font color=black></TR>';

	//	echo '<TR><TD align='.ALIGN_LEFT.' valign=middle>'._('Without').'</TD><TD>';
	//	echo '<BR><TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Teacher').'</TD><TD><SELECT name=without_teacher_id><OPTION value="">'._('N/A').'</OPTION>';
		echo '<TD><fieldset style="border:1px solid gray;"><legend><B>'._('Exclude').'</B></legend><TABLE><TR><TD align=center valign=top>';
		echo '<TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Teacher Name').'</TD><TD><SELECT style="background-color:'.Preferences('HIGHLIGHT_BACKGROUND').'" name=without_teacher_id><OPTION value="">'._('N/A').'</OPTION>';

		foreach($teachers_array as $teacher)
			echo '<OPTION value='.$teacher['USER_ID'].'>'.$teacher['LAST_NAME'].', '.$teacher['FIRST_NAME'].' '.$teacher['MIDDLE_NAME'].'</OPTION>';
		echo '</SELECT></TD></TR><TR><TD align='.ALIGN_RIGHT.'>'._('School Period').'</TD><TD><SELECT style="background-color:'.Preferences('HIGHLIGHT_BACKGROUND').'" name=without_school_period_id><OPTION value="">'._('N/A').'</OPTION>';
		foreach($periods_array as $period)
			echo '<OPTION value='.$period['SCHOOL_PERIOD_ID'].'>'.$period['TITLE'].'</OPTION>';
		echo '</SELECT></TD></TR></TABLE>';
		*/
                echo '<div id="more_select_div"></div>';
                echo '<TABLE height=55><TR><TD align='.ALIGN_LEFT.'>';
		echo '<DIV id=course_div></DIV>'."<A HREF=# onclick='window.open(\"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=choose_course\",\"\",\"scrollbars=yes,resizable=yes,width=800,height=400\");' id='course_link'><DIV id=choose_course_div><B>"._('Choose a Course')."</B></DIV></A>";//</TD></TR>";
                echo "</TABLE>";
//		echo '</fieldset></TD></TR>';
		echo '</fieldset></DIV></CENTER><BR>';
	}
}

if(!$_REQUEST['servicefunction'])
{
	if($_REQUEST['search_servicefunction']!='list')
		unset($_SESSION['ScdMajorAddRequests.php']);
	$addition['link'] = array('FULL_NAME'=>false);
	$addition['SELECT'] = ",CAST (NULL AS CHAR(1)) AS CHECKBOX";
	$addition['functions'] = array('CHECKBOX'=>'_makeChooseCheckbox');
	$addition['columns_before'] = array('CHECKBOX'=>'</A><INPUT type=checkbox value=Y name=controller onclick="checkAll(this.form,this.form.controller.checked,\'student\');CheckAll_hilite(this.form,this.form.controller.checked,\'student\')"><A>');
	$addition['new'] = true;
	$addition['options']['onclick']="check_clicked(this);";
	/*Widgets('request');
	Widgets('activity');*/
	Widgets('all');
	
	Search('student_id',$addition);
	if($_REQUEST['search_servicefunction']=='list')
		echo '<BR><CENTER>'.$submitButton->createSButton(_('Add Request(s) to Selected Students'))."</CENTER></FORM>";
}

if($_REQUEST['servicefunction']=='choose_course')
{
	if($_REQUEST['course_id'])
	{
		$weights_array = db_request(db_execute("SELECT COURSE_CREDIT_TITLE FROM COURSE_CREDITS WHERE COURSE_ID='$_REQUEST[course_id]'"));
		if(count($weights_array)==1)
			$_REQUEST['course_credits'] = $weights_array[1]['COURSE_CREDIT_TITLE'];
	}

	if(!$_REQUEST['course_credits'])
		include 'modules/mod_curriculum/ScdCourseProperties.php';
	else
	{
		$_SESSION['ScdMajorAddRequests.php']['count']++;
		$inputs = '<INPUT type=hidden name=course_id['.$_SESSION['ScdMajorAddRequests.php']['count'].'] value='.$_REQUEST['course_id'].'>';
		$inputs .= '<INPUT type=hidden name=course_credits['.$_SESSION['ScdMajorAddRequests.php']['count'].'] value='.$_REQUEST['course_credits'].'>';
		$inputs .= '<INPUT type=hidden name=subject_id['.$_SESSION['ScdMajorAddRequests.php']['count'].'] value='.$_REQUEST['subject_id'].'>';
		
		$course_title = db_request(db_execute("SELECT TITLE FROM COURSES WHERE COURSE_ID='".$_REQUEST['course_id']."'"));
		$course_title = $course_title[1]['TITLE'];
		if(SystemPreferences('WEIGHTS')=='Y')
			$course_title .= ' '.$_REQUEST['course_credits'];

		echo "<script language=javascript>";//opener.document.getElementById(\"course_div\").innerHTML = opener.document.getElementById(\"course_div\").innerHTML + \"<LI>$course_title $inputs\";
		if($_SESSION['ScdMajorAddRequests.php']['count']==1)
			echo "\n opener.document.getElementById(\"choose_course_div\").innerHTML = \"".button('add')._('Choose another Course').'";';
		echo "\n window.close();";
                $teacher = db_request(db_execute("SELECT FIRST_NAME,LAST_NAME,MIDDLE_NAME FROM USERS WHERE USER_ID='$user_id'"));
                //$period = db_request(db_execute("SELECT TITLE FROM SCHOOL_PERIODS WHERE SCHOOL_PERIOD_ID='$school_period_id' AND SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."'"));

                $thr .= ''._('Teacher Name').'<SELECT style="background-color:'.Preferences('HIGHLIGHT_BACKGROUND').'" name=with_teacher_id['.$_SESSION['ScdMajorAddRequests.php']['count'].']><OPTION value="">'._('N/A').'</OPTION>';
                $teachers_array = db_request(db_execute("SELECT DISTINCT(cs.TEACHER_ID),u.FIRST_NAME,u.USER_ID,u.LAST_NAME,u.MIDDLE_NAME FROM COURSE_SECTIONS cs ,USERS u WHERE cs.TEACHER_ID = u.USER_ID AND COURSE_ID = '".$_REQUEST['course_id']."'"));
                //var_dump($teachers_array);
		foreach($teachers_array as $index=>$teacher){
			$thr .= '<OPTION value='.$teacher['TEACHER_ID'].'>'.$teacher['LAST_NAME'].', '.$teacher['FIRST_NAME'].' '.$teacher['MIDDLE_NAME'].'</OPTION>';
                }
		$thr .= '</SELECT>';
                $per = _('School Period').'<SELECT style="background-color:'.Preferences('HIGHLIGHT_BACKGROUND').'" name=with_school_period_id['.$_SESSION['ScdMajorAddRequests.php']['count'].']><OPTION value="">'._('N/A').'</OPTION>';
		//$periods_array = db_request(db_execute("SELECT SCHOOL_PERIOD_ID,TITLE FROM SCHOOL_PERIODS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY SORT_ORDER"));
                $periods_array = db_request(db_execute("SELECT DISTINCT(cs.SCHOOL_PERIOD_ID),sp.SCHOOL_PERIOD_ID,sp.TITLE FROM COURSE_SECTIONS cs, SCHOOL_PERIODS sp WHERE cs.SCHOOL_PERIOD_ID=sp.SCHOOL_PERIOD_ID AND COURSE_ID = '".$_REQUEST['course_id']."'"));
		foreach($periods_array as $index=>$period)
			$per .= '<OPTION value='.$period['SCHOOL_PERIOD_ID'].'>'.$period['TITLE'].'</OPTION>';
		$per .= '</SELECT>';


                echo "window.opener.jQuery('#cs_tb tr:last').after('<tr><td>".$course_title.$inputs."</td><td><p>".$thr."</p><p>".$per."</p></td><td><p>".$thr."</p><p>".$per."</p></td></tr>');";
                //echo 'window.opener.jQuery("#more_select_div").load("ajax.php",{function_:"get_form",count:"'.$_SESSION['ScdMajorAddRequests.php']['count'].'",course:"'.$course_title.'"},function(data){
//                        alert(data);
  //                  });
    //                alert(a);
      //              ';
                //echo 'window.opener.jQuery("#more_select_div").append(\'<TABLE border=1 style="width: 100%;" bgcolor=#'.Preferences('HIGHLIGHT_BACKGROUND').' cellpadding=3><tr><td>'.$course_title .$inputs.'</td></tr></table>\');';
                echo "</script>";
	}
}

function _makeChooseCheckbox($value,$title)
{	global $THIS_array;

	return "<INPUT onchange='hl_cb(this)' type=checkbox name=student[".$THIS_array['STUDENT_ID']."] value=Y>";
}

?>