CMS  Version 3.9
results_graphs.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("questionnaire", "svg_charts");
40 
69 {
71 
83  static function drawSelectGraph($question, $resultsManager, $answers)
84  {
85  $pk = $question->getPrimaryKey();
86  $question_id = $question->$pk;
87  $options = explode("\n", $question->options);
88  $idx = 1;
89  $optionAnswers = array();
90  $legends = array();
91 
92  $answerValues = regroupList($answers, "value");
93 
94  $total = 0;
95  foreach($options as $option)
96  {
97  $count = count($answerValues[$idx]);
98 
99  $total += $count;
100  $idx++;
101  }
102 
103  $idx = 1;
104  if(count($options) > 0)
105  {
106  foreach($options as $option)
107  {
108  $count = count($answerValues[$idx]);
109  $optionAnswers[] = $count;
110 
111  if ($total > 0)
112  {
113  $percent = number_format(100 * ($count / $total), 1);
114  $legends[] = trim($option) . " ( $percent% )";
115  }
116  else
117  {
118  $legends[] = trim($option);
119  }
120  $idx++;
121  }
122  }
123 
124 
125 
126  $pie = new PieChart("results_$question_id", 650, 400, $optionAnswers, $legends, "standard");
127  $pie->setRadius(150);
128  $pie->setCenter(180,180);
129  $pie->setLabelSize(12);
130  $pie->setStrokeWidth(2);
131  $pie->showLegend(true, 375, 20);
132  $pie->shadow = false;
133  //$pie->emboss = true;
134  $pie->draw();
135  }
136 
148  static function drawChecklistGraph($question, $resultsManager, $answers)
149  {
150  $pk = $question->getPrimaryKey();
151  $question_id = $question->$pk;
153 
154  $datay = array();
155 
156  $optionAnswers = array();
157  $count = array();
158  $optionText = QuestionnaireResultsGraph::getOptionText($question->options);
159  $steps = count($optionText);
160 
161  for($idx = 1; $idx <= $steps; ++$idx)
162  {
163  //echo "idx is $idx<br/>";
164  // store for calculating mean and median
165  $totalValue += $idx * $counts[$idx];
166  $totalCount += $counts[$idx];
167  $datay[] = $counts[$idx] ? $counts[$idx] : 0;
168  $datax[] = $optionText[$idx];
169  }
170 
171  // Setup the graph.
172  $graph = new Histogram(650,300);
173  $graph = new Histogram("histogram_$question_id", count($datax) * 80 + 100, 400, 50, 10, count($datax) * 80, 300, "standard");
174  $graph->setLabels($datax);
175  $graph->ticks = 10;
176  $graph->columnMargin = 0.4;
177 
178  $series = new HistogramSeries("block", "Answers", $datay);
179  $graph->addSeries($series);
180  $graph->draw();
181  }
182 
194  static function drawRatingsGraph($question, $resultsManager, $answers)
195  {
196  $pk = $question->getPrimaryKey();
197  $question_id = $question->$pk;
198  $answerValues = regroupList($answers, "value");
199 
200  $datay = array();
201 
202  $optionAnswers = array();
203  $count = array();
204  $optionText = QuestionnaireResultsGraph::getRatingsOptionText($question->options);
205  $steps = count($optionText);
206 
207  for($idx = 1; $idx <= $steps; ++$idx)
208  {
209  //echo "idx is $idx<br/>";
210  // store for calculating mean and median
211  $counts[$idx] = count($answerValues[$idx]);
212  $totalValue += $idx * $counts[$idx];
213  $totalCount += $counts[$idx];
214  $datay[] = $counts[$idx];
215  $datax[] = $optionText[$idx];
216  }
217 
218  $mean = "Average: ".number_format($totalValue / $totalCount, 1);
219 
220  // Setup the graph.
221  $graph = new Histogram("histogram_$question_id", count($datax) * 80 + 100, 400, 50, 10, count($datax) * 80, 300, "standard");
222  $graph->setLabels($datax);
223  $graph->ticks = 10;
224  $graph->columnMargin = 0.4;
225 
226  //$graph->setTitle($mean);
227 
228  $series = new HistogramSeries("block", "Ratings", $datay);
229  $graph->addSeries($series);
230  $graph->draw();
231  }
232 
241  static function drawSelectResults($surveyField, $qField, $survey_id, $question_id)
242  {
244  {
245  echo "<p><em>Survey Graphs are not available</em></p>";
246  return;
247  }
248 
250 
251  $question = $surveyManager->getQuestion($question_id);
252  $survey = $surveyManager->getSurvey($survey_id);
253 
254  $answers = $survey->Answers("WHERE $qField=$question_id");
255 
257  }
258 
267  static function drawChecklistResults($surveyField, $qField, $survey_id, $question_id)
268  {
270  {
271  echo "<p><em>Survey Graphs are not available</em></p>";
272  return;
273  }
274 
276 
277  $question = $surveyManager->getQuestion($question_id);
278  $survey = $surveyManager->getSurvey($survey_id);
279 
280  $answers = $survey->Answers("WHERE $qField=$question_id");
281 
283  }
284 
285 
294  static function drawRatingResults($surveyField, $qField, $survey_id, $question_id)
295  {
297  {
298  echo "<p><em>Survey Graphs are not available</em></p>";
299  return;
300  }
301 
303 
304  $question = $surveyManager->getQuestion($question_id);
305  $survey = $surveyManager->getSurvey($survey_id);
306 
307  $answers = $survey->Answers("WHERE $qField=$question_id");
308 
310 
311  }
312 
313  private static function getRatingsOptionText($options)
314  {
315  $optionText = array();
316 
317  list($from, $to, $steps) = explode("\n", $options);
318  if (!$from) $from = "Lowest";
319  if (!$to) $to = "Highest";
320  if (!$steps) $steps = 5;
321 
322  for($idx = 1; $idx <= $steps; ++$idx)
323  {
324  if($idx > 1 AND $idx < $steps)
325  $optionText[$idx] = $idx;
326  else
327  $optionText[$idx] = ($idx == 1) ? jsSafe(trim($from)) : jsSafe(trim($to));
328 
329  }
330  return $optionText;
331  }
332 
333  private static function getOptionText($options)
334  {
335  $optionText = array();
336 
337  $options = explode("\n", $options);
338 
339  $idx = 1;
340  foreach($options as $option)
341  {
342  $optionText[$idx++] = jsSafe(wordwrap(trim(stripHTML($option)), 12));
343  }
344 
345  return $optionText;
346  }
347 }?>
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static getCheckListAnswerCounts($answers)
Draws a graph of the aggregated answers for a given question.
static drawSelectResults($surveyField, $qField, $survey_id, $question_id)
Deprecated - call drawGraph from question renderer.
static drawChecklistGraph($question, $resultsManager, $answers)
Draws an interactive graphical view of answers to one question.
static drawRatingResults($surveyField, $qField, $survey_id, $question_id)
Deprecated - call drawGraph from question renderer.
static drawSelectGraph($question, $resultsManager, $answers)
Draws an interactive graphical view of answers to one question.
static drawChecklistResults($surveyField, $qField, $survey_id, $question_id)
Deprecated - call drawGraph from question renderer.
static drawRatingsGraph($question, $resultsManager, $answers)
Draws an interactive graphical view of answers to one question.
$survey_id
if(! $survey_id) $survey
$question_id
$question
$answers
$series
$pie
Definition: test_pie.inc:4