/**
  * Adds more content to the actual content of the discussion page. It's added at the end of the actual content.
  * Updates the attribute refs with the internal links of the discussion.
  * 
  * @param string $content. New content to add to the wiki page content.
  */
 function add_content($content)
 {
     /*
      *$this->content = $this->content + $content;
      *$content = $this->content;
      */
     // clean internal links of the page
     $links_refs = wiki_sintax_find_internal_links($content);
     $links_clean = wiki_clean_internal_links($links_refs);
     $content = wiki_set_clean_internal_links($content, $links_refs, $links_clean);
     $this->refs = wiki_internal_link_to_string($links_clean);
     $this->content .= $content;
 }
 /**
  * Sets the integral content of the wiki page.
  * Updates the attribute refs with the internal links of the wikipage.
  * Adds slashes to the page name.
  * 
  * @param string $content. New content to asign to the wiki page.
  */
 function set_content($content)
 {
     // clean internal links of the page
     $links_refs = wiki_sintax_find_internal_links($content);
     $links_clean = wiki_clean_internal_links($links_refs);
     $content = wiki_set_clean_internal_links($content, $links_refs, $links_clean);
     $this->refs = wiki_internal_link_to_string($links_clean);
     $this->content = addslashes($content);
 }
function wiki_edit_treatment(&$WS)
{
    global $USER, $CFG, $COURSE;
    $context = get_context_instance(CONTEXT_MODULE, $WS->cm->id);
    //user wants to  save the document
    $save = optional_param('dfformsave', NULL, PARAM_ALPHA);
    if (isset($save)) {
        $data->pagename = $WS->page;
        $version = optional_param('dfformversion', NULL, PARAM_INT);
        $data->version = $version + 1;
        $txt = '';
        // discussions
        if (isset($WS->dfform['addtitle'])) {
            switch ($WS->dfform['editor']) {
                case 'ewiki':
                    $txt = chr(13) . chr(10) . '!!! ' . $WS->dfform['addtitle'] . ' ' . chr(13) . chr(10);
                    break;
                case 'htmleditor':
                    $txt = chr(13) . chr(10) . '<h1> ' . $WS->dfform['addtitle'] . ' </h1>' . chr(13) . chr(10);
                    break;
                default:
                    // dfwiki or nwiki
                    $txt = chr(13) . chr(10) . '= ' . $WS->dfform['addtitle'] . ' =' . chr(13) . chr(10);
                    break;
            }
        }
        if (isset($WS->dfform['content'])) {
            if ($WS->dfform['editor'] == 'nwiki') {
                $section = optional_param('section', '', PARAM_TEXT);
                $sectionnum = optional_param('sectionnum', 0, PARAM_INTEGER);
                if ($section == '' && $sectionnum == 0) {
                    // main page
                    $restore = get_string('restore', 'wiki');
                    if (!wiki_is_locked_by_userid($WS, $WS->dfwiki->id, $data->pagename, $USER->id) && $save != $restore && substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                        error(get_string('pageoverrided', 'wiki') . ' ' . $WS->page);
                    }
                    $txt = $txt . $WS->dfform['content'];
                    $txt = stripslashes($txt);
                } else {
                    // section
                    $section = urldecode($section);
                    $newcontent = stripslashes($WS->dfform['content']);
                    $txt = wiki_join_sections($WS->page, $section, $sectionnum, $newcontent);
                }
            } else {
                $txt = $txt . $WS->dfform['content'];
            }
        }
        if (isset($WS->dfform['oldcontent'])) {
            $txt = $WS->dfform['oldcontent'] . $txt;
        }
        $data->author = $USER->username;
        $data->userid = $USER->id;
        $data->ownerid = $WS->member->id;
        $data->created = optional_param('dfformcreated', NULL, PARAM_INT);
        $data->lastmodified = time();
        //get internal links of the page
        $links_refs = wiki_sintax_find_internal_links($txt);
        $links_clean = wiki_clean_internal_links($links_refs);
        $txt = wiki_set_clean_internal_links($txt, $links_refs, $links_clean);
        $data->content = addslashes($txt);
        $WS->content = $txt;
        $data->refs = wiki_internal_link_to_string($links_refs);
        $data->editable = optional_param('dfformeditable', NULL, PARAM_BOOL);
        $data->hits = 0;
        //wiki_view_content will increase that number if necessary.
        $data->dfwiki = $WS->dfwiki->id;
        $data->editor = $WS->dfform['editor'];
        $WS->groupmember->groupid = isset($WS->gid) ? $WS->gid : $WS->groupmember->groupid;
        $data->groupid = $WS->groupmember->groupid;
        //print_object('_______________'.$data->groupid);
        $definitive = optional_param('dfformdefinitive', NULL, PARAM_INT);
        if (isset($definitive)) {
            $data->highlight = $definitive;
            if ($data->highlight != 1) {
                $data->highlight = 0;
            }
        }
        //Check if the version passed is the last one or another one.
        if (($max = wiki_page_current_version_number($data, $WS)) >= $data->version) {
            notify("WARNING: some version may be overwrited.", 'notifyproblem', $align = 'center');
            $data->version = $max + 1;
        }
        ///Add some slashes before inserting the record
        $data->pagename = addslashes($data->pagename);
        $wikimanager = wiki_manager_get_instance();
        $page = new wiki_page(PAGERECORD, $data);
        if (!($pageid = $wikimanager->save_wiki_page($page))) {
            echo $WS->page;
            error(get_string('noinsert', 'wiki'));
        }
        add_to_log($COURSE->id, 'wiki', "save page", addslashes("view.php?id={$WS->cm->id}&amp;page={$WS->page}"), $pageid, $WS->cm->id);
    }
    //do a preview
    $preview = optional_param('dfformpreview', NULL, PARAM_ALPHA);
    if (isset($preview)) {
        //set pageaction to edit
        $WS->pageaction = 'edit';
    }
    //cancell action
    $cancel = optional_param('dfformcancel', NULL, PARAM_ALPHA);
    if (isset($cancel)) {
        //do nothing
    }
    // allow file uploading and deleting in sections also
    $sectionhash = optional_param('sectionhash', '', PARAM_TEXT);
    if ($sectionhash != '') {
        $pagename = $WS->page . '#' . $sectionhash;
    } else {
        $pagename = $WS->page;
    }
    //look for uploaded files
    $upload = optional_param('dfformupload', NULL, PARAM_ALPHA);
    if (isset($upload)) {
        if (!wiki_is_locked_by_userid($WS, $WS->dfwiki->id, $pagename, $USER->id)) {
            if ($sectionhash == '') {
                error(get_string('pageoverrided', 'wiki'));
            } else {
                error(get_string('sectionoverrided', 'wiki'));
            }
        }
        require_capability('mod/wiki:uploadfiles', $context);
        wiki_upload_file('dfformfile', $WS);
        if ($WS->pageaction == 'view') {
            wiki_release_lock($WS, $WS->dfwiki->id, $pagename);
            $WS->pageaction = 'edit';
        }
    }
    $wikitags = optional_param('wikitags', '', PARAM_TEXT);
    wiki_tags_save_tags($WS, $wikitags);
    //check for deleted file@@@@@@@@@
    $delfile = optional_param('dfformdelfile');
    if (!empty($delfile)) {
        if (!wiki_is_locked_by_userid($WS, $WS->dfwiki->id, $pagename, $USER->id)) {
            if ($sectionhash == '') {
                error(get_string('pageoverrided', 'wiki'));
            } else {
                error(get_string('sectionoverrided', 'wiki'));
            }
        }
        require_capability('mod/wiki:deletefiles', $context);
        if (isset($WS->dfdir->content)) {
            wiki_upload_del($delfile, $WS);
            if ($WS->pageaction == 'view') {
                wiki_release_lock($WS, $WS->dfwiki->id, $pagename);
                $WS->pageaction = 'edit';
            }
        }
    }
    //either cancell or save action was selected
    if (isset($cancel) or isset($save)) {
        //need to close the window if we're working on a Wiki Book
        if (strrchr($WS->dfwiki->name, "DFIT::")) {
            echo '<script language="javascript"> window.opener.location.reload(); window.close(); </script>';
        }
    }
}
function wiki_grade_append_to_discussion($WS, $pagename, $grade, $comment)
{
    global $USER, $CFG, $COURSE;
    $context = get_context_instance(CONTEXT_MODULE, $WS->cm->id);
    $wikimanager = wiki_manager_get_instance();
    $title = get_string('eval_discuss_title', 'wiki');
    switch ($WS->dfform['editor']) {
        case 'ewiki':
            $append = chr(13) . chr(10) . '!!! ' . $title . ' ' . chr(13) . chr(10);
            break;
        case 'htmleditor':
            $append = chr(13) . chr(10) . '<h1> ' . $title . ' </h1>' . chr(13) . chr(10);
            break;
        default:
            $append = chr(13) . chr(10) . '= ' . $title . ' =' . chr(13) . chr(10);
            break;
    }
    $anonymous = optional_param('grade_anonymous', '', PARAM_TEXT);
    if ($anonymous == 'on') {
        $a->user = get_string('eval_anonymous_user', 'wiki');
    } else {
        $info = $wikimanager->get_user_info_by_id($USER->id);
        $userprofile = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $USER->id . '&amp;course=' . $COURSE->id . '">' . $info->firstname . ' ' . $info->lastname . '</a>';
        $a->user = get_string('user') . ' ' . $userprofile;
    }
    $a->grade = wiki_grade_get_wikigrade($WS, $grade);
    $append .= get_string('eval_discuss_body_1', 'wiki', $a);
    $append .= "\n {$comment}\n";
    unset($a);
    $a->quality = wiki_grade_get_quality($pagename, $WS->dfwiki->id, $WS->member->id, $WS->groupmember->groupid, 0);
    $append .= get_string('eval_discuss_body_2', 'wiki', $a);
    $pagename = 'discussion:' . $pagename;
    $data->pagename = $pagename;
    $page = wiki_page_last_version($pagename, $WS);
    if ($page) {
        $txt = $page->content . $append;
        $data->version = $page->version + 1;
        $data->created = $page->created;
        //get internal links of the page
        $links_refs = wiki_sintax_find_internal_links($txt);
        $links_clean = wiki_clean_internal_links($links_refs);
        $txt = wiki_set_clean_internal_links($txt, $links_refs, $links_clean);
        $data->refs = wiki_internal_link_to_string($links_refs);
        $data->editor = $page->editor;
        $data->editable = $page->editable;
        $data->hits = $page->hits;
    } else {
        $txt = $append;
        $data->version = 1;
        $data->created = time();
        $data->refs = '';
        //$data->editor   = $WS->dfform['editor'];
        $data->editor = 'nwiki';
        $data->editable = 1;
        $data->hits = 0;
    }
    $data->author = $USER->username;
    $data->userid = $USER->id;
    $data->ownerid = $WS->member->id;
    $data->lastmodified = time();
    $data->content = addslashes($txt);
    $data->dfwiki = $WS->dfwiki->id;
    $data->groupid = $WS->groupmember->groupid;
    //$WS->groupmember->groupid = isset($WS->gid) ? $WS->gid : $WS->groupmember->groupid;
    //Check if the version passed is the last one or another one.
    if (($max = wiki_page_current_version_number($data, $WS)) >= $data->version) {
        notify("WARNING: some discussion version may be overwrited.", 'notifyproblem', $align = 'center');
        $data->version = $max + 1;
    }
    ///Add some slashes before inserting the record
    $data->pagename = addslashes($data->pagename);
    $newpage = new wiki_page(PAGERECORD, $data);
    if (!($pageid = $wikimanager->save_wiki_page($newpage))) {
        error(get_string('noinsert', 'wiki'));
    }
    add_to_log($COURSE->id, 'wiki', 'save page', addslashes("view.php?id={$WS->cm->id}&amp;page={$pagename}"), $pageid, $WS->cm->id);
}