ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailbox.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 
24 
33 require_once("Services/Mail/classes/class.ilMail.php");
34 
35 class ilMailbox
36 {
42  var $ilias;
43 
49  var $lng;
50 
56  var $mtree;
57 
63  var $user_id;
64 
71  var $actions;
72 
79 
86 
93 
99  function ilMailbox($a_user_id = 0)
100  {
101  global $ilias,$lng;
102 
103 
104  $this->ilias = &$ilias;
105  $this->lng = &$lng;
106  $this->user_id = $a_user_id;
107 
108  $this->table_mail_obj_data = 'mail_obj_data';
109  $this->table_tree = 'mail_tree';
110 
111  if ($a_user_id)
112  {
113  $this->mtree = new ilTree($this->user_id);
114  $this->mtree->setTableNames($this->table_tree,$this->table_mail_obj_data);
115  }
116 
117  // i added this, becaus if i create a new user automatically during
118  // CAS authentication, we have no $lng variable (alex, 16.6.2006)
119  // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
120  if (is_object($this->lng))
121  {
122  $this->lng->loadLanguageModule("mail");
123 
124  $this->actions = array(
125  "moveMails" => $this->lng->txt("mail_move_to"),
126  "markMailsRead" => $this->lng->txt("mail_mark_read"),
127  "markMailsUnread" => $this->lng->txt("mail_mark_unread"),
128  "deleteMails" => $this->lng->txt("delete"));
129  }
130 
131  // array contains basic folders and there lng translation for every new user
132  $this->default_folder = array(
133  "b_inbox" => "inbox",
134  "c_trash" => "trash",
135  "d_drafts" => "drafts",
136  "e_sent" => "sent",
137  "z_local" => "local");
138 
139  }
144  function getInboxFolder()
145  {
146  global $ilDB;
147 
148  $res = $ilDB->queryf('
149  SELECT * FROM '.$this->table_mail_obj_data.'
150  WHERE user_id = %s
151  AND m_type = %s',
152  array('integer', 'text'),
153  array($this->user_id, 'inbox'));
154 
155  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
156 
157  return $row->obj_id;
158  }
159 
164  function getDraftsFolder()
165  {
166  global $ilDB;
167 
168  $res = $ilDB->queryf('
169  SELECT * FROM '.$this->table_mail_obj_data.'
170  WHERE user_id = %s
171  AND m_type = %s',
172  array('integer', 'text'),
173  array($this->user_id, 'drafts'));
174 
175  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
176 
177 
178  return $row->obj_id;
179  }
180 
185  function getTrashFolder()
186  {
187  global $ilDB;
188 
189  $res = $ilDB->queryf('
190  SELECT * FROM '.$this->table_mail_obj_data.'
191  WHERE user_id = %s
192  AND m_type = %s',
193  array('integer', 'text'),
194  array($this->user_id, 'trash'));
195 
196  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
197 
198  return $row->obj_id;
199  }
200 
205  function getSentFolder()
206  {
207  global $ilDB;
208 
209  $res = $ilDB->queryf('
210  SELECT * FROM '.$this->table_mail_obj_data.'
211  WHERE user_id = %s
212  AND m_type = %s',
213  array('integer', 'text'),
214  array($this->user_id, 'sent'));
215 
216  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
217 
218 
219  return $row->obj_id;
220  }
221 
226  function getRootFolderId()
227  {
228  return $this->mtree->getRootID($this->user_id);
229  }
230 
238  function getActions($a_mobj_id)
239  {
240  if ($a_mobj_id)
241  {
242  $folder_data = $this->getFolderData($a_mobj_id);
243 
244  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
245  {
246  #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
247  return $this->actions;
248  }
249  }
250 
251  return $this->actions;
252  }
253 
261  function _countNewMails($a_user_id)
262  {
263  global $ilDB;
264  global $ilias;
265 
266  if (!$a_user_id)
267  {
268  return 0;
269  }
270 
271  // CHECK FOR SYSTEM MAIL
272  $res = $ilDB->queryf('
273  SELECT count(mail_id) cnt FROM mail
274  WHERE folder_id = %s
275  AND user_id = %s
276  AND m_status = %s',
277  array('integer', 'integer', 'text'),
278  array('0', $a_user_id, 'unread'));
279 
280  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
281 
282  $res2 = $ilDB->queryf('
283  SELECT count(mail_id) cnt FROM mail m,mail_obj_data mo
284  WHERE m.user_id = mo.user_id
285  AND m.folder_id = mo.obj_id
286  AND mo.m_type = %s
287  AND m.user_id = %s
288  AND m.m_status = %s',
289  array('text', 'integer', 'text'),
290  array('inbox', $a_user_id, 'unread'));
291 
292 
293  $row2 = $res2->fetchRow(DB_FETCHMODE_OBJECT);
294 
295  return $row->cnt + $row2->cnt;
296  }
297 
303  {
304  global $ilDB;
305 
306 /* $root_id = $this->getLastInsertId();
307  ++$root_id;
308 */
309  $root_id = $ilDB->nextId($this->table_mail_obj_data);
310 
311  $res = $ilDB->manipulateF('
312  INSERT INTO '. $this->table_mail_obj_data .'
313  ( obj_id,
314  user_id,
315  title,
316  m_type
317  )
318  VALUES( %s, %s, %s, %s)',
319  array('integer','integer', 'text', 'text'),
320  array($root_id, $this->user_id, 'a_root', 'root'));
321 
322  $this->mtree->addTree($this->user_id,$root_id);
323 
324  foreach ($this->default_folder as $key => $folder)
325  {
326  /*$last_id = $this->getLastInsertId();
327  ++$last_id;
328  */
329  $last_id = $ilDB->nextId($this->table_mail_obj_data);
330  $statement = $ilDB->manipulateF('
331  INSERT INTO '. $this->table_mail_obj_data .'
332  ( obj_id,
333  user_id,
334  title,
335  m_type
336  )
337  VALUES( %s, %s, %s, %s)',
338  array('integer','integer', 'text', 'text'),
339  array($last_id,$this->user_id, $key, $folder));
340 
341  $this->mtree->insertNode($last_id,$root_id);
342  }
343  }
351  function addFolder($a_parent_id,$a_folder_name)
352  {
353  global $ilDB;
354 
355  if ($this->folderNameExists($a_folder_name))
356  {
357  return 0;
358  }
359  // ENTRY IN mail_obj_data
360  $next_id = $ilDB->nextId($this->table_mail_obj_data);
361  $statement = $ilDB->manipulateF('
362  INSERT INTO '. $this->table_mail_obj_data .'
363  ( obj_id,
364  user_id,
365  title,
366  m_type
367  )
368  VALUES(%s,%s,%s,%s)',
369  array('integer','integer', 'text', 'text'),
370  array($next_id, $this->user_id, $a_folder_name, 'user_folder'));
371 
372  // ENTRY IN mail_tree
373  $this->mtree->insertNode($next_id,$a_parent_id);
374  return $next_id;
375  }
376 
384  function renameFolder($a_obj_id, $a_new_folder_name)
385  {
386  global $ilDB;
387 
388  if ($this->folderNameExists($a_new_folder_name))
389  {
390  return false;
391  }
392 
393  $statement = $ilDB->manipulateF('
394  UPDATE '. $this->table_mail_obj_data .'
395  SET title = %s
396  WHERE obj_id = %s',
397  array('text', 'integer'),
398  array($a_new_folder_name, $a_obj_id));
399 
400  return true;
401  }
402 
409  function folderNameExists($a_folder_name)
410  {
411  global $ilDB;
412 
413  $res = $ilDB->queryf('
414  SELECT obj_id FROM '. $this->table_mail_obj_data .'
415  WHERE user_id = %s
416  AND title = %s',
417  array('integer', 'text'),
418  array($this->user_id, $a_folder_name));
419 
420  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
421 
422  return $row->obj_id ? true : false;
423  }
424 
430  function deleteFolder($a_folder_id)
431  {
432  global $ilDB;
433 
434  $query = $ilDB->queryf('
435  SELECT title FROM mail_obj_data
436  WHERE obj_id = %s',
437  array('integer'),
438  array($a_folder_id)
439  );
440 
441  $row = $ilDB->fetchAssoc($query);
442 
443  if( array_key_exists($row['title'], $this->default_folder) )
444  {
445  return false;
446  }
447 
448  include_once("Services/Mail/classes/class.ilMail.php");
449  $umail = new ilMail($this->user_id);
450 
451  // SAVE SUBTREE DATA
452  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
453 
454  // DELETE ENTRY IN TREE
455  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
456 
457  // DELETE ENTRY IN mobj_data
458  foreach($subtree as $node)
459  {
460  // DELETE mail(s) of folder(s)
461  $mails = $umail->getMailsOfFolder($node["obj_id"]);
462 
463  foreach ($mails as $mail)
464  {
465  $mail_ids[] = $mail["mail_id"];
466  }
467 
468  if (is_array($mail_ids))
469  {
470  $umail->deleteMails($mail_ids);
471  }
472 
473  // DELETE mobj_data entries
474  $statement = $ilDB->manipulateF('
475  DELETE FROM '. $this->table_mail_obj_data .'
476  WHERE obj_id = %s',
477  array('integer'),
478  array($node['obj_id']));
479  }
480 
481  return true;
482  }
483 
484  // DONE: can be substituted by ilUtil::getLastInsertId
485  function getLastInsertId()
486  {
487  global $ilDB;
488 
489  return $ilDB->getLastInsertId();
490  }
491 
497  function getFolderData($a_obj_id)
498  {
499  global $ilDB;
500 
501  $res = $ilDB->queryf('
502  SELECT * FROM '. $this->table_mail_obj_data .'
503  WHERE user_id = %s
504  AND obj_id = %s',
505  array('integer', 'integer'),
506  array($this->user_id, $a_obj_id));
507 
508  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
509 
510  return array(
511  "title" => stripslashes($row->title),
512  "type" => $row->m_type
513  );
514  }
520  function getParentFolderId($a_obj_id)
521  {
522  global $ilDB;
523 
524  $res = $ilDB->queryf('
525  SELECT * FROM '. $this->table_tree .'
526  WHERE child = %s',
527  array('integer'),
528  array($a_obj_id));
529 
530  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
531 
532 
533  return $row->parent;
534  }
541  function getSubFolders($a_folder = 0,$a_folder_parent = 0)
542  {
543 
544  global $ilDB;
545 
546  if (!$a_folder)
547  {
548  $a_folder = $this->getRootFolderId();
549  }
550 
551  foreach ($this->default_folder as $key => $value)
552  {
553  $res = $ilDB->queryf('
554  SELECT obj_id,m_type FROM '. $this->table_mail_obj_data .'
555  WHERE user_id = %s
556  AND title = %s',
557  array('integer', 'text'),
558  array($this->user_id, $key));
559 
560  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
561 
562  $user_folder[] = array(
563  "title" => $key,
564  "type" => $row->m_type,
565  "obj_id" => $row->obj_id);
566  }
567 
568  $res = $ilDB->queryf('
569  SELECT * FROM '. $this->table_tree. ', '. $this->table_mail_obj_data .'
570  WHERE '. $this->table_mail_obj_data.'.obj_id = '. $this->table_tree.'.child
571  AND '. $this->table_tree.'.depth > %s
572  AND '. $this->table_tree.'.tree = %s
573  ORDER BY '. $this->table_mail_obj_data.'.title ',
574  array('integer', 'integer'),
575  array('2', $this->user_id));
576 
577  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
578  {
579  $user_folder[] = array(
580  "title" => stripslashes($row->title),
581  "type" => $row->m_type,
582  "obj_id" => $row->child);
583  }
584 
585  return $user_folder;
586  }
587 
593  function setUserId($a_user_id)
594  {
595  $this->user_id = $a_user_id;
596  }
597 
606  function delete()
607  {
608  global $ilDB;
609 
610  $data = array($this->user_id);
611 
612  $statement = $ilDB->manipulateF('
613  DELETE FROM mail_obj_data WHERE user_id = %s',
614  array('integer'), array($this->user_id)
615  );
616 
617  $statement = $ilDB->manipulateF('
618  DELETE FROM mail_options WHERE user_id = %s',
619  array('integer'), array($this->user_id)
620  );
621 
622  $statement = $ilDB->manipulateF('
623  DELETE FROM mail_saved WHERE user_id = %s',
624  array('integer'), array($this->user_id)
625  );
626 
627  $statement = $ilDB->manipulateF('
628  DELETE FROM mail_tree WHERE tree = %s',
629  array('integer'), array($this->user_id)
630  );
631 
632  return true;
633  }
634 
643  {
644  global $ilDB;
645 
646  $tmp_user =& ilObjectFactory::getInstanceByObjId($this->user_id,false);
647 
648  $statement = $ilDB->manipulateF('
649  UPDATE mail
650  SET sender_id = %s,
651  import_name = %s
652  WHERE sender_id = %s',
653  array('integer', 'text', 'integer'),
654  array('0', $tmp_user->getLogin(), $this->user_id));
655 
656  return true;
657  }
658 
659 }
660 ?>