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

Compare date poll with user's calendar #747

Closed
wants to merge 23 commits into from
Closed
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
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
['name' => 'acl#getByToken', 'url' => '/acl/get/s/{token}', 'verb' => 'GET'],
['name' => 'acl#get', 'url' => '/acl/get/{id}', 'verb' => 'GET'],

['name' => 'system#findCalendarEvents', 'url' => '/events/list/', 'verb' => 'POST'],
['name' => 'system#get_site_users_and_groups', 'url' => '/siteusers/get/', 'verb' => 'POST'],
['name' => 'system#validate_public_username', 'url' => '/check/username', 'verb' => 'POST']
]
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/OptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function __construct(

/**
* Get all options of given poll
* @PublicPage
* @NoCSRFRequired
* @NoAdminRequired
* @param integer $pollId
* @return array Array of Option objects
Expand Down
46 changes: 44 additions & 2 deletions lib/Controller/SystemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

namespace OCA\Polls\Controller;

use DateTime;
use DateInterval;
use OCP\ILogger;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
Expand All @@ -32,11 +35,12 @@
use OCP\IUserManager;
use OCP\IConfig;
use OCP\IRequest;
use OCA\Polls\Db\Option;
use OCA\Polls\Db\Share;
use OCA\Polls\Db\ShareMapper;
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\VoteMapper;
use OCP\ILogger;
use OCA\Polls\Service\CalendarService;

class SystemController extends Controller {

Expand All @@ -47,6 +51,7 @@ class SystemController extends Controller {
private $userManager;
private $voteMapper;
private $shareMapper;
private $calendarService;


/**
Expand All @@ -60,6 +65,7 @@ class SystemController extends Controller {
* @param IUserManager $userManager
* @param VoteMapper $voteMapper
* @param ShareMapper $shareMapper
* @param CalendarService $calendarService,
*/
public function __construct(
string $appName,
Expand All @@ -70,7 +76,8 @@ public function __construct(
IGroupManager $groupManager,
IUserManager $userManager,
VoteMapper $voteMapper,
ShareMapper $shareMapper
ShareMapper $shareMapper,
CalendarService $calendarService
) {
parent::__construct($appName, $request);
$this->voteMapper = $voteMapper;
Expand All @@ -80,6 +87,41 @@ public function __construct(
$this->systemConfig = $systemConfig;
$this->groupManager = $groupManager;
$this->userManager = $userManager;
$this->calendarService = $calendarService;
}


/**
* findCalendarEvents
* @NoAdminRequired
* @param integer $from
* @param integer $to
* @return DataResponse
*/
public function findCalendarEvents($from, $to = null) {

if (is_int($from)) {
$searchFrom = new DateTime();
$searchFrom = $searchFrom->setTimestamp($from);
} else {
$searchFrom = new DateTime($from);
}


if (!$to) {
$searchTo = clone $searchFrom;
$searchTo = $searchTo->add(new DateInterval('PT1H'));

} else if (is_int($to)) {
$searchTo = new DateTime();
$searchTo = $searchTo->setTimestamp($to);
} else {
$searchTo = new DateTime($to);
}

$events = array_values($this->calendarService->getEvents($searchFrom, $searchTo));
return new DataResponse($events, Http::STATUS_OK);

}

/**
Expand Down
102 changes: 102 additions & 0 deletions lib/Service/CalendarService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
*
* @author René Gieling <github@dartcafe.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Polls\Service;

use DateTime;
use OCP\Calendar\IManager as CalendarManager;
use OCP\Calendar\ICalendar;

class CalendarService {

private $calendarManager;
private $calendars;

public function __construct(
CalendarManager $calendarManager
) {
$this->calendarManager = $calendarManager;
$this->calendars = $this->calendarManager->getCalendars();
}

/**
* getEvents - get events from the user's calendars inside given timespan
* @NoAdminRequired
* @param DateTime $from
* @param DateTime $to
* @return Array
*/
public function getEvents($from, $to) {
$events = [];

foreach ($this->calendars as $calendar) {
$foundEvents = $calendar->search('' ,['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]);
foreach ($foundEvents as $event) {
array_push($events, [
'relatedFrom' => $from->getTimestamp(),
'relatedTo' => $to->getTimestamp(),
'name' => $calendar->getDisplayName(),
'key' => $calendar->getKey(),
'displayColor' => $calendar->getDisplayColor(),
'permissions' => $calendar->getPermissions(),
'eventId' => $event['id'],
'UID' => $event['objects'][0]['UID'][0],
'summary' => isset($event['objects'][0]['SUMMARY'][0])? $event['objects'][0]['SUMMARY'][0] : '',
'description' => isset($event['objects'][0]['DESCRIPTION'][0])? $event['objects'][0]['DESCRIPTION'][0] : '',
'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '',
'eventFrom' => isset($event['objects'][0]['DTSTART'][0]) ? $event['objects'][0]['DTSTART'][0]->getTimestamp() : 0,
'eventTo' => isset($event['objects'][0]['DTEND'][0] ) ? $event['objects'][0]['DTEND'][0]->getTimestamp() : 0,
'calDav' => $event
]);
}
}
return $events;
}

/**
* Get user's calendars
* @NoAdminRequired
* @return Array
*/
public function getCalendars() {
return $this->calendars;
}


/**
* Get events from the user's calendar which are 2 hours before and 3 hours after the timestamp
* @NoAdminRequired
* @param DateTime $from
* @return Array
*/
public function getEventsAround($from) {
$from = new DateTime($from);
$to = new DateTime($from);
return $this->getEvents(
$from->sub(new DateInterval('P2H')),
$to->add(new DateInterval('P3H'))
);
}

}
3 changes: 1 addition & 2 deletions src/js/components/VoteTable/VoteTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@

<VoteTableHeader v-for="(option) in sortedOptions"
:key="option.id"
:option="option"
:poll-type="poll.type" />
:option="option" />
</div>

<div v-for="(participant) in participants" :key="participant.userId" :class="{currentuser: (participant.userId === currentUser) }">
Expand Down
49 changes: 41 additions & 8 deletions src/js/components/VoteTable/VoteTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{{ option.pollOptionText }}
</div>

<div v-if="poll.type === 'datePoll'" v-tooltip.auto="moment.unix(option.timestamp).format('llll')" class="date-box">
<div v-if="poll.type === 'datePoll'" v-tooltip.auto="{content: tooltip}" class="date-box">
<div class="month">
{{ moment.unix(option.timestamp).format('MMM') + " '" + moment.unix(option.timestamp).format('YY') }}
</div>
Expand All @@ -39,6 +39,7 @@
<div class="time">
{{ moment.unix(option.timestamp).format('LT') }}
</div>
<div v-if="calendarEvent !== undefined" class="conflict icon icon-calendar" />
</div>

<div class="counter">
Expand All @@ -62,10 +63,6 @@ export default {
option: {
type: Object,
default: undefined
},
pollType: {
type: String,
default: undefined
}
},

Expand All @@ -80,13 +77,41 @@ export default {
computed: {
...mapState({
poll: state => state.poll,
votes: state => state.votes.list
votes: state => state.votes.list,
events: state => state.options.events
}),

pollType() {
return this.poll.type
},

...mapGetters([
'votesRank',
'winnerCombo'
]),

calendarEvent() {
return this.events.find(event => event.timestamp === this.option.timestamp)
},

tooltip() {
let tooltip = moment.unix(this.option.timestamp).format('llll')
if (this.calendarEvent !== undefined) {
tooltip = tooltip.concat(t('polls', '\n\nFound calendar events:'))
this.calendarEvent.events.forEach(event => {
tooltip = tooltip.concat(
'\n',
moment.unix(event.eventFrom).format('LT'),
' - ',
moment.unix(event.eventTo).format('LT'),
': ',
event.summary
)
})
}
return tooltip
},

yesVotes() {
const pollOptionText = this.option.pollOptionText
return this.votesRank.find(rank => {
Expand Down Expand Up @@ -134,7 +159,15 @@ export default {
color: #49bc49;
}
}

.conflict {
font-style: normal;
font-weight: 400;
width: 32px;
height: 32px;
// background-size: 28px;
background-color: var(--color-warning);
border-radius: 50%;
}
.counter {
flex: 0;
display: flex;
Expand Down Expand Up @@ -180,7 +213,7 @@ export default {
flex: 1 0;
padding: 0 2px;
align-items: center;
justify-content: center;
justify-content: start;

.month, .dow {
font-size: 1.2em;
Expand Down
31 changes: 30 additions & 1 deletion src/js/store/modules/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import sortBy from 'lodash/sortBy'

const defaultOptions = () => {
return {
list: []
list: [],
events: []
}
}

Expand All @@ -40,6 +41,14 @@ const mutations = {
Object.assign(state, defaultOptions())
},

resetEvents(state) {
state.events = []
},

addEvents(state, payload) {
state.events.push(payload.event)
},

optionRemove(state, payload) {
state.list = state.list.filter(option => {
return option.id !== payload.option.id
Expand Down Expand Up @@ -94,13 +103,33 @@ const actions = {
return axios.get(OC.generateUrl(endPoint))
.then((response) => {
context.commit('optionsSet', { list: response.data })
if (context.rootState.poll.type === 'datePoll') {
context.dispatch('loadEvents')
}
}, (error) => {
context.commit('reset')
console.error('Error loading options', { error: error.response }, { payload: payload })
throw error
})
},

loadEvents(context) {
context.commit('resetEvents')
context.state.list.forEach(function(item) {
axios.post(OC.generateUrl('apps/polls/events/list/'), { from: item.timestamp })
.then((response) => {
if (response.data.length) {
context.commit('addEvents', {
event: {
timestamp: item.timestamp,
events: response.data
}
})
}
})
})
},

updateOptions(context) {
context.state.list.forEach((item, i) => {
context.dispatch('updateOptionAsync', { option: item })
Expand Down