<?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...
#####################################################
include("header.php");
function addContributeQuizzQuestion ($qid) {
global $db,$myts, $bcoosConfig,$question,$optionText,$qid,$answer,$coef,$good,$bad,$comment,$optionSort,$image;
        $good = $myts->oopsNl2Br($myts->makeTareaData4Save($good));
        $bad = $myts->oopsNl2Br($myts->makeTareaData4Save($bad));
        $comment = $myts->oopsNl2Br($myts->makeTareaData4Save($comment));
        $question = $myts->oopsNl2Br($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] != "--"))
                        {
				$bcoosConfig['title'] .= " - "._MI_QUIZZ_NAME;
                                include(XOOPS_ROOT_PATH."/header.php");
                                OpenTable();
                                print "<center>"._QUIZZ_INCORRECTORDER."</center>";
                                CloseTable();
                                include (XOOPS_ROOT_PATH."/footer.php");
                                exit;
                        }
                }
                # change the answer the the ordered answer
                $answer = $ordered_answer;
    }
    if(!$db->query("INSERT INTO ".$db->prefix("quizz_descontrib")." VALUES (NULL, '$question', '$timeStamp', 0, '$qid','$answer','$coef','$good','$bad','$comment','')"))
        {
        echo $db->error(). ": ".$db->error(). "<br>";
        return;
    }
    $array = $db->fetchArray($db->query("SELECT pollID,timeStamp  FROM ".$db->prefix("quizz_descontrib")." WHERE timeStamp='$timeStamp'"));
    $id = $array['pollID'];
    $t = $array['timeStamp'];
    for($i = 1; $i <= sizeof($optionText); $i++)
        {
        if($optionText[$i] != "")
        {
            $optionText[$i] = $myts->MakeTboxData4Save($optionText[$i]);
        }
        if(!$db->query("INSERT INTO ".$db->prefix("quizz_datacontrib")." (pollID, optionText, optionCount, voteID) VALUES ($id, '$optionText[$i]', 0, $i)"))
        {
            echo $db->error(). ": ".$db->error(). "<br>";
            return;
        }
    }
        # update the image name
        if (!empty($image) and $image != "none")
        {
                   if (!copy($image, XOOPS_ROOT_PATH."/modules/quizz/images/${t}.gif"))
                {
                        Opentable();
                        print "Error ! Can't copy image $image <br>";
                        CloseTable();
                }
            if(!$db->query("UPDATE ".$db->prefix("quizz_descontrib")." SET image='${t}.gif' WHERE pollID='$id'"))
        {
        echo $db->error(). ": ".$db->error(). "<br>";
        return;
            }
        }
    OpenTable();
    print ""._QUIZZ_CONTRIBTHANKS."<br /><a href=\"".XOOPS_URL."/modules/quizz/index.php\">"._QUIZZ_BACKTOINDEX."</a>\n";
    CloseTable();
}
function QuizzContribute ($qid) {
global $xoopsUser,$bcoosConfig,$qid;
    OpenTable();
    echo "<center><b>"._QUIZZ_ADDQUESTION."</b></center>";
    CloseTable();
    echo "<br>";
    OpenTable();
    # get the login infos  for the registered user
    if (!$xoopsUser)
    {
        OpenTable();
        echo "<center><b>"._QUIZZ_ADDQUESTIONSORRY."</b></center>";
        CloseTable();
        ########################### return;
    }
    # the current user
    $logname = $xoopsUser->getVar("uname", "E");
    print "<form enctype=\"multipart/form-data\" action='".$bcoosConfig["xoops_url"]."/modules/quizz/index.php' method='post'>";
    print "<input type='hidden' name='do' value='addContributeQuizzQuestion'>";
    print "<input type='hidden' name='qid' value=\"$qid\">";
    print "<table width='100%' border=0><tr><td>"._QUIZZ_QUESTIONTITLE.": <input type=\"text\" name=\"question\" size=\"50\" maxlength=\"100\" value=\"???\"><br><br><br></td></tr></table><table width='100%' border=1><tr><td>";
    print "<tr><td>"._QUIZZ_COEF."</td><td colspan=3><input type='text' name='coef' value='1' size=3></td></tr>";
    print "<tr><td>"._QUIZZ_IMAGE." (*)</td><td colspan=3><input type='file' name='image'></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\" name=\"optionText[$i]\" size=\"50\" maxlength=\"50\"></td>";
        $optionTex = isset($optionText) ? $optionText : "";
        echo "<td><input type=\"radio\" name=\"answer\" value=\"$i\" $checked> $optionTex<br></td>";
                print "<td><SELECT name=\"optionSort[$i]\">";
                echo "<option name=\"rank\" $sel>--</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 cols=\"50\" rows=\"4\" name=\"comment\"></textarea></td></tr>"
        ."<tr><td>"._QUIZZ_IFBADANSWER." (*)<br>"
    ."<TEXTAREA cols=\"50\" rows=\"4\" name=\"bad\"></textarea></td></tr>"
    ."<tr><td>"._QUIZZ_IFGOODANSWER." (*)<br>"
    ."<TEXTAREA cols=\"50\" rows=\"4\" name=\"good\"></textarea></td></tr>"
    ."</table><br><br><input type=\"submit\" class=button value=\""._QUIZZ_ADDQUESTION."\">"
    ."</form>";
    print "* : "._QUIZZ_HELPOPTION."\n";
    CloseTable();
}
function QuizzShow ($qid) {
global $xoopsUser,$db,$myts,$bcoosConfig;
        # check for  authorization
    $result = $db->query("SELECT quizzTitle, comment , active, restrict_user, log_user, image, contrib, expire , displayscore,nbscore, conditions  from ".$db->prefix("quizz_admin")." where quizzID='$qid'");
    list($quizzTitle,$comment,$active, $restrict_user, $log_user,$image,$contrib,$expire,$displayscore,$nbscore,$conditions) = $db->fetchRow($result);
        # check if quizz is active
        if ($active == 0) {
                if (!$xoopsUser) {
                        OpenTable();
                        print "<center>"._QUIZZ_MUSTBEACTIVE."</center>";
                        CloseTable();
                        return;
                } else {
                        $xoopsModule = XoopsModule::getByDirname("quizz");
                        if(!$xoopsUser->isAdmin($xoopsModule->mid())) {
                                OpenTable();
                                print "<center>"._QUIZZ_MUSTBEACTIVE."</center>";
                                CloseTable();
                                return;
                        }
                }
        }
        # first check if it exists an expiration date (no xx)
        if (!ereg("xx",$expire)) {
		$Cann = strftime("%Y",strtotime($expire));
		$Cmoi = strftime("%m",strtotime($expire));
		$Cjour = strftime("%d",strtotime($expire));
		$Cheur = strftime("%H",strtotime($expire));
		$Cmin = strftime("%M",strtotime($expire));
		$today = getdate();
		$day = $today[mday];
		if ($day < 10) { $day = "$day"; }
		$month = $today[mon];
		$year = $today[year];
		$hour = $today[hours];
		$min = $today[minutes];
		$sec = $today[seconds];
                # check for the day
		if (($date[1] <= $year) AND ($Cmoi <= $month) AND ($Cjour <= $day) AND ($Cheur <= $hour) AND ($Cmin <= $min)) {
                        $expired = 1;
			OpenTable();
			echo "<DIV ALIGN=\"center\">"._QUIZZ_HASEXPIRED."</DIV><br>";
			CloseTable();
                        return;
                }
		# show the score if needed
                if ($displayscore == 1 AND $expired == 1) {
			OpenTable();
			echo ""._QUIZZ_LISTSCORE."<br>";
			$result = $db->query("SELECT username, score FROM ".$db->query("quizz_check")." WHERE qid='$qid' ORDER BY score DESC,time DESC LIMIT $nbscore ");
			while(list($username,$res) = $db->fetchRow($result)) {
				print "$username : $res<br>";
			}
			CloseTable();
			# bye bye !! :)
			OpenTable();
			echo ""._QUIZZ_THANKS."";
			CloseTable();
                }
                if ($expired == 1) {
                return;
                }
        }
        # get the login infos  for the registered user
    if ($xoopsUser) {
                    $logname = $xoopsUser->getVar("uname", "E");
                        $adrs = $xoopsUser->getVar("email", "E");
                    }
        # check if the user is logged (only one vote for the quizz)
        if ($log_user == 1) {
                # search in the database for a previous vote
                $result = $db->query("SELECT username FROM ".$db->prefix("quizz_check")." WHERE  (qid='$qid' AND username='$logname') OR (qid='$qid' AND email='$adrs')");
                if($db->getRowsNum($result) > 0) {
                        print "<center>"._QUIZZ_ALREADYVOTED."</center>";
                        return;
                }
        }
        if ($xoopsUser) {
                $xoopsModule = XoopsModule::getByDirname("quizz");
                if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
                        $menu = "[ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzViewScore&qid=$qid\">"._QUIZZ_VIEWSCORE."</a> | ";
                            $menu .= "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzRemoveScore&qid=$qid\">"._QUIZZ_DELSCORE."</a> | ";
                            $menu .= "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzViewStats&qid=$qid\">"._QUIZZ_VIEWSTAT."</a> | ";
                               $menu .= "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModify&qid=$qid\">"._MODIFY."</a> | ";
                               $menu .= "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzRemove&qid=$qid\">"._DELETE."</a> ]";
                }
        }
        if (!empty($image))
        {
            echo "<table width='100%' border=0><tr><td><b>$quizzTitle</b> $menu</td><td valign=middle rowspan=2><center><img src='".XOOPS_URL."/modules/quizz/images/$image' border=0 alt=''></center></td></tr>";
                print "<tr><td>".$myts->MakeTareaData4Show($comment)."</td></tr></table>";
        }
        else
        {
            $men=isset($menu) ? $menu : "";
            echo "<table width='100%' border=0><tr><td><b>$quizzTitle</b> $men</td></tr>";
                print "<tr><td>".$myts->MakeTareaData4Show($comment)."</td></tr></table>";
        }
            if (!empty($conditions)) {
            OpenTable();
                print "<table width='100%' border=0><tr><td>".sprintf(_QUIZZ_READCONDITIONS, $bcoosConfig['xoops_url'], $qid)."</td></tr></table>";
            CloseTable();
                        }
        # print the quizz form
    print "<form action=\"".XOOPS_URL."/modules/quizz/index.php\" method=\"post\">";
    print "<input type=\"hidden\" name=\"qid\" value=\"$qid\">";
    print "<input type=\"hidden\" name=\"do\" value=\"vote\">";
        if ($xoopsUser)
        {
            print "<input type=\"hidden\" name=\"logname\" value=\"$logname\">";
            print "<input type=\"hidden\" name=\"adrs\" value=\"$adrs\">";
        }
        else
        {
            OpenTable();
                print "<table width='100%' border=0><tr><td>"._QUIZZ_LOGNAME." : <input type='text' name='logname'></td>";
                print "<td>"._EMAIL." : <input type='text' name='adrs'></td></tr></table>";
            CloseTable();
                        echo "<P>";
        }
        $num = 1;
    $result = $db->query("SELECT pollID, pollTitle, voters, comment, answer, image FROM ".$db->prefix("quizz_desc")." WHERE qid='$qid'");
    while(list($pollID, $pollTitle, $voters, $comment,$answer,$image) = $db->fetchRow($result))
    {
                echo "<table width='100%' border=0 class='bg2'><tr><td>";
                if (!empty($image))
                {
        echo "<td><img src='".XOOPS_URL."/modules/quizz/images/$image' border=0 align='absmiddle'></td>"
                ."<td><b>"._QUIZZ_QUESTIONTITLE." $num : ".$myts->MakeTboxData4Show($pollTitle)."</b></td>"
                ."</tr></table>";
                }
                else
                {
        echo "<b>"._QUIZZ_QUESTIONTITLE." $num : ".$myts->MakeTboxData4Show($pollTitle)."</b> ";
                }
                if ($xoopsUser)
                {
                        $xoopsModule = XoopsModule::getByDirname("quizz");
                        if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
                                       print "[ <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> ]";
                               }
                       }
                echo "</td></tr></table>";
                print "<table border=0 width='100%' class='bg3'>";
                if (!empty($comment))
                {
            echo "<tr><td>&nbsp;&nbsp;<i>".$myts->MakeTareaData4Show($comment)."</i></td></tr>";
                }
        print "<input type=\"hidden\" name=\"pollID\" value=\"$pollID\">";
                # ordered answer needed ?
            if (ereg(",",$answer))
            {
            $result1 = $db->query("SELECT optionText, voteID FROM ".$db->prefix("quizz_data")." WHERE pollID='$pollID' ORDER BY voteID");
                        # compute the list of answer for the combo box
            while(list($optionText,$voteID) = $db->fetchRow($result1))
                        {
                                $array_text[$voteID] = $optionText;
                        }
                        for($i = 1; $i <= sizeof($array_text); $i++)
                        {
                                if (!empty($array_text[$i]))
                                {
                                    print "<tr><td>&nbsp;&nbsp;<SELECT name=\"voteQuizz[$pollID][$i]\">";
                                        for($j = 1; $j <= sizeof($array_text); $j++)
                                        {
                                        if (!empty($array_text[$j]))
                                        {
                                    echo "<option name=\"rank\" value='$j' $sel>".$myts->MakeTboxData4Edit($array_text[$j])."</option>";
                                        }
                                        }
                            echo "</select> ".$myts->MakeTboxData4Edit($optionText)."</tr>";
                                }
                        }
                }
                else
                {
        $ok=0;
        for($i = 1; $i <= 12; $i++)
        {
            $result1 = $db->query("SELECT optionText FROM ".$db->prefix("quizz_data")." WHERE (pollID='$pollID') AND (voteID='$i')");
            list($optionText) = $db->fetchRow($result1);
            if($optionText != "")
            {
                if ($ok == 0) { $checked = "checked" ; } else { $checked = ""; }
                print "<tr><td><input type=\"radio\" name=\"voteQuizz[$pollID]\" value=\"$i\" $checked> $optionText<br></td></tr>\n";
                $ok++;
                        }
        } # end poll question
                }
        $num++;
                print "</table><BR><BR>";
    } # end poll loop
    print "<center><input type=\"submit\" class=button value='"._QUIZZ_SUBMIT."'></center>";
    print "</form>\n\n";
    # display the possibility for contributor to add question if needed
    if ($contrib == 1)
    {
    print "<center><a href=\"".XOOPS_URL."/modules/quizz/index.php?qid=$qid&do=contrib\"> [ "._QUIZZ_CONTRIBUTE." ]</a></center>\n";
    }
}
function QuizzVote ($qid,$voteQuizz,$logname,$adrs) {
global $bcoosConfig,$myts,$xoopsUser,$db,$optionSort;
        # check if the mandatory infos aren't empty
        #print "logname = $logname email = $adrs<br>";
        if (empty($logname) or empty($adrs)) {
            OpenTable();
                print "<center>"._QUIZZ_MISSINGINFOS."</center>";
            CloseTable();
                return;
        }
    $result = $db->query("select quizzTitle, nbscore, displayscore, displayresults, emailadmin, active, restrict_user, log_user, image, contrib, admemail from ".$db->prefix("quizz_admin")." where quizzID='$qid'");
    list($quizzTitle, $nbscore, $displayscore,$displayresults , $emailadmin, $active, $restrict_user, $log_user, $image, $contrib,$admemail) = $db->fetchRow($result);
        # check if quizz is active
        if ($active == 0) {
            OpenTable();
                print "<center>"._QUIZZ_MUSTBEACTIVE."</center>";
            CloseTable();
                return;
        }
        # check if the user is logged (only one vote for the quizz)
        if ($log_user == 1) {
                # search in the database for a previous vote
            $result = $db->query("SELECT username FROM ".$db->prefix("quizz_check")." WHERE (qid='$qid' AND username='$logname') OR (qid='$qid' AND email='$adrs')");
                list($username) = $db->fetchRow($result);
                # already recorded ?
                if ($username == $logname or $email==$adrs) {
                      print "<center>"._QUIZZ_ALREADYVOTED."</center>";
                   return;
                }
        }
        # display a header if you have choose to show the score
        if ($displayscore == 1)
        {
        $commen = isset($comment) ? $myts->MakeTareaData4Show($comment) : "";
                if (!empty($image)) {
                    echo "<table width='100%' border=0><tr><td><b>"._QUIZZ_RESULT." \"$quizzTitle\"</b></td><td valign=middle rowspan=2><center><img src='".XOOPS_URL."/modules/quizz/images/$image' border=0 alt=''></center></td></tr>";
                        print "<tr><td>$commen</td></tr></table>";
                } else {
                    echo "<table width='100%'><tr><td><b>"._QUIZZ_RESULT." \"$quizzTitle\"</b></td></tr>";
                        print "<tr><td>$commen</td></tr></table>";
                }
        }
        # update nb voter for current quizz
    $db->query("UPDATE ".$db->prefix("quizz_admin")." SET voters=voters+1 WHERE quizzID='$qid'");
    $result = $db->query("SELECT pollID, pollTitle, voters, answer, coef, good, bad, comment, image FROM ".$db->prefix("quizz_desc")." WHERE qid='$qid'");
        $score = 0;
                $num = 1;
    while(list($pollID, $pollTitle, $voters,$answer,$coef,$good,$bad,$comment,$image) = $db->fetchRow($result))
    {
                # check if ordered answer needed
            if (ereg(",",$answer))
            {
                    $array_answer = split(',',$answer);
                    $ordered_answer = implode(",",$voteQuizz[$pollID]);
                    $ordered_answer = preg_replace("/,--|--,|--/","",$ordered_answer);
                        foreach (explode(',',$answer) as $val)
                        {
                        $db->query("UPDATE ".$db->prefix("quizz_data")." SET optionCount=optionCount+1 WHERE (pollID='$pollID') AND (voteID='$val')");
                        }
                # update nb result for current answer (stat purpose)
            }
                else
                {
                $ordered_answer =  "";
        # update nb result for current answer (stat purpose)
        $db->query("UPDATE ".$db->prefix("quizz_data")." SET optionCount=optionCount+1 WHERE (pollID='$pollID') AND (voteID='$voteQuizz[$pollID]')");
                }
        # update nb result for current question (stat purpose)
        $db->query("UPDATE ".$db->prefix("quizz_desc")." SET voters=voters+1 WHERE pollID='$pollID'");
        # display results ?
        if ($displayresults == 1)
        {
                        # check if ordered answers to display results
                        if (!empty($ordered_answer))
                        {
            $result1 = $db->query("SELECT optionText, voteID FROM ".$db->prefix("quizz_data")." WHERE pollID='$pollID' ORDER BY voteID");
                        # compute the list of answer for the combo box
            while(list($optionText,$voteID) = $db->fetchRow($result1))
                        {
                                $array_text[$voteID] = $optionText;
                        }
                        # sort of the user answer
                        $optionText = "";
                        foreach ($voteQuizz[$pollID] as $val)
                        {
                                $optionText .= "$array_text[$val] ";
                        }
                        $answerText = "";
                        foreach (explode(',',$answer) as $val)
                        {
                                $answerText .= "$array_text[$val] ";
                        }
                        }
                        else
                        {
                $result1 = $db->query("SELECT optionText FROM ".$db->prefix("quizz_data")." WHERE (pollID='$pollID') AND (voteID='$voteQuizz[$pollID]')");
                 list($optionText) = $db->fetchRow($result1);
                 $result1 = $db->query("SELECT optionText FROM ".$db->prefix("quizz_data")." WHERE (pollID='$pollID') AND (voteID='$answer')");
                 list($answerText) = $db->fetchRow($result1);
                         }
             print "<B>$num</B> - "._QUIZZ_FORTHEQUESTION." \"".$myts->MakeTboxData4Show($pollTitle)."\", ";
             print ""._QUIZZ_YOUHAVECHOOSE." \"".$myts->MakeTboxData4Show($optionText)."\" "._QUIZZ_CORRECTANSWER." \"".$myts->MakeTboxData4Show($answerText)."\" ";
             $report = "<B>$num</B> - "._QUIZZ_FORTHEQUESTION." \"$".$myts->MakeTboxData4Show($pollTitle)."\", ";
             $report .= ""._QUIZZ_YOUHAVECHOOSE." \"".$myts->MakeTboxData4Show($optionText)."\" "._QUIZZ_CORRECTANSWER." \"".$myts->MakeTboxData4Show($answerText)."\" \n";
                        if ((empty($ordered_answer) and $voteQuizz[$pollID] == $answer) or
                                (!empty($ordered_answer) and $ordered_answer == $answer))
             {
                 if (!empty($good)) { print "<br><B>".$myts->MakeTareaData4Show($good)."</B>"; }
             }
             else
             {
                 if (!empty ($bad)) {print "<br><B>".$myts->MakeTareaData4Show($bad)."</B>"; }
             }
             echo "<P>";
        }
        # check for the correct answer ?
                if ((empty($ordered_answer) and $voteQuizz[$pollID] == $answer) or
                        (!empty($ordered_answer) and $ordered_answer == $answer))
        {
           $score= $score + $coef;
        }
                if (empty($ordered_answer))
                {
                    if(empty($answers)) { $answers = "$voteQuizz[$pollID]|"; } else { $answers .= "$voteQuizz[$pollID]|"; }
                }
                else
                {
                    if(empty($answers)) { $answers = "$ordered_answer|"; } else { $answers .= "$ordered_answer|"; }
                }
                $num++;
    }
    # display the final score ?
    if ($displayscore == 1) {
            echo ""._QUIZZ_YOURSCORE." : $score<P>";
            }
        # store the available infos if needed
        $ctime = time();
        if(!$db->query("INSERT INTO ".$db->prefix("quizz_check")." VALUES (NULL,'$ctime','$logname','$adrs','$qid','$score','$answers')"))
        {
                echo $db->error(). ": ".$db->error(). "<br>";
                return;
        }
        # show the score if needed
        if ($displayscore == 1) {
            echo ""._QUIZZ_LISTSCORE."<br>";
            $result = $db->query("SELECT username, score FROM ".$db->prefix("quizz_check")." WHERE qid='$qid' ORDER BY score DESC,time DESC LIMIT $nbscore ");
                while(list($username,$res) = $db->fetchRow($result))
                {
                        print "$username : $res<br>";
                }
            }
    # bye bye !! :)
    echo "<P><CENTER>"._QUIZZ_THANKS."</CENTER>";
    # send an email if needed
    if ($emailadmin)
    {
                if (empty($admemail)) { $admemail = $bcoosConfig['adminmail']; }
        $subject = "$logname "._QUIZZ_YOUHASVOTED." $quizzTitle";
        $message = "$logname "._QUIZZ_YOUHASVOTED." $quizzTitle\n";
        $message .= _QUIZZ_LOGNAME." : $logname\n";
        $message .= _EMAIL." : $adrs\n";
        $message .= _QUIZZ_YOURSCORE." : $score ($answers)\n";
        $message .= "$report\n";
        xoops_mail($admemail, $subject, $message);
    }
}
function QuizzList () {
global $db,$myts,$bcoosConfig,$xoopsUser,$cid,$prefix,$_GET;
	$cid = intval($_GET['cid']);
        if($cid == 0) {
		echo "<b>"._QUIZZCATLIST."</b><br><br>";
		print "<table width='100%'>";
		$result = $db->query("SELECT cid, name, comment, image FROM ".$db->prefix("quizz_categories"));
		while(list($cid, $name,$comment,$image) = $db->fetchRow($result)) {
			$reque =  "AND active ='1'";
			if ( $xoopsUser ) {
				if ( $xoopsUser->isAdmin() ) { $reque = ""; }
			}
			# compute the number of Quizz by Category
			$res = $db->query("select COUNT(*) from ".$db->prefix("quizz_admin")." where cid='$cid' $reque");
			list($nb) = $db->fetchRow($res);
			print "<tr><td>";
			print "";
			if ($nb > 0) { print "<a href=\"".XOOPS_URL."/modules/quizz/index.php?cid=$cid\">  ".$myts->MakeTboxData4Show($name)." </a>"; 
			} else { print " ".$myts->MakeTboxData4Show($name)." "; }
                        if (!empty($comment)) { print " : ".$myts->MakeTareaData4Show($comment).""; }
			
			print " ($nb "._QUIZZ_QUIZZ.") ";
                        print "\n";
			# print the admin menu if needed
			if ($xoopsUser) {
				$xoopsModule = XoopsModule::getByDirname("quizz");
				if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
					print " [ <a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzModifyCategory&cid=$cid\">"._MODIFY."</a> | ";
					print "<a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzDelCategory&cid=$cid\">"._DELETE."</a> ]";
				}
			}
			if (!empty($image)) { print "</td><td><img src='".XOOPS_URL."/modules/quizz/images/$image' border=0 alt='$comment'>"; }
			print "</td></tr>";
                }
               print "</table>";
	} else {
		echo "<b>"._QUIZZ_LIST."</b><br><br>";
		print "<table width='100%' border=0>";
		$result = $db->query("select quizzId, quizzTitle, active, restrict_user, administrator,displayscore,nbscore from ".$db->prefix("quizz_admin")." where cid='$cid' order by timeStamp desc");
		while(list($qid, $quizzTitle,$active,$restrict_user,$administrator,$displayscore,$nbscore) = $db->fetchRow($result)) {
			# display the inactive quizz if admin
			if ($xoopsUser) {
				$xoopsModule = XoopsModule::getByDirname("quizz");
				if ( $xoopsUser->isAdmin($xoopsModule->mid()) or ($administrator != "" and $xoopsUser->getVar("uname", "E") == $administrator)) {
					if ($active == 1) { $img = "green_dot.gif"; $alt = _QUIZZ_ACTIVE;  } else { $img = "red_dot.gif"; $alt = _QUIZZ_INACTIVE; }
					print "<tr><td>";
					print "";
					print "<a href=\"".XOOPS_URL."/modules/quizz/index.php?qid=$qid\">$quizzTitle</a> <img src='".XOOPS_URL."/modules/quizz/images/$img' alt='$alt'>";
					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> ]";
                                        # compute the number of Question in the Quizz
					$res = $db->query("select COUNT(*) from ".$db->prefix("quizz_desc")." where qid='$qid'");
					list($nb) = $db->fetchRow($res);
					print " ($nb "._QUIZZ_QUESTIONSNB.")\n";
					if ($displayscore) {
						$ress = $db->query("SELECT username, score FROM ".$db->prefix("quizz_check")." WHERE qid='$qid' ORDER BY score DESC,time DESC LIMIT $nbscore ");
						print "<br><br>";
						if ($db->getRowsNum($ress)>0) {
							echo "<B>"._QUIZZ_LISTSCORE.":</B><br>";
							while(list($username,$res) = $db->fetchRow($ress)) {
								print "$username : $res<br>"; }
                                                                print "<br>";
                                                        }
							print "</td></tr>";
						}
				} elseif ($active == 1) {
                                        print "<tr><td>";
                                        print "<a href=\"".XOOPS_URL."/modules/quizz/index.php?qid=$qid\">  ".$myts->MakeTboxData4Show($quizzTitle)." </a>";
                                        # compute the number of Question in the Quizz
					$res = $db->query("select COUNT(*) from ".$db->prefix("quizz_desc")." where qid='$qid'");
					list($nb) = $db->fetchRow($res);
					print " ($nb "._QUIZZ_QUESTIONSNB.")\n";
                                        if ($displayscore) {
						$ress = $db->query("SELECT username, score FROM ".$db->prefix("quizz_check")." WHERE qid='$qid' ORDER BY score DESC,time DESC LIMIT $nbscore ");
                                                print "<br><br>";
                                                if ($db->getRowsNum($ress)>0) {
							echo "<B>"._QUIZZ_LISTSCORE.":</B><br>";
							while(list($username,$res) = $db->fetchRow($ress)) { print "$username : $res<br>"; }
                                                        print "<br>";
                                                }
                                                    print "</td></tr>";
					}
				}
                                print "</td></tr>";
			}
                }
                print "</table>";
        }
	if (!$xoopsUser) {
		$result = $db->query("select quizzId, quizzTitle, active, restrict_user, administrator,displayscore,nbscore from ".$db->prefix("quizz_admin")." where cid='$cid' order by timeStamp desc");
		while(list($qid, $quizzTitle,$active,$restrict_user,$administrator,$displayscore,$nbscore) = $db->fetchRow($result)) {
			if ($active == 1) {
				print "<tr><td>";
				print "<a href=\"".XOOPS_URL."/modules/quizz/index.php?qid=$qid\">  ".$myts->MakeTboxData4Show($quizzTitle)." </a>";
				# compute the number of Question in the Quizz
				$res = $db->query("select COUNT(*) from ".$db->prefix("quizz_desc")." where qid='$qid'");
				list($nb) = $db->fetchRow($res);
				print " ($nb "._QUIZZ_QUESTIONSNB.")\n";
				if ($displayscore) {
					$ress = $db->query("SELECT username, score FROM ".$db->prefix("quizz_check")." WHERE qid='$qid' ORDER BY score DESC,time DESC LIMIT $nbscore ");
					print "<br><br>";
					if ($db->getRowsNum($ress)>0) {
						echo "<B>"._QUIZZ_LISTSCORE.":</B><br>";
						while(list($username,$res) = $db->fetchRow($ress)) { print "$username : $res<br>"; }
						print "<br>";
					}
					print "</td></tr>";
				}
                                print "</td></tr>";
			}
                }
		print "</table>";
        }
}
function ViewConditions ($qid) {
global $db;
        $result = $db->query("select quizzTitle, conditions  from ".$db->prefix("quizz_admin")." where quizzID='$qid'");
        list($quizzTitle,$conditions) = $db->fetchRow($result);
        OpenTable();
                echo "<table width='100%' border=0><tr><td><center><b>$quizzTitle</b><br><br><b>"._QUIZZ_CONDITIONS.":<br><br></center></b></td></tr>";
                      print "<tr><td>$conditions<br><center><a href=\"javascript:history.go(-1)\">"._QUIZZ_BACK."</a></center></td></tr></table>";
        CloseTable();
}
global $_GET,$_POST;
$qid = intval($_GET['qid']);
if ($qid == 0) { $qid = intval($_POST);} 
$bcoosConfig['title'] .= " - "._MI_QUIZZ_NAME;
if($bcoosConfig['startpage'] == "quizz") {
        $xoopsOption['show_rblock'] =1;
        include(XOOPS_ROOT_PATH."/header.php");
        make_cblock();
        echo "<br />";
}else{
        $xoopsOption['show_rblock'] =0;
        include(XOOPS_ROOT_PATH."/header.php");
}
        OpenTable();
