<?php

$default_year = get_school_year();
$pre_year = $default_year;
$aft_year = $default_year+1;
$days = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

if($_REQUEST['select_course_section_id'] && ($_REQUEST['select_course_section_id'] != 'N/A') && ($_REQUEST['button_create'] == 'Create')){
/*	if($_REQUEST['select_class']!='All')
	{
		$stu_array = db_request(db_execute("SELECT s.STUDENT_ID as ADN,s.LAST_NAME as LAST,s.FIRST_NAME as FIRST,s.MIDDLE_NAME as M,sr.START_DATE,sr.END_DATE FROM 
							STUDENTS s, STUDENT_REGISTRATION sr WHERE s.STUDENT_ID IN (SELECT STUDENT_ID FROM STUDENT_SCHEDULE WHERE 
							SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' AND s.STUDENT_ID=sr.STUDENT_ID AND sr.SCHOOL_YEAR=".get_school_year()." AND
							COURSE_SECTION_ID='".$_REQUEST['select_course_section_id']."' AND (END_DATE >= '".get_db_date()."' OR END_DATE IS NULL))  AND 
							(('".get_db_date()."' BETWEEN sr.START_DATE AND sr.END_DATE) OR sr.END_DATE IS NULL) ORDER BY s.LAST_NAME ASC"));
	}else{*/
		$stu_array = db_request(db_execute("SELECT s.STUDENT_ID,s.CFIELD_14 as ADN,s.LAST_NAME as LAST,s.FIRST_NAME as FIRST,s.MIDDLE_NAME as M FROM 
							STUDENTS s, STUDENT_REGISTRATION sr WHERE s.STUDENT_ID IN (SELECT STUDENT_ID FROM STUDENT_SCHEDULE WHERE 
							SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' AND 
							COURSE_SECTION_ID='".$_REQUEST['select_course_section_id']."' AND (END_DATE >= '".get_db_date()."' OR END_DATE IS NULL)) AND s.STUDENT_ID=sr.STUDENT_ID AND sr.SCHOOL_YEAR=".get_school_year()." AND
							(('".get_db_date()."' BETWEEN sr.START_DATE AND sr.END_DATE) OR sr.END_DATE IS NULL) ORDER BY s.LAST_NAME ASC"));
							
/*							echo "SELECT s.STUDENT_ID as ADN,s.CFIELD_142 as CL,s.LAST_NAME as LAST,s.FIRST_NAME as FIRST,s.MIDDLE_NAME as M FROM 
							STUDENTS s, STUDENT_REGISTRATION sr WHERE s.STUDENT_ID IN (SELECT STUDENT_ID FROM STUDENT_SCHEDULE WHERE 
							SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' AND 
							COURSE_SECTION_ID='".$_REQUEST['select_course_section_id']."') AND s.STUDENT_ID=sr.STUDENT_ID AND sr.SCHOOL_YEAR=".get_school_year()." AND
							((".get_db_date()." BETWEEN sr.START_DATE AND sr.END_DATE) OR sr.END_DATE IS NULL) ORDER BY s.LAST_NAME ASC";
							*/
//	}
	if($_REQUEST['teacher_id'])
		$teacher_name = db_request(db_execute("SELECT FIRST_NAME".CCAT."'  '".CCAT."LAST_NAME AS TEACHER_NAME FROM USERS WHERE USER_ID ='".$_REQUEST['teacher_id']."'"));

	$course_array = db_request(db_execute("SELECT c.TITLE,cc.CREDITS,c.SHORT_NAME,cs.SCHOOL_TERM_ID FROM COURSE_SECTIONS cs, courses c,COURSE_CREDITS cc WHERE cs.TEACHER_ID ='".$_REQUEST['teacher_id']."' 
				AND cs.SCHOOL_ID ='".get_school_id()."' AND cs.SCHOOL_YEAR ='".get_school_year()."' AND cs.COURSE_SECTION_ID='".$_REQUEST['select_course_section_id']."'
				AND cs.COURSE_ID = c.COURSE_ID AND cc.COURSE_ID = c.COURSE_ID
				"));
	$course_name = $course_array[1]['TITLE'];
	$credits = $course_array[1]['CREDITS'];
	$ccode = $course_array[1]['SHORT_NAME'];
	$school_term = get_school_term($course_array[1]['SCHOOL_TERM_ID']);
        foreach($days as $day)
        {
            $course_name = str_replace($day,_($day).' ',$course_name);
        }
	ob_start();

	echo "<table width='100%'><TR><TD align=center><br><b>ARRUPE JESUIT UNIVERSITY </b></TD></TR></table>";
	echo "<table width='100%'><TR><TD align=left><b> COURSE: ".$course_name."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   [".$credits." credits]
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CODE: ".$ccode."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SCHOOL TERM: ".$school_term."</b><BR/>";
	echo "<b>LECTURER: ".$teacher_name[1]['TEACHER_NAME']."</b></TD></TR></table>";
	echo "<table width='95%' cellspacing='0' align=center border='1' bordercolor='black'>";

	
/*	for($z=0;$z<2;$z++)
	{
		echo '<tr><td>1&nbsp;</td>
			  <td>2&nbsp;</td>
			  <td>3&nbsp;</td>';

		for($y=0;$y<15;$y++)
		{
			echo '<td>&nbsp;</td>';
		}
		echo '</tr>';
	}
*/
	echo "<tr><td>&nbsp;</td><td colspan=9 align=center>COURSE WORK</td><td colspan=2 align=center>EXAM</td><td colspan=4 align=center>TOTALS</td></tr>";
	echo '<tr><td><B>STUDENT REG.#, NAME</B></td>
			  <td><B>Paper 1 (100%)</B></td>
			  <td><B>10%</B></td>
			  <td align=center><B>Paper 2 (100%)</B></td>
			  <td align=center><B>10%</B></td>
			  <td align=center><B>Mid-Sem (100%)</B></td>
			  <td align=center><B>25%</B></td>
			  <td align=center><B>Pres. (100%)</B></td>
			  <td align=center><B>5%</B></td>
			  <td align=center><B>Total 50%</B></td>
			  <td align=center><B>Final 50%</B></td>
			  <td align=center><B>Total 50%</B></td>
			  <td align=center><B>100%</B></td>
			  <td align=center><B>Final Mark</B></td>
			  <td align=center><B>Grade</B></td>';
			  
//	foreach($assignment_array as $assignment)
//	{
//		echo '<td align=center>'.$assignment['TITLE'].'</td>';
//	}
	echo '</tr>';

/*	
	for($y=0;$y<12;$y++)
	{
		echo '<td align=center>100</td>';
	}
	echo '</tr>';
*/		  
	for($i=1;$i<count($stu_array)+1;$i++)
	{
		echo '<tr>';
		echo '<td align=center>'.$stu_array[$i]['ADN'].'&nbsp;<br/>'.$stu_array[$i]['LAST'].', '.$stu_array[$i]['FIRST'].' '.$stu_array[$i]['M'].'</td>';

		for($y=0;$y<14;$y++)
		{
			echo '<td>&nbsp;</td>';
		}
		echo '</tr>';
	}
	echo "</table><br/><br/>";
	echo "<table width='95%' cellspacing='0' align=center border='0'>";
	echo "<tr><td>Signed:______________ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date:_______________&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>__________________________</td></tr>";
	echo "<tr><td align=left>Lecturer</td><td>DEAN</td></tr>";
	
//	echo "<table cellpadding='3' align=center cellspacing='0' bordercolor='red' border='1'><tr><td align=center><font color=red>CA=Continuous Assessment, CE=Class Exercises, AS=Assignments, PJT=Projects & Practicals, EX=Exam</font></td></tr></table>";

	if($_SERVER['HTTPS']=='on')
		$html = str_replace('=(["\'])?assets/',"=\\1https://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')).'/assets/',ob_get_contents());
	else
		$html = ereg_replace('=(["\'])?assets/',"=\\1http://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')).'/assets/',ob_get_contents());

	ob_end_clean();

	if(!$htmldocPath)
		ErrorMessage(array(_("PDFs cannot be produced because HTMLDOC isn't working. Please ensure that HTMLDOC is installed and that its path is configured in config.php")), 'fatal');

	if(function_exists('sys_get_temp_dir'))
		$tmp_dir = sys_get_temp_dir();

	$temphtml=tempnam($tmp_dir,'html');

    $fp=@fopen($temphtml,"w+");
    if (!$fp) {
    	die("Can't open $temphtml");
    }
//    echo '<HTML><BODY>'.$html.'</BODY></HTML>';
    fputs($fp,'<HTML><BODY>'.$html.'</BODY></HTML>');
    @fclose($fp);

    header("Cache-Control: public");
    header("Pragma: ");
    header("Content-Type: application/pdf");
    header("Content-Disposition: inline; filename=\"".".:".ProgramTitle().":.".".pdf\"\n");

    $orientation = 'portrait';

    if(($_REQUEST['customised_view'] && $_REQUEST['customised_view']!='false') || $_SESSION['orientation'] == 'landscape')
    {
    	$orientation = 'landscape';
    	unset($_SESSION['orientation']);
    }

	if($language!='en_US')
	    	$charset = '--charset utf-8';
    putenv("HTMLDOC_NOCGI=1");
    passthru("$htmldocPath --webpage --embedfonts --quiet -t pdf12 --jpeg --no-links --$orientation $charset --footer t --header . --left 0.2in --top 0.2in \"$temphtml\"");
	@unlink($temphtml);
	
}else{
	DisplayHeader(".:".ProgramTitle().":.");
	
//	echo _('This function is disabled.');	
	$dis_bg=false;
	if($dis_bg===false)
	{
	echo '<br><br>';
	echo PopTable('header',_('Select options'));
	echo "<FORM name='blank_gradebook' action=modloader.php?servicemod=mod_grades/BlankGradebook.php&selected_menu=$_REQUEST[selected_menu]&link_menu=$_REQUEST[link_menu] METHOD=POST>";
	echo '<table align=center>';
	
	
	//get teacher names
        if(get_user('PROFILE')=='teacher')
        {
            $teacher_array = db_request(db_execute("SELECT DISTINCT(USER_ID),LAST_NAME,FIRST_NAME,SCHOOLS FROM USERS WHERE PROFILE='teacher' AND USER_ID=".get_user_id()." ORDER BY LAST_NAME ASC"),array(),array('USER_ID'));
        }else{
            $teacher_array = db_request(db_execute("SELECT DISTINCT(USER_ID),LAST_NAME,FIRST_NAME,SCHOOLS FROM USERS WHERE PROFILE='teacher' ORDER BY LAST_NAME ASC"),array(),array('USER_ID'));
        }
	echo '<tr><td align=left>'._('Select a teacher').':</TD><TD><select onchange="document.blank_gradebook.submit();" name=select_teacher><option value="N/A">'._('N/A').'</option>';

	foreach($teacher_array as $teacher)
	{
            if(strpos($teacher[1]['SCHOOLS'],get_school_id())===false)
            {
//                This blank string
            }else{
		if($teacher[1]['USER_ID']==$_POST['select_teacher'])
			echo '<option value='.$teacher[1]['USER_ID'].' SELECTED>'.$teacher[1]['LAST_NAME'].','.$teacher[1]['FIRST_NAME'].'</option>';
		else
			echo '<option value='.$teacher[1]['USER_ID'].'>'.$teacher[1]['LAST_NAME'].','.$teacher[1]['FIRST_NAME'].'</option>';

            }
	}
	echo '</select></td></tr>';
	
	
	//get school terms
	$mps = get_all_school_term('FY',get_user_school_term());
	$mps = explode(',',str_replace("'",'',$mps));
	foreach($mps as $mp)
	{
		$mps_array[$mp] = $mp;
	}
	echo '<tr><td align=left>'._('Select a school term').':</TD><TD><select onchange="document.blank_gradebook.submit();" name=select_mp><option value="N/A">'._('N/A').'</option>';
	foreach($mps_array as $mp)
	{
		if(get_school_term($mp)==get_school_term($mps_array[$_POST['select_mp']]))
			echo '<option value='.$mp.' SELECTED>'.get_school_term($mp).'</option>';
		else
			echo '<option value='.$mp.'>'.get_school_term($mp).'</option>';
	}
	echo '</select></td></tr>';
	
	
	//get course sections
	$course_array = db_request(db_execute("SELECT cs.COURSE_SECTION_ID,cs.TITLE,c.TITLE AS CTITLE FROM COURSE_SECTIONS cs, COURSES c WHERE cs.TEACHER_ID ='".$_POST['select_teacher']."' AND cs.SCHOOL_ID ='".get_school_id()."' AND cs.SCHOOL_YEAR ='".get_school_year()."' AND c.COURSE_ID = cs.COURSE_ID"),array(),array('COURSE_SECTION_ID'));

        $days = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
        
	echo '<tr><td align=left>'._('Select a course section').':</TD><TD><select onchange="document.blank_gradebook.submit();" name=select_course_section_id><option value="N/A">'._('N/A').'</option>';
	foreach($course_array as $key=>$course)
	{
                foreach($days as $day)
                {
                    if(strpos($course_array[$key][1]['TITLE'],$day)!==false)
                    {
                        $course_array[$key][1]['TITLE'] = str_replace($day,_($day).' ',$course_array[$key][1]['TITLE']);
                    }
                }
                if($_POST['select_course_section_id']==$course[1]['COURSE_SECTION_ID'])
			echo '<option value='.$course[1]['COURSE_SECTION_ID'].' SELECTED>'.$course_array[$key][1]['CTITLE'].': '.$course_array[$key][1]['TITLE'].'</option>';
		else
			echo '<option value='.$course[1]['COURSE_SECTION_ID'].'>'.$course_array[$key][1]['CTITLE'].': '.$course_array[$key][1]['TITLE'].'</option>';
	}
	echo '</select></td></tr>';
	
/*
	//get classes
	if($_POST['select_course_section_id']!='N/A')
		$class_array = db_request(db_execute("SELECT DISTINCT(CFIELD_142) FROM STUDENTS WHERE STUDENT_ID IN (SELECT STUDENT_ID FROM STUDENT_SCHEDULE WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' AND COURSE_SECTION_ID='".$_POST['select_course_section_id']."' AND (END_DATE <= '".get_db_date()."' OR END_DATE IS NULL))"),array(),array('CFIELD_142'));
	echo '<tr><td align=left>'._('Select a class').':</TD><TD><select onchange="document.blank_gradebook.submit();" name=select_class><option value="All">'._('All').'</option>';
	foreach($class_array as $class)
	{
		if($_POST['select_class']==$class[1]['CFIELD_142'])
			echo '<option value='.$class[1]['CFIELD_142'].' SELECTED>'.$class[1]['CFIELD_142'].'</option>';
		else
			echo '<option value='.$class[1]['CFIELD_142'].'>'.$class[1]['CFIELD_142'].'</option>';
	}
	echo '</select></td></tr>';
*/
	if($_POST['select_course_section_id']!='N/A' && $_POST['select_course_section_id'] != '')
		echo '<TR><TD align=center colspan=2><A HREF=modloader.php?servicemod=mod_grades/BlankGradebook.php&teacher_id='.$_POST['select_teacher'].'&CREATEPDF=true&button_create=Create&select_course_section_id='.$_POST['select_course_section_id'].'&select_class='.$_POST['select_class'].' target="_blank"><B>'._('Create').'</B></A></TD></TR>';
	else
		echo '<TR><TD align=center colspan=2><A HREF=modloader.php?servicemod=mod_grades/BlankGradebook.php&teacher_id='.$_POST['select_teacher'].'&button_create=Create&select_course_section_id='.$_POST['select_course_section_id'].'&select_class='.$_POST['select_class'].' target="_blank"><B>'._('Create').'</B></A></TD></TR>';
	echo '<TR><TD align=center colspan=2><A HREF=modloader.php?servicemod=mod_grades/BlankGradebook.php><B>'._('Reset').'</B></A></TD></TR>';

	echo '</table>';
	echo '</FORM>';
	}
	echo PopTable('footer');
}
?>
