/**
 * Display History tab content with page versions
 */
function wiki_info_content(&$WS)
{
    global $CFG, $COURSE, $USER;
    //generate a list with all versions
    $vers = wiki_get_all_page_versions($CFG->prefix, $WS->page, $WS->dfwiki->id, $WS->groupmember->groupid, $WS->member->id);
    if ($vers) {
        $countver = count($vers);
        if ($countver > 1) {
            echo '<script type="text/javascript" src="wiki/hist.js"></script>';
            echo '<form id="historyform" method="post" ' . 'action="view.php?id=' . $WS->linkid . '&amp;page=' . urlencode('diff/' . $WS->page) . "&amp;gid={$WS->groupmember->groupid}&amp;uid={$WS->member->id}" . '">';
            echo '<input type="submit" name="' . get_string('compareversions', 'wiki') . '" value="' . get_string('compareversions', 'wiki') . '"/>';
            echo '<br/><br/>';
        }
        //print table with the page versions
        $prop = null;
        $prop->id = 'historytable';
        $prop->width = "100%";
        $prop->border = "1";
        $prop->padding = "5";
        $prop->spacing = "1";
        $prop->class = "generaltable boxalignleft";
        $prop->header = "true";
        $prop->valignth = "top";
        $prop->classth = 'header c0 nwikileftnow';
        wiki_table_start($prop);
        if ($countver > 1) {
            echo '';
            $prop = null;
            $prop->header = "true";
            $prop->valign = "top";
            $prop->class = "nwikileftnow header c1";
            wiki_change_column($prop);
        }
        echo get_string('version');
        $prop = null;
        $prop->header = "true";
        $prop->valign = "top";
        $prop->class = "nwikileftnow header c1";
        wiki_change_column($prop);
        echo get_string('user');
        $prop = null;
        $prop->header = "true";
        $prop->valign = "top";
        $prop->class = "nwikileftnow header c2";
        wiki_change_column($prop);
        echo get_string('lastmodified');
        if (wiki_grade_got_permission($WS)) {
            $prop = null;
            $prop->header = "true";
            $prop->valign = "top";
            $prop->class = "nwikileftnow header c3";
            wiki_change_column($prop);
            echo get_string('eval_editions_quality', 'wiki');
        }
        $prop = null;
        $prop->header = "true";
        //print content
        $i = 0;
        foreach ($vers as $ver) {
            if ($ver->highlight) {
                $class = "textcenter nwikihighlight";
            } else {
                $class = "textcenter nwikibargroundblanco";
            }
            if (isset($prop->header)) {
                $prop->class = $class;
                wiki_change_row($prop);
            } else {
                $prop = null;
                $prop->class = $class;
                wiki_change_row($prop);
            }
            // input types for history diff
            if ($countver > 1) {
                $style = 'style="visibility:hidden" ';
                $checked = 'checked="checked"';
                if ($i == 0) {
                    echo '<input type="radio" value="' . $ver->version . '" name="oldid" ' . $style . ' />';
                    echo '<input type="radio" value="' . $ver->version . '" name="diff" ' . $checked . ' />';
                } elseif ($i == 1) {
                    echo '<input type="radio" value="' . $ver->version . '" name="oldid" ' . $checked . ' />';
                    echo '<input type="radio" value="' . $ver->version . '" name="diff" ' . $style . ' />';
                } else {
                    echo '<input type="radio" value="' . $ver->version . '" name="oldid" />';
                    echo '<input type="radio" value="' . $ver->version . '" name="diff" ' . $style . ' />';
                }
                $prop = null;
                $prop->class = $class;
                wiki_change_column($prop);
            }
            if ($ver->version == $WS->pagedata->version) {
                echo $ver->version;
            } else {
                $prop = null;
                $prop->href = "javascript:document.forms['formu" . $i . "'].submit()";
                $out = wiki_a($ver->version, $prop, true);
                $prop = null;
                $prop->name = "dfcontent";
                $prop->value = "11";
                $out .= wiki_input_hidden($prop, true);
                $out2 = wiki_div($out, '', true);
                $prop = null;
                $prop->id = "formu{$i}";
                $prop->action = "view.php?id={$WS->linkid}&amp;page=" . urlencode("oldversion/{$ver->pagename}") . "&amp;ver={$ver->version}&amp;gid={$WS->groupmember->groupid}&amp;uid={$WS->member->id}";
                $prop->method = "post";
                wiki_form($out2, $prop);
            }
            $prop = null;
            $prop->class = $class;
            wiki_change_column($prop);
            $author = wiki_get_user_info($ver->author);
            echo $author;
            $prop = null;
            $prop->class = $class;
            wiki_change_column($prop);
            $modified = strftime('%A, %d %B %Y %H:%M', $ver->lastmodified);
            echo $modified;
            if (wiki_grade_got_permission($WS)) {
                $prop = null;
                $prop->class = $class;
                wiki_change_column($prop);
                $scale = array(1 => "+", 2 => "=", 3 => "-");
                $gradevalue = get_field_select('wiki_evaluation_edition', 'valoration', 'wiki_pageid=' . $ver->id);
                if ($gradevalue) {
                    echo wiki_grade_translate($gradevalue, $scale);
                } else {
                    echo get_string('eval_notset', 'wiki');
                }
            }
            $i++;
        }
        wiki_table_end();
        if ($countver > 1) {
            echo '<br/>';
            echo '<input type="submit" name="' . get_string('compareversions', 'wiki') . '" value="' . get_string('compareversions', 'wiki') . '"/>';
        }
        echo '</form>';
    } else {
        print_string('noversion', 'wiki');
    }
}
function wiki_grade_get_wikigrade($WS, $gradeid = null)
{
    global $CFG, $USER;
    $wikimanager = wiki_manager_get_instance();
    $scale = get_record('scale', 'id', (int) $WS->dfwiki->notetype);
    $user = optional_param('uid', 0, PARAM_INT);
    $group = optional_param('gid', 0, PARAM_INT);
    $where = wiki_grade_get_sql_filter($WS->cm->groupmode, $WS->dfwiki->studentmode, $group, $user);
    if (!$gradeid) {
        $gradeid = get_field_sql('SELECT AVG(wikigrade) as wikigrade 
                                  FROM ' . $CFG->prefix . 'wiki_evaluation
                                  WHERE wikiid=' . $WS->dfwiki->id . ' AND pagename="' . addslashes($WS->page) . '"' . ' AND ' . $where . ' GROUP BY pagename, wikiid, groupid, ownerid');
    }
    if (!$gradeid) {
        return get_string('eval_notset', 'wiki');
    }
    $scale_values = split(',', $scale->scale);
    $num_values = count($scale_values);
    $values = array();
    $i = 0;
    while ($i < $num_values) {
        $values[$i + 1] = trim($scale_values[$i]);
        $i++;
    }
    $stringwikigrade = wiki_grade_translate($gradeid, $values);
    return $stringwikigrade;
}