Skip to content

Commit

Permalink
Merge pull request #598 from TheRestartProject/RES-1633_unapproved_ev…
Browse files Browse the repository at this point in the history
…ents

RES-1633 unapproved events
  • Loading branch information
edwh authored Feb 20, 2023
2 parents d259b18 + 1a7e9d2 commit c8ec5a5
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 78 deletions.
20 changes: 19 additions & 1 deletion app/Http/Controllers/CalendarEventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,16 @@ public function exportCalendar($events)
$ical[] = 'PRODID:-//Restarters//NONSGML Events Calendar/EN';

// loop over events
$me = auth()->user();

foreach ($events as $event) {
// We need to filter by approval status. If the event is not approved, we can only see it if we are
// an admin, network coordinator, or the host of the event.

if (!User::userCanSeeEvent($me, $event)) {
continue;
}

if (! is_null($event->event_start_utc) ) {
$ical[] = 'BEGIN:VEVENT';

Expand All @@ -119,7 +128,16 @@ public function exportCalendar($events)
$ical[] = 'DESCRIPTION:'.url('/party/view').'/'.$event->idevents;
$ical[] = "LOCATION:{$event->location}";
$ical[] = 'URL:'.url('/party/view').'/'.$event->idevents;
$ical[] = 'STATUS:CONFIRMED';

if ($event->cancelled) {
$ical[] = 'STATUS:CANCELLED';
} else if ($event->approved && $event->theGroup->approved) {
// Events are only confirmed once the event and the group are approved.
$ical[] = 'STATUS:CONFIRMED';
} else {
$ical[] = 'STATUS:TENTATIVE';
}

$ical[] = 'END:VEVENT';
}
}
Expand Down
125 changes: 74 additions & 51 deletions app/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
use App\Helpers\SearchHelper;
use App\Party;
use App\Search;
use App\User;
use App\UserGroups;
use Auth;
use Carbon\Carbon;
use DateTime;
use DB;
use Illuminate\Http\Request;
use Response;
use Illuminate\Database\Eloquent\Collection;

