forked from bleistivt/Plugin-DiscussionPolls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.discussionpolls.plugin.php
More file actions
785 lines (697 loc) · 27 KB
/
Copy pathclass.discussionpolls.plugin.php
File metadata and controls
785 lines (697 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
<?php if(!defined('APPLICATION')) exit();
/* Copyright 2013-2014 Zachary Doll
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$PluginInfo['DiscussionPolls'] = array(
'Name' => 'Discussion Polls',
'Description' => 'A plugin that allows creating polls that attach to a discussion. Respects permissions.',
'Version' => '1.3.6',
'RegisterPermissions' => array('Plugins.DiscussionPolls.Add', 'Plugins.DiscussionPolls.View', 'Plugins.DiscussionPolls.Vote', 'Plugins.DiscussionPolls.Manage'),
'MobileFriendly' => TRUE,
'SettingsUrl' => '/dashboard/settings/discussionpolls',
'SettingsPermission' => 'Garden.Settings.Manage',
'Author' => 'Zachary Doll',
'AuthorEmail' => 'hgtonight@daklutz.com ',
'AuthorUrl' => 'http://www.daklutz.com',
'License' => 'GPLv3'
);
class DiscussionPollsPlugin extends Gdn_Plugin {
public function __construct() {
parent::__construct();
}
/**
* Creates a settings page at /dashboard/settins/discussionpolls
* @param VanillaController $Sender SettingsController
*/
public function SettingsController_DiscussionPolls_Create($Sender) {
$Sender->Permission('Garden.Settings.Manage');
$Sender->AddCSSFile('settings.discussionpolls.css', 'plugins/DiscussionPolls');
$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array('Plugins.DiscussionPolls.EnableShowResults'));
$ConfigurationModel->SetField(array('Plugins.DiscussionPolls.DisablePollTitle'));
$Sender->Form->SetModel($ConfigurationModel);
if($Sender->Form->AuthenticatedPostBack() === FALSE) {
$Sender->Form->SetData($ConfigurationModel->Data);
}
else {
if($Sender->Form->Save() !== FALSE) {
$Sender->InformMessage('<span class="InformSprite Sliders"></span>' . T('Your changes have been saved.'), 'HasSprite');
}
}
// Makes it look like a dashboard page
$Sender->AddSideMenu('/dashboard/settings/discussionpolls');
$Sender->Title('Discussion Polls Settings');
$Sender->Render($this->ThemeView('settings'));
}
/**
* Creates a Poll method on the discussion controller
* I use it as a mini-controller with it's own methods
* @param VanillaController $Sender DiscussionController
*/
public function DiscussionController_Poll_Create($Sender) {
$this->Dispatch($Sender, $Sender->RequestArgs);
}
/**
* Default action on /discussion/poll is not found
* @param VanillaController $Sender DiscussionController
*/
public function Controller_Index($Sender) {
//shift request args for implied method
array_unshift($Sender->RequestArgs, NULL);
$this->Controller_Results($Sender);
}
/**
* Used to submit a poll vote via form
* @param VanillaController $Sender DiscussionController
*/
public function Controller_Submit($Sender) {
$Session = Gdn::Session();
$FormPostValues = $Sender->Form->FormValues();
// not submitting anything
if($Sender->Form->AuthenticatedPostBack() === FALSE || !GetValue('DiscussionID', $FormPostValues)) {
// throw permission exception
throw PermissionException();
}
else {
// You have to have voting privilege only
if(!$Session->CheckPermission('Plugins.DiscussionPolls.Vote', FALSE) || !$Session->UserID) {
Gdn::Session()->Stash('DiscussionPollsMessage', T('Plugins.DiscussionPolls.UnableToSubmit', 'You do not have permission to submit a poll.'));
Redirect('discussion/' . $FormPostValues['DiscussionID']);
}
$DPModel = new DiscussionPollsModel();
if(!$DPModel->CheckFullyAnswered($FormPostValues)) {
//save partial answers
$Partial = $DPModel->SavePartialAnswer($FormPostValues, $Session->UserID);
}
else {
$Saved = $DPModel->SaveAnswer($FormPostValues, $Session->UserID);
}
// Return the proper view
if($Sender->DeliveryType() == DELIVERY_TYPE_VIEW) {
// Used for AJAX poll submission returns the results
$Poll = $DPModel->GetByDiscussionID($FormPostValues['DiscussionID']);
if($Saved) {
$Results = $this->_RenderResults($Poll, FALSE);
$Type = 'Full Poll';
}
else {
$Results = T('Plugins.DiscussionPolls.SavedPartial', 'We have saved your completed poll questions.');
$Type = 'Partial Poll';
}
$Data = array('html' => $Results, 'type' => $Type);
echo json_encode($Data);
}
else {
if($Saved) {
// Don't stash any message
}
else if($Partial) {
Gdn::Session()->Stash('DiscussionPollsMessage', T('Plugins.DiscussionPolls.UnsweredAllQuestions', 'You have not answered all questions!'));
}
else {
Gdn::Session()->Stash('DiscussionPollsMessage', T('Plugins.DiscussionPolls.UnsweredUnable', 'Unable to save!'));
}
Redirect('discussion/' . $FormPostValues['DiscussionID']);
}
}
}
/**
* Renders the results of a poll either full page for legacy users
* or as a partial for frontend ajax
* @param VanillaController $Sender DiscussionController
*/
public function Controller_Results($Sender) {
$DPModel = new DiscussionPollsModel();
$Poll = $DPModel->GetID($Sender->RequestArgs[1]);
$PollResults = $this->_RenderResults($Poll, FALSE);
if($Sender->DeliveryType() == DELIVERY_TYPE_VIEW) {
$Data = array('html' => $PollResults);
echo json_encode($Data);
}
else {
$Sender->SetData('PollString', $PollResults);
$Sender->Render($this->ThemeView('poll'));
}
}
/*
* Renders the results of deleting a poll
* This will only be seen on legacy systems without JS
* @param VanillaController $Sender DiscussionController
*/
public function Controller_Delete($Sender) {
$Session = Gdn::Session();
$DPModel = new DiscussionPollsModel();
$DiscussionModel = new DiscussionModel();
$Poll = $DPModel->GetID($Sender->RequestArgs[1]);
$Discussion = $DiscussionModel->GetID($Poll->DiscussionID);
$PollOwnerID = $Discussion->InsertUserID;
if($Session->CheckPermission('Plugins.DiscussionPolls.Manage') || $PollOwnerID == $Session->UserID) {
$DPModel = new DiscussionPollsModel();
$DPModel->DeleteID($Sender->RequestArgs[1]);
$Result = 'Removed poll with id ' . $Sender->RequestArgs[1];
if($Sender->DeliveryType() == DELIVERY_TYPE_VIEW) {
$Data = array('html' => $Result);
echo json_encode($Data);
}
else {
$Sender->SetData('PollString', $Result);
$Sender->Render($this->ThemView('poll'));
}
}
else {
// throw permission exception
throw PermissionException();
}
}
/**
* Add frontend css and js to the discussion controller
* @param VanillaController $Sender DiscussionController
*/
public function DiscussionController_Render_Before($Sender) {
// Add poll voting resources
$Sender->AddJsFile('discussionpolls.js', 'plugins/DiscussionPolls');
$Sender->AddCSSFile('discussionpolls.css', 'plugins/DiscussionPolls');
$Sender->AddDefinition('DP_ShowResults', T('Show Results'));
$Sender->AddDefinition('DP_ShowForm', T('Show Poll Form'));
$Sender->AddDefinition('DP_ConfirmDelete', T('Are you sure you want to delete this poll?'));
//check for any stashed messages from poll submit
$Message = Gdn::Session()->Stash('DiscussionPollsMessage');
if($Message) {
//inform
Gdn::Controller()->InformMessage($Message);
//pass to form error
$Sender->SetData('DiscussionPollsMessage', $Message);
}
}
/**
* Add backend css and js to the discussion controller
* @param VanillaController $Sender PostController
*/
public function PostController_Render_Before($Sender) {
$Session = Gdn::Session();
// Make sure we can add/manage polls
if(!$Session->CheckPermission(array('Plugins.DiscussionPolls.Add', 'Plugins.DiscussionPolls.Manage'), FALSE)) {
// don't render poll form at all
return;
}
// Add poll creation resources
$Sender->AddJsFile('admin.discussionpolls.js', 'plugins/DiscussionPolls');
$Sender->AddCSSFile('admin.discussionpolls.css', 'plugins/DiscussionPolls');
//get question template for jquery poll expansion
$DefaultQuestionString = $this->_RenderQuestionFields($Sender->Form, FALSE);
$Sender->AddDefinition('DP_EmptyQuestion', $DefaultQuestionString);
// Translated definitions
$Sender->AddDefinition('DP_NextQuestion', T('Next Question'));
$Sender->AddDefinition('DP_PrevQuestion', T('Previous Question'));
}
/**
* Insert poll in first post of discussion in 2.0.x
* @param VanillaController $Sender DiscussionController
*/
public function DiscussionController_AfterCommentBody_Handler($Sender) {
// Make sure event argument type is Discussion
if($Sender->EventArguments['Type'] == 'Discussion') {
$this->_PollInsertion($Sender);
}
}
/**
* Insert poll in first post of discussion in 2.1b1
* @param VanillaController $Sender DiscussionController
*/
public function DiscussionController_AfterDiscussionBody_Handler($Sender) {
$this->_PollInsertion($Sender);
}
/**
* Render the poll admin form on the add/edit discussion page in 2.x
* @param VanillaController $Sender PostController
*/
public function PostController_DiscussionFormOptions_Handler($Sender) {
$Session = Gdn::Session();
// Make sure we can add/manage polls
if(!$Session->CheckPermission(array('Plugins.DiscussionPolls.Add', 'Plugins.DiscussionPolls.Manage'), FALSE)) {
// don't render poll form at all
return;
}
// render check box
$Sender->EventArguments['Options'] .= '<li>' . $Sender->Form->CheckBox('DP_Attach', T('Attach Poll'), array('value' => '1', 'checked' => TRUE)) . '</li>';
// Load up existing poll data
if(GetValueR('Discussion.DiscussionID', $Sender)) {
$DID = $Sender->Discussion->DiscussionID;
}
else {
$DID = NULL;
}
$DPModel = new DiscussionPollsModel();
$DiscussionPoll = $DPModel->GetByDiscussionID($DID);
// If there is existing poll data, disable editing
// Editing will be in a future release
if(!empty($DiscussionPoll->PollID)) {
$Closed = TRUE;
$Disabled = array('disabled' => 'true');
echo Wrap(T('Plugins.DiscussionPolls.PollClosedToEdits', 'You cannot edit a poll. You <em>may</em> delete this poll by unchecking the Attach Poll checkbox.'), 'div', array('class' => 'Messages Warning'));
}
else {
$Disabled = array();
$Closed = FALSE;
}
$Sender->AddDefinition('DP_Closed', $Closed);
// The opening of the form
$Sender->Form->SetValue('DP_Title', $DiscussionPoll->Title);
//render form
DPRenderQuestionForm($Sender->Form, $DiscussionPoll, $Disabled, $Closed);
}
/**
* Validate the poll fields before we save so we can inform the user
* without saving the discussion
* @param VanillaModel $Sender DiscussionModel
* @return boolean Whether or not validation was successful
*/
public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
$FormPostValues = GetValue('FormPostValues', $Sender->EventArguments, array());
if(!GetValue('DP_Attach', $FormPostValues)) {
// No need to validate
return FALSE;
}
// Only validate new polls
// TODO: Remove this when poll editing becomes a thing
$DiscussionID = GetValue('DiscussionID', $Sender->EventArguments, 0);
$DPModel = new DiscussionPollsModel();
if($DPModel->Exists($DiscussionID)) {
return FALSE;
}
// Validate that all poll fields are filled out
$Invalid = FALSE;
$Error = '';
if(!C('Plugins.DiscussionPolls.DisablePollTitle', FALSE) && trim($FormPostValues['DP_Title']) == FALSE) {
$Invalid = TRUE;
$Error = 'You must enter a valid poll title!';
}
// validate each question
if(!$Invalid) {
foreach($FormPostValues['DP_Questions'] as $QIndex => $Question) {
if(trim($Question) == FALSE) {
// check to see if all the options are also blank
foreach($FormPostValues['DP_Options' . $QIndex] as $Option) {
if(trim($Option) != FALSE) {
$Invalid = TRUE;
$Error = 'You must enter valid text for question #' . ($QIndex + 1);
}
}
if($Invalid === FALSE) {
// remove the question
unset($Sender->EventArguments['FormPostValues']['DP_Questions'][$QIndex]);
// unsetting the options will prevent any more questions from being added
unset($Sender->EventArguments['FormPostValues']['DP_Options' . $QIndex]);
}
break;
}
else {
$OptionCount = 0;
foreach($FormPostValues['DP_Options' . $QIndex] as $OIndex => $Option) {
if(trim($Option) == FALSE) {
// unset that option
unset($Sender->EventArguments['FormPostValues']['DP_Options' . $QIndex][$OIndex]);
}
else {
$OptionCount++;
}
}
if($OptionCount < 2) {
$Invalid = TRUE;
$Error = 'You must enter at least two valid options for question #' . ($QIndex + 1);
break;
}
}
}
}
if($Invalid) {
$Error = Wrap('Error', 'h1') . Wrap($Error, 'p');
// should prevent the discussion from being saved
die($Error);
}
return TRUE;
}
/**
* Save poll when saving a discussion
* @param VanillaModel $Sender DiscussionModel
* @return boolean if the poll was saved
*/
public function DiscussionModel_AfterSaveDiscussion_Handler($Sender) {
// Needed no matter what
$DPModel = new DiscussionPollsModel();
$Session = Gdn::Session();
// Make sure we can add/manage polls
if(!$Session->CheckPermission(array('Plugins.DiscussionPolls.Add', 'Plugins.DiscussionPolls.Manage'), FALSE)) {
// this should only be shown to users that are mucking with the system
// Gdn::Controller()->InformMessage(T('Plugins.DiscussionPolls.UnableToEdit', 'You do not have permission to edit a poll.'));
return FALSE;
}
// Don't trust the discussion ID implicitly
$DiscussionID = GetValue('DiscussionID', $Sender->EventArguments, 0);
if($DiscussionID == 0) {
$Error = Wrap('Error', 'h1') . Wrap('Invalid discussion id', 'p');
return FALSE;
}
$FormPostValues = GetValue('FormPostValues', $Sender->EventArguments, array());
// Unchecking the poll option will remove the poll
if(!GetValue('DP_Attach', $FormPostValues)) {
return FALSE;
}
if($DPModel->Exists($DiscussionID)) {
// Skip saving if a poll exists
Gdn::Controller()->InformMessage(T('Plugins.DiscussionPolls.AlreadyExists', 'This poll already exists, poll was not updated'));
return FALSE;
}
// Check to see if there are already poll responses; exit
if($DPModel->HasResponses($DiscussionID) &&
!$Session->CheckPermission('Plugins.DiscussionPolls.Manage')) {
Gdn::Controller()->InformMessage(T('Plugins.DiscussionPolls.UnableToEditAfterResponses', 'You do not have permission to edit a poll with responses.'));
return FALSE;
}
// Validate that all poll fields are filled out
$Invalid = FALSE;
$Error = '';
if(trim($FormPostValues['DP_Title']) == FALSE && !C('Plugins.DiscussionPolls.DisablePollTitle', FALSE)) {
$Invalid = TRUE;
$Error = 'You must enter a valid poll title!';
}
foreach($FormPostValues['DP_Questions'] as $Index => $Question) {
if(trim($Question) == FALSE) {
$Invalid = TRUE;
$Error = 'You must enter valid question text!';
break;
}
foreach($FormPostValues['DP_Options' . $Index] as $Option) {
if(trim($Option) == FALSE) {
$Invalid = TRUE;
$Error = 'You must enter valid option text!';
break;
}
}
}
if($Invalid) {
// fail silently since this shouldn't happen
$Error = Wrap('Error', 'h1') . Wrap($Error, 'p');
return FALSE;
}
else {
// save poll form fields
$DPModel->Save($FormPostValues);
return TRUE;
}
}
/**
* Remove attached poll when discussion is deleted
* @param VanillaModel $Sender DiscussionModel
*/
public function DiscussionModel_DeleteDiscussion_Handler($Sender) {
// Get discussionID that is being deleted
$DiscussionID = $Sender->EventArguments['DiscussionID'];
// Delete via model
$DPModel = new DiscussionPollsModel();
$DPModel->DeleteByDiscussionID($DiscussionID);
}
/**
* Determines what part of the poll (if any) needs to be rendered
* Checks permissions and displays any tools available to user
* @param VanillaController $Sender
* @return type
*/
protected function _PollInsertion($Sender) {
$Discussion = $Sender->Discussion;
$Session = Gdn::Session();
$DPModel = new DiscussionPollsModel();
// Does an attached poll exist?
if($DPModel->Exists($Discussion->DiscussionID)) {
$Results = FALSE;
$Closed = FALSE;
$Poll = $DPModel->GetByDiscussionID($Discussion->DiscussionID);
// Can the current user view polls?
if(!$Session->CheckPermission('Plugins.DiscussionPolls.View')) {
// make this configurable?
echo Wrap(T('Plugins.DiscussionPolls.NoView', 'You do not have permission to view polls.'), 'div', array('class' => 'DP_AnswerForm'));
return;
}
// Check to see if the discussion is closed
if($Discussion->Closed) {
// Close the Poll if the discussion is closed (workaround)
$DPModel->Close($Discussion->DiscussionID);
// TODO: Get rid of workaround by finding _some way_ to hook into the discussion model
// and close/open the poll **only** when the attached discussion is [un]closed.
$Closed = TRUE;
}
// Has the user voted?
if($DPModel->HasAnswered($Poll->PollID, $Session->UserID) || !$Session->IsValid() || $Closed) {
$Results = TRUE;
// Render results
$this->_RenderResults($Poll);
}
else {
$PartialAnswers = $DPModel->PartialAnswer($Poll->PollID, $Session->UserID);
//if some saved partial answers inform
if(!empty($PartialAnswers)) {
// TODO: Remove?
Gdn::Controller()->InformMessage(T('Plugins.DiscussionPolls.LoadedPartial', 'Your answered questions have been loaded.'));
}
// Render the submission form
$this->_RenderVotingForm($Sender, $Poll, $PartialAnswers);
}
// Render poll tools
// Owner and Plugins.DiscussionPolls.Manage gets delete if exists and attach if it doesn't
// Plugins.DiscussionPolls.View gets show results if the results aren't shown
$Tools = '';
if($Discussion->InsertUserID == $Session->UserID || $Session->CheckPermission('Plugins.DiscussionPolls.Manage')) {
$Tools .= Wrap(
Anchor(T('Delete Poll'), '/discussion/poll/delete/' . $Poll->PollID), 'li', array('id' => 'DP_Remove')
);
}
if(!$Results && C('Plugins.DiscussionPolls.EnableShowResults', TRUE)) {
$Tools .= Wrap(
Anchor(T('Show Results'), '/discussion/poll/results/' . $Poll->PollID), 'li', array('id' => 'DP_Results')
);
}
echo WrapIf($Tools, 'ul', array('id' => 'DP_Tools'));
}
else {
// Poll does not exist
if($Discussion->InsertUserID == $Session->UserID || $Session->CheckPermission('Plugins.DiscussionPolls.Manage')) {
echo Wrap(
Wrap(
Anchor(T('Attach Poll'), '/vanilla/post/editdiscussion/' . $Discussion->DiscussionID), 'li'), 'ul', array('id' => 'DP_Tools')
);
}
}
}
/**
* Renders a poll object as results
* @param stdClass $Poll the poll object we are rendering
* @param boolean $Echo echo or return result string
* @return mixed Will return string if $Echo is false, will return true otherwise
*/
protected function _RenderResults($Poll, $Echo = TRUE) {
include($this->ThemeView('results'));
if($Echo) {
DPRenderResults($Poll);
return TRUE;
}
else {
ob_start();
DPRenderResults($Poll);
$Result = ob_get_contents();
ob_end_clean();
return $Result;
}
}
/**
* Renders / fetches question fields for form
* @param stdClass $PollForm the poll object we are rendering
* @param boolean $Echo echo or return result string
* @return mixed Will return string if $Echo is false, will return true otherwise
*/
protected function _RenderQuestionFields($PollForm, $Echo = TRUE) {
include_once($this->ThemeView('questions'));
if($Echo) {
DPRenderQuestionField($PollForm);
return TRUE;
}
else {
ob_start();
DPRenderQuestionField($PollForm);
$Result = ob_get_contents();
ob_end_clean();
return $Result;
}
}
/**
* Renders a voting form for a poll object
* @param VanillaController $Sender controller object
* @param stdClass $Poll poll object
* @param boolean $Echo echo or return result string
* @return mixed Will return string if $Echo is false, will return true otherwise
*/
protected function _RenderVotingForm($Sender, $Poll, $PartialAnswers, $Echo = TRUE) {
$Sender->PollForm = new Gdn_Form();
$Sender->PollForm->AddHidden('DiscussionID', $Poll->DiscussionID);
$Sender->PollForm->AddHidden('PollID', $Poll->PollID);
if($Sender->Data('DiscussionPollsMessage')) {
$Sender->PollForm->AddError($Sender->Data('DiscussionPollsMessage'));
}
include_once($this->ThemeView('voting'));
if($Echo) {
DiscussionPollAnswerForm($Sender->PollForm, $Poll, $PartialAnswers);
return TRUE;
}
else {
ob_start();
DiscussionPollAnswerForm($Sender->PollForm, $Poll, $PartialAnswers);
$Result = ob_get_contents();
ob_end_clean();
return $Result;
}
}
/*
* Set view that can be copied over to current theme
* e.g. view -> current_theme/views/plugins/DiscussionPolls/view.php
* @param View name of the view
*/
public function ThemeView($View) {
$ThemeViewLoc = CombinePaths(array(
PATH_THEMES, Gdn::Controller()->Theme, 'views', $this->GetPluginFolder(FALSE)
));
if(file_exists($ThemeViewLoc . DS . $View . '.php')) {
$View = $ThemeViewLoc . DS . $View . '.php';
}
else {
$View = Gdn::controller()->fetchViewLocation($View, '', 'plugins/DiscussionPolls');
}
return $View;
}
/**
* Setup database structure for model
*/
public function Structure() {
$Database = Gdn::Database();
$Construct = $Database->Structure();
$Construct->Table('DiscussionPolls');
$Construct
->PrimaryKey('PollID')
->Column('DiscussionID', 'int', FALSE, 'key')
->Column('Text', 'varchar(140)', TRUE)
->Column('Open', 'tinyint(1)', '1')
->Set();
$Construct->Table('DiscussionPollQuestions');
$Construct
->PrimaryKey('QuestionID')
->Column('PollID', 'int', FALSE, 'key')
->Column('Text', 'varchar(140)')
->Column('CountResponses', 'int', '0')
->Set();
$Construct->Table('DiscussionPollQuestionOptions');
$Construct
->PrimaryKey('OptionID')
->Column('QuestionID', 'int', FALSE, 'key')
->Column('PollID', 'int', FALSE, 'key')
->Column('Text', 'varchar(140)')
->Column('CountVotes', 'int', '0')
->Set();
$Construct->Table('DiscussionPollAnswers');
$Construct
->PrimaryKey('AnswerID')
->Column('PollID', 'int', FALSE, 'key')
->Column('QuestionID', 'int', FALSE, 'key')
->Column('UserID', 'int', FALSE, 'key')
->Column('OptionID', 'int', TRUE, 'key')
->Set();
$Construct->Table('DiscussionPollAnswerPartial');
$Construct
->Column('PollID', 'int', FALSE, 'index.1') // multicolumn for quick lookup
->Column('QuestionID', 'int', FALSE)
->Column('UserID', 'int', FALSE, 'index.1')
->Column('OptionID', 'int', FALSE)
->Set();
}
/**
* This is executed when enabled via the dashboard
* It sets up the database and permissions used
*/
public function Setup() {
// Register permissions
$PermissionModel = Gdn::PermissionModel();
$PermissionModel->Define(
array(
'Plugins.DiscussionPolls.Add',
'Plugins.DiscussionPolls.View' => 1,
'Plugins.DiscussionPolls.Vote',
'Plugins.DiscussionPolls.Manage'
));
// Set initial guest permissions.
$PermissionModel->Save(array(
'Role' => 'Guest',
'Plugins.DiscussionPolls.View' => 1
));
// Set initial confirm email permissions.
$PermissionModel->Save(array(
'Role' => 'Confirm Email',
'Plugins.DiscussionPolls.View' => 1
));
// Set initial applicant permissions.
$PermissionModel->Save(array(
'Role' => 'Applicant',
'Plugins.DiscussionPolls.View' => 1
));
// Set initial member permissions.
$PermissionModel->Save(array(
'Role' => 'Member',
'Plugins.DiscussionPolls.Add' => 1,
'Plugins.DiscussionPolls.View' => 1,
'Plugins.DiscussionPolls.Vote' => 1
));
// Set initial moderator permissions.
$PermissionModel->Save(array(
'Role' => 'Moderator',
'Plugins.DiscussionPolls.Add' => 1,
'Plugins.DiscussionPolls.View' => 1,
'Plugins.DiscussionPolls.Vote' => 1,
'Plugins.DiscussionPolls.Manage' => 1
));
// Set initial admininstrator permissions.
$PermissionModel->Save(array(
'Role' => 'Administrator',
'Plugins.DiscussionPolls.Add' => 1,
'Plugins.DiscussionPolls.View' => 1,
'Plugins.DiscussionPolls.Vote' => 1,
'Plugins.DiscussionPolls.Manage' => 1
));
// Set up the db structure
$this->Structure();
}
/**
* Run when a plugin is disabled via dashboard
* Right now it only removes permissions on 2.1b1+
*/
public function OnDisable() {
// Deregister permissions (only in 2.1+)
if(version_compare(APPLICATION_VERSION, '2.1b1', '>=')) {
$PermissionModel = Gdn::PermissionModel();
$PermissionModel->Undefine(
array(
'Plugins.DiscussionPolls.Add',
'Plugins.DiscussionPolls.View',
'Plugins.DiscussionPolls.Vote',
'Plugins.DiscussionPolls.Manage'
));
}
}
}