/** * */ protected function _addFilters() { $pFilters = $this->_content->getElementsByTagName('p'); if (empty($pFilters->length)) { return false; } $styleCell = array('size' => 10, 'name' => 'Calibri'); $filter = $this->_mainSection->createTextRun(); foreach ($pFilters->item(0)->childNodes as $child) { if ($child instanceof DOMElement) { $filter->addText($child->nodeValue, $styleCell + array('bold' => true)); } else { $nodeValue = trim($child->nodeValue); if (!empty($nodeValue)) { $filter->addText($nodeValue, $styleCell); } } $filter->addText(' ', $styleCell); } }
/** * 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'); } } } } } } } }