Passed
Pull Request — master (#224)
by Kai
02:51
created
tests/bootstrap.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -25,8 +25,8 @@
 block discarded – undo
25 25
 	define('PHPUNIT_RUN', 1);
26 26
 }
27 27
 
28
-require_once(__DIR__.'/../../../lib/base.php');
29
-require_once(__DIR__.'/../vendor/autoload.php');
28
+require_once(__DIR__ . '/../../../lib/base.php');
29
+require_once(__DIR__ . '/../vendor/autoload.php');
30 30
 
31 31
 \OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests');
32 32
 \OC_App::loadApp('polls');
Please login to merge, or discard this patch.
lib/Controller/PageController.php 1 patch
Indentation   +700 added lines, -700 removed lines patch added patch discarded remove patch
@@ -56,704 +56,704 @@
 block discarded – undo
56 56
 
57 57
 class PageController extends Controller {
58 58
 
59
-	private $userId;
60
-	private $commentMapper;
61
-	private $dateMapper;
62
-	private $eventMapper;
63
-	private $notificationMapper;
64
-	private $participationMapper;
65
-	private $participationTextMapper;
66
-	private $textMapper;
67
-	private $urlGenerator;
68
-	private $manager;
69
-	private $avatarManager;
70
-	private $logger;
71
-	private $trans;
72
-	private $userMgr;
73
-	private $groupManager;
74
-
75
-	/**
76
-	 * PageController constructor.
77
-	 * @param $appName
78
-	 * @param IRequest $request
79
-	 * @param IUserManager $manager
80
-	 * @param IGroupManager $groupManager
81
-	 * @param IAvatarManager $avatarManager
82
-	 * @param ILogger $logger
83
-	 * @param IL10N $trans
84
-	 * @param IURLGenerator $urlGenerator
85
-	 * @param $userId
86
-	 * @param CommentMapper $commentMapper
87
-	 * @param DateMapper $dateMapper
88
-	 * @param EventMapper $eventMapper
89
-	 * @param NotificationMapper $notificationMapper
90
-	 * @param ParticipationMapper $ParticipationMapper
91
-	 * @param ParticipationTextMapper $ParticipationTextMapper
92
-	 * @param TextMapper $textMapper
93
-	 */
94
-	public function __construct(
95
-		$appName,
96
-		IRequest $request,
97
-		IUserManager $manager,
98
-		IGroupManager $groupManager,
99
-		IAvatarManager $avatarManager,
100
-		ILogger $logger,
101
-		IL10N $trans,
102
-		IURLGenerator $urlGenerator,
103
-		$userId,
104
-		CommentMapper $commentMapper,
105
-		DateMapper $dateMapper,
106
-		EventMapper $eventMapper,
107
-		NotificationMapper $notificationMapper,
108
-		ParticipationMapper $ParticipationMapper,
109
-		ParticipationTextMapper $ParticipationTextMapper,
110
-		TextMapper $textMapper
111
-	) {
112
-		parent::__construct($appName, $request);
113
-		$this->manager = $manager;
114
-		$this->groupManager = $groupManager;
115
-		$this->avatarManager = $avatarManager;
116
-		$this->logger = $logger;
117
-		$this->trans = $trans;
118
-		$this->urlGenerator = $urlGenerator;
119
-		$this->userId = $userId;
120
-		$this->commentMapper = $commentMapper;
121
-		$this->dateMapper = $dateMapper;
122
-		$this->eventMapper = $eventMapper;
123
-		$this->notificationMapper = $notificationMapper;
124
-		$this->participationMapper = $ParticipationMapper;
125
-		$this->participationTextMapper = $ParticipationTextMapper;
126
-		$this->textMapper = $textMapper;
127
-		$this->userMgr = \OC::$server->getUserManager();
128
-	}
129
-
130
-	/**
131
-	 * @NoAdminRequired
132
-	 * @NoCSRFRequired
133
-	 */
134
-	public function index() {
135
-		$polls = $this->eventMapper->findAllForUserWithInfo($this->userId);
136
-		$comments = $this->commentMapper->findDistinctByUser($this->userId);
137
-		$partic = $this->participationMapper->findDistinctByUser($this->userId);
138
-		$particText = $this->participationTextMapper->findDistinctByUser($this->userId);
139
-		$response = new TemplateResponse('polls', 'main.tmpl', [
140
-			'polls' => $polls,
141
-			'comments' => $comments,
142
-			'participations' => $partic,
143
-			'participations_text' => $particText,
144
-			'userId' => $this->userId,
145
-			'userMgr' => $this->manager,
146
-			'urlGenerator' => $this->urlGenerator
147
-		]);
148
-		if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
149
-			$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
150
-			$response->setContentSecurityPolicy($csp);
151
-		}
152
-		return $response;
153
-	}
154
-
155
-	/**
156
-	 * @param string $pollId
157
-	 * @param string $from
158
-	 */
159
-	private function sendNotifications($pollId, $from) {
160
-		$poll = $this->eventMapper->find($pollId);
161
-		$notifications = $this->notificationMapper->findAllByPoll($pollId);
162
-		foreach ($notifications as $notification) {
163
-			if ($from === $notification->getUserId()) {
164
-				continue;
165
-			}
166
-			$email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email');
167
-			if (strlen($email) === 0 || !isset($email)) {
168
-				continue;
169
-			}
170
-			$url = \OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getURLGenerator()->linkToRoute('polls.page.goto_poll',
171
-				array('hash' => $poll->getHash())));
172
-
173
-			$recUser = $this->userMgr->get($notification->getUserId());
174
-			$sendUser = $this->userMgr->get($from);
175
-			$rec = "";
176
-			if ($recUser !== null) {
177
-				$rec = $recUser->getDisplayName();
178
-			}
179
-			if ($sendUser !== null) {
180
-				$sender = $sendUser->getDisplayName();
181
-			} else {
182
-				$sender = $from;
183
-			}
184
-			$msg = $this->trans->t('Hello %s,<br/><br/><strong>%s</strong> participated in the poll \'%s\'.<br/><br/>To go directly to the poll, you can use this <a href="%s">link</a>',
185
-				array(
186
-					$rec,
187
-					$sender,
188
-					$poll->getTitle(),
189
-					$url
190
-				));
191
-
192
-			$msg .= "<br/><br/>";
193
-
194
-			$toName = $this->userMgr->get($notification->getUserId())->getDisplayName();
195
-			$subject = $this->trans->t('Polls App - New Comment');
196
-			$fromAddress = Util::getDefaultEmailAddress('no-reply');
197
-			$fromName = $this->trans->t("Polls App") . ' (' . $from . ')';
198
-
199
-			try {
200
-				$mailer = \OC::$server->getMailer();
201
-				$message = $mailer->createMessage();
202
-				$message->setSubject($subject);
203
-				$message->setFrom(array($fromAddress => $fromName));
204
-				$message->setTo(array($email => $toName));
205
-				$message->setHtmlBody($msg);
206
-				$mailer->send($message);
207
-			} catch (\Exception $e) {
208
-				$message = 'Error sending mail to: ' . $toName . ' (' . $email . ')';
209
-				Util::writeLog("polls", $message, Util::ERROR);
210
-			}
211
-		}
212
-	}
213
-
214
-	/**
215
-	 * @NoAdminRequired
216
-	 * @NoCSRFRequired
217
-	 * @PublicPage
218
-	 * @param string $hash
219
-	 * @return TemplateResponse
220
-	 */
221
-	public function gotoPoll($hash) {
222
-		$poll = $this->eventMapper->findByHash($hash);
223
-		if ($poll->getType() == '0') {
224
-			$dates = $this->dateMapper->findByPoll($poll->getId());
225
-			$votes = $this->participationMapper->findByPoll($poll->getId());
226
-		} else {
227
-			$dates = $this->textMapper->findByPoll($poll->getId());
228
-			$votes = $this->participationTextMapper->findByPoll($poll->getId());
229
-		}
230
-		$comments = $this->commentMapper->findByPoll($poll->getId());
231
-		try {
232
-			$notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId);
233
-		} catch (DoesNotExistException $e) {
234
-			$notification = null;
235
-		}
236
-		if ($this->hasUserAccess($poll)) {
237
-			return new TemplateResponse('polls', 'goto.tmpl', [
238
-				'poll' => $poll,
239
-				'dates' => $dates,
240
-				'comments' => $comments,
241
-				'votes' => $votes,
242
-				'notification' => $notification,
243
-				'userId' => $this->userId,
244
-				'userMgr' => $this->manager,
245
-				'urlGenerator' => $this->urlGenerator,
246
-				'avatarManager' => $this->avatarManager
247
-			]);
248
-		} else {
249
-			User::checkLoggedIn();
250
-			return new TemplateResponse('polls', 'no.acc.tmpl', []);
251
-		}
252
-	}
253
-
254
-	/**
255
-	 * @NoAdminRequired
256
-	 * @NoCSRFRequired
257
-	 * @param string $pollId
258
-	 * @return RedirectResponse
259
-	 */
260
-	public function deletePoll($pollId) {
261
-		$poll = new Event();
262
-		$poll->setId($pollId);
263
-		$this->eventMapper->delete($poll);
264
-		$this->textMapper->deleteByPoll($pollId);
265
-		$this->dateMapper->deleteByPoll($pollId);
266
-		$this->participationMapper->deleteByPoll($pollId);
267
-		$this->participationTextMapper->deleteByPoll($pollId);
268
-		$this->commentMapper->deleteByPoll($pollId);
269
-		$url = $this->urlGenerator->linkToRoute('polls.page.index');
270
-		return new RedirectResponse($url);
271
-	}
272
-
273
-	/**
274
-	 * @NoAdminRequired
275
-	 * @NoCSRFRequired
276
-	 * @param string $hash
277
-	 * @return TemplateResponse
278
-	 */
279
-	public function editPoll($hash) {
280
-		$poll = $this->eventMapper->findByHash($hash);
281
-		if ($this->userId !== $poll->getOwner()) {
282
-			return new TemplateResponse('polls', 'no.create.tmpl');
283
-		}
284
-		if ($poll->getType() == '0') {
285
-			$dates = $this->dateMapper->findByPoll($poll->getId());
286
-		} else {
287
-			$dates = $this->textMapper->findByPoll($poll->getId());
288
-		}
289
-		return new TemplateResponse('polls', 'create.tmpl', [
290
-			'poll' => $poll,
291
-			'dates' => $dates,
292
-			'userId' => $this->userId,
293
-			'userMgr' => $this->manager,
294
-			'urlGenerator' => $this->urlGenerator
295
-		]);
296
-	}
297
-
298
-	/**
299
-	 * @NoAdminRequired
300
-	 * @NoCSRFRequired
301
-	 * @param $pollId
302
-	 * @param $pollType
303
-	 * @param $pollTitle
304
-	 * @param $pollDesc
305
-	 * @param $userId
306
-	 * @param $chosenDates
307
-	 * @param $expireTs
308
-	 * @param $accessType
309
-	 * @param $accessValues
310
-	 * @param $isAnonymous
311
-	 * @param $hideNames
312
-	 * @return RedirectResponse
313
-	 */
314
-	public function updatePoll(
315
-		$pollId,
316
-		$pollType,
317
-		$pollTitle,
318
-		$pollDesc,
319
-		$userId,
320
-		$chosenDates,
321
-		$expireTs,
322
-		$accessType,
323
-		$accessValues,
324
-		$isAnonymous,
325
-		$hideNames
326
-	) {
327
-		$event = $this->eventMapper->find($pollId);
328
-		$event->setTitle(htmlspecialchars($pollTitle));
329
-		$event->setDescription(htmlspecialchars($pollDesc));
330
-		$event->setIsAnonymous($isAnonymous ? 1 : 0);
331
-		$event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
332
-
333
-		if ($accessType === 'select') {
334
-			if (isset($accessValues)) {
335
-				$accessValues = json_decode($accessValues);
336
-				if ($accessValues !== null) {
337
-					$groups = array();
338
-					$users = array();
339
-					if ($accessValues->groups !== null) {
340
-						$groups = $accessValues->groups;
341
-					}
342
-					if ($accessValues->users !== null) {
343
-						$users = $accessValues->users;
344
-					}
345
-					$accessType = '';
346
-					foreach ($groups as $gid) {
347
-						$accessType .= $gid . ';';
348
-					}
349
-					foreach ($users as $uid) {
350
-						$accessType .= $uid . ';';
351
-					}
352
-				}
353
-			}
354
-		}
355
-		$event->setAccess($accessType);
356
-
357
-		$chosenDates = json_decode($chosenDates);
358
-
359
-		$expire = null;
360
-		if ($expireTs !== null && $expireTs !== '') {
361
-			$expire = date('Y-m-d H:i:s', $expireTs + 60 * 60 * 24); //add one day, so it expires at the end of a day
362
-		}
363
-		$event->setExpire($expire);
364
-
365
-		$this->dateMapper->deleteByPoll($pollId);
366
-		$this->textMapper->deleteByPoll($pollId);
367
-		if ($pollType === 'event') {
368
-			$event->setType(0);
369
-			$this->eventMapper->update($event);
370
-			sort($chosenDates);
371
-			foreach ($chosenDates as $el) {
372
-				$date = new Date();
373
-				$date->setPollId($pollId);
374
-				$date->setDt(date('Y-m-d H:i:s', $el));
375
-				$this->dateMapper->insert($date);
376
-			}
377
-		} else {
378
-			$event->setType(1);
379
-			$this->eventMapper->update($event);
380
-			foreach ($chosenDates as $el) {
381
-				$text = new Text();
382
-				$text->setText($el);
383
-				$text->setPollId($pollId);
384
-				$this->textMapper->insert($text);
385
-			}
386
-		}
387
-		$url = $this->urlGenerator->linkToRoute('polls.page.index');
388
-		return new RedirectResponse($url);
389
-	}
390
-
391
-	/**
392
-	 * @NoAdminRequired
393
-	 * @NoCSRFRequired
394
-	 */
395
-	public function createPoll() {
396
-		return new TemplateResponse('polls', 'create.tmpl',
397
-			['userId' => $this->userId, 'userMgr' => $this->manager, 'urlGenerator' => $this->urlGenerator]);
398
-	}
399
-
400
-	/**
401
-	 * @NoAdminRequired
402
-	 * @NoCSRFRequired
403
-	 * @param $pollType
404
-	 * @param $pollTitle
405
-	 * @param $pollDesc
406
-	 * @param $userId
407
-	 * @param $chosenDates
408
-	 * @param $expireTs
409
-	 * @param $accessType
410
-	 * @param $accessValues
411
-	 * @param $isAnonymous
412
-	 * @param $hideNames
413
-	 * @return RedirectResponse
414
-	 */
415
-	public function insertPoll(
416
-		$pollType,
417
-		$pollTitle,
418
-		$pollDesc,
419
-		$userId,
420
-		$chosenDates,
421
-		$expireTs,
422
-		$accessType,
423
-		$accessValues,
424
-		$isAnonymous,
425
-		$hideNames
426
-	) {
427
-		$secureRandom = new SecureRandom();
428
-		$event = new Event();
429
-		$event->setTitle(htmlspecialchars($pollTitle));
430
-		$event->setDescription(htmlspecialchars($pollDesc));
431
-		$event->setOwner($userId);
432
-		$event->setCreated(date('Y-m-d H:i:s'));
433
-		$event->setHash($secureRandom->generate(
434
-			16,
435
-			'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
436
-		));
437
-		$event->setIsAnonymous($isAnonymous ? 1 : 0);
438
-		$event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
439
-
440
-		if ($accessType === 'select') {
441
-			if (isset($accessValues)) {
442
-				$accessValues = json_decode($accessValues);
443
-				if ($accessValues !== null) {
444
-					$groups = array();
445
-					$users = array();
446
-					if ($accessValues->groups !== null) {
447
-						$groups = $accessValues->groups;
448
-					}
449
-					if ($accessValues->users !== null) {
450
-						$users = $accessValues->users;
451
-					}
452
-					$accessType = '';
453
-					foreach ($groups as $gid) {
454
-						$accessType .= $gid . ';';
455
-					}
456
-					foreach ($users as $uid) {
457
-						$accessType .= $uid . ';';
458
-					}
459
-				}
460
-			}
461
-		}
462
-		$event->setAccess($accessType);
463
-
464
-		$chosenDates = json_decode($chosenDates);
465
-
466
-		$expire = null;
467
-		if ($expireTs !== null && $expireTs !== '') {
468
-			$expire = date('Y-m-d H:i:s', $expireTs + 60 * 60 * 24); //add one day, so it expires at the end of a day
469
-		}
470
-		$event->setExpire($expire);
471
-
472
-		if ($pollType === 'event') {
473
-			$event->setType(0);
474
-			$ins = $this->eventMapper->insert($event);
475
-			$poll_id = $ins->getId();
476
-			sort($chosenDates);
477
-			foreach ($chosenDates as $el) {
478
-				$date = new Date();
479
-				$date->setPollId($poll_id);
480
-				$date->setDt(date('Y-m-d H:i:s', $el));
481
-				$this->dateMapper->insert($date);
482
-			}
483
-		} else {
484
-			$event->setType(1);
485
-			$ins = $this->eventMapper->insert($event);
486
-			$poll_id = $ins->getId();
487
-			$cnt = 1;
488
-			foreach ($chosenDates as $el) {
489
-				$text = new Text();
490
-				$text->setText($el . '_' . $cnt);
491
-				$text->setPollId($poll_id);
492
-				$this->textMapper->insert($text);
493
-				$cnt++;
494
-			}
495
-		}
496
-		$url = $this->urlGenerator->linkToRoute('polls.page.index');
497
-		return new RedirectResponse($url);
498
-	}
499
-
500
-	/**
501
-	 * @NoAdminRequired
502
-	 * @NoCSRFRequired
503
-	 * @PublicPage
504
-	 * @param $pollId
505
-	 * @param $userId
506
-	 * @param $types
507
-	 * @param $dates
508
-	 * @param $receiveNotifications
509
-	 * @param $changed
510
-	 * @return RedirectResponse
511
-	 */
512
-	public function insertVote($pollId, $userId, $types, $dates, $receiveNotifications, $changed) {
513
-		if ($this->userId !== null) {
514
-			if ($receiveNotifications === 'true') {
515
-				try {
516
-					//check if user already set notification for this poll
517
-					$this->notificationMapper->findByUserAndPoll($pollId, $userId);
518
-				} catch (DoesNotExistException $e) {
519
-					//insert if not exist
520
-					$not = new Notification();
521
-					$not->setUserId($userId);
522
-					$not->setPollId($pollId);
523
-					$this->notificationMapper->insert($not);
524
-				}
525
-			} else {
526
-				try {
527
-					//delete if entry is in db
528
-					$not = $this->notificationMapper->findByUserAndPoll($pollId, $userId);
529
-					$this->notificationMapper->delete($not);
530
-				} catch (DoesNotExistException $e) {
531
-					//doesn't exist in db, nothing to do
532
-				}
533
-			}
534
-		}
535
-		$poll = $this->eventMapper->find($pollId);
536
-		if ($changed === 'true') {
537
-			$dates = json_decode($dates);
538
-			$types = json_decode($types);
539
-			$count_dates = count($dates);
540
-			if ($poll->getType() == '0') {
541
-				$this->participationMapper->deleteByPollAndUser($pollId, $userId);
542
-			} else {
543
-				$this->participationTextMapper->deleteByPollAndUser($pollId, $userId);
544
-			}
545
-			for ($i = 0; $i < $count_dates; $i++) {
546
-				if ($poll->getType() == '0') {
547
-					$part = new Participation();
548
-					$part->setPollId($pollId);
549
-					$part->setUserId($userId);
550
-					$part->setDt(date('Y-m-d H:i:s', $dates[$i]));
551
-					$part->setType($types[$i]);
552
-					$this->participationMapper->insert($part);
553
-				} else {
554
-					$part = new ParticipationText();
555
-					$part->setPollId($pollId);
556
-					$part->setUserId($userId);
557
-					$part->setText($dates[$i]);
558
-					$part->setType($types[$i]);
559
-					$this->participationTextMapper->insert($part);
560
-				}
561
-
562
-			}
563
-			$this->sendNotifications($pollId, $userId);
564
-		}
565
-		$hash = $poll->getHash();
566
-		$url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]);
567
-		return new RedirectResponse($url);
568
-	}
569
-
570
-	/**
571
-	 * @NoAdminRequired
572
-	 * @NoCSRFRequired
573
-	 * @PublicPage
574
-	 * @param $pollId
575
-	 * @param $userId
576
-	 * @param $commentBox
577
-	 * @return JSONResponse
578
-	 */
579
-	public function insertComment($pollId, $userId, $commentBox) {
580
-		$comment = new Comment();
581
-		$comment->setPollId($pollId);
582
-		$comment->setUserId($userId);
583
-		$comment->setComment($commentBox);
584
-		$comment->setDt(date('Y-m-d H:i:s'));
585
-		$this->commentMapper->insert($comment);
586
-		$this->sendNotifications($pollId, $userId);
587
-		if ($this->manager->get($userId) !== null) {
588
-			$newUserId = $this->manager->get($userId)->getDisplayName();
589
-		} else {
590
-			$newUserId = $userId;
591
-		}
592
-		return new JSONResponse(array(
593
-			'comment' => $commentBox,
594
-			'date' => date('Y-m-d H:i:s'),
595
-			'userName' => $newUserId
596
-		));
597
-	}
598
-
599
-	/**
600
-	 * @NoAdminRequired
601
-	 * @NoCSRFRequired
602
-	 * @param $searchTerm
603
-	 * @param $groups
604
-	 * @param $users
605
-	 * @return array
606
-	 */
607
-	 public function search($searchTerm, $groups, $users) {
608
-		return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users));
609
-	}
610
-
611
-	/**
612
-	 * @NoAdminRequired
613
-	 * @NoCSRFRequired
614
-	 * @param $searchTerm
615
-	 * @param $groups
616
-	 * @return array
617
-	 */
618
-	 public function searchForGroups($searchTerm, $groups) {
619
-		$selectedGroups = json_decode($groups);
620
-		$groups = $this->groupManager->search($searchTerm);
621
-		$gids = array();
622
-		$sgids = array();
623
-		foreach ($selectedGroups as $sg) {
624
-			$sgids[] = str_replace('group_', '', $sg);
625
-		}
626
-		foreach ($groups as $g) {
627
-			$gids[] = $g->getGID();
628
-		}
629
-		$diffGids = array_diff($gids, $sgids);
630
-		$gids = array();
631
-		foreach ($diffGids as $g) {
632
-			$gids[] = ['gid' => $g, 'isGroup' => true];
633
-		}
634
-		return $gids;
635
-	}
636
-
637
-	/**
638
-	 * @NoAdminRequired
639
-	 * @NoCSRFRequired
640
-	 * @param $searchTerm
641
-	 * @param $users
642
-	 * @return array
643
-	 */
644
-	 public function searchForUsers($searchTerm, $users) {
645
-		$selectedUsers = json_decode($users);
646
-		Util::writeLog("polls", print_r($selectedUsers, true), Util::ERROR);
647
-		$userNames = $this->userMgr->searchDisplayName($searchTerm);
648
-		$users = array();
649
-		$sUsers = array();
650
-		foreach ($selectedUsers as $su) {
651
-			$sUsers[] = str_replace('user_', '', $su);
652
-		}
653
-		foreach ($userNames as $u) {
654
-			$alreadyAdded = false;
655
-			foreach ($sUsers as &$su) {
656
-				if ($su === $u->getUID()) {
657
-					unset($su);
658
-					$alreadyAdded = true;
659
-					break;
660
-				}
661
-			}
662
-			if (!$alreadyAdded) {
663
-				$users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false);
664
-			} else {
665
-				continue;
666
-			}
667
-		}
668
-		return $users;
669
-	}
670
-
671
-	/**
672
-	 * @NoAdminRequired
673
-	 * @NoCSRFRequired
674
-	 * @param $username
675
-	 * @return string
676
-	 */
677
-	 public function getDisplayName($username) {
678
-		return $this->manager->get($username)->getDisplayName();
679
-	}
680
-
681
-	/**
682
-	 * @return Event[]
683
-	 */
684
-	 public function getPollsForUser() {
685
-		return $this->eventMapper->findAllForUser($this->userId);
686
-	}
687
-
688
-	/**
689
-	 * @param $user
690
-	 * @return Event[]
691
-	 */
692
-	 public function getPollsForUserWithInfo($user = null) {
693
-		if ($user === null) {
694
-			return $this->eventMapper->findAllForUserWithInfo($this->userId);
695
-		} else {
696
-			return $this->eventMapper->findAllForUserWithInfo($user);
697
-		}
698
-	}
699
-	/**
700
-	 * @return array
701
-	 */
702
-	 public function getGroups() {
703
-		// $this->requireLogin();
704
-		if (class_exists('\OC_Group', true)) {
705
-			// Nextcloud <= 11, ownCloud
706
-			return \OC_Group::getUserGroups($this->userId);
707
-		}
708
-		// Nextcloud >= 12
709
-		$groups = \OC::$server->getGroupManager()->getUserGroups(\OC::$server->getUserSession()->getUser());
710
-		return array_map(function ($group) {
711
-			return $group->getGID();
712
-		}, $groups);
713
-	}
714
-
715
-	/**
716
-	 * @param $poll
717
-	 * @return bool
718
-	 */
719
-	private function hasUserAccess($poll) {
720
-		$access = $poll->getAccess();
721
-		$owner = $poll->getOwner();
722
-		if ($access === 'public') {
723
-			return true;
724
-		}
725
-		if ($access === 'hidden') {
726
-			return true;
727
-		}
728
-		if ($this->userId === null) {
729
-			return false;
730
-		}
731
-		if ($access === 'registered') {
732
-			return true;
733
-		}
734
-		if ($owner === $this->userId) {
735
-			return true;
736
-		}
737
-		Util::writeLog("polls", $this->userId, Util::ERROR);
738
-		$user_groups = $this->getGroups();
739
-		$arr = explode(';', $access);
740
-		foreach ($arr as $item) {
741
-			if (strpos($item, 'group_') === 0) {
742
-				$grp = substr($item, 6);
743
-				foreach ($user_groups as $user_group) {
744
-					if ($user_group === $grp) {
745
-						return true;
746
-					}
747
-				}
748
-			} else {
749
-				if (strpos($item, 'user_') === 0) {
750
-					$usr = substr($item, 5);
751
-					if ($usr === User::getUser()) {
752
-						return true;
753
-					}
754
-				}
755
-			}
756
-		}
757
-		return false;
758
-	}
59
+    private $userId;
60
+    private $commentMapper;
61
+    private $dateMapper;
62
+    private $eventMapper;
63
+    private $notificationMapper;
64
+    private $participationMapper;
65
+    private $participationTextMapper;
66
+    private $textMapper;
67
+    private $urlGenerator;
68
+    private $manager;
69
+    private $avatarManager;
70
+    private $logger;
71
+    private $trans;
72
+    private $userMgr;
73
+    private $groupManager;
74
+
75
+    /**
76
+     * PageController constructor.
77
+     * @param $appName
78
+     * @param IRequest $request
79
+     * @param IUserManager $manager
80
+     * @param IGroupManager $groupManager
81
+     * @param IAvatarManager $avatarManager
82
+     * @param ILogger $logger
83
+     * @param IL10N $trans
84
+     * @param IURLGenerator $urlGenerator
85
+     * @param $userId
86
+     * @param CommentMapper $commentMapper
87
+     * @param DateMapper $dateMapper
88
+     * @param EventMapper $eventMapper
89
+     * @param NotificationMapper $notificationMapper
90
+     * @param ParticipationMapper $ParticipationMapper
91
+     * @param ParticipationTextMapper $ParticipationTextMapper
92
+     * @param TextMapper $textMapper
93
+     */
94
+    public function __construct(
95
+        $appName,
96
+        IRequest $request,
97
+        IUserManager $manager,
98
+        IGroupManager $groupManager,
99
+        IAvatarManager $avatarManager,
100
+        ILogger $logger,
101
+        IL10N $trans,
102
+        IURLGenerator $urlGenerator,
103
+        $userId,
104
+        CommentMapper $commentMapper,
105
+        DateMapper $dateMapper,
106
+        EventMapper $eventMapper,
107
+        NotificationMapper $notificationMapper,
108
+        ParticipationMapper $ParticipationMapper,
109
+        ParticipationTextMapper $ParticipationTextMapper,
110
+        TextMapper $textMapper
111
+    ) {
112
+        parent::__construct($appName, $request);
113
+        $this->manager = $manager;
114
+        $this->groupManager = $groupManager;
115
+        $this->avatarManager = $avatarManager;
116
+        $this->logger = $logger;
117
+        $this->trans = $trans;
118
+        $this->urlGenerator = $urlGenerator;
119
+        $this->userId = $userId;
120
+        $this->commentMapper = $commentMapper;
121
+        $this->dateMapper = $dateMapper;
122
+        $this->eventMapper = $eventMapper;
123
+        $this->notificationMapper = $notificationMapper;
124
+        $this->participationMapper = $ParticipationMapper;
125
+        $this->participationTextMapper = $ParticipationTextMapper;
126
+        $this->textMapper = $textMapper;
127
+        $this->userMgr = \OC::$server->getUserManager();
128
+    }
129
+
130
+    /**
131
+     * @NoAdminRequired
132
+     * @NoCSRFRequired
133
+     */
134
+    public function index() {
135
+        $polls = $this->eventMapper->findAllForUserWithInfo($this->userId);
136
+        $comments = $this->commentMapper->findDistinctByUser($this->userId);
137
+        $partic = $this->participationMapper->findDistinctByUser($this->userId);
138
+        $particText = $this->participationTextMapper->findDistinctByUser($this->userId);
139
+        $response = new TemplateResponse('polls', 'main.tmpl', [
140
+            'polls' => $polls,
141
+            'comments' => $comments,
142
+            'participations' => $partic,
143
+            'participations_text' => $particText,
144
+            'userId' => $this->userId,
145
+            'userMgr' => $this->manager,
146
+            'urlGenerator' => $this->urlGenerator
147
+        ]);
148
+        if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
149
+            $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
150
+            $response->setContentSecurityPolicy($csp);
151
+        }
152
+        return $response;
153
+    }
154
+
155
+    /**
156
+     * @param string $pollId
157
+     * @param string $from
158
+     */
159
+    private function sendNotifications($pollId, $from) {
160
+        $poll = $this->eventMapper->find($pollId);
161
+        $notifications = $this->notificationMapper->findAllByPoll($pollId);
162
+        foreach ($notifications as $notification) {
163
+            if ($from === $notification->getUserId()) {
164
+                continue;
165
+            }
166
+            $email = \OC::$server->getConfig()->getUserValue($notification->getUserId(), 'settings', 'email');
167
+            if (strlen($email) === 0 || !isset($email)) {
168
+                continue;
169
+            }
170
+            $url = \OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getURLGenerator()->linkToRoute('polls.page.goto_poll',
171
+                array('hash' => $poll->getHash())));
172
+
173
+            $recUser = $this->userMgr->get($notification->getUserId());
174
+            $sendUser = $this->userMgr->get($from);
175
+            $rec = "";
176
+            if ($recUser !== null) {
177
+                $rec = $recUser->getDisplayName();
178
+            }
179
+            if ($sendUser !== null) {
180
+                $sender = $sendUser->getDisplayName();
181
+            } else {
182
+                $sender = $from;
183
+            }
184
+            $msg = $this->trans->t('Hello %s,<br/><br/><strong>%s</strong> participated in the poll \'%s\'.<br/><br/>To go directly to the poll, you can use this <a href="%s">link</a>',
185
+                array(
186
+                    $rec,
187
+                    $sender,
188
+                    $poll->getTitle(),
189
+                    $url
190
+                ));
191
+
192
+            $msg .= "<br/><br/>";
193
+
194
+            $toName = $this->userMgr->get($notification->getUserId())->getDisplayName();
195
+            $subject = $this->trans->t('Polls App - New Comment');
196
+            $fromAddress = Util::getDefaultEmailAddress('no-reply');
197
+            $fromName = $this->trans->t("Polls App") . ' (' . $from . ')';
198
+
199
+            try {
200
+                $mailer = \OC::$server->getMailer();
201
+                $message = $mailer->createMessage();
202
+                $message->setSubject($subject);
203
+                $message->setFrom(array($fromAddress => $fromName));
204
+                $message->setTo(array($email => $toName));
205
+                $message->setHtmlBody($msg);
206
+                $mailer->send($message);
207
+            } catch (\Exception $e) {
208
+                $message = 'Error sending mail to: ' . $toName . ' (' . $email . ')';
209
+                Util::writeLog("polls", $message, Util::ERROR);
210
+            }
211
+        }
212
+    }
213
+
214
+    /**
215
+     * @NoAdminRequired
216
+     * @NoCSRFRequired
217
+     * @PublicPage
218
+     * @param string $hash
219
+     * @return TemplateResponse
220
+     */
221
+    public function gotoPoll($hash) {
222
+        $poll = $this->eventMapper->findByHash($hash);
223
+        if ($poll->getType() == '0') {
224
+            $dates = $this->dateMapper->findByPoll($poll->getId());
225
+            $votes = $this->participationMapper->findByPoll($poll->getId());
226
+        } else {
227
+            $dates = $this->textMapper->findByPoll($poll->getId());
228
+            $votes = $this->participationTextMapper->findByPoll($poll->getId());
229
+        }
230
+        $comments = $this->commentMapper->findByPoll($poll->getId());
231
+        try {
232
+            $notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId);
233
+        } catch (DoesNotExistException $e) {
234
+            $notification = null;
235
+        }
236
+        if ($this->hasUserAccess($poll)) {
237
+            return new TemplateResponse('polls', 'goto.tmpl', [
238
+                'poll' => $poll,
239
+                'dates' => $dates,
240
+                'comments' => $comments,
241
+                'votes' => $votes,
242
+                'notification' => $notification,
243
+                'userId' => $this->userId,
244
+                'userMgr' => $this->manager,
245
+                'urlGenerator' => $this->urlGenerator,
246
+                'avatarManager' => $this->avatarManager
247
+            ]);
248
+        } else {
249
+            User::checkLoggedIn();
250
+            return new TemplateResponse('polls', 'no.acc.tmpl', []);
251
+        }
252
+    }
253
+
254
+    /**
255
+     * @NoAdminRequired
256
+     * @NoCSRFRequired
257
+     * @param string $pollId
258
+     * @return RedirectResponse
259
+     */
260
+    public function deletePoll($pollId) {
261
+        $poll = new Event();
262
+        $poll->setId($pollId);
263
+        $this->eventMapper->delete($poll);
264
+        $this->textMapper->deleteByPoll($pollId);
265
+        $this->dateMapper->deleteByPoll($pollId);
266
+        $this->participationMapper->deleteByPoll($pollId);
267
+        $this->participationTextMapper->deleteByPoll($pollId);
268
+        $this->commentMapper->deleteByPoll($pollId);
269
+        $url = $this->urlGenerator->linkToRoute('polls.page.index');
270
+        return new RedirectResponse($url);
271
+    }
272
+
273
+    /**
274
+     * @NoAdminRequired
275
+     * @NoCSRFRequired
276
+     * @param string $hash
277
+     * @return TemplateResponse
278
+     */
279
+    public function editPoll($hash) {
280
+        $poll = $this->eventMapper->findByHash($hash);
281
+        if ($this->userId !== $poll->getOwner()) {
282
+            return new TemplateResponse('polls', 'no.create.tmpl');
283
+        }
284
+        if ($poll->getType() == '0') {
285
+            $dates = $this->dateMapper->findByPoll($poll->getId());
286
+        } else {
287
+            $dates = $this->textMapper->findByPoll($poll->getId());
288
+        }
289
+        return new TemplateResponse('polls', 'create.tmpl', [
290
+            'poll' => $poll,
291
+            'dates' => $dates,
292
+            'userId' => $this->userId,
293
+            'userMgr' => $this->manager,
294
+            'urlGenerator' => $this->urlGenerator
295
+        ]);
296
+    }
297
+
298
+    /**
299
+     * @NoAdminRequired
300
+     * @NoCSRFRequired
301
+     * @param $pollId
302
+     * @param $pollType
303
+     * @param $pollTitle
304
+     * @param $pollDesc
305
+     * @param $userId
306
+     * @param $chosenDates
307
+     * @param $expireTs
308
+     * @param $accessType
309
+     * @param $accessValues
310
+     * @param $isAnonymous
311
+     * @param $hideNames
312
+     * @return RedirectResponse
313
+     */
314
+    public function updatePoll(
315
+        $pollId,
316
+        $pollType,
317
+        $pollTitle,
318
+        $pollDesc,
319
+        $userId,
320
+        $chosenDates,
321
+        $expireTs,
322
+        $accessType,
323
+        $accessValues,
324
+        $isAnonymous,
325
+        $hideNames
326
+    ) {
327
+        $event = $this->eventMapper->find($pollId);
328
+        $event->setTitle(htmlspecialchars($pollTitle));
329
+        $event->setDescription(htmlspecialchars($pollDesc));
330
+        $event->setIsAnonymous($isAnonymous ? 1 : 0);
331
+        $event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
332
+
333
+        if ($accessType === 'select') {
334
+            if (isset($accessValues)) {
335
+                $accessValues = json_decode($accessValues);
336
+                if ($accessValues !== null) {
337
+                    $groups = array();
338
+                    $users = array();
339
+                    if ($accessValues->groups !== null) {
340
+                        $groups = $accessValues->groups;
341
+                    }
342
+                    if ($accessValues->users !== null) {
343
+                        $users = $accessValues->users;
344
+                    }
345
+                    $accessType = '';
346
+                    foreach ($groups as $gid) {
347
+                        $accessType .= $gid . ';';
348
+                    }
349
+                    foreach ($users as $uid) {
350
+                        $accessType .= $uid . ';';
351
+                    }
352
+                }
353
+            }
354
+        }
355
+        $event->setAccess($accessType);
356
+
357
+        $chosenDates = json_decode($chosenDates);
358
+
359
+        $expire = null;
360
+        if ($expireTs !== null && $expireTs !== '') {
361
+            $expire = date('Y-m-d H:i:s', $expireTs + 60 * 60 * 24); //add one day, so it expires at the end of a day
362
+        }
363
+        $event->setExpire($expire);
364
+
365
+        $this->dateMapper->deleteByPoll($pollId);
366
+        $this->textMapper->deleteByPoll($pollId);
367
+        if ($pollType === 'event') {
368
+            $event->setType(0);
369
+            $this->eventMapper->update($event);
370
+            sort($chosenDates);
371
+            foreach ($chosenDates as $el) {
372
+                $date = new Date();
373
+                $date->setPollId($pollId);
374
+                $date->setDt(date('Y-m-d H:i:s', $el));
375
+                $this->dateMapper->insert($date);
376
+            }
377
+        } else {
378
+            $event->setType(1);
379
+            $this->eventMapper->update($event);
380
+            foreach ($chosenDates as $el) {
381
+                $text = new Text();
382
+                $text->setText($el);
383
+                $text->setPollId($pollId);
384
+                $this->textMapper->insert($text);
385
+            }
386
+        }
387
+        $url = $this->urlGenerator->linkToRoute('polls.page.index');
388
+        return new RedirectResponse($url);
389
+    }
390
+
391
+    /**
392
+     * @NoAdminRequired
393
+     * @NoCSRFRequired
394
+     */
395
+    public function createPoll() {
396
+        return new TemplateResponse('polls', 'create.tmpl',
397
+            ['userId' => $this->userId, 'userMgr' => $this->manager, 'urlGenerator' => $this->urlGenerator]);
398
+    }
399
+
400
+    /**
401
+     * @NoAdminRequired
402
+     * @NoCSRFRequired
403
+     * @param $pollType
404
+     * @param $pollTitle
405
+     * @param $pollDesc
406
+     * @param $userId
407
+     * @param $chosenDates
408
+     * @param $expireTs
409
+     * @param $accessType
410
+     * @param $accessValues
411
+     * @param $isAnonymous
412
+     * @param $hideNames
413
+     * @return RedirectResponse
414
+     */
415
+    public function insertPoll(
416
+        $pollType,
417
+        $pollTitle,
418
+        $pollDesc,
419
+        $userId,
420
+        $chosenDates,
421
+        $expireTs,
422
+        $accessType,
423
+        $accessValues,
424
+        $isAnonymous,
425
+        $hideNames
426
+    ) {
427
+        $secureRandom = new SecureRandom();
428
+        $event = new Event();
429
+        $event->setTitle(htmlspecialchars($pollTitle));
430
+        $event->setDescription(htmlspecialchars($pollDesc));
431
+        $event->setOwner($userId);
432
+        $event->setCreated(date('Y-m-d H:i:s'));
433
+        $event->setHash($secureRandom->generate(
434
+            16,
435
+            'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
436
+        ));
437
+        $event->setIsAnonymous($isAnonymous ? 1 : 0);
438
+        $event->setFullAnonymous($isAnonymous && $hideNames ? 1 : 0);
439
+
440
+        if ($accessType === 'select') {
441
+            if (isset($accessValues)) {
442
+                $accessValues = json_decode($accessValues);
443
+                if ($accessValues !== null) {
444
+                    $groups = array();
445
+                    $users = array();
446
+                    if ($accessValues->groups !== null) {
447
+                        $groups = $accessValues->groups;
448
+                    }
449
+                    if ($accessValues->users !== null) {
450
+                        $users = $accessValues->users;
451
+                    }
452
+                    $accessType = '';
453
+                    foreach ($groups as $gid) {
454
+                        $accessType .= $gid . ';';
455
+                    }
456
+                    foreach ($users as $uid) {
457
+                        $accessType .= $uid . ';';
458
+                    }
459
+                }
460
+            }
461
+        }
462
+        $event->setAccess($accessType);
463
+
464
+        $chosenDates = json_decode($chosenDates);
465
+
466
+        $expire = null;
467
+        if ($expireTs !== null && $expireTs !== '') {
468
+            $expire = date('Y-m-d H:i:s', $expireTs + 60 * 60 * 24); //add one day, so it expires at the end of a day
469
+        }
470
+        $event->setExpire($expire);
471
+
472
+        if ($pollType === 'event') {
473
+            $event->setType(0);
474
+            $ins = $this->eventMapper->insert($event);
475
+            $poll_id = $ins->getId();
476
+            sort($chosenDates);
477
+            foreach ($chosenDates as $el) {
478
+                $date = new Date();
479
+                $date->setPollId($poll_id);
480
+                $date->setDt(date('Y-m-d H:i:s', $el));
481
+                $this->dateMapper->insert($date);
482
+            }
483
+        } else {
484
+            $event->setType(1);
485
+            $ins = $this->eventMapper->insert($event);
486
+            $poll_id = $ins->getId();
487
+            $cnt = 1;
488
+            foreach ($chosenDates as $el) {
489
+                $text = new Text();
490
+                $text->setText($el . '_' . $cnt);
491
+                $text->setPollId($poll_id);
492
+                $this->textMapper->insert($text);
493
+                $cnt++;
494
+            }
495
+        }
496
+        $url = $this->urlGenerator->linkToRoute('polls.page.index');
497
+        return new RedirectResponse($url);
498
+    }
499
+
500
+    /**
501
+     * @NoAdminRequired
502
+     * @NoCSRFRequired
503
+     * @PublicPage
504
+     * @param $pollId
505
+     * @param $userId
506
+     * @param $types
507
+     * @param $dates
508
+     * @param $receiveNotifications
509
+     * @param $changed
510
+     * @return RedirectResponse
511
+     */
512
+    public function insertVote($pollId, $userId, $types, $dates, $receiveNotifications, $changed) {
513
+        if ($this->userId !== null) {
514
+            if ($receiveNotifications === 'true') {
515
+                try {
516
+                    //check if user already set notification for this poll
517
+                    $this->notificationMapper->findByUserAndPoll($pollId, $userId);
518
+                } catch (DoesNotExistException $e) {
519
+                    //insert if not exist
520
+                    $not = new Notification();
521
+                    $not->setUserId($userId);
522
+                    $not->setPollId($pollId);
523
+                    $this->notificationMapper->insert($not);
524
+                }
525
+            } else {
526
+                try {
527
+                    //delete if entry is in db
528
+                    $not = $this->notificationMapper->findByUserAndPoll($pollId, $userId);
529
+                    $this->notificationMapper->delete($not);
530
+                } catch (DoesNotExistException $e) {
531
+                    //doesn't exist in db, nothing to do
532
+                }
533
+            }
534
+        }
535
+        $poll = $this->eventMapper->find($pollId);
536
+        if ($changed === 'true') {
537
+            $dates = json_decode($dates);
538
+            $types = json_decode($types);
539
+            $count_dates = count($dates);
540
+            if ($poll->getType() == '0') {
541
+                $this->participationMapper->deleteByPollAndUser($pollId, $userId);
542
+            } else {
543
+                $this->participationTextMapper->deleteByPollAndUser($pollId, $userId);
544
+            }
545
+            for ($i = 0; $i < $count_dates; $i++) {
546
+                if ($poll->getType() == '0') {
547
+                    $part = new Participation();
548
+                    $part->setPollId($pollId);
549
+                    $part->setUserId($userId);
550
+                    $part->setDt(date('Y-m-d H:i:s', $dates[$i]));
551
+                    $part->setType($types[$i]);
552
+                    $this->participationMapper->insert($part);
553
+                } else {
554
+                    $part = new ParticipationText();
555
+                    $part->setPollId($pollId);
556
+                    $part->setUserId($userId);
557
+                    $part->setText($dates[$i]);
558
+                    $part->setType($types[$i]);
559
+                    $this->participationTextMapper->insert($part);
560
+                }
561
+
562
+            }
563
+            $this->sendNotifications($pollId, $userId);
564
+        }
565
+        $hash = $poll->getHash();
566
+        $url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]);
567
+        return new RedirectResponse($url);
568
+    }
569
+
570
+    /**
571
+     * @NoAdminRequired
572
+     * @NoCSRFRequired
573
+     * @PublicPage
574
+     * @param $pollId
575
+     * @param $userId
576
+     * @param $commentBox
577
+     * @return JSONResponse
578
+     */
579
+    public function insertComment($pollId, $userId, $commentBox) {
580
+        $comment = new Comment();
581
+        $comment->setPollId($pollId);
582
+        $comment->setUserId($userId);
583
+        $comment->setComment($commentBox);
584
+        $comment->setDt(date('Y-m-d H:i:s'));
585
+        $this->commentMapper->insert($comment);
586
+        $this->sendNotifications($pollId, $userId);
587
+        if ($this->manager->get($userId) !== null) {
588
+            $newUserId = $this->manager->get($userId)->getDisplayName();
589
+        } else {
590
+            $newUserId = $userId;
591
+        }
592
+        return new JSONResponse(array(
593
+            'comment' => $commentBox,
594
+            'date' => date('Y-m-d H:i:s'),
595
+            'userName' => $newUserId
596
+        ));
597
+    }
598
+
599
+    /**
600
+     * @NoAdminRequired
601
+     * @NoCSRFRequired
602
+     * @param $searchTerm
603
+     * @param $groups
604
+     * @param $users
605
+     * @return array
606
+     */
607
+        public function search($searchTerm, $groups, $users) {
608
+        return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users));
609
+    }
610
+
611
+    /**
612
+     * @NoAdminRequired
613
+     * @NoCSRFRequired
614
+     * @param $searchTerm
615
+     * @param $groups
616
+     * @return array
617
+     */
618
+        public function searchForGroups($searchTerm, $groups) {
619
+        $selectedGroups = json_decode($groups);
620
+        $groups = $this->groupManager->search($searchTerm);
621
+        $gids = array();
622
+        $sgids = array();
623
+        foreach ($selectedGroups as $sg) {
624
+            $sgids[] = str_replace('group_', '', $sg);
625
+        }
626
+        foreach ($groups as $g) {
627
+            $gids[] = $g->getGID();
628
+        }
629
+        $diffGids = array_diff($gids, $sgids);
630
+        $gids = array();
631
+        foreach ($diffGids as $g) {
632
+            $gids[] = ['gid' => $g, 'isGroup' => true];
633
+        }
634
+        return $gids;
635
+    }
636
+
637
+    /**
638
+     * @NoAdminRequired
639
+     * @NoCSRFRequired
640
+     * @param $searchTerm
641
+     * @param $users
642
+     * @return array
643
+     */
644
+        public function searchForUsers($searchTerm, $users) {
645
+        $selectedUsers = json_decode($users);
646
+        Util::writeLog("polls", print_r($selectedUsers, true), Util::ERROR);
647
+        $userNames = $this->userMgr->searchDisplayName($searchTerm);
648
+        $users = array();
649
+        $sUsers = array();
650
+        foreach ($selectedUsers as $su) {
651
+            $sUsers[] = str_replace('user_', '', $su);
652
+        }
653
+        foreach ($userNames as $u) {
654
+            $alreadyAdded = false;
655
+            foreach ($sUsers as &$su) {
656
+                if ($su === $u->getUID()) {
657
+                    unset($su);
658
+                    $alreadyAdded = true;
659
+                    break;
660
+                }
661
+            }
662
+            if (!$alreadyAdded) {
663
+                $users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false);
664
+            } else {
665
+                continue;
666
+            }
667
+        }
668
+        return $users;
669
+    }
670
+
671
+    /**
672
+     * @NoAdminRequired
673
+     * @NoCSRFRequired
674
+     * @param $username
675
+     * @return string
676
+     */
677
+        public function getDisplayName($username) {
678
+        return $this->manager->get($username)->getDisplayName();
679
+    }
680
+
681
+    /**
682
+     * @return Event[]
683
+     */
684
+        public function getPollsForUser() {
685
+        return $this->eventMapper->findAllForUser($this->userId);
686
+    }
687
+
688
+    /**
689
+     * @param $user
690
+     * @return Event[]
691
+     */
692
+        public function getPollsForUserWithInfo($user = null) {
693
+        if ($user === null) {
694
+            return $this->eventMapper->findAllForUserWithInfo($this->userId);
695
+        } else {
696
+            return $this->eventMapper->findAllForUserWithInfo($user);
697
+        }
698
+    }
699
+    /**
700
+     * @return array
701
+     */
702
+        public function getGroups() {
703
+        // $this->requireLogin();
704
+        if (class_exists('\OC_Group', true)) {
705
+            // Nextcloud <= 11, ownCloud
706
+            return \OC_Group::getUserGroups($this->userId);
707
+        }
708
+        // Nextcloud >= 12
709
+        $groups = \OC::$server->getGroupManager()->getUserGroups(\OC::$server->getUserSession()->getUser());
710
+        return array_map(function ($group) {
711
+            return $group->getGID();
712
+        }, $groups);
713
+    }
714
+
715
+    /**
716
+     * @param $poll
717
+     * @return bool
718
+     */
719
+    private function hasUserAccess($poll) {
720
+        $access = $poll->getAccess();
721
+        $owner = $poll->getOwner();
722
+        if ($access === 'public') {
723
+            return true;
724
+        }
725
+        if ($access === 'hidden') {
726
+            return true;
727
+        }
728
+        if ($this->userId === null) {
729
+            return false;
730
+        }
731
+        if ($access === 'registered') {
732
+            return true;
733
+        }
734
+        if ($owner === $this->userId) {
735
+            return true;
736
+        }
737
+        Util::writeLog("polls", $this->userId, Util::ERROR);
738
+        $user_groups = $this->getGroups();
739
+        $arr = explode(';', $access);
740
+        foreach ($arr as $item) {
741
+            if (strpos($item, 'group_') === 0) {
742
+                $grp = substr($item, 6);
743
+                foreach ($user_groups as $user_group) {
744
+                    if ($user_group === $grp) {
745
+                        return true;
746
+                    }
747
+                }
748
+            } else {
749
+                if (strpos($item, 'user_') === 0) {
750
+                    $usr = substr($item, 5);
751
+                    if ($usr === User::getUser()) {
752
+                        return true;
753
+                    }
754
+                }
755
+            }
756
+        }
757
+        return false;
758
+    }
759 759
 }
