public function postQuestion($id) { $note = 0; $qcm_student = []; $nb_choices = Choice::where('question_id', $id)->count(); for ($i = 0; $i < $nb_choices; $i++) { array_push($qcm_student, Input::get("status{$i}")); } $qcm_teacher = []; $choices = Choice::where('question_id', $id)->get(); foreach ($choices as $choice) { array_push($qcm_teacher, $choice->status); } for ($i = 0; $i < count($qcm_teacher); $i++) { if ($qcm_teacher[$i] == $qcm_student[$i]) { $note++; } } $test_score = DB::table('scores')->where('user_id', '=', Auth::user()->id)->where('question_id', '=', Question::where('id', $id)->first()->id)->first(); if (!is_null($test_score)) { //echo "fait"; Session::flash('message_error', 'Vous avez déjà fait ce QCM'); return Redirect::to('/student'); } else { $score = new Score(); $score->user_id = Auth::user()->id; $score->question_id = Question::where('id', $id)->first()->id; $score->status_question = 'fait'; $score->note = $note; $score->save(); Session::flash('message_success', 'Votre score a bien été enregistré'); return Redirect::to('/student'); } return Redirect::to('/student'); }
public static function store() { $params = $_POST; $rain = isset($_POST['rain']) && $_POST['rain'] ? "1" : "0"; // checked=1, unchecked=0 $wet_no_rain = isset($_POST['wet_no_rain']) && $_POST['wet_no_rain'] ? "1" : "0"; // checked=1, unchecked=0 $windy = isset($_POST['windy']) && $_POST['windy'] ? "1" : "0"; // checked=1, unchecked=0 $variant = isset($_POST['variant']) && $_POST['variant'] ? "1" : "0"; // checked=1, unchecked=0 $dark = isset($_POST['dark']) && $_POST['dark'] ? "1" : "0"; // checked=1, unchecked=0 $led = isset($_POST['led']) && $_POST['led'] ? "1" : "0"; // checked=1, unchecked=0 $snow = isset($_POST['snow']) && $_POST['snow'] ? "1" : "0"; // checked=1, unchecked=0 $date = $_POST['date']; $time = $_POST['time']; $comment = $_POST['comment']; $courseid = $_POST['courseid']; $gamedate = $date . " " . $time . ":00"; $game = new Game(array('courseid' => $courseid, 'gamedate' => $gamedate, 'comment' => $comment, 'rain' => $rain, 'wet_no_rain' => $wet_no_rain, 'windy' => $windy, 'variant' => $variant, 'dark' => $dark, 'led' => $led, 'snow' => $snow)); $errors = $game->errors(); $course = Course::find($courseid); $scores = array(); // When implementing multiple players per game, cycle through playerid's here $playerid = $_POST['playerid']; foreach ($course->holes as $hole) { $stroke = $_POST['hole' . $hole->hole_num]; // 'holeN' will be something like 'playername-holeN' $ob = $_POST['obhole' . $hole->hole_num]; $score = new Score(array('holeid' => $hole->holeid, 'playerid' => $playerid, 'stroke' => $stroke, 'ob' => $ob)); $errors = array_merge($errors, $score->errors()); $scores[] = $score; } if (count($errors) == 0) { // Game and scores were all valid $gameid = $game->save(); foreach ($scores as $score) { $score->gameid = $gameid; $score->save(); } Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset lisätty.')); } else { View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course)); } }
/** * Checks if round is ready to be saved. If not, displays new round creation page with information already inputted. If yes, attempts to store new round and related scores. */ public static function store() { $player = self::get_user_logged_in(); if (!$player) { View::make('player/login.html', array('error' => 'Vain kirjautuneet käyttäjät voivat lisätä ratoja.')); } $params = $_POST; if ($params['numberOfPlayers'] != $params['numberOfPlayers_orig'] or $params['course'] != $params['course_orig']) { // If course or number of players selection has changed we need to display create page again with new information. $playerScores = array(); $numberOfHoles = Hole::count_holes($params['course_orig']); for ($i = 1; $i <= $params['numberOfPlayers_orig']; $i++) { $holeScores = array(); for ($j = 1; $j <= $numberOfHoles; $j++) { $holeScores[] = $params['p' . $i . '_h' . $j]; } $playerScores[] = array('player' => Player::find($params['player_' . $i]), 'holes' => $holeScores); } $players = Player::all(); $course = Course::find($params['course']); $courses = Course::all(); $holes = Hole::find_by_course($course->id); $played = $params['played']; View::make('round/new_continued.html', array('numberOfPlayers' => $params['numberOfPlayers'], 'players' => $players, 'course' => $course, 'holes' => $holes, 'courses' => $courses, 'playerScores' => $playerScores, 'played' => $played)); } else { $attributes = array('courseId' => $params['course'], 'played' => $params['played'], 'addedBy' => self::get_user_logged_in()->id); $playerScores = array(); $numberOfHoles = Hole::count_holes($params['course_orig']); $round = new Round($attributes); $errors = $round->errors(); if (count($errors) == 0) { $round->save(); for ($i = 1; $i <= $params['numberOfPlayers_orig']; $i++) { $holeScores = array(); for ($j = 1; $j <= $numberOfHoles; $j++) { $hole = Hole::find_by_course_and_holenumber($params['course_orig'], $j); $holeScores[] = array('throws' => $params['p' . $i . '_h' . $j], 'holeId' => $hole->id); } $score = new Score(array('playerId' => $params['player_' . $i], 'roundId' => $round->id, 'scores' => $holeScores)); $score->save(); } Redirect::to('/round/' . $round->id, array('message' => 'Kierros lisätty.')); } else { View::make('round/new.html', array('errors' => $errors, 'attributes' => $attributes)); } } }
/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aSeries !== null) { if ($this->aSeries->isModified() || $this->aSeries->isNew()) { $affectedRows += $this->aSeries->save($con); } $this->setSeries($this->aSeries); } if ($this->aReview !== null) { if ($this->aReview->isModified() || $this->aReview->isNew()) { $affectedRows += $this->aReview->save($con); } $this->setReview($this->aReview); } if ($this->aScore !== null) { if ($this->aScore->isModified() || $this->aScore->isNew()) { $affectedRows += $this->aScore->save($con); } $this->setScore($this->aScore); } if ($this->isNew()) { $this->modifiedColumns[] = ModelPeer::ID; } // If this object has been modified, then save it to the database. if ($this->isModified()) { if ($this->isNew()) { $pk = ModelPeer::doInsert($this, $con); $affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which // should always be true here (even though technically // BasePeer::doInsert() can insert multiple rows). $this->setId($pk); //[IMV] update autoincrement primary key $this->setNew(false); } else { $affectedRows += ModelPeer::doUpdate($this, $con); } $this->resetModified(); // [HL] After being saved an object is no longer 'modified' } if ($this->collConfigs !== null) { foreach ($this->collConfigs as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } if ($this->collReviews !== null) { foreach ($this->collReviews as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } if ($this->collScores !== null) { foreach ($this->collScores as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } if ($this->collUserMetas !== null) { foreach ($this->collUserMetas as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } $this->alreadyInSave = false; } return $affectedRows; }
exit; } $record->updated_at = new DateTime(); $record->score = $score; if ($nickname) { $record->nickname = $nickname; } if ($record->save()) { echo json_encode(array('success' => true, 'score' => $score, 'id' => $record->id)); exit; } else { echo json_encode(array('success' => false)); exit; } } else { $record = new Score(); $record->created_at = new DateTime(); $record->updated_at = new DateTime(); $record->user = isset($user['unique']) ? $user['unique'] : null; $record->nickname = $nickname ? $nickname : 'Yolo!'; $record->score = $score; if ($record->save()) { echo json_encode(array('success' => true, 'score' => $score, 'id' => $record->id)); exit; } else { echo json_encode(array('success' => false)); exit; } } }); $app->run();
public static function store() { $params = $_POST; $rain = isset($_POST['rain']) && $_POST['rain'] ? "1" : "0"; // checked=1, unchecked=0 $wet_no_rain = isset($_POST['wet_no_rain']) && $_POST['wet_no_rain'] ? "1" : "0"; // checked=1, unchecked=0 $windy = isset($_POST['windy']) && $_POST['windy'] ? "1" : "0"; // checked=1, unchecked=0 $variant = isset($_POST['variant']) && $_POST['variant'] ? "1" : "0"; // checked=1, unchecked=0 $dark = isset($_POST['dark']) && $_POST['dark'] ? "1" : "0"; // checked=1, unchecked=0 $led = isset($_POST['led']) && $_POST['led'] ? "1" : "0"; // checked=1, unchecked=0 $snow = isset($_POST['snow']) && $_POST['snow'] ? "1" : "0"; // checked=1, unchecked=0 $doubles = isset($_POST['doubles']) && $_POST['doubles'] ? "1" : "0"; // checked=1, unchecked=0 $temp = $_POST['temp'] != "" ? $_POST['temp'] : null; // temperature can be null (or 0!) $date = $_POST['date']; $time = $_POST['time']; $comment = $_POST['comment']; $courseid = $_POST['courseid']; $gamedate = $date . " " . $time . ":00"; $game = new Game(array('courseid' => $courseid, 'gamedate' => $gamedate, 'comment' => $comment, 'rain' => $rain, 'wet_no_rain' => $wet_no_rain, 'windy' => $windy, 'variant' => $variant, 'dark' => $dark, 'led' => $led, 'snow' => $snow, 'doubles' => $doubles, 'temp' => $temp)); $errors = $game->errors(); $course = Course::find($courseid); if (isset($_FILES['csv']['tmp_name'])) { // Scores will be read from a CSV file. $tmpName = $_FILES['csv']['tmp_name']; if (!empty($tmpName)) { $csvAsArray = array_map('str_getcsv', file($tmpName)); } else { $errors[] = "Valitse CSV-tiedosto."; } if (count($errors) == 0) { // Game was valid $gameid = $game->save(); // Read and save scores $score_errors = CSVScoreProcessor::process($csvAsArray, $gameid, $course); if (count($score_errors) == 0) { // Scores were valid as well // Clear cached pages Cache::clear(); Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset lisätty.')); } else { // Scores had errors, nothing was saved $errors = array_merge($errors, $score_errors); View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course)); } } else { View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course)); } } else { // Scores will be read from input forms. $scores = array(); // Game's players $players = array(); foreach (Player::all() as $player) { if (isset($_POST['player' . $player->playerid])) { $players[] = $player; } } // Cycle through players foreach ($players as $player) { $legal_index = 'legal-player' . $player->playerid; $legal = isset($_POST[$legal_index]) && $_POST[$legal_index] ? "1" : "0"; // checked=1, unchecked=0 foreach ($course->holes as $hole) { // inputs are in format 'player1-hole1' $stroke = $_POST['player' . $player->playerid . '-hole' . $hole->hole_num]; $ob = $_POST['player' . $player->playerid . '-obhole' . $hole->hole_num]; $score = new Score(array('holeid' => $hole->holeid, 'playerid' => $player->playerid, 'stroke' => $stroke, 'ob' => $ob, 'legal' => $legal)); $errors = array_merge($errors, $score->errors()); $scores[] = $score; } } if (count($errors) == 0) { // Game and scores were all valid $gameid = $game->save(); foreach ($scores as $score) { $score->gameid = $gameid; $score->save(); } // Clear cached pages Cache::clear(); Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset lisätty.')); } else { View::make('game/new.html', array('errors' => $errors, 'attributes' => $params, 'course' => $course, 'players' => $players)); } } }