<?php

//if (Preferences('LANGUAGE') == 'ru_RU')
//{
//	$item = 'Dubl Progress Periods';
//}else{
	$item = _('Progress Periods');
//}

$years_array = db_request(db_execute("SELECT SCHOOL_TERM_ID,SCHOOL_YEAR FROM SCHOOL_YEARS WHERE SCHOOL_ID='".get_school_id()."'"),array(),array('SCHOOL_YEAR'));
$sems_array = db_request(db_execute("SELECT SCHOOL_TERM_ID,SCHOOL_YEAR FROM SCHOOL_SEMESTERS WHERE SCHOOL_ID='".get_school_id()."'"),array(),array('SCHOOL_YEAR'));
$qtrs_array = db_request(db_execute("SELECT SCHOOL_TERM_ID,SCHOOL_YEAR FROM SCHOOL_QUARTERS WHERE SCHOOL_ID='".get_school_id()."'"),array(),array('SCHOOL_YEAR'));
$pros_array = db_request(db_execute("SELECT SCHOOL_TERM_ID,SCHOOL_YEAR FROM SCHOOL_WEEKS WHERE SCHOOL_ID='".get_school_id()."'"),array(),array('SCHOOL_YEAR'));

$table_list = '<TABLE><TR><TD valign=top><B>'._('duplicate these schools terms:').'</B><BR>';
$table_list .= '<INPUT type=checkbox CHECKED DISABLED>'._('Year').'<BR><INPUT type=checkbox name=mps[SCHOOL_SEMESTERS] CHECKED>'._('Semesters').'<BR><INPUT type=checkbox name=mps[SCHOOL_QUARTERS] CHECKED>'._('Quarters').'<BR><INPUT type=checkbox name=mps[SCHOOL_WEEKS]>'.$item.'<BR>';
$table_list .= '</TD><TD width=30 valign=top><BR><BR><IMG SRC=images/arrow_right.gif height=40></TD><TD valign=top><B>'._('to these school years:').'</B>';
for($i=get_school_year()-12;$i<get_school_year();$i++)
{
	if($years_array[$i])
	{
		$table_list .= '<BR><INPUT type=checkbox CHECKED DISABLED><font color=gray>'.$i.((SystemPreferences('YEAR_TITLE')=='YYYY-YYYY')?'-'.($i+1):'');
		$table_list .= ' ('.$years_array[$i].' '._('year').', '.$sems_array[$i].' '._('semesters').', '.$qtrs_array[$i].' '._('quarters').', '.$pros_array[$i].' '._('progress periods').')</font>';
	}
	else
		$table_list .= '<BR><INPUT type=checkbox name=years['.$i.'] value=Y>'.$i.((SystemPreferences('YEAR_TITLE')=='YYYY-YYYY')?'-'.($i+1):'');
}
$table_list .= '</TD></TR></TABLE>';


DisplayHeader(".:".ProgramTitle().":.");

if(Prompt(_('School Terms Confirmation'),'',$table_list))
{
	$_SYSTEM['DisableAddonLoader'] = true;
	if ($_REQUEST['years']) {
		foreach($_REQUEST['years'] as $school_year=>$y)
		{
			db_execute("UPDATE SCHOOL_YEARS SET ROLLOVER_ID=SCHOOL_TERM_ID WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
			db_execute("INSERT INTO SCHOOL_YEARS (SCHOOL_TERM_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT ".db_seq_nextval('SCHOOL_TERM_SEQ').",'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_YEARS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
		
			if($_REQUEST['mps']['SCHOOL_SEMESTERS'])
			{
				db_execute("UPDATE SCHOOL_SEMESTERS SET ROLLOVER_ID=SCHOOL_TERM_ID WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
				if($DBType == 'mssql') //added the TOP 1, because we had an error with this query
				  db_execute("INSERT INTO SCHOOL_SEMESTERS (YEAR_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT (".db_limit("SELECT SCHOOL_TERM_ID FROM SCHOOL_YEARS y WHERE y.SCHOOL_YEAR='".$school_year."' AND y.ROLLOVER_ID=s.YEAR_ID", 1)."),'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_SEMESTERS s WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'", true, false);
				else
				  db_execute("INSERT INTO SCHOOL_SEMESTERS (SCHOOL_TERM_ID,YEAR_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT ".db_seq_nextval('SCHOOL_TERM_SEQ').",(".db_limit("SELECT SCHOOL_TERM_ID FROM SCHOOL_YEARS y WHERE y.SCHOOL_YEAR='".$school_year."' AND y.ROLLOVER_ID=s.YEAR_ID", 1)."),'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_SEMESTERS s WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
			
				if($_REQUEST['mps']['SCHOOL_QUARTERS'])
				{
					db_execute("UPDATE SCHOOL_QUARTERS SET ROLLOVER_ID=SCHOOL_TERM_ID WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
				    if($DBType == 'mssql') //added the TOP 1, because we had an error with this query
					  db_execute("INSERT INTO SCHOOL_QUARTERS (SEMESTER_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT (".db_limit("SELECT SCHOOL_TERM_ID FROM SCHOOL_SEMESTERS s WHERE s.SCHOOL_YEAR='".$school_year."' AND s.ROLLOVER_ID=q.SEMESTER_ID", 1)."),'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_QUARTERS q WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'", true, false);
					else
					  db_execute("INSERT INTO SCHOOL_QUARTERS (SCHOOL_TERM_ID,SEMESTER_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT ".db_seq_nextval('SCHOOL_TERM_SEQ').",(".db_limit("SELECT SCHOOL_TERM_ID FROM SCHOOL_SEMESTERS s WHERE s.SCHOOL_YEAR='".$school_year."' AND s.ROLLOVER_ID=q.SEMESTER_ID", 1)."),'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_QUARTERS q WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
			
					if($_REQUEST['mps']['SCHOOL_WEEKS'])
				        if($DBType == 'mssql')
						  db_execute("INSERT INTO SCHOOL_WEEKS (QUARTER_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT (".db_limit("SELECT SCHOOL_TERM_ID FROM SCHOOL_QUARTERS q WHERE q.SCHOOL_YEAR='".$school_year."' AND q.ROLLOVER_ID=p.QUARTER_ID", 1)."),'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_WEEKS p WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'", true, false);
						else
						  db_execute("INSERT INTO SCHOOL_WEEKS (SCHOOL_TERM_ID,QUARTER_ID,SCHOOL_YEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT ".db_seq_nextval('SCHOOL_TERM_SEQ').",(".db_limit("SELECT SCHOOL_TERM_ID FROM SCHOOL_QUARTERS q WHERE q.SCHOOL_YEAR='".$school_year."' AND q.ROLLOVER_ID=p.QUARTER_ID", 1)."),'".$school_year."',SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID FROM SCHOOL_WEEKS p WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");
				}
			}
		}
	}
	echo '<FORM action=modloader.php?servicemod='.$_REQUEST['servicemod'].' method=POST>';
	DisplayHeader('<IMG SRC=images/check.gif> '._('The data have been successfully rolled.'),'<INPUT type=submit value='._('OK').'>');
	echo '</FORM>';
	unset($_SESSION['_REQUEST_vars']['tables']);
	unset($_SESSION['_REQUEST_vars']['delete_ok']);
}


?>