Browse Source

Add attachments without template - refs BT#9461

Daniel Barreto 10 years ago
parent
commit
001b1a078a

+ 10 - 4
main/inc/lib/pdf.lib.php

@@ -351,7 +351,8 @@ class PDF
         $document_html,
         $css = '',
         $pdf_name = '',
-        $course_code = null
+        $course_code = null,
+        $outputMode = 'D'
     ) {
         global $_configuration;
 
@@ -433,10 +434,15 @@ class PDF
             $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
         } else {
             $pdf_name = replace_dangerous_char($pdf_name);
-            $output_file = $pdf_name.'.pdf';
+            // Save temporally into Archive folder
+            $output_file = api_get_path(SYS_ARCHIVE_PATH) . $pdf_name.'.pdf';
+        }
+        $this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file
+        if ($outputMode == 'F') {
+            // Do NOT exit when export to file
+        } else {
+            exit;
         }
-        $this->pdf->Output($output_file, 'D'); // F to save the pdf in a file
-        exit;
     }
 
     /**

+ 69 - 7
plugin/advanced_subscription/src/AdvancedSubscriptionPlugin.php

@@ -302,15 +302,31 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
      * @param string $content
      * @param int $sessionId
      * @param bool $save
+     * @param array $fileAttachments
      * @return bool|int
      */
-    public function sendMailMessage($studentId, $receiverId, $subject, $content, $sessionId, $save = false)
+    public function sendMailMessage($studentId, $receiverId, $subject, $content, $sessionId, $save = false, $fileAttachments = array())
     {
-        $mailId = MessageManager::send_message(
-            $receiverId,
-            $subject,
-            $content
-        );
+        if (
+            !empty($fileAttachments) &&
+            is_array($fileAttachments) &&
+            isset($fileAttachments['files']) &&
+            isset($fileAttachments['comments'])
+        ) {
+            $mailId = MessageManager::send_message(
+                $receiverId,
+                $subject,
+                $content,
+                $fileAttachments['files'],
+                $fileAttachments['comments']
+            );
+        } else {
+            $mailId = MessageManager::send_message(
+                $receiverId,
+                $subject,
+                $content
+            );
+        }
 
         if ($save && !empty($mailId)) {
             // Save as sent message
@@ -523,6 +539,34 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
                 );
                 break;
             case ADVANCED_SUBSCRIPTION_ACTION_ADMIN_APPROVE:
+                $fileAttachments = array();
+                if (api_get_plugin_setting('courselegal', 'tool_enable')) {
+                    $courseLegal = CourseLegalPlugin::create();
+                    $courses = SessionManager::get_course_list_by_session_id($data['sessionId']);
+                    $course = current($courses);
+                    $data['courseId'] = $course['id'];
+                    $data['course'] = api_get_course_info_by_id($data['courseId']);
+                    $termsAndConditions = $courseLegal->getData($data['courseId'], $data['sessionId']);
+                    $termsAndConditions = $termsAndConditions['content'];
+                    $termsAndConditions = $this->renderTemplateString($termsAndConditions, $data);
+                    $tpl = new Template(get_lang('TermsAndConditions'));
+                    $tpl->assign('session', $data['session']);
+                    $tpl->assign('student', $data['student']);
+                    $tpl->assign('sessionId', $data['sessionId']);
+                    $tpl->assign('termsContent', $termsAndConditions);
+                    $termsAndConditions = $tpl->fetch('/advanced_subscription/views/terms_and_conditions_to_pdf.tpl');
+                    $pdf = new PDF();
+                    $filename = 'terms' . sha1(rand(0,99999));
+                    $pdf->content_to_pdf($termsAndConditions, null, $filename, null, 'F');
+                    $fileAttachments['file'][] = array(
+                        'name' => $filename . '.pdf',
+                        'application/pdf' => $filename . '.pdf',
+                        'tmp_name' => api_get_path(SYS_ARCHIVE_PATH) . $filename . '.pdf',
+                        'error' => UPLOAD_ERR_OK,
+                        'size' => filesize(api_get_path(SYS_ARCHIVE_PATH) . $filename . '.pdf'),
+                    );
+                    $fileAttachments['comments'][] = get_lang('TermsAndConditions');
+                }
                 // Mail to student
                 $mailIds[] = $this->sendMailMessage(
                     $data['studentUserId'],
@@ -530,7 +574,8 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
                     $this->get_lang('MailAdminAccept'),
                     $template->fetch('/advanced_subscription/views/admin_accepted_notice_student.tpl'),
                     $data['sessionId'],
-                    true
+                    true,
+                    $fileAttachments
                 );
                 // Mail to superior
                 $mailIds[] = $this->sendMailMessage(
@@ -1115,4 +1160,21 @@ class AdvancedSubscriptionPlugin extends Plugin implements HookPluginInterface
 
         return false;
     }
+
+    /**
+     * Return string replacing tags "{{}}"with variables assigned in $data
+     * @param string $templateContent
+     * @param array $data
+     * @return string
+     */
+    public function renderTemplateString($templateContent, $data = array())
+    {
+        $twigString = new \Twig_Environment(new \Twig_Loader_String());
+        $templateContent = $twigString->render(
+            $templateContent,
+            $data
+        );
+
+        return $templateContent;
+    }
 }

