Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpline volunteers beta #12

Merged
merged 4 commits into from
Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ You can also supply a list of volunteers in a BMLT server.
static $bmlt_password = "";
```

Some additional details on this:

- If you specify only one day of a volunteer shift, it will be assumed re-occuring
- You can visualize the shift schedule by going to http://example.com/yap/schedule.html (for example)

## Contribute

Fork this repo and send a pull request. For testing locally, run the yap server and then use `ngrok`. Grab the ngrok hostname that is generated and then use that in your Twilio settings.
Expand Down
91 changes: 91 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@
# BMLT uses weird date formatting, Sunday is 1. PHP uses 0 based Sunday.
static $days_of_the_week = [1 => "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

class VolunteerInfo {
public $title;
public $start;
public $end;
public $weekday_id;
public $weekday;
public $origin_duration;
public $origin_start_time;
}

class Coordinates {
public $location;
public $latitude;
public $longitude;
}

class DurationInterval {
public $hours;
public $minutes;
public $seconds;

public function getDurationFormat() {
return $this->hours . " hours " . $this->minutes . " minutes " . $this->seconds . " seconds";
}
}

function getCoordinatesForAddress($address) {
$coordinates = new Coordinates();

Expand Down Expand Up @@ -121,6 +141,76 @@ function getHelplineVolunteer($service_body_int, $format_id, $tracker) {
return explode("#@-@#", $volunteers[$tracker]->contact_phone_1)[2];
}

function getHelplineSchedule($service_body_int) {
$finalSchedule = [];
$volunteerNames = [];
auth_bmlt();
$bmlt_search_endpoint = getHelplineBMLTRootServer() . '/client_interface/json/?switcher=GetSearchResults&services='.$service_body_int.'&formats='.getFormat('HV').'&advanced_published=0';
$volunteers = json_decode(get($bmlt_search_endpoint));
for ($v = 0; $v < count($volunteers); $v++) {
$volunteerInfo = new VolunteerInfo();
$volunteerInfo->title = $volunteers[$v]->meeting_name;
$volunteerInfo->start = getNextMeetingInstance($volunteers[$v]->weekday_tinyint, $volunteers[$v]->start_time)->format("Y-m-d H:i:s");
$volunteerInfo->origin_duration = getDurationInterval($volunteers[$v]->duration_time);
$volunteerInfo->end = date_add(new DateTime($volunteerInfo->start), date_interval_create_from_date_string($volunteerInfo->origin_duration->getDurationFormat()))->format("Y-m-d H:i:s");
$volunteerInfo->weekday_id = intval($volunteers[$v]->weekday_tinyint);
$volunteerInfo->weekday = $GLOBALS['days_of_the_week'][$volunteers[$v]->weekday_tinyint];
$volunteerInfo->origin_start_time = $volunteers[$v]->start_time;
array_push($volunteerNames, $volunteers[$v]->meeting_name);
array_push($finalSchedule, $volunteerInfo);
}

$volunteerShiftCounts = array_count_values($volunteerNames);

// Get the volunteer names
foreach (array_keys($volunteerShiftCounts) as $volunteerName) {
// Ensure that they are only listed once
if ($volunteerShiftCounts[$volunteerName] == 1) {
// Get the information for their shift
foreach ($finalSchedule as $volunteerInfoItem) {
if ($volunteerName == $volunteerInfoItem->title) {
// Figure out what day is represented already
$dayRepresented = $volunteerInfoItem->weekday_id;

for ($d = 1; $d <= 7; $d++) {
// Loop through days, don't read the day that is already represented
if ($dayRepresented !== $d) {
$volunteerClone = clone $volunteerInfoItem;
$volunteerClone->start = getNextMeetingInstance($d, $volunteerInfoItem->origin_start_time)->format("Y-m-d H:i:s");
$volunteerClone->end = date_add(new DateTime($volunteerClone->start), date_interval_create_from_date_string($volunteerClone->origin_duration->getDurationFormat()))->format("Y-m-d H:i:s");
$volunteerClone->weekday = $GLOBALS['days_of_the_week'][$d];
$volunteerClone->weekday_id = $d;
array_push($finalSchedule, $volunteerClone);
}
}
}
}
}
}

return json_encode($finalSchedule);
}

function countOccurences($initialSchedule, $volunteerName) {
$occurences = 0;
for ($v = 0; $v <= count($initialSchedule); $v++) {
if ($initialSchedule[$v]["title"] == $volunteerName) {
$occurences++;
}
}

return $occurences > 1;
}

function getDurationInterval($duration_time) {
$durationArray = explode(":", $duration_time);
$durationInterval = new DurationInterval();
$durationInterval->hours = $durationArray[0];
$durationInterval->minutes = $durationArray[1];
$durationInterval->seconds = $durationArray[2];
return $durationInterval;
}

function getHelplineBMLTRootServer() {
return isset($GLOBALS['helpline_bmlt_root_server']) ? $GLOBALS['helpline_bmlt_root_server'] : $GLOBALS['bmlt_root_server'];
}
Expand All @@ -139,6 +229,7 @@ function auth_bmlt() {
}

function get($url) {
error_log($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
Expand Down
4 changes: 4 additions & 0 deletions helpline-schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
include 'functions.php';
header("content-type: application/json");
echo getHelplineSchedule($_REQUEST["service_body_id"]);
21 changes: 21 additions & 0 deletions schedule.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.6.0/fullcalendar.css' />
<script src='http://code.jquery.com/jquery-1.11.3.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.6.0/fullcalendar.min.js'></script>
</head>
<body>
<div id='calendar'></div>
<script type="text/javascript">
$(document).ready(function() {
$('#calendar').fullCalendar({
allDaySlot: false,
defaultView: 'agendaWeek',
events : "/helpline-schedule.php?service_body_id=2"
});
});
</script>
</body>
</html>