class ExportController extends Controller
{
Expand Down Expand Up @@ -71,6 +73,8 @@ public function devices(Request $request, $idevents = NULL, $idgroups = NULL)
$filename .= '.csv';
$file = fopen(base_path() . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . $filename, 'w+');

$me = auth()->user();

// Do not include model column
if ($host == 'therestartproject.org') {
$columns = [
Expand All @@ -90,32 +94,35 @@ public function devices(Request $request, $idevents = NULL, $idgroups = NULL)
fputcsv($file, $columns);

foreach ($all_devices as $device) {
$wasteImpact = 0;
$co2Diverted = 0;

if ($device->isFixed()) {
if ($device->deviceCategory->powered) {
$wasteImpact = $device->eWasteDiverted();
$co2Diverted = $device->eCo2Diverted($eEmissionRatio, $displacementFactor);
} else {
$wasteImpact = $device->uWasteDiverted();
$co2Diverted = $device->uCo2Diverted($uEmissionratio, $displacementFactor);
set_time_limit(60);
if (User::userCanSeeEvent($me, $event)) {
$wasteImpact = 0;
$co2Diverted = 0;

if ($device->isFixed()) {
if ($device->deviceCategory->powered) {
$wasteImpact = $device->eWasteDiverted();
$co2Diverted = $device->eCo2Diverted($eEmissionRatio, $displacementFactor);
} else {
$wasteImpact = $device->uWasteDiverted();
$co2Diverted = $device->uCo2Diverted($uEmissionratio, $displacementFactor);
}
}
}

fputcsv($file, [
$device->item_type,
$device->deviceCategory->name,
$device->brand,
$device->problem,
$device->getRepairStatus(),
$device->getSpareParts(),
$device->deviceEvent->getEventName(),
$device->deviceEvent->theGroup->name,
$device->deviceEvent->getFormattedLocalStart('Y-m-d'),
$wasteImpact,
$co2Diverted
]);
fputcsv($file, [
$device->item_type,
$device->deviceCategory->name,
$device->brand,
$device->problem,
$device->getRepairStatus(),
$device->getSpareParts(),
$device->deviceEvent->getEventName(),
$device->deviceEvent->theGroup->name,
$device->deviceEvent->getFormattedLocalStart('Y-m-d'),
$wasteImpact,
$co2Diverted
]);
}
}
} else {
$columns = [
Expand All @@ -134,35 +141,44 @@ public function devices(Request $request, $idevents = NULL, $idgroups = NULL)
];

fputcsv($file, $columns);
$party = null;

foreach ($all_devices as $device) {
$wasteImpact = 0;
$co2Diverted = 0;

if ($device->isFixed()) {
if ($device->deviceCategory->powered) {
$wasteImpact = $device->eWasteDiverted();
$co2Diverted = $device->eCo2Diverted($eEmissionRatio, $displacementFactor);
} else {
$wasteImpact = $device->uWasteDiverted();
$co2Diverted = $device->uCo2Diverted($uEmissionratio, $displacementFactor);
set_time_limit(60);
$party = !$party || $party->idevents != $device->event ? Party::findOrFail($device->event) : $party;

if (User::userCanSeeEvent($me, $party)) {
$wasteImpact = 0;
$co2Diverted = 0;

if ($device->isFixed())
{
if ($device->deviceCategory->powered)
{
$wasteImpact = $device->eWasteDiverted();
$co2Diverted = $device->eCo2Diverted($eEmissionRatio, $displacementFactor);
} else
{
$wasteImpact = $device->uWasteDiverted();
$co2Diverted = $device->uCo2Diverted($uEmissionratio, $displacementFactor);
}
}
}

fputcsv($file, [
$device->item_type,
$device->deviceCategory->name,
$device->brand,
$device->model,
$device->problem,
$device->getRepairStatus(),
$device->getSpareParts(),
$device->deviceEvent->getEventName(),
$device->deviceEvent->theGroup->name,
$device->deviceEvent->getFormattedLocalStart('Y-m-d'),
$wasteImpact,
$co2Diverted,
]);
fputcsv($file, [
$device->item_type,
$device->deviceCategory->name,
$device->brand,
$device->model,
$device->problem,
$device->getRepairStatus(),
$device->getSpareParts(),
$device->deviceEvent->getEventName(),
$device->deviceEvent->theGroup->name,
$device->deviceEvent->getFormattedLocalStart('Y-m-d'),
$wasteImpact,
$co2Diverted,
]);
}
}
}

Expand Down Expand Up @@ -205,7 +221,7 @@ public function parties(Request $request)
});
$k = implode(' ', $key);
});
$headers = array_merge(['Date', 'Venue', 'Group'], $statsKeys);
$headers = array_merge(['Date', 'Venue', 'Group', 'Approved'], $statsKeys);