+ 2 - 8
plugin/advanced_subscription/src/terms_and_conditions.php

@@ -52,17 +52,11 @@ if (
 
     $data['session'] = api_get_session_info($data['sessionId']);
     $data['student'] = Usermanager::get_user_info_by_id($data['studentUserId']);
+    $data['course'] = api_get_course_info_by_id($data['courseId']);
     $data['acceptTermsUrl'] = $plugin->getQueueUrl($data);
     $data['rejectTermsUrl'] = $plugin->getTermsUrl($data, ADVANCED_SUBSCRIPTION_TERMS_MODE_REJECT);
     // Use Twig with String loader
-    $twigString = new \Twig_Environment(new \Twig_Loader_String());
-    $termsContent = $twigString->render(
-        $termsAndConditions,
-        array(
-            'session' => $data['session'],
-            'student' => $data['student'],
-        )
-    );
+    $termsContent = $plugin->renderTemplateString($termsAndConditions, $data);
 
 } else {
     $termsContent = '';

+ 47 - 0
plugin/advanced_subscription/test/terms_to_pdf.php

@@ -0,0 +1,47 @@
+<?php
+/* For license terms, see /license.txt */
+/**
+ * A script to render all mails templates
+ * @package chamilo.plugin.advanced_subscription
+ */
+
+require_once __DIR__ . '/../config.php';
+
+// Protect test
+api_protect_admin_script();
+
+$data['action'] = 'confirm';
+$data['currentUserId'] = 1;
+$data['queueId'] = 0;
+$data['is_connected'] = true;
+$data['profile_completed'] = 90.0;
+$data['sessionId'] = intval($_REQUEST['s']);
+$data['studentUserId'] = intval($_REQUEST['u']);
+$data['student'] = UserManager::get_user_info_by_id($data['studentUserId']);
+$data['session'] = api_get_session_info($data['sessionId']);
+
+if (!empty($data['sessionId']) && !empty($data['studentUserId'])) {
+    $plugin = AdvancedSubscriptionPlugin::create();
+
+    if (api_get_plugin_setting('courselegal', 'tool_enable')) {
+        $courseLegal = CourseLegalPlugin::create();
+        $courses = SessionManager::get_course_list_by_session_id($data['sessionId']);
+        $course = current($courses);
+        $data['courseId'] = $course['id'];
+        $data['course'] = api_get_course_info_by_id($data['courseId']);
+        $termsAndConditions = $courseLegal->getData($data['courseId'], $data['sessionId']);
+        $termsAndConditions = $termsAndConditions['content'];
+        $termsAndConditions = $plugin->renderTemplateString($termsAndConditions, $data);
+        $tpl = new Template($plugin->get_lang('Terms'));
+        $tpl->assign('session', $data['session']);
+        $tpl->assign('student', $data['student']);
+        $tpl->assign('sessionId', $data['sessionId']);
+        $tpl->assign('termsContent', $termsAndConditions);
+        $termsAndConditions = $tpl->fetch('/advanced_subscription/views/terms_and_conditions_to_pdf.tpl');
+        $pdf = new PDF();
+        $filename = 'terms' . sha1(rand(0,99999));
+        $pdf->content_to_pdf($termsAndConditions, null, $filename, null, 'F');
+        $fileDir = api_get_path(WEB_ARCHIVE_PATH) . $filename . '.pdf';
+        echo '<pre>', print_r($fileDir, 1), '</pre>';
+    }
+}

+ 23 - 0
plugin/advanced_subscription/views/terms_and_conditions_to_pdf.tpl

@@ -0,0 +1,23 @@
+{# start copy from head.tpl #}
+<meta charset="{{ system_charset }}" />
+<link href="http://www.chamilo.org/documentation.php" rel="help" />
+<link href="http://www.chamilo.org/team.php" rel="author" />
+<link href="http://www.chamilo.org" rel="copyright" />
+{{ prefetch }}
+{{ favico }}
+{{ browser_specific_head }}
+<link rel="apple-touch-icon" href="{{ _p.web }}apple-touch-icon.png" />
+<link href="{{ _p.web_plugin }}advanced_subscription/views/css/style.css" rel="stylesheet" type="text/css">
+<meta name="apple-mobile-web-app-capable" content="yes" />
+<meta name="Generator" content="{{ _s.software_name }} {{ _s.system_version|slice(0,1) }}" />
+{#  Use the latest engine in ie8/ie9 or use google chrome engine if available  #}
+{#  Improve usability in portal devices #}
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
+<title>{{ title_string }}</title>
+{{ css_file_to_string }}
+{{ css_style_print }}
+{{ js_file_to_string }}
+{# end copy from head.tpl #}
+<div class="legal-terms-popup">
+    {{ termsContent }}
+</div>