Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_report_Rendering | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
306 | |
0.00% |
0 / 1 |
| render | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
72 | |||
| renderReport | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
72 | |||
| renderToCommandline | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * This helper aims at providing utility methods to render |
| 24 | * reports into HTML. |
| 25 | * |
| 26 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
| 27 | * |
| 28 | */ |
| 29 | class tao_helpers_report_Rendering |
| 30 | { |
| 31 | /** |
| 32 | * Render a common_report_Report object into an HTML string output. |
| 33 | * |
| 34 | * @param common_report_Report $report A report to be rendered |
| 35 | * @return string The HTML rendering. |
| 36 | */ |
| 37 | public static function render(common_report_Report $report) |
| 38 | { |
| 39 | |
| 40 | $stack = new SplStack(); |
| 41 | $renderingStack = new SplStack(); |
| 42 | $traversed = []; |
| 43 | |
| 44 | $stack->push($report); |
| 45 | $nesting = 0; |
| 46 | |
| 47 | while ($stack->count() > 0) { |
| 48 | $current = $stack->pop(); |
| 49 | |
| 50 | if (in_array($current, $traversed, true) === false && $current->hasChildren() === true) { |
| 51 | $nesting++; |
| 52 | // -- Hierarchical report, 1st pass (descending). |
| 53 | |
| 54 | // Repush report for a 2ndpass. |
| 55 | $stack->push($current); |
| 56 | |
| 57 | // Tag as already traversed. |
| 58 | $traversed[] = $current; |
| 59 | |
| 60 | // Push the children for a 1st pass. |
| 61 | foreach ($current as $child) { |
| 62 | $stack->push($child); |
| 63 | } |
| 64 | } elseif (in_array($current, $traversed, true) === true && $current->hasChildren() === true) { |
| 65 | $nesting--; |
| 66 | // -- Hierachical report, 2nd pass (ascending). |
| 67 | |
| 68 | // Get the nested renderings of the current report. |
| 69 | $children = []; |
| 70 | foreach ($current as $child) { |
| 71 | $children[] = $renderingStack->pop(); |
| 72 | } |
| 73 | |
| 74 | $renderingStack->push(self::renderReport($current, $children, $nesting)); |
| 75 | } else { |
| 76 | // -- Leaf report, 1st & single pass. |
| 77 | $renderingStack->push(self::renderReport($current, [], $nesting, true)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return $renderingStack->pop(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Contains the logic to render a report and its children. |
| 86 | * |
| 87 | * @param common_report_Report $report A report to be rendered. |
| 88 | * @param array $childRenderedReports An array of strings containing the separate rendering of $report's child |
| 89 | * reports. |
| 90 | * @param integer $nesting The current nesting level (root = 0). |
| 91 | * @return string The HTML output of $report. |
| 92 | */ |
| 93 | private static function renderReport( |
| 94 | common_report_Report $report, |
| 95 | array $childRenderedReports = [], |
| 96 | $nesting = 0, |
| 97 | $leaf = false |
| 98 | ) { |
| 99 | |
| 100 | switch ($report->getType()) { |
| 101 | case common_report_Report::TYPE_SUCCESS: |
| 102 | $typeClass = 'success'; |
| 103 | break; |
| 104 | |
| 105 | case common_report_Report::TYPE_WARNING: |
| 106 | $typeClass = 'warning'; |
| 107 | break; |
| 108 | |
| 109 | case common_report_Report::TYPE_ERROR: |
| 110 | $typeClass = 'error'; |
| 111 | break; |
| 112 | |
| 113 | default: |
| 114 | $typeClass = 'info'; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | $openingTag = '<div class="feedback-' . $typeClass . ' feedback-nesting-' . $nesting . ' ' |
| 119 | . (($leaf === true) ? 'leaf' : 'hierarchical') . ' tao-scope">'; |
| 120 | $leafIcon = ($leaf === true) ? ' leaf-icon' : ' hierarchical-icon'; |
| 121 | $icon = '<span class="icon-' . $typeClass . $leafIcon . '"></span>'; |
| 122 | $message = nl2br(_dh($report->__toString())); |
| 123 | $endingTag = '</div>'; |
| 124 | $okButton = '<p><button id="import-continue" class="btn-info"><span class="icon-right"></span>' |
| 125 | . __("Continue") . '</button></p>'; |
| 126 | |
| 127 | // Put all the children renderings together. |
| 128 | $content = implode('', $childRenderedReports); |
| 129 | return $openingTag . $icon . $message . $content . (($nesting != 0) ? '' : $okButton) . $endingTag; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Contains the logic to render a report and its children to the command line |
| 134 | * |
| 135 | * @deprecated |
| 136 | * @param common_report_Report $report A report to be rendered. |
| 137 | * @param integer $intend the intend of the message. |
| 138 | * @return string The command line output of $report. |
| 139 | */ |
| 140 | public static function renderToCommandline(common_report_Report $report, $intend = 0) |
| 141 | { |
| 142 | return helpers_Report::renderToCommandLine($report, helpers_Report::AUTOSENSE, $intend); |
| 143 | } |
| 144 | } |