Browse Source

Fix PDF export see BT#13394

jmontoyaa 7 years ago
parent
commit
9b58903a87

+ 3 - 1
main/exercise/exercise.class.php

@@ -78,6 +78,7 @@ class Exercise
     public $questionTypeWithFeedback;
     public $showPreviousButton;
     public $notifications;
+    public $export = false;
 
     /**
      * Constructor of the class
@@ -4416,7 +4417,8 @@ class Exercise
                                 0,
                                 0,
                                 $results_disabled,
-                                $showTotalScoreAndUserChoicesInLastAttempt
+                                $showTotalScoreAndUserChoicesInLastAttempt,
+                                $this->export
                             );
                         } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
                             ExerciseShowFunctions::display_multiple_answer_true_false(

+ 5 - 2
main/exercise/exercise_show.php

@@ -369,7 +369,7 @@ foreach ($questionList as $questionId) {
 }
 
 $counter = 1;
-$exercise_content = null;
+$exercise_content = '';
 $category_list = array();
 $useAdvancedEditor = true;
 
@@ -377,6 +377,8 @@ if (!empty($maxEditors) && count($questionList) > $maxEditors) {
     $useAdvancedEditor = false;
 }
 
+$objExercise->export = $action === 'export';
+
 $countPendingQuestions = 0;
 foreach ($questionList as $questionId) {
     $choice = $exerciseResult[$questionId];
@@ -922,7 +924,8 @@ foreach ($questionList as $questionId) {
     $question_content = '<div class="question_row">';
 
     if ($show_results) {
-        //Shows question title an description
+        $objQuestionTmp->export = $action == 'export';
+        // Shows question title an description
         $question_content .= $objQuestionTmp->return_header(
             $objExercise,
             $counter,

+ 1 - 0
main/exercise/question.class.php

@@ -36,6 +36,7 @@ abstract class Question
     public $question_table_class = 'table table-striped';
     public $questionTypeWithFeedback;
     public $extra;
+    public $export = false;
     public static $questionTypes = array(
         UNIQUE_ANSWER => array('unique_answer.class.php', 'UniqueAnswer'),
         MULTIPLE_ANSWER => array('multiple_answer.class.php', 'MultipleAnswer'),

+ 15 - 2
main/inc/lib/exercise_show_functions.lib.php

@@ -290,7 +290,7 @@ class ExerciseShowFunctions
      * @param boolean $ans Whether to show the answer comment or not
      * @param bool $resultsDisabled
      * @param bool $showTotalScoreAndUserChoices
-     *
+     * @param bool $export
      * @return void
      */
     public static function display_unique_or_multiple_answer(
@@ -304,8 +304,21 @@ class ExerciseShowFunctions
         $questionId,
         $ans,
         $resultsDisabled,
-        $showTotalScoreAndUserChoices
+        $showTotalScoreAndUserChoices,
+        $export = false
     ) {
+        if ($export) {
+            $answer = strip_tags_blacklist($answer, ['title', 'head']);
+            // Fix answers that contains this tags
+            $tags = [
+                '<html>',
+                '</html>',
+                '<body>',
+                '</body>'
+            ];
+            $answer = str_replace($tags, '', $answer);
+        }
+
         $hide_expected_answer = false;
         if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
             $hide_expected_answer = true;

+ 2 - 2
main/inc/lib/pdf.lib.php

@@ -462,11 +462,11 @@ class PDF
 
             //Fixing only images @todo do the same thing with other elements
             $elements = $doc->getElementsByTagName('img');
+            $protocol = api_get_protocol();
             if (!empty($elements)) {
                 foreach ($elements as $item) {
                     $old_src = $item->getAttribute('src');
-                    //$old_src= str_replace('../','',$old_src);
-                    if (strpos($old_src, 'http') === false) {
+                    if (strpos($old_src, $protocol) === false) {
                         if (strpos($old_src, '/main/default_course_document') === false) {
                             if (strpos($old_src, '/main/inc/lib/') === false) {
                                 $old_src_fixed = str_replace(api_get_path(REL_COURSE_PATH).$course_data['path'].'/document/', '', $old_src);

+ 16 - 0
main/inc/lib/text.lib.php

@@ -869,3 +869,19 @@ function trim_value(& $value)
 {
     $value = trim($value);
 }
+
+/**
+ *
+ * Strips only the given tags in the given HTML string.
+ * @param string $html
+ * @param array $tags
+ * @return string
+ */
+function strip_tags_blacklist($html, $tags)
+{
+    foreach ($tags as $tag) {
+        $regex = '#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi';
+        $html = preg_replace($regex, '', $html);
+    }
+    return $html;
+}