ILIAS  eassessment Revision 61809
 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  array_push($members, $rcpt);
145 
146  unset($rcpt);
147  }
148  else
149  {
150  array_push($members, $rbacreview->getRoleMailboxAddress($role['obj_id']));
151  }
152  }
153  }
154  }
155  }
156 
157  if(count($members))
158  $mail_data = $this->umail->appendSearchResult($members, 'to');
159  else
160  $mail_data = $this->umail->getSavedData();
161 
162  $this->umail->savePostData(
163  $mail_data["user_id"],
164  $mail_data["attachments"],
165  $mail_data["rcp_to"],
166  $mail_data["rcp_cc"],
167  $mail_data["rcp_bcc"],
168  $mail_data["m_type"],
169  $mail_data["m_email"],
170  $mail_data["m_subject"],
171  $mail_data["m_message"],
172  $mail_data["use_placeholders"]
173  );
174 
175  #$this->ctrl->returnToParent($this);
176  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
177  }
178 
179  function mailMembers()
180  {
181  $members = array();
182 
183  if (!is_array($this->umail->getSavedData()))
184  {
185  $this->umail->savePostData(
186  $ilUser->getId(),
187  array(),
188  "",
189  "",
190  "",
191  "",
192  "",
193  "",
194  "",
195  ""
196  );
197  }
198 
199  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
200 
201  foreach ($ids as $member)
202  {
203  $login = ilObjUser::_lookupLogin($member);
204  array_push($members, $login);
205  }
206  $mail_data = $this->umail->appendSearchResult($members,"to");
207 
208  $this->umail->savePostData(
209  $mail_data["user_id"],
210  $mail_data["attachments"],
211  $mail_data["rcp_to"],
212  $mail_data["rcp_cc"],
213  $mail_data["rcp_bcc"],
214  $mail_data["m_type"],
215  $mail_data["m_email"],
216  $mail_data["m_subject"],
217  $mail_data["m_message"],
218  $mail_data["use_placeholders"]
219  );
220 
221  #$this->ctrl->returnToParent($this);
222  ilUtil::redirect("ilias.php?baseClass=ilMailGUI&type=search_res");
223  }
224 
228  public function adoptMembers()
229  {
230  global $lng;
231  $ids = ((int)$_GET['search_members']) ? array((int)$_GET['search_members']) : $_POST['search_members'];
232 
233  if ((int)$ids && !is_array($ids))
234  $ids = array((int)$ids);
235 
236  if ($ids )
237  {
238  $members = array();
239 
240  foreach ($ids as $member)
241  {
242  $login = ilObjUser::_lookupLogin($member);
243 
244  if (!$this->abook->checkEntry($login))
245  {
246  $name = ilObjUser::_lookupName($member);
247  $email = '';
248  if(ilObjUser::_lookupPref((int)$member, 'public_email') == 'y')
249  {
250  $email = ilObjUser::_lookupEmail($member);
251  }
252  $this->abook->addEntry(
253  $login,
254  $name["firstname"],
255  $name["lastname"],
256  $email
257  );
258  }
259  }
260  ilUtil::sendInfo($lng->txt("mail_members_added_addressbook"));
261  }
262  else
263  {
264  ilUtil::sendInfo($lng->txt("mail_select_one_entry"));
265  }
266 
267  $this->showMembers();
268  }
269 
273  function cancel()
274  {
275  if ($_GET["view"] == "mycourses" &&
276  $_GET["ref"] == "mail")
277  {
278  $this->ctrl->returnToParent($this);
279  }
280  else
281  {
282  $this->showMyCourses();
283  }
284  }
285 
289  public function showMyCourses()
290  {
291  global $lng, $ilUser, $ilObjDataCache, $tree, $tpl;
292 
293  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
294 
295  $this->tpl->setVariable('HEADER', $this->lng->txt('mail') );
296 
297  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
298 
299  $_GET['view'] = 'mycourses';
300 
301  $lng->loadLanguageModule('crs');
302 
303  include_once 'Services/Contact/classes/class.ilMailSearchCoursesTableGUI.php';
304  $table = new ilMailSearchCoursesTableGUI($this);
305  $table->setId('search_crs_tbl');
306  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
307  $crs_ids = ilCourseParticipants::_getMembershipByType($ilUser->getId(), 'crs');
308  $counter = 0;
309  $tableData = array();
310  if (is_array($crs_ids) && count($crs_ids) > 0)
311  {
312  $num_courses_hidden_members = 0;
313  include_once("./Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php");
314  foreach($crs_ids as $crs_id)
315  {
317  {
318  $oCrsParticipants = ilCourseParticipants::_getInstanceByObjId($crs_id);
319  $crs_members = $oCrsParticipants->getParticipants();
320 
321  foreach($crs_members as $key => $member)
322  {
323  $tmp_usr = new ilObjUser($member);
324  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
325  {
326  unset($crs_members[$key]);
327  }
328  }
329  unset($tmp_usr);
330 
331  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
332  $hiddenMembers = false;
333  if((int)$oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
334  {
335  ++$num_courses_hidden_members;
336  $hiddenMembers = true;
337  }
338  unset($oTmpCrs);
339 
340  $ref_ids = ilObject::_getAllReferences($crs_id);
341  $ref_id = current($ref_ids);
342  $path_arr = $tree->getPathFull($ref_id, $tree->getRootId());
343  $path_counter = 0;
344  $path = '';
345  foreach($path_arr as $data)
346  {
347  if($path_counter++)
348  {
349  $path .= " -> ";
350  }
351  $path .= $data['title'];
352  }
353  $path = $this->lng->txt('path').': '.$path;
354 
355  $current_selection_list = new ilAdvancedSelectionListGUI();
356  $current_selection_list->setListTitle($this->lng->txt("actions"));
357  $current_selection_list->setId("act_".$counter);
358 
359  $this->ctrl->setParameter($this, 'search_crs', $crs_id);
360  $this->ctrl->setParameter($this, 'view', 'mycourses');
361 
362  if ($this->mailing_allowed)
363  $current_selection_list->addItem($this->lng->txt("mail_members"), '', $this->ctrl->getLinkTarget($this, "mail"));
364  $current_selection_list->addItem($this->lng->txt("mail_list_members"), '', $this->ctrl->getLinkTarget($this, "showMembers"));
365 
366  $this->ctrl->clearParameters($this);
367 
368  $rowData = array
369  (
370  "CRS_ID" => $crs_id,
371  "CRS_NAME" => $ilObjDataCache->lookupTitle($crs_id),
372  "CRS_NO_MEMBERS" => count($crs_members),
373  "CRS_PATH" => $path,
374  'COMMAND_SELECTION_LIST' => $current_selection_list->getHTML(),
375  "hidden_members" => $hiddenMembers,
376  );
377  $counter++;
378  $tableData[] = $rowData;
379  }
380  }
381 
382  //if((int)$counter)
383  //{
384  // $table->addCommandButton('mail',$lng->txt('mail_members'));
385  // $table->addCommandButton('showMembers',$lng->txt('mail_list_members'));
386  //}
387 
388  if($num_courses_hidden_members > 0)
389  {
390  $searchTpl->setCurrentBlock('caption_block');
391  $searchTpl->setVariable('TXT_LIST_MEMBERS_NOT_AVAILABLE', $this->lng->txt('mail_crs_list_members_not_available'));
392  $searchTpl->parseCurrentBlock();
393  }
394  }
395 
396  $searchTpl->setVariable('TXT_MARKED_ENTRIES', $lng->txt('marked_entries'));
397 
398  $table->setData($tableData);
399  if($_GET['ref'] == 'mail') $this->tpl->setVariable('BUTTON_CANCEL', $lng->txt('cancel'));
400 
401  $searchTpl->setVariable('TABLE', $table->getHtml());
402  $tpl->setContent($searchTpl->get());
403  $tpl->show();
404  }
405 
409  public function showMembers()
410  {
411  global $lng, $ilUser, $ilObjDataCache;
412 
413  include_once 'Modules/Course/classes/class.ilCourseParticipants.php';
414 
415  if ($_GET["search_crs"] != "")
416  {
417  $_POST["search_crs"] = explode(",", $_GET["search_crs"]);
418  $_GET["search_crs"] = "";
419  }
420  else if ($_SESSION["search_crs"] != "")
421  {
422  $_POST["search_crs"] = explode(",", $_SESSION["search_crs"]);
423  $_SESSION["search_crs"] = "";
424  }
425 
426  if (!is_array($_POST["search_crs"]) ||
427  count($_POST["search_crs"]) == 0)
428  {
429  ilUtil::sendInfo($lng->txt("mail_select_course"));
430  $this->showMyCourses();
431  }
432  else
433  {
434  foreach($_POST['search_crs'] as $crs_id)
435  {
436  $oTmpCrs = ilObjectFactory::getInstanceByObjId($crs_id);
437  if($oTmpCrs->getShowMembers() == $oTmpCrs->SHOW_MEMBERS_DISABLED)
438  {
439  unset($_POST['search_crs']);
440  ilUtil::sendInfo($lng->txt('mail_crs_list_members_not_available_for_at_least_one_crs'));
441  return $this->showMyCourses();
442  }
443  unset($oTmpCrs);
444  }
445 
446  $this->tpl->setVariable("HEADER", $this->lng->txt("mail"));
447 
448  $this->ctrl->setParameter($this, "view", "crs_members");
449  if ($_GET["ref"] != "") $this->ctrl->setParameter($this, "ref", $_GET["ref"]);
450  if (is_array($_POST["search_crs"])) $this->ctrl->setParameter($this, "search_crs", implode(",", $_POST["search_crs"]));
451  $this->tpl->setVariable("ACTION", $this->ctrl->getFormAction($this));
452  $this->ctrl->clearParameters($this);
453 
454  $lng->loadLanguageModule('crs');
455  include_once 'Services/Contact/classes/class.ilMailSearchCoursesMembersTableGUI.php';
456  $table = new ilMailSearchCoursesMembersTableGUI($this, 'crs');
457  $table->setId('show_crs_mmbrs_tbl');
458  $tableData = array();
459  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
460  foreach($_POST["search_crs"] as $crs_id)
461  {
462  $members_obj = ilCourseParticipants::_getinstanceByObjId($crs_id);
463  $tmp_members = $members_obj->getParticipants();
464  $course_members = ilUtil::_sortIds($tmp_members,'usr_data','lastname','usr_id');
465 
466  foreach ($course_members as $member)
467  {
468  $tmp_usr = new ilObjUser($member);
469  if($tmp_usr->checkTimeLimit()== false || $tmp_usr->getActive() == false )
470  {
471  unset($tmp_usr);
472  continue;
473  }
474  unset($tmp_usr);
475 
476  $name = ilObjUser::_lookupName($member);
477  $login = ilObjUser::_lookupLogin($member);
478 
479  $fullname = "";
480  if(in_array(ilObjUser::_lookupPref($member, 'public_profile'), array("g", 'y')))
481  $fullname = $name['lastname'].', '.$name['firstname'];
482 
483  $rowData = array(
484  'MEMBERS_ID' => $member,
485  'MEMBERS_LOGIN' => $login,
486  'MEMBERS_NAME' => $fullname,
487  'MEMBERS_CRS_GRP' => $ilObjDataCache->lookupTitle($crs_id),
488  'MEMBERS_IN_ADDRESSBOOK' => $this->abook->checkEntryByLogin($login) ? $lng->txt("yes") : $lng->txt("no"),
489  'search_crs' => $crs_id
490  );
491  $tableData[] = $rowData;
492  }
493  }
494  $table->setData($tableData);
495  if (count($tableData))
496  {
497  //$table->addCommandButton('mail', $lng->txt("grp_mem_send_mail"));
498  //$table->addCommandButton('adoptMembers', $lng->txt("mail_into_addressbook"));
499 
500  $searchTpl->setVariable("TXT_MARKED_ENTRIES",$lng->txt("marked_entries"));
501  }
502 
503  $searchTpl->setVariable('TABLE', $table->getHtml());
504  $this->tpl->setContent($searchTpl->get());
505  $this->tpl->show();
506  }
507  }
508 
509 }
510 
511 ?>