Example #1
0
 private function _process_value($v)
 {
     return _process_value($v, $this->selections, defaultDB());
 }
Example #2
0
function do_template($temp, $db = NULL, &$pick_db = NULL, &$reason = NULL)
{
    if ($db === NULL) {
        $db = defaultDB();
    }
    if (is_string($temp)) {
        return $temp;
    }
    $repeats = 0;
    $sentence = NULL;
    $reason = NULL;
    $ignore = NULL;
    $reset = [];
    if (is_array($pick_db)) {
        $reset = [];
        foreach ($pick_db as $k => $_) {
            $reset[$k] = _process_value($_, $reset, $db);
        }
    }
    while ($repeats < 1 and $sentence === NULL) {
        $repeats += 1;
        $sentence = [];
        $pick_db = $reset;
        foreach ($temp as $k => $t) {
            $res = do_pick($t, $db, $pick_db, $reason);
            if ($res === NULL) {
                $reason .= " on key {$k}";
                $reason .= " (picks were " . dump_pick_db($pick_db) . ")";
                $sentence = NULL;
                break;
            } elseif ($res !== FALSE) {
                $sentence[$k] = $res;
            }
        }
    }
    if ($sentence !== NULL) {
        return array_values($sentence);
    }
}
Example #3
0
     $reason = "choices were not in an array (" . gettype($choices) . ")";
     return $try();
 }
 if (safe_get("choices{$n}-no-shuffle", $quiz->get_others()) or safe_get("no_shuffle", $choices)) {
     $shuffle = false;
     unset($choices["no_shuffle"]);
 }
 $results = array_map(function ($answer, $key) use($dopick, &$selections, &$stop, &$reason, &$correct, $lang) {
     if ($stop) {
         return;
     }
     $ret = $dopick($answer, TRUE);
     if ($ret === NULL) {
         $stop = TRUE;
     } else {
         if ($key === "correct" or is_array($answer) and array_key_exists("correct", $answer) and !!_process_value($answer["correct"], $selections, defaultDB())) {
             $correct[] = format_word($ret, $lang);
         }
     }
     return $ret;
 }, $choices, array_keys($choices));
 $cquiz_answers["answer{$n}"] = $correct;
 if ($stop) {
     return $try();
 }
 if ($shuffle) {
     shuffle($results);
 }
 if (count($results) !== count(vec_norm(array_unique($results)))) {
     $reason = "results were not unique (" . implode(",", $results) . ")";
     return $try();
Example #4
0
function which($lang, $spart, $key, $given = NULL, $rand = NULL, $name = NULL)
{
    global $OP_MULTIPLE_CHOICE;
    global $OP_PARAGRAPH;
    global $OP_RPAREN;
    global $OP_LQUOTE;
    global $OP_RQUOTE;
    $selections = [];
    $path = [];
    $mgr = defaultDB()->get_mgr($lang, $spart);
    $given = PATH($mgr, $given);
    $paren = [];
    $_gender = null;
    $recurse = function ($mgr) use(&$_gender, $spart, $given, &$recurse, &$path, &$paren, $rand, $key, $lang) {
        global $OP_LPAREN;
        foreach ($mgr->simple_keys as $k) {
            if ($given->key_exists($k)) {
                $path[] = $given->key_value($k);
            } else {
                if ($k === "gender" and $spart === "verb") {
                    $_gender = make_pick(PICK($k, safe_get($k, $rand))->l($lang), $k);
                } else {
                    $path[] = make_pick(PICK($k, safe_get($k, $rand))->l($lang), $k);
                    if ($k !== $key) {
                        if (!$paren) {
                            $paren[] = $OP_LPAREN;
                        }
                        $paren[] = make_pick(PICK($k, safe_get($k, $rand))->l($lang), $k);
                    }
                }
            }
        }
        foreach ($mgr->recursive_keys as $k) {
            if ($given->key_exists($k)) {
                $path[] = $given->key_value($k);
                $recurse($mgr->level[$k][$given->key_value($k)]);
            }
        }
    };
    $recurse($mgr);
    if ($paren) {
        $paren[] = $OP_RPAREN;
    }
    $get_val = function ($pick_db, $v) use($mgr, $path) {
        $p = $path;
        $path = PATH($pick_db["word"]);
        foreach ($p as $k => $_) {
            $path->add2([$k => _process_value($_, $pick_db, defaultDB(), $path)]);
        }
        $path->add($v);
        return $path->get();
    };
    $answers = [];
    foreach ($mgr->key2values[$key] as $v) {
        $answers[] = ["correct" => function ($pick_db) use($v, $get_val) {
            return $pick_db["result"] === $get_val($pick_db, $v);
        }, "value" => $v];
    }
    $ret = ["help" => "What {$key} is this word?", "selections" => $selections, "sentence" => array_merge([["lang" => $lang, "speechpart" => $spart, "path" => $path, "attr" => ["!template" => NULL, "!hidden" => NULL], "store_word" => "word", "store" => "result"]], $paren, [$OP_PARAGRAPH, $OP_MULTIPLE_CHOICE]), "choices0" => $answers, "choices0-tooltip" => "What {$key}?", "choices0-no-shuffle" => true, "choices0-language" => "en"];
    if ($name !== null) {
        $ret["sentence"][0]["name"] = $name;
    }
    if ($_gender !== null) {
        $ret["sentence"][0]["verb-gender"] = $_gender;
    }
    return $ret;
}