ILIAS  Release_4_2_x_branch Revision 61807
 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('mail_visible',$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 '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') );
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  {
329  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
330 
331  $isOffline = $oTmpCrs->getOfflineStatus();
332  $hasUntrashedReferences = ilObject::_hasUntrashedReference($crs_id);
333  $showMemberListEnabled = (boolean)$oTmpCrs->getShowMembers();
334  $ref_ids = array_keys(ilObject::_getAllReferences($crs_id));
335  $isPrivilegedUser = $rbacsystem->checkAccess('edit', $ref_ids[0]);
336 
337  if($hasUntrashedReferences && ((!$isOffline && $showMemberListEnabled) || $isPrivilegedUser))
338  {
339  $oCrsParticipants = ilCourseParticipants::_getInstanceByObjId($crs_id);
340  $crs_members = $oCrsParticipants->getParticipants();
341 
342  foreach($crs_members as $key => $member)
343  {
344  $tmp_usr = new ilObjUser($member);
345  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
346  {
347  unset($crs_members[$key]);
348  }
349  }
350  unset($tmp_usr);
351 
352  $hiddenMembers = false;
353  if((int)$oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
354  {
355  ++$num_courses_hidden_members;
356  $hiddenMembers = true;
357  }
358  unset($oTmpCrs);
359 
360  $ref_ids = ilObject::_getAllReferences($crs_id);
361  $ref_id = current($ref_ids);
362  $path_arr = $tree->getPathFull($ref_id, $tree->getRootId());
363  $path_counter = 0;
364  $path = '';
365  foreach($path_arr as $data)
366  {
367  if($path_counter++)
368  {
369  $path .= " -> ";
370  }
371  $path .= $data['title'];
372  }
373  $path = $this->lng->txt('path').': '.$path;
374 
375  $current_selection_list = new ilAdvancedSelectionListGUI();
376  $current_selection_list->setListTitle($this->lng->txt("actions"));
377  $current_selection_list->setId("act_".$counter);
378 
379  $this->ctrl->setParameter($this, 'search_crs', $crs_id);
380  $this->ctrl->setParameter($this, 'view', 'mycourses');
381 
382  if($_GET["ref"] == "mail")
383  {
384  if ($this->mailing_allowed)
385  $current_selection_list->addItem($this->lng->txt("mail_members"), '', $this->ctrl->getLinkTarget($this, "mail"));
386  }
387  else if($_GET["ref"] == "wsp")
388  {
389  $current_selection_list->addItem($this->lng->txt("wsp_share_with_members"), '', $this->ctrl->getLinkTarget($this, "share"));
390  }
391  $current_selection_list->addItem($this->lng->txt("mail_list_members"), '', $this->ctrl->getLinkTarget($this, "showMembers"));
392 
393  $this->ctrl->clearParameters($this);
394 
395  $rowData = array
396  (
397  "CRS_ID" => $crs_id,
398  "CRS_NAME" => $ilObjDataCache->lookupTitle($crs_id),
399  "CRS_NO_MEMBERS" => count($crs_members),
400  "CRS_PATH" => $path,
401  'COMMAND_SELECTION_LIST' => $current_selection_list->getHTML(),
402  "hidden_members" => $hiddenMembers,
403  );
404  $counter++;
405  $tableData[] = $rowData;
406  }
407  }
408 
409  //if((int)$counter)
410  //{
411  // $table->addCommandButton('mail',$lng->txt('mail_members'));
412  // $table->addCommandButton('showMembers',$lng->txt('mail_list_members'));
413  //}
414 
415  if($num_courses_hidden_members > 0)
416  {
417  $searchTpl->setCurrentBlock('caption_block');
418  $searchTpl->setVariable('TXT_LIST_MEMBERS_NOT_AVAILABLE', $this->lng->txt('mail_crs_list_members_not_available'));
419  $searchTpl->parseCurrentBlock();
420  }
421  }
422 
423  $searchTpl->setVariable('TXT_MARKED_ENTRIES', $lng->txt('marked_entries'));
424 
425  $table->setData($tableData);
426  if($_GET['ref'] == 'mail') $this->tpl->setVariable('BUTTON_CANCEL', $lng->txt('cancel'));
427 
428  $searchTpl->setVariable('TABLE', $table->getHtml());
429  $tpl->setContent($searchTpl->get());
430 
431  if($_GET["ref"] != "wsp")
432  {
433  $tpl->show();
434  }
435  }
436 
440  public function showMembers()
441  {
442  global $lng, $ilUser, $ilObjDataCache;
443 
444  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
445 
446  if ($_GET["search_crs"] != "")
447  {
448  $_POST["search_crs"] = explode(",", $_GET["search_crs"]);
449  $_GET["search_crs"] = "";
450  }
451  else if ($_SESSION["search_crs"] != "")
452  {
453  $_POST["search_crs"] = explode(",", $_SESSION["search_crs"]);
454  $_SESSION["search_crs"] = "";
455  }
456 
457  if (!is_array($_POST["search_crs"]) ||
458  count($_POST["search_crs"]) == 0)
459  {
460  ilUtil::sendInfo($lng->txt("mail_select_course"));
461  $this->showMyCourses();
462  }
463  else
464  {
465  foreach($_POST['search_crs'] as $crs_id)
466  {
467  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
468  if($oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
469  {
470  unset($_POST['search_crs']);
471  ilUtil::sendInfo($lng->txt('mail_crs_list_members_not_available_for_at_least_one_crs'));
472  return $this->showMyCourses();
473  }
474  unset($oTmpCrs);
475  }
476 
477  $this->tpl->setTitle($this->lng->txt("mail"));
478 
479  $this->ctrl->setParameter($this, "view", "crs_members");
480  if ($_GET["ref"] != "") $this->ctrl->setParameter($this, "ref", $_GET["ref"]);
481  if (is_array($_POST["search_crs"])) $this->ctrl->setParameter($this, "search_crs", implode(",", $_POST["search_crs"]));
482  $this->tpl->setVariable("ACTION", $this->ctrl->getFormAction($this));
483  $this->ctrl->clearParameters($this);
484 
485  $lng->loadLanguageModule('crs');
486  include_once 'Services/Contact/classes/class.ilMailSearchCoursesMembersTableGUI.php';
487  $context = $_GET["ref"] ? $_GET["ref"] : "mail";
488  $table = new ilMailSearchCoursesMembersTableGUI($this, 'crs', $context);
489  $table->setId('show_crs_mmbrs_tbl');
490  $tableData = array();
491  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
492  foreach($_POST["search_crs"] as $crs_id)
493  {
494  $members_obj = ilCourseParticipants::_getinstanceByObjId($crs_id);
495  $tmp_members = $members_obj->getParticipants();
496  $course_members = ilUtil::_sortIds($tmp_members,'usr_data','lastname','usr_id');
497 
498  foreach ($course_members as $member)
499  {
500  $tmp_usr = new ilObjUser($member);
501  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
502  {
503  unset($tmp_usr);
504  continue;
505  }
506  unset($tmp_usr);
507 
508  $name = ilObjUser::_lookupName($member);
509  $login = ilObjUser::_lookupLogin($member);
510 
511  $fullname = "";
512  if(in_array(ilObjUser::_lookupPref($member, 'public_profile'), array("g", 'y')))
513  $fullname = $name['lastname'].', '.$name['firstname'];
514 
515  $rowData = array(
516  'MEMBERS_ID' => $member,
517  'MEMBERS_LOGIN' => $login,
518  'MEMBERS_NAME' => $fullname,
519  'MEMBERS_CRS_GRP' => $ilObjDataCache->lookupTitle($crs_id),
520  'MEMBERS_IN_ADDRESSBOOK' => $this->abook->checkEntryByLogin($login) ? $lng->txt("yes") : $lng->txt("no"),
521  'search_crs' => $crs_id
522  );
523  $tableData[] = $rowData;
524  }
525  }
526  $table->setData($tableData);
527  if (count($tableData))
528  {
529  //$table->addCommandButton('mail', $lng->txt("grp_mem_send_mail"));
530  //$table->addCommandButton('adoptMembers', $lng->txt("mail_into_addressbook"));
531 
532  $searchTpl->setVariable("TXT_MARKED_ENTRIES",$lng->txt("marked_entries"));
533  }
534 
535  $searchTpl->setVariable('TABLE', $table->getHtml());
536  $this->tpl->setContent($searchTpl->get());
537 
538  if($_GET["ref"] != "wsp")
539  {
540  $this->tpl->show();
541  }
542  }
543  }
544 
545  function share()
546  {
547  global $lng;
548 
549  if ($_GET["view"] == "mycourses")
550  {
551  $ids = $_REQUEST["search_crs"];
552  if (sizeof($ids))
553  {
554  $this->addPermission($ids);
555  }
556  else
557  {
558  ilUtil::sendInfo($lng->txt("mail_select_course"));
559  $this->showMyCourses();
560  }
561  }
562  else if ($_GET["view"] == "crs_members")
563  {
564  $ids = $_REQUEST["search_members"];
565  if (sizeof($ids))
566  {
567  $this->addPermission($ids);
568  }
569  else
570  {
571  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
572  $this->showMembers();
573  }
574  }
575  else
576  {
577  $this->showMyCourses();
578  }
579  }
580 
581  protected function addPermission($a_obj_ids)
582  {
583  if(!is_array($a_obj_ids))
584  {
585  $a_obj_ids = array($a_obj_ids);
586  }
587 
588  $existing = $this->wsp_access_handler->getPermissions($this->wsp_node_id);
589  $added = false;
590  foreach($a_obj_ids as $object_id)
591  {
592  if(!in_array($object_id, $existing))
593  {
594  $added = $this->wsp_access_handler->addPermission($this->wsp_node_id, $object_id);
595  }
596  }
597 
598  if($added)
599  {
600  ilUtil::sendSuccess($this->lng->txt("wsp_share_success"), true);
601  }
602  $this->ctrl->redirectByClass("ilworkspaceaccessgui", "share");
603  }
604 }
605 
606 ?>