Please login to merge, or discard this patch.
lib/Db/Comment.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -34,8 +34,8 @@
 block discarded – undo
34 34
  * @method void setPollId(integer $value)
35 35
  */
36 36
 class Comment extends Model {
37
-	protected $userId;
38
-	protected $dt;
39
-	protected $comment;
40
-	protected $pollId;
37
+    protected $userId;
38
+    protected $dt;
39
+    protected $comment;
40
+    protected $pollId;
41 41
 }
Please login to merge, or discard this patch.
lib/Db/Date.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -30,6 +30,6 @@
 block discarded – undo
30 30
  * @method void setPollId(integer $value)
31 31
  */
32 32
 class Date extends Model {
33
-	protected $dt;
34
-	protected $pollId;
33
+    protected $dt;
34
+    protected $pollId;
35 35
 }
Please login to merge, or discard this patch.
lib/Db/Model.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -26,14 +26,14 @@
 block discarded – undo
26 26
 use OCP\AppFramework\Db\Entity;
27 27
 
28 28
 class Model extends Entity {
29
-	/**
30
-	 * FactoryMuffin checks for the existence of setters with method_exists($obj, $attr) but that returns false.
31
-	 * By overwriting the __set() magic method we can trigger the changed flag on $obj->attr assignment.
32
-	 *
33
-	 * @param $name
34
-	 * @param $value
35
-	 */
36
-	public function __set($name, $value) {
37
-		$this->setter($name, [$value]);
38
-	}
29
+    /**
30
+     * FactoryMuffin checks for the existence of setters with method_exists($obj, $attr) but that returns false.
31
+     * By overwriting the __set() magic method we can trigger the changed flag on $obj->attr assignment.
32
+     *
33
+     * @param $name
34
+     * @param $value
35
+     */
36
+    public function __set($name, $value) {
37
+        $this->setter($name, [$value]);
38
+    }
39 39
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -34,6 +34,6 @@
 block discarded – undo
34 34
 	 * @param $value
35 35
 	 */
36 36
 	public function __set($name, $value) {
37
-		$this->setter($name, [$value]);
37
+		$this->setter($name, [ $value ]);
38 38
 	}
39 39
 }