// Send these to getEventStats() to speed things up a bit.
$eEmissionRatio = \App\Helpers\LcaStats::getEmissionRatioPowered();
Expand All @@ -223,6 +239,7 @@ public function parties(Request $request)
$party->getFormattedLocalStart(),
$party->getEventName(),
$party->theGroup && $party->theGroup->name ? $party->theGroup->name : '?',
$party->approved ? 'true' : 'false',
];
$PartyArray[$i] += $stats;
}
Expand Down Expand Up @@ -260,8 +277,14 @@ public function getTimeVolunteered(Request $request, $search = null, $export = f
$all_group_tags = GroupTags::all();

//Get all applicable groups
if (Fixometer::hasRole($user, 'Administrator')) {
if (Fixometer::hasRole($user, 'Administrator'))
{
$all_groups = Group::all();
} else if (Fixometer::hasRole($user, 'NetworkCoordinator')) {
$all_groups = new Collection();
foreach ($user->networks as $network) {
$all_groups->merge($network->groups);
}
} elseif (Fixometer::hasRole($user, 'Host')) {
$host_groups = UserGroups::where('user', $user->id)->where('role', 3)->pluck('group')->toArray();
$all_groups = Group::whereIn('groups.idgroups', $host_groups);
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Resources/Party.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Party extends JsonResource
* format="int64",
* example=1
* )
* @OA\Property(
* property="approved",
* title="approved",
* description="Whether this event has been approved.",
* format="boolean",
* example="false"
* )
*/

/**
* @OA\Property(
* property="start",
* title="start",
Expand Down Expand Up @@ -228,6 +238,7 @@ public function toArray($request)
// We return information which can be public, and we rename fields to look more consistent.
return [
'id' => $this->idevents,
'approved' => $this->approved ? true : false,
'start' => $this->event_start_utc,
'end' => $this->event_end_utc,
'timezone' => $this->timezone,
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Resources/PartySummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class PartySummary extends JsonResource
* format="int64",
* example=1
* )
* @OA\Property(
* property="approved",
* title="approved",
* description="Whether this event has been approved.",
* format="boolean",
* example="false"
* )
*/

/**
* @OA\Property(
* property="start",
* title="start",
Expand Down Expand Up @@ -110,6 +120,7 @@ public function toArray($request)
// peculiarity which I can't get to the bottom of. So pull it from the resource.
return [
'id' => $this->idevents,
'approved' => $this->approved ? true : false,
'start' => $this->event_start_utc,
'end' => $this->event_end_utc,
'timezone' => $this->timezone,
Expand Down
2 changes: 1 addition & 1 deletion app/Party.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function ofTheseGroups($groups = 'admin', $only_past = false, $devices =
*,
`e`.`venue` AS `venue`, `e`.`link` AS `link`, `e`.`location` as `location`,
`g`.`name` AS group_name,
UNIX_TIMESTAMP(e.`event_start_utc`) ) AS `event_timestamp`
UNIX_TIMESTAMP(e.`event_start_utc`) AS `event_timestamp`
FROM `'.$this->table.'` AS `e`
INNER JOIN `groups` as `g` ON `e`.`group` = `g`.`idgroups`
Expand Down
11 changes: 10 additions & 1 deletion app/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use App\Device;
use App\Helpers\Fixometer;
use DB;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -41,7 +42,15 @@ public function parties($list = [], $groups = [], $from = null, $to = null, $gro
$eventsQuery->orderBy('events.event_start_utc', 'desc');

// We need to explicitly select what we want to return otherwise gtag.group might overwrite events.group.
return $eventsQuery->select(['events.*', 'gtag.group_tag'])->get();
$events = $eventsQuery->select(['events.*', 'gtag.group_tag'])->get();

$me = auth()->user();

$events = $events->filter(function ($event) use ($me) {
return User::userCanSeeEvent($me, $event);
});

return $events;
}

public function deviceStatusCount($parties)
Expand Down
20 changes: 20 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Events\UserDeleted;
use App\Events\UserUpdated;
use App\Helpers\Fixometer;
use App\Network;
use App\UserGroups;
use App\UsersPermissions;
Expand Down Expand Up @@ -582,4 +583,23 @@ public function preferredLocale()
// to users (not admins).
return $this->language;
}

public static function userCanSeeEvent($user, $event) {
// We need to filter based on approved visibility:
// - where the group is approved, this event is visible
// - where the group is not approved, this event is visible to network coordinators or group hosts.
$amHost = $user && $user->hasRole('Host');
$admin = $user && $user->hasRole('Administrator');

$group = Group::find($event->group);

if (($event->approved && $group->approved) ||
$admin ||
($user && $user->isCoordinatorForGroup($group)) ||
($amHost && $user && Fixometer::userIsHostOfGroup($group->idgroups, $user->id))) {
return true;
}

return false;
}
}
1 change: 0 additions & 1 deletion public/icons/map_marker_ico.svg

This file was deleted.

10 changes: 6 additions & 4 deletions resources/views/layouts/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
<?php
// We want to show the current branch. This will only be set on development or staging environments.
$branch = "Unknown branch";
@exec('git branch | ' . "grep ' * '", $shellOutput);
foreach ($shellOutput as $line) {
if (strpos($line, '* ') !== false) {
$branch = trim(strtolower(str_replace('* ', '', $line)));
if (is_dir('.git')) {
@exec('git branch | ' . "grep ' * '", $shellOutput);
foreach ($shellOutput as $line) {
if (strpos($line, '* ') !== false) {
$branch = trim(strtolower(str_replace('* ', '', $line)));
}
}
}
?>
Expand Down
Loading

0 comments on commit c8ec5a5

Please sign in to comment.