function test_find_sections()
 {
     $sections = ouwiki_find_sections($this->sample);
     $this->assertEqual($sections, array('0_0' => 'Start', '1_666' => 'Valid section heading', '2_666' => 'Test&', '3_666' => 'Testwhatever', '4_666' => 'Test spacing and stuff', '5_666' => 'V5', '6_666' => 'V6', '0_1' => 'End'));
 }
 * to post a new comment.
 * @copyright © 2007 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package ouwiki
 */
/** */
$countasview = true;
require 'basicpage.php';
// Get the current page version
$pageversion = ouwiki_get_current_page($subwiki, $pagename);
if (!$pageversion) {
    error('Cannot view comments when page does not exist');
}
// Need list of known sections on current version
$knownsections = ouwiki_find_sections($pageversion->xhtml);
// Get section, make sure the name is valid
$section = optional_param('section', '', PARAM_RAW);
if (!preg_match('/^[0-9]+_[0-9]+$/', $section)) {
    $section = null;
}
if ($section) {
    if (!array_key_exists($section, $knownsections)) {
        error("Unknown section {$section}");
    }
}
// Check permissions. (Note that you can always delete your
// own posts, $candelete is for other people's.)
$canpost = has_capability('mod/ouwiki:comment', $context);
$candelete = has_capability('mod/ouwiki:deletecomments', $context);
// Get list of posts
/**
 * @param object $subwiki For details of user/group and ID so that
 *   we can make links
 * @param object $cm Course-module object (again for making links)
 * @param object $pageversion Data from page and version tables.
 * @return string HTML content for page
 */
function ouwiki_display_page($subwiki, $cm, $pageversion, $gewgaws = false, $page = 'history')
{
    global $CFG;
    // Get comments - only if using per-section comment system. prevents unnecessary db access
    $comments = array();
    if (ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_PERSECTION || ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_BOTH) {
        if ($gewgaws) {
            $comments = ouwiki_get_recent_comments($pageversion->pageid, ouwiki_find_sections($pageversion->xhtml));
        }
    }
    // And params
    $params = ouwiki_display_wiki_parameters($pageversion->title, $subwiki, $cm);
    // Get annotations - only if using annotation comment system. prevents unnecessary db access
    if (ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_ANNOTATION || ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_BOTH) {
        $annotations = ouwiki_get_annotations($pageversion);
    } else {
        $annotations = '';
    }
    // Title
    $title = is_null($pageversion->title) ? get_string('startpage', 'ouwiki') : htmlspecialchars($pageversion->title);
    // setup annotations according to the page we are on
    if ($page == 'view') {
        // create the annotations
        if ((ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_ANNOTATION || ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_BOTH) && count($annotations)) {
            ouwiki_highlight_existing_annotations(&$pageversion->xhtml, $annotations, 'view');
        }
    } elseif ($page == 'annotate') {
        // call function for the annotate page
        ouwiki_setup_annotation_markers(&$pageversion->xhtml);
        ouwiki_highlight_existing_annotations(&$pageversion->xhtml, $annotations, 'annotate');
    }
    $result = '';
    $result .= '<div class="ouwiki_content"><div class="ouw_topheading">';
    $returncomments = true;
    $result .= '<div class="ouw_heading"><h1 id="ouw_topheading">' . $title . '</h1>' . ($gewgaws ? ouwiki_internal_display_heading_bit(1, $pageversion->title, $subwiki, $cm, $comments, null, $annotations, $pageversion->locked, $returncomments) : '</div>');
    // List of recent changes
    if ($gewgaws && $pageversion->recentversions) {
        $result .= '<div class="ouw_recentchanges">' . get_string('recentchanges', 'ouwiki') . ': <span class="ouw_recentchanges_list">';
        $first = true;
        foreach ($pageversion->recentversions as $recentversion) {
            if ($first) {
                $first = false;
            } else {
                $result .= '; ';
            }
            $result .= ouwiki_recent_span($recentversion->timecreated);
            $result .= ouwiki_nice_date($recentversion->timecreated) . '</span> (';
            $recentversion->id = $recentversion->userid;
            // so it looks like a user object
            $result .= ouwiki_display_user($recentversion, $cm->course, false) . ')';
        }
        if (class_exists('ouflags') && ou_get_is_mobile()) {
            $result .= '; </span></div>';
        } else {
            $result .= '; <br/><a class="seedetails" href="history.php?' . $params . '">' . get_string('seedetails', 'ouwiki') . '</a></span></div>';
        }
    }
    $result .= '</div><div class="ouw_belowmainhead">';
    if ($returncomments !== true) {
        $result .= $returncomments;
    }
    $result .= '<div class="ouw_topspacer"></div>';
    // Content of page
    $result .= ouwiki_convert_content($pageversion->xhtml, $subwiki, $cm);
    if ($gewgaws) {
        // Add in links/etc. around headings
        global $ouwiki_internal_re;
        $ouwiki_internal_re->comments = $comments;
        $ouwiki_internal_re->pagename = $pageversion->title;
        $ouwiki_internal_re->subwiki = $subwiki;
        $ouwiki_internal_re->cm = $cm;
        $ouwiki_internal_re->annotations = $annotations;
        $ouwiki_internal_re->locked = $pageversion->locked;
        $result = preg_replace_callback('|<h([1-9]) id="ouw_s([0-9]+_[0-9]+)">(.*?)(<br\\s*/>)?</h[1-9]>|s', 'ouwiki_internal_re_heading_bits', $result);
    }
    $result .= '<div class="clearer"></div></div></div>';
    if ($gewgaws) {
        $links = ouwiki_get_links_to($pageversion->pageid);
        if (count($links) > 0) {
            $result .= '<div class="ouw_linkedfrom"><h3>' . get_string(count($links) == 1 ? 'linkedfromsingle' : 'linkedfrom', 'ouwiki') . '</h3><ul>';
            $first = true;
            foreach ($links as $link) {
                $result .= ' <li>';
                if ($first) {
                    $first = false;
                } else {
                    $result .= '&#8226; ';
                }
                $result .= '<a href="view.php?' . ouwiki_display_wiki_parameters($link->title, $subwiki, $cm) . '">' . ($link->title ? htmlspecialchars($link->title) : get_string('startpage', 'ouwiki')) . '</a></li>';
            }
            $result .= '</ul></div>';
        }
        if ($subwiki->cancomment && (ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_PERSECTION || ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_BOTH)) {
            $result .= '<div id="ouw_ac_formcontainer" style="display:none">' . ouwiki_display_comment_form('view', null, null, $pageversion->title, $subwiki, $cm) . '</div>';
        }
    }
    // disply the orphaned annotations
    if ((ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_ANNOTATION || ouwiki_get_commenting($subwiki->commenting) == OUWIKI_COMMENTS_BOTH) && $annotations && $page != 'history') {
        $orphaned = '';
        foreach ($annotations as $annotation) {
            if ($annotation->orphaned) {
                $orphaned .= ouwiki_setup_hidden_annotation($annotation);
            }
        }
        $result = $orphaned !== '' ? $result . '<h3>' . get_string('orphanedannotations', 'ouwiki') . '</h3>' . $orphaned : $result;
    }
    return array($result, $annotations);
}