コード例 #1
0
 $cquiz_answers = [];
 $_mini = [];
 $allow_space = FALSE;
 $n = 0;
 $cquiz_answers = [];
 $refresh = function ($finish = false) use(&$result_json, &$_mini, &$allow_space, &$cquiz_answers) {
     if ($_mini) {
         $result_json[] = ["text", serialize_sentence_part($_mini, $allow_space) . (($finish and $allow_space) ? " " : "")];
         $_mini = [];
     }
 };
 $dopick = function ($answer, $mode = TRUE) use(&$reason, &$selections) {
     return $mode ? do_pick($answer, NULL, $selections, $reason) : _process_value($answer, $selections, defaultDB());
 };
 foreach ($sentence as $word) {
     if (ISHTML($word)) {
         $refresh();
         $result_json[] = ["html", $word->text];
     } elseif ($word === $OP_MULTIPLE_CHOICE or $word === $OP_MATCHING_CHOICES) {
         $refresh(1);
         $shuffle = true;
         $stop = FALSE;
         $correct = [];
         $choices = $quiz->get_other("choices{$n}");
         $lang = safe_get("choices{$n}-language", $quiz->get_others());
         if (is_callable($choices)) {
             $choices = $choices($selections, defaultDB());
         }
         if (!is_array($choices)) {
             $reason = "choices were not in an array (" . gettype($choices) . ")";
             return $try();
コード例 #2
0
function do_pick($t, $db, &$pick_db, &$reason)
{
    if ($db === NULL) {
        $db = defaultDB();
    }
    if ($t === NULL) {
        $reason = "pick was null";
        return $t;
    } elseif (ISOP($t) or ISHTML($t)) {
        return $t;
    } elseif (ISPICK($t)) {
        return $t->rand($db);
    } elseif (is_string($t)) {
        return $t;
    } elseif (is_callable($t)) {
        $t = _process_value($t, $pick_db, $db);
        if ($t === NULL) {
            $reason = "custom function returned NULL";
            return $t;
        } else {
            return do_pick($t, $db, $pick_db, $reason);
        }
    } elseif (array_key_exists("condition", $t) and !$t["condition"]($pick_db, $db, null)) {
        return FALSE;
    } elseif (array_key_exists("value", $t)) {
        return _process_value($t["value"], $pick_db, $db);
    }
    if (!($word = safe_get("word", $t))) {
        $searcher = $db->searcher();
        #var_dump(array_keys($searcher->master));
        if (array_key_exists("name", $t)) {
            $searcher = $searcher->name(_process_value($t["name"], $pick_db, $db));
        }
        if (array_key_exists("language", $t)) {
            $searcher = $searcher->lang(_process_value($t["language"], $pick_db, $db));
        } elseif (array_key_exists("lang", $t)) {
            $searcher = $searcher->lang(_process_value($t["lang"], $pick_db, $db));
        }
        if (array_key_exists("speechpart", $t)) {
            $searcher = $searcher->partofspeech(_process_value($t["speechpart"], $pick_db, $db));
        } elseif (array_key_exists("spart", $t)) {
            $searcher = $searcher->partofspeech(_process_value($t["spart"], $pick_db, $db));
        }
        if (array_key_exists("attr", $t)) {
            foreach ($t["attr"] as $k => $v) {
                $v = _process_value($v, $pick_db, $db);
                if ($reverse = substr($k, 0, 1) === "!") {
                    $k = substr($k, 1);
                    $m = "only_without_attr";
                } else {
                    $m = "only_with_attr";
                }
                $searcher = $searcher->{$m}($v !== NULL ? ATTR($k, $v) : ATTR($k));
            }
        }
        $word = $searcher->rand();
    }
    if (!ISWORD($word)) {
        $reason = "could not find a word with name " . var_export($t["name"], 1) . " and attrs " . var_export(safe_get("attr", $t), 1);
        return;
    }
    if (array_key_exists("store_word", $t)) {
        $pick_db[$t["store_word"]] = $word;
    }
    $word->read_paths();
    $path = PATH($word);
    if (array_key_exists("path", $t)) {
        $p = $t["path"];
        if (!is_array($p)) {
            $p = _process_value($p, $pick_db, $db, $path);
        }
        foreach ($p as $k => $_) {
            $path->add2([$k => _process_value($_, $pick_db, $db, $path)]);
        }
    }
    if (array_key_exists("verb-gender", $t)) {
        $g = $t["verb-gender"];
        $g = _process_value($g, $pick_db, $db, $path);
        if ($g !== NULL and $path->exists()) {
            $path->add($g);
            if (!$path->hasvalue()) {
                $path->take("gender");
            }
        }
    }
    if ($path->hasvalue() || !(string) $path) {
        $ret = $path->hasvalue() ? $path->get() : $word->name();
        if (array_key_exists("store", $t)) {
            $pick_db[$t["store"]] = $ret;
        }
        if (array_key_exists("store_path", $t)) {
            $pick_db[$t["store_path"]] = $path;
        }
        return format_word($ret, $word->lang());
    } else {
        $reason = "path '{$path}' didn't exist in word with id <a target='_blank' href='dictionary.php?id=" . $word->id() . "'>" . $word->id() . "</a> or was NULL";
        return;
    }
}