function generateAlgoreaCodes($db, $contestID) { // retrieving awarded contestants through "award1" model $modelName = 'award1'; $model = getViewModel($modelName); $request = array("modelName" => $modelName, "model" => $model, "filters" => array()); foreach ($model["fields"] as $fieldName => $field) { $request["fields"][] = $fieldName; } $request["filters"] = array('awarded' => true, 'contestID' => $contestID); if (!$_SESSION["isAdmin"]) { $request["filters"]["userID"] = $_SESSION["userID"]; } $result = selectRows($db, $request); $awarded = $result['items']; if (!count($awarded)) { return; } // we hope that there will be no collision in a serie of generated codes $query = "INSERT ignore INTO `contestant` (`ID`, `algoreaCode`) values "; $first = true; foreach ($awarded as $contestant) { if (!$first) { $query = $query . ', '; } $first = false; $query = $query . '(' . $contestant->ID . ', \'' . genAccessCode($db) . '\')'; } $query = $query . ' on duplicate key update `algoreaCode` = values(`algoreaCode`)'; $db->exec($query); }
function createTeam($db, $contestants) { global $tinyOrm, $config; if ($_SESSION["groupClosed"]) { error_log("Hack attempt ? trying to create team on closed group " . $_SESSION["groupID"]); echo json_encode(array("success" => false, "message" => "Groupe fermé")); return; } if (isset($_SESSION["userCode"])) { $password = $_SESSION["userCode"]; } else { $password = genAccessCode($db); } $stmt = $db->prepare("INSERT INTO `team` (`groupID`, `password`) VALUES (?, ?)"); $stmt->execute(array($_SESSION["groupID"], $password)); $teamID = $db->lastInsertId(); if ($config->db->use == 'dynamoDB') { try { $tinyOrm->insert('team', array('ID' => $teamID, 'groupID' => $_SESSION["groupID"], 'password' => $password)); } catch (\Aws\DynamoDb\Exception $e) { error_log($e->getMessage . " - " . $e->getCode()); error_log('DynamoDB error creating team, teamID: ' . $teamID); } } $stmt = $db->prepare("UPDATE `group` SET `startTime` = NOW() WHERE `group`.`ID` = ? AND `startTime` IS NULL"); $stmt->execute(array($_SESSION["groupID"])); $stmt = $db->prepare("UPDATE `group` SET `nbTeamsEffective` = `nbTeamsEffective` + 1, `nbStudentsEffective` = `nbStudentsEffective` + ? WHERE `ID` = ?"); $stmt->execute(array(count($contestants), $_SESSION["groupID"])); $_SESSION["teamID"] = $teamID; $_SESSION["teamPassword"] = $password; foreach ($contestants as $contestant) { if (!isset($contestant["grade"])) { $contestant["grade"] = -2; } list($contestant["firstName"], $contestant["lastName"], $saniValid, $trash) = DataSanitizer::formatUserNames($contestant["firstName"], $contestant["lastName"]); $stmt = $db->prepare("\n INSERT INTO `contestant` (`lastName`, `firstName`, `genre`, `grade`, `teamID`, `cached_schoolID`, `saniValid`) \n VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute(array($contestant["lastName"], $contestant["firstName"], $contestant["genre"], $contestant["grade"], $teamID, $_SESSION["schoolID"], $saniValid)); } echo json_encode((object) array("success" => true, "teamID" => $teamID, "password" => $password)); }
function checkRequestGroup($db, &$request, &$record, $operation, &$roles) { // Generated fields if ($operation === "insert") { $record["code"] = genAccessCode($db); $record["password"] = genAccessCode($db); $roles[] = "generator"; } /* if ((!isset($record["year"])) || (!isset($record["grade"]))) { error_log("year or level missing when updating group"); return false; } */ //$level = getLevel($record["grade"]); /* $contestID = getContest($db, $record["year"], $level); $record["contestID"] = $contestID; unset($record["year"]); */ if (!$_SESSION["isAdmin"]) { $record["userID"] = $_SESSION["userID"]; } else { die("You cannot create groups as Administrator"); } // Filters if (!$_SESSION["isAdmin"]) { if ($operation == "insert") { $request["filters"]["schoolID"] = $record["schoolID"]; } $request["filters"]["userID"] = $_SESSION["userID"]; $request["filters"]["statusNotHidden"] = true; $request["filters"]["checkOfficial"] = true; } // This can't be done through a standard filter yet if ($operation === "update" && groupContestChanged($db, $record["ID"], $record["contestID"])) { $message = "can't change the contest of a group that has been used"; // TODO : translate error_log($message); echo json_encode(array("success" => false, "message" => $message)); return false; } return true; }