PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/blocks/survey/controller.php

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