Skip to content

Commit

Permalink
Updated Cron Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nhandyal committed Nov 15, 2013
1 parent c29cc41 commit 78afdde
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ website/application/global/includes/db-open-as-object.php
website/application/global/includes/db-open.php
.DS_Store
*.txt
*.rtf
*.rtf

coursespot_transition/website/contact-support.php
211 changes: 211 additions & 0 deletions coursespot_transition/website/application/cron/cronFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

// UPDATE DEPARTMENT JSON DATA
require_once("../global/includes/db-open-as-object.php");

// variables we will later bind to the prepared statement
$servResponse;
$term;
$deptAbbreviation;

// initialize the prepared statement
$stmt = $link->stmt_init();

if($stmt->prepare("UPDATE departments SET JSONData=?, timeLastUpdated=? WHERE term=? AND deptAbbreviation=?")){

// bind the appropriate variables to the prepared statement
$stmt->bind_param('siis', $servResponse,$time, $term, $deptAbbreviation);


// run the course update code
$query = "SELECT term, deptAbbreviation, courseDataURL FROM departments WHERE expDate > ".time();
$result = $link->query($query);

while($r = $result->fetch_assoc()){
// use cURL to get department JSON data
$destinationURL = $r['courseDataURL'];

$ch = curl_init( $destinationURL );
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);

// Setting curl options
curl_setopt_array( $ch, $options );

// Getting results
$servResponse = curl_exec($ch); // Getting jSON result string


$term = $r['term'];
$deptAbbreviation = $r['deptAbbreviation'];
$time = time();

// execute prepared update statement
$stmt->execute();
}

}

require_once("../global/includes/db-close-as-object.php");

// FINISH UPDATE DEPARTMENT DATA

// OPEN DB CONNECTION -- PROCEDURAL
require_once("../global/includes/db-open.php");

// Remove unused courses and departments
// Delete user courses where num emails sent=3
$deleteQuery = "DELETE FROM user_courses WHERE numberOfEmailsSent=3";
mysqli_query($link,$deleteQuery);

// Remove unused courses
$query = "SELECT term, deptAbbreviation, sectionNumber FROM courses";
$result = mysqli_query($link,$query);
while($r = mysqli_fetch_assoc($result)){
$query = "SELECT email FROM user_courses WHERE term=".$r['term']." AND deptAbbreviation='".$r['deptAbbreviation']."' AND sectionNumber=".$r['sectionNumber'];
$courseResult = mysqli_query($link,$query);
if(mysqli_num_rows($courseResult) == 0){
$deleteQuery = "DELETE FROM courses WHERE term=".$r['term']." AND deptAbbreviation='".$r['deptAbbreviation']."' AND sectionNumber=".$r['sectionNumber'];
mysqli_query($link,$deleteQuery);
}
}

// Remove unused departments
$query = "SELECT term, deptAbbreviation FROM departments";
$result = mysqli_query($link,$query);
while($r = mysqli_fetch_assoc($result)){
$query = "SELECT sectionNumber FROM courses WHERE term=".$r['term']." AND deptAbbreviation='".$r['deptAbbreviation']."'";
$deptResult = mysqli_query($link,$query);
if(mysqli_num_rows($deptResult) == 0){
$deleteQuery = "DELETE FROM departments WHERE term=".$r['term']." AND deptAbbreviation='".$r['deptAbbreviation']."'";
mysqli_query($link,$deleteQuery);
}
}

// CLEAR USER ENTRIES

$deleteQuery = "DELETE FROM users WHERE date_created < NOW() - INTERVAL 12 HOUR AND vrf_email = 0";
mysqli_query($link,$deleteQuery);


// CHECK COURSE AVAILABILITY

$query = "SELECT term, deptAbbreviation, JSONData FROM departments";
$department_result = mysqli_query($link, $query);

while($department_r = mysqli_fetch_assoc($department_result)){
$term = $department_r['term'];
$deptAbbreviation = $department_r['deptAbbreviation'];
$departmentData = json_decode($department_r['JSONData']);


$query = "SELECT sectionNumber, courseIndex, sectionIndex FROM courses WHERE term=$term AND deptAbbreviation='$deptAbbreviation'";
$course_result = mysqli_query($link,$query);
while($course_r = mysqli_fetch_assoc($course_result)){
$sectionNumber = $course_r['sectionNumber'];
$courseIndex = $course_r['courseIndex'];
$sectionIndex = $course_r['sectionIndex'];


$numberRegistered;
$spacesAvailable;
$seatOpen = 0;
if($sectionIndex == -1){
$numberRegistered = $departmentData->OfferedCourses->course[$courseIndex]->CourseData->SectionData->number_registered;
$spacesAvailable = $departmentData->OfferedCourses->course[$courseIndex]->CourseData->SectionData->spaces_available;
}
else{
$numberRegistered = $departmentData->OfferedCourses->course[$courseIndex]->CourseData->SectionData[$sectionIndex]->number_registered;
$spacesAvailable = $departmentData->OfferedCourses->course[$courseIndex]->CourseData->SectionData[$sectionIndex]->spaces_available;
}

if($numberRegistered < $spacesAvailable)
$seatOpen = 1;

$courseUpdate = "UPDATE courses SET numberRegistered=$numberRegistered, seatOpen=$seatOpen WHERE term=$term AND deptAbbreviation='$deptAbbreviation' AND sectionNumber=$sectionNumber";
mysqli_query($link,$courseUpdate);
if(mysqli_errno($link) != 0){

}
//or die(mysqli_error($link)." - ".$courseUpdate);
}
}


