Exemple #1
0
function game_cryptex_continue($id, $game, $attempt, $cryptexrec, $endofgame, $context)
{
    global $DB, $USER;
    if ($endofgame) {
        game_updateattempts($game, $attempt, -1, true);
        $endofgame = false;
    }
    if ($attempt != false and $cryptexrec != false) {
        $crossm = $DB->get_record('game_cross', array('id' => $attempt->id));
        return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context);
    }
    if ($attempt === false) {
        $attempt = game_addattempt($game);
    }
    $cryptex = new CryptexDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        print_error(get_string('no_words', 'game'));
    }
    $infos = array();
    $reps = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if (game_strpos($rec->answertext, ' ')) {
                continue;
                // Spaces not allowed.
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid);
        $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid);
        if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions AS r')) != false) {
            $reps[$rec->answertext] = $rec2->r;
        }
    }
    $cryptex->setwords($answers, $game->param1, $reps);
    // The game->param4 is minimum words.
    // The game->param2 is maximum words.
    if ($cryptex->computedata($crossm, $crossd, $letters, $game->param4, $game->param2)) {
        $newcrossd = array();
        foreach ($crossd as $rec) {
            if (array_key_exists($rec->answertext, $infos)) {
                $info = $infos[$rec->answertext];
                $rec->id = 0;
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
            }
            game_update_queries($game, $attempt, $rec, 0, '');
            $newcrossd[] = $rec;
        }
        $cryptexrec = $cryptex->savecryptex($game, $crossm, $newcrossd, $attempt->id, $letters);
    }
    game_updateattempts($game, $attempt, 0, 0);
    return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, false, false, false, $context);
}
function game_cross_continue($id, $game, $attempt, $cross, $g = '', $endofgame = '')
{
    if ($endofgame) {
        if ($g == '') {
            game_updateattempts($game, $attempt, -1, true);
            $endofgame = false;
        }
    }
    if ($attempt != false and $cross != false) {
        return game_cross_play($id, $game, $attempt, $cross, $g, false, false, $endofgame, false, false, false, false);
    }
    if ($attempt == false) {
        $attempt = game_addattempt($game);
    }
    $textlib = textlib_get_instance();
    $cross = new CrossDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        error('game_cross_continue: ' . get_string('cross_nowords', 'game'));
    }
    $infos = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if ($textlib->strpos($rec->answertext, ' ')) {
                continue;
                //spaces not allowed
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment);
    }
    $cross->setwords($answers, $game->param1);
    if ($cross->computedata($crossm, $crossd, $game->param2)) {
        $new_crossd = array();
        foreach ($crossd as $rec) {
            $info = $infos[$rec->answertext];
            if ($info != false) {
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
                $rec->attachment = $info[3];
            }
            $new_crossd[] = $rec;
        }
        $cross->save($game, $crossm, $new_crossd, $attempt->id);
        game_updateattempts($game, $attempt, 0, 0);
        return game_cross_play($id, $game, $attempt, $crossm, '', false, false, false, false, false, false, false);
    }
    if (count($crossd) == 0) {
        error('game_cross_continue: ' . get_string('cross_nowords', 'game'));
    }
}
function showlegend($legend, $title)
{
    if (count($legend) == 0) {
        return;
    }
    echo "<br><b>{$title}</b><br>";
    foreach ($legend as $key => $line) {
        $line = game_repairquestion($line);
        echo game_filtertext("{$key}: {$line}<br>", 0);
    }
}
function game_cryptex_continue($id, $game, $attempt, $cryptexrec, $endofgame)
{
    if ($endofgame) {
        game_updateattempts($game, $attempt, -1, true);
        $endofgame = false;
    }
    if ($attempt != false and $cryptexrec != false) {
        $crossm = get_record_select('game_cross', "id={$attempt->id}");
        return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm);
    }
    if ($attempt === false) {
        $attempt = game_addattempt($game);
    }
    $textlib = textlib_get_instance();
    $cryptex = new CryptexDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        error(get_string('cryptex_nowords', 'game'));
    }
    $infos = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if ($textlib->strpos($rec->answertext, ' ')) {
                continue;
                //spaces not allowed
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid);
    }
    $cryptex->setwords($answers, $game->param1);
    if ($cryptex->computedata($crossm, $crossd, $letters, $game->param2)) {
        $new_crossd = array();
        foreach ($crossd as $rec) {
            if (array_key_exists($rec->answertext, $infos)) {
                $info = $infos[$rec->answertext];
                $rec->id = 0;
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
            }
            game_update_queries($game, $attempt, $rec, 0, '');
            $new_crossd[] = $rec;
        }
        $cryptexrec = $cryptex->save($game, $crossm, $new_crossd, $attempt->id, $letters);
    }
    game_updateattempts($game, $attempt, 0, 0);
    return game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm);
}
Exemple #5
0
function game_cross_new($game, $attemptid, &$crossm)
{
    global $DB, $USER;
    $cross = new CrossDB();
    $questions = array();
    $infos = array();
    $answers = array();
    $recs = game_questions_shortanswer($game);
    if ($recs == false) {
        print_error('game_cross_continue: ' . get_string('no_words', 'game'));
    }
    $infos = array();
    $reps = array();
    foreach ($recs as $rec) {
        if ($game->param7 == false) {
            if (textlib::strpos($rec->answertext, ' ')) {
                continue;
                //spaces not allowed
            }
        }
        $rec->answertext = game_upper($rec->answertext);
        $answers[$rec->answertext] = game_repairquestion($rec->questiontext);
        $infos[$rec->answertext] = array($game->sourcemodule, $rec->questionid, $rec->glossaryentryid, $rec->attachment);
        $a = array('gameid' => $game->id, 'userid' => $USER->id, 'questionid' => $rec->questionid, 'glossaryentryid' => $rec->glossaryentryid);
        if (($rec2 = $DB->get_record('game_repetitions', $a, 'id,repetitions r')) != false) {
            $reps[$rec->answertext] = $rec2->r;
        }
    }
    $cross->setwords($answers, $game->param1, $reps);
    //game->param2 is maximum words in crossword
    if ($cross->computedata($crossm, $crossd, $lettets, $game->param2)) {
        $new_crossd = array();
        foreach ($crossd as $rec) {
            $info = $infos[$rec->answertext];
            if ($info != false) {
                $rec->sourcemodule = $info[0];
                $rec->questionid = $info[1];
                $rec->glossaryentryid = $info[2];
                $rec->attachment = $info[3];
            }
            $new_crossd[] = $rec;
        }
        $cross->savecross($game, $crossm, $new_crossd, $attemptid);
    }
    if (count($crossd) == 0) {
        print_error('game_cross_continue: ' . get_string('no_words', 'game'));
    }
}
 function showhtml_base($crossm, $crossd, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game)
 {
     $textlib = textlib_get_instance();
     $this->m_LegendH = array();
     $this->m_LegendV = array();
     $sRet = "CrosswordWidth  = {$crossm->cols};\n";
     $sRet .= "CrosswordHeight = {$crossm->rows};\n";
     $sRet .= "Words=" . count($crossd) . ";\n";
     $sWordLength = "";
     $sguess = "";
     $ssolutions = '';
     $shtmlsolutions = '';
     $sWordX = "";
     $sWordY = "";
     $sClue = "";
     $LastHorizontalWord = -1;
     $i = -1;
     $LegendV = array();
     $LegendH = array();
     if ($game->glossaryid) {
         $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
         $contextglossary = get_context_instance(CONTEXT_MODULE, $cmglossary->id);
     }
     foreach ($crossd as $rec) {
         if ($rec->horizontal == false and $LastHorizontalWord == -1) {
             $LastHorizontalWord = $i;
         }
         $i++;
         $sWordLength .= "," . $textlib->strlen($rec->answertext);
         if ($rec->questionid != 0) {
             $q = game_filterquestion(str_replace('\\"', '"', $rec->questiontext), $rec->questionid, $context->id, $game->course);
             $rec->questiontext = game_repairquestion($q);
         } else {
             //glossary
             $q = game_filterglossary(str_replace('\\"', '"', $rec->questiontext), $rec->glossaryentryid, $contextglossary->id, $game->course);
             $rec->questiontext = game_repairquestion($q);
         }
         $sClue .= ',"' . game_tojavascriptstring(game_filtertext($rec->questiontext, 0)) . "\"\r\n";
         if ($showstudentguess) {
             $sguess .= ',"' . $rec->studentanswer . '"';
         } else {
             $sguess .= ",''";
         }
         $sWordX .= "," . ($rec->col - 1);
         $sWordY .= "," . ($rec->row - 1);
         if ($showsolution) {
             $ssolutions .= ',"' . $rec->answertext . '"';
         } else {
             $ssolutions .= ',""';
         }
         if ($showhtmlsolutions) {
             $shtmlsolutions .= ',"' . base64_encode($rec->answertext) . '"';
         }
         $attachment = '';
         //if( game_issoundfile( $rec->attachment)){
         //    $attachment = game_showattachment( $rec->attachment);
         //}
         $s = $rec->questiontext . $attachment;
         if ($rec->horizontal) {
             if (array_key_exists($rec->row, $LegendH)) {
                 $LegendH[$rec->row][] = $s;
             } else {
                 $LegendH[$rec->row] = array($s);
             }
         } else {
             if (array_key_exists($rec->col, $LegendV)) {
                 $LegendV[$rec->col][] = $s;
             } else {
                 $LegendV[$rec->col] = array($s);
             }
         }
     }
     $letters = get_string('lettersall', 'game');
     $textlib = textlib_get_instance();
     $this->m_LegendV = array();
     foreach ($LegendH as $key => $value) {
         if (count($value) == 1) {
             $this->m_LegendH[$key] = $value[0];
         } else {
             for ($i = 0; $i < count($value); $i++) {
                 $this->m_LegendH[$key . $textlib->substr($letters, $i, 1)] = $value[$i];
             }
         }
     }
     $this->m_LegendV = array();
     foreach ($LegendV as $key => $value) {
         if (count($value) == 1) {
             $this->m_LegendV[$key] = $value[0];
         } else {
             for ($i = 0; $i < count($value); $i++) {
                 $this->m_LegendV[$key . $textlib->substr($letters, $i, 1)] = $value[$i];
             }
         }
     }
     ksort($this->m_LegendH);
     ksort($this->m_LegendV);
     $sClue = $textlib->substr($sClue, 1, $textlib->strlen($sClue) - 1);
     $sRet .= "WordLength = new Array( " . $textlib->substr($sWordLength, 1, $textlib->strlen($sWordLength) - 1) . ");\n";
     $sRet .= "Clue = new Array( " . $sClue . ");\n";
     $sguess = str_replace(' ', '_', $sguess);
     $sRet .= "Guess = new Array( " . $textlib->substr($sguess, 1, $textlib->strlen($sguess) - 1) . ");\n";
     $sRet .= "Solutions = new Array( " . $textlib->substr($ssolutions, 1, $textlib->strlen($ssolutions) - 1) . ");\n";
     if ($showhtmlsolutions) {
         $sRet .= "HtmlSolutions = new Array( " . $textlib->substr($shtmlsolutions, 1, $textlib->strlen($shtmlsolutions) - 1) . ");\n";
     }
     $sRet .= "WordX = new Array( " . $textlib->substr($sWordX, 1, $textlib->strlen($sWordX) - 1) . ");\n";
     $sRet .= "WordY = new Array( " . $textlib->substr($sWordY, 1, $textlib->strlen($sWordY) - 1) . ");\n";
     $sRet .= "LastHorizontalWord = {$LastHorizontalWord};\n";
     return $sRet;
 }
 public function showhtml_base($crossm, $crossd, $showsolution, $showhtmlsolutions, $showstudentguess, $context, $game)
 {
     $this->mLegendh = array();
     $this->mLegendv = array();
     $sret = "CrosswordWidth  = {$crossm->cols};\n";
     $sret .= "CrosswordHeight = {$crossm->rows};\n";
     $sret .= "Words=" . count($crossd) . ";\n";
     $swordlength = "";
     $sguess = "";
     $ssolutions = '';
     $shtmlsolutions = '';
     $swordx = "";
     $swordy = "";
     $sclue = "";
     $lasthorizontalword = -1;
     $i = -1;
     $legendv = array();
     $legendh = array();
     if ($game->glossaryid) {
         $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
         $contextglossary = game_get_context_module_instance($cmglossary->id);
     }
     foreach ($crossd as $rec) {
         if ($rec->horizontal == false and $lasthorizontalword == -1) {
             $lasthorizontalword = $i;
         }
         $i++;
         $swordlength .= "," . game_strlen($rec->answertext);
         if ($rec->questionid != 0) {
             $q = game_filterquestion(str_replace('\\"', '"', $rec->questiontext), $rec->questionid, $context->id, $game->course);
             $rec->questiontext = game_repairquestion($q);
         } else {
             // Glossary.
             $q = game_filterglossary(str_replace('\\"', '"', $rec->questiontext), $rec->glossaryentryid, $contextglossary->id, $game->course);
             $rec->questiontext = game_repairquestion($q);
         }
         $sclue .= ',"' . game_tojavascriptstring(game_filtertext($rec->questiontext, 0)) . "\"\r\n";
         if ($showstudentguess) {
             $sguess .= ',"' . $rec->studentanswer . '"';
         } else {
             $sguess .= ",''";
         }
         $swordx .= "," . ($rec->col - 1);
         $swordy .= "," . ($rec->row - 1);
         if ($showsolution) {
             $ssolutions .= ',"' . $rec->answertext . '"';
         } else {
             $ssolutions .= ',""';
         }
         if ($showhtmlsolutions) {
             $shtmlsolutions .= ',"' . base64_encode($rec->answertext) . '"';
         }
         $attachment = '';
         $s = $rec->questiontext . $attachment;
         if ($rec->horizontal) {
             if (array_key_exists($rec->row, $legendh)) {
                 $legendh[$rec->row][] = $s;
             } else {
                 $legendh[$rec->row] = array($s);
             }
         } else {
             if (array_key_exists($rec->col, $legendv)) {
                 $legendv[$rec->col][] = $s;
             } else {
                 $legendv[$rec->col] = array($s);
             }
         }
     }
     $letters = get_string('lettersall', 'game');
     $this->mlegendh = array();
     foreach ($legendh as $key => $value) {
         if (count($value) == 1) {
             $this->mlegendh[$key] = $value[0];
         } else {
             for ($i = 0; $i < count($value); $i++) {
                 $this->mlegendh[$key . game_substr($letters, $i, 1)] = $value[$i];
             }
         }
     }
     $this->mlegendv = array();
     foreach ($legendv as $key => $value) {
         if (count($value) == 1) {
             $this->mlegendv[$key] = $value[0];
         } else {
             for ($i = 0; $i < count($value); $i++) {
                 $this->mlegendv[$key . game_substr($letters, $i, 1)] = $value[$i];
             }
         }
     }
     ksort($this->mlegendh);
     ksort($this->mlegendv);
     $sret .= "WordLength = new Array( " . game_substr($swordlength, 1) . ");\n";
     $sret .= "Clue = new Array( " . game_substr($sclue, 1) . ");\n";
     $sguess = str_replace(' ', '_', $sguess);
     $sret .= "Guess = new Array( " . game_substr($sguess, 1) . ");\n";
     $sret .= "Solutions = new Array( " . game_substr($ssolutions, 1) . ");\n";
     if ($showhtmlsolutions) {
         $sret .= "HtmlSolutions = new Array( " . game_substr($shtmlsolutions, 1) . ");\n";
     }
     $sret .= "WordX = new Array( " . game_substr($swordx, 1) . ");\n";
     $sret .= "WordY = new Array( " . game_substr($swordy, 1) . ");\n";
     $sret .= "LastHorizontalWord = {$lasthorizontalword};\n";
     return $sret;
 }