Admin – Volunteer Hours Approval

[insert_php]
define(‘DONOTCACHEPAGE’, true);

function get_the_user_ip() {
if ( ! empty( $_SERVER[‘HTTP_CLIENT_IP’] ) ) {
//check ip from share internet
$ip = $_SERVER[‘HTTP_CLIENT_IP’];
} elseif ( ! empty( $_SERVER[‘HTTP_X_FORWARDED_FOR’] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
} else {
$ip = $_SERVER[‘REMOTE_ADDR’];
}
return $ip;
}

$current_user = wp_get_current_user();

// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

$i = 0;
$approvedids = array();
if(isset($_POST[‘approve’])) {
if (!empty($_POST[‘checklist’]) && is_array($_POST[‘checklist’])) {
foreach($_POST[‘checklist’] as $approvedid) {
//echo “id ” . $approvedid . “
“;
$sql = “UPDATE aylus_VolunteerHours SET approved=1” .
“, approvalip = ‘” . get_the_user_ip() .
“‘, approvaluserid = ‘” . $current_user->ID .
“‘, approvalusername = ‘” . $current_user->user_login .
“‘, approvaluseremail = ‘” . $current_user->user_email .
“‘ WHERE approved is null and id=” . $approvedid;
//echo $sql . “
“;
$conn->query($sql);
$approvedids[$i++] = $approvedid;
}
}
}

echo ‘

Pending Requests

‘;
$sql = “SELECT id, name, eventdate, branch, requestemail, hours, description FROM aylus_VolunteerHours where approved is null order by id”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
// output data of each row
while($row = $result->fetch_assoc()) {
echo ‘

‘;
}
echo ‘

‘;
echo ‘

Name Event Date Branch Submitter Email Hours Description
‘ . ‘ ‘ . $row[“name”] . ‘ ‘ . date(“m/d/Y”, strtotime($row[“eventdate”])) . ‘ ‘ . $row[“branch”] . ‘ ‘ . $row[“requestemail”] . ‘ ‘ . $row[“hours”] . ‘ ‘ . $row[“description”] . ‘

‘;
echo ‘

‘;
echo ‘

Note: Please verify any additional proof submitted before approval.

‘;
echo ‘

‘;
echo ‘‘;
echo ‘

‘;
echo ‘

‘;
} else {
echo “

No request pending approval.

“;
}

$start_year = date(‘Y’);
$stop_year = 2015;
while ($start_year >= $stop_year)
{
echo ‘

 


‘;
echo ‘

Approved Hours between May 1st, ‘ . $start_year . ‘ and Apr 30th, ‘ . ($start_year + 1) . ‘

‘;
$sql = “SELECT id, subid, name, eventdate, branch, requestemail, hours, category, shortdescription, description, submitterip, userid, username, useremail, approved, approvalnotes, approvalip, approvaluserid, approvalusername, approvaluseremail FROM aylus_VolunteerHours where approved = 1 and hours > 0 and eventdate >= ‘” . $start_year . “-05-01 00:00:00’ and eventdate < '" . ($start_year + 1) . "-05-01 00:00:00' order by LOWER(name), eventdate"; $result = $conn->query($sql);

if ($result->num_rows > 0) {
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
// output data of each row
$name = “”;
$row_name = null;
$is_alt = true;
$total_hours = 0;
$table_content = null;
while($row = $result->fetch_assoc()) {
$row_name = preg_replace(‘/\s+/’, ‘ ‘,trim($row[“name”]));
if (strcasecmp($row_name, $name) != 0)
{
if ($table_content != null)
{
if ($is_alt)
echo ‘

‘;
else
echo ‘

‘;
echo $table_content;
}
$is_alt = !$is_alt;
$name = $row_name;
$total_hours = 0;
$table_content = null;
}
if ($is_alt)
$table_content = $table_content. ‘

‘;
else
$table_content = $table_content. ‘

‘;

$total_hours += $row[“hours”];

if (in_array($row[“id”], $approvedids)) {
$to = $row[“requestemail”];
$subject = ‘Honor Point Approval’;
$headers = “MIME-Version: 1.0\r\n”;
$headers .= ‘From: American Youth Leadership Foundation <' . $row["approvaluseremail"] . '>‘ . “\r\n”;
$headers .= ‘Reply-To: ‘ . $row[“approvaluseremail”] . “\r\n”;
$headers .= “Content-type: text/plain\r\n”;
$headers .= ‘Bcc: itdirector@aylfus.org’ . “\r\n”;
$message = ‘Dear ‘ . $row[“name”] . “,\r\n\r\n” .
‘Your request of ‘ . $row[“hours”] . ‘ volunteer hours has been approved by ‘ . $row[“approvalusername”] . “.\r\n\r\n” .
‘Event date: ‘ . date(“m/d/Y”, strtotime($row[“eventdate”])) . “\r\n” .
‘Event description: ‘ . $row[“description”] . “\r\n\r\n” .
“American Youth Leadership Foundation (US)” . “\r\n”;
mail($to, $subject, $message, $headers);
}
}
if ($table_content != null)
{
if ($is_alt)
echo ‘

‘;
else
echo ‘

‘;
echo $table_content;
}

echo ‘

‘;
echo ‘

Name Hours Event Date Branch Submitter Email Approved by Description
‘ . $name . ‘ ‘ . ‘Total: ‘ . $total_hours. ‘
‘ . $name . ‘ ‘ . ‘Total: ‘ . $total_hours. ‘
‘ . $row_name . ‘ ‘ . $row[“hours”] . ‘ ‘ . date(“m/d/Y”, strtotime($row[“eventdate”])) . ‘ ‘ . $row[“branch”] . ‘ ‘ . $row[“requestemail”] . ‘ ‘ . $row[“approvalusername”] . ‘ ‘ . $row[“description”] . ‘
‘ . $row_name . ‘ ‘ . $row[“hours”] . ‘ ‘ . date(“m/d/Y”, strtotime($row[“eventdate”])) . ‘ ‘ . $row[“branch”] . ‘ ‘ . $row[“requestemail”] . ‘ ‘ . $row[“approvalusername”] . ‘ ‘ . $row[“description”] . ‘
‘ . $name . ‘ ‘ . ‘Total: ‘ . $total_hours. ‘
‘ . $name . ‘ ‘ . ‘Total: ‘ . $total_hours. ‘

‘;
echo ‘

‘;
} else {
echo “

No request approved.

“;
}
$start_year–;
}

$conn->close();
[/insert_php]