ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFeedback.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
15 class ilFeedback
16 {
17  function ilFeedback($a_id = 0)
18  {
19  if($a_id > 0){
20  $this->id = $a_id;
21  $this->getBarometer();
22  }
23 
24  }
25  /* Set / Get Methods */
26  function setId($a_id)
27  {
28  $this->id = $a_id;
29  }
30  function getId(){
31  return($this->id);
32  }
33  function setTitle($a_title)
34  {
35  $this->title = $a_title;
36  }
37  function getTitle(){
38  return($this->title);
39  }
40  function setDescription($a_description){
41  $this->description = $a_description;
42  }
43  function getDescription(){
44  return($this->description);
45  }
46  function setAnonymous($a_anonymous){
47  $this->anonymous = $a_anonymous;
48  }
49 
50  function getAnonymous(){
51  return($this->anonymous);
52  }
53  function setRequired($a_required){
54  $this->required = $a_required;
55  }
56 
57  function getRequired(){
58  return($this->required);
59  }
60  function setShowOn($a_show_location){
61  $this->show_on = $a_show_location;
62  }
63 
64  function getShowOn(){
65  return($this->show_on);
66  }
67  function setVotes($a_votes){
68  $this->votes = $a_votes;
69  }
70 
71  function getVotes(){
72  return($this->votes);
73  }
74  function setStarttime($a_starttime){
75  $this->starttime = $a_starttime;
76  }
77 
78  function getStarttime(){
79  return($this->starttime);
80  }
81  function setEndtime($a_endtime){
82  $this->endtime = $a_endtime;
83  }
84 
85  function getEndtime(){
86  return($this->endtime);
87  }
88  function setInterval($a_interval){
89  $this->interval = $a_interval;
90  }
91 
92  function getInterval(){
93  return($this->interval);
94  }
95  function setIntervalUnit($a_interval_unit){
96  $this->interval_unit = $a_interval_unit;
97  }
98 
99  function getIntervalUnit(){
100  return($this->interval_unit);
101  }
102  function setFirstVoteBest($a_first_vote_best){
103  $this->first_vote_best = $a_first_vote_best;
104  }
105 
106  function getFirstVoteBest(){
107  return($this->first_vote_best);
108  }
109  function setObjId($a_obj_id){
110  $this->obj_id = $a_obj_id;
111  }
112 
113  function getObjId(){
114  return($this->obj_id);
115  }
116  function setRefId($a_ref_id){
117  $this->ref_id = $a_ref_id;
118  }
119  function getRefId(){
120  return($this->ref_id);
121  }
122  function setTextAnswer($a_text_answer){
123  $this->text_answer = $a_text_answer;
124  }
125 
126  function getTextAnswer(){
127  return($this->text_answer);
128  }
129  function setIds($a_ids){
130  $this->ids = $a_ids;
131  }
132  function setUserId($a_user_id){
133  $this->user_id = $a_user_id;
134  }
135  function setVote($a_vote){
136  $this->vote = $a_vote;
137  }
138  function setNote($a_note){
139  $this->note = $a_note;
140  }
141 
145  function setAllData($a_barometer){
146  $this->setId($a_barometer['fb_id']);
147  $this->setTitle($a_barometer['title']);
148  $this->setDescription($a_barometer['description']);
149  $this->setAnonymous($a_barometer['anonymous']);
150  $this->setRequired($a_barometer['required']);
151  $this->setShowOn($a_barometer['show_on']);
152  $this->setVotes($a_barometer['votes']);
153  $this->setStarttime($a_barometer['starttime']);
154  $this->setEndtime($a_barometer['endtime']);
155  $this->setInterval($a_barometer['repeat_interval']);
156  $this->setIntervalUnit($a_barometer['interval_unit']);
157  $this->setFirstVoteBest($a_barometer['first_vote_best']);
158  $this->setTextAnswer($a_barometer['text_answer']);
159  $this->setObjId($a_barometer['obj_id']);
160  $this->setRefId($a_barometer['ref_id']);
161  }
162 
166  function create()
167  {
168  global $ilDB;
169 
170  $this->id = $ilDB->nextId("feedback_items");
171  /*$q = "INSERT INTO feedback_items (fb_id,title, description, anonymous,".
172  "required, show_on, text_answer, votes, starttime, endtime, ".
173  "repeat_interval, interval_unit, first_vote_best, ref_id,obj_id) VALUES(".
174  $ilDB->quote($this->id, "integer").", ".
175  $ilDB->quote($this->title, "text").", ".
176  $ilDB->quote($this->description, "clob").", ".
177  $ilDB->quote((int) $this->anonymous, "integer").", ".
178  $ilDB->quote((int) $this->required, "integer").", ".
179  $ilDB->quote($this->show_on, "text").", ".
180  $ilDB->quote((int) $this->text_answer, "integer").", ".
181  $ilDB->quote($this->votes, "clob").", ".
182  $ilDB->quote((int) $this->starttime, "integer").", ".
183  $ilDB->quote((int) $this->endtime, "integer").", ".
184  $ilDB->quote((int) $this->interval, "integer").", ".
185  $ilDB->quote((int) $this->interval_unit, "integer").", ".
186  $ilDB->quote((int) $this->first_vote_best, "integer").", ".
187  $ilDB->quote((int) $this->ref_id, "integer").", ".
188  $ilDB->quote((int) $this->obj_id, "integer").")";*/
189  $ilDB->insert("feedback_items", array(
190  "fb_id" => array("integer", $this->id),
191  "title" => array("text", $this->title),
192  "description" => array("clob", $this->description),
193  "anonymous" => array("integer", (int) $this->anonymous),
194  "required" => array("integer", (int) $this->required),
195  "show_on" => array("text", $this->show_on),
196  "text_answer" => array("integer", (int) $this->text_answer),
197  "votes" => array("clob", $this->votes),
198  "starttime" => array("integer", (int) $this->starttime),
199  "endtime" => array("integer", (int) $this->endtime),
200  "repeat_interval" => array("integer", (int) $this->interval),
201  "interval_unit" => array("integer", (int) $this->interval_unit),
202  "first_vote_best" => array("integer", (int) $this->first_vote_best),
203  "ref_id" => array("integer", (int) $this->ref_id),
204  "obj_id" => array("integer", (int) $this->obj_id)
205  ));
206 
207 //echo "-$q-";
208  //$ilDB->manipulate($q);
209  }
210 
214  function update()
215  {
216  global $ilDB;
217 
218  /*$q = "UPDATE feedback_items set ".
219  "title=".$ilDB->quote($this->title, "text").", ".
220  "description=".$ilDB->quote($this->description, "clob").", ".
221  "anonymous=".$ilDB->quote((int) $this->anonymous, "integer").", ".
222  "required=".$ilDB->quote((int) $this->required, "integer").", ".
223  "show_on=".$ilDB->quote($this->show_on, "text").", ".
224  "text_answer=".$ilDB->quote((int) $this->text_answer, "integer").", ".
225  "votes=".$ilDB->quote($this->votes, "clob").", ".
226  "starttime=".$ilDB->quote((int) $this->starttime, "integer").", ".
227  "endtime=".$ilDB->quote((int) $this->endtime, "integer").", ".
228  "repeat_interval=".$ilDB->quote((int) $this->interval, "integer").", ".
229  "interval_unit=".$ilDB->quote((int) $this->interval_unit, "integer").", ".
230  "first_vote_best=".$ilDB->quote((int) $this->first_vote_best, "integer").
231  " WHERE fb_id=".$ilDB->quote($this->id, "integer");
232  $ilDB->manipulate($q);*/
233 
234  $ilDB->update("feedback_items", array(
235  "title" => array("text", $this->title),
236  "description" => array("clob", $this->description),
237  "anonymous" => array("integer", (int) $this->anonymous),
238  "required" => array("integer", (int) $this->required),
239  "show_on" => array("text", $this->show_on),
240  "text_answer" => array("integer", (int) $this->text_answer),
241  "votes" => array("clob", $this->votes),
242  "starttime" => array("integer", (int) $this->starttime),
243  "endtime" => array("integer", (int) $this->endtime),
244  "repeat_interval" => array("integer", (int) $this->interval),
245  "interval_unit" => array("integer", (int) $this->interval_unit),
246  "first_vote_best" => array("integer", (int) $this->first_vote_best)
247  ),array(
248  "fb_id" => array("integer", $this->id)
249  ));
250 
251  }
252 
256  function getBarometer()
257  {
258  global $ilDB;
259 
260  $q = "SELECT * FROM feedback_items WHERE fb_id = ".
261  $ilDB->quote($this->id, "integer");
262  $res = $ilDB->query($q);
263  if ($row = $ilDB->fetchAssoc($res))
264  {
265  $this->setAllData($row);
266  }
267  }
268 
273  {
274  global $ilDB;
275 
276  $q = "SELECT * FROM feedback_items WHERE obj_id = ".
277  $ilDB->quote($this->obj_id, "integer");
278  $res = $ilDB->query($q);
279  if($row = $ilDB->fetchAssoc($res))
280  {
281  $this->setAllData($row);
282  }
283  }
284 
289  {
290  global $ilDB;
291  $q = "SELECT * FROM feedback_items WHERE ref_id = ".
292  $ilDB->quote($this->ref_id, "integer");
293  $res = $ilDB->query($q);
294  if($row = $ilDB->fetchAssoc($res))
295  {
296  $this->setAllData($row);
297  }
298  }
299 
303  function getFeedback($required = 0)
304  {
305  global $ilDB;
306 
307  $filter_req = ($required)
308  ? ' required = 1 AND '
309  : '';
310  $q = "SELECT * FROM feedback_items WHERE ".
311  $filter_req.
312  " ((starttime <= ".$ilDB->quote(time(), "integer")." AND".
313  " endtime >= ".$ilDB->quote(time(), "integer").
314  ") OR (starttime <= 0 AND endtime <= 0))";
315 
316  $res = $ilDB->query($q);
317  if($row = $ilDB->fetchAssoc($res))
318  {
319  $this->setAllData($row);
320  }
321  }
322 
328  function getAllBarometer($a_show_inactive = 1,$a_only_req = 0)
329  {
330  global $ilDB;
331 
332  if ($this->ref_id)
333  {
334  $where.=" ref_id = ".$ilDB->quote($this->ref_id, "integer");
335  }
336  if ($a_only_req == 1)
337  {
338  if ($where != '')
339  {
340  $where .= ' AND required = 1 ';
341  }
342  else
343  {
344  $where = ' required = 1 ';
345  }
346  }
347  $q = "SELECT * FROM feedback_items WHERE ".$where;
348 
349  if ($a_show_inactive == 0)
350  {
351  if ($where != '')
352  {
353  $where = ' AND '.$where;
354  }
355  $q = "SELECT * FROM feedback_items WHERE ".
356  " ((starttime <= ".$ilDB->quote(time(), "integer")." AND".
357  " endtime >= ".$ilDB->quote(time(), "integer").
358  ") OR (starttime <= 0 AND endtime <=0 ))".$where;
359  }
360  $res = $ilDB->query($q);
361  $i = 0;
362  while($row = $ilDB->fetchAssoc($res))
363  {
364  $barometers[$i] = new ilFeedback();
365  $barometers[$i]->setAllData($row);
366  $i++;
367  }
368 
369  return $barometers ? $barometers : array();
370  }
371 
375  function delete()
376  {
377  global $ilDB;
378 
379  /*
380  foreach ($this->ids as $k => $v)
381  {
382  $this->ids[$k] = $ilDB->quote($v,'integer');
383  }
384  */
385 
386  $q = "DELETE FROM feedback_items WHERE ".
387  $ilDB->in("fb_id", $this->ids, false, "integer");
388 
389  //"fb_id IN (".implode(',',$this->ids).")";
390  $ilDB->manipulate($q);
391  $q = "DELETE FROM feedback_results WHERE ".
392  $ilDB->in("fb_id", $this->ids, false, "integer");
393  //"fb_id IN (".implode(',',$this->ids).")";
394  $ilDB->manipulate($q);
395  }
396 
400  function saveResult(){
401  global $ilDB;
402  //Save Only if there is not already a result from this user for this barometer
403 
404  if($this->canVote($this->user_id,$this->id)==1 || $this->user_id == 0)
405  {
406 /* $q = "INSERT INTO feedback_results (".
407  "fb_id,user_id,vote,note,votetime) VALUES (".
408  $ilDB->quote($this->id, "integer").", ".
409  $ilDB->quote($this->user_id, "integer").", ".
410  $ilDB->quote($this->vote, "integer").", ".
411  $ilDB->quote($this->note, "clob").", ".
412  $ilDB->quote(time(), "integer").")";
413  $ilDB->manipulate($q);*/
414  $ilDB->insert("feedback_results", array(
415  "fb_id" => array("integer", $this->id),
416  "user_id" => array("integer", $this->user_id),
417  "vote" => array("integer", $this->vote),
418  "note" => array("clob", $this->note),
419  "votetime" => array("integer", time())
420  ));
421  }
422  }
423 
427  function canVote($a_user_id,$a_fb_id){
428  global $ilDB, $ilUser;
429  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
430 
431  $q = "SELECT * FROM feedback_results WHERE ".
432  "fb_id = ".$ilDB->quote($a_fb_id, "integer")." AND ".
433  "user_id = ".$ilDB->quote($a_user_id, "integer").
434  " ORDER BY votetime DESC";;
435  $res = $ilDB->query($q);
436 
437  $row_results = $ilDB->fetchAssoc($res);
438  $q = "SELECT * FROM feedback_items WHERE ".
439  "fb_id = ".$ilDB->quote($a_fb_id, "integer");
440  $res1 = $ilDB->query($q);
441  $row_items = $ilDB->fetchAssoc($res1);
442 
443  // check end time
444  if (!($row_items["starttime"]<=time() && $row_items["endtime"]>=time()))
445  {
446  return (0);
447  }
448 
449  $members_obj = ilCourseParticipants::_getInstanceByObjId($row_items['obj_id']);
450 
451  //Check if the user is Member of that course, otherwise its not necessary that he votes
452  if(($res->numRows()==0) && $members_obj->isAssigned($ilUser->getId()))
453  return(1);
454 
455  if($members_obj->isAssigned($ilUser->getId()))
456  {
457  if($row_items['repeat_interval'] > 0){
458  $interval = $this->interval2seconds($row_items['repeat_interval'], $row_items['interval_unit']);
459  if((time() - $row_results['votetime']) >= $interval){
460  return(1);
461  }
462  }
463  }
464 
465 
466  return(0);
467  }
468 
472  function getChartData()
473  {
474  global $ilDB;
475 
476  if($this->user_id!='')
477  {
478  $user_filter = ' feedback_results.user_id = '.$ilDB->quote($this->user_id, "integer").' AND ';
479  }
480  $q='SELECT usr_data.login, feedback_results.user_id,feedback_results.vote, feedback_results.votetime FROM'.
481  ' feedback_results LEFT JOIN usr_data ON usr_data.usr_id = feedback_results.user_id WHERE '.
482  ' '.$user_filter.' feedback_results.fb_id='.$ilDB->quote($this->id, "integer").
483  ' ORDER BY feedback_results.votetime,usr_data.login';
484 
485  $res = $ilDB->query($q);
486  $i=0;
487  $j=1;
488  $k=1;
489  $n=0;
490  $pvt='';
491  $datapie[0][0] = 'Vote';
492  while($row = $ilDB->fetchAssoc($res))
493  {
494  $row["timelabel"] = date("d.m.Y H:i", $row["votetime"]);
495  if(!isset($tmp[$row['user_id']]))
496  {
497  $tmp[$row['user_id']]=$j++;
498  }
499  if(!isset($tmpv[$row['vote']]))
500  {
501  $tmpv[$row['vote']] = $k++;
502  }
503  $data[$i][0] = $row['timelabel'];
504  $data[$i][$tmp[$row['user_id']]] = $row['vote'];
505  $legend[$row['login']] = $row['login'];
506  $legendpie[$row['vote']] = $row['vote'];
507 
508 
509  $datapie[0][$tmpv[$row['vote']]]++;
510  if($row['votetime']!=$pvt){
511  $i++;
512 
513  }
514  $pvt=$row['votetime'];
515 
516  $table[$n]['votetime'] = $row['timelabel'];
517  $table[$n]['user'] = $row['login'];
518  $table[$n]['vote'] = $row['vote'];
519  $n++;
520  }
521  if(is_array($data))
522  {
523  foreach($data as $k => $v)
524  {
525  /* Look if there are set all Y-values. If a user has no Y value for a certain date, the Y value has to be set to something otherwise PHPlot will not work correctly.
526  The array keys have also to be sorted for PHPlot */
527  if(count($v)<=count($tmp)){
528  for($i=1;$i<=count($tmp);$i++)
529  if(!isset($v[$i]))
530  $data[$k][$i]='';
531  }
532  ksort($data[$k]);
533  }
534  }
535  return(array('data' => $data,'legend' => $legend,'legendpie' => $legendpie, 'datapie' => $datapie, 'table' => $table));
536 
537  }
541  function getNotes()
542  {
543  global $ilDB;
544 
545  if($this->user_id!='')
546  {
547  $user_filter = ' AND feedback_results.user_id='.$ilDB->quote($this->user_id, "integer");
548  }
549  $q='SELECT usr_data.login, feedback_results.user_id,feedback_results.note,feedback_results.vote, feedback_results.votetime FROM'.
550  ' feedback_results LEFT JOIN usr_data ON usr_data.usr_id = feedback_results.user_id'.
551  ' WHERE feedback_results.note IS NOT NULL '.
552  ' '.$user_filter.' AND feedback_results.fb_id='.$ilDB->quote($this->id, "integer").
553  ' ORDER BY feedback_results.votetime,usr_data.login';
554 
555  $res = $ilDB->query($q);
556  $i=0;
557  while($row = $ilDB->fetchAssoc($res))
558  {
559  $row["timelabel"] = date("d.m.Y H:i", $row["votetime"]);
560  $data[$i]['user'] = $row['login'];
561  $data[$i]['votetime'] = $row['timelabel'];
562  $data[$i]['note'] = $row['note'];
563  $i++;
564  }
565  return($data);
566  }
570  function getResultUsers()
571  {
572  global $ilDB;
573 
574  $q='SELECT distinct(usr_data.login), feedback_results.user_id, feedback_results.votetime '.
575  ' FROM '.
576  ' feedback_results LEFT JOIN usr_data ON usr_data.usr_id = feedback_results.user_id'.
577  ' WHERE feedback_results.fb_id='.$ilDB->quote($this->id, "integer").
578  ' ORDER BY feedback_results.votetime,usr_data.login';
579 
580  $res = $ilDB->query($q);
581 
582  while($row = $ilDB->fetchAssoc($res))
583  {
584  $users[$row['user_id']] = $row['login'];
585  }
586  return($users);
587  }
588 
593  function interval2seconds($a_interval,$a_interval_unit)
594  {
595  switch($a_interval_unit)
596  {
597  case 1:
598  //Days
599  $multi_by = 24 * 60 * 60;
600  break;
601  case 2:
602  //Weeks
603  $mult_by = 7 * 24 * 60 * 60;
604  break;
605  case 3:
606  // Months
607  $mult_by = 30 * 24 * 60 *60;
608  break;
609 
610  default:
611  //Hours
612  $mult_by = 60 * 60;
613  break;
614  }
615  $seconds = $a_interval * $mult_by;
616  return($seconds);
617  }
618 }
619 ?>