PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete5/concrete/blocks/survey/controller.php

https://bitbucket.org/jmhmd/hrfreeclinic
PHP | 300 lines | 207 code | 46 blank | 47 comment | 20 complexity | f2516f05c093b2dd141fbe6a8d35b06f MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Blocks
  4. * @subpackage BlockTypes
  5. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  6. * @license http://www.concrete5.org/license/ MIT License
  7. *
  8. */
  9. /**
  10. * Controller for the survey block, which allows site owners to add surveys and uses Google's graphing web service to display results.
  11. *
  12. * @package Blocks
  13. * @subpackage BlockTypes
  14. * @author Ryan Tyler <ryan@concrete5.org>
  15. * @author Tony Trupp <tony@concrete5.org>
  16. * @category Concrete
  17. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  18. * @license http://www.concrete5.org/license/ MIT License
  19. *
  20. */
  21. defined('C5_EXECUTE') or die(_("Access Denied."));
  22. class SurveyBlockController extends BlockController {
  23. protected $btTable = 'btSurvey';
  24. protected $btInterfaceWidth = "420";
  25. protected $btInterfaceHeight = "300";
  26. protected $btIncludeAll = 1;
  27. var $options = array();
  28. /**
  29. * Used for localization. If we want to localize the name/description we have to include this
  30. */
  31. public function getBlockTypeDescription() {
  32. return t("Provide a simple survey, along with results in a pie chart format.");
  33. }
  34. public function getBlockTypeName() {
  35. return t("Survey");
  36. }
  37. function __construct($obj = NULL) {
  38. parent::__construct($obj);
  39. global $c;
  40. if (is_object($c)) {
  41. $this->cID = $c->getCollectionID();
  42. }
  43. if($this->bID) {
  44. $db = Loader::db();
  45. $v = array($this->bID);
  46. $q = "select optionID, optionName, displayOrder from btSurveyOptions where bID = ? order by displayOrder asc";
  47. $r = $db->query($q, $v);
  48. $this->options = array();
  49. if ($r) {
  50. while ($row = $r->fetchRow()) {
  51. $opt = new BlockPollOption;
  52. $opt->optionID = $row['optionID'];
  53. $opt->optionName = $row['optionName'];
  54. $opt->displayOrder = $row['displayOrder'];
  55. $this->options[] = $opt;
  56. }
  57. }
  58. }
  59. }
  60. function getQuestion() {return $this->question;}
  61. function getPollOptions() { return $this->options; }
  62. function requiresRegistration() {return $this->requiresRegistration;}
  63. function hasVoted() {
  64. $u = new User();
  65. if ($u->isRegistered()) {
  66. $db = Loader::db();
  67. $v = array($u->getUserID(), $this->bID, $this->cID);
  68. $q = "select count(resultID) as total from btSurveyResults where uID = ? and bID = ? AND cID = ?";
  69. $result = $db->getOne($q,$v);
  70. if ($result > 0) {
  71. return true;
  72. }
  73. } elseif ($_COOKIE['ccmPoll' . $this->bID.'-'.$this->cID] == 'voted') {
  74. return true;
  75. }
  76. return false;
  77. }
  78. function delete() {
  79. $db = Loader::db();
  80. $v = array($this->bID);
  81. $q = "delete from btSurveyOptions where bID = ?";
  82. $db->query($q, $v);
  83. $q = "delete from btSurveyResults where bID = ?";
  84. $db->query($q, $v);
  85. return parent::delete();
  86. }
  87. function action_form_save_vote() {
  88. $u = new User();
  89. $db = Loader::db();
  90. $bo = $this->getBlockObject();
  91. global $c;
  92. if ($this->requiresRegistration()) {
  93. if (!$u->isRegistered()) {
  94. $this->redirect('/login');
  95. }
  96. }
  97. if (!$this->hasVoted()) {
  98. $duID = 0;
  99. if($u->getUserID()>0) {
  100. $duID = $u->getUserID();
  101. }
  102. $v = array($_REQUEST['optionID'], $this->bID, $duID, $_SERVER['REMOTE_ADDR'], $this->cID);
  103. $q = "insert into btSurveyResults (optionID, bID, uID, ipAddress, cID) values (?, ?, ?, ?, ?)";
  104. $db->query($q, $v);
  105. setcookie("ccmPoll" . $this->bID.'-'.$this->cID, "voted", time() + 1296000, DIR_REL . '/');
  106. $this->redirect($c->getCollectionPath());
  107. }
  108. }
  109. function duplicate($newBID) {
  110. $db = Loader::db();
  111. foreach($this->options as $opt) {
  112. $v1 = array($newBID, $opt->getOptionName(), $opt->getOptionDisplayOrder());
  113. $q1 = "insert into btSurveyOptions (bID, optionName, displayOrder) values (?, ?, ?)";
  114. $db->query($q1, $v1);
  115. $v2 = array($opt->getOptionID());
  116. $newOptionID = get_insert_id();
  117. $q2 = "select * from btSurveyResults where optionID = ?";
  118. $r2 = $db->query($q2, $v2);
  119. if ($r2) {
  120. while ($row = $r2->fetchRow()) {
  121. $v3 = array($newOptionID, $row['uID'], $row['ipAddress'], $row['timestamp']);
  122. $q3 = "insert into btSurveyResults (optionID, uID, ipAddress, timestamp) values (?, ?, ?, ?)";
  123. $db->query($q3, $v3);
  124. }
  125. }
  126. }
  127. return parent::duplicate($newBID);
  128. }
  129. function save($args) {
  130. parent::save($args);
  131. $db = Loader::db();
  132. if(!is_array($args['survivingOptionNames']))
  133. $args['survivingOptionNames'] = array();
  134. $slashedArgs=array();
  135. foreach($args['survivingOptionNames'] as $arg)
  136. $slashedArgs[]=addslashes($arg);
  137. $db->query("DELETE FROM btSurveyOptions WHERE optionName NOT IN ('".implode("','",$slashedArgs)."') AND bID = ".intval($this->bID) );
  138. if (is_array($args['pollOption'])) {
  139. $displayOrder = 0;
  140. foreach($args['pollOption'] as $optionName) {
  141. $v1 = array($this->bID, $optionName, $displayOrder);
  142. $q1 = "insert into btSurveyOptions (bID, optionName, displayOrder) values (?, ?, ?)";
  143. $db->query($q1, $v1);
  144. $displayOrder++;
  145. }
  146. }
  147. $query = "DELETE FROM btSurveyResults
  148. WHERE optionID NOT IN (
  149. SELECT optionID FROM btSurveyOptions WHERE bID = {$this->bID}
  150. )
  151. AND bID = {$this->bID} ";
  152. $db->query($query);
  153. }
  154. public function displayChart($bID, $cID) {
  155. // Prepare the database query
  156. $db = Loader::db();
  157. // Get all available options
  158. $options = array();
  159. $v = array(intval($bID));
  160. $q = 'select optionID, optionName from btSurveyOptions where bID = ? order by displayOrder asc';
  161. $r = $db->Execute($q, $v);
  162. $i = 0;
  163. while ($row = $r->fetchRow()) {
  164. $options[$i]['name'] = $row['optionName'];
  165. $options[$i]['id'] = $row['optionID'];
  166. $i++;
  167. }
  168. // Get chosen count for each option
  169. $total_results = 0;
  170. $i = 0;
  171. foreach ($options as $option) {
  172. $v = array($option['id'], intval($bID), intval($cID));
  173. $q = 'select count(*) from btSurveyResults where optionID = ? and bID = ? and cID = ?';
  174. $r = $db->Execute($q, $v);
  175. if ($row = $r->fetchRow()) {
  176. $options[$i]['amount'] = $row['count(*)'];
  177. $total_results += $row['count(*)'];
  178. }
  179. $i++;
  180. }
  181. if ($total_results <= 0) {
  182. $chart_options = '<div style="text-align: center; margin-top: 15px;">No data is available yet.</div>';
  183. $this->set('chart_options', $chart_options);
  184. return;
  185. }
  186. // Convert option counts to percentages, initiate colors
  187. $availableChartColors=array('00CCdd','cc3333','330099','FF6600','9966FF','dd7700','66DD00','6699FF','FFFF33','FFCC33','00CCdd','cc3333','330099','FF6600','9966FF','dd7700','66DD00','6699FF','FFFF33','FFCC33');
  188. $percentage_value_string = '';
  189. foreach ($options as $option) {
  190. $option['amount'] /= $total_results;
  191. $percentage_value_string .= round($option['amount'], 3) . ',';
  192. $graphColors[]=array_pop($availableChartColors);
  193. }
  194. // Strip off trailing comma
  195. $percentage_value_string = substr_replace($percentage_value_string,'',-1);
  196. // Get Google Charts API image
  197. $img_src = '<img border="" src="http://chart.apis.google.com/chart?cht=p&chd=t:' . $percentage_value_string . '&chs=120x120&chco=' . join(',',$graphColors) . '" />';
  198. $this->set('pie_chart', $img_src);
  199. // Build human-readable option list
  200. $i = 1;
  201. $chart_options = '<table style="margin-left: 20px; float: left; width: 130px;">';
  202. foreach($options as $option) {
  203. $chart_options .= '<tr>';
  204. $chart_options .= '<td width="55px" class="note" style="white-space:nowrap">';
  205. $chart_options .= '<div class="surveySwatch" style="background:#' . $graphColors[$i - 1] . '"></div>';
  206. $chart_options .= '&nbsp;' . ($option['amount'] > 0) ? round($option['amount'] / $total_results * 100) : 0;
  207. $chart_options .= '%</td>';
  208. $chart_options .= '<td>';
  209. $chart_options .= '<strong>' . $options[$i - 1]['name'] . '</strong>';
  210. $chart_options .= '</td>';
  211. $chart_options .= '</tr>';
  212. $i++;
  213. }
  214. $chart_options .= '</table>';
  215. $this->set('chart_options', $chart_options);
  216. }
  217. }
  218. /**
  219. * @package Blocks
  220. * @subpackage BlockTypes
  221. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  222. * @license http://www.concrete5.org/license/ MIT License
  223. *
  224. */
  225. /**
  226. * An object that represents a survey option.
  227. *
  228. * @package Blocks
  229. * @subpackage BlockTypes
  230. * @author Andrew Embler <andrew@concrete5.org>
  231. * @category Concrete
  232. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  233. * @license http://www.concrete5.org/license/ MIT License
  234. *
  235. */
  236. class BlockPollOption {
  237. var $optionID, $optionName, $displayOrder;
  238. function getOptionID() {return $this->optionID;}
  239. function getOptionName() {return $this->optionName;}
  240. function getOptionDisplayOrder() {return $this->displayOrder;}
  241. function getResults() {
  242. global $c;
  243. $db = Loader::db();
  244. $v = array($this->optionID, intval($c->getCollectionID()) );
  245. $q = "select count(resultID) from btSurveyResults where optionID = ? AND cID=?";
  246. $result = $db->getOne($q, $v);
  247. if ($result > 0) {
  248. return $result;
  249. } else {
  250. return 0;
  251. }
  252. }
  253. }
  254. ?>