public function test_ouwiki_structure()
 {
     $this->resetAfterTest(true);
     $user = $this->get_new_user();
     $course = $this->get_new_course();
     // Setup a wiki to use.
     $ouwiki = $this->get_new_ouwiki($course->id, OUWIKI_SUBWIKIS_SINGLE);
     $cm = get_coursemodule_from_instance('ouwiki', $ouwiki->id);
     $this->assertNotEmpty($cm);
     $context = context_module::instance($cm->id);
     $groupid = 0;
     $this->setUser($user);
     $subwiki = ouwiki_get_subwiki($course, $ouwiki, $cm, $context, $groupid, $user->id, true);
     // Create the start page.
     $startpagename = 'testpage1';
     $formdata = null;
     $startpageversion = ouwiki_get_current_page($subwiki, $startpagename, OUWIKI_GETPAGE_CREATE);
     $verid = ouwiki_save_new_version($course, $cm, $ouwiki, $subwiki, $startpagename, $startpagename, -1, -1, -1, null, $formdata);
     // Create a page with no sub pages.
     $pagename2 = 'testpage2';
     $content2 = 'testcontent2';
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $startpagename, $pagename2, $content2, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename2);
     $this->assertEquals($pageversion->title, $pagename2);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add another page to start page.
     $pagename3 = 'testpage3';
     $content3 = 'testcontent3';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $startpagename, $pagename3, $content3, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename3);
     $this->assertEquals($pageversion->title, $pagename3);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add another page to start page.
     $pagename9 = 'testpage9';
     $content9 = 'testcontent9';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $startpagename, $pagename9, $content9, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename9);
     $this->assertEquals($pageversion->title, $pagename9);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add pages to testpage3.
     // Add page to test page 3.
     $pagename4 = 'testpage4';
     $content4 = 'testcontent4';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename3, $pagename4, $content4, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename4);
     $this->assertEquals($pageversion->title, $pagename4);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add another page to testpage 3.
     $pagename8 = 'testpage8';
     $content8 = 'testcontent8';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename3, $pagename8, $content8, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename8);
     $this->assertEquals($pageversion->title, $pagename8);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add pages to testpage4.
     // Add page to test page 4.
     $pagename5 = 'testpage5';
     $content5 = 'testcontent5';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename4, $pagename5, $content5, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename5);
     $this->assertEquals($pageversion->title, $pagename5);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add another page to testpage 4.
     $pagename7 = 'testpage7';
     $content7 = 'testcontent7';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename4, $pagename7, $content7, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename7);
     $this->assertEquals($pageversion->title, $pagename7);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add page to test page 5.
     $pagename6 = 'testpage6';
     $content6 = 'testcontent6';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename5, $pagename6, $content6, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename6);
     $this->assertEquals($pageversion->title, $pagename6);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add page to test page 9.
     $pagename10 = 'testpage10';
     $content10 = 'testcontent10';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename9, $pagename10, $content10, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename10);
     $this->assertEquals($pageversion->title, $pagename10);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add another page to testpage 9.
     $pagename12 = 'testpage12';
     $content12 = 'testcontent12';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename9, $pagename12, $content12, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename12);
     $this->assertEquals($pageversion->title, $pagename12);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Add page to test page 10.
     $pagename11 = 'testpage11';
     $content11 = 'testcontent11';
     // We don't get anything returned for this.
     ouwiki_create_new_page($course, $cm, $ouwiki, $subwiki, $pagename10, $pagename11, $content11, $formdata);
     // Try get that page.
     $pageversion = ouwiki_get_current_page($subwiki, $pagename11);
     $this->assertEquals($pageversion->title, $pagename11);
     // Test fullname info from ouwiki_get_current_page.
     $this->assertEquals(fullname($user), fullname($pageversion));
     // Create the index.
     $index = ouwiki_get_subwiki_index($subwiki->id);
     // Check to see that there are 12 posts.
     $this->assertEquals(count($index), 12);
     reset($index);
     $orphans = false;
     // Check for orphan posts - there should be none.
     foreach ($index as $indexitem) {
         if (count($indexitem->linksfrom) == 0 && $indexitem->title !== 'testpage1') {
             $orphans = true;
             break;
         }
     }
     $this->assertEquals($orphans, false);
     // Test tree structure functions.
     // Build tree.
     ouwiki_build_tree($index);
     // Check to see whether pages have the correct number of children in them including the root node.
     $page = $this->get_page_from_index_by_pagename('testpage3', $index);
     $subtree = ouwiki_get_sub_tree_from_index($page->pageid, $index);
     $this->assertEquals(6, count($subtree));
     // Check to see whether pages have the correct number of children in them including the root node.
     $page = $this->get_page_from_index_by_pagename('testpage4', $index);
     $subtree = ouwiki_get_sub_tree_from_index($page->pageid, $index);
     $this->assertEquals(4, count($subtree));
     // Check linkto, linksfrom, and children arrays for testpage 4
     // - see structure diagram in function description for links to, from, and children for P4.
     $linksfrom = $page->linksfrom;
     $linksto = $page->linksto;
     $children = $page->children;
     $this->assertEquals(count($linksfrom), 1);
     $this->assertEquals(count($linksto), 2);
     $this->assertEquals(count($children), 2);
     // Test linksfrom from testpage 4.
     $p = $this->get_page_from_index_by_pageid($linksfrom[0], $index);
     $this->assertEquals($p->title, $pagename3);
     // Test linksto for testpage 4.
     $p = $this->get_page_from_index_by_pageid($linksto[0], $index);
     $this->assertEquals($p->title, $pagename5);
     $p = $this->get_page_from_index_by_pageid($linksto[1], $index);
     $this->assertEquals($p->title, $pagename7);
     // Test children for testpage 4.
     $p = $this->get_page_from_index_by_pageid($children[0], $index);
     $this->assertEquals($p->title, $pagename5);
     $p = $this->get_page_from_index_by_pageid($children[1], $index);
     $this->assertEquals($p->title, $pagename7);
     // Check to see whether pages have the correct number of children in them including the root node.
     $page = $this->get_page_from_index_by_pagename('testpage5', $index);
     $this->assertEquals(2, count(ouwiki_get_sub_tree_from_index($page->pageid, $index)));
     // Check to see whether pages have the correct number of children in them including the root node.
     $page = $this->get_page_from_index_by_pagename('testpage9', $index);
     $this->assertEquals(4, count(ouwiki_get_sub_tree_from_index($page->pageid, $index)));
     // Check to see whether pages have the correct number of children in them including the root node.
     $page = $this->get_page_from_index_by_pagename('testpage10', $index);
     $this->assertEquals(2, count(ouwiki_get_sub_tree_from_index($page->pageid, $index)));
 }
            if (count($indexitem->linksfrom) != 0 || $indexitem->title === '') {
                print '<li>' . ouwiki_display_wikiindex_page_in_index($indexitem, $subwiki, $cm) . '</li>';
            } else {
                $orphans = true;
            }
        }
        print '</ul>';
    }
}
if ($orphans) {
    print '<h2 class="ouw_orphans">' . get_string('orphanpages', 'ouwiki') . '</h2>';
    print '<ul class="ouw_index">';
    foreach ($index as $indexitem) {
        if (count($indexitem->linksfrom) == 0 && $indexitem->title !== '') {
            if ($treemode) {
                $orphanindex = ouwiki_get_sub_tree_from_index($indexitem->pageid, $index);
                ouwiki_build_tree($orphanindex);
                print ouwiki_tree_index($func, $indexitem->pageid, $orphanindex, $subwiki, $cm);
            } else {
                print '<li>' . ouwiki_display_wikiindex_page_in_index($indexitem, $subwiki, $cm) . '</li>';
            }
        }
    }
    print '</ul>';
}
$missing = ouwiki_get_subwiki_missingpages($subwiki->id);
if (count($missing) > 0) {
    print '<div class="ouw_missingpages"><h2>' . get_string('missingpages', 'ouwiki') . '</h2>';
    print '<p>' . get_string(count($missing) > 1 ? 'advice_missingpages' : 'advice_missingpage', 'ouwiki') . '</p>';
    print '<ul>';
    $first = true;
 public function prepare_package()
 {
     global $CFG;
     $pagehtml = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' . '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . html_writer::start_tag('html', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
     $content = html_writer::empty_tag('meta', array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')) . html_writer::tag('title', get_string('export', 'forumngfeature_export'));
     if ($this->tree) {
         $content .= $this->prepare_tree_inline_styles();
     }
     $pagehtml .= html_writer::tag('head', $content);
     $pagehtml .= html_writer::start_tag('body') . "\n";
     $pagehtml .= html_writer::tag('h1', s($this->ouwiki->name));
     if ($this->tree) {
         $orphans = false;
         $pagehtml .= '</ul>';
         foreach ($this->pageversions as $pageversion) {
             if (count($pageversion->linksfrom) == 0 && $pageversion->title !== '') {
                 $orphans = true;
             } else {
                 $pageversion->xhtml = $this->prepare_page($pageversion);
             }
         }
         $pagehtml .= '<ul class="ouw_indextree">';
         $func = 'ouwiki_display_portfolio_page_in_index';
         $pagehtml .= ouwiki_tree_index($func, reset($this->pageversions)->pageid, $this->pageversions, $this->subwiki, $this->cm);
         $pagehtml .= '</ul>';
         if ($orphans) {
             $pagehtml .= '<h2 class="ouw_orphans">' . get_string('orphanpages', 'ouwiki') . '</h2>';
             $pagehtml .= '<ul class="ouw_indextree">';
             foreach ($this->pageversions as $pageversion) {
                 if (count($pageversion->linksfrom) == 0 && $pageversion->title !== '') {
                     $pageversion->xhtml = $this->prepare_page($pageversion);
                     $orphanindex = ouwiki_get_sub_tree_from_index($pageversion->pageid, $this->pageversions);
                     ouwiki_build_tree($orphanindex);
                     $pagehtml .= ouwiki_tree_index($func, $pageversion->pageid, $orphanindex, $this->subwiki, $this->cm);
                 }
             }
             $pagehtml .= '</ul>';
         }
     } else {
         $orphans = false;
         foreach ($this->pageversions as $pageversion) {
             if (count($pageversion->linksfrom) == 0 && $pageversion->title !== '') {
                 $orphans = true;
             } else {
                 $pagehtml .= $this->prepare_page($pageversion);
             }
         }
         if ($orphans) {
             $pagehtml .= '<h2 class="ouw_orphans">' . get_string('orphanpages', 'ouwiki') . '</h2>';
             foreach ($this->pageversions as $pageversion) {
                 if (count($pageversion->linksfrom) == 0 && $pageversion->title !== '') {
                     $pagehtml .= $this->prepare_page($pageversion);
                 }
             }
         }
     }
     $pagehtml .= html_writer::end_tag('body') . html_writer::end_tag('html');
     $content = $pagehtml;
     $name = $this->make_filename_safe($this->ouwiki->name) . '.html';
     $manifest = $this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH;
     $this->copy_files($this->multifiles);
     $this->get('exporter')->write_new_file($content, $name, $manifest);
 }