PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/php/lib/mail_manager/component/send_mail.class.php

https://bitbucket.org/chamilo/chamilo-app-survey-dev/
PHP | 412 lines | 342 code | 67 blank | 3 comment | 37 complexity | a68f0e87a1b8870b62c7d9ab64acbfe1 MD5 | raw file
  1. <?php
  2. namespace application\survey;
  3. use common\libraries\PlatformSetting;
  4. use common\libraries\Translation;
  5. use common\libraries\Request;
  6. use common\libraries\Breadcrumb;
  7. use common\libraries\BreadcrumbTrail;
  8. use repository\RepositoryDataManager;
  9. use common\libraries\EqualityCondition;
  10. use tracking\Tracker;
  11. use tracking\Event;
  12. use common\libraries\Theme;
  13. use user\UserDataManager;
  14. use common\libraries\path;
  15. use common\libraries\Mail;
  16. use common\libraries\DynamicTabsRenderer;
  17. ini_set("memory_limit", "-1");
  18. ini_set("max_execution_time", "0");
  19. class SurveyMailManagerSendMailComponent extends SurveyMailManager
  20. {
  21. private $invitees;
  22. private $reporting_users;
  23. private $not_started;
  24. private $started;
  25. private $finished;
  26. private $mail_send = true;
  27. private $publication_id;
  28. private $survey_id;
  29. private $type;
  30. private $send_date;
  31. function run()
  32. {
  33. $this->publication_id = Request :: get(SurveyMailManager :: PARAM_PUBLICATION_ID);
  34. $this->type = Request :: get(SurveyMailManager :: PARAM_TYPE);
  35. if (! SurveyRights :: is_allowed_in_surveys_subtree(SurveyRights :: RIGHT_INVITE, $this->publication_id, SurveyRights :: TYPE_PUBLICATION, $this->get_user_id()))
  36. {
  37. $this->display_header();
  38. $this->display_error_message(Translation :: get('NotAllowed'));
  39. $this->display_footer();
  40. exit();
  41. }
  42. switch ($this->type)
  43. {
  44. case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
  45. $this->invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_PARTICIPATE, $this->publication_id, SurveyRights :: TYPE_PUBLICATION);
  46. $this->not_started = array();
  47. $this->started = array();
  48. $this->finished = array();
  49. $condition = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $this->publication_id);
  50. $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  51. while ($tracker = $trackers->next_result())
  52. {
  53. if ($tracker->get_status() == SurveyParticipantTracker :: STATUS_FINISHED)
  54. {
  55. $this->finished[] = $tracker->get_user_id();
  56. }
  57. else
  58. if ($tracker->get_status() == SurveyParticipantTracker :: STATUS_STARTED)
  59. {
  60. $this->started[] = $tracker->get_user_id();
  61. }
  62. }
  63. $invitee_count = count(array_unique($this->invitees));
  64. $started_count = count(array_unique($this->started));
  65. $finished_count = count(array_unique($this->finished));
  66. $started_and_finished_users = array_merge($this->started, $this->finished);
  67. $this->not_started = array_diff($this->invitees, $started_and_finished_users);
  68. $not_started_count = $invitee_count - $started_count - $finished_count;
  69. $users = array();
  70. $users[SurveyRights :: PARTICIPATE_RIGHT_NAME] = $invitee_count;
  71. $users[SurveyParticipantTracker :: STATUS_STARTED] = $started_count;
  72. $users[SurveyParticipantTracker :: STATUS_NOTSTARTED] = $not_started_count;
  73. $users[SurveyParticipantTracker :: STATUS_FINISHED] = $finished_count;
  74. break;
  75. case SurveyMailManager :: MAIL_EXPORT_TYPE :
  76. $this->invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_EXPORT_RESULT, $this->publication_id, SurveyRights :: TYPE_PUBLICATION);
  77. $invitee_count = count(array_unique($this->invitees));
  78. $users = array();
  79. $users[SurveyRights :: EXPORT_RESULT_RIGHT_NAME] = $invitee_count;
  80. //
  81. break;
  82. case SurveyMailManager :: MAIL_REPORTING_TYPE :
  83. $this->invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_REPORTING, $this->publication_id, SurveyRights :: TYPE_PUBLICATION);
  84. $invitee_count = count(array_unique($this->invitees));
  85. $users = array();
  86. $users[SurveyRights :: REPORTING_RIGHT_NAME] = $invitee_count;
  87. break;
  88. }
  89. $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
  90. $this->survey_id = $survey_publication->get_content_object_id();
  91. $survey = RepositoryDataManager :: get_instance()->retrieve_content_object($this->survey_id);
  92. $form = new SurveyPublicationMailerForm($this, $this->get_user(), $users, $this->type, $this->get_url(array(
  93. self :: PARAM_PUBLICATION_ID => $this->publication_id, SurveyMailManager :: PARAM_TYPE => $this->type)));
  94. if ($form->validate())
  95. {
  96. $values = $form->exportValues();
  97. $user_ids = $form->get_seleted_group_user_ids();
  98. $this->parse_values($values, $user_ids);
  99. }
  100. else
  101. {
  102. $this->display_header();
  103. echo $this->get_survey_html($survey);
  104. echo $form->toHtml();
  105. $this->display_footer();
  106. }
  107. }
  108. function get_survey_html($survey)
  109. {
  110. $html = array();
  111. $html[] = '<div class="content_object" style="background-image: url(' . Theme :: get_image_path('survey') . 'survey_22.png);">';
  112. switch ($this->type)
  113. {
  114. case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
  115. $html[] = '<div class="title">' . Translation :: get('MailToParticipantsForSurvey') . ' ' . ' </div>';
  116. break;
  117. case SurveyMailManager :: MAIL_EXPORT_TYPE :
  118. $html[] = '<div class="title">' . Translation :: get('MailToExportersForSurvey') . ' ' . ' </div>';
  119. break;
  120. case SurveyMailManager :: MAIL_REPORTING_TYPE :
  121. $html[] = '<div class="title">' . Translation :: get('MailToReportersForSurvey') . ' ' . ' </div>';
  122. break;
  123. }
  124. $html[] = $survey->get_title() . '<br/>';
  125. $html[] = '</div>';
  126. return implode("\n", $html);
  127. }
  128. function parse_values($values, $user_ids)
  129. {
  130. $users = array();
  131. $mail_user_ids = array();
  132. $dm = UserDataManager :: get_instance();
  133. switch ($this->type)
  134. {
  135. case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
  136. if (isset($user_ids))
  137. {
  138. $this->invitees = array_intersect($this->invitees, $user_ids);
  139. }
  140. $not_started = $values[SurveyParticipantTracker :: STATUS_NOTSTARTED];
  141. if ($not_started == 1)
  142. {
  143. $mail_user_ids = array_merge($mail_user_ids, $this->not_started);
  144. }
  145. $started = $values[SurveyParticipantTracker :: STATUS_STARTED];
  146. if ($started == 1)
  147. {
  148. $mail_user_ids = array_merge($mail_user_ids, $this->started);
  149. }
  150. $finished = $values[SurveyParticipantTracker :: STATUS_FINISHED];
  151. if ($finished == 1)
  152. {
  153. $mail_user_ids = array_merge($mail_user_ids, $this->finished);
  154. }
  155. $invitees = $values[SurveyRights :: PARTICIPATE_RIGHT_NAME];
  156. if ($invitees == 1)
  157. {
  158. $mail_user_ids = array_merge($mail_user_ids, $this->invitees);
  159. }
  160. break;
  161. case SurveyMailManager :: MAIL_EXPORT_TYPE :
  162. if (isset($user_ids))
  163. {
  164. $this->invitees = array_intersect($this->invitees, $user_ids);
  165. }
  166. $invitees = $values[SurveyRights :: EXPORT_RESULT_RIGHT_NAME];
  167. if ($invitees == 1)
  168. {
  169. $mail_user_ids = array_merge($mail_user_ids, $this->invitees);
  170. }
  171. break;
  172. case SurveyMailManager :: MAIL_REPORTING_TYPE :
  173. if (isset($user_ids))
  174. {
  175. $this->invitees = array_intersect($this->invitees, $user_ids);
  176. }
  177. $invitees = $values[SurveyRights :: REPORTING_RIGHT_NAME];
  178. if ($invitees == 1)
  179. {
  180. $mail_user_ids = array_merge($mail_user_ids, $this->invitees);
  181. }
  182. break;
  183. }
  184. $mail_user_ids = array_unique($mail_user_ids);
  185. if (count($mail_user_ids) == 0)
  186. {
  187. $this->redirect(Translation :: get('NoSurveyMailsSend'), false, array(
  188. self :: PARAM_ACTION => self :: ACTION_BROWSE));
  189. }
  190. else
  191. {
  192. $email_content = $values[SurveyPublicationMailerForm :: EMAIL_CONTENT];
  193. $fullbody = array();
  194. $fullbody[] = $email_content;
  195. $fullbody[] = '<br/><br/>';
  196. $fullbody[] = '<p id="link">';
  197. $parameters = array();
  198. switch ($this->type)
  199. {
  200. case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
  201. $parameters[SurveyManager :: PARAM_ACTION] = SurveyManager :: ACTION_TAKE;
  202. // $parameters[SurveyManager :: PARAM_SURVEY_ID] = $this->survey_id;
  203. $parameters[SurveyManager :: PARAM_PUBLICATION_ID] = $this->publication_id;
  204. $url = Path :: get(WEB_PATH) . $this->get_link($parameters);
  205. $fullbody[] = '<a href=' . $url . '>' . Translation :: get('ClickToTakeSurvey') . '</a>';
  206. $selected_tab = SurveyMailManagerBrowserComponent :: TAB_MAILS_TO_PARTICIPANTS;
  207. break;
  208. case SurveyMailManager :: MAIL_EXPORT_TYPE :
  209. $parameters[SurveyManager :: PARAM_ACTION] = SurveyManager :: ACTION_EXPORT;
  210. $parameters[SurveyManager :: PARAM_PUBLICATION_ID] = $this->publication_id;
  211. $url = Path :: get(WEB_PATH) . $this->get_link($parameters);
  212. $fullbody[] = '<a href=' . $url . '>' . Translation :: get('ClickToExportResults') . '</a>';
  213. $selected_tab = SurveyMailManagerBrowserComponent :: TAB_MAILS_TO_EXPORTERS;
  214. break;
  215. case SurveyMailManager :: MAIL_REPORTING_TYPE :
  216. $parameters[SurveyManager :: PARAM_ACTION] = SurveyManager :: ACTION_REPORTING;
  217. $parameters[SurveyManager :: PARAM_PUBLICATION_ID] = $this->publication_id;
  218. $url = Path :: get(WEB_PATH) . $this->get_link($parameters);
  219. $fullbody[] = '<a href=' . $url . '>' . Translation :: get('ClickToViewReports') . '</a>';
  220. $selected_tab = SurveyMailManagerBrowserComponent :: TAB_MAILS_TO_REPORTERS;
  221. break;
  222. }
  223. $fullbody[] = '<br/><br/>' . Translation :: get('OrCopyAndPasteThisText') . ':';
  224. $fullbody[] = '<br/><a href=' . $url . '>' . $url . '</a>';
  225. $fullbody[] = '</p>';
  226. $body = implode("\n", $fullbody);
  227. $email_header = $values[SurveyPublicationMailerForm :: EMAIL_HEADER];
  228. $email_from_address = $values[SurveyPublicationMailerForm :: FROM_ADDRESS];
  229. $email_reply_address = $values[SurveyPublicationMailerForm :: REPLY_ADDRESS];
  230. $email_from_address_name = $values[SurveyPublicationMailerForm :: FROM_ADDRESS_NAME];
  231. $email_reply_address_name = $values[SurveyPublicationMailerForm :: REPLY_ADDRESS_NAME];
  232. $email = new SurveyPublicationMail();
  233. $email->set_mail_header($email_header);
  234. $email->set_mail_content($body);
  235. $email->set_sender_user_id($this->get_user_id());
  236. $email->set_from_address($email_from_address);
  237. $email->set_from_address_name($email_from_address_name);
  238. $email->set_reply_address($email_reply_address);
  239. $email->set_reply_address_name($email_reply_address_name);
  240. $email->set_publication_id($this->publication_id);
  241. $email->set_send_date(time());
  242. $email->set_type($this->type);
  243. $succes = $email->create();
  244. if ($succes)
  245. {
  246. foreach ($mail_user_ids as $user_id)
  247. {
  248. $user = $dm->retrieve_user($user_id);
  249. $to_email = $user->get_email();
  250. $this->send_mail($user_id, $to_email, $email);
  251. }
  252. $cron_enabled = PlatformSetting :: get('enable_mail_cron_job', 'application\survey');
  253. if ($this->mail_send == false)
  254. {
  255. if (! $cron_enabled)
  256. {
  257. $message = Translation :: get('NotAllMailsSend');
  258. }
  259. else
  260. {
  261. $message = Translation :: get('NotAllMailJobsCreated');
  262. }
  263. }
  264. else
  265. {
  266. if (! $cron_enabled)
  267. {
  268. $message = Translation :: get('AllMailsSend');
  269. }
  270. else
  271. {
  272. $message = Translation :: get('AllMailJobsCreated');
  273. }
  274. }
  275. $this->redirect($message, ! $this->mail_send, array(self :: PARAM_ACTION => self :: ACTION_BROWSE,
  276. DynamicTabsRenderer :: PARAM_SELECTED_TAB => $selected_tab));
  277. }
  278. else
  279. {
  280. $this->redirect(Translation :: get('NoMailsSend'), true, array(
  281. self :: PARAM_ACTION => self :: ACTION_BROWSE,
  282. DynamicTabsRenderer :: PARAM_SELECTED_TAB => $selected_tab));
  283. }
  284. }
  285. }
  286. function send_mail($user_id, $to_email, $email)
  287. {
  288. $arg = array();
  289. $args[SurveyPublicationMailTracker :: PROPERTY_USER_ID] = $user_id;
  290. $args[SurveyPublicationMailTracker :: PROPERTY_SURVEY_PUBLICATION_MAIL_ID] = $email->get_id();
  291. $args[SurveyPublicationMailTracker :: PROPERTY_SURVEY_PUBLICATION_ID] = $this->publication_id;
  292. $cron_enabled = PlatformSetting :: get('enable_mail_cron_job', 'application\survey');
  293. if (! $cron_enabled)
  294. {
  295. $from = array();
  296. $from[Mail :: NAME] = $email->get_from_address_name();
  297. $from[Mail :: EMAIL] = $email->get_from_address();
  298. $mail = Mail :: factory($email->get_mail_header(), $email->get_mail_content(), $to_email, $from);
  299. $reply = array();
  300. $reply[Mail :: NAME] = $email->get_reply_address_name();
  301. $reply[Mail :: EMAIL] = $email->get_reply_address();
  302. $mail->set_reply($reply);
  303. // Check whether it was sent successfully
  304. if ($mail->send() === FALSE)
  305. {
  306. $this->mail_send = false;
  307. $args[SurveyPublicationMailTracker :: PROPERTY_STATUS] = SurveyPublicationMailTracker :: STATUS_MAIL_NOT_SEND;
  308. }
  309. else
  310. {
  311. $args[SurveyPublicationMailTracker :: PROPERTY_STATUS] = SurveyPublicationMailTracker :: STATUS_MAIL_SEND;
  312. }
  313. $tracker = Event :: trigger(SurveyPublicationMailTracker :: REGISTER_PUBLICATION_MAIL_EVENT, SurveyManager :: APPLICATION_NAME, $args);
  314. }
  315. else
  316. {
  317. $args[SurveyPublicationMailTracker :: PROPERTY_STATUS] = SurveyPublicationMailTracker :: STATUS_MAIL_IN_QUEUE;
  318. $tracker = Event :: trigger(SurveyPublicationMailTracker :: REGISTER_PUBLICATION_MAIL_EVENT, SurveyManager :: APPLICATION_NAME, $args);
  319. $publication_mail_tracker_id = $tracker[0]->get_id();
  320. $mail_job = new MailJob();
  321. $mail_job->set_status(MailJob :: STATUS_NEW);
  322. $mail_job->set_UUID(0);
  323. $mail_job->set_publication_mail_tracker_id($publication_mail_tracker_id);
  324. if (! $mail_job->create())
  325. {
  326. $this->mail_send = false;
  327. }
  328. }
  329. }
  330. function add_additional_breadcrumbs(BreadcrumbTrail $breadcrumbtrail)
  331. {
  332. $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(
  333. SurveyManager :: PARAM_ACTION => SurveyManager :: ACTION_BROWSE)), Translation :: get('SurveyManagerBrowserComponent')));
  334. $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(
  335. SurveyManager :: PARAM_ACTION => SurveyManager :: ACTION_BROWSE_PARTICIPANTS,
  336. SurveyManager :: PARAM_PUBLICATION_ID => Request :: get(SurveyManager :: PARAM_PUBLICATION_ID))), Translation :: get('SurveyManagerParticipantBrowserComponent')));
  337. $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(self :: PARAM_ACTION => self :: ACTION_BROWSE,
  338. self :: PARAM_PUBLICATION_ID => Request :: get(self :: PARAM_PUBLICATION_ID))), Translation :: get('SurveyMailManagerBrowserComponent')));
  339. }
  340. function get_additional_parameters()
  341. {
  342. return array(SurveyManager :: PARAM_PUBLICATION_ID);
  343. }
  344. }
  345. ?>