1 <?php
2 3 4 5 6
7 class Poll extends DataObject {
8
9 static $db = array(
10 'PollTitle' => 'Text',
11 'IsActive' => 'Boolean',
12 'Subsite' => 'Int',
13 'DiscussionLink' => 'Varchar(255)',
14 'AnswerType' => 'Int'
15 );
16
17 static $has_many = array(
18 'PollAnswers' => 'PollAnswer'
19 );
20
21 static $searchable_fields = array(
22 'PollTitle'
23 );
24
25 static $summary_fields=array (
26 'PollTitle',
27 'ActiveText'
28 );
29 static $defaults = array(
30 'AnswerType' => 1,
31 'Subsite' => 0,
32 );
33
34 static $log_cookie = true;
35 static $result_graphing = false;
36 static $cookie_timeout_time = 1;
37 static $graph_type = "p3";
38 static $graph_size = "200x100";
39
40 static = "&chf=bg,s,E0D2E6";
41
42 function getCMSFields(){
43 $fields = parent::getCMSFields();
44 $fAnswerType = new OptionsetField('AnswerType',$this->fieldLabel('AnswerType'), array(
45 1 => _t('Poll.TYPE_ONE',"One"),
46 2 => _t('Poll.TYPE_MANY',"Many"),
47 ));
48 $fields->replaceField('AnswerType', $fAnswerType);
49
50 if(singleton('SiteTree')->hasExtension('SiteTreeSubsites')){
51 $subsiteField = new SubsiteDropdownField('Subsite', _t('Poll.Subsite','Subsite'), $this->Subsite);
52 $fields->replaceField('Subsite', $subsiteField);
53 }else{
54 $fields->removeByName('Subsite');
55 }
56 return $fields;
57 }
58
59 function fieldLabels() {
60 $labels = parent::fieldLabels();
61 $labels['ActiveText'] = $labels['IsActive'];
62 return $labels;
63 }
64
65 66 67 68 69 70
71 function getTotalVotes() {
72 $total = 0;
73 if($this->PollAnswers()->exists()) {
74 foreach($this->PollAnswers() as $answer) {
75 $total += $answer->Votes;
76 }
77 }
78 return $total;
79 }
80
81 82 83 84
85 function ActiveText() {
86 ($this->IsActive) ? $title = _t('Poll.Active') : $title = _t('Poll.NotActive');
87 return $title;
88 }
89
90 function questionFields() {
91 $answers = array();
92 foreach($this->PollAnswers() as $answer)
93 $answers[$answer->ID]=$answer->Answer;
94 switch ($this->AnswerType) {
95 case 1:
96 $fields = new FieldSet(
97 new OptionsetField('PollAnswer', $this->PollTitle, $answers),
98 new HiddenField('ID', '', $this->ID)
99 );
100 break;
101
102 case 2:
103 $fields = new FieldSet(
104 new CheckboxSetField('PollAnswer', $this->PollTitle, $answers),
105 new HiddenField('ID', '', $this->ID)
106 );
107 break;
108
109 default:
110 break;
111 }
112
113
114 return $fields;
115 }
116 117 118 119 120 121
122 static function generateChart($mapdetails) {
123 return "http://chart.apis.google.com/chart?$mapdetails";
124 }
125 126 127 128
129 function answerFields() {
130 $total_votes = $this->TotalVotes;
131 $percentage = 100;
132 $graphInfo = array();
133 $fields = array();
134 $fields[] = new LiteralField('Poll_Title',"<h6 class=\"pollTitle\">".$this->PollTitle."</h6>");
135 foreach($this->PollAnswers() as $this_answer) {
136 if($total_votes > 0) {
137 $percentage=floor(((int)$this_answer->Votes/$total_votes)*100);
138 }
139
140
141 if(!Poll::$result_graphing) {
142 $fields[] = new LiteralField('Answer_' . $this_answer->ID, "<div class=\"poll_answer_caption\">" . $this_answer->Answer ." (". $percentage . "%)</div><div class=\"poll_answer_result\"><span style=\"width: " . $percentage . "%\"> </span></div>");
143 }
144 }
145 146 147
148 if(Poll::$result_graphing == true) {
149 $output = "chs=". Poll::$graph_size ."&cht=".Poll::$graph_type;
150 $values = "&chd=t:";
151 $labels = "&chl=";
152 foreach($this->PollAnswers() as $result) {
153 if($total_votes > 0) {
154 $percentage=floor(((int)$result->Votes/$total_votes)*100);
155 }
156 $values .= $result->Votes.".0,";
157 $labels .= $result->Answer ." ". $percentage ."% |";
158 }
159 $values = substr($values, 0, -1);
160 $labels = substr($labels, 0, -1);
161 $output .= $values.$labels.Poll::$extraGraphParams;
162 $chart = $this->generateChart($output);
163 $fields[] = new LiteralField("AnswerGraph", "<img src='$chart' alt='chart' />");
164 }
165
166 if($this->DiscussionLink) {
167 $fields[] = new LiteralField("DiscussionLink", "<p class='discussionLink'>Tell Us More on the <a href='$this->DiscussionLink'>Discussion Boards</a></p>");
168 }
169 return $fields;
170 }
171 }
172
173 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-