ILIAS  Release_4_1_x_branch Revision 61804
 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()
28  {
29  global $tpl, $ilCtrl, $lng, $ilUser, $rbacsystem;
30 
31  $this->tpl = $tpl;
32  $this->ctrl = $ilCtrl;
33  $this->lng = $lng;
34 
35  // check if current user may send mails
36  include_once "Services/Mail/classes/class.ilMail.php";
37  $mail = new ilMail($_SESSION["AccountId"]);
38  $this->mailing_allowed = $rbacsystem->checkAccess('mail_visible',$mail->getMailObjectReferenceId());
39 
40  $this->ctrl->saveParameter($this, "mobj_id");
41 
42  $this->umail = new ilFormatMail($ilUser->getId());
43  $this->abook = new ilAddressbook($ilUser->getId());
44  }
45 
46  public function executeCommand()
47  {
48  $forward_class = $this->ctrl->getNextClass($this);
49  switch($forward_class)
50  {
51  default:
52  if (!($cmd = $this->ctrl->getCmd()))
53  {
54  $cmd = "showMyCourses";
55  }
56 
57  $this->$cmd();
58  break;
59  }
60  return true;
61  }
62 
63  function mail()
64  {
65  global $ilUser, $lng;
66  if ($_GET["view"] == "mycourses")
67  {
68  $ids = ((int)$_GET['search_crs']) ? array((int)$_GET['search_crs']) : $_POST['search_crs'];
69 
70  if ($ids)
71  {
72  $this->mailCourses();
73  }
74  else
75  {
76  ilUtil::sendInfo($lng->txt("mail_select_course"));
77  $this->showMyCourses();
78  }
79  }
80  else if ($_GET["view"] == "crs_members")
81  {
82  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
83  if ($ids)
84  {
85  $this->mailMembers();
86  }
87  else
88  {
89  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
90  $this->showMembers();
91  }
92  }
93  else
94  {
95  $this->showMyCourses();
96  }
97  }
98 
99  function mailCourses()
100  {
101  global $ilUser, $lng, $rbacreview;
102 
103  $members = array();
104 
105  if (!is_array($old_mail_data = $this->umail->getSavedData()))
106  {
107  $this->umail->savePostData(
108  $ilUser->getId(),
109  array(),
110  "",
111  "",
112  "",
113  "",
114  "",
115  "",
116  "",
117  ""
118  );
119  }
120 
121  require_once 'classes/class.ilObject.php';
122 
123  $ids = ((int)$_GET['search_crs']) ? array((int)$_GET['search_crs']) : $_POST['search_crs'];
124 
125  foreach ($ids as $crs_id)
126  {
127  $ref_ids = ilObject::_getAllReferences($crs_id);
128 
129  foreach ($ref_ids as $ref_id)
130  {
131  $roles = $rbacreview->getAssignableChildRoles($ref_id);
132  foreach ($roles as $role)
133  {
134  if (substr($role['title'], 0, 14) == 'il_crs_member_' ||
135  substr($role['title'], 0, 13) == 'il_crs_tutor_' ||
136  substr($role['title'], 0, 13) == 'il_crs_admin_')
137  {
138  if(isset($old_mail_data['rcp_to']) &&
139  trim($old_mail_data['rcp_to']) != '')
140  {
141  $rcpt = $rbacreview->getRoleMailboxAddress($role['obj_id']);
142 
143  if(!$this->umail->doesRecipientStillExists($rcpt, $old_mail_data['rcp_to']))
144  // does not work if Pear is enabled and Mailbox Address contain special chars!!
145 // array_push($members, $rcpt);
146  // FIX for Mantis: 7523, 8061
147  array_push($members, '#'.$role['title']);
148 
149  unset($rcpt);
150  }
151  else
152  {
153  // does not work if Pear is enabled and Mailbox Address contain special chars!!
154 // array_push($members, $rbacreview->getRoleMailboxAddress($role['obj_id']));
155  // FIX for Mantis: 7523, 8061
156  array_push($members, '#'.$role['title']);
157  }
158  }
159  }
160  }
161  }
162 
163  if(count($members))
164  $mail_data = $this->umail->appendSearchResult($members, 'to');
165  else
166  $mail_data = $this->umail->getSavedData();
167 
168  $this->umail->savePostData(
169  $mail_data["user_id"],
170  $mail_data["attachments"],
171  $mail_data["rcp_to"],
172  $mail_data["rcp_cc"],
173  $mail_data["rcp_bcc"],
174  $mail_data["m_type"],
175  $mail_data["m_email"],
176  $mail_data["m_subject"],
177  $mail_data["m_message"],
178  $mail_data["use_placeholders"]
179  );
180 
181  #$this->ctrl->returnToParent($this);
182  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
183  }
184 
185  function mailMembers()
186  {
187  global $ilUser;
188 
189  $members = array();
190 
191  if (!is_array($this->umail->getSavedData()))
192  {
193  $this->umail->savePostData(
194  $ilUser->getId(),
195  array(),
196  "",
197  "",
198  "",
199  "",
200  "",
201  "",
202  "",
203  ""
204  );
205  }
206 
207  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
208 
209  foreach ($ids as $member)
210  {
211  $login = ilObjUser::_lookupLogin($member);
212  array_push($members, $login);
213  }
214  $mail_data = $this->umail->appendSearchResult($members,"to");
215 
216  $this->umail->savePostData(
217  $mail_data["user_id"],
218  $mail_data["attachments"],
219  $mail_data["rcp_to"],
220  $mail_data["rcp_cc"],
221  $mail_data["rcp_bcc"],
222  $mail_data["m_type"],
223  $mail_data["m_email"],
224  $mail_data["m_subject"],
225  $mail_data["m_message"],
226  $mail_data["use_placeholders"]
227  );
228 
229  #$this->ctrl->returnToParent($this);
230  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
231  }
232 
236  public function adoptMembers()
237  {
238  global $lng;
239  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
240 
241  if ((int)$ids && !is_array($ids))
242  $ids = array((int)$ids);
243 
244  if ($ids )
245  {
246  $members = array();
247 
248  foreach ($ids as $member)
249  {
250  $login = ilObjUser::_lookupLogin($member);
251 
252  if (!$this->abook->checkEntry($login))
253  {
254  $name = ilObjUser::_lookupName($member);
255  $email = '';
256  if(ilObjUser::_lookupPref((int)$member, 'public_email') == 'y')
257  {
258  $email = ilObjUser::_lookupEmail($member);
259  }
260  $this->abook->addEntry(
261  $login,
262  $name["firstname"],
263  $name["lastname"],
264  $email
265  );
266  }
267  }
268  ilUtil::sendInfo($lng->txt("mail_members_added_addressbook"));
269  }
270  else
271  {
272  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
273  }
274 
275  $this->showMembers();
276  }
277 
281  function cancel()
282  {
283  if ($_GET["view"] == "mycourses" &&
284  $_GET["ref"] == "mail")
285  {
286  $this->ctrl->returnToParent($this);
287  }
288  else
289  {
290  $this->showMyCourses();
291  }
292  }
293 
297  public function showMyCourses()
298  {
299  global $lng, $ilUser, $ilObjDataCache, $tree, $tpl, $rbacsystem;
300 
301  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
302 
303  $this->tpl->setVariable('HEADER', $this->lng->txt('mail') );
304 
305  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
306 
307  $_GET['view'] = 'mycourses';
308 
309  $lng->loadLanguageModule('crs');
310 
311  include_once 'Services/Contact/classes/class.ilMailSearchCoursesTableGUI.php';
312  $table = new ilMailSearchCoursesTableGUI($this);
313  $table->setId('search_crs_tbl');
314  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
315  $crs_ids = ilCourseParticipants::_getMembershipByType($ilUser->getId(), 'crs');
316  $counter = 0;
317  $tableData = array();
318  if (is_array($crs_ids) && count($crs_ids) > 0)
319  {
320  $num_courses_hidden_members = 0;
321  include_once("./Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php");
322  foreach($crs_ids as $crs_id)
323  {
324  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
325 
326  $isOffline = $oTmpCrs->getOfflineStatus();
327  $hasUntrashedReferences = ilObject::_hasUntrashedReference($crs_id);
328  $showMemberListEnabled = (boolean)$oTmpCrs->getShowMembers();
329  $ref_ids = array_keys(ilObject::_getAllReferences($crs_id));
330  $isPrivilegedUser = $rbacsystem->checkAccess('edit', $ref_ids[0]);
331 
332  if($hasUntrashedReferences && ((!$isOffline && $showMemberListEnabled) || $isPrivilegedUser))
333  {
334  $oCrsParticipants = ilCourseParticipants::_getInstanceByObjId($crs_id);
335  $crs_members = $oCrsParticipants->getParticipants();
336 
337  foreach($crs_members as $key => $member)
338  {
339  $tmp_usr = new ilObjUser($member);
340  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
341  {
342  unset($crs_members[$key]);
343  }
344  }
345  unset($tmp_usr);
346 
347  $hiddenMembers = false;
348  if((int)$oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
349  {
350  ++$num_courses_hidden_members;
351  $hiddenMembers = true;
352  }
353  unset($oTmpCrs);
354 
355  $ref_ids = ilObject::_getAllReferences($crs_id);
356  $ref_id = current($ref_ids);
357  $path_arr = $tree->getPathFull($ref_id, $tree->getRootId());
358  $path_counter = 0;
359  $path = '';
360  foreach($path_arr as $data)
361  {
362  if($path_counter++)
363  {
364  $path .= " -> ";
365  }
366  $path .= $data['title'];
367  }
368  $path = $this->lng->txt('path').': '.$path;
369 
370  $current_selection_list = new ilAdvancedSelectionListGUI();
371  $current_selection_list->setListTitle($this->lng->txt("actions"));
372  $current_selection_list->setId("act_".$counter);
373 
374  $this->ctrl->setParameter($this, 'search_crs', $crs_id);
375  $this->ctrl->setParameter($this, 'view', 'mycourses');
376 
377  if ($this->mailing_allowed)
378  $current_selection_list->addItem($this->lng->txt("mail_members"), '', $this->ctrl->getLinkTarget($this, "mail"));
379  $current_selection_list->addItem($this->lng->txt("mail_list_members"), '', $this->ctrl->getLinkTarget($this, "showMembers"));
380 
381  $this->ctrl->clearParameters($this);
382 
383  $rowData = array
384  (
385  "CRS_ID" => $crs_id,
386  "CRS_NAME" => $ilObjDataCache->lookupTitle($crs_id),
387  "CRS_NO_MEMBERS" => count($crs_members),
388  "CRS_PATH" => $path,
389  'COMMAND_SELECTION_LIST' => $current_selection_list->getHTML(),
390  "hidden_members" => $hiddenMembers,
391  );
392  $counter++;
393  $tableData[] = $rowData;
394  }
395  }
396 
397  //if((int)$counter)
398  //{
399  // $table->addCommandButton('mail',$lng->txt('mail_members'));
400  // $table->addCommandButton('showMembers',$lng->txt('mail_list_members'));
401  //}
402 
403  if($num_courses_hidden_members > 0)
404  {
405  $searchTpl->setCurrentBlock('caption_block');
406  $searchTpl->setVariable('TXT_LIST_MEMBERS_NOT_AVAILABLE', $this->lng->txt('mail_crs_list_members_not_available'));
407  $searchTpl->parseCurrentBlock();
408  }
409  }
410 
411  $searchTpl->setVariable('TXT_MARKED_ENTRIES', $lng->txt('marked_entries'));
412 
413  $table->setData($tableData);
414  if($_GET['ref'] == 'mail') $this->tpl->setVariable('BUTTON_CANCEL', $lng->txt('cancel'));
415 
416  $searchTpl->setVariable('TABLE', $table->getHtml());
417  $tpl->setContent($searchTpl->get());
418  $tpl->show();
419  }
420 
424  public function showMembers()
425  {
426  global $lng, $ilUser, $ilObjDataCache;
427 
428  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
429 
430  if ($_GET["search_crs"] != "")
431  {
432  $_POST["search_crs"] = explode(",", $_GET["search_crs"]);
433  $_GET["search_crs"] = "";
434  }
435  else if ($_SESSION["search_crs"] != "")
436  {
437  $_POST["search_crs"] = explode(",", $_SESSION["search_crs"]);
438  $_SESSION["search_crs"] = "";
439  }
440 
441  if (!is_array($_POST["search_crs"]) ||
442  count($_POST["search_crs"]) == 0)
443  {
444  ilUtil::sendInfo($lng->txt("mail_select_course"));
445  $this->showMyCourses();
446  }
447  else
448  {
449  foreach($_POST['search_crs'] as $crs_id)
450  {
451  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
452  if($oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
453  {
454  unset($_POST['search_crs']);
455  ilUtil::sendInfo($lng->txt('mail_crs_list_members_not_available_for_at_least_one_crs'));
456  return $this->showMyCourses();
457  }
458  unset($oTmpCrs);
459  }
460 
461  $this->tpl->setVariable("HEADER", $this->lng->txt("mail"));
462 
463  $this->ctrl->setParameter($this, "view", "crs_members");
464  if ($_GET["ref"] != "") $this->ctrl->setParameter($this, "ref", $_GET["ref"]);
465  if (is_array($_POST["search_crs"])) $this->ctrl->setParameter($this, "search_crs", implode(",", $_POST["search_crs"]));
466  $this->tpl->setVariable("ACTION", $this->ctrl->getFormAction($this));
467  $this->ctrl->clearParameters($this);
468 
469  $lng->loadLanguageModule('crs');
470  include_once 'Services/Contact/classes/class.ilMailSearchCoursesMembersTableGUI.php';
471  $table = new ilMailSearchCoursesMembersTableGUI($this, 'crs');
472  $table->setId('show_crs_mmbrs_tbl');
473  $tableData = array();
474  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
475  foreach($_POST["search_crs"] as $crs_id)
476  {
477  $members_obj = ilCourseParticipants::_getinstanceByObjId($crs_id);
478  $tmp_members = $members_obj->getParticipants();
479  $course_members = ilUtil::_sortIds($tmp_members,'usr_data','lastname','usr_id');
480 
481  foreach ($course_members as $member)
482  {
483  $tmp_usr = new ilObjUser($member);
484  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
485  {
486  unset($tmp_usr);
487  continue;
488  }
489  unset($tmp_usr);
490 
491  $name = ilObjUser::_lookupName($member);
492  $login = ilObjUser::_lookupLogin($member);
493 
494  $fullname = "";
495  if(in_array(ilObjUser::_lookupPref($member, 'public_profile'), array("g", 'y')))
496  $fullname = $name['lastname'].', '.$name['firstname'];
497 
498  $rowData = array(
499  'MEMBERS_ID' => $member,
500  'MEMBERS_LOGIN' => $login,
501  'MEMBERS_NAME' => $fullname,
502  'MEMBERS_CRS_GRP' => $ilObjDataCache->lookupTitle($crs_id),
503  'MEMBERS_IN_ADDRESSBOOK' => $this->abook->checkEntryByLogin($login) ? $lng->txt("yes") : $lng->txt("no"),
504  'search_crs' => $crs_id
505  );
506  $tableData[] = $rowData;
507  }
508  }
509  $table->setData($tableData);
510  if (count($tableData))
511  {
512  //$table->addCommandButton('mail', $lng->txt("grp_mem_send_mail"));
513  //$table->addCommandButton('adoptMembers', $lng->txt("mail_into_addressbook"));
514 
515  $searchTpl->setVariable("TXT_MARKED_ENTRIES",$lng->txt("marked_entries"));
516  }
517 
518  $searchTpl->setVariable('TABLE', $table->getHtml());
519  $this->tpl->setContent($searchTpl->get());
520  $this->tpl->show();
521  }
522  }
523 
524 }
525 
526 ?>