示例#1
0
文件: Docx.php 项目: fredcido/simuweb
 /**
  *
  * @return boolean 
  */
 protected function _addGraphs()
 {
     $xpath = new DOMXPath($this->_dom);
     $graphs = $xpath->query('//div[@class="graphs"]', $this->_content);
     if (empty($graphs->length)) {
         return false;
     }
     $images = $graphs->item(0)->getElementsByTagName('img');
     foreach ($images as $img) {
         $src = $img->getAttribute('src');
         $src = preg_replace('/^.+image\\/id\\//i', '', $src);
         $src = preg_replace('/\\/image.png$/i', '', $src);
         $contents = App_Cache::load($src);
         $randomName = App_General_String::randomHash();
         $fileName = $this->_tempDir . DIRECTORY_SEPARATOR . $randomName . '.png';
         file_put_contents($fileName, $contents);
         $size = getimagesize($fileName);
         $this->_mainSection->addImage($fileName, array('align' => 'center', 'width' => 675, 'height' => $size[1]));
     }
     return true;
 }
/**
 * Function to print all blocks (parts) of a question or answer text in one textrun.
 * Blocks can be of type string, newline or image.
 * If the first block is a string it is printed as a listitem using the numbering and depth provided.
 * Otherwise, the empty string is used as the list item string.
 *
 * @param PHPWord_Section $section A PHP Word section
 * @param array $blocks The array of blocks as created by the conversion functions.
 * @param PHPWord_Numbering_AbstractNumbering $numbering The numbering used for the list item
 * @param int $depth The depth in the enumeration (0 for questions, 1 for answers).
 */
function offlinequiz_print_blocks_docx(PHPWord_Section $section, $blocks, $numbering = null, $depth = 0)
{
    // We skip leading newlines.
    while ($blocks[0]['type'] == 'newline') {
        array_shift($blocks);
    }
    // First print the list item string.
    if (!empty($numbering)) {
        $itemstring = ' ';
        $style = 'nStyle';
        if ($blocks[0]['type'] == 'string') {
            $itemstring = $blocks[0]['value'];
            if (array_key_exists('style', $blocks[0])) {
                $style = $blocks[0]['style'];
            }
            array_shift($blocks);
        }
        $section->addListItem($itemstring, $depth, $numbering, $style);
        // We also skip the first sequential newline because we got a newline with addListItem.
        if (!empty($blocks) && $blocks[0]['type'] == 'newline') {
            array_shift($blocks);
        }
    }
    // Now we go through the rest of the blocks (if there are any) and print them to a textrun.
    if (!empty($blocks)) {
        if (empty($numbering)) {
            $textrun = $section->createTextRun();
        } else {
            $textrun = $section->createTextRun('questionTab');
            $textrun->addText("\t", 'nStyle');
        }
        $counter = count($blocks);
        foreach ($blocks as $block) {
            $counter--;
            if ($block['type'] == 'string') {
                // Skip empty string at the end of the text block.
                if ($counter == 0 && strlen(trim($block['value']) == '')) {
                    continue;
                }
                if (array_key_exists('style', $block) && !empty($block['style'])) {
                    $textrun->addText($block['value'], $block['style']);
                } else {
                    $textrun->addText($block['value'], 'nStyle');
                }
            } else {
                if ($block['type'] == 'newline') {
                    if (empty($numbering)) {
                        $textrun = $section->createTextRun();
                    } else {
                        $textrun = $section->createTextRun('questionTab');
                        $textrun->addText("\t", 'nStyle');
                    }
                } else {
                    if ($block['type'] == 'image') {
                        // Retrieve the style info.
                        $style = array();
                        $style['width'] = 200;
                        $style['height'] = 100;
                        $style['align'] = 'center';
                        if ($block['width']) {
                            $style['width'] = intval($block['width']);
                        }
                        if ($block['height']) {
                            $style['height'] = intval($block['height']);
                        }
                        if ($block['align']) {
                            $style['align'] = $block['align'];
                            if ($style['align'] == 'middle') {
                                $style['align'] = 'center';
                            }
                        }
                        // Now add the image and start a new textrun.
                        $section->addImage($block['value'], $style);
                        if ($counter > 0) {
                            if (empty($numbering)) {
                                $textrun = $section->createTextRun();
                            } else {
                                $textrun = $section->createTextRun('questionTab');
                                $textrun->addText("\t", 'nStyle');
                            }
                        }
                    }
                }
            }
        }
    }
}