Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/cceadmin/
Upload File :
Current File : /var/www/cesa.co.za/cceadmin/importmod.php

<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg7.php" ?>
<?php include "ewmysql7.php" ?>
<?php include "phpfn7.php" ?>
<?php include "CCEMessagesinfo.php" ?>
<?php include "CCEModulesinfo.php" ?>
<?php include "CCEUsersinfo.php" ?>
<?php include "userfn7.php" ?>
<?php
if (!isset($_SESSION[EW_SESSION_STATUS]) || ($_SESSION[EW_SESSION_STATUS] != "login")) {
	header("Location: /cceadmin/login.php");
	die();
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php include "header.php" ?>
<h1> Import Moderation</h1>
<?php
$conn = mysql_connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS);
mysql_select_db(EW_CONN_DB);

if (isset($_POST["go"]) && ($_POST["go"]==1)) {

	include_once("Excel/reader.php");
	include_once("Excel/OLERead.php");
	
	$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/cceadmin/moderations/';
	
	$uploadfilename1="";
	$uploadfile1="";
	if (isset($_FILES['file1'])) {
		$uploadfilename1 = strtolower(str_replace(array("'","\"","(",")","#"," "), "_", basename(stripslashes($_FILES['file1']['name']))));
		$uploadfile1 = $uploaddir . $uploadfilename1;
		$ifilenum=1;
		while (file_exists($uploadfile1) && !empty($uploadfilename1)) {
			$uploadfile1 = $uploaddir . $ifilenum."_".$uploadfilename1;
			$ifilenum++;
		}
		move_uploaded_file($_FILES['file1']['tmp_name'], $uploadfile1);
		
		$sql = "UPDATE CCEAssignments SET ";
		if ($_POST["asstype"] == "f") {
			$sql .= "ModeratedFile";
		} elseif ($_POST["asstype"] == "r") {
			$sql .= "ModeratedFile2";
		} else {
			$sql .= "ModeratedFile3";
		}
		$sql .= "='".str_replace($uploaddir, "", $uploadfile1)."' WHERE CCEAssignmentID=".$_POST["assid"];
		mysql_query($sql);

		$excel = new Spreadsheet_Excel_Reader();
		
		// read spreadsheet data
		$excel->read($uploadfile1);

		echo "Spreadsheet read<br>";

		$k=5;

		$sql="SELECT * FROM CCERulingCodes";
		$rec=mysql_query($sql);
		$arrRulingCodes=array();
		while ($row=mysql_fetch_assoc($rec)) {
			$arrRulingCodes[$row["RulingCode"]]=$row["Ruling"];
		}
		//var_dump($excel);
		while ($k<=$excel->sheets[0]['numRows']) {
//			echo "<pre>";
//			var_dump($excel->sheets[0]['cells'][$k]);
//			echo "</pre>";
			$Surname = addslashes(trim($excel->sheets[0]['cells'][$k][3]));
			//$Status = strtoupper(trim($excel->sheets[0]['cells'][$k][11]));
			$Remarks = str_replace("\\0","",addslashes(trim($excel->sheets[0]['cells'][$k][11])));
			$Ruling = strtoupper(trim($excel->sheets[0]['cells'][$k][8]));
			
			if (in_array($Ruling, array("E","A","S","NE","R"))) {

				$sSubNo=trim($excel->sheets[0]['cells'][$k][4]);
				$sOnTime=strtoupper(trim($excel->sheets[0]['cells'][$k][6]));
				$sOneWeekLate=strtoupper(trim($excel->sheets[0]['cells'][$k][7]));
				$sLang=strtoupper(trim($excel->sheets[0]['cells'][$k][9]));
				$sEffort=strtoupper(trim($excel->sheets[0]['cells'][$k][10]));
				
				$Status="A";
				if ($Ruling=="R") $Status="N";
				
				$iScore=0;
				$sScoreBreakdown="";
				if (($Status=="A") && ($Ruling!="NE")) {
					// Get previous ruling
					if ($_POST["asstype"] == "r") {
						$RulingPrev="R";
						$sql="SELECT CCEStudentAssignments.RulingCode FROM CCEStudents JOIN CCEStudentAssignments USING (CCEStudentID)
						WHERE UPPER(CCEStudents.Surname)='".strtoupper($Surname)."' AND CCEStudentAssignments.CCEAssignmentID=".$_POST["assid"]."
						AND CCEStudentAssignments.Passed>0 ORDER BY CCEStudentAssigID DESC LIMIT 0,1";
						$recPrev=mysql_query($sql);
						if ($rowPrev=mysql_fetch_assoc($recPrev)) $RulingPrev=$rowPrev["RulingCode"];
					}
					// Achieved
					/*
					if ($sSubNo==1) {
						// First submission
						$iScore=5.5;
						if ($Ruling=="E") $iScore+=2;
						if ($Ruling=="NE") {
							// Needs enhancement
							$iScore-=1;
						}
						if ($sLang=="YES") $iScore++;
						if ($sEffort=="YES") $iScore+=0.5;
						if ($sOnTime=="YES") $iScore++;
	
						if ($iScore>10) $iScore=10;
					} else {
						// Second submission
						$iScore=6;
						if ($Ruling=="E") $iScore+=1;
						if ($Ruling=="NE") $iScore-=0.5;
						if ($sOnTime=="NO") $iScore--;
						if ($iScore>7) $iScore=7;
					}
	
					if (($sOneWeekLate=="YES") && ($iScore>4)) $iScore=4;
					if ($iScore<0) $iScore=0;
					*/
					if ($sOnTime=="YES") $iScore++;
					if ($sLang=="YES") $iScore++;
					if ($sEffort=="YES") $iScore+=0.5;
					$iScore+=5.5;
					if ($RulingPrev == "R") $iScore-=3;
					
					if ($Ruling=="E") $iScore+=2;
					
					if ($RulingPrev=="NE") $iScore-=1;
					if ($sOneWeekLate=="YES") $iScore-=3;
					
					$sScoreBreakdown="Score breakdown:"."\r\n"."Content and Understanding: 5.5 points"."\r\n";
					
					$sScoreBreakdown.="Submitted on time: ".$sOnTime.", +".($sOnTime=="YES"?"1 point":"0 points")."\r\n";
					if ($sOneWeekLate=="YES") $sScoreBreakdown.="Submitted more than 1 week late: YES, -3 points"."\r\n";
					$sScoreBreakdown.="Good Language / Presentation: ".$sLang.", +".($sLang=="YES"?"1 point":"0 points")."\r\n";
					$sScoreBreakdown.="Good Effort / Originality / Innovation: ".$sEffort.", +".($sEffort=="YES"?"0.5 points":"0 points")."\r\n";
					
					if ($Ruling=="E") $sScoreBreakdown.="Excellent work submitted: +2 points"."\r\n";
					if ($RulingPrev=="NE") $sScoreBreakdown.="Needed Enhancement: -1 point"."\r\n";
					if ($RulingPrev == "R") $sScoreBreakdown.="Resubmission: -3 points"."\r\n";
					$sScoreBreakdown.="TOTAL SCORE: ".$iScore."\r\n"."\r\n";
					
				}
				
				/*
				$sL=1;
				$sD=trim($excel->sheets[0]['cells'][$k][4]);
				$sE=strtoupper(trim($excel->sheets[0]['cells'][$k][5]));
				if ($sD==2) $sL=0;
				$sT=1;
				if ($Ruling=="R") $sT=0;
				$sM=-1;
				if ($sD==1) $sM=1;
				$sN=-1;
				if ($sE=="YES") $sN=0;
				$sO=0;
				if ($Status=="A") $sO=5;
				$sP=0;
				if ($Ruling=="E") $sP=2;
				$sQ=0;
				if ($Ruling=="SNE") $sQ=-1;
				$sR=0;
				if ($Ruling=="NE") $sR=-1;
				$sS=0;
				if ($sRuling=="R") $sS=-2;
				$sU=0;
				if (strtoupper(trim($excel->sheets[0]['cells'][$k][8]))=="YES") $sU=1;
				$sV=0;
				if (strtoupper(trim($excel->sheets[0]['cells'][$k][9]))=="YES") $sV=1;
				$sW=0;
				if ($sD==2) $sW=1;
				$sX=0;
				if (strtoupper(trim($excel->sheets[0]['cells'][$k][9]))=="NO") $sX=0.5;
				$sY=0;
				if ($Status=="A") $sY=5;
				$sZ=-1;
				if ($sE=="YES") $sZ=0;
				$sAA=0;
				if ($Ruling=="NE") $sAA=-0.5;
				
				$iScore=$sL*$sT*($sM+$sN+$sO+$sP+$sQ+$sR+$sS+$sU+$sV+$sX)+$sW*($sY+$sZ+$sAA);
				if ($iScore<0) $iScore=0;
				if (($_POST["asstype"]!="f") && ($iScore>7)) $iScore=7;
				if ((strtoupper(trim($excel->sheets[0]['cells'][$k][6]))=="YES") && ($iScore>4)) $iScore=4;
				*/
				if (!empty($Surname) && !empty($Status) && !empty($Remarks) && !empty($Ruling) && (($Status=="A") || ($Status=="N"))) {
					echo $Surname." - ".$Status." - ".$Ruling." - ".$iScore."<br>";
	/*				$sResult = "Result: ";
					if ($Status=="A") {
						$sResult.="Achieved";
					} else {
						$sResult.="Not Achieved";
					}
	*/				
					if (isset($arrRulingCodes[$Ruling])) $sResult="Result: ".$arrRulingCodes[$Ruling].(empty($iScore)?"":", Score: ".$iScore)."."."\r\n"."\r\n";
					$Remarks=$sResult.$sScoreBreakdown.$Remarks;
					$sql = "INSERT INTO CCEModerationImport (CCEAssignmentID, Surname, StatusCode, Remarks, RulingCode, Score) 
					VALUES (".$_POST["assid"].", '".$Surname."', '".$Status."', '".$Remarks."', '".$Ruling."', ".$iScore.")";
//					echo $sql;
					mysql_query($sql);
	//				echo $sql."<br>";
				}
			}
			$k++;
		}
		mysql_query("CALL ImportModeration");
		echo "Import finished.<br><a href='importmod.php'>Click here to import another spreadsheet</a>";

		$sql="SELECT CCEStudents.*, CCEStudentAssignments.CCEStudentAssigID, CCEStudentAssignments.LastFeedback FROM CCEStudents inner join CCEStudentAssignments using (CCEStudentID) WHERE CCEAssignmentID=".$_POST["assid"]." AND LastFeedback IS NOT NULL AND LastFeedback<>''";
		$rec=mysql_query($sql);
?>
<p>
<b>Send <?php echo mysql_num_rows($rec); ?> feedback messages:</b>
</p>
<form method="post" action="importmod.php">
<input type="hidden" name="go" value="2">
<input type="hidden" name="assid" value="<?php echo $_POST["assid"]; ?>">
<table border="0">
<tr><td><strong>Name</strong></td><td><strong>Message</strong></td><td><strong>Send</strong></td></tr>
<?php
		while ($row=mysql_fetch_assoc($rec)) {
?>
<tr><td><?php echo $row["FirstName"]." ".$row["Surname"]; ?></td><td><?php echo $row["LastFeedback"]; ?></td><td><input type="checkbox" name="StudentIDs[]" value="<?php echo $row["CCEStudentAssigID"]; ?>" checked="checked"></td></tr>
<?php		
		}
?>		
</table>
<input type="submit" value="Send Now">
</form>
<?php
	}
} elseif (isset($_POST["go"]) && ($_POST["go"]==2)) {
	$arrStudents=$_POST["StudentIDs"];
	$iNumFeedbackSent=0;
	for ($iArrStud=0; $iArrStud<count($arrStudents); $iArrStud++) {
		$sql="SELECT CCEStudents.CCEFacilitatorID, CCEStudentAssignments.* FROM CCEStudents inner join CCEStudentAssignments using (CCEStudentID) WHERE CCEStudentAssigID =".$arrStudents[$iArrStud]." AND LastFeedback IS NOT NULL AND LastFeedback<>''";
		$recCSA=mysql_query($sql, $conn);
		if ($rowCSA=mysql_fetch_assoc($recCSA)) {
		
			$iNumFeedbackSent++;
		
			$sSendTo="F".$rowCSA["CCEFacilitatorID"].",S".$rowCSA['CCEStudentID'].",M".$rowCSA['CCEStudentID'];
	
	//		echo 'SendAssignmentFeedback('.$_POST['assid'].', '.$rowCSA['CCEStudentID'].', '.$sSendTo.', '.$rowCSA['LastFeedback'].', "")<br>';
			$iMessageID = SendAssignmentFeedback($_POST['assid'], $rowCSA['CCEStudentID'], $sSendTo, $rowCSA['LastFeedback'], "");
			
			echo "Message ID: ".$iMessageID."<br>";
			
			mysql_query("INSERT INTO CCEAssignmentFeedback (CCEStudentAssigID, CCEMessageID) VALUES ('".$rowCSA["CCEStudentAssigID"]."', '".$iMessageID."')");       
			mysql_query("UPDATE CCEStudentAssignments SET LFSentTo='', LastFeedback='' WHERE CCEStudentAssigID='".$rowCSA["CCEStudentAssigID"]."'");
			
		}
	}
	echo "<p><b>".$iNumFeedbackSent." messages sent.</b></p><br><a href='importmod.php'>Click here to import another spreadsheet</a>";
} else {
?>    
    <form action="importmod.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <input type="hidden" name="go" value="1" />
      <table border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td>Assignment:</td>
          <td><label>
            <select name="assid" id="assid">
<?php
	$sql = "SELECT * FROM CCEAssignments WHERE Archived=0
	ORDER BY CCEAssignments.RefNum";
	$recMod=mysql_query($sql);
	while ($rowMod=mysql_fetch_assoc($recMod)) {
?>            
			<option value="<?php echo $rowMod["CCEAssignmentID"]; ?>"<?php if (isset($_GET["id"]) && ($_GET["id"]==$rowMod["CCEAssignmentID"])) echo " selected"; ?>><?php echo $rowMod["RefNum"].": ".$rowMod["AssignmentName"]; ?></option>
<?php
	}
?>              
			</select>
          </label></td>
        </tr>
        <tr>
          <td>Type:</td>
          <td><label>
            <input name="asstype" type="radio" id="asstype" value="f" checked="checked" />
            First Evaluation<br />
            <input type="radio" name="asstype" id="asstype2" value="r" /> 
            Resubmissions
            <br>
            <input type="radio" name="asstype" id="asstype3" value="l" />
Late submissions         </label></td>
        </tr>
        <tr>
          <td>Spreadsheet:</td>
          <td><label>
          <input type="file" name="file1" id="file1" />
          </label></td>
        </tr>
      </table>
      <p>
        <label>
        <input type="submit" name="button" id="button" value="Import Now" />
        </label>
      </p>
    </form>
<?php
}
?>    
    <p>&nbsp;</p>

<?php include "footer.php" ?>