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