ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailSearchCoursesGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once './Services/User/classes/class.ilObjUser.php';
6 require_once "Services/Mail/classes/class.ilMailbox.php";
7 require_once "Services/Mail/classes/class.ilFormatMail.php";
8 require_once "Services/Contact/classes/class.ilAddressbook.php";
9 
17 {
18  private $tpl = null;
19  private $ctrl = null;
20  private $lng = null;
21 
22  private $umail = null;
23  private $abook = null;
24 
25  protected $mailing_allowed;
26 
27  public function __construct($wsp_access_handler = null, $wsp_node_id = null)
28  {
29  global $tpl, $ilCtrl, $lng, $ilUser, $rbacsystem;
30 
31  $this->tpl = $tpl;
32  $this->ctrl = $ilCtrl;
33  $this->lng = $lng;
34 
35  // personal workspace
36  $this->wsp_access_handler = $wsp_access_handler;
37  $this->wsp_node_id = $wsp_node_id;
38 
39  // check if current user may send mails
40  include_once "Services/Mail/classes/class.ilMail.php";
41  $mail = new ilMail($_SESSION["AccountId"]);
42  $this->mailing_allowed = $rbacsystem->checkAccess('internal_mail',$mail->getMailObjectReferenceId());
43 
44  $this->ctrl->saveParameter($this, "mobj_id");
45  $this->ctrl->saveParameter($this, "ref");
46 
47  $this->umail = new ilFormatMail($ilUser->getId());
48  $this->abook = new ilAddressbook($ilUser->getId());
49  }
50 
51  public function executeCommand()
52  {
53  $forward_class = $this->ctrl->getNextClass($this);
54  switch($forward_class)
55  {
56  default:
57  if (!($cmd = $this->ctrl->getCmd()))
58  {
59  $cmd = "showMyCourses";
60  }
61 
62  $this->$cmd();
63  break;
64  }
65  return true;
66  }
67 
68  function mail()
69  {
70  global $ilUser, $lng;
71  if ($_GET["view"] == "mycourses")
72  {
73  $ids = ((int)$_GET['search_crs']) ? array((int)$_GET['search_crs']) : $_POST['search_crs'];
74 
75  if ($ids)
76  {
77  $this->mailCourses();
78  }
79  else
80  {
81  ilUtil::sendInfo($lng->txt("mail_select_course"));
82  $this->showMyCourses();
83  }
84  }
85  else if ($_GET["view"] == "crs_members")
86  {
87  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
88  if ($ids)
89  {
90  $this->mailMembers();
91  }
92  else
93  {
94  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
95  $this->showMembers();
96  }
97  }
98  else
99  {
100  $this->showMyCourses();
101  }
102  }
103 
104  function mailCourses()
105  {
106  global $ilUser, $lng, $rbacreview;
107 
108  $members = array();
109 
110  if (!is_array($old_mail_data = $this->umail->getSavedData()))
111  {
112  $this->umail->savePostData(
113  $ilUser->getId(),
114  array(),
115  "",
116  "",
117  "",
118  "",
119  "",
120  "",
121  "",
122  ""
123  );
124  }
125 
126  require_once './Services/Object/classes/class.ilObject.php';
127 
128  $ids = ((int)$_GET['search_crs']) ? array((int)$_GET['search_crs']) : $_POST['search_crs'];
129 
130  foreach ($ids as $crs_id)
131  {
132  $ref_ids = ilObject::_getAllReferences($crs_id);
133 
134  foreach ($ref_ids as $ref_id)
135  {
136  $roles = $rbacreview->getAssignableChildRoles($ref_id);
137  foreach ($roles as $role)
138  {
139  if (substr($role['title'], 0, 14) == 'il_crs_member_' ||
140  substr($role['title'], 0, 13) == 'il_crs_tutor_' ||
141  substr($role['title'], 0, 13) == 'il_crs_admin_')
142  {
143  if(isset($old_mail_data['rcp_to']) &&
144  trim($old_mail_data['rcp_to']) != '')
145  {
146  $rcpt = $rbacreview->getRoleMailboxAddress($role['obj_id']);
147 
148  if(!$this->umail->doesRecipientStillExists($rcpt, $old_mail_data['rcp_to']))
149  // does not work if Pear is enabled and Mailbox Address contain special chars!!
150 // array_push($members, $rcpt);
151  // FIX for Mantis: 7523, 8061
152  array_push($members, '#'.$role['title']);
153 
154  unset($rcpt);
155  }
156  else
157  {
158  // does not work if Pear is enabled and Mailbox Address contain special chars!!
159 // array_push($members, $rbacreview->getRoleMailboxAddress($role['obj_id']));
160  // FIX for Mantis: 7523, 8061
161  array_push($members, '#'.$role['title']);
162  }
163  }
164  }
165  }
166  }
167 
168  if(count($members))
169  $mail_data = $this->umail->appendSearchResult($members, 'to');
170  else
171  $mail_data = $this->umail->getSavedData();
172 
173  $this->umail->savePostData(
174  $mail_data["user_id"],
175  $mail_data["attachments"],
176  $mail_data["rcp_to"],
177  $mail_data["rcp_cc"],
178  $mail_data["rcp_bcc"],
179  $mail_data["m_type"],
180  $mail_data["m_email"],
181  $mail_data["m_subject"],
182  $mail_data["m_message"],
183  $mail_data["use_placeholders"]
184  );
185 
186  #$this->ctrl->returnToParent($this);
187  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
188  }
189 
190  function mailMembers()
191  {
192  global $ilUser;
193 
194  $members = array();
195 
196  if (!is_array($this->umail->getSavedData()))
197  {
198  $this->umail->savePostData(
199  $ilUser->getId(),
200  array(),
201  "",
202  "",
203  "",
204  "",
205  "",
206  "",
207  "",
208  ""
209  );
210  }
211 
212  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
213 
214  foreach ($ids as $member)
215  {
216  $login = ilObjUser::_lookupLogin($member);
217  array_push($members, $login);
218  }
219  $mail_data = $this->umail->appendSearchResult($members,"to");
220 
221  $this->umail->savePostData(
222  $mail_data["user_id"],
223  $mail_data["attachments"],
224  $mail_data["rcp_to"],
225  $mail_data["rcp_cc"],
226  $mail_data["rcp_bcc"],
227  $mail_data["m_type"],
228  $mail_data["m_email"],
229  $mail_data["m_subject"],
230  $mail_data["m_message"],
231  $mail_data["use_placeholders"]
232  );
233 
234  #$this->ctrl->returnToParent($this);
235  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
236  }
237 
241  public function adoptMembers()
242  {
243  global $lng;
244  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
245 
246  if ((int)$ids && !is_array($ids))
247  $ids = array((int)$ids);
248 
249  if ($ids )
250  {
251  $members = array();
252 
253  foreach ($ids as $member)
254  {
255  $login = ilObjUser::_lookupLogin($member);
256 
257  if (!$this->abook->checkEntry($login))
258  {
259  $name = ilObjUser::_lookupName($member);
260  $email = '';
261  if(ilObjUser::_lookupPref((int)$member, 'public_email') == 'y')
262  {
263  $email = ilObjUser::_lookupEmail($member);
264  }
265  $this->abook->addEntry(
266  $login,
267  $name["firstname"],
268  $name["lastname"],
269  $email
270  );
271  }
272  }
273  ilUtil::sendInfo($lng->txt("mail_members_added_addressbook"));
274  }
275  else
276  {
277  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
278  }
279 
280  $this->showMembers();
281  }
282 
286  function cancel()
287  {
288  if ($_GET["view"] == "mycourses" &&
289  $_GET["ref"] == "mail")
290  {
291  $this->ctrl->returnToParent($this);
292  }
293  else
294  {
295  $this->showMyCourses();
296  }
297  }
298 
302  public function showMyCourses()
303  {
304  global $lng, $ilUser, $ilObjDataCache, $tree, $tpl, $rbacsystem;
305 
306  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
307 
308  $this->tpl->setTitle($this->lng->txt('mail_addressbook') );
309 
310  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
311 
312  $_GET['view'] = 'mycourses';
313 
314  $lng->loadLanguageModule('crs');
315 
316  include_once 'Services/Contact/classes/class.ilMailSearchCoursesTableGUI.php';
317  $table = new ilMailSearchCoursesTableGUI($this, "crs", $_GET["ref"]);
318  $table->setId('search_crs_tbl');
319  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
320  $crs_ids = ilCourseParticipants::_getMembershipByType($ilUser->getId(), 'crs');
321  $counter = 0;
322  $tableData = array();
323  if (is_array($crs_ids) && count($crs_ids) > 0)
324  {
325  $num_courses_hidden_members = 0;
326  include_once("./Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php");
327  foreach($crs_ids as $crs_id)
328  {
332  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
333 
334  $isOffline = !$oTmpCrs->isActivated();
335  $hasUntrashedReferences = ilObject::_hasUntrashedReference($crs_id);
336  $showMemberListEnabled = (boolean)$oTmpCrs->getShowMembers();
337  $ref_ids = array_keys(ilObject::_getAllReferences($crs_id));
338  $isPrivilegedUser = $rbacsystem->checkAccess('write', $ref_ids[0]);
339 
340  if($hasUntrashedReferences && ((!$isOffline && $showMemberListEnabled) || $isPrivilegedUser))
341  {
342  $oCrsParticipants = ilCourseParticipants::_getInstanceByObjId($crs_id);
343  $crs_members = $oCrsParticipants->getParticipants();
344 
345  foreach($crs_members as $key => $member)
346  {
347  $tmp_usr = new ilObjUser($member);
348  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
349  {
350  unset($crs_members[$key]);
351  }
352  }
353  unset($tmp_usr);
354 
355  $hiddenMembers = false;
356  if((int)$oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
357  {
358  ++$num_courses_hidden_members;
359  $hiddenMembers = true;
360  }
361  unset($oTmpCrs);
362 
363  $ref_ids = ilObject::_getAllReferences($crs_id);
364  $ref_id = current($ref_ids);
365  $path_arr = $tree->getPathFull($ref_id, $tree->getRootId());
366  $path_counter = 0;
367  $path = '';
368  foreach($path_arr as $data)
369  {
370  if($path_counter++)
371  {
372  $path .= " -> ";
373  }
374  $path .= $data['title'];
375  }
376  $path = $this->lng->txt('path').': '.$path;
377 
378  $current_selection_list = new ilAdvancedSelectionListGUI();
379  $current_selection_list->setListTitle($this->lng->txt("actions"));
380  $current_selection_list->setId("act_".$counter);
381 
382  $this->ctrl->setParameter($this, 'search_crs', $crs_id);
383  $this->ctrl->setParameter($this, 'view', 'mycourses');
384 
385  if($_GET["ref"] == "mail")
386  {
387  if ($this->mailing_allowed)
388  $current_selection_list->addItem($this->lng->txt("mail_members"), '', $this->ctrl->getLinkTarget($this, "mail"));
389  }
390  else if($_GET["ref"] == "wsp")
391  {
392  $current_selection_list->addItem($this->lng->txt("wsp_share_with_members"), '', $this->ctrl->getLinkTarget($this, "share"));
393  }
394  $current_selection_list->addItem($this->lng->txt("mail_list_members"), '', $this->ctrl->getLinkTarget($this, "showMembers"));
395 
396  $this->ctrl->clearParameters($this);
397 
398  $rowData = array
399  (
400  "CRS_ID" => $crs_id,
401  "CRS_NAME" => $ilObjDataCache->lookupTitle($crs_id),
402  "CRS_NO_MEMBERS" => count($crs_members),
403  "CRS_PATH" => $path,
404  'COMMAND_SELECTION_LIST' => $current_selection_list->getHTML(),
405  "hidden_members" => $hiddenMembers,
406  );
407  $counter++;
408  $tableData[] = $rowData;
409  }
410  }
411 
412  //if((int)$counter)
413  //{
414  // $table->addCommandButton('mail',$lng->txt('mail_members'));
415  // $table->addCommandButton('showMembers',$lng->txt('mail_list_members'));
416  //}
417 
418  if($num_courses_hidden_members > 0)
419  {
420  $searchTpl->setCurrentBlock('caption_block');
421  $searchTpl->setVariable('TXT_LIST_MEMBERS_NOT_AVAILABLE', $this->lng->txt('mail_crs_list_members_not_available'));
422  $searchTpl->parseCurrentBlock();
423  }
424  }
425 
426  $searchTpl->setVariable('TXT_MARKED_ENTRIES', $lng->txt('marked_entries'));
427 
428  $table->setData($tableData);
429  if($_GET['ref'] == 'mail') $this->tpl->setVariable('BUTTON_CANCEL', $lng->txt('cancel'));
430 
431  $searchTpl->setVariable('TABLE', $table->getHtml());
432  $tpl->setContent($searchTpl->get());
433 
434  if($_GET["ref"] != "wsp")
435  {
436  $tpl->show();
437  }
438  }
439 
443  public function showMembers()
444  {
445  global $lng, $ilUser, $ilObjDataCache;
446 
447  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
448 
449  if ($_GET["search_crs"] != "")
450  {
451  $_POST["search_crs"] = explode(",", $_GET["search_crs"]);
452  $_GET["search_crs"] = "";
453  }
454  else if ($_SESSION["search_crs"] != "")
455  {
456  $_POST["search_crs"] = explode(",", $_SESSION["search_crs"]);
457  $_SESSION["search_crs"] = "";
458  }
459 
460  if(is_array($_POST['search_crs']))
461  {
462  $_POST['search_crs'] = array_filter(array_map('intval', $_POST['search_crs']));
463  }
464 
465  if (!is_array($_POST["search_crs"]) ||
466  count($_POST["search_crs"]) == 0)
467  {
468  ilUtil::sendInfo($lng->txt("mail_select_course"));
469  $this->showMyCourses();
470  }
471  else
472  {
473  foreach($_POST['search_crs'] as $crs_id)
474  {
475  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
476  if($oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
477  {
478  unset($_POST['search_crs']);
479  ilUtil::sendInfo($lng->txt('mail_crs_list_members_not_available_for_at_least_one_crs'));
480  return $this->showMyCourses();
481  }
482  unset($oTmpCrs);
483  }
484 
485  $this->tpl->setTitle($this->lng->txt("mail_addressbook"));
486 
487  $this->ctrl->setParameter($this, "view", "crs_members");
488  if ($_GET["ref"] != "") $this->ctrl->setParameter($this, "ref", $_GET["ref"]);
489  if (is_array($_POST["search_crs"])) $this->ctrl->setParameter($this, "search_crs", implode(",", $_POST["search_crs"]));
490  $this->tpl->setVariable("ACTION", $this->ctrl->getFormAction($this));
491  $this->ctrl->clearParameters($this);
492 
493  $lng->loadLanguageModule('crs');
494  include_once 'Services/Contact/classes/class.ilMailSearchCoursesMembersTableGUI.php';
495  $context = $_GET["ref"] ? $_GET["ref"] : "mail";
496  $table = new ilMailSearchCoursesMembersTableGUI($this, 'crs', $context);
497  $table->setId('show_crs_mmbrs_tbl');
498  $tableData = array();
499  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
500  foreach($_POST["search_crs"] as $crs_id)
501  {
502  $members_obj = ilCourseParticipants::_getinstanceByObjId($crs_id);
503  $tmp_members = $members_obj->getParticipants();
504  $course_members = ilUtil::_sortIds($tmp_members, 'usr_data', 'lastname', 'usr_id');
505 
506  foreach ($course_members as $member)
507  {
508  $tmp_usr = new ilObjUser($member);
509  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
510  {
511  unset($tmp_usr);
512  continue;
513  }
514  unset($tmp_usr);
515 
516  $name = ilObjUser::_lookupName($member);
517  $login = ilObjUser::_lookupLogin($member);
518 
519  $fullname = "";
520  if(in_array(ilObjUser::_lookupPref($member, 'public_profile'), array("g", 'y')))
521  $fullname = $name['lastname'].', '.$name['firstname'];
522 
523  $rowData = array(
524  'members_id' => $member,
525  'members_login' => $login,
526  'members_name' => $fullname,
527  'members_crs_grp' => $ilObjDataCache->lookupTitle($crs_id),
528  'members_in_addressbook' => $this->abook->checkEntryByLogin($login) ? $lng->txt("yes") : $lng->txt("no"),
529  'search_crs' => $crs_id
530  );
531 
532  $tableData[] = $rowData;
533  }
534  }
535  $table->setData($tableData);
536  if (count($tableData))
537  {
538  $searchTpl->setVariable("TXT_MARKED_ENTRIES",$lng->txt("marked_entries"));
539  }
540 
541  $searchTpl->setVariable('TABLE', $table->getHtml());
542  $this->tpl->setContent($searchTpl->get());
543 
544  if($_GET["ref"] != "wsp")
545  {
546  $this->tpl->show();
547  }
548  }
549  }
550 
551  function share()
552  {
553  global $lng;
554 
555  if ($_GET["view"] == "mycourses")
556  {
557  $ids = $_REQUEST["search_crs"];
558  if (sizeof($ids))
559  {
560  $this->addPermission($ids);
561  }
562  else
563  {
564  ilUtil::sendInfo($lng->txt("mail_select_course"));
565  $this->showMyCourses();
566  }
567  }
568  else if ($_GET["view"] == "crs_members")
569  {
570  $ids = $_REQUEST["search_members"];
571  if (sizeof($ids))
572  {
573  $this->addPermission($ids);
574  }
575  else
576  {
577  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
578  $this->showMembers();
579  }
580  }
581  else
582  {
583  $this->showMyCourses();
584  }
585  }
586 
587  protected function addPermission($a_obj_ids)
588  {
589  if(!is_array($a_obj_ids))
590  {
591  $a_obj_ids = array($a_obj_ids);
592  }
593 
594  $existing = $this->wsp_access_handler->getPermissions($this->wsp_node_id);
595  $added = false;
596  foreach($a_obj_ids as $object_id)
597  {
598  if(!in_array($object_id, $existing))
599  {
600  $added = $this->wsp_access_handler->addPermission($this->wsp_node_id, $object_id);
601  }
602  }
603 
604  if($added)
605  {
606  ilUtil::sendSuccess($this->lng->txt("wsp_share_success"), true);
607  }
608  $this->ctrl->redirectByClass("ilworkspaceaccessgui", "share");
609  }
610 }
611 
612 ?>