ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilObjPoll.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once "Services/Object/classes/class.ilObject2.php";
6 require_once "Services/Object/classes/class.ilObjectActivation.php";
7 
16 class ilObjPoll extends ilObject2
17 {
18  protected $online; // [bool]
19  protected $access_type; // [int]
20  protected $access_begin; // [timestamp]
21  protected $access_end; // [timestamp]
22  protected $access_visibility; // [bool]
23  protected $question; // [string]
24  protected $image; // [string]
25  protected $view_results; // [int]
26  protected $period; // [bool]
27  protected $period_begin; // [timestamp]
28  protected $period_end; // [timestamp]
29 
30  // 4.5
31  protected $max_number_answers = 1; // [int]
32  protected $result_sort_by_votes = false; // [bool]
33  protected $mode_non_anonymous = false; // [bool]
34  protected $show_comments = false; //[bool]
35  protected $show_results_as = 1; //[int]
36 
38  const VIEW_RESULTS_NEVER = 2;
41 
44 
45  function __construct($a_id = 0, $a_reference = true)
46  {
47  // default
48  $this->setOnline(false);
49  $this->setViewResults(self::VIEW_RESULTS_AFTER_VOTE);
51  $this->setVotingPeriod(false);
52 
53  parent::__construct($a_id, $a_reference);
54  }
55 
56  function initType()
57  {
58  $this->type = "poll";
59  }
60 
61  function setOnline($a_value)
62  {
63  $this->online = (bool)$a_value;
64  }
65 
66  function isOnline()
67  {
68  return $this->online;
69  }
70 
71  function setAccessType($a_value)
72  {
73  $this->access_type = (int)$a_value;
74  }
75 
76  function getAccessType()
77  {
78  return $this->access_type;
79  }
80 
81  function setAccessBegin($a_value)
82  {
83  $this->access_begin = (int)$a_value;
84  }
85 
86  function getAccessBegin()
87  {
88  return $this->access_begin;
89  }
90 
91  function setAccessEnd($a_value)
92  {
93  $this->access_end = (int)$a_value;
94  }
95 
96  function getAccessEnd()
97  {
98  return $this->access_end;
99  }
100 
101  function setAccessVisibility($a_value)
102  {
103  $this->access_visibility = (bool)$a_value;
104  }
105 
107  {
109  }
110 
111  function setQuestion($a_value)
112  {
113  $this->question = (string)$a_value;
114  }
115 
116  function getQuestion()
117  {
118  return $this->question;
119  }
120 
121  function setImage($a_value)
122  {
123  $this->image = (string)$a_value;
124  }
125 
126  function getImage()
127  {
128  return $this->image;
129  }
130 
131  function setViewResults($a_value)
132  {
133  $this->view_results = (int)$a_value;
134  }
135 
136  function getViewResults()
137  {
138  return $this->view_results;
139  }
140 
141  function setVotingPeriod($a_value)
142  {
143  $this->period = (bool)$a_value;
144  }
145 
146  function getVotingPeriod()
147  {
148  return $this->period;
149  }
150 
151  function setVotingPeriodBegin($a_value)
152  {
153  $this->period_begin = (int)$a_value;
154  }
155 
157  {
158  return $this->period_begin;
159  }
160 
161  function setVotingPeriodEnd($a_value)
162  {
163  $this->period_end = (int)$a_value;
164  }
165 
167  {
168  return $this->period_end;
169  }
170 
171  function setMaxNumberOfAnswers($a_value)
172  {
173  $this->max_number_answers = (int)$a_value;
174  }
175 
177  {
179  }
180 
181  function setSortResultByVotes($a_value)
182  {
183  $this->result_sort_by_votes = (bool)$a_value;
184  }
185 
187  {
189  }
190 
191  function setNonAnonymous($a_value)
192  {
193  $this->mode_non_anonymous = (bool)$a_value;
194  }
195 
196  function getNonAnonymous()
197  {
199  }
200 
201  function setShowComments($a_value)
202  {
203  $this->show_comments = (bool)$a_value;
204  }
205 
206  function getShowComments()
207  {
208  return $this->show_comments;
209  }
210 
211  function setShowResultsAs($a_value)
212  {
213  $this->show_results_as = (int)$a_value;
214  }
215 
216  function getShowResultsAs()
217  {
218  return $this->show_results_as;
219  }
220 
221  protected function doRead()
222  {
223  global $ilDB;
224 
225  $set = $ilDB->query("SELECT * FROM il_poll".
226  " WHERE id = ".$ilDB->quote($this->getId(), "integer"));
227  $row = $ilDB->fetchAssoc($set);
228  $this->setQuestion($row["question"]);
229  $this->setImage($row["image"]);
230  $this->setOnline($row["online_status"]);
231  $this->setViewResults($row["view_results"]);
232  $this->setVotingPeriod($row["period"]);
233  $this->setVotingPeriodBegin($row["period_begin"]);
234  $this->setVotingPeriodEnd($row["period_end"]);
235  $this->setMaxNumberOfAnswers($row["max_answers"]);
236  $this->setSortResultByVotes($row["result_sort"]);
237  $this->setNonAnonymous($row["non_anon"]);
238  $this->setShowResultsAs($row["show_results_as"]);
239 
240  // #14661
241  include_once("./Services/Notes/classes/class.ilNote.php");
242  $this->setShowComments(ilNote::commentsActivated($this->getId(), 0, $this->getType()));
243 
244  if($this->ref_id)
245  {
246  $activation = ilObjectActivation::getItem($this->ref_id);
247  $this->setAccessType($activation["timing_type"]);
249  {
250  // default entry values should not be loaded if not activated
251  $this->setAccessBegin($activation["timing_start"]);
252  $this->setAccessEnd($activation["timing_end"]);
253  $this->setAccessVisibility($activation["visible"]);
254  }
255  }
256  }
257 
258  protected function propertiesToDB()
259  {
260  $fields = array(
261  "question" => array("text", $this->getQuestion()),
262  "image" => array("text", $this->getImage()),
263  "online_status" => array("integer", $this->isOnline()),
264  "view_results" => array("integer", $this->getViewResults()),
265  "period" => array("integer", $this->getVotingPeriod()),
266  "period_begin" => array("integer", $this->getVotingPeriodBegin()),
267  "period_end" => array("integer", $this->getVotingPeriodEnd()),
268  "max_answers" => array("integer", $this->getMaxNumberOfAnswers()),
269  "result_sort" => array("integer", $this->getSortResultByVotes()),
270  "non_anon" => array("integer", $this->getNonAnonymous()),
271  "show_results_as" => array("integer", $this->getShowResultsAs()),
272  );
273 
274  return $fields;
275  }
276 
277  protected function doCreate()
278  {
279  global $ilDB;
280 
281  if($this->getId())
282  {
283  $fields = $this->propertiesToDB();
284  $fields["id"] = array("integer", $this->getId());
285 
286  $ilDB->insert("il_poll", $fields);
287 
288 
289  // object activation default entry will be created on demand
290 
291 
292  // block handling
293  include_once "Modules/Poll/classes/class.ilPollBlock.php";
294  $block = new ilPollBlock();
295  $block->setType("poll");
296  $block->setContextObjId($this->getId());
297  $block->setContextObjType("poll");
298  $block->create();
299  }
300  }
301 
302  protected function doUpdate()
303  {
304  global $ilDB;
305 
306  if($this->getId())
307  {
308  $fields = $this->propertiesToDB();
309 
310  $ilDB->update("il_poll", $fields,
311  array("id"=>array("integer", $this->getId())));
312 
313  // #14661
314  include_once("./Services/Notes/classes/class.ilNote.php");
315  ilNote::activateComments($this->getId(), 0, $this->getType(), $this->getShowComments());
316 
317  if($this->ref_id)
318  {
319  $activation = new ilObjectActivation();
320  $activation->setTimingType($this->getAccessType());
321  $activation->setTimingStart($this->getAccessBegin());
322  $activation->setTimingEnd($this->getAccessEnd());
323  $activation->toggleVisible($this->getAccessVisibility());
324  $activation->update($this->ref_id);
325  }
326 
327  }
328  }
329 
330  protected function doDelete()
331  {
332  global $ilDB;
333 
334  if($this->getId())
335  {
336  $this->deleteImage();
337  $this->deleteAllAnswers();
338 
339  if($this->ref_id)
340  {
342  }
343 
344  $ilDB->manipulate("DELETE FROM il_poll".
345  " WHERE id = ".$ilDB->quote($this->id, "integer"));
346  }
347  }
348 
357  public function doCloneObject($new_obj, $a_target_id, $a_copy_id = 0)
358  {
359  assert($new_obj instanceof ilObjPoll);
360 
361  // question/image
362  $new_obj->setQuestion($this->getQuestion());
363  $image = $this->getImageFullPath();
364  if($image)
365  {
366  $image = array("tmp_name"=>$image,
367  "name"=>$this->getImage());
368  $new_obj->uploadImage($image, true);
369  }
370 
371  //copy online status if object is not the root copy object
372  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
373 
374  if(!$cp_options->isRootNode($this->getRefId()))
375  {
376  $new_obj->setOnline($this->isOnline());
377  }
378 
379  $new_obj->setViewResults($this->getViewResults());
380  $new_obj->setShowComments($this->getShowComments());
381  $new_obj->setShowResultsAs($this->getShowResultsAs());
382  $new_obj->update();
383 
384  // answers
385  $answers = $this->getAnswers();
386  if($answers)
387  {
388  foreach($answers as $item)
389  {
390  $new_obj->saveAnswer($item["answer"]);
391  }
392  }
393 
394  return $new_obj;
395  }
396 
397 
398  //
399  // image
400  //
401 
407  function getImageFullPath($a_as_thumb = false)
408  {
409  $img = $this->getImage();
410  if($img)
411  {
412  $path = $this->initStorage($this->id);
413  if(!$a_as_thumb)
414  {
415  return $path.$img;
416  }
417  else
418  {
419  return $path."thb_".$img;
420  }
421  }
422  }
423 
427  public function deleteImage()
428  {
429  if($this->id)
430  {
431  include_once "Modules/Poll/classes/class.ilFSStoragePoll.php";
432  $storage = new ilFSStoragePoll($this->id);
433  $storage->delete();
434 
435  $this->setImage(null);
436  }
437  }
438 
446  public static function initStorage($a_id, $a_subdir = null)
447  {
448  include_once "Modules/Poll/classes/class.ilFSStoragePoll.php";
449  $storage = new ilFSStoragePoll($a_id);
450  $storage->create();
451 
452  $path = $storage->getAbsolutePath()."/";
453 
454  if($a_subdir)
455  {
456  $path .= $a_subdir."/";
457 
458  if(!is_dir($path))
459  {
460  mkdir($path);
461  }
462  }
463 
464  return $path;
465  }
466 
473  function uploadImage(array $a_upload, $a_clone = false)
474  {
475  if(!$this->id)
476  {
477  return false;
478  }
479 
480  $this->deleteImage();
481 
482  // #10074
483  $clean_name = preg_replace("/[^a-zA-Z0-9\_\.\-]/", "", $a_upload["name"]);
484 
485  $path = $this->initStorage($this->id);
486  $original = "org_".$this->id."_".$clean_name;
487  $thumb = "thb_".$this->id."_".$clean_name;
488  $processed = $this->id."_".$clean_name;
489 
490  $success = false;
491  if(!$a_clone)
492  {
493  $success = ilUtil::moveUploadedFile($a_upload["tmp_name"], $original, $path.$original);
494  }
495  else
496  {
497  $success = copy($a_upload["tmp_name"], $path.$original);
498  }
499 
500  if($success)
501  {
502  chmod($path.$original, 0770);
503 
504  // take quality 100 to avoid jpeg artefacts when uploading jpeg files
505  // taking only frame [0] to avoid problems with animated gifs
506  $original_file = ilUtil::escapeShellArg($path.$original);
507  $thumb_file = ilUtil::escapeShellArg($path.$thumb);
508  $processed_file = ilUtil::escapeShellArg($path.$processed);
509  ilUtil::execConvert($original_file."[0] -geometry \"100x100>\" -quality 100 PNG:".$thumb_file);
510  ilUtil::execConvert($original_file."[0] -geometry \"".self::getImageSize().">\" -quality 100 PNG:".$processed_file);
511 
512  $this->setImage($processed);
513  return true;
514  }
515  return false;
516  }
517 
518  public static function getImageSize()
519  {
520  // :TODO:
521  return "300x300";
522  }
523 
524 
525  //
526  // Answer
527  //
528 
529  function getAnswers()
530  {
531  global $ilDB;
532 
533  $res = array();
534 
535  $sql = "SELECT * FROM il_poll_answer".
536  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer").
537  " ORDER BY pos ASC";
538  $set = $ilDB->query($sql);
539  while($row = $ilDB->fetchAssoc($set))
540  {
541  $res[] = $row;
542  }
543  return $res;
544  }
545 
546  function getAnswer($a_id)
547  {
548  global $ilDB;
549 
550  $sql = "SELECT * FROM il_poll_answer".
551  " WHERE id = ".$ilDB->quote($a_id, "integer");
552  $set = $ilDB->query($sql);
553  return (array)$ilDB->fetchAssoc($set);
554  }
555 
556  function saveAnswer($a_text, $a_pos = null)
557  {
558  global $ilDB;
559 
560  if(!trim($a_text))
561  {
562  return;
563  }
564 
565  $id = $ilDB->nextId("il_poll_answer");
566 
567  if(!$a_pos)
568  {
569  // append
570  $sql = "SELECT max(pos) pos".
571  " FROM il_poll_answer".
572  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer");
573  $set = $ilDB->query($sql);
574  $a_pos = $ilDB->fetchAssoc($set);
575  $a_pos = (int)$a_pos["pos"]+10;
576  }
577 
578  $fields = array(
579  "id" => array("integer", $id),
580  "poll_id" => array("integer", $this->getId()),
581  "answer" => array("text", trim($a_text)),
582  "pos" => array("integer", $a_pos)
583  );
584  $ilDB->insert("il_poll_answer", $fields);
585 
586  return $id;
587  }
588 
589  function updateAnswer($a_id, $a_text)
590  {
591  global $ilDB;
592 
593  $ilDB->update("il_poll_answer",
594  array("answer" => array("text", $a_text)),
595  array("id" => array("integer", $a_id)));
596  }
597 
599  {
600  $answers = $this->getAnswers();
601 
602  $pos = array();
603  foreach($answers as $item)
604  {
605  $pos[$item["id"]] = $item["pos"];
606  }
607 
608  $this->updateAnswerPositions($pos);
609  }
610 
611  function updateAnswerPositions(array $a_pos)
612  {
613  global $ilDB;
614 
615  asort($a_pos);
616 
617  $pos = 0;
618  foreach(array_keys($a_pos) as $id)
619  {
620  $pos += 10;
621 
622  $ilDB->update("il_poll_answer",
623  array("pos" => array("integer", $pos)),
624  array("id" => array("integer", $id)));
625  }
626  }
627 
628  function deleteAnswer($a_id)
629  {
630  global $ilDB;
631 
632  if($a_id)
633  {
634  $ilDB->manipulate("DELETE FROM il_poll_vote".
635  " WHERE answer_id = ".$ilDB->quote($this->getId(), "integer"));
636 
637  $ilDB->manipulate("DELETE FROM il_poll_answer".
638  " WHERE id = ".$ilDB->quote($a_id, "integer"));
639  }
640  }
641 
642  protected function deleteAllAnswers()
643  {
644  global $ilDB;
645 
646  if($this->getId())
647  {
648  $this->deleteAllVotes();
649 
650  $ilDB->manipulate("DELETE FROM il_poll_answer".
651  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer"));
652  }
653  }
654 
655  public function deleteAllVotes()
656  {
657  global $ilDB;
658 
659  if($this->getId())
660  {
661  $ilDB->manipulate("DELETE FROM il_poll_vote".
662  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer"));
663  }
664  }
665 
666  function saveAnswers(array $a_answers)
667  {
668  $existing = $this->getAnswers();
669 
670  $ids = array();
671  $pos = 0;
672  foreach($a_answers as $answer)
673  {
674  if(trim($answer))
675  {
676  // existing answer?
677  $found = false;
678  foreach($existing as $idx => $item)
679  {
680  if(trim($answer) == $item["answer"])
681  {
682  $found = true;
683  unset($existing[$idx]);
684 
685  $id = $item["id"];
686  }
687  }
688 
689  // create new answer
690  if(!$found)
691  {
692  $id = $this->saveAnswer($answer);
693  }
694 
695  // add existing answer id to order
696  if($id)
697  {
698  $ids[$id] = ++$pos;
699  }
700  }
701  }
702 
703  // remove obsolete answers
704  if(sizeof($existing))
705  {
706  foreach($existing as $item)
707  {
708  $this->deleteAnswer($item["id"]);
709  }
710  }
711 
712  // save current order
713  if(sizeof($ids))
714  {
715  $this->updateAnswerPositions($ids);
716  }
717 
718  return sizeof($ids);
719  }
720 
721 
722  //
723  // votes
724  //
725 
726  function saveVote($a_user_id, $a_answers)
727  {
728  global $ilDB;
729 
730  if($this->hasUserVoted($a_user_id))
731  {
732  return;
733  }
734 
735  if(!is_array($a_answers))
736  {
737  $a_answers = array($a_answers);
738  }
739 
740  foreach($a_answers as $answer_id)
741  {
742  $fields = array("user_id" => array("integer", $a_user_id),
743  "poll_id" => array("integer", $this->getId()),
744  "answer_id" => array("integer", $answer_id));
745  $ilDB->insert("il_poll_vote", $fields);
746  }
747  }
748 
749  function hasUserVoted($a_user_id)
750  {
751  global $ilDB;
752 
753  $sql = "SELECT user_id".
754  " FROM il_poll_vote".
755  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer").
756  " AND user_id = ".$ilDB->quote($a_user_id, "integer");
757  $ilDB->setLimit(1);
758  $set = $ilDB->query($sql);
759  return (bool)$ilDB->numRows($set);
760  }
761 
762  function countVotes()
763  {
764  global $ilDB;
765 
766  $sql = "SELECT COUNT(DISTINCT(user_id)) cnt".
767  " FROM il_poll_vote".
768  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer");
769  $set = $ilDB->query($sql);
770  $row = $ilDB->fetchAssoc($set);
771  return (int)$row["cnt"];
772  }
773 
775  {
776  global $ilDB;
777 
778  $res = array();
779  $cnt = 0;
780 
781  $sql = "SELECT answer_id, count(*) cnt".
782  " FROM il_poll_vote".
783  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer").
784  " GROUP BY answer_id";
785  $set = $ilDB->query($sql);
786  while($row = $ilDB->fetchAssoc($set))
787  {
788  $cnt += $row["cnt"];
789  $res[$row["answer_id"]] = array("abs"=>$row["cnt"], "perc"=>0);
790  }
791 
792  foreach($res as $id => $item)
793  {
794  $res[$id]["perc"] = $item["abs"]/$cnt*100;
795  }
796 
797  return array("perc"=>$res, "total"=>$this->countVotes());
798  }
799 
800  public function getVotesByUsers()
801  {
802  global $ilDB;
803 
804  $res = array();
805 
806  $sql = "SELECT answer_id, user_id, firstname, lastname, login".
807  " FROM il_poll_vote".
808  " JOIN usr_data ON (usr_data.usr_id = il_poll_vote.user_id)".
809  " WHERE poll_id = ".$ilDB->quote($this->getId(), "integer");
810  $set = $ilDB->query($sql);
811  while($row = $ilDB->fetchAssoc($set))
812  {
813  if(!isset($res[$row["user_id"]]))
814  {
815  $res[$row["user_id"]] = $row;
816  }
817  $res[$row["user_id"]]["answers"][] = $row["answer_id"];
818  }
819 
820  return $res;
821  }
822 }
823 
824 ?>
deleteImage()
remove existing file
const VIEW_RESULTS_AFTER_PERIOD
hasUserVoted($a_user_id)
$path
Definition: aliased.php:25
const VIEW_RESULTS_AFTER_VOTE
const VIEW_RESULTS_NEVER
getImageFullPath($a_as_thumb=false)
Get image incl.
updateAnswer($a_id, $a_text)
setAccessEnd($a_value)
setVotingPeriod($a_value)
const VIEW_RESULTS_ALWAYS
setAccessVisibility($a_value)
setAccessType($a_value)
static getItem($a_ref_id)
Get item data.
const SHOW_RESULTS_AS_PIECHART
setSortResultByVotes($a_value)
setQuestion($a_value)
setShowResultsAs($a_value)
Add rich text string
The name of the decorator.
setOnline($a_value)
setShowComments($a_value)
getAnswer($a_id)
saveAnswer($a_text, $a_pos=null)
setMaxNumberOfAnswers($a_value)
setAccessBegin($a_value)
const SHOW_RESULTS_AS_BARCHART
setImage($a_value)
saveVote($a_user_id, $a_answers)
Generate an image
static _getInstance($a_copy_id)
Get instance of copy wizard options.
$success
Definition: Utf8Test.php:86
static execConvert($args)
execute convert command
static getImageSize()
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
updateAnswerPositions(array $a_pos)
uploadImage(array $a_upload, $a_clone=false)
Upload new image file.
Create styles array
The data for the language used.
static initStorage($a_id, $a_subdir=null)
Init file system storage.
static deleteAllEntries($a_ref_id)
Delete all db entries for ref id.
Class ilObjPoll.
saveAnswers(array $a_answers)
static commentsActivated($a_rep_obj_id, $a_obj_id, $a_obj_type)
Are comments activated for object?
Custom block for polls.
doCloneObject($new_obj, $a_target_id, $a_copy_id=0)
Clone poll.
static activateComments($a_rep_obj_id, $a_obj_id, $a_obj_type, $a_activate=true)
Activate notes feature.
static escapeShellArg($a_arg)
__construct($a_id=0, $a_reference=true)
global $ilDB
Class ilObject2 This is an intermediate progress of ilObject class.
setNonAnonymous($a_value)
setViewResults($a_value)
setVotingPeriodBegin($a_value)
Class ilObjectActivation.
setVotingPeriodEnd($a_value)
deleteAnswer($a_id)