/**
 * Returns an array with the URLs of a page (plus optinal section if it's a
 * a partial editing) to view.
 * @param  String $pagename
 * @param  String $anchor Section name
 * @param  String $anchortype (0, 1, 2)<br>
 *                   0: no anchor<br>
 *                   1: [[page#section]]<br>
 *                   2: [[page##section]]
 * @return Array  URLs Contains a list (if there are more than one section
 *                     with the same name in the same page) of URLs that
 *                     match the page and section
 */
function wiki_view_page_url($pagename, $anchor = '', $anchortype = 0, $wikibook = '')
{
    global $CFG, $COURSE, $WS;
    if ($wikibook) {
        $wikibook = '&amp;wikibook=' . urlencode($wikibook);
    }
    // support page synonyms
    $pagename = wiki_get_real_pagename($pagename);
    $urls = array();
    $page = wiki_page_last_version($pagename);
    if ($page) {
        $sectionnums = wiki_get_section_positions($page->content, $anchor);
    }
    if ($page && $anchortype > 0 && count($sectionnums) > 0) {
        if ($anchortype == 1) {
            // [[page#section]]
            foreach ($sectionnums as $sectionnum) {
                $newurl = 'view.php?id=$COURSE->id&amp;page=view/' . urlencode($pagename) . '&amp;gid=$gid&amp;uid=$uid' . $wikibook . '#' . $sectionnum;
                $newurl = wiki_format_url($newurl);
                $urls[] = $newurl;
            }
        } else {
            if ($anchortype == 2) {
                // [[page##section]]
                foreach ($sectionnums as $sectionnum) {
                    $newurl = 'view.php?id=$COURSE->id&amp;page=view/' . urlencode($pagename) . '&amp;gid=$gid&amp;uid=$uid' . $wikibook . '&amp;section=' . urlencode($anchor) . '&amp;sectionnum=' . $sectionnum;
                    $newurl = wiki_format_url($newurl);
                    $urls[] = $newurl;
                }
            }
        }
    } else {
        // no anchor
        $newurl = 'view.php?id=' . $COURSE->id . '&amp;page=' . urlencode($pagename) . '&amp;gid=$gid&amp;uid=$uid' . $wikibook;
        $newurl = wiki_format_url($newurl);
        $urls[] = $newurl;
    }
    return $urls;
}
    function built_index($page, $level)
    {
        global $CFG, $WS;
        //printed links array
        $printed_links = array();
        // dfwiki-block || course-block
        $dir = "";
        if ($this->instance->pagetype == "mod-wiki-view") {
            $dir = $CFG->wwwroot . '/mod/wiki/view.php?id=' . $WS->cm->id;
        } else {
            $dir = $CFG->wwwroot . '/course/view.php?id=' . $WS->cm->course;
        }
        //search in vector
        if (!in_array($page, $this->pagesel)) {
            //put in vector
            $this->pagesel[] = $page;
            //get last version
            if (!($pageinfo = wiki_page_last_version($page))) {
                //empty page
                //put in result
                return '<li class="wiki_listme">' . $this->images['square'] . format_text($this->trim_string($page, 20), FORMAT_PLAIN) . '<a href="' . $dir . '&amp;gid=' . $WS->groupmember->groupid . '&amp;uid=' . $WS->member->id . '&amp;page=' . urlencode($page) . '">?</a></li>';
            } else {
                //get links
                $links = wiki_internal_link_to_array($pageinfo->refs);
                $links = wiki_filter_section_links($links);
                //put in result
                if (count($links) == 0) {
                    $res = '<li class="wiki_listme">' . $this->images['square'] . '<a href="' . $dir . '&amp;gid=' . $WS->groupmember->groupid . '&amp;uid=' . $WS->member->id . '&amp;page=' . urlencode($page) . '">' . format_text($this->trim_string($page, 20), FORMAT_PLAIN) . '</a>' . '</li>';
                } else {
                    //determine if this level is opened or close
                    if ($level <= $this->levelsel) {
                        $display = '';
                        $image_ico = $this->images['minus'];
                    } else {
                        $display = 'display:none';
                        $image_ico = $this->images['plus'];
                    }
                    $res = '<li class="wiki_listme">' . $image_ico . '<a href="' . $dir . '&amp;gid=' . $WS->groupmember->groupid . '&amp;uid=' . $WS->member->id . '&amp;page=' . urlencode($page) . '">' . format_text($this->trim_string($page, 20), FORMAT_PLAIN) . '</a>
						<ul class="wiki_listme" style="margin:auto auto auto 15px;' . $display . '">';
                    //foreach link do recursive
                    foreach ($links as $link) {
                        //get real page name
                        $link = wiki_get_real_pagename($link);
                        //search in printed_links
                        if (!in_array($link, $printed_links)) {
                            //put in printed links
                            $printed_links[] = $link;
                            //mount
                            $res .= $this->built_index($link, $level + 1);
                        }
                    }
                    $res .= '</ul></li>';
                }
                //return result
                return $res;
            }
        } else {
            //page is already printed
            return '<li class="wiki_listme">' . $this->images['square'] . '<a href="' . $dir . '&amp;gid=' . $WS->groupmember->groupid . '&amp;uid=' . $WS->member->id . '&amp;page=' . urlencode($page) . '"><i>' . format_text($this->trim_string($page, 20), FORMAT_PLAIN) . '</i></a></li>';
        }
    }
/**
 * Returns true if the section exist in the page
 *
 * @param $pagename Page name
 * @param $section  Section name
 */
