<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.4.2 www.exoops.info 2006/03
#####################################################
#  Based on Quizz 1.4.1
#  by xbee (xbee@xbee.net) http://www.xbee.net
#  Licence: GPL
#
#  Adapted, modified and improved for Xoops 1.0 RC3
#  by Moumou inconnu0215@noos.fr
#  and Pascal Le Boustouller pascal@xoopsien.net - http://www.xoopsien.net
#  Copyright © 2002
# Thank you to leave this copyright in place...
#####################################################
//exoops.info revamped Licence:GPL
include_once("admin_header.php");
function QuizzAdmin() {
    global $bcoosConfig,$db,$myts,$xoopsModule;
xoops_cp_header();
    OpenTable();
        echo "<center>[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdd\">"._QUIZZ_NEW."</a> ]</center><BR><BR>";
        echo "<table width='100%' border=0>";
    $result = $db->query("SELECT
        ".$db->prefix("quizz_admin").".quizzId,
        ".$db->prefix("quizz_admin").".quizzTitle,
        ".$db->prefix("quizz_admin").".active,
        ".$db->prefix("quizz_categories").".name
        FROM ".$db->prefix("quizz_admin").", ".$db->prefix("quizz_categories")."
        WHERE ".$db->prefix("quizz_admin").".cid = ".$db->prefix("quizz_categories").".cid
        ORDER BY timeStamp DESC");
        while(list($qid, $quizzTitle,$active,$category) = $db->fetchRow($result))
        {
        print "<tr><td>";
        if ($active == 1) { print "<B>"._QUIZZ_ACTIVE."</B> - "; } else {print "<B>"._QUIZZ_INACTIVE."</B> - ";}
        print "Quizz <B>$qid</B> : ";
        print "\"".$myts->MakeTboxData4Show($quizzTitle)."\" ";
        print "(".$myts->MakeTboxData4Show($category).") <BR>";
        print "[ <a href=\"".XOOPS_URL."/modules/quizz/index.php?qid=$qid\">"._QUIZZ_SEE."</a> | ";
        print "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzViewScore&qid=$qid\">"._QUIZZ_VIEWSCORE."</a> | ";
        print "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzRemoveScore&qid=$qid\">"._QUIZZ_DELSCORE."</a> | ";
        print "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzViewStats&qid=$qid\">"._QUIZZ_VIEWSTAT."</a> | ";
        print "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModify&qid=$qid\">"._MODIFY."</a> | ";
        print "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzRemove&qid=$qid\">"._DELETE."</a> ]";
                print "<tr><td>&nbsp;</td></tr>\n";
        }
        print "\n";
        print "</td></tr></table><BR><BR>\n";
 # display if available the contributor questions
    $result = $db->query("SELECT pollID, pollTitle, qid FROM ".$db->prefix("quizz_descontrib")." ORDER BY qid");
        $resultS  = mysql_num_rows($result);
        if ($resultS > 0) {
        echo "<b>"._QUIZZ_ADDCONTRIB."</b><BR><BR>";
    print "<table width='100%'>";
    while(list($pollID,$pollTitle,$qid) = $db->fetchRow($result))
    {
    print "<tr><td>Quizz $qid : <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAddContrib&pid=$pollID&qid=$qid\">".$myts->MakeTboxData4Show($pollTitle)."</a></td></tr>";
    }
    print "</table><BR><BR>";
        }
    echo "<b>"._QUIZZ_ADDCAT."</b><BR><BR>";
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='createPostedQuizzCategory'>";
    print "";
    print "<table width='100%'>";
    print "<tr><td width='120' >"._CATEGORY."</td><td><input type='text' class='text' name='CatName' size=30></td></tr>";
    print "<tr><td>"._QUIZZ_COMMENT."</td><td><input type='text' class='text' name='CatComment' size=30> (*)</td></tr>";
    print "<tr><td>"._QUIZZ_CATIMAGE."</td><td><input type='text' class='text' name='CatImage' size=30> (*)</td></tr>";
    print "<tr><td>(*) :"._QUIZZ_HELPOPTION."</td><td><input type='submit' class='button' value='"._QUIZZ_ADD."'></td></tr></table>";
    print "</form><BR>";
    echo "<b>"._QUIZZ_DELCAT."</b><BR><BR>";
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='QuizzDelCategory'>";
    print "";
    print "<table width='100%'>";
    print "<tr><td width='120' >"._CATEGORY
        ." <SELECT class='select' name=\"cid\">";
    $result = $db->query("select cid, name from ".$db->prefix("quizz_categories"));
    while(list($cid, $name) = $db->fetchRow($result)) {
            echo "<option value=\"$cid\">".$myts->MakeTboxData4Show($name)."</option>\n";
	}
    print "</select></td><td><input type='submit' class='button' value='"._DELETE."'></td></tr></table>";
    print "</form><BR>";
    echo "<b>"._MODIFYCATEGORY."</b><BR><BR>";
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='QuizzModifyCategory'>";
    print "";
    print "<table width='100%'>";
    print "<tr><td width='120'>"._CATEGORY
        ." <SELECT class='select' name=\"cid\">";
    $result = $db->query("select cid, name from ".$db->prefix("quizz_categories"));
        while(list($cid, $name) = $db->fetchRow($result))
            {
                echo "<option value=\"$cid\">".$myts->MakeTboxData4Show($name)."</option>\n";
        }
    print "</select></td><td><input type='submit' class=button value='"._MODIFY."'></td></tr></table>";
    print "</form>";
    CloseTable();
xoops_cp_footer();
}
function QuizzRemoveScore() {
global $bcoosConfig,$xoopsModule,$_GET;
    $qid = intval($_GET['qid']);
xoops_cp_header();
    OpenTable();
    echo "<center>"._QUIZZ_SURE2DELETESCORE." $qid ?<br><br>"
        ."[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=deletePostedScoreQuizz&qid=$qid\">"._YES."</a> | <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\">"._NO."</a> ]</center>";
    CloseTable();
xoops_cp_footer();
}
function QuizzViewScore() {
global $nblots,$db,$bcoosConfig,$myts,$xoopsModule, $_GET;
	$qid = intval($_GET['qid']);
    $result = $db->query("select quizzTitle, voters from ".$db->prefix("quizz_admin")." where quizzID='$qid'");
    list($quizzTitle, $nbscore) = $db->fetchRow($result);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_LISTSCORE."</b><BR>";
    $result = $db->query("SELECT username, score FROM ".$db->prefix("quizz_check")." WHERE qid='$qid' ORDER BY score DESC,time DESC LIMIT $nbscore ");
        print "<table>";
        while(list($username,$score) = $db->fetchRow($result))
        {
           print "<tr><td>".$myts->MakeTboxData4Show($username)." :</td><td> $score</td></tr>";
        }
        print "</table><BR><BR>";
        # display the drawings lots
        print "<table width='100%'>";
    print "<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='QuizzViewScore'>";
    print "<INPUT type='hidden' name='qid' value='$qid'>";
        # perform the drawings lots if needed
        if (isset($nblots))
        {
            print "<tr><td colspan=2><B>"._QUIZZ_ANDTHEWINNERSARE." :</B></td></tr>";
        $result = $db->query("SELECT username, score, email FROM ".$db->prefix("quizz_check")." WHERE qid='$qid' ORDER BY score DESC, RAND() LIMIT $nblots");
                print "<tr><td>";
        while(list($username,$score,$email) = $db->fetchRow($result))
        {
            print "".$myts->MakeTboxData4Show($username)." ($score) : <A HREF=\"mailto:".$myts->makeTboxData4Show($email)."\">".$myts->makeTboxData4Show($email)."</A><br>";
        }
                print "</td></tr>";
        }
        else
        {
                $nblots=10;
        }
    print "<tr><td align='left'><BR><form>"._QUIZZ_NBWINNERS." <input type='text' class='text' name='nblots' value='$nblots' size=3> <input type='submit' class='button' value='"._QUIZZ_LAUNCH."'></td></tr>";
        print "</form>";
    print "<tr><td><BR><center><a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\"> [ "._QUIZZ_ADMIN." ]</a></center></td></tr>";
                print "</table>";
    CloseTable();
xoops_cp_footer();
}
function QuizzViewStats() {
global $bcoosConfig,$db,$myts,$xoopsModule, $_GET;
	$qid = intval($_GET['qid']);
xoops_cp_header();
    OpenTable();
    echo "<center><a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\"> [ "._QUIZZ_ADMIN." ]</a></center>";
        echo "<BR><BR><b>"._QUIZZ_LISTSTATS."</b>";
    $result = $db->query("select quizzTitle, voters from ".$db->prefix("quizz_admin")." where quizzID='$qid'");
    list($quizzTitle, $voters) = $db->fetchRow($result);
    $result = $db->query("select MAX(score), MIN(score), AVG(score) from ".$db->prefix("quizz_check")." where qid='$qid'");
    list($max,$min,$mean) = $db->fetchRow($result);
    echo "<table width='100%' border=0>";
    echo "<tr><td>"._QUIZZ_NBVOTE." : $voters</td></tr>";
    echo "<tr><td>"._QUIZZ_MEANSCORE." : $mean </td></tr>";
    echo "<tr><td>"._QUIZZ_MINSCORE." : $min</td></tr>";
    echo "<tr><td>"._QUIZZ_MAXSCORE." : $max</td></tr>";
    echo "</table><BR><BR>";
    echo "<table width='100%' border=0>";
    $result1 = $db->query("SELECT pollID, pollTitle FROM ".$db->prefix("quizz_desc")." WHERE qid='$qid'");
    $a = 1;
        while(list($pollID, $question) = $db->fetchRow($result1))
        {
                        print "<tr><td><B> "._QUIZZ_QUESTION." $a :</B> \"".$myts->MakeTboxData4Show($question)."\" </td></tr>";
                        $result = $db->query("SELECT pollID, pollTitle, timeStamp FROM ".$db->prefix("quizz_desc")." WHERE pollID='$pollID'");
                        $result = $db->query("SELECT SUM(optionCount) AS SUM FROM ".$db->prefix("quizz_data")." WHERE pollID='$pollID'");
                        $sum = $db->fetchArray($result);
                        $sum = $sum['SUM'];
                        echo "<tr><td><table border=\"0\">";
                        /* cycle through all options */
                        for($i = 1; $i <= 12; $i++)
                        {
                                        /* select next vote option */
                                        $result = $db->query("SELECT pollID, optionText, optionCount, voteID FROM ".$db->prefix("quizz_data")." WHERE (pollID='$pollID') AND (voteID='$i')");
                                        $array = $db->fetchArray($result);
                                        if(is_array($array))
                                        {
                                                        $optionText = $array['optionText'];
                                                        $optionCount = $array['optionCount'];
                                                        if($optionText != "")
                                                        {
                                                                        echo "<tr><td>";
                                                                        echo "".$myts->MakeTboxData4Show($optionText)."";
                                                                        echo "</td>";
                                                                        if($sum)
                                                                        {
                                                                                        $percent = 100 * $optionCount / $sum;
                                                                        }
                                                                        else
                                                                        {
                                                                                        $percent = 0;
                                                                        }
                                                                        echo "<td>";
                                                                        $percentInt = (int)$percent * 4;
                                                                        $percent2 = (int)$percent;
                                                                        if ($percent > 0)
                                                                        {
                                                                                        echo "<img src=\"".XOOPS_URL."/modules/quizz/images/leftbar.gif\" height=\"12\" width=\"7\" Alt=\"$percent2 %\">";
                                                                                        echo "<img src=\"".XOOPS_URL."/modules/quizz/images/mainbar.gif\" height=\"12\" width=\"$percentInt\" Alt=\"$percent2 %\">";
                                                                                        echo "<img src=\"".XOOPS_URL."/modules/quizz/images/rightbar.gif\" height=\"12\" width=\"7\" Alt=\"$percent2 %\">";
                                                                        }
                                                                        else
                                                                        {
                                                                                        echo "<img src=\"".XOOPS_URL."/modules/quizz/images/leftbar.gif\" height=\"12\" width=\"7\" Alt=\"$percent2 %\">";
                                                                                        echo "<img src=\"".XOOPS_URL."/modules/quizz/images/mainbar.gif\" height=\"12\" width=\"3\" Alt=\"$percent2 %\">";
                                                                                        echo "<img src=\"".XOOPS_URL."/modules/quizz/images/rightbar.gif\" height=\"12\" width=\"7\" Alt=\"$percent2 %\">";
                                                                        }
                                                                        printf(" %.2f %% (%d)", $percent, $optionCount);
                                                                        echo "</td></tr>";
                                                        }
                                        }
                        }
                        echo "</table><br><br></td></tr>";
                        $a++;
        }
    echo "</table>";
    CloseTable();
xoops_cp_footer();
}
function QuizzModify() {
global $bcoosConfig,$db,$myts,$xoopsModule,$_GET;
	$qid = intval($_GET['qid']);
    $result = $db->query("SELECT quizzTitle, nbscore, displayscore, displayresults, emailadmin, comment, image, restrict_user, log_user, active, cid, contrib, expire, admemail, administrator, conditions FROM ".$db->prefix("quizz_admin")." WHERE quizzId='$qid'");
    list($quizzTitle, $nbscore, $displayscore, $displayresults, $emailadmin, $comment, $image,$restrict_user,$log_user,$active,$cid,$contrib,$expire,$admemail, $administrator,$conditions) = $db->fetchRow($result);
xoops_cp_header();
    OpenTable();
    print "<center><a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\"> [ "._QUIZZ_ADMIN." ]</a></center><BR><BR>";
        echo "<b>"._MODIFY."</b><BR><BR>";
    if ($active==1) { $act = "checked"; } else { $act = ""; }
    if ($emailadmin==1) { $eadm = "checked"; } else { $eadm = ""; }
    if ($displayscore==1) { $dis = "checked"; } else { $dis = ""; }
    if ($displayresults==1) { $res = "checked"; } else { $res = ""; }
    if ($contrib==1) { $con = "checked"; } else { $con = ""; }
    if ($log_user==1) { $log = "checked"; } else { $log = ""; }
   // if ($restrict_user==1) { $restrict = "checked"; } else { $restrict = ""; }
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='modifyPostedQuizz'>";
    print "<INPUT type='hidden' name='qid' value='$qid'>";
    print "<table width='100%'>";
    print "<tr><td>"._QUIZZ_GENINFOS."</td></tr>";
    print "<tr><td>"._QUIZZ_TITLE."</td><td><input type='text' class='text' name='quizztitle' value=\"".$myts->MakeTboxData4Edit($quizzTitle)."\" size=30></td></tr>";
    print "<tr><td>"._QUIZZ_ACTIVE."</td><td><input type='checkbox' name='active' $act> "._QUIZZ_HELPACTIVE."</td></tr>";
    print "<tr><td>"._QUIZZ_VIEWSCORE."</td><td><input type='checkbox' name='displayscore' $dis> "._QUIZZ_HELPVIEWSCORE."</td></tr>";
    print "<tr><td>"._QUIZZ_VIEWANSWER."</td><td><input type='checkbox' name='displayresults' $res> "._QUIZZ_HELPANSWER."</td></tr>";
    print "<tr><td>"._QUIZZ_SENDEMAIL."</td><td><input type='checkbox' name='emailadmin' $eadm> "._QUIZZ_HELPEMAIL."</td></tr>";
    print "<tr><td>"._QUIZZ_CONTRIB."</td><td><input type='checkbox' name='contrib' $con> "._QUIZZ_HELPCONTRIB."</td></tr>";
    //print "<tr><td>"._QUIZZ_ONLYREGISTERED."</td><td><input type='checkbox' name='restrict' $restrict> "._QUIZZ_HELPONLYREGISTERED."</td></tr>";
    print "<tr><td>"._QUIZZ_LIMITVOTE."</td><td><input type='checkbox' name='log' $log> "._QUIZZ_HELPLIMITVOTE."</td></tr>";
    print "<tr><td>"._QUIZZ_NBSCORE."</td><td><input type='text' class='text' name='nbscore' value='".$myts->MakeTboxData4Edit($nbscore)."' size=3> "._QUIZZ_HELPNBSCORE."</td></tr>";
    print "<tr><td>"._QUIZZ_ADMEMAIL."</td><td><input type='text' class='text' name='admemail' size=30 value='".$myts->MakeTboxData4Edit($admemail)."'> (*) "._QUIZZ_HELPADMEMAIL."</td></tr>";
    print "<tr><td>"._QUIZZ_ADMINISTRATOR."</td><td><input type='text' class='text' name='administrator' size=30 value='".$myts->MakeTboxData4Edit($administrator)."'> (*) "._QUIZZ_HELPADMINISTRATOR."</td></tr>";
    print "<tr><td>"._QUIZZ_IMAGE."</td><td><input type='text' class='text' name='image' value=\"".$myts->MakeTboxData4Edit($image)."\" size=30> (*) "._QUIZZ_HELPIMAGE."</td></tr>";
    print "<tr><td>"._CATEGORY."</td><td><SELECT class='select' name=\"cid\">";
    $result = $db->query("select cid, name from ".$db->prefix("quizz_categories"));
    while(list($catid, $name) = $db->fetchRow($result))
        {
        if ($catid==$cid) { $sel = "selected "; }
            echo "<option $sel value=\"$catid\">".$myts->MakeTboxData4Edit($name)."</option>\n";
                $sel = "";
        }
    print "</select></td></tr>";
        # expiration date (a lot of code for so few things)
        ereg("([0-9x]{4})-([0-9x]{1,2})-([0-9x]{1,2}) ([0-9x]{1,2}):([0-9x]{1,2})", $expire, $datetime);
        print "<tr><td>"._QUIZZ_EXPIRATION." (*)</td><td>";
    $xday = 1;
    echo " "._QUIZZ_DAY.": <SELECT class=\"select\" name=\"day\">";
        if ($datetime[3] == "xx") { $sel="selected"; } else { $sel=""; }
        echo "<option name=\"day\" $sel>xx</option>";
    while ($xday <= 31) {
        if ($xday == $datetime[3]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        echo "<option name=\"day\" $sel>$xday</option>";
        $xday++;
    }
    echo "</select>";
    $xmonth = 1;
    echo " "._QUIZZ_UMONTH.": <SELECT class=\"select\" name=\"month\">";
        if ($datetime[2] == "xx") { $sel="selected"; } else { $sel=""; }
        echo "<option name=\"month\" $sel>xx</option>";
    while ($xmonth <= 12) {
        if ($xmonth == $datetime[2]) { $sel = "selected";
        } else { $sel = ""; }
        echo "<option name=\"month\" $sel>$xmonth</option>";
        $xmonth++;
    }
    echo "</select>";
    echo " "._QUIZZ_YEAR.": <input type=\"text\" class=\"text\" name=\"year\" value=\"$datetime[1]\" size=\"5\" maxlength=\"4\">";
    echo " "._QUIZZ_HOUR.": <SELECT class=\"select\" name=\"hour\">";
    $xhour = 0;
    $cero = "0";
	if ($datetime[4] == "xx") { $sel="selected"; } else { $sel=""; }
        echo "<option name=\"hour\" $sel>xx</option>";
	while ($xhour <= 23) {
		$dummy = $xhour;
		if ($xhour < 10) { $xhour = "$cero$xhour"; }
		if ($xhour == $datetime[4]) { $sel = "selected";
		} else { $sel = ""; }
		echo "<option name=\"hour\" $sel>$xhour</option>";
		$xhour = $dummy;
		$xhour++;
	}
    echo "</select>";
    echo ": <SELECT class=\"select\" name=\"min\">";
    $xmin = 0;
        if ($datetime[5] == "xx") { $sel="selected"; } else { $sel=""; }
        echo "<option name=\"min\" $sel>xx</option>";
    while ($xmin <= 59) {
        if (($xmin == 0) OR ($xmin == 5)) {
            $xmin = "0$xmin";
        }
        if ($xmin == $datetime[5]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        echo "<option name=\"min\" $sel>$xmin</option>";
        $xmin = $xmin + 5;
    }
    echo "</select>";
        $now = formatTimestamp(time());
    print "</select><br> ("._QUIZZ_NOWIS." : $now)</td></tr>";
    print "<tr><td colspan=2>"._QUIZZ_COMMENT." (*)<br><TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"comment\">".$myts->MakeTareaData4Edit($comment)."</textarea></td></tr>";
    print "<tr><td colspan=2>"._QUIZZ_CONDITIONS." (*)<br><TEXTAREA class='textarea' cols=\"50\" rows=\"4\" name=\"conditions\">".$myts->MakeTareaData4Edit($conditions)."</textarea></td></tr>";
    print "</table>";
    print "<br><center><input type='submit' class='button' value='"._MODIFY."'></center>";
        print "* : "._QUIZZ_HELPOPTION."\n";
    print "</form><BR>";
    print "<table width='100%'>";
    print "<tr><td><B>"._QUIZZ_QUESTIONS."</B></td></tr>";
    print "<tr><td>";
    print "<center><a href='".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAddQuestion&qid=$qid' > [ "._QUIZZ_ADDQUESTION." ]</a></center>";
    print "<BR></td></tr>";
    $result = $db->query("SELECT pollID, pollTitle FROM ".$db->prefix("quizz_desc")." WHERE qid='$qid'");
    while(list($pollID, $question) = $db->fetchRow($result))
    {
        print "<tr><td>";
        print ""._QUIZZ_QUESTION." $pollID : ";
        print "\"".$myts->MakeTboxData4Edit($question)."\" [ <a href='".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModifyQuestion&pid=$pollID&qid=$qid' >"._MODIFY."</a> | ";
        print "<a href='".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzDelQuestion&pid=$pollID&qid=$qid' >"._DELETE."</a> ]</td></tr>";
        }
    print "</table>";
    CloseTable();
xoops_cp_footer();
}
function QuizzAddContrib() {
global $db,$bcoosConfig,$myts,$xoopsModule,$_GET;
	$qid = intval($_GET['qid']);
	$pid = intval($_GET['pid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_ADDCONTRIB."</b><BR><BR>";
    $result = $db->query("select pollTitle, answer, coef, good, bad, comment,image from ".$db->prefix("quizz_descontrib")." where pollID='$pid'");
    list ($pollTitle,$answer,$coef,$good,$bad,$comment,$image) = $db->fetchRow($result);
        $pollTitle = preg_replace("/\"/","&quot;",$pollTitle);
        # check if the answer was ordered
        if (ereg(",",$answer))
        {
                $array_answer = split(',',$answer);
        }
    print "\n<form method='post' action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='createPostedQuizzQuestion'>";
    print "<INPUT type='hidden' name='qid' value=\"$qid\">";
    print "<INPUT type='hidden' name='pid' value=\"$pid\">";
    print "<INPUT type='hidden' name='mode' value='contrib'>";
    print "<table><tr><td>"._QUIZZ_QUESTIONTITLE.": <input type=\"text\" class=\"text\" name=\"question\" size=\"50\" maxlength=\"100\" value=\"".$myts->MakeTboxData4Edit($pollTitle)."\"><br><br><br></td></tr></table>";
    print "<table><tr><td>";
    print "<tr><td>"._QUIZZ_COEF."</td><td><input type='text' class=\"text\" name='coef' value='$coef' size=3></td></tr>";
    print "<tr><td>"._QUIZZ_IMAGE." (*)</td><td colspan=3><input type='text' class='text' name='image' value='".$myts->MakeTboxData4Edit($image)."' size=50></td></tr>";
    $i=1;
    print "<tr><td colspan=2>"._QUIZZ_ANSWERS."</td><td>"._QUIZZ_ANSWER."</td></tr>";
    $result = $db->query("select optionText, voteID from ".$db->prefix("quizz_datacontrib")." WHERE pollID='$pid' ORDER BY voteID");
    while(list($Text,$voteID) = $db->fetchRow($result))
    {
        if ($answer == $i) {$checked = "checked";} else {$checked = "";}
        echo "<tr><td>"._QUIZZ_ANSWER." $i :</td><td><input type=\"text\" class=\"text\" name=\"optionText[$i]\" size=\"50\" maxlength=\"50\" value=\"".$myts->MakeTboxData4Edit($Text)."\"></td>\n";
        echo "<td><input type=\"radio\" name=\"answer\" value=\"$i\" $checked> "/*.$myts->MakeTboxData4Edit($optionText)*/."<br></td>";
                print "<td><SELECT class=\"select\" name=\"optionSort[$i]\">";
                echo "<option name=\"rank\">--</option>";
                for($j = 1; $j <= 12; $j++)
                {
                                if ($array_answer[$i-1] == $j) { $sel = "selected"; } else { $sel = "";  }
                                echo "<option name=\"rank\" $sel>$j</option>";
                }
                echo "</select>";
        $i++;
    }
    echo "</tr></table><br><br>"
    ."<table width='100%' border=0>"
        ."<tr><td colspan=2>"._QUIZZ_COMMENT." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"comment\">".$myts->MakeTareaData4Edit($comment)."</textarea></td></tr>"
        ."<tr><td colspan=2>"._QUIZZ_IFBADANSWER." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"bad\">".$myts->MakeTareaData4Edit($bad)."</textarea></td></tr>"
    ."<tr><td colspan=2>"._QUIZZ_IFGOODANSWER." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"good\">".$myts->MakeTareaData4Edit($good)."</textarea></td></tr>"
    ."<tr><td><input type=\"submit\" class='button' value=\""._QUIZZ_ADD."\"></form></td>"
    ."<td>"
    ."<form method='post' action='".XOOPS_URL."/modules/quizz/admin/index.php'>"
    ."<INPUT type='hidden' name='act' value='deleteContributorQuizzQuestion'>"
    ."<INPUT type='hidden' name='qid' value=\"$qid\">"
    ."<INPUT type='hidden' name='pid' value=\"$pid\">"
    ."<input type=\"submit\" class='button' value=\""._QUIZZ_DELCONTRIB."\">"
    ."</form></td></tr></table>";
    print "* : "._QUIZZ_HELPOPTION."\n";
    CloseTable();
xoops_cp_footer();
}
function QuizzModifyCategory() {
global $bcoosConfig,$db,$myts,$xoopsModule,$_GET;
	$cid = intval($_GET['cid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._MODIFYCATEGORY."</b><BR><BR>";
    $result = $db->query("SELECT name, comment, image from ".$db->prefix("quizz_categories")." where cid='$cid'");
    list ($name, $comment, $image) = $db->fetchRow($result);
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='modifyPostedQuizzCategory'>";
    print "<INPUT type='hidden' name='cid' value='$cid'>";
    print "";
    print "<table width='100%'>";
    print "<tr><td>"._CATEGORY."</td><td><input type='text' class='text' name='CatName' value='".$myts->MakeTboxData4Edit($name)."'size=30></td>";
    print "<tr><td>"._QUIZZ_COMMENT."</td><td><input type='text' class='text' name='CatComment' value='".$myts->MakeTboxData4Edit($comment)."'size=30> (*)</td>";
    print "<tr><td>"._QUIZZ_CATIMAGE."</td><td><input type='text' class='text' name='CatImage' value='".$myts->MakeTboxData4Edit($image)."'size=30> (*)</td>";
    print "<td><input type='submit' class='button' value='"._MODIFY."'></td></tr></table>";
    print "</form>";
    print "* : "._QUIZZ_HELPOPTION."\n";
    CloseTable();
xoops_cp_footer();
}
function QuizzModifyQuestion() {
global $bcoosConfig,$db,$myts,$xoopsModule,$_GET;
	$qid = intval($_GET['qid']);
	$pid = intval($_GET['pid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_MODIFYQUESTION."</b><BR><BR>";
    $result = $db->query("select pollTitle, answer, coef, good, bad, comment, image from ".$db->prefix("quizz_desc")." where pollID='$pid'");
    list ($pollTitle,$answer,$coef,$good,$bad,$comment,$image) = $db->fetchRow($result);
        $pollTitle = preg_replace("/\"/","&quot;",$pollTitle);
        # check if the answer was ordered
        if (ereg(",",$answer))
        {
                $array_answer = split(',',$answer);
        }
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='modifyPostedQuizzQuestion'>";
    print "<INPUT type='hidden' name='qid' value=\"$qid\">";
    print "<INPUT type='hidden' name='pid' value=\"$pid\">";
    print "<table width='100%' border=0><tr><td>"._QUIZZ_QUESTIONTITLE.": <input type=\"text\" class=\"text\" name=\"question\" size=\"50\" maxlength=\"100\" value=\"".$myts->MakeTboxData4Edit($pollTitle)."\"><br><br><br></td></tr></table>";
        print "<table width='100%' border=0><tr><td>";
    print "<tr><td>"._QUIZZ_COEF."</td><td colspan=3><input type='text' class=\"text\" name='coef' value='$coef' size=3></td></tr>";
    print "<tr><td>"._QUIZZ_IMAGE." (*)</td><td colspan=3><input type='text' class=\"text\" name='image' value='".$myts->MakeTboxData4Edit($image)."' size=50></td></tr>";
        $i=1;
        print "<tr><td colspan=2>"._QUIZZ_ANSWERS."</td><td>"._QUIZZ_ANSWER."</td><td>"._QUIZZ_RANK."</td></tr>";
    $result = $db->query("SELECT optionText, voteID FROM ".$db->prefix("quizz_data")." WHERE pollID='$pid' ORDER BY voteID");
    while(list($Text,$voteID) = $db->fetchRow($result))
    {
        if ($answer == $i) {$checked = "checked";} else {$checked = "";}
        echo "<tr><td>"._QUIZZ_ANSWER." $i :</td><td><input type=\"text\" class=\"text\" name=\"optionText[$i]\" size=\"50\" maxlength=\"50\" value=\"".$myts->MakeTboxData4Edit($Text)."\"></td>\n";
        echo "<td><input type=\"radio\" name=\"answer\" value=\"$i\" $checked><br></td>";
                print "<td><SELECT class=\"select\" name=\"optionSort[$i]\">";
                echo "<option name=\"rank\">--</option>";
                for($j = 1; $j <= 12; $j++)
                {
                                if ($array_answer[$i-1] == $j) { $sel = "selected"; } else { $sel = "";  }
                                echo "<option name=\"rank\" $sel>$j</option>";
                }
                echo "</select>";
        $i++;
    }
    echo "</tr></table><br><br>"
    ."<table width='100%' border=0>"
        ."<tr><td>"._QUIZZ_COMMENT." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"comment\">".$myts->MakeTareaData4Edit($comment)."</textarea></td></tr>"
        ."<tr><td>"._QUIZZ_IFBADANSWER." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"bad\">".$myts->MakeTareaData4Edit($bad)."</textarea></td></tr>"
    ."<tr><td>"._QUIZZ_IFGOODANSWER." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"good\">".$myts->MakeTareaData4Edit($good)."</textarea></td></tr></table><br><br>"
    ."<input type=\"submit\" class='button' value=\""._QUIZZ_MODIFYQUESTION."\">"
    ."</form>";
    print "* : "._QUIZZ_HELPOPTION."\n";
    CloseTable();
xoops_cp_footer();
}
function modifyPostedQuizzQuestion() {
global $question, $optionText,$pid,$answer,$coef,$good,$bad,$comment,$optionSort,$image,$myts,$bcoosConfig,$db,$_POST;
	$qid = intval($_POST['qid']);
	$pid = intval($_POST['pid']);
    $question = $myts->MakeTboxData4Save($question);
    $good = $myts->MakeTareaData4Save($good);
    $bad = $myts->MakeTareaData4Save($bad);
    $comment = $myts->MakeTareaData4Save($comment);
        $ordered_answer = implode(",",$optionSort);
        $ordered_answer = preg_replace("/,--|--,|--/","",$ordered_answer);
        # check if sorted answer is needed
        if (!empty($ordered_answer))
        {
                # check if all availaible answers are sorted
                for($i = 1; $i <= sizeof($optionText); $i++)
                {
                        if ((!empty($optionText[$i]) and $optionSort[$i] == "--") or
                           (empty($optionText[$i]) and $optionSort[$i] != "--"))
                        {
                                xoops_cp_header();
                                OpenTable();
                                print "<center>"._QUIZZ_INCORRECTORDER."</center>";
                                CloseTable();
                                xoops_cp_footer();
                                exit;
                        }
                }
                # change the answer the the ordered answer
                $answer = $ordered_answer;
    }
        # update general information about current question ...
    if(!$db->query("UPDATE ".$db->prefix("quizz_desc")." SET pollTitle='$question', answer='$answer', coef='$coef', good='$good', bad='$bad', comment='$comment', image='$image' WHERE pollID='$pid'"))
        {
        echo $db->error(). ": ".$db->error(). "<br>";
        return;
    }
    for($i = 1; $i <= sizeof($optionText); $i++)
        {
        if($optionText[$i] != "")
        {
            $optionText[$i] = $myts->MakeTboxData4Save($optionText[$i]);
        }
        if(!$db->query("UPDATE ".$db->prefix("quizz_data")." SET optionText='$optionText[$i]' WHERE voteID='$i' AND pollID='$pid'"))
        {
            echo $db->error(). ": ".$db->error(). "<br>";
            return;
        }
    }
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModify&qid=$qid");
}
function QuizzAddQuestion() {
    global $bcoosConfig,$xoopsModule,$_GET;
    $qid = intval($_GET['qid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_ADDQUESTION."</b><BR><BR>";
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='createPostedQuizzQuestion'>";
    print "<INPUT type='hidden' name='qid' value='".$qid."'>";
    print "<table width='100%' border=0><tr><td>"._QUIZZ_QUESTIONTITLE.": <input type=\"text\" class=\"text\" name=\"question\" size=\"50\" maxlength=\"100\" value=\"???\"><br><br><br></td></tr></table><table width='100%' border=0><tr><td>";
    print "<tr><td>"._QUIZZ_COEF."</td><td colspan=3><input type='text' class='text' name='coef' value='1' size=3></td></tr>";
    print "<tr><td>"._QUIZZ_IMAGE." (*)</td><td colspan=3><input type='text' class='text' name='image' size=50></td></tr>";
        print "<tr><td colspan=2>"._QUIZZ_ANSWERS."</td><td>"._QUIZZ_ANSWER."</td><td>"._QUIZZ_RANK."</td></tr>";
    for($i = 1; $i <= 12; $i++)
    {
        if ($i == 1) { $checked = "checked"; } else { $checked = "";}
        print "<tr><td>"._QUIZZ_ANSWER." $i:</td>";
                echo "<td><input type=\"text\" class=\"text\" name=\"optionText[$i]\" size=\"50\" maxlength=\"50\"></td>";
        echo "<td><input type=\"radio\" name=\"answer\" value=\"$i\" $checked> <br></td>";
                print "<td><SELECT class=\"select\" name=\"optionSort[$i]\">";
                echo "<option name=\"rank\">--</option>";
                for($j = 1; $j <= 12; $j++)
                {
                                echo "<option name=\"rank\" $sel>$j</option>";
                }
                echo "</select>";
    }
    echo "</table><br><br>"
    ."<table width='100%' border=0>"
        ."<tr><td>"._QUIZZ_COMMENT." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"6\" name=\"comment\"></textarea></td></tr>"
        ."<tr><td>"._QUIZZ_IFBADANSWER." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"6\" name=\"bad\"></textarea></td></tr>"
    ."<tr><td>"._QUIZZ_IFGOODANSWER." (*)<br>"
    ."<TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"6\" name=\"good\"></textarea></td></tr>"
    ."</table><br><br><input type=\"submit\" class='button' value=\""._QUIZZ_ADDQUESTION."\">"
    ."</form>";
    print "* : "._QUIZZ_HELPOPTION."\n";
    CloseTable();
xoops_cp_footer();
}
function QuizzAdd() {
    global $bcoosConfig,$db,$myts,$xoopsModule;
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_NEW."</b><BR><BR>";
    print "\n<form method='post'action='".XOOPS_URL."/modules/quizz/admin/index.php'>";
    print "<INPUT type='hidden' name='act' value='createPostedQuizz'>";
    print "";
    print "<table width='100%'>";
    print "<tr><td>"._QUIZZ_TITLE."</td><td><input type='text' class='text' name='quizztitle' size=30></td></tr>";
    print "<tr><td>"._QUIZZ_VIEWANSWER."</td><td><input type='checkbox' name='displayresults' checked> "._QUIZZ_HELPANSWER."</td></tr>";
    print "<tr><td>"._QUIZZ_VIEWSCORE."</td><td><input type='checkbox' name='displayscore' checked> "._QUIZZ_HELPVIEWSCORE."</td></tr>";
    print "<tr><td>"._QUIZZ_SENDEMAIL."</td><td><input type='checkbox' name='emailadmin' checked> "._QUIZZ_HELPEMAIL."</td></tr>";
    print "<tr><td>"._QUIZZ_ONLYREGISTERED."</td><td><input type='checkbox' name='restrict' checked> "._QUIZZ_HELPONLYREGISTERED."</td></tr>";
    print "<tr><td>"._QUIZZ_LIMITVOTE."</td><td><input type='checkbox' name='log' checked> "._QUIZZ_HELPLIMITVOTE."</td></tr>";
    print "<tr><td>"._QUIZZ_CONTRIB."</td><td><input type='checkbox' name='contrib' checked> "._QUIZZ_HELPCONTRIB."</td></tr>";
    print "<tr><td>"._QUIZZ_NBSCORE."</td><td><input type='text' class='text' name='nbscore' size=3 value=10> "._QUIZZ_HELPNBSCORE."</td></tr>";
    print "<tr><td>"._QUIZZ_ADMEMAIL."</td><td><input type='text' class='text' name='admemail' size=30> (*) "._QUIZZ_HELPADMEMAIL."</td></tr>";
    print "<tr><td>"._QUIZZ_ADMINISTRATOR."</td><td><input type='text' class='text' name='administrator' size=30> (*) "._QUIZZ_HELPADMINISTRATOR."</td></tr>";
    print "<tr><td>"._QUIZZ_IMAGE."</td><td><input type='text' class='text' name='image' size=30> (*) "._QUIZZ_HELPIMAGE."</td></tr>";
        print "<tr><td>"._CATEGORY."</td><td><SELECT class='select' name='cid' >";
    $result = $db->query("SELECT cid, name FROM ".$db->prefix("quizz_categories"));
    while(list($catid, $name) = $db->fetchRow($result))
    {
        if ($catid==$cid) { $sel = "selected "; }
            echo "<option $sel value=\"$catid\">".$myts->MakeTboxData4PreviewInForm($name)."</option>\n";
                $sel = "";
    }
    print "</select></td></tr>";
        print "<tr><td>"._QUIZZ_EXPIRATION." (*)</td><td>";
    $xday = 1;
    echo " "._QUIZZ_DAY.": <SELECT class=\"select\" name=\"day\">";
        echo "<option name=\"day\" selected>xx</option>"; # by default print the "no expiration date" tag
    while ($xday <= 31)
        {
        echo "<option name=\"day\">$xday</option>";
        $xday++;
    }
    echo "</select>";
    $xmonth = 1;
    echo " "._QUIZZ_UMONTH.": <SELECT class=\"select\" name=\"month\">";
        echo "<option name=\"month\" selected>xx</option>"; # by default print the "no expiration date" tag
    while ($xmonth <= 12)
        {
        echo "<option name=\"month\">$xmonth</option>";
        $xmonth++;
    }
    echo "</select>";
    echo " "._QUIZZ_YEAR.": <input type=\"text\" class=\"text\" name=\"year\" value=\"xxxx\" size=\"5\" maxlength=\"4\">";
    echo " "._QUIZZ_HOUR.": <SELECT class=\"select\" name=\"hour\">";
    $xhour = 0;
    $cero = "0";
        echo "<option name=\"hour\" selected>xx</option>"; # by default print the "no expiration date" tag
    while ($xhour <= 23)
        {
        $dummy = $xhour;
        if ($xhour < 10) { $xhour = "$cero$xhour"; }
        echo "<option name=\"hour\">$xhour</option>";
        $xhour = $dummy;
        $xhour++;
    }
    echo "</select>";
    echo ": <SELECT class=\"select\" name=\"min\">";
    $xmin = 0;
        echo "<option name=\"min\" selected>xx</option>"; # by default print the "no expiration date" tag
    while ($xmin <= 59)
        {
        if (($xmin == 0) OR ($xmin == 5)) { $xmin = "0$xmin"; }
        echo "<option name=\"min\">$xmin</option>";
        $xmin = $xmin + 5;
    }
    echo "</select>";
        $now = FormatTimestamp(time());
    print "</select><br> ("._QUIZZ_NOWIS." : $now)</td></tr>";
    print "<tr><td colspan=2>"._QUIZZ_COMMENT." (*)<br><TEXTAREA class='textarea' wrap=\"virtual\" cols=\"50\" rows=\"4\" name=\"comment\"></textarea></td></tr>";
    print "<tr><td colspan=2>"._QUIZZ_CONDITIONS." (*)<br><TEXTAREA class='textarea' cols=\"50\" rows=\"4\" name=\"conditions\"></textarea></td></tr>";
    print "</table>";
    print "<br><center><input type='submit' class='button' value='"._QUIZZ_CREATE."'></center>";
    print "</form>";
    print "* : "._QUIZZ_HELPOPTION."\n";
    CloseTable();
xoops_cp_footer();
}
function modifyPostedQuizz() {
global $quizztitle, $displayresults,$displayscore,$emailadmin,$nbscore,$comment,$image,$log,$restrict,$active,$cid,$contrib,$year,$month,$day,$hour,$min,$admemail,$administrator,$conditions,$myts,$bcoosConfig,$db,$_POST;
	$qid = intval($_POST['qid']);
    $quizztitle = $myts->MakeTboxData4Save($quizztitle);
    $comment = $myts->MakeTareaData4Save($comment);
    $conditions = $myts->MakeTareaData4Save($conditions);
    $expire = "$year-$month-$day $hour:$min";
	if(isset($emailadmin)) $emailadmin=1; else $emailadmin=0;
        if(isset($displayresults)) $displayresults=1; else $displayresults=0;
        if(isset($displayscore)) $displayscore=1; else $displayscore=0;
        if(isset($restrict)) $restrict=1; else $restrict=0;
        if(isset($log)) $log=1; else $log=0;
        if(isset($active)) $active=1; else $active=0;
        if(isset($contrib)) $contrib=1; else $contrib=0;
	
	$sql = "UPDATE ".$db->prefix("quizz_admin")." SET 
	quizzTitle='$quizztitle', 
	nbscore='$nbscore', 
	displayscore='$displayscore', 
	displayresults='$displayresults', 
	emailadmin='$emailadmin', 
	comment='$comment', 
	image='$image', 
	restrict_user='$restrict', 
	log_user='$log', 
	active='$active', 
	cid='$cid', 
	contrib='$contrib', 
	expire='$expire', 
	admemail='$admemail', 
	administrator='$administrator', 
	conditions='$conditions' 
	WHERE quizzID='$qid'";
	echo $sql;
    if(!$db->query($sql)) {
        echo $db->error(). ": ".$db->error(). "<br>";
        return;
    }
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function createPostedQuizz() {
global $quizztitle, $displayresults, $displayscore,$emailadmin,$nbscore,$comment,$image,$restrict,$log,$contrib,$year,$month,$day,$hour,$min,$prefix,$base_url,$admemail,$administrator,$conditions,$myts,$bcoosConfig,$db,$_POST;
	$cid = intval($_POST['cid']);
    if(isset($emailadmin)) $emailadmin=1; else $emailadmin=0;
    if(isset($displayresults)) $displayresults=1; else $displayresults=0;
    if(isset($displayscore)) $displayscore=1; else $displayscore=0;
    if(isset($restrict)) $restrict=1; else $restrict=0;
    if(isset($log)) $log=1; else $log=0;
    if(isset($contrib)) $contrib=1; else $contrib=0;
    $expire = "$year-$month-$day $hour:$min";
    $timeStamp = time();
    $quizztitle = $myts->MakeTboxData4Save($quizztitle);
    $comment = $myts->MakeTareaData4Save($comment);
    $conditions = $myts->MakeTareaData4Save($conditions);
    if(!$db->query("INSERT INTO ".$db->prefix("quizz_admin")." VALUES (NULL, '$quizztitle', '$timeStamp', 0, '$nbscore','$displayscore','$displayresults','$emailadmin','$comment',0,'$restrict','$log','$image','$cid','$contrib','$expire','$admemail','$administrator','$conditions')")) {
        echo $db->error(). ": ".$db->error(). "<br>";
        return;
    }
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function createPostedQuizzQuestion() {
global $question, $optionText,$answer,$coef,$good,$bad,$mode,$pid,$comment,$prefix,$base_url,$optionSort,$image,$myts,$bcoosConfig,$db,$_POST;
    $qid = intval($_POST['qid']);
    $pid = intval($_POST['pid']);
    $good = $myts->MakeTareaData4Save($good);
    $bad = $myts->MakeTareaData4Save($bad);
    $comment = $myts->MakeTareaData4Save($comment);
    $question = $myts->MakeTboxData4Save($question);
    $timeStamp = time();
        $ordered_answer = implode(",",$optionSort);
        $ordered_answer = preg_replace("/,--|--,|--/","",$ordered_answer);
        # check if sorted answer is needed
        if (!empty($ordered_answer))
        {
                # check if all availaible answers are sorted
                for($i = 1; $i <= sizeof($optionText); $i++)
                {
                        if ((!empty($optionText[$i]) and $optionSort[$i] == "--") or
                           (empty($optionText[$i]) and $optionSort[$i] != "--"))
                        {
                                include (XOOPS_ROOT_PATH."/header.php");
                                OpenTable();
                                print "<center>"._QUIZZ_INCORRECTORDER."</center>";
                                CloseTable();
                                xoops_cp_footer();
                                exit;
                        }
                }
                # change the answer the the ordered answer
                $answer = $ordered_answer;
    }
    if(!$db->query("INSERT INTO ".$db->prefix("quizz_desc")." VALUES (NULL, '$question', '$timeStamp', 0, '$qid','$answer','$coef','$good','$bad','$comment','$image')"))
        {
        echo $db->error(). ": ".$db->error(). "<br>";
        return;
    }
    $array = $db->fetchArray($db->query("SELECT pollID FROM ".$db->prefix("quizz_desc")." WHERE timeStamp='$timeStamp'"));
    $id = $array['pollID'];
    for($i = 1; $i <= sizeof($optionText); $i++)
        {
        if($optionText[$i] != "")
        {
            $optionText[$i] = $myts->MakeTboxData4Save($optionText[$i]);
        }
        if(!$db->query("INSERT INTO ".$db->prefix("quizz_data")." (pollID, optionText, optionCount, voteID) VALUES ($id, '$optionText[$i]', 0, $i)"))
        {
            echo $db->error(). ": ".$db->error(). "<br>";
            return;
        }
    }
    # delete contributor question if needed
    if ($mode == "contrib")
    {
         $db->query("DELETE FROM ".$db->prefix("quizz_datacontrib")." WHERE pollID='$pid'");
         $db->query("DELETE FROM ".$db->prefix("quizz_descontrib")." WHERE pollID='$pid'");
    }
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?&act=QuizzModify&qid=$qid");
}
function deletePostedScoreQuizz() {
global $db,$bcoosConfig,$_POST;
	$qid = intval($_POST['qid']);
    # delete all the score and stats for the selected Quiz
    $db->query("UPDATE ".$db->prefix("quizz_admin")." SET voters='0' WHERE quizzID='$qid'");
    $result = $db->query("SELECT pollID FROM ".$db->prefix("quizz_desc")." WHERE qid='$qid'");
    while(list($pollID) = $db->fetchRow($result))
    {
        $db->query("UPDATE ".$db->prefix("quizz_data")." SET optionCount='0' WHERE pollID='$pollID'");
        }
    $db->query("DELETE FROM ".$db->prefix("quizz_check")." WHERE qid='$qid'");
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function deletePostedContributorQuizzQuestion() {
global $bcoosConfig,$db,$_POST;
	$pid = intval($_POST['pid']);
  # delete contributor question
         $db->query("DELETE FROM ".$db->prefix("quizz_datacontrib")." WHERE pollID='$pid'");
         $db->query("DELETE FROM ".$db->prefix("quizz_descontrib")." WHERE pollID='$pid'");
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?&act=QuizzAdmin");
}
function QuizzDelCategory() {
global $bcoosConfig,$db,$xoopsModule,$_GET;
	$cid = intval($_GET['cid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_DELCAT."</b><br><br><center>"
        .""._QUIZZ_SURE2DELETECAT." $cid & "._QUIZZ_ALLCONTENTS."<br><br>"
        ."[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=removePostedQuizzCategory&cid=$cid\">"._YES."</a> | <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\">"._NO."</a> ]</center>";
    CloseTable();
xoops_cp_footer();
}
function createPostedQuizzCategory() {
global $CatName,$CatComment,$CatImage,$bcoosConfig,$db,$myts;
	$CatName=$myts->MakeTboxData4Save($CatName);
        $CatComment=$myts->MakeTboxData4Save($CatComment);
    if(!$db->query("INSERT INTO ".$db->prefix("quizz_categories")." VALUES (NULL, '$CatName','$CatComment','$CatImage')"))
        {
                echo $db->error(). ": ".$db->error(). "<br>";
                return;
    }
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function modifyPostedQuizzCategory() {
 global $CatName,$CatComment,$CatImage,$bcoosConfig,$db,$myts,$_POST;
	$cid = intval($_POST['cid']);
        $CatName=$myts->MakeTboxData4Save($CatName);
        $CatComment=$myts->MakeTboxData4Save($CatComment);
    if(!$db->query("UPDATE ".$db->prefix("quizz_categories")." SET name='$CatName',comment='$CatComment',image='$CatImage' WHERE cid='$cid'"))
        {
                echo $db->error(). ": ".$db->error(). "<br>";
                return;
    }
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function deleteContributorQuizzQuestion() {
global $pid,$bcoosConfig,$xoopsModule,$_GET;
	$qid = intval($_GET['qid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_DELCONTRIB." $pid?</b><br><br><center>"
        ."[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=deletePostedContributorQuizzQuestion&qid=$qid&pid=$pid\">"._YES."</a> | <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\">"._NO."</a> ]</center>";
    CloseTable();
xoops_cp_footer();
}
function QuizzDelQuestion() {
global $pid,$bcoosConfig,$xoopsModule,$_GET;
	$qid = intval($_GET['qid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._QUIZZ_DELQUESTION."</b><br><br><center>"
        .""._QUIZZ_SURE2DELQUESTION." $pid?<br><br>"
        ."[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=removePostedQuizzQuestion&qid=$qid&pid=$pid\">"._YES."</a> | <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModify&qid=$qid\">"._NO."</a> ]</center>";
    CloseTable();
xoops_cp_footer();
}
function QuizzRemove() {
global $bcoosConfig,$xoopsModule,$_GET;
	$qid = intval($_GET['qid']);
xoops_cp_header();
    OpenTable();
    echo "<b>"._DELETE."</b><br><br><center>"
        .""._QUIZZ_SURE2DELETE." $qid?<br><br>"
        ."[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=removePostedQuizz&qid=$qid\">"._YES."</a> | <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\">"._NO."</a> ]</center>";
    CloseTable();
xoops_cp_footer();
}
function removePostedQuizzCategory() {
global $db,$bcoosConfig,$_GET;
	$cid = intval($_GET['cid']);
    $db->query("DELETE FROM ".$db->prefix("quizz_categories")." WHERE cid='$cid'");
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function removePostedQuizz() {
global $db,$_GET;
	$qid = intval($_GET['qid']);
    $result = $db->query("SELECT pollID FROM ".$db->prefix("quizz_desc")." WHERE qid='$qid'");
    while(list($pollID) = $db->fetchRow($result))
    {
        $db->query("DELETE FROM ".$db->prefix("quizz_desc")." WHERE pollID='$pollID'");
        $db->query("DELETE FROM ".$db->prefix("quizz_data")." WHERE pollID='$pollID'");
        }
    $db->query("DELETE FROM ".$db->prefix("quizz_admin")." WHERE quizzID='$qid'");
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin");
}
function removePostedQuizzQuestion() {
global $db,$_GET;
	$qid = intval($_GET['qid']);
	$pid = intval($_GET['pid']);
    $db->query("DELETE FROM ".$db->prefix("quizz_desc")." WHERE pollID='$pid'");
    $db->query("DELETE FROM ".$db->prefix("quizz_data")." WHERE pollID='$pid'");
    Header("Location: ".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModify&qid=$qid");
}
if (!isset($act))
{
    QuizzAdmin();
}
else
{
switch($act) {
    case "createPostedQuizzQuestion":
    createPostedQuizzQuestion();
    break;
    case "modifyPostedQuizz":
    modifyPostedQuizz();
    break;
    case "modifyPostedQuizzCategory":
    modifyPostedQuizzCategory();
    break;
    case "modifyPostedQuizzQuestion":
    modifyPostedQuizzQuestion();
    break;
    case "deletePostedScoreQuizz":
    deletePostedScoreQuizz();
    break;
    case "removePostedQuizzCategory":
    removePostedQuizzCategory();
    break;
    case "removePostedQuizz":
    removePostedQuizz();
    break;
    case "removePostedQuizzQuestion":
    removePostedQuizzQuestion();
    break;
    case "QuizzAddQuestion":
    QuizzAddQuestion();
    break;
    case "QuizzDelQuestion":
    QuizzDelQuestion();
    break;
    case "QuizzViewStats":
    QuizzViewStats();
    break;
    case "QuizzRemoveScore":
    QuizzRemoveScore();
    break;
    case "QuizzViewScore":
    QuizzViewScore();
    break;
    case "QuizzRemove":
    QuizzRemove();
    break;
    case "createPostedQuizz":
    createPostedQuizz();
    break;
    case "QuizzModifyCategory":
    QuizzModifyCategory();
    break;
    case "QuizzModifyQuestion":
    QuizzModifyQuestion();
    break;
    case "QuizzAddContrib":
    QuizzAddContrib();
    break;
    case "QuizzModify":
    QuizzModify();
    break;
    case "QuizzDelCategory":
    QuizzDelCategory();
    break;
    case "createPostedQuizzCategory":
    createPostedQuizzCategory();
    break;
    case "deletePostedContributorQuizzQuestion":
    deletePostedContributorQuizzQuestion();
    break;
    case "deleteContributorQuizzQuestion":
    deleteContributorQuizzQuestion();
    break;
    case "QuizzAdd":
    QuizzAdd();
    break;
    case "QuizzAdmin":
    QuizzAdmin();
    break;
    default:
    QuizzAdmin();
    break;
}
}
?>