Ver código fonte

Use DOMDocument to add print button for certificates

Angel Fernando Quiroz Campos 8 anos atrás
pai
commit
46c7afa543
1 arquivos alterados com 27 adições e 22 exclusões
  1. 27 22
      main/gradebook/lib/GradebookUtils.php

+ 27 - 22
main/gradebook/lib/GradebookUtils.php

@@ -692,41 +692,46 @@ class GradebookUtils
     {
         // Generate document HTML
         $content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $sessionId, $is_preview);
-        $new_content_html = null;
-        $variables = null;
+        $new_content_html = isset($content_html['content']) ? $content_html['content'] : null;
+        $variables = isset($content_html['variables']) ? $content_html['variables'] : null;
         $contentHead = null;
 
-        if (isset($content_html['content'])) {
-            $new_content = explode('</head>', $content_html['content']);
-            $new_content_html = $new_content[1];
-            $contentHead = $new_content[0];
-        }
-
-        if (isset($content_html['variables'])) {
-            $variables = $content_html['variables'];
-        }
-
         $path_image = api_get_path(WEB_COURSE_PATH) . api_get_course_path($course_code) . '/document/images/gallery';
         $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
 
         $path_image_in_default_course = api_get_path(WEB_CODE_PATH) . 'default_course_document';
         $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
         $new_content_html = str_replace(SYS_CODE_PATH . 'img/', api_get_path(WEB_IMG_PATH), $new_content_html);
-        $print = '';
+
+        $dom = new DOMDocument();
+        $dom->loadHTML($new_content_html);
 
         //add print header
-        if ($hide_print_button == false) {
-            $print .= '<style media="print" type="text/css">#print_div {visibility:hidden;}</style>';
-            $print .= '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div">';
-            $print .= Display::return_icon('printmgr.gif', get_lang('Print'));
-            $print .= '</a>';
-        }
+        if (!$hide_print_button) {
+            $head = $dom->getElementsByTagName('head');
+            $body = $dom->getElementsByTagName('body');
 
-        // Add header
-        $new_content_html =  $contentHead. $print . '</head>' . $new_content_html;
+            $printStyle = $dom->createElement('style');
+            $printStyle->setAttribute('media', 'print');
+            $printStyle->setAttribute('type', 'text/css');
+            $printStyle->textContent = '#print_div {visibility:hidden;}';
+
+            $head->item(0)->appendChild($printStyle);
+
+            $printIcon = $dom->createDocumentFragment();
+            $printIcon->appendXML(Display::return_icon('printmgr.gif', get_lang('Print')));
+
+            $printA = $dom->createElement('button');
+            $printA->setAttribute('onclick', 'window.print();');
+            $printA->setAttribute('id', 'print_div');
+            $printA->setAttribute('style', 'float:right; padding:4px; border: 0 none;');
+            $printA->appendChild($printIcon);
+
+            $body->item(0)->insertBefore($printA, $body->item(0)->firstChild);
+        }
 
         return array(
-            'content' => $new_content_html,
+            'content' => $dom->saveHTML(),
             'variables' => $variables
         );
     }