function wiki_section_exists($pagename, $section)
{
    $pagename = wiki_get_real_pagename($pagename);
    $page = wiki_page_last_version($pagename);
    $sectionnums = wiki_get_section_positions($page->content, $section);
    if (count($sectionnums) < 1) {
        return false;
    }
    return true;
}
 function get_content()
 {
     global $CFG, $WS;
     $wiki = wiki_param('dfwiki');
     $groupmember = wiki_param('groupmember');
     if ($this->content !== NULL) {
         return $this->content;
     }
     //Scritp WIKI_TREE
     $prop = null;
     $prop->type = 'text/javascript';
     if (isset($WS->dfcourse)) {
         $prop->src = '../mod/wiki/editor/wiki_tree.js';
     } else {
         $prop->src = 'editor/wiki_tree.js';
     }
     wiki_script('', $prop);
     $this->content = new stdClass();
     $this->content->items = array();
     $this->content->icons = array();
     //$this->content->footer = '<br />'.helpbutton ('list_pages', get_string('block_list_pages', 'wiki'), 'wiki', true, false, '', true).get_string('block_list_pages', 'wiki');
     $this->content->items[] = '<a href="http://www.google.com">google</a>';
     $this->content->icons[] = '<img src="icon.gif" alt="">';
     //If we are out of a dfwiki activity or in a different
     //dfwiki format course and we want to create a block:
     if (empty($WS->dfwiki)) {
         $this->content->text = get_string('block_warning', 'wiki');
         return $this->content;
     }
     $ead = wiki_manager_get_instance();
     $pages = $ead->get_wiki_page_names_of_wiki($wiki, $groupmember->groupid);
     $this->orderpages($pages);
     $syns = $ead->get_wiki_synonyms();
     $this->orderpages($syns);
     $list = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="nwikileftnow">
 			<ul class="wiki_listme">';
     if (!isset($WS->pagedata->pagename)) {
         $WS->load_page_data();
     }
     if (count($pages) != 0) {
         foreach ($this->ordereds as $key => $ordered) {
             //chack if the letter is the curretn one.
             if ($key == $this->get_index($WS->pagedata->pagename)) {
                 $display = '';
                 $image_ico = $this->images['minus'];
             } else {
                 $display = 'display:none';
                 $image_ico = $this->images['plus'];
             }
             $list .= '<li class="wiki_listme">' . $image_ico . '<a href="#" class="wiki_folding">' . $key . '</a>
 					<ul class="wiki_listme" style="margin:auto auto auto 15px;' . $display . '">';
             // dfwiki-block || course-block
             $dir = "";
             if ($this->instance->pagetype == "mod-wiki-view") {
                 $dir = $CFG->wwwroot . '/mod/wiki/view.php?id=' . $WS->cm->id;
             } else {
                 $dir = $CFG->wwwroot . '/course/view.php?id=' . $WS->cm->course;
             }
             foreach ($ordered as $link) {
                 if (in_array($link, $syns)) {
                     $page_icon = $this->images['syn'];
                     $page = wiki_get_real_pagename($link);
                 } else {
                     $page_icon = $this->images['square'];
                     $page = $link;
                 }
                 $list .= '<li class="wiki_listme">' . $page_icon . '<a href="' . $dir . '&amp;page=' . urlencode($page) . '&amp;gid=' . $WS->groupmember->groupid . '&amp;uid=' . $WS->member->id . '" title="' . urlencode($link) . '">' . format_text($this->trim_string($link, 20), FORMAT_PLAIN) . '</a>' . '</li>';
             }
             $list .= '</ul></li>';
         }
     } else {
         $list .= '<li>' . get_string('nopages', 'wiki') . '</li>';
     }
     $list .= '</ul></td></tr></table>';
     $this->content->text = $list;
     $this->content->footer .= '<style>.wiki_listme { padding-right:5px; } .dir-rtl td.nwikileftnow  { text-align:right; } </style>';
     return $this->content;
 }
 function built_level($tofind, $page, $level)
 {
     global $WS, $CFG;
     //printed links array
     $printed_links = array();
     //search in vector
     if (!in_array($page, $this->pagesel)) {
         //put in vector
         $this->pagesel[] = $page;
         if ($tofind == $page) {
             if ($this->pagelev > $level) {
                 $this->pagelev = $level;
             }
         } else {
             //get last version
             if ($pageinfo = wiki_page_last_version($page)) {
                 //get links
                 $links = wiki_internal_link_to_array($pageinfo->refs);
                 //foreach link do recursive
                 foreach ($links as $link) {
                     //get real page name
                     $link = wiki_get_real_pagename($link);
                     //search in printed_links
                     if (!in_array($link, $printed_links)) {
                         //put in printed links
                         $printed_links[] = $link;
                         //mount
                         $this->built_level($tofind, $link, $level + 1);
                     }
                 }
             }
         }
     }
 }
 /**
  * return an array with all pages that have a link to the given one.
  * @param String $pagename
  * @return array of wikipages record object
  */
 function get_wiki_page_camefrom($pagename)
 {
     global $CFG, $WS;
     $pagename = wiki_get_real_pagename($pagename);
     //get all pages list in the current dfwiki  EXCLUDING DISCUSSION PAGES
     $quer = 'dfwiki=' . $WS->dfwiki->id . '
                     AND groupid=' . $WS->groupmember->groupid . '
                     AND pagename NOT LIKE \'discussion:%\'
                     AND refs LIKE \'%' . addslashes($pagename) . '%\'';
     $res = array();
     if ($pages = get_records_select('wiki_pages', $quer, 'pagename ASC', 'DISTINCT pagename,dfwiki')) {
         foreach ($pages as $page) {
             $res[] = $page;
         }
     }
     return $res;
 }