ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFeedback.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35 class ilFeedback {
36  function ilFeedback($a_id = 0){
37  if($a_id > 0){
38  $this->id = $a_id;
39  $this->getBarometer();
40  }
41 
42  }
43  /* Set / Get Methods */
44  function setId($a_id){
45  $this->id = $a_id;
46  }
47  function getId(){
48  return($this->id);
49  }
50  function setTitle($a_title){
51  $this->title = $a_title;
52  }
53  function getTitle(){
54  return($this->title);
55  }
56  function setDescription($a_description){
57  $this->description = $a_description;
58  }
59  function getDescription(){
60  return($this->description);
61  }
62  function setAnonymous($a_anonymous){
63  $this->anonymous = $a_anonymous;
64  }
65 
66  function getAnonymous(){
67  return($this->anonymous);
68  }
69  function setRequired($a_required){
70  $this->required = $a_required;
71  }
72 
73  function getRequired(){
74  return($this->required);
75  }
76  function setShowOn($a_show_location){
77  $this->show_on = $a_show_location;
78  }
79 
80  function getShowOn(){
81  return($this->show_on);
82  }
83  function setVotes($a_votes){
84  $this->votes = $a_votes;
85  }
86 
87  function getVotes(){
88  return($this->votes);
89  }
90  function setStarttime($a_starttime){
91  $this->starttime = $a_starttime;
92  }
93 
94  function getStarttime(){
95  return($this->starttime);
96  }
97  function setEndtime($a_endtime){
98  $this->endtime = $a_endtime;
99  }
100 
101  function getEndtime(){
102  return($this->endtime);
103  }
104  function setInterval($a_interval){
105  $this->interval = $a_interval;
106  }
107 
108  function getInterval(){
109  return($this->interval);
110  }
111  function setIntervalUnit($a_interval_unit){
112  $this->interval_unit = $a_interval_unit;
113  }
114 
115  function getIntervalUnit(){
116  return($this->interval_unit);
117  }
118  function setFirstVoteBest($a_first_vote_best){
119  $this->first_vote_best = $a_first_vote_best;
120  }
121 
122  function getFirstVoteBest(){
123  return($this->first_vote_best);
124  }
125  function setObjId($a_obj_id){
126  $this->obj_id = $a_obj_id;
127  }
128 
129  function getObjId(){
130  return($this->obj_id);
131  }
132  function setRefId($a_ref_id){
133  $this->ref_id = $a_ref_id;
134  }
135  function getRefId(){
136  return($this->ref_id);
137  }
138  function setTextAnswer($a_text_answer){
139  $this->text_answer = $a_text_answer;
140  }
141 
142  function getTextAnswer(){
143  return($this->text_answer);
144  }
145  function setIds($a_ids){
146  $this->ids = $a_ids;
147  }
148  function setUserId($a_user_id){
149  $this->user_id = $a_user_id;
150  }
151  function setVote($a_vote){
152  $this->vote = $a_vote;
153  }
154  function setNote($a_note){
155  $this->note = $a_note;
156  }
157 
161  function setAllData($a_barometer){
162  $this->setId($a_barometer['fb_id']);
163  $this->setTitle($a_barometer['title']);
164  $this->setDescription($a_barometer['description']);
165  $this->setAnonymous($a_barometer['anonymous']);
166  $this->setRequired($a_barometer['required']);
167  $this->setShowOn($a_barometer['show_on']);
168  $this->setVotes($a_barometer['votes']);
169  $this->setStarttime($a_barometer['starttime']);
170  $this->setEndtime($a_barometer['endtime']);
171  $this->setInterval($a_barometer['repeat_interval']);
172  $this->setIntervalUnit($a_barometer['interval_unit']);
173  $this->setFirstVoteBest($a_barometer['first_vote_best']);
174  $this->setTextAnswer($a_barometer['text_answer']);
175  $this->setObjId($a_barometer['obj_id']);
176  $this->setRefId($a_barometer['ref_id']);
177  }
178 
182  function create(){
183  global $ilDB;
184  $q = "INSERT INTO feedback_items (title, description, anonymous,".
185  "required, show_on, text_answer, votes, starttime, endtime, ".
186  "repeat_interval, interval_unit, first_vote_best, ref_id,obj_id) VALUES(".
187  $ilDB->quote($this->title).", ".
188  $ilDB->quote($this->description).", ".
189  $ilDB->quote($this->anonymous).", ".
190  $ilDB->quote($this->required).", ".
191  $ilDB->quote($this->show_on).", ".
192  $ilDB->quote($this->text_answer).", ".
193  $ilDB->quote($this->votes).", ".
194  $ilDB->quote($this->starttime).", ".
195  $ilDB->quote($this->endtime).", ".
196  $ilDB->quote($this->interval).", ".
197  $ilDB->quote($this->interval_unit).", ".
198  $ilDB->quote($this->first_vote_best).", ".
199  $ilDB->quote($this->ref_id).", ".
200  $ilDB->quote($this->obj_id).")";
201  $ilDB->query($q);
202  $this->id = $ilDB->getLastInsertId();
203 
204 
205  }
206 
210  function update(){
211  global $ilDB;
212  $q = "UPDATE feedback_items set ".
213  "title=".$ilDB->quote($this->title).", ".
214  "description=".$ilDB->quote($this->description).", ".
215  "anonymous=".$ilDB->quote($this->anonymous).", ".
216  "required=".$ilDB->quote($this->required).", ".
217  "show_on=".$ilDB->quote($this->show_on).", ".
218  "text_answer=".$ilDB->quote($this->text_answer).", ".
219  "votes=".$ilDB->quote($this->votes).", ".
220  "starttime=".$ilDB->quote($this->starttime).", ".
221  "endtime=".$ilDB->quote($this->endtime).", ".
222  "repeat_interval=".$ilDB->quote($this->interval).", ".
223  "interval_unit=".$ilDB->quote($this->interval_unit).", ".
224  "first_vote_best=".$ilDB->quote($this->first_vote_best)." WHERE fb_id=".$ilDB->quote($this->id);
225 
226  $ilDB->query($q);
227  $this->id = $ilDB->getLastInsertId();
228 
229 
230  }
231 
235  function getBarometer(){
236  global $ilDB;
237  $q = "SELECT * FROM feedback_items WHERE fb_id=".$ilDB->quote($this->id);
238  $res = $ilDB->query($q);
239  if($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
240  $this->setAllData($row);
241 
242 
243  }
248  global $ilDB;
249  $q = "SELECT * FROM feedback_items WHERE obj_id=".$ilDB->quote($this->obj_id);
250  $res = $ilDB->query($q);
251  if($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
252  $this->setAllData($row);
253 
254  }
255 
260  global $ilDB;
261  $q = "SELECT * FROM feedback_items WHERE ref_id=".$ilDB->quote($this->ref_id);
262  $res = $ilDB->query($q);
263  if($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
264  $this->setAllData($row);
265 
266  }
267 
271  function getFeedback($required=0){
272  global $ilDB;
273  $filter_req = ($required) ? ' required=1 AND ' : '';
274  $q = "SELECT * FROM feedback_items WHERE ".
275  $filter_req.
276  " ((starttime<=UNIX_TIMESTAMP() AND".
277  " endtime>=UNIX_TIMESTAMP()) OR(starttime<=0 AND endtime<=0))";
278  $res = $ilDB->query($q);
279  if($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
280  $this->setAllData($row);
281  }
282 
288  function getAllBarometer($a_show_inactive=1,$a_only_req=0){
289  global $ilDB;
290 
291  if($this->ref_id)
292  $where.=" ref_id=".$ilDB->quote($this->ref_id);
293  if($a_only_req==1)
294  if($where!='')
295  $where .= ' AND required=1 ';
296  else
297  $where = ' required = 1 ';
298  $q = "SELECT * FROM feedback_items WHERE ".$where;
299 
300  if($a_show_inactive==0){
301  if($where!='')
302  $where = ' AND'.$where;
303  $q = "SELECT * FROM feedback_items WHERE ".
304  " ((starttime<=UNIX_TIMESTAMP() AND".
305  " endtime>=UNIX_TIMESTAMP()) OR(starttime<=0 AND endtime<=0))".$where;
306  }
307  $res = $ilDB->query($q);
308  $i = 0;
309  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)){
310  $barometers[$i] = new ilFeedback();
311  $barometers[$i]->setAllData($row);
312  $i++;
313  }
314 
315  return $barometers ? $barometers : array();
316  }
320  function delete(){
321  global $ilDB;
322  foreach ($this->ids as $k => $v)
323  $this->ids[$k] = $ilDB->quote($v);
324  $q = "DELETE FROM feedback_items WHERE ".
325  "fb_id IN (".implode(',',$this->ids).")";
326  $ilDB->query($q);
327  $q = "DELETE FROM feedback_results WHERE ".
328  "fb_id IN (".implode(',',$this->ids).")";
329  $ilDB->query($q);
330  }
331 
335  function saveResult(){
336  global $ilDB;
337  //Save Only if there is not already a result from this user for this barometer
338 
339  if($this->canVote($this->user_id,$this->id)==1 || $this->user_id == 0){
340  $q = "INSERT INTO feedback_results (".
341  "fb_id,user_id,vote,note,votetime) VALUES (".
342  $ilDB->quote($this->id).", ".
343  $ilDB->quote($this->user_id).", ".
344  $ilDB->quote($this->vote).", ".
345  $ilDB->quote($this->note).", UNIX_TIMESTAMP())";
346  $ilDB->query($q);
347  }
348  }
349 
353  function canVote($a_user_id,$a_fb_id){
354  global $ilDB, $ilUser;
355  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
356 
357  $q = "SELECT * FROM feedback_results WHERE ".
358  "fb_id=".$ilDB->quote($a_fb_id)." AND ".
359  "user_id=".$ilDB->quote($a_user_id)." ORDER BY votetime DESC";;
360  $res = $ilDB->query($q);
361 
362  $row_results = $res->fetchRow(DB_FETCHMODE_ASSOC);
363  $q = "SELECT * FROM feedback_items WHERE ".
364  "fb_id = ".$ilDB->quote($a_fb_id);
365  $res1 = $ilDB->query($q);
366  $row_items = $res1->fetchRow(DB_FETCHMODE_ASSOC);
367 
368  // check end time
369  if (!($row_items["starttime"]<=time() && $row_items["endtime"]>=time()))
370  {
371  return (0);
372  }
373 
374  $members_obj = ilCourseParticipants::_getInstanceByObjId($row_items['obj_id']);
375 
376  //Check if the user is Member of that course, otherwise its not necessary that he votes
377  if(($res->numRows()==0) && $members_obj->isAssigned($ilUser->getId()))
378  return(1);
379 
380  if($members_obj->isAssigned($ilUser->getId()))
381  {
382  if($row_items['repeat_interval'] > 0){
383  $interval = $this->interval2seconds($row_items['repeat_interval'], $row_items['interval_unit']);
384  if((time() - $row_results['votetime']) >= $interval){
385  return(1);
386  }
387  }
388  }
389 
390 
391  return(0);
392  }
393 
397  function getChartData(){
398  global $ilDB;
399  if($this->user_id!='')
400  $user_filter = ' feedback_results.user_id='.$ilDB->quote($this->user_id).' AND ';
401  $q='SELECT usr_data.login, feedback_results.user_id,feedback_results.vote, feedback_results.votetime, FROM_UNIXTIME(feedback_results.votetime,"%d.%m.%Y %H:%i") as timelabel FROM'.
402  ' feedback_results LEFT JOIN usr_data ON usr_data.usr_id = feedback_results.user_id WHERE '.
403  ' '.$user_filter.' feedback_results.fb_id='.$ilDB->quote($this->id).
404  ' ORDER BY feedback_results.votetime,usr_data.login';
405 
406  $res = $ilDB->query($q);
407  $i=0;
408  $j=1;
409  $k=1;
410  $n=0;
411  $pvt='';
412  $datapie[0][0] = 'Vote';
413  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)){
414 
415  if(!isset($tmp[$row['user_id']]))
416  $tmp[$row['user_id']]=$j++;
417  if(!isset($tmpv[$row['vote']]))
418  $tmpv[$row['vote']] = $k++;
419  $data[$i][0] = $row['timelabel'];
420  $data[$i][$tmp[$row['user_id']]] = $row['vote'];
421  $legend[$row['login']] = $row['login'];
422  $legendpie[$row['vote']] = $row['vote'];
423 
424 
425  $datapie[0][$tmpv[$row['vote']]]++;
426  if($row['votetime']!=$pvt){
427  $i++;
428 
429  }
430  $pvt=$row['votetime'];
431 
432  $table[$n]['votetime'] = $row['timelabel'];
433  $table[$n]['user'] = $row['login'];
434  $table[$n]['vote'] = $row['vote'];
435  $n++;
436  }
437  if(is_array($data)){
438  foreach($data as $k => $v){
439  /* 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.
440  The array keys have also to be sorted for PHPlot */
441  if(count($v)<=count($tmp)){
442  for($i=1;$i<=count($tmp);$i++)
443  if(!isset($v[$i]))
444  $data[$k][$i]='';
445  }
446  ksort($data[$k]);
447  }
448  }
449  return(array('data' => $data,'legend' => $legend,'legendpie' => $legendpie, 'datapie' => $datapie, 'table' => $table));
450 
451  }
455  function getNotes(){
456  global $ilDB;
457  if($this->user_id!='')
458  $user_filter = ' AND feedback_results.user_id='.$ilDB->quote($this->user_id);
459  $q='SELECT usr_data.login, feedback_results.user_id,feedback_results.note,feedback_results.vote, feedback_results.votetime, FROM_UNIXTIME(feedback_results.votetime,"%d.%m.%Y %H:%i") as timelabel FROM'.
460  ' feedback_results LEFT JOIN usr_data ON usr_data.usr_id = feedback_results.user_id'.
461  ' WHERE feedback_results.note<>""'.
462  ' '.$user_filter.' AND feedback_results.fb_id='.$ilDB->quote($this->id).
463  ' ORDER BY feedback_results.votetime,usr_data.login';
464 
465  $res = $ilDB->query($q);
466  $i=0;
467  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)){
468  $data[$i]['user'] = $row['login'];
469  $data[$i]['votetime'] = $row['timelabel'];
470  $data[$i]['note'] = $row['note'];
471  $i++;
472  }
473  return($data);
474  }
478  function getResultUsers(){
479 
480  global $ilDB;
481  $q='SELECT distinct(usr_data.login), feedback_results.user_id FROM'.
482  ' feedback_results LEFT JOIN usr_data ON usr_data.usr_id = feedback_results.user_id'.
483  ' WHERE feedback_results.fb_id='.$ilDB->quote($this->id).
484  ' ORDER BY feedback_results.votetime,usr_data.login';
485 
486  $res = $ilDB->query($q);
487 
488  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)){
489  $users[$row['user_id']] = $row['login'];
490  }
491  return($users);
492  }
493 
498  function interval2seconds($a_interval,$a_interval_unit){
499  switch($a_interval_unit){
500  case 1:
501  //Days
502  $multi_by = 24 * 60 * 60;
503  break;
504  case 2:
505  //Weeks
506  $mult_by = 7 * 24 * 60 * 60;
507  break;
508  case 3:
509  // Months
510  $mult_by = 30 * 24 * 60 *60;
511  break;
512 
513  default:
514  //Hours
515  $mult_by = 60 * 60;
516  break;
517  }
518  $seconds = $a_interval * $mult_by;
519  return($seconds);
520  }
521 }
522 ?>