function wiki_edit_content(&$WS)
{
    global $CFG;
    $context = get_context_instance(CONTEXT_MODULE, $WS->cm->id);
    require_capability('mod/wiki:caneditawiki', $context);
    $cont = false;
    $txt = '';
    $titlebox = '';
    $oldcontentbox = '';
    $viewtitle = '0';
    if ($WS->pageaction == 'adddiscussion' or isset($WS->dfform['addtitle']) and $WS->pageaction == 'edit') {
        $viewtitle = '1';
        if (isset($WS->dfform['addtitle'])) {
            $titlebox = $WS->dfform['addtitle'];
            $oldcontentbox = $WS->dfform['oldcontent'];
            $contentbox = $WS->dfform['content'];
        } else {
            $titlebox = '';
            $oldcontentbox = $WS->dfform['content'];
            $contentbox = '';
        }
    } else {
        if (isset($WS->dfform['content'])) {
            $contentbox = $WS->dfform['content'];
        } else {
            $contentbox = '';
        }
    }
    //need to check if  we have preview mode
    $preview = optional_param('dfformpreview', NULL, PARAM_ALPHA);
    if (isset($preview)) {
        //we're in preview mode
        print_simple_box_start('center', '100%', '', '20');
        wiki_size_text("Preview", 2);
        // discussions
        if ($titlebox == '') {
            $txt = $oldcontentbox . $contentbox;
        } else {
            switch ($WS->dfwiki->editor) {
                case 'ewiki':
                    $txt = $oldcontentbox . chr(13) . chr(10) . '!!! ' . $titlebox . ' ' . chr(13) . chr(10) . $contentbox . chr(10);
                    break;
                case 'htmleditor':
                    $txt = $oldcontentbox . chr(13) . chr(10) . '<h1> ' . $titlebox . ' </h1>' . chr(13) . chr(10) . $contentbox . chr(10);
                    break;
                default:
                    $txt = $oldcontentbox . chr(13) . chr(10) . '= ' . $titlebox . ' =' . chr(13) . chr(10) . $contentbox . chr(10);
                    break;
            }
        }
        wiki_print_page_content(stripslashes_safe($txt), $WS);
        print_simple_box_end();
    } else {
        //we're not in preview mode
        //check if there's content
        //echo 'wiki_forceeditorchange='.$CFG->wiki_forceeditorchange;
        if (!isset($WS->pagedata->content) || $CFG->wiki_forceeditorchange) {
            if (!isset($WS->pagedata->content)) {
                $WS->pagedata->content = '';
            }
            //  si no hem decidit encara l'editor per aquesta pagina i no hem adjuntat cap arxiu demanem l'editor
            if (!isset($WS->dfform['selectedit']) && (!isset($WS->pagedata->editor) || $WS->pagedata->editor == '') || $CFG->wiki_forceeditorchange) {
                $cont = true;
                //$pagename=stripslashes($pagename);
                $prop = null;
                $prop->border = "0";
                $prop->class = "nwikileftnow";
                wiki_table_start($prop);
                /// removed "choose different wiki syntax on new page listbox" so teachers will not get confused (nadavkav 25.10.09)
                /*
                				print_string('anothereditor','wiki');
                				//Converts reserved chars for html to prevent chars misreading
                				$pagetemp = stripslashes_safe($WS->page);
                
                                wiki_change_column();
                				$prop = null;
                				$prop->id = "sel_edit";
                				$prop->method = "post";
                				$prop->action = 'view.php?id='.$WS->linkid.'&amp;page=edit/'.urlencode($pagetemp).'&amp;gid='.$WS->groupmember->groupid.'&amp;uid='.$WS->member->id;
                				wiki_form_start($prop);
                				wiki_div_start();
                				$opt = "";
                                if ($WS->dfwiki->editor != 'dfwiki'){
                                	$prop = null;
                                	$prop->value = "0";
                                	$opt = wiki_option(get_string('dfwiki','wiki'), $prop, true);
                                }
                                if ($WS->dfwiki->editor != 'nwiki'){
                                	$prop = null;
                                	$prop->value = "3";
                                	$opt .= wiki_option(get_string('nwiki','wiki'), $prop, true);
                                }
                                if ($WS->dfwiki->editor != 'ewiki'){
                                	$prop = null;
                                	$prop->value = "1";
                                	$opt .= wiki_option(get_string('ewiki','wiki'), $prop, true);
                				}
                                if ($WS->dfwiki->editor != 'htmleditor'){
                                	$prop = null;
                                	$prop->value = "2";
                                	$opt .= wiki_option(get_string('htmleditor','wiki'), $prop, true);
                                	}
                                $prop = null;
                                $prop->name = "dfformselectedit";
                                wiki_select($opt,$prop);
                                $prop = null;
                                $prop->value = get_string("continue");
                                wiki_input_submit($prop);
                                wiki_div_end();
                                wiki_form_end();
                */
                $prop = null;
                $prop->class = "nwikileftnow";
                wiki_change_row($prop);
                echo "&nbsp;";
                wiki_table_end();
                if (isset($WS->dfform['content'])) {
                    $content_text =& $WS->dfform['content'];
                } else {
                    $content_text =& $WS->pagedata->content;
                }
                if (isset($WS->dfform['selectedit'])) {
                    switch ($WS->dfform['selectedit']) {
                        case '0':
                            $WS->pagedata->editor = $WS->dfwiki->editor = 'dfwiki';
                            break;
                        case '1':
                            $WS->pagedata->editor = $WS->dfwiki->editor = 'ewiki';
                            break;
                        case '2':
                            $WS->pagedata->editor = $WS->dfwiki->editor = 'htmleditor';
                            break;
                        case '3':
                            $WS->pagedata->editor = $WS->dfwiki->editor = 'nwiki';
                            break;
                        default:
                            error('No editor was selected');
                            break;
                    }
                } else {
                    $WS->pagedata->editor = $WS->dfwiki->editor;
                }
                wiki_print_editor($content_text, $viewtitle, $titlebox, '', $WS);
            }
        }
    }
    if (isset($WS->dfform['content'])) {
        $content_text = stripslashes_safe($WS->dfform['content']);
    } else {
        $content_text = stripslashes_safe($WS->pagedata->content);
    }
    // partial editing of a section available only with nwiki editor
    $section = optional_param('section', '', PARAM_TEXT);
    $sectionnum = optional_param('sectionnum', 0, PARAM_INTEGER);
    if (!isset($preview) && $WS->pagedata->editor == 'nwiki' && !($section == '' && $sectionnum == 0) && !wiki_is_uploading_modified()) {
        $section = stripslashes($section);
        $sectionnum = wiki_get_real_section_position($WS->page, $section, $sectionnum);
        $res = wiki_split_sections($content_text, $section, $sectionnum);
        if ($res->error != '') {
            $sectionhash = optional_param('sectionhash', '', PARAM_TEXT);
            wiki_release_lock($WS, $WS->dfwiki->id, $WS->pagedata->pagename . '#' . $sectionhash);
            error($res->error);
        } else {
            $content_text = $res->current_part;
        }
    }
    //print the editor
    if ($cont == false) {
        if (!isset($WS->dfform['selectedit'])) {
            $editor = optional_param('editor', '', PARAM_ALPHA);
            if (!empty($WS->dfform['editor'])) {
                $WS->pagedata->editor = $WS->dfform['editor'];
            } else {
                if (!empty($editor)) {
                    $WS->pagedata->editor = $editor;
                }
            }
            if ($WS->pageaction == 'adddiscussion' or isset($WS->dfform['addtitle'])) {
                if (!isset($oldcontentbox)) {
                    wiki_print_editor($contentbox, $viewtitle, $titlebox, $content_text, $WS);
                } else {
                    wiki_print_editor($contentbox, $viewtitle, $titlebox, $oldcontentbox, $WS);
                }
            } else {
                wiki_print_editor($content_text, $viewtitle, $titlebox, '', $WS);
            }
        } else {
            //comming from the above form
            switch ($WS->dfform['selectedit']) {
                case '0':
                    $WS->pagedata->editor = 'dfwiki';
                    wiki_print_editor($contentbox, $viewtitle, $titlebox, $oldcontentbox, $WS);
                    break;
                case '1':
                    $WS->pagedata->editor = 'ewiki';
                    wiki_print_editor($contentbox, $viewtitle, $titlebox, $oldcontentbox, $WS);
                    break;
                case '2':
                    $WS->pagedata->editor = 'htmleditor';
                    wiki_print_editor($contentbox, $viewtitle, $titlebox, $oldcontentbox, $WS);
                    break;
                case '3':
                    $WS->pagedata->editor = 'nwiki';
                    wiki_print_editor($contentbox, $viewtitle, $titlebox, $oldcontentbox, $WS);
                    break;
                default:
                    error('No editor was selected');
                    break;
            }
        }
    }
}
function parse_nwiki_text($text)
{
    global $regex, $nowikitext, $USER, $tocheaders, $WS;
    // Security Patch !!!
    // $find will contain the regex to find
    // $find   = array_keys($regex['than']);
    // $replace will contain the replacements of items in $find
    // $replace = array_values($regex['than']);
    // $text    = preg_replace($find, $replace, $text);
    $section = optional_param('section', '', PARAM_TEXT);
    $sectionnum = optional_param('sectionnum', 0, PARAM_INTEGER);
    $sectionhash = optional_param('sectionhash', '', PARAM_TEXT);
    $preview = optional_param('dfformpreview', NULL, PARAM_TEXT);
    $action = optional_param('dfformaction', NULL, PARAM_TEXT);
    if (!($section == '' && $sectionnum == 0) && $preview == '' && $action == '') {
        // check if the section exists or it has been deleted
        $section = stripslashes($section);
        $section_positions = wiki_get_section_positions($text, $section);
        if (!in_array($sectionnum, $section_positions)) {
            $num_positions = count($section_positions);
            if ($num_positions == 0) {
                // section has been deleted
                $error_text = get_string('sectionerror', 'wiki', $section) . ': ' . strtolower(get_string('sectiondeleted', 'wiki'));
                error($error_text);
            } elseif ($num_positions == 1) {
                // section position has changed
                $sectionnum = $section_positions[0];
            } else {
                // there are more than one section with this name
                $error_text = get_string('sectionerror', 'wiki', $section) . ': ' . strtolower(get_string('sectionmultiple', 'wiki')) . ': ';
                if (isset($WS->dfcourse)) {
                    $type = 'course';
                } else {
                    $type = 'mod';
                }
                $urls = wiki_view_page_url($WS->page, $section, 2, '', $type);
                $num_urls = count($urls);
                for ($i = 0; $i < $num_urls; $i++) {
                    if ($i == 0) {
                        $error_text .= '<a href="' . $urls[$i] . '">[[' . $WS->page . '##' . $section . '</a>';
                    } elseif ($i == $num_urls - 1) {
                        $error_text .= ', <a href="' . $urls[$i] . '">' . ($i + 1) . ']]</a>';
                    } else {
                        $error_text .= ', <a href="' . $urls[$i] . '">' . ($i + 1) . '</a>';
                    }
                }
                error($error_text);
            }
        }
        $res = wiki_split_sections($text, $section, $sectionnum);
        if ($res->error != '') {
            $text = $res->error;
        } else {
            $text = $res->current_part;
        }
    }
    // Remove carriage returns
    $text = str_replace("\r", "", $text);
    foreach ($regex['nowiki'] as $match => $func) {
        $text = preg_replace_callback($match, $func, $text);
    }
    foreach ($regex['variable'] as $match => $func) {
        $text = preg_replace_callback($match, $func, $text);
    }
    // $find will contain the regex to find
    $find = array_keys($regex['replace']);
    // $replace will contain the replacements of items in $find
    $replace = array_values($regex['replace']);
    $text2 = preg_replace($find, $replace, $text);
    foreach ($nowikitext as $index => $singlenowikitext) {
        $text2 = preg_replace("/<<ThisIsThePlaceOfTheNoWikiText-{$index}>>/", $singlenowikitext, $text2);
    }
    // here comes the TOC, search for --TOC-- and include the real TOC
    $toc = parse_toc($tocheaders);
    $text2 = str_replace("NWikiTableOfContents", $toc, $text2);
    $text2 = preg_replace('/^\\n/m', '</p><p>', $text2);
    // remove empty paragraphs
    $text2 = preg_replace('/<p>\\s*<\\/p>/s', '', "<p>{$text2}</p>");
    // join consecutive pre blocks
    $text2 = preg_replace('/<\\/pre>\\s*<pre class="quote">/s', '<br />', $text2);
    $options->para = false;
    return format_text($text2, FORMAT_HTML, $options);
    //return $text2;
}