/**
 * This page export the game millionaire to html
 * 
 * @author  bdaloukas
 * @version $Id: exporthtml_millionaire.php,v 1.14 2012/07/25 11:16:03 bdaloukas Exp $
 * @package game
 **/
function game_millionaire_html_getquestions($game, $context, &$maxanswers, &$countofquestions, &$retfeedback, $destdir, &$files)
{
    global $CFG, $DB, $USER;
    $maxanswers = 0;
    $countofquestions = 0;
    $files = array();
    if ($game->sourcemodule != 'quiz' and $game->sourcemodule != 'question') {
        print_error(get_string('millionaire_sourcemodule_must_quiz_question', 'game', get_string('modulename', 'quiz')) . ' ' . get_string('modulename', $game->sourcemodule));
    }
    if ($game->sourcemodule == 'quiz') {
        if ($game->quizid == 0) {
            print_error(get_string('must_select_quiz', 'game'));
        }
        $select = "qtype='multichoice' AND quiz='{$game->quizid}' " . " AND qqi.question=q.id";
        $table = "{question} q,{quiz_question_instances} qqi";
    } else {
        if ($game->questioncategoryid == 0) {
            print_error(get_string('must_select_questioncategory', 'game'));
        }
        //include subcategories
        $select = 'category=' . $game->questioncategoryid;
        if ($game->subcategories) {
            $cats = question_categorylist($game->questioncategoryid);
            if (strpos($cats, ',') > 0) {
                $select = 'category in (' . $cats . ')';
            }
        }
        $select .= " AND qtype='multichoice'";
        $table = "{question} q";
    }
    $select .= " AND q.hidden=0";
    $sql = "SELECT q.id as id, q.questiontext FROM {$table} WHERE {$select}";
    $recs = $DB->get_records_sql($sql);
    $ret = '';
    $retfeedback = '';
    foreach ($recs as $rec) {
        $recs2 = $DB->get_records('question_answers', array('question' => $rec->id), 'fraction DESC', 'id,answer,feedback');
        //Must parse the questiontext and get the name of files.
        $line = $rec->questiontext;
        $line = game_export_split_files($game->course, $context, 'questiontext', $rec->id, $rec->questiontext, $destdir, $files);
        $linefeedback = '';
        foreach ($recs2 as $rec2) {
            $line .= '#' . str_replace(array('"', '#'), array("'", ' '), game_export_split_files($game->course, $context, 'answer', $rec2->id, $rec2->answer, $destdir, $files));
            $linefeedback .= '#' . str_replace(array('"', '#'), array("'", ' '), $rec2->feedback);
        }
        if ($ret != '') {
            $ret .= ",\r";
        }
        $ret .= '"' . base64_encode($line) . '"';
        if ($retfeedback != '') {
            $retfeedback .= ",\r";
        }
        $retfeedback .= '"' . base64_encode($linefeedback) . '"';
        if (count($recs2) > $maxanswers) {
            $maxanswers = count($recs2);
        }
        $countofquestions++;
    }
    return $ret;
}
function game_exmportjavame_getanswers_question_select($game, $context, $table, $select, $fields, $courseid, $destdir, &$files)
{
    global $CFG, $DB;
    echo "files1={$files}<br>";
    $sql = "SELECT {$fields} FROM {$CFG->prefix}{$table} WHERE {$select}";
    if (($questions = $DB->get_records_sql($sql)) === false) {
        return;
    }
    $line = 0;
    $map = array();
    foreach ($questions as $question) {
        unset($ret);
        $ret->qtype = $question->qtype;
        $ret->question = $question->questiontext;
        $ret->question = str_replace(array('"', '#'), array("'", ' '), game_export_split_files($game->course, $context, 'questiontext', $question->id, $ret->question, $destdir, $files));
        switch ($question->qtype) {
            case 'shortanswer':
                $rec = $DB->get_record('question_answers', array('question' => $question->id), 'id,answer,feedback');
                $ret->answer = $rec->answer;
                $ret->feedback = $rec->feedback;
                $map[] = $ret;
                break;
            default:
                break;
        }
    }
    return $map;
}