public function onVote(WC_Challenge $chall) { if ('0' === ($userid = GWF_Session::getUserID())) { return GWF_HTML::err('ERR_LOGIN_REQUIRED'); } if (!WC_ChallSolved::hasSolved($userid, $chall->getID())) { return $this->module->error('err_chall_vote'); } $form = $this->getFormVote($chall, false, $userid); if (false !== ($error = $form->validate($this->module))) { return $error; } if (false !== ($vs = $chall->getVotesDif())) { $vs->onUserVoteSafe($_POST['dif'], $userid); } if (false !== ($vs = $chall->getVotesEdu())) { $vs->onUserVoteSafe($_POST['edu'], $userid); } if (false !== ($vs = $chall->getVotesFun())) { $vs->onUserVoteSafe($_POST['fun'], $userid); } if (false === WC_ChallSolved::setVoted($userid, $chall->getID(), true)) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } if (false === $chall->onRecalcVotes()) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } return $this->module->message('msg_chall_voted'); }
function math_pyramid_check(WC_Challenge $chall, $formula, $maxlen, $precision = 4) { error_reporting(E_ERROR); GWF_Debug::setDieOnError(false); GWF_Debug::setMailOnError(false); $len = strlen($formula); $tests = array('0' => 0, '1' => 0.2357, '3.14' => 7.2971, '10' => 235.7023, '100' => 235702.2604); $eval = new EvalMath(); $fa = "f(a) = {$formula}"; if (false === $eval->evaluate($fa)) { echo GWF_HTML::error('Math Pyramid', $chall->lang('err_formula', array(htmlspecialchars($fa)))); return false; } GWF_Debug::setDieOnError(true); GWF_Debug::setMailOnError(true); $back = GWF_HTML::message('Math Pyramid', $chall->lang('msg_formula', array(htmlspecialchars($fa))), false); $correct = 0; foreach ($tests as $a => $result) { $result2 = $eval->evaluate("f({$a})"); $result = sprintf('%.0' . $precision . 'f', $result); $result2 = sprintf('%.0' . $precision . 'f', $result2); if ($result === $result2) { $back .= GWF_HTML::message('Math Pyramid', $chall->lang('msg_correct', array($a, $result2, $result)), false); $correct++; } else { $back .= GWF_HTML::error('Math Pyramid', $chall->lang('err_wrong', array($a, $result2, $result)), false); } } require_once GWF_CORE_PATH . 'module/WeChall/WC_MathChall.php'; if ($chall->getID() > 0 && $correct === count($tests)) { if (false === WC_MathChall::insertSolution($chall->getID(), GWF_Session::getUserID(), $formula)) { $back .= GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } else { $back .= GWF_HTML::message('Math Pyramid', WC_HTML::lang('msg_wmc_sol_inserted', array($len, WC_MathChall::getLimitedHREF($chall, $len))), false); } } # Check Len if ($len > $maxlen) { $back .= GWF_HTML::error('Math Pyramid', $chall->lang('err_too_long', array($len, $maxlen)), false); } echo $back; if ($correct === count($tests) && $len <= $maxlen) { if ($len < $maxlen) { echo GWF_HTML::message('Math Pyramid', $chall->lang('msg_new_record', array($len, $maxlen)), false); GWF_Settings::setSetting('WC_MATH_PYRAMID', $len); } $chall->onChallengeSolved(GWF_Session::getUserID()); } }
private function templateSolutions(WC_Challenge $chall, GWF_User $user, $length, $token) { $tt = $this->module->lang('pt_wmc_sol', array($chall->display('chall_title'), $length)); GWF_Website::setPageTitle($tt); $ipp = 50; $cid = $chall->getID(); $length = (int) $length; $whitelist = array('user_name', 'wmc_date', 'wmc_length', 'wmc_solution'); $by = GDO::getWhitelistedByS(Common::getGetString('by'), $whitelist, self::DEFAULT_BY); $dir = GDO::getWhitelistedDirS(Common::getGetString('dir'), self::DEFAULT_DIR); $wmc = GWF_TABLE_PREFIX . 'wc_math_chall'; $users = GWF_TABLE_PREFIX . 'user'; $where = "wmc_cid={$cid} AND wmc_length>={$length}"; $db = gdo_db(); $result = $db->queryFirst("SELECT COUNT(*) c FROM {$wmc} WHERE {$where}"); $nRows = (int) $result['c']; $nPages = GWF_PageMenu::getPagecount($ipp, $nRows); $page = Common::clamp(Common::getGetInt('page', 1), 1, $nPages); $from = GWF_PageMenu::getFrom($page, $ipp); $limit = GDO::getLimit($ipp, $from); $query = "SELECT user_name, wmc_date, wmc_length, wmc_solution FROM {$wmc} LEFT JOIN {$users} ON user_id=wmc_uid WHERE {$where} ORDER BY {$by} {$dir} {$limit}"; $tVars = array('sort_url' => GWF_WEB_ROOT . "index.php?mo=WeChall&me=MathSolutions&cid={$cid}&length={$length}&token={$token}&by=%BY%&dir=%DIR%", 'data' => $db->queryAll($query), 'page_menu' => GWF_PageMenu::display($page, $nPages, GWF_WEB_ROOT . sprintf('index.php?mo=WeChall&me=MathSolutions&cid=%d&length=%d&token=%s&by=%s&dir=%s&page=%%PAGE%%', $cid, $length, $token, urlencode($by), urlencode($dir))), 'table_title' => $tt, 'chall' => $chall); return $this->module->templatePHP('math_solutions.php', $tVars); }
private function onReset(WC_Challenge $chall) { require_once GWF_CORE_PATH . 'module/WeChall/WC_ChallSolved.php'; # Remove solved $cid = $chall->getID(); $solved = GDO::table('WC_ChallSolved'); if (false === $solved->update("csolve_date='', csolve_options=0", "csolve_cid={$cid}")) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } $affected = $solved->affectedRows(); $back = GWF_HTML::message('WeChall', "Reset {$affected} players that have solved it."); # Remove from users from groups $gid = $chall->getGID(); $usergroup = GDO::table('GWF_UserGroup'); if (false === $usergroup->deleteWhere("ug_groupid={$gid}")) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } $affected = $usergroup->affectedRows(); $back .= GWF_HTML::message('WeChall', "Removed {$affected} players from the challenge group."); # Reset votes Module_WeChall::includeVotes(); if (false === $chall->getVotesDif()->resetVotesSameSettings()) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } if (false === $chall->getVotesEdu()->resetVotesSameSettings()) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } if (false === $chall->getVotesFun()->resetVotesSameSettings()) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } if (false === $chall->onRecalcVotes()) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } # reset solve count and various vars if (false === $chall->saveVars(array('chall_solvecount' => 0, 'chall_views' => 0, 'chall_date' => GWF_Time::getDate(GWF_Date::LEN_SECOND)))) { return GWF_HTML::err('ERR_DATABASE', array(__FILE__, __LINE__)); } return $back; }