Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_poll.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
20 require_once SQ_FUDGE_PATH.'/general/text.inc';
21 require_once SQ_FUDGE_PATH.'/general/datetime.inc';
22 
36 class Page_Poll extends Page
37 {
38 
39 
46  function __construct($assetid=0)
47  {
48  $this->_ser_attrs = TRUE;
49  parent::__construct($assetid);
50 
51  }//end constructor
52 
53 
64  function _createAdditional(&$link)
65  {
66  if (!parent::_createAdditional($link)) return FALSE;
67 
68  // add a bodycopy to this page when creating
69  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy');
70  $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
71 
72  $folders = Array('vote_formats','results_formats','past_questions_formats');
73  $type = 'folder';
74 
75  $formats = Array();
76  foreach ($folders as $name) {
77  $asset = new $type();
78  $copy_link = Array('asset' => &$this, 'value' => $name ,'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
79 
80  $asset->setAttrValue('name', ucwords(str_replace('_',' ', $name)));
81  if (!$asset->create($copy_link)) return FALSE;
82 
83  $formats[$name] = $asset;
84 
85  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
86  unset($asset);
87  }
88 
89  //-- Create Bodycopies --/
90  $bodycopies = Array();
91  $bodycopies['question_format'] = 'vote_formats';
92  $bodycopies['option_list_format'] = 'vote_formats';
93  $bodycopies['results_format'] = 'results_formats';
94  $bodycopies['results_list_format'] = 'results_formats';
95  $bodycopies['past_questions_format'] = 'past_questions_formats';
96  $bodycopies['past_questions_list_format'] = 'past_questions_formats';
97 
98  foreach ($bodycopies as $value => $folder_asset) {
99  $asset = new Bodycopy();
100  $copy_link = Array('asset' => &$formats[$folder_asset], 'value' => $value ,'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
101  $asset->setAttrValue('name', ucwords(str_replace('_',' ',$value)));
102  if (!$asset->create($copy_link)) return FALSE;
103  }
104 
105  return TRUE;
106 
107  }//end _createAdditional()
108 
109 
130  function prepareLink(&$asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
131  {
132  $return_value = FALSE;
133 
134  // if this is a poll_question then we need to make it SQ_LINK_TYPE_2
135  if ($side_of_link == 'major' &&( $asset instanceof poll_question)) {
136  if ($link_type != SQ_LINK_TYPE_2) {
137  $link_type = SQ_LINK_TYPE_2;
138  $return_value = TRUE;
139  }
140  }
141 
142  return $return_value;
143 
144  }//end prepareLink()
145 
146 
154  function _getAllowedLinks()
155  {
156  $page_links = parent::_getAllowedLinks();
157  $page_links[SQ_LINK_TYPE_2]['folder'] = Array('card' => 'M', 'exclusive' => FALSE);
158  $page_links[SQ_LINK_TYPE_2]['poll_question'] = Array('card' => 'M', 'exclusive' => FALSE);
159 
160  $page_links[SQ_LINK_NOTICE]['asset'] = Array('card' => 'M', 'exclusive' => FALSE);
161  $page_links[SQ_LINK_TYPE_2]['bodycopy'] = Array('card' => 3, 'exclusive' => FALSE);
162  return $page_links;
163 
164  }//end _getAllowedLinks()
165 
166 
176  function printBody()
177  {
178  // declaration
179  $active_question_id = $this->attr('active_question');
180  $format_type = 'question_format';
181  $prefix = $this->getPrefix();
182  $can_vote = FALSE;
183  $am = $GLOBALS['SQ_SYSTEM']->am;
184 
185  // validation
186  $valid = TRUE;
187  if ($active_question_id) {
188  $active_question = $GLOBALS['SQ_SYSTEM']->am->getAsset($active_question_id, '', TRUE);
189 
190  // alias question to active_question
191  $question =& $active_question;
192  if (is_null($question)) $valid = FALSE;
193 
194  } else {
195  $valid = FALSE;
196  }
197 
198  if (!$valid) {
199  // active question not found
200  return;
201  }
202 
203 
204  // if a previous question is selected...
205  if (array_get_index($_GET, 'SQ_POLL_ID') && array_get_index($_GET, 'SQ_ACTION')) {
206  $SQ_POLL_ID = (int) $_GET['SQ_POLL_ID'];
207  $SQ_ACTION = $_GET['SQ_ACTION'];
208 
209  // if the POLL_ID is valid, and we're viewing the results page,
210  if (is_integer($SQ_POLL_ID) && $SQ_POLL_ID > 0 && $SQ_ACTION == $prefix.'_results') {
211 
212  // grab the question by the poll id
213  $question = $GLOBALS['SQ_SYSTEM']->am->getAsset($SQ_POLL_ID, '', TRUE);
214 
215  // if the question doesn't have an ID, or isn't of type poll_question, or isn't a child of the poll
216  if (!$question->id || !$am->getAssetTypeInfo(Array($question->id), Array('poll_question'), TRUE) || $question->status < SQ_STATUS_LIVE || !$am->getLinkByAsset($this->id, $question->id, SQ_LINK_TYPE_2)) {
217  // revert to the default (active_question)
218  $question = $active_question;
219  }
220 
221  }
222  }
223  // end validation
224 
225  // default view, above determines if they can vote
226  $format_type = 'results_format';
227  $keyword_reps = Array();
228 
229  // determine which method is used to check if a user can vote
230  if (!$this->attr('allow_multiple_votes')) {
231  // A user is logged in
232  if ((!is_null($GLOBALS['SQ_SYSTEM']->user->name)) && (!$GLOBALS['SQ_SYSTEM']->userPublic())) {
233  // check the "shadow link" table whether this user has already voted...
234  if (!$GLOBALS['SQ_SYSTEM']->am->getShadowLinkByAsset($question->id, $GLOBALS['SQ_SYSTEM']->user->id, SQ_LINK_NOTICE)) {
235  // add them to the array of previously-voted users, only if they are voting though
236  if (isset($_POST['option_id'])) {
237  $GLOBALS['SQ_SYSTEM']->am->createShadowAssetLink($question, $GLOBALS['SQ_SYSTEM']->user, SQ_LINK_NOTICE);
238  }
239  $can_vote = TRUE;
240  }
241  } else {
242  if (array_get_index($_SESSION, $prefix.'_VOTED') == FALSE) {
243  $can_vote = TRUE;
244  }
245  }
246  } else {
247  $can_vote = TRUE;
248  }
249 
250  // if the poll was just voted on...
251  if (isset($_POST['option_id'])) {
252  // save the results (if applicable), then show the results screen
253  if ($can_vote) {
254  // tie any vote processing to the subsequent printing of results
255  // (due to possible slow DB replication issues) using db2
256  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
257  $this->_processVote($question, $_POST['option_id']);
258  }
259  $format_type = 'results_format';
260  $keyword_reps = $this->_getResultsFormatKeywordReplacements($question);
261  } else {
262 
263  // if accessing the results screen by ID of View Results, or cannot vote
264  if (array_get_index($_GET, 'SQ_ACTION') == $prefix.'_past_polls') {
265  $format_type = 'past_questions_format';
266  $keyword_reps = $this->_getPastQuestionFormatKeywordReplacements($question, $active_question_id);
267  } else {
268  // if can vote, but we're not specifically asking for the results screen
269  if ($can_vote && array_get_index($_GET, 'SQ_ACTION') != $prefix.'_results') {
270  // set the question to vote on to the active question
271  $question = $active_question;
272  $format_type = 'question_format';
273  $keyword_reps = $this->_getQuestionFormatKeywordReplacements($question);
274  } else {
275  // print the results screen
276  $format_type = 'results_format';
277  $keyword_reps = $this->_getResultsFormatKeywordReplacements($question);
278  }
279  }
280 
281  }//end if ($_POST['option_id'])
282 
283  $format_bodycopy = $this->getFormatAsset($format_type);
284  if (!is_null($format_bodycopy)) {
285  $keywords = $format_bodycopy->getKeywords();
286  foreach ($keywords as $keyword) {
287  if (!isset($keyword_reps[$keyword])) {
288  $keyword_reps[$keyword] = $this->getKeywordReplacement($keyword);
289  }
290  }
291 
292  $format_bodycopy->setKeywordReplacements($keyword_reps);
293  $this->_printFormatAsset($format_bodycopy, $format_type);
294  }
295 
296  if (isset($_POST['option_id']) && $can_vote) {
297  // untie the connection
298  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
299  }
300 
301  }//end printBody()
302 
303 
314  protected function _printFormatAsset($format_bodycopy, $format_type)
315  {
316  if ($format_type == 'question_format') {
317  echo '<form id="'.$this->getPrefix().'_poll" action="'.$this->getURL().'" method="post">';
318  $format_bodycopy->printBody();
319  echo '</form>';
320  } else {
321  $format_bodycopy->printBody();
322  }
323 
324  }//end _printFormatAsset()
325 
326 
335  function &getFolder($type='type_formats')
336  {
337  $null = NULL;
338  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, $type);
339 
340  if (empty($link)) return $null;
341 
342  $folder = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
343  if (is_null($folder)) return $null;
344 
345  return $folder;
346 
347  }//end getFolder()
348 
349 
359  function onRequestKeywords(&$broadcaster, $vars=Array())
360  {
361  $parent_folders = $GLOBALS['SQ_SYSTEM']->am->getParents($broadcaster->id, 'folder', TRUE);
362  $parent_bodycopy = $GLOBALS['SQ_SYSTEM']->am->getParents($broadcaster->id, 'bodycopy', TRUE);
363 
364  $vote_formats = $this->getFolder('vote_formats');
365  $result_formats = $this->getFolder('results_formats');
366  $past_questions_formats = $this->getFolder('past_questions_formats');
367 
368  $keywords = Array();
369  if (in_array($vote_formats->id, array_keys($parent_folders))) {
370  $quetion_format = $this->getFormatAsset('question_format');
371  $option_list_format = $this->getFormatAsset('option_list_format');
372  if (in_array($quetion_format->id, array_keys($parent_bodycopy))) {
373  $keywords = $this->_getQuestionFormatKeywords();
374  } else if (in_array($option_list_format->id, array_keys($parent_bodycopy))) {
375  $keywords = $this->_getOptionListFormatKeywords();
376  }
377  } else if (in_array($result_formats->id, array_keys($parent_folders))) {
378  $results_format = $this->getFormatAsset('results_format');
379  $results_list_format = $this->getFormatAsset('results_list_format');
380  if (in_array($results_format->id, array_keys($parent_bodycopy))) {
381  $keywords = $this->_getResultsFormatKeywords();
382  } else if (in_array($results_list_format->id, array_keys($parent_bodycopy))) {
383  $keywords = $this->_getResultsListFormatKeywords();
384  }
385  } else if (in_array($past_questions_formats->id, array_keys($parent_folders))) {
386  $past_questions_format = $this->getFormatAsset('past_questions_format');
387  $past_questions_list_format = $this->getFormatAsset('past_questions_list_format');
388  if (in_array($past_questions_format->id, array_keys($parent_bodycopy))) {
389  $keywords = $this->_getPastQuestionsFormatKeywords();
390  } else if (in_array($past_questions_list_format->id, array_keys($parent_bodycopy))) {
391  $keywords = $this->_getPastQuestionsListFormatKeywords();
392  }
393  }
394 
395  $vars['keywords'] = array_merge($vars['keywords'], $keywords);
396 
397  }//end onRequestKeywords()
398 
399 
409  {
410 
411  $keywords = Array(
412  'poll_question' => $this->_componentQuestion($question),
413  'poll_date' => $this->_componentDate($question),
414  'poll_option_list' => $this->_componentOptionList($question),
415  'poll_vote_button' => $this->_componentVoteButton(),
416  'poll_vote_button_new_window' => $this->_componentVoteButton(TRUE),
417  'poll_vote_button_js' => $this->_componentVoteButtonJavascript(),
418  'poll_vote_button_js_new_window' => $this->_componentVoteButtonJavascript(TRUE),
419  'poll_view_results' => $this->_componentViewResults(),
420  'poll_view_results_url' => $this->_componentViewResultsURL(),
421  'poll_view_past_questions' => $this->_componentViewPastQuestions(),
422  'poll_view_past_questions_url' => $this->_componentViewPastQuestionsURL(),
423  'poll_results_list' => $this->_componentGraph($question),
424  'poll_active_question' => $this->_componentActiveQuestion($question, FALSE),
425  'poll_active_question_results' => $this->_componentActiveQuestion($question, FALSE, TRUE),
426  'poll_active_question_date' => $this->_componentActiveQuestion($question, TRUE),
427  'poll_active_question_date_results' => $this->_componentActiveQuestion($question, TRUE, TRUE),
428  );
429 
430  return $keywords;
431 
432  }//end _getQuestionFormatKeywordReplacements()
433 
434 
442  {
443  $keywords = Array();
444  $keywords['poll_question'] = translate('cms_poll_question');
445  $keywords['poll_date'] = translate('cms_poll_date');
446  $keywords['poll_option_list'] = translate('cms_poll_option_list');
447  $keywords['poll_vote_button'] = translate('cms_poll_vote_button');
448  $keywords['poll_vote_button_new_window'] = translate('cms_poll_vote_button_new_window');
449  $keywords['poll_vote_button_js'] = translate('cms_poll_vote_button_js');
450  $keywords['poll_vote_button_js_new_window'] = translate('cms_poll_vote_button_js_new_window');
451  $keywords['poll_view_results'] = translate('cms_poll_view_results');
452  $keywords['poll_view_results_url'] = translate('cms_poll_view_results_url');
453  $keywords['poll_view_past_questions'] = translate('cms_poll_view_past_questions');
454  $keywords['poll_view_past_questions_url'] = translate('cms_poll_view_past_questions_url');
455  $keywords['poll_active_question'] = translate('cms_poll_active_question');
456  $keywords['poll_active_question_results'] = translate('cms_poll_active_question_results');
457  $keywords['poll_active_question_date'] = translate('cms_poll_active_question_date');
458  $keywords['poll_active_question_date_results'] = translate('cms_poll_active_question_date_results');
459 
460  return $keywords;
461 
462  }//end _getQuestionFormatKeywords()
463 
464 
472  {
473  $keywords = Array();
474  $keywords['poll_option_label'] = translate('cms_poll_option_label');
475  $keywords['poll_option_number'] = translate('cms_poll_option_number');
476  $keywords['poll_option_id'] = translate('cms_poll_option_id');
477  $keywords['poll_option_vote_selection_button'] = translate('cms_poll_option_vote_selection_button');
478 
479  return $keywords;
480 
481  }//end _getOptionListFormatKeywords()
482 
483 
491  {
492  $keywords = Array();
493  $keywords = $this->_getQuestionFormatKeywords();
494  $keywords['poll_results_list'] = translate('cms_poll_results_list');
495  if (isset($keywords['poll_option_list'])) {
496  unset($keywords['poll_option_list']);
497  }
498 
499  return $keywords;
500 
501  }//end _getResultsFormatKeywords()
502 
503 
513  {
514  return $this->_getQuestionFormatKeywordReplacements($question);
515 
516  }//end _getResultsFormatKeywordReplacements()
517 
518 
526  {
527  $keywords = Array();
528  $keywords['poll_option_number'] = translate('cms_poll_option_number');
529  $keywords['poll_option_id'] = translate('cms_poll_option_id');
530  $keywords['poll_option_label'] = translate('cms_poll_option_label');
531  $keywords['poll_option_bar'] = translate('cms_poll_option_bar');
532  $keywords['poll_option_votes_number'] = translate('cms_poll_option_votes_number');
533  $keywords['poll_option_votes_percent'] = translate('cms_poll_option_votes_percent');
534 
535  return $keywords;
536 
537  }//end _getResultsListFormatKeywords()
538 
539 
547  {
548  $keywords = Array();
549  $keywords['poll_question_list'] = translate('cms_poll_question_list');
550  $keywords['poll_question_list_date'] = translate('cms_poll_question_list_date');
551  $keywords['poll_question_list_no_active'] = translate('cms_poll_question_list_no_active');
552  $keywords['poll_question_list_date_no_active'] = translate('cms_poll_question_list_date_no_active');
553  $keywords['poll_active_question'] = translate('cms_poll_active_question');
554  $keywords['poll_date'] = translate('cms_poll_date');
555 
556 
557  return $keywords;
558 
559  }//end _getPastQuestionsFormatKeywords()
560 
561 
571  function _getPastQuestionFormatKeywordReplacements($question, $active_question_id)
572  {
573 
574  $keywords = Array(
575  'poll_date' => $this->_componentDate($question),
576  'poll_question_list' => $this->_componentQuestionList($active_question_id, FALSE, FALSE),
577  'poll_question_list_date' => $this->_componentQuestionList($active_question_id, TRUE, FALSE),
578  'poll_question_list_no_active' => $this->_componentQuestionList($active_question_id, FALSE, TRUE),
579  'poll_question_list_date_no_active' => $this->_componentQuestionList($active_question_id, TRUE, TRUE),
580  'poll_active_question' => $this->_componentActiveQuestion($question, FALSE),
581  );
582 
583  return $keywords;
584 
585  }//end _getPastQuestionFormatKeywordReplacements()
586 
587 
595  {
596  $keywords = Array();
597  $keywords['poll_question'] = translate('cms_poll_question');
598  $keywords = array_merge($keywords,$this->getAvailableKeywords());
599 
600  return $keywords;
601 
602  }//end _getPastQuestionsListFormatKeywords()
603 
604 
613  function &getFormatAsset($format='question_format')
614  {
615  $null = NULL;
616  $folders = $this->attr('format_bodycopies');
617 
618  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, $folders[$format]);
619  if (empty($link)) return $null;
620 
621  $folder = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
622  if (is_null($folder)) return $null;
623 
624  $bodycopy_link = $GLOBALS['SQ_SYSTEM']->am->getLink($folder->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, $format);
625  if (empty($bodycopy_link)) return $null;
626 
627  $format_bodycopy = $GLOBALS['SQ_SYSTEM']->am->getAsset($bodycopy_link['minorid'], $bodycopy_link['minor_type_code']);
628 
629  return $format_bodycopy;
630 
631  }//end getFormatAsset()
632 
633 
645  function _processVote(&$question, $option_ids)
646  {
647  if (!is_array($option_ids)) {
648  $option_ids = Array($option_ids);
649  }
650 
651  $options = $question->attr('options');
652  $save = FALSE;
653  foreach ($option_ids as $option_id) {
654  // if a valid option was voted for...
655  if (isset($options[$option_id])) {
656 
657  // increase votes, and queue the result for saving
658  $options[$option_id]['votes']++;
659  $question->setAttrValue('options', $options);
660  $save = TRUE;
661  }
662  }
663 
664  if ($save) {
665  // override write-protection
666  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
667  $question->saveAttributes();
668  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
669 
670  $_SESSION[$this->getPrefix().'_VOTED'] = TRUE;
671  }
672 
673  return $save;
674 
675  }//end _processVote()
676 
677 
678 //-- Interface Components --//
679 
680 
689  function _componentQuestion(&$question)
690  {
691  return $question->attr('name');
692 
693  }//end _componentQuestion()
694 
695 
704  function _componentDate(&$question)
705  {
706  return easy_datetime($question->created);
707 
708  }//end _componentDate()
709 
710 
719  function _componentOptionList(&$question)
720  {
721  $format_bodycopy = $this->getFormatAsset('option_list_format');
722  if (is_null($format_bodycopy)) return FALSE;
723  $keywords = Array();
724 
725  // grab the option list
726  $options = $question->attr('options');
727  $output = '';
728 
729  // $option_values is an array of the options text and the options votes
730  foreach ($options as $option_id => $option_values ) {
731  $keywords['poll_option_label'] = $option_values['option_text'];
732  $keywords['poll_option_number'] = $option_id + 1;
733  $keywords['poll_option_id'] = $option_id;
734  ob_start();
735  if ($question->attr('allow_multiple')) {
736  check_box('option_id[]',$option_id);
737  } else {
738  radio_button('option_id',$option_id);
739  }
740  $selection_button = ob_get_clean();
741  $keywords['poll_option_vote_selection_button'] = $selection_button;
742 
743  ob_start();
744  $format_bodycopy->setKeywordReplacements($keywords);
745  $format_bodycopy->printBody();
746  $output .= ob_get_clean();
747  }
748 
749  return $output;
750 
751  }//end _componentOptionList()
752 
753 
762  function _componentVoteButton($new_window=FALSE)
763  {
764  ob_start();
765  $xtras = '';
766 
767  if ($new_window) $xtras .= $this->_newWindowOption();
768  submit_button($this->getPrefix().'_poll_vote', $this->attr('vote_button_text'), $xtras);
769 
770  return ob_get_clean();
771 
772  }//end _componentVoteButton()
773 
774 
784  function _componentVoteButtonJavascript($new_window=FALSE)
785  {
786  $xtras = '';
787 
788  if ($new_window) $xtras .= $this->_newWindowOption();
789 
790  return $xtras.' document.getElementById(\''.$this->getPrefix().'_poll\').submit();';
791 
792  }//end _componentVoteButtonJavascript()
793 
794 
801  function _newWindowOption()
802  {
803  $width = $this->attr('new_window_width');
804  $height = $this->attr('new_window_height');
805  $options = $this->attr('new_window_options');
806 
807  $params = 'location='.((strpos($options, 'hide_location' ) !== FALSE) ? '0' : '1').','
808  .'menubar='.((strpos($options, 'hide_menubar' ) !== FALSE) ? '0' : '1').','
809  .'scrollbars='.((strpos($options, 'hide_scrollbars') !== FALSE) ? '0' : '1').','
810  .'statusbar='.((strpos($options, 'hide_status' ) !== FALSE) ? '0' : '1').','
811  .'toolbar='.((strpos($options, 'hide_toolbar' ) !== FALSE) ? '0' : '1').','
812  .'resizable='.((strpos($options, 'not_resizable' ) !== FALSE) ? '0' : '1');
813 
814  $xtras = ' window.open(\'\', \'results\', \'width='.$width.',height='.$height.','.$params.'\'); document.getElementById("'.$this->getPrefix().'_poll").target=\'results\';';
815  return $xtras;
816 
817  }//end _newWindowOption()
818 
819 
827  {
828  ob_start();
829  ?>
830  <a href="<?php echo $this->getURL(); ?>?SQ_ACTION=<?php echo $this->getPrefix(); ?>_results"><?php echo $this->attr('view_results_text'); ?></a>
831  <?php
832 
833  return ob_get_clean();
834 
835  }//end _componentViewResults()
836 
837 
845  {
846  return $this->getURL().'?SQ_ACTION='.$this->getPrefix().'_results';
847 
848  }//end _componentViewResultsURL()
849 
850 
858  {
859  ob_start();
860 
861  ?>
862  <a href="<?php echo $this->getURL(); ?>?SQ_ACTION=<?php echo $this->getPrefix(); ?>_past_polls"><?php echo $this->attr('previous_questions_text'); ?></a>
863  <?php
864 
865  return ob_get_clean();
866 
867  }//end _componentViewPastQuestions()
868 
869 
877  {
878  return $this->getURL().'?SQ_ACTION='.$this->getPrefix().'_past_polls';
879 
880  }//end _componentViewPastQuestionsURL()
881 
882 
895  function _componentGraph(&$question)
896  {
897  return $this->_componentGraphHTML($question);
898 
899  }//end _componentGraph()
900 
901 
910  function _componentGraphHTML(&$question)
911  {
912  $output = '';
913  ob_start();
914 
915  $question_width = $this->attr('graph_box_width');
916  $question_height = $this->attr('graph_box_height');
917  $question_colour_bar = $this->attr('graph_box_colour_bar');
918 
919  $poll_total = 0;
920 
921  $options = $question->attr('options');
922 
923  // print out the stylesheet for the graph
924  ?>
925  <style type="text/css">
926  #sq-page-poll-graph {
927  padding: 10px;
928  width: <?php echo $question_width; ?>px;
929  }
930 
931  #sq-page-poll-graph-bar {
932  background: <?php echo $question_colour_bar; ?>;
933  margin-left: 20px;
934  }
935 
936  #sq-page-poll-result {
937  float: right;
938  }
939 
940  #sq-page-poll-key {
941  float: left;
942  }
943  </style>
944  <?php
945 
946  ?>
947  <div id="sq-page-poll-graph">
948  <?php
949 
950  // grab the total number of votes
951  foreach ($options as $option_values) {
952  $poll_total += $option_values['votes'];
953  }
954 
955  // ensure no division by zero
956  if ($poll_total == 0) $poll_total++;
957 
958  $format_bodycopy = $this->getFormatAsset('results_list_format');
959  if (is_null($format_bodycopy)) return FALSE;
960  $keywords = Array();
961 
962  $output .= ob_get_clean();
963 
964  // foreach poll option, draw out a graph and its results
965  foreach ($options as $option_id => $option_values) {
966  ob_start();
967  $width = $option_values['votes'] / $poll_total;
968 
969  $keywords['poll_option_label'] = $option_values['option_text'];
970  $keywords['poll_option_number'] = $option_id + 1;
971  $keywords['poll_option_id'] = $option_id;
972 
973  ob_start();
974  ?>
975  <div id="sq-page-poll-graph-bar" style="width: <?php echo round($width * $question_width / 2); ?>px">&nbsp;</div>
976  <?php
977  $bar_output = ob_get_clean();
978  $keywords['poll_option_bar'] = $bar_output;
979  $keywords['poll_option_votes_number'] = $option_values['votes'];
980  $keywords['poll_option_votes_percent'] = number_format(($width*100),0,'.',' ').'%';
981 
982  $format_bodycopy->setKeywordReplacements($keywords);
983  $format_bodycopy->printBody();
984  $output .= ob_get_clean();
985  }//end foreach
986 
987  ob_start();
988  ?>
989  </div>
990  <?php
991 
992  $output .= ob_get_clean();
993  return $output;
994 
995  }//end _componentGraphHTML()
996 
997 
1008  function _componentQuestionList($active_question_id, $date=FALSE, $no_active=FALSE)
1009  {
1010  $output = '';
1011 
1012  $format_bodycopy = $this->getFormatAsset('past_questions_list_format');
1013  if (is_null($format_bodycopy)) return FALSE;
1014  $keywords = Array();
1015 
1016  ob_start();
1017 
1018  // grab all the children of the $this that are poll_questions, and order them by date (oldest -> newest)
1019  $poll_children = $GLOBALS['SQ_SYSTEM']->am->getLinks( $this->id, SQ_LINK_TYPE_2,
1020  'poll_question',
1021  FALSE,
1022  'major',
1023  NULL,
1024  NULL,
1025  NULL,
1026  'created',
1027  NULL
1028  );
1029 
1030  // format the array to pass to...
1031  $asset_id_list = Array();
1032  foreach ($poll_children as $child_id => $child_data) {
1033  array_push($asset_id_list, $child_data['minorid']);
1034  }
1035 
1036  // ... getAssetInfo, which grabs a summary of the asset's children
1037  $info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($asset_id_list, 'poll_question');
1038 
1039 
1040  // loop through the array backwards (we want to order them newest -> oldest)
1041  array_reverse($info);
1042  $output = ob_get_clean();
1043 
1044  foreach ($info as $info_index => $info_data) {
1045  ob_start();
1046  // if the asset is not live, skip it
1047  if ($info_data['status'] < SQ_STATUS_LIVE) continue;
1048 
1049  // if the poll is the active poll, and they don't want to print
1050  // the active poll, jump to the next iteration.
1051  if ($info_index == $active_question_id && $no_active) {
1052  continue;
1053  }
1054 
1055  $link = '';
1056  ob_start();
1057  ?>
1058  <a href="<?php echo $this->getURL().'?SQ_ACTION='.$this->getPrefix().'_results&SQ_POLL_ID='.$info_index; ?>">
1059  <?php
1060 
1061  echo $info_data['name'];
1062  if ($date) {
1063  echo '&nbsp;('.easy_datetime(strtotime($info_data['created'])).')';
1064  }
1065  if ($info_index == $active_question_id) {
1066  echo '&nbsp;(<strong>Active</strong>)';
1067  }
1068 
1069  ?>
1070  </a>
1071  <?php
1072  $link .= ob_get_clean();
1073  $keywords['poll_question'] = $link;
1074  $poll_question_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($info_index);
1075  foreach ($format_bodycopy->getKeywords() as $keyword) {
1076  if (empty($keywords[$keyword])) {
1077  $keywords[$keyword] = $poll_question_asset->getKeywordReplacement($keyword);
1078  }
1079  }
1080 
1081  $format_bodycopy->setKeywordReplacements($keywords);
1082  $format_bodycopy->printBody();
1083  $output .= ob_get_clean();
1084  }//end foreach ($info as ...)
1085 
1086  ob_start();
1087 
1088  $output .= ob_get_clean();
1089  return $output;
1090 
1091  }//end _componentQuestionList()
1092 
1093 
1104  function _componentActiveQuestion(&$active_question, $date, $results=FALSE)
1105  {
1106  ob_start();
1107 
1108  ?>
1109  <a href="<?php echo $this->getURL(); ?>?SQ_ACTION=<?php echo $this->getPrefix(); ?><?php echo ($results ? '_results' : ''); ?>">
1110  <?php
1111 
1112  echo $active_question->attr('name');
1113  if ($date) {
1114  echo '&nbsp;('.easy_datetime($active_question->created).')';
1115  }
1116 
1117  ?>
1118  </a>
1119  <?php
1120 
1121  return ob_get_clean();
1122 
1123  }//end _componentActiveQuestion()
1124 
1125 
1126 }//end class
1127 
1128 
1129 ?>