public function submit($song_id = null)
 {
     $this->require_login();
     if (!$song_id) {
         $this->content_view = "scores/submit";
         $this->data['songs'] = Ranked_file::get_all_charts();
         $this->data['packs'] = Pack::all(array('order' => 'name asc'));
     } else {
         if (Ranked_file::count(array('conditions' => array('id = ?', $song_id))) != 1) {
             redirect('scores/submit');
         } else {
             if ($_POST) {
                 $song = Ranked_file::find($song_id);
                 $this->data['song'] = $song;
                 $total_notes = $this->input->post('marvelous_count') + $this->input->post('perfect_count') + $this->input->post('great_count') + $this->input->post('good_count') + $this->input->post('boo_count') + $this->input->post('miss_count');
                 $total_holds = $this->input->post('ok_count');
                 if ($total_notes > $song->taps || $total_holds > $song->holds) {
                     $this->content_view = "scores/confirm";
                     $this->data['error_nc'] = true;
                 } elseif ($this->form_validation->run('submit_score') == FALSE) {
                     $this->content_view = "scores/confirm";
                     $this->data['error'] = true;
                 } else {
                     $attributes = array('user_id' => $this->session->userdata('user_id'), 'file_id' => $song_id, 'date_achieved' => $this->input->post('score_achieved'), 'marvelous_count' => $this->input->post('marvelous_count'), 'perfect_count' => $this->input->post('perfect_count'), 'great_count' => $this->input->post('great_count'), 'good_count' => $this->input->post('good_count'), 'boo_count' => $this->input->post('boo_count'), 'miss_count' => $this->input->post('miss_count'), 'ok_count' => $this->input->post('ok_count'), 'mines_hit' => $this->input->post('mines_hit'), 'screenshot_url' => $this->input->post('screenshot_url'));
                     $new_score = new User_score($attributes);
                     $new_score->save();
                     // Check if score is below AA
                     $max_dp_max = $song->dance_points;
                     $max_dp_achieved = $new_score->marvelous_count * 2 + $new_score->perfect_count * 2 + $new_score->great_count * 1 + $new_score->boo_count * -4 + $new_score->miss_count * -8 + $new_score->ok_count * 6 + $new_score->mines_hit * -8;
                     $max_dp_percent = $max_dp_achieved / $max_dp_max * 100;
                     if ($max_dp_percent < 93) {
                         $new_score->status = "below_aa";
                         $new_score->save();
                     }
                     // Check if score is in top 10% of all scores
                     $this->data['score_pending'] = false;
                     $percentile = User_score::get_top_10_percentile();
                     if ($song->difficulty_score > $percentile) {
                         $new_score->status = "pending";
                         $new_score->was_pending = 1;
                         $new_score->save();
                         $this->data['score_pending'] = true;
                     }
                     $this->content_view = "scores/success";
                 }
             } else {
                 $this->data['song'] = Ranked_file::find($song_id);
                 $this->content_view = "scores/confirm";
                 $this->data['error'] = false;
             }
         }
     }
 }
 public function charts_json()
 {
     $charts = Ranked_file::get_all_charts();
     $json_ready = array();
     $json_ready['data'] = array();
     foreach ($charts as $song) {
         $data = array();
         // Fill out the data array.
         if ($this->session->userdata('user_level') >= 2) {
             $data[0] = "<span class=\"label warning\"><a href=\"/mod/edit_chart/{$song->id}\">Edit</a></span> <a href=\"/charts/view/{$song->id}\">{$song->title}</a>";
         } else {
             $data[0] = "<a href=\"/charts/view/{$song->id}\">{$song->title}</a>";
         }
         $data[1] = $song->artist;
         $data[2] = number_format($song->rate, 1);
         $data[3] = number_format($song->difficulty_score, 2);
         $data[4] = "<a href=\"/packs/view/{$song->pack_id}\">{$song->pack_name}</a>" . (!empty($song->pack_abbr) ? " (" . $song->pack_abbr . ")" : "");
         $data[5] = gmdate("i:s", $song->length);
         $typestring = "";
         if ($song->stamina_file) {
             $typestring .= "Stamina, ";
         }
         $typestring .= ucwords($song->file_type);
         $data[6] = $typestring;
         if ($this->session->userdata('user_level')) {
             $data[7] = "<span class=\"label primary\"><a href=\"/scores/submit/{$song->id}\">Submit Score</a></span>";
         } else {
             $data[7] = null;
         }
         array_push($json_ready['data'], $data);
     }
     $this->data['charts'] = $json_ready;
     $this->output->set_content_type('application/json');
 }
 public function index()
 {
     $this->data['subtitle'] = "Ranked Charts List";
     $this->data['songs'] = Ranked_file::get_all_charts();
     $this->content_view = "charts/list";
 }