include_once(XOOPS_ROOT_PATH."/class/xoopsmodule.php");
if ($xoopsUser) {
        $xoopsModule = XoopsModule::getByDirname("quizz");
        if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
            OpenTable();
                    print "<table width='100%' border=0><tr><td><center><a href=\"".XOOPS_URL."/modules/quizz/admin/index.php?act=QuizzAdmin\"> [ "._QUIZZ_ADMIN." ]</a></center></td></tr></table>";
            CloseTable();
                        echo "<br><br>";
        }
}
if($qid == 0) {
        QuizzList();
	
} else if (!isset($do)) {
        QuizzShow($qid);
} else {
        switch($do)
        {
        case "vote":
              quizzVote($qid,$voteQuizz,$logname,$adrs);
              break;
        case "contrib":
              QuizzContribute($qid);
              break;
        case "addContributeQuizzQuestion":
              addContributeQuizzQuestion($qid);
              break;
        case "viewConditions":
                      ViewConditions($qid);
                      break;
        default:
              QuizzShow($qid);
              break;
        }
}
CloseTable();
echo "<br>";
OpenTable();
echo "<center><b><a href='credits.php'>Quizz 1.4.2</a></b></center>";
CloseTable();
include(XOOPS_ROOT_PATH."/footer.php");
?>