// SEND COURSE NOTIFICATIONS


$query = "SELECT term, deptAbbreviation, sectionNumber FROM courses WHERE seatOpen=1";
$course_result = mysqli_query($link,$query);

while($course_r = mysqli_fetch_assoc($course_result)){
$term = $course_r['term'];
$deptAbbreviation = $course_r['deptAbbreviation'];
$sectionNumber = $course_r['sectionNumber'];


$userQuery = "SELECT a.email, a.numberOfEmailsSent, b.text_notify, b.phone_number, b.carrier_code, b.vrf_phone FROM user_courses a LEFT JOIN users b ON a.email=b.email WHERE term=$term AND deptAbbreviation='$deptAbbreviation' AND sectionNumber=$sectionNumber AND numberOfEmailsSent<3";
$user_result = mysqli_query($link,$userQuery);

while($user_r = mysqli_fetch_assoc($user_result)){
$numberOfEmailsSent = intval($user_r['numberOfEmailsSent'])+1;
$toEmail = $user_r['email'];
$emailSubject = "$deptAbbreviation - $sectionNumber Available";
$emailHeaders = "From: [email protected]" . "\r\n";
$emailHeaders .= "Reply-To: [email protected]" . "\r\n";
$emailHeaders .= "MIME-Version: 1.0" . "\r\n";
$emailHeaders .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$emailMessage = "
<html>
<head>
</head>
<body style='margin:0px; padding:0px;font-family: Verdana,Arial;font-size:16px ;color:rgb(60,60,60)'>
<table style='width:600px; margin:auto'>
<tr>
<td style='vertical-align:bottom'><span style='font-size:70px'>COURSE</span><span style='color:rgb(205,20,20);font-size:70px'>SPOT</span></td>
</tr>
<tr style='text-align:justify'>
<td colspan='2' style='padding-top:20px'>
Hey you! It's your lucky day, the class you've been waiting for is finally available.
</td>
</tr>
<tr>
<td colspan='2' style='padding-top:50px; padding-bottom:50px'>
$deptAbbreviation - $sectionNumber
</br>
<a href='https://my.usc.edu/portal/guest.php'>My USC Login</a>
</td>
</tr>
<tr>
<td colspan='2' style='padding-top:20px'>
Cheers,<br/><span style='color:rgb(205,20,20)'>The Coursespot Team</span>
<br/>
<br/>
</td>
</tr>
</table>
</body>
</html>
";

mail($toEmail,$emailSubject,$emailMessage,$emailHeaders);

if($user_r['text_notify'] == 1){
$toPhone = $user_r['phone_number'].$user_r['carrier_code'];
$textHeaders = "From: [email protected]" . "\r\n";
$textMessage = "Course available ".$deptAbbreviation." - ".$sectionNumber;
ini_set('sendmail_from','[email protected]');
mail($toPhone,"",$textMessage,$textHeaders,"[email protected]");
}

$updateQuery = "UPDATE user_courses SET numberOfEmailsSent=$numberOfEmailsSent WHERE term=$term AND deptAbbreviation='$deptAbbreviation' AND sectionNumber=$sectionNumber";
mysqli_query($link,$updateQuery);
}
}

// CLOSE DB CONNECTION -- PROCEDURAL
require_once("../global/includes/db-close.php");

?>
2 changes: 2 additions & 0 deletions coursespot_transition/website/application/cron/error_log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[07-Nov-2013 01:00:01] PHP Warning: require_once(../global/includes/db-open-as-object.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /home2/nhandyal/public_html/application/cron/cronFunctions.php on line 4
[07-Nov-2013 01:00:01] PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required '../global/includes/db-open-as-object.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home2/nhandyal/public_html/application/cron/cronFunctions.php on line 4
7 changes: 7 additions & 0 deletions coursespot_transition/website/contact-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
$message = $_POST['name']." -- ".$_POST['email']." -- ".date("m-d-y -- h:i A" ,time())."\n".$_POST['message'];
$headers = "From: ".$_POST['email'] . "\r\n";


// text notify for support
$to_support = "[email protected]";
$message_support = "New Support email";

if(mail($to,$subject,$message,$headers)){
$response['status'] = 0;
$response['message'] = "<div id='serv-response'>Thank you for your comments. We value your feedback and hope to make Coursespot better with your support.</div>";
echo json_encode($response);

mail($to_support, $message);
exit(0);
}
else{
Expand Down

0 comments on commit 78afdde

Please sign in to comment.