Please login to merge, or discard this patch.
lib/Db/Event.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -46,14 +46,14 @@
 block discarded – undo
46 46
  * @method void setFullAnonymous(integer $value)
47 47
  */
48 48
 class Event extends Model {
49
-	protected $type;
50
-	protected $title;
51
-	protected $description;
52
-	protected $owner;
53
-	protected $created;
54
-	protected $access;
55
-	protected $expire;
56
-	protected $hash;
57
-	protected $isAnonymous;
58
-	protected $fullAnonymous;
49
+    protected $type;
50
+    protected $title;
51
+    protected $description;
52
+    protected $owner;
53
+    protected $created;
54
+    protected $access;
55
+    protected $expire;
56
+    protected $hash;
57
+    protected $isAnonymous;
58
+    protected $fullAnonymous;
59 59
 }
Please login to merge, or discard this patch.
lib/Db/Participation.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -34,8 +34,8 @@
 block discarded – undo
34 34
  * @method void setType(integer $value)
35 35
  */
36 36
 class Participation extends Model {
37
-	protected $dt;
38
-	protected $userId;
39
-	protected $pollId;
40
-	protected $type;
37
+    protected $dt;
38
+    protected $userId;
39
+    protected $pollId;
40
+    protected $type;
41 41
 }
Please login to merge, or discard this patch.
lib/Db/Text.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -30,6 +30,6 @@
 block discarded – undo
30 30
  * @method void setPollId(integer $value)
31 31
  */
32 32
 class Text extends Model {
33
-	protected $text;
34
-	protected $pollId;
33
+    protected $text;
34
+    protected $pollId;
35 35
 }
Please login to merge, or discard this patch.
lib/Db/ParticipationText.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -34,8 +34,8 @@
 block discarded – undo
34 34
  * @method void setType(integer $value)
35 35
  */
36 36
 class ParticipationText extends Model {
37
-	protected $text;
38
-	protected $userId;
39
-	protected $pollId;
40
-	protected $type;
37
+    protected $text;
38
+    protected $userId;
39
+    protected $pollId;
40
+    protected $type;
41 41
 }
Please login to merge, or discard this patch.