PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/campsite/src/plugins/poll/classes/PollAnswer.php

https://github.com/joechrysler/Campsite
PHP | 537 lines | 297 code | 84 blank | 156 comment | 34 complexity | 65221ed05849cc01f4272d5341a3e269 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @package Campsite
  4. */
  5. class PollAnswer extends DatabaseObject {
  6. /**
  7. * The column names used for the primary key.
  8. * @var array
  9. */
  10. var $m_keyColumnNames = array('fk_poll_nr', 'fk_language_id', 'nr_answer');
  11. var $m_dbTableName = 'plugin_poll_answer';
  12. var $m_columnNames = array(
  13. // int - poll id
  14. 'fk_poll_nr',
  15. // int - language id
  16. 'fk_language_id',
  17. // int - nr of answer
  18. 'nr_answer',
  19. // string - the literal answer
  20. 'answer',
  21. // int - number of votes for this answer
  22. 'nr_of_votes',
  23. // float - score of this answers in this language
  24. 'percentage',
  25. // float - score of this answers overall languages
  26. 'percentage_overall',
  27. // int - the commulative value of all votes
  28. 'value',
  29. // float - value / number of votes
  30. 'average_value',
  31. // timestamp - last_modified
  32. 'last_modified'
  33. );
  34. private static $s_defaultOrder = array('bynumber'=>'asc');
  35. /**
  36. * Construct by passing in the primary key to access the poll answer in
  37. * the database.
  38. *
  39. * @param int $p_fk_language_id
  40. * @param int $p_fk_poll_nr
  41. * @param int $p_nr_answer
  42. */
  43. function PollAnswer($p_fk_language_id = null, $p_fk_poll_nr = null, $p_nr_answer = null)
  44. {
  45. parent::DatabaseObject($this->m_columnNames);
  46. $this->m_data['fk_language_id'] = $p_fk_language_id;
  47. $this->m_data['fk_poll_nr'] = $p_fk_poll_nr;
  48. $this->m_data['nr_answer'] = $p_nr_answer;
  49. if ($this->keyValuesExist()) {
  50. $this->fetch();
  51. }
  52. } // constructor
  53. /**
  54. * A way for internal functions to call the superclass create function.
  55. * @param array $p_values
  56. */
  57. function __create($p_values = null) { return parent::create($p_values); }
  58. /**
  59. * Create an poll answer in the database. Use the SET functions to
  60. * change individual values.
  61. *
  62. * @param string $p_fk_default_language_id
  63. * @param date $p_date_begin
  64. * @param date $p_date_end
  65. * @param int $p_nr_of_answers
  66. * @param bool $p_is_show_after_expiration
  67. * @return void
  68. */
  69. function create($p_answer)
  70. {
  71. global $g_ado_db;
  72. if (!strlen($p_answer)) {
  73. return false;
  74. }
  75. // Create the record
  76. $values = array(
  77. 'answer' => $p_answer
  78. );
  79. $success = parent::create($values);
  80. if (!$success) {
  81. return;
  82. }
  83. /*
  84. if (function_exists("camp_load_translation_strings")) {
  85. camp_load_translation_strings("api");
  86. }
  87. $logtext = getGS('Poll Id $1 created.', $this->m_data['IdPoll']);
  88. Log::Message($logtext, null, 31);
  89. */
  90. $CampCache = CampCache::singleton();
  91. $CampCache->clear('user');
  92. return true;
  93. } // fn create
  94. /**
  95. * Create a translation of an answer set.
  96. *
  97. * @param int $p_fk_poll_nr
  98. * @param int $p_source_language_id
  99. * @param int $p_target_language_id
  100. * @return Poll
  101. */
  102. function CreateTranslationSet($p_fk_poll_nr, $p_source_language_id, $p_target_language_id)
  103. {
  104. // Construct the duplicate PollAnswer object.
  105. foreach (PollAnswer::getAnswers($p_fk_poll_nr, $p_source_language_id) as $answer) {
  106. $answer_copy = new PollAnswer($p_target_language_id, $p_fk_poll_nr, $answer->getNumber());
  107. $answer_copy->create($answer->getProperty('answer'));
  108. }
  109. /*
  110. if (function_exists("camp_load_translation_strings")) {
  111. camp_load_translation_strings("api");
  112. }
  113. $logtext = getGS('Article #$1 "$2" ($3) translated to "$5" ($4)',
  114. $this->getArticleNumber(), $this->getTitle(), $this->getLanguageName(),
  115. $articleCopy->getTitle(), $articleCopy->getLanguageName());
  116. Log::Message($logtext, null, 31);
  117. */
  118. return $pollAnswerCopy;
  119. } // fn createTranslation
  120. /**
  121. * Create a copy of an answer set.
  122. *
  123. * @param int $p_fk_poll_nr
  124. * @param int $p_fk_language_id
  125. * @param int $p_parent_nr
  126. * @param array $p_answers
  127. * @return Poll
  128. */
  129. function CreateCopySet($p_fk_poll_nr, $p_fk_language_id, $p_parent_nr, $p_answers)
  130. {
  131. // Construct the duplicate PollAnswer object.
  132. foreach ($p_answers as $answer) {
  133. if (isset($answer['number']) && !empty($answer['number']) && strlen($answer['text'])) {
  134. $answer_copy = new PollAnswer($p_fk_language_id, $p_fk_poll_nr, $answer['number']);
  135. $answer_copy->create($answer['text']);
  136. if (isset($answer['nr_of_votes']) && !empty($answer['nr_of_votes'])) {
  137. $answer_copy->setProperty('nr_of_votes', $answer['nr_of_votes']);
  138. }
  139. if (isset($answer['value']) && !empty($answer['nr_of_votes'])) {
  140. $answer_copy->setProperty('value', $answer['value']);
  141. $answer_copy->setProperty('average_value', $answer_copy->getProperty('value') / $answer_copy->getProperty('nr_of_votes'));
  142. }
  143. }
  144. }
  145. // Copy PollAnswerAttachments
  146. PollAnswerAttachment::CreateCopySet($p_fk_poll_nr, $p_fk_language_id, $p_parent_nr);
  147. /*
  148. if (function_exists("camp_load_translation_strings")) {
  149. camp_load_translation_strings("api");
  150. }
  151. $logtext = getGS('Article #$1 "$2" ($3) translated to "$5" ($4)',
  152. $this->getArticleNumber(), $this->getTitle(), $this->getLanguageName(),
  153. $articleCopy->getTitle(), $articleCopy->getLanguageName());
  154. Log::Message($logtext, null, 31);
  155. */
  156. return $pollAnswerCopy;
  157. } // fn createTranslation
  158. function getPollAnswerAttachments()
  159. {
  160. $PollAnswerAttachments = PollAnswerAttachment::getPollAnswerAttachments($this->getPollNumber(), $this->getNumber());
  161. return $PollAnswerAttachments;
  162. }
  163. /**
  164. * Delete poll from database. This will
  165. * only delete one specific translation of the poll question.
  166. *
  167. * @return boolean
  168. */
  169. function delete()
  170. {
  171. // Delte PollAnswerAttachments
  172. PollAnswerAttachment::OnPollAnswerDelete($this->getPollNumber(), $this->getNumber());
  173. // Delete from plugin_poll_answer table
  174. $deleted = parent::delete();
  175. /*
  176. if ($deleted) {
  177. if (function_exists("camp_load_translation_strings")) {
  178. camp_load_translation_strings("api");
  179. }
  180. $logtext = getGS('Article #$1: "$2" ($3) deleted.',
  181. $this->m_data['Number'], $this->m_data['Name'], $this->getLanguageName())
  182. ." (".getGS("Publication")." ".$this->m_data['IdPublication'].", "
  183. ." ".getGS("Issue")." ".$this->m_data['NrIssue'].", "
  184. ." ".getGS("Section")." ".$this->m_data['NrSection'].")";
  185. Log::Message($logtext, null, 32);
  186. }
  187. */
  188. $CampCache = CampCache::singleton();
  189. $CampCache->clear('user');
  190. return $deleted;
  191. } // fn delete
  192. public function OnPollDelete($p_fk_poll_nr, $p_fk_language_id)
  193. {
  194. foreach (PollAnswer::getAnswers($p_fk_poll_nr, $p_fk_language_id) as $answer) {
  195. $answer->delete();
  196. }
  197. }
  198. public function getAnswers($p_fk_poll_nr = null, $p_fk_language_id = null)
  199. {
  200. global $g_ado_db;
  201. $answers = array();
  202. if (!is_null($p_fk_poll_nr) && !is_null($p_fk_language_id)) {
  203. $fk_poll_nr = $p_fk_poll_nr;
  204. $fk_language_id = $p_fk_language_id;
  205. } elseif (isset($this)) {
  206. $fk_poll_nr = $this->m_data['fk_poll_nr'];
  207. $fk_language_id = $this->m_data['fk_language_id'];
  208. }
  209. if (!$fk_poll_nr || !$fk_language_id) {
  210. return array();
  211. }
  212. $query = "SELECT nr_answer
  213. FROM plugin_poll_answer
  214. WHERE fk_poll_nr = $fk_poll_nr
  215. AND fk_language_id = $fk_language_id
  216. ORDER BY nr_answer";
  217. $res = $g_ado_db->Execute($query);
  218. while ($row = $res->fetchRow()) {
  219. $answers[] = new PollAnswer($fk_language_id, $fk_poll_nr, $row['nr_answer']);
  220. }
  221. return $answers;
  222. }
  223. public static function SyncNrOfAnswers($p_fk_language_id, $p_fk_poll_nr)
  224. {
  225. global $g_ado_db;
  226. $poll = new Poll($p_fk_language_id, $p_fk_poll_nr);
  227. if (count($poll->getTranslations()) > 1) {
  228. $nr_of_answers = $poll->getProperty('nr_of_answers');
  229. $query = "DELETE FROM plugin_poll_answer
  230. WHERE fk_poll_nr = $p_fk_poll_nr
  231. AND fk_language_id = $p_fk_language_id
  232. AND nr_answer > $nr_of_answers";
  233. $g_ado_db->execute($query);
  234. Poll::triggerStatistics($p_fk_poll_nr);
  235. }
  236. }
  237. public function getPoll()
  238. {
  239. $poll = new Poll($this->m_data['fk_language_id'], $this->m_data['fk_poll_nr']);
  240. return $poll;
  241. }
  242. public function vote($p_value = 1)
  243. {
  244. if (!settype($p_value, 'float')) {
  245. return false;
  246. }
  247. $this->setProperty('nr_of_votes', $this->getProperty('nr_of_votes') + 1);
  248. $this->setProperty('value', $this->getProperty('value') + $p_value);
  249. $this->setProperty('average_value', $this->getProperty('value') / $this->getProperty('nr_of_votes'));
  250. $this->getPoll()->increaseUserVoteCount();
  251. Poll::triggerStatistics($this->m_data['fk_poll_nr']);
  252. }
  253. public function getNumber()
  254. {
  255. return $this->m_data['nr_answer'];
  256. }
  257. public function getPollNumber()
  258. {
  259. return $this->m_data['fk_poll_nr'];
  260. }
  261. public function getAnswer()
  262. {
  263. return $this->getProperty('answer');
  264. }
  265. public function getLanguageId()
  266. {
  267. return $this->getProperty('fk_language_id');
  268. }
  269. /**
  270. * Method to call parent::setProperty
  271. * with clening the cache.
  272. *
  273. * @param string $p_name
  274. * @param sring $p_value
  275. */
  276. function setProperty($p_name, $p_value)
  277. {
  278. $return = parent::setProperty($p_name, $p_value);
  279. $CampCache = CampCache::singleton();
  280. $CampCache->clear('user');
  281. return $return;
  282. }
  283. /////////////////// Special template engine methods below here /////////////////////////////
  284. /**
  285. * Gets an issue list based on the given parameters.
  286. *
  287. * @param array $p_parameters
  288. * An array of ComparisonOperation objects
  289. * @param string $p_order
  290. * An array of columns and directions to order by
  291. * @param integer $p_count
  292. * The count of answers.
  293. *
  294. * @return array $issuesList
  295. * An array of Issue objects
  296. */
  297. public static function GetList(array $p_parameters, $p_order = null, $p_start = 0, $p_limit = 0, &$p_count)
  298. {
  299. global $g_ado_db;
  300. $hasPollNr = false;
  301. $hasLanguageId = fase;
  302. $selectClauseObj = new SQLSelectClause();
  303. if (!is_array($p_parameters)) {
  304. return null;
  305. }
  306. // adodb::selectLimit() interpretes -1 as unlimited
  307. if ($p_limit == 0) {
  308. $p_limit = -1;
  309. }
  310. // sets the where conditions
  311. foreach ($p_parameters as $param) {
  312. $comparisonOperation = self::ProcessListParameters($param);
  313. if (empty($comparisonOperation)) {
  314. continue;
  315. }
  316. if (strpos($comparisonOperation['left'], 'poll_nr') !== false) {
  317. $hasPollNr = true;
  318. }
  319. if (strpos($comparisonOperation['left'], 'language_id') !== false) {
  320. $hasLanguageId = true;
  321. }
  322. $whereCondition = $comparisonOperation['left'] . ' '
  323. . $comparisonOperation['symbol'] . " '"
  324. . $comparisonOperation['right'] . "' ";
  325. $selectClauseObj->addWhere($whereCondition);
  326. }
  327. // validates whether publication identifier was given
  328. if ($hasPollNr == false) {
  329. CampTemplate::singleton()->trigger_error('missed parameter Poll Number in statement list_pollanswers');
  330. return;
  331. }
  332. // validates whether language identifier was given
  333. if ($hasLanguageId == false) {
  334. CampTemplate::singleton()->trigger_error('missed parameter Language Identifier in statement list_pollanswers');
  335. return;
  336. }
  337. // sets the columns to be fetched
  338. $tmpPollAnswer = new PollAnswer();
  339. $columnNames = $tmpPollAnswer->getColumnNames(true);
  340. foreach ($columnNames as $columnName) {
  341. $selectClauseObj->addColumn($columnName);
  342. }
  343. // sets the main table for the query
  344. $mainTblName = $tmpPollAnswer->getDbTableName();
  345. $selectClauseObj->setTable($mainTblName);
  346. unset($tmpPollAnswer);
  347. if (!is_array($p_order)) {
  348. $p_order = array();
  349. }
  350. // sets the ORDER BY condition
  351. $p_order = count($p_order) > 0 ? $p_order : PollAnswer::$s_defaultOrder;
  352. $order = PollAnswer::ProcessListOrder($p_order);
  353. foreach ($order as $orderColumn => $orderDirection) {
  354. $selectClauseObj->addOrderBy($orderColumn . ' ' . $orderDirection);
  355. }
  356. $sqlQuery = $selectClauseObj->buildQuery();
  357. // count all available results
  358. $countRes = $g_ado_db->Execute($sqlQuery);
  359. $p_count = $countRes->recordCount();
  360. //get the wanted rows
  361. $pollAnswerRes = $g_ado_db->Execute($sqlQuery);
  362. // builds the array of poll objects
  363. $pollAnswersList = array();
  364. while ($pollAnswer = $pollAnswerRes->FetchRow()) {
  365. $pollAnswerObj = new PollAnswer($pollAnswer['fk_language_id'], $pollAnswer['fk_poll_nr'], $pollAnswer['nr_answer']);
  366. if ($pollAnswerObj->exists()) {
  367. $pollAnswersList[] = $pollAnswerObj;
  368. }
  369. }
  370. return $pollAnswersList;
  371. } // fn GetList
  372. /**
  373. * Processes a paremeter (condition) coming from template tags.
  374. *
  375. * @param array $p_param
  376. * The array of parameters
  377. *
  378. * @return array $comparisonOperation
  379. * The array containing processed values of the condition
  380. */
  381. private static function ProcessListParameters(ComparisonOperation $p_param)
  382. {
  383. $comparisonOperation = array();
  384. switch (strtolower($p_param->getLeftOperand())) {
  385. case 'fk_poll_nr':
  386. $comparisonOperation['left'] = 'fk_poll_nr';
  387. break;
  388. case 'fk_language_id':
  389. $comparisonOperation['left'] = 'fk_language_id';
  390. break;
  391. case 'onhitlist':
  392. $comparisonOperation['left'] = 'on_hitlist';
  393. break;
  394. }
  395. if (isset($comparisonOperation['left'])) {
  396. $operatorObj = $p_param->getOperator();
  397. $comparisonOperation['right'] = $p_param->getRightOperand();
  398. $comparisonOperation['symbol'] = $operatorObj->getSymbol('sql');
  399. }
  400. return $comparisonOperation;
  401. } // fn ProcessListParameters
  402. /**
  403. * Processes an order directive coming from template tags.
  404. *
  405. * @param array $p_order
  406. * The array of order directives
  407. *
  408. * @return array
  409. * The array containing processed values of the condition
  410. */
  411. private static function ProcessListOrder(array $p_order)
  412. {
  413. $order = array();
  414. foreach ($p_order as $field=>$direction) {
  415. $dbField = null;
  416. switch (strtolower($field)) {
  417. case 'bynumber':
  418. $dbField = 'nr_answer';
  419. break;
  420. case 'byanswer':
  421. $dbField = 'answer';
  422. break;
  423. case 'byvotes':
  424. $dbField = 'nr_of_votes';
  425. break;
  426. case 'bypercentage':
  427. $dbField = 'percentage';
  428. break;
  429. case 'bypercentage_overall':
  430. $dbField = 'percentage_overall';
  431. break;
  432. case 'byvalue':
  433. $dbField = 'value';
  434. break;
  435. case 'byaverage_value':
  436. $dbField = 'average_value';
  437. break;
  438. case 'bylastmodified':
  439. $dbField = 'last_modified';
  440. break;
  441. default:
  442. $dbField = 'nr_answer';
  443. break;
  444. }
  445. if (!is_null($dbField)) {
  446. $direction = !empty($direction) ? $direction : 'asc';
  447. }
  448. $order[$dbField] = $direction;
  449. }
  450. return $order;
  451. }
  452. } // class PollAnswer
  453. ?>