예제 #1
0
	public function index($cluster_id = 0)
	{
		// Cacheable Controller
		$this->is_cachable = TRUE;
		
		$this->template->header->this_page = 'reports';
		$this->template->content = new View('reports');
		$this->themes->js = new View('reports_js');

		// Get locale
		$l = Kohana::config('locale.language.0');

		$db = new Database;

		// Get incident_ids if we are to filter by category
		$allowed_ids = array();
		
		if (isset($_GET['c']) AND (int) $_GET['c']!=0)
		{
			$category_id = (int) $_GET['c'];
			$query = 'SELECT ic.incident_id AS incident_id FROM '.$this->table_prefix.'incident_category AS ic INNER JOIN '.$this->table_prefix.'category AS c ON (ic.category_id = c.id)  WHERE c.id='.$category_id.' OR c.parent_id='.$category_id.';';
			$query = $db->query($query);

			if ($query->count())
			{
				foreach ( $query as $items )
				{
					$allowed_ids[] = $items->incident_id;
				}
			}
			else
			{
				$allowed_ids[] = "-1";
			}
		}

		// Get location_ids if we are to filter by location
		$location_ids = array();

		// Break apart location variables, if necessary
		$southwest = array();
		if (isset($_GET['sw']))
		{
			$southwest = explode(",",$_GET['sw']);
		}

		$northeast = array();
		if (isset($_GET['ne']))
		{
			$northeast = explode(",",$_GET['ne']);
		}

		if ( count($southwest) == 2 AND count($northeast) == 2 )
		{
			$lon_min = (float) $southwest[0];
			$lon_max = (float) $northeast[0];
			$lat_min = (float) $southwest[1];
			$lat_max = (float) $northeast[1];

			$query = 'SELECT id FROM '.$this->table_prefix.'location WHERE latitude >='.$lat_min.' AND latitude <='.$lat_max.' AND longitude >='.$lon_min.' AND longitude <='.$lon_max;

			$query = $db->query($query);

			foreach ( $query as $items )
			{
				$location_ids[] =  $items->id;
			}
		}
		elseif (isset($_GET['l']) AND !empty($_GET['l']) AND $_GET['l']!=0)
		{
			$location_ids[] = (int) $_GET['l'];
		}

		// Get the count
		$incident_id_in = '1=1';
		if (count($allowed_ids) > 0)
		{
			$incident_id_in = 'id IN ('.implode(',',$allowed_ids).')';
		}

		$location_id_in = '1=1';
		if (count($location_ids) > 0)
		{
			$location_id_in = 'location_id IN ('.implode(',',$location_ids).')';
		}

		// Pagination
		$pagination = new Pagination(array(
				'query_string' => 'page',
				'items_per_page' => (int) Kohana::config('settings.items_per_page'),
				'total_items' => ORM::factory("incident")
					->where("incident_active", 1)
					->where($location_id_in)
					->where($incident_id_in)
					->count_all()
				));

		// Reports
		$incidents = ORM::factory("incident")
			->where("incident_active", 1)
			->where($location_id_in)
			->where($incident_id_in)
			->orderby("incident_date", "desc")
			->find_all((int) Kohana::config('settings.items_per_page'), $pagination->sql_offset);

		// Swap out category titles with their proper localizations using an array (cleaner way to do this?)

		$localized_categories = array();
		foreach ($incidents as $incident)
		{
			foreach ($incident->category AS $category)
			{
				$ct = (string)$category->category_title;
				if( ! isset($localized_categories[$ct]))
				{
					$translated_title = Category_Lang_Model::category_title($category->id,$l);
					$localized_categories[$ct] = $category->category_title;
					if($translated_title)
					{
						$localized_categories[$ct] = $translated_title;
					}
				}
			}
		}

		$this->template->content->localized_categories = $localized_categories;

		$this->template->content->incidents = $incidents;

		//Set default as not showing pagination. Will change below if necessary.
		$this->template->content->pagination = "";

		// Pagination and Total Num of Report Stats
		if ($pagination->total_items == 1)
		{
			$plural = "";
		}
		else
		{
			$plural = "s";
		}

		if ($pagination->total_items > 0)
		{
			$current_page = ($pagination->sql_offset/ (int) Kohana::config('settings.items_per_page')) + 1;
			$total_pages = ceil($pagination->total_items/ (int) Kohana::config('settings.items_per_page'));

			if ($total_pages > 1)
			{ // If we want to show pagination
				$this->template->content->pagination_stats = Kohana::lang('ui_admin.showing_page').' '.$current_page.' '.Kohana::lang('ui_admin.of').' '.$total_pages.' '.Kohana::lang('ui_admin.pages');

				$this->template->content->pagination = $pagination;
			}
			else
			{ // If we don't want to show pagination
				$this->template->content->pagination_stats = $pagination->total_items.' '.Kohana::lang('ui_admin.reports');
			}
		}
		else
		{
			$this->template->content->pagination_stats = '('.$pagination->total_items.' report'.$plural.')';
		}

		// Category Title, if Category ID available

		$category_id = ( isset($_GET['c']) AND !empty($_GET['c']) )
			? $_GET['c'] : "0";
		$category = ORM::factory('category')
			->find($category_id);

		if($category->loaded)
		{
			$translated_title = Category_Lang_Model::category_title($category_id,$l);
			if($translated_title)
			{
				$this->template->content->category_title = $translated_title;
			}else{
				$this->template->content->category_title = $category->category_title;
			}
		}else{
			$this->template->content->category_title = "";
		}

		// Collect report stats
		$this->template->content->report_stats = new View('reports_stats');
		// Total Reports

		$total_reports = Incident_Model::get_total_reports(TRUE);

		// Average Reports Per Day
		$oldest_timestamp = Incident_Model::get_oldest_report_timestamp();

		// Round the number of days up to the nearest full day
		$days_since = ceil((time() - $oldest_timestamp) / 86400);
		if ($days_since < 1) {
			$avg_reports_per_day = $total_reports;
		}else{
			$avg_reports_per_day = round(($total_reports / $days_since),2);
		}

		// Percent Verified
		$total_verified = Incident_Model::get_total_reports_by_verified(true);
		$percent_verified = ($total_reports == 0) ? '-' : round((($total_verified / $total_reports) * 100),2).'%';

		$this->template->content->report_stats->total_reports = $total_reports;
		$this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
		$this->template->content->report_stats->percent_verified = $percent_verified;

		$this->template->header->header_block = $this->themes->header_block();
	}
예제 #2
0
 /** Deletes all reports from the database **/
 public function deleteall()
 {
     // Only superadmins should be able to do this...
     if (!$this->auth->has_permission("delete_all_reports")) {
         url::redirect(url::site() . 'admin/dashboard');
     }
     if (isset($_POST["confirm_delete_all"]) && $_POST["confirm_delete_all"] == 1) {
         $table_prefix = Kohana::config('database.default.table_prefix');
         Database::instance()->query("UPDATE `" . $table_prefix . "message` SET `incident_id` = 0;");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "media`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "location`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "comment`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "rating`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "form_response`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "incident_person`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "incident_lang`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "incident_category`");
         Database::instance()->query("TRUNCATE TABLE `" . $table_prefix . "incident`");
     }
     $this->template->content = new View('admin/reports/delete_all');
     $this->template->content->report_count = Incident_Model::get_total_reports();
     $this->themes->js = new View('admin/reports/delete_all_js');
 }
예제 #3
0
 /**
  * Displays all reports.
  */
 public function index()
 {
     // Cacheable Controller
     $this->is_cachable = TRUE;
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports');
     $this->themes->js = new View('reports_js');
     // Store any exisitng URL parameters
     $this->themes->js->url_params = json_encode($_GET);
     // Enable the map
     $this->themes->map_enabled = TRUE;
     // Set the latitude and longitude
     $this->themes->js->latitude = Kohana::config('settings.default_lat');
     $this->themes->js->longitude = Kohana::config('settings.default_lon');
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     // Load the alert radius view
     $alert_radius_view = new View('alert_radius_view');
     $alert_radius_view->show_usage_info = FALSE;
     $alert_radius_view->enable_find_location = FALSE;
     $alert_radius_view->css_class = "rb_location-radius";
     $this->template->content->alert_radius_view = $alert_radius_view;
     // Get locale
     $l = Kohana::config('locale.language.0');
     // Get the report listing view
     $report_listing_view = $this->_get_report_listing_view($l);
     // Set the view
     $this->template->content->report_listing_view = $report_listing_view;
     // Load the category
     $category_id = (isset($_GET['c']) and intval($_GET['c']) > 0) ? intval($_GET['c']) : 0;
     $category = ORM::factory('category', $category_id);
     if ($category->loaded) {
         $translated_title = Category_Lang_Model::category_title($category_id, $l);
         // Set the category title
         $this->template->content->category_title = $translated_title ? $translated_title : $category->category_title;
     } else {
         $this->template->content->category_title = "";
     }
     // Collect report stats
     $this->template->content->report_stats = new View('reports_stats');
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Get the date of the oldest report
     $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     // Get the date of the latest report
     $latest_timestamp = Incident_Model::get_latest_report_timestamp();
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     $avg_reports_per_day = $days_since < 1 ? $total_reports : round($total_reports / $days_since, 2);
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(TRUE);
     $percent_verified = $total_reports == 0 ? '-' : round($total_verified / $total_reports * 100, 2) . '%';
     // Category tree view
     $this->template->content->category_tree_view = category::get_category_tree_view();
     // Additional view content
     $this->template->content->oldest_timestamp = $oldest_timestamp;
     $this->template->content->latest_timestamp = $latest_timestamp;
     $this->template->content->report_stats->total_reports = $total_reports;
     $this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->report_stats->percent_verified = $percent_verified;
     $this->template->content->services = Service_Model::get_array();
     $this->template->header->header_block = $this->themes->header_block();
 }
예제 #4
0
 /**
  * Displays all reports.
  */
 public function index()
 {
     // Cacheable Controller
     $this->is_cachable = TRUE;
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports/main');
     $this->themes->js = new View('reports/reports_js');
     $this->template->header->page_title .= Kohana::lang('ui_main.reports') . Kohana::config('settings.title_delimiter');
     // Store any exisitng URL parameters
     $this->themes->js->url_params = json_encode($_GET);
     // Enable the map
     $this->themes->map_enabled = TRUE;
     // Set the latitude and longitude
     $this->themes->js->latitude = Kohana::config('settings.default_lat');
     $this->themes->js->longitude = Kohana::config('settings.default_lon');
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     // Get Default Color
     $this->themes->js->default_map_all = $this->template->content->default_map_all = Kohana::config('settings.default_map_all');
     // Get default icon
     $this->themes->js->default_map_all_icon = $this->template->content->default_map_all_icon = '';
     if (Kohana::config('settings.default_map_all_icon_id')) {
         $icon_object = ORM::factory('media')->find(Kohana::config('settings.default_map_all_icon_id'));
         $this->themes->js->default_map_all_icon = $this->template->content->default_map_all_icon = Kohana::config('upload.relative_directory') . "/" . $icon_object->media_thumb;
     }
     // Load the alert radius view
     $alert_radius_view = new View('alerts/radius');
     $alert_radius_view->show_usage_info = FALSE;
     $alert_radius_view->enable_find_location = TRUE;
     $alert_radius_view->css_class = "rb_location-radius";
     $this->template->content->alert_radius_view = $alert_radius_view;
     // Get locale
     $l = Kohana::config('locale.language.0');
     // Get the report listing view
     $report_listing_view = $this->_get_report_listing_view($l);
     // Set the view
     $this->template->content->report_listing_view = $report_listing_view;
     // Load the category
     $category_id = (isset($_GET['c']) and intval($_GET['c']) > 0) ? intval($_GET['c']) : 0;
     $category = ORM::factory('category', $category_id);
     if ($category->loaded) {
         // Set the category title
         $this->template->content->category_title = Category_Lang_Model::category_title($category_id, $l);
     } else {
         $this->template->content->category_title = "";
     }
     // Collect report stats
     $this->template->content->report_stats = new View('reports/stats');
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Get the date of the oldest report
     if (isset($_GET['s']) and !empty($_GET['s']) and intval($_GET['s']) > 0) {
         $oldest_timestamp = intval($_GET['s']);
     } else {
         $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     }
     // Get the date of the latest report
     if (isset($_GET['e']) and !empty($_GET['e']) and intval($_GET['e']) > 0) {
         $latest_timestamp = intval($_GET['e']);
     } else {
         $latest_timestamp = Incident_Model::get_latest_report_timestamp();
     }
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     $avg_reports_per_day = $days_since < 1 ? $total_reports : round($total_reports / $days_since, 2);
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(TRUE);
     $percent_verified = $total_reports == 0 ? '-' : round($total_verified / $total_reports * 100, 2) . '%';
     // Category tree view
     $this->template->content->category_tree_view = category::get_category_tree_view();
     // Additional view content
     $this->template->content->custom_forms_filter = new View('reports/submit_custom_forms');
     $this->template->content->custom_forms_filter->disp_custom_fields = customforms::get_custom_form_fields();
     $this->template->content->custom_forms_filter->search_form = TRUE;
     $this->template->content->oldest_timestamp = $oldest_timestamp;
     $this->template->content->latest_timestamp = $latest_timestamp;
     $this->template->content->report_stats->total_reports = $total_reports;
     $this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->report_stats->percent_verified = $percent_verified;
     $this->template->content->services = Service_Model::get_array();
 }
예제 #5
0
 /**
  * Displays all reports.
  */
 public function index($cluster_id = 0)
 {
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports');
     // Filter By Category
     $category_filter = isset($_GET['c']) && !empty($_GET['c']) ? "category_id = " . $_GET['c'] : " 1=1 ";
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page'), 'total_items' => ORM::factory('incident')->join('incident_category', 'incident.id', 'incident_category.incident_id')->where('incident_active', '1')->where($category_filter)->count_all()));
     $incidents = ORM::factory('incident')->select('DISTINCT incident.*')->join('incident_category', 'incident.id', 'incident_category.incident_id')->where('incident_active', '1')->where($category_filter)->groupby('incident.id')->orderby('incident_date', 'desc')->find_all((int) Kohana::config('settings.items_per_page'), $pagination->sql_offset);
     $this->template->content->incidents = $incidents;
     //Set default as not showing pagination. Will change below if necessary.
     $this->template->content->pagination = '';
     // Pagination and Total Num of Report Stats
     if ($pagination->total_items == 1) {
         $plural = '';
     } else {
         $plural = 's';
     }
     if ($pagination->total_items > 0) {
         $current_page = $pagination->sql_offset / (int) Kohana::config('settings.items_per_page') + 1;
         $total_pages = ceil($pagination->total_items / (int) Kohana::config('settings.items_per_page'));
         if ($total_pages > 1) {
             // If we want to show pagination
             $this->template->content->pagination_stats = Kohana::lang('ui_admin.showing_page') . ' ' . $current_page . ' ' . Kohana::lang('ui_admin.of') . ' ' . $total_pages . ' ' . Kohana::lang('ui_admin.pages');
             $this->template->content->pagination = $pagination;
         } else {
             // If we don't want to show pagination
             $this->template->content->pagination_stats = $pagination->total_items . ' ' . Kohana::lang('ui_admin.reports');
         }
     } else {
         $this->template->content->pagination_stats = '(' . $pagination->total_items . ' report' . $plural . ')';
     }
     $icon_html = array();
     $icon_html[1] = "<img src=\"" . url::base() . "media/img/image.png\">";
     //image
     $icon_html[2] = "<img src=\"" . url::base() . "media/img/video.png\">";
     //video
     $icon_html[3] = "";
     //audio
     $icon_html[4] = "";
     //news
     $icon_html[5] = "";
     //podcast
     //Populate media icon array
     $this->template->content->media_icons = array();
     foreach ($incidents as $incident) {
         $incident_id = $incident->id;
         if (ORM::factory('media')->where('incident_id', $incident_id)->count_all() > 0) {
             $medias = ORM::factory('media')->where('incident_id', $incident_id)->find_all();
             //Modifying a tmp var prevents Kohona from throwing an error
             $tmp = $this->template->content->media_icons;
             $tmp[$incident_id] = '';
             foreach ($medias as $media) {
                 $tmp[$incident_id] .= $icon_html[$media->media_type];
                 $this->template->content->media_icons = $tmp;
             }
         }
     }
     // Category Title, if Category ID available
     $category_id = isset($_GET['c']) && !empty($_GET['c']) ? $_GET['c'] : "0";
     $category = ORM::factory('category')->find($category_id);
     $this->template->content->category_title = $category->loaded ? $category->category_title : "";
     // Collect report stats
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Average Reports Per Day
     $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     $avg_reports_per_day = round($total_reports / $days_since, 2);
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(true);
     $percent_verified = round($total_verified / $total_reports * 100, 2) . '%';
     $this->template->content->total_reports = $total_reports;
     $this->template->content->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->percent_verified = $percent_verified;
 }
예제 #6
0
	public function index($cluster_id = 0)
	{
		// Cacheable Controller
		$this->is_cachable = TRUE;
		$this->template->header->this_page = 'reports';
		$this->template->content = new View('reports');
		$this->themes->js = new View('reports_js');
		// Get locale
		$l = Kohana::config('locale.language.0');
		$this->template->content->area_name = "";
		$this->template->content->disp_distance = "";
		//FORMのhiddenタグ用パラメータ初期化と代入
		if(isset($_SESSION["locale"])){
			$_GET["l"] = $_SESSION["locale"];
		}

		// 引き回すGETパラメータのテンプレートへの引き渡し
		$this->template->content->keyword = valid::initGetVal('keyword',"text");
		$this->template->content->address = valid::initGetVal('address',"text");
		$this->template->content->distance = valid::initGetVal('distance',"number");
		$this->template->content->c = valid::initGetVal('c',"number");
		$this->template->content->sw = valid::initGetVal('sw',"text");
		$this->template->content->ne = valid::initGetVal('ne',"text");
		$this->template->content->l = valid::initGetVal('l',"natural_numbewr");
		$this->template->content->mode = valid::initGetVal('mode',"text");
		$this->template->content->order = valid::initGetVal('order',"text");

		$db = new Database;

		// Get incident_ids if we are to filter by category
		$allowed_ids = array();
		if (isset($_GET['c']) AND !empty($_GET['c']) AND $_GET['c']!=0)
		{
			$category_id = $db->escape($_GET['c']);
			$query = 'SELECT ic.incident_id AS incident_id FROM '.$this->table_prefix.'incident_category AS ic INNER JOIN '.$this->table_prefix.'category AS c ON (ic.category_id = c.id)  WHERE c.id='.$category_id.' OR c.parent_id='.$category_id.';';
			$query = $db->query($query);
			foreach ( $query as $items )
			{
				$allowed_ids[] = $items->incident_id;
			}
		}

		// Get location_ids if we are to filter by location
		$location_ids = array();

		// Break apart location variables, if necessary
		$southwest = array();
		if (isset($_GET['sw']))
		{
			$southwest = explode(",",$_GET['sw']);
		}

		$northeast = array();
		if (isset($_GET['ne']))
		{
			$northeast = explode(",",$_GET['ne']);
		}
		//指定地区の指定半径内インシデント取得でGoogleMAPAPIで緯度経度を取得できなかった場合DBを取りに行かないようにするためのフラグ
		$dbget_flg = true;
		$this->template->content->choices_flg = false;
		//指定地区の指定半径内インシデント取得処理
		if(isset($_GET["address"]) && trim($_GET["address"]) !== "" && isset($_GET["distance"]) && is_numeric($_GET["distance"]) && $_GET["distance"] > 0){
			$address = urlencode($_GET["address"]);
			// http://www.geocoding.jp/を利用して指定地区名の緯度経度を取得
			$geocoding_url = 'http://www.geocoding.jp/api/?q='.$address;
		    $geo_geocoding = @file_get_contents($geocoding_url,false,stream_context_create(array('http' => array('timeout'=>$this->api_timeout))));
			// APIのエラーハンドリング
			if($geo_geocoding === FALSE){
				if(count($http_response_header) > 0){
					$stat_tokens = explode(' ', $http_response_header[0]);
					switch($stat_tokens[1]){
						case 404:
						// 404 Not found の場合
						break;
						case 500:
						// 500 Internal Server Error の場合
						break;
						default:
						// その他
						break;
					}
				}else{
					// タイムアウトの場合
				}
			}else{
				$geo_geocoding = simplexml_load_string($geo_geocoding);
			}
			//結果の取得とインシデントの取得
			if(isset($geo_geocoding->coordinate)){
				if(isset($geo_geocoding->coordinate->lat) && isset($geo_geocoding->coordinate->lng)){
					$lat_center = $geo_geocoding->coordinate->lat;
					$lon_center = $geo_geocoding->coordinate->lng;
					$area_name = $geo_geocoding->address;
					$_GET["address"] = $this->template->content->area_name = trim($area_name);
					if($_GET["distance"] >= 1){
						$this->template->content->disp_distance = $_GET["distance"]."km";
					}else{
						$this->template->content->disp_distance = ($_GET["distance"]*1000)."m";
					}
					$query = 'SELECT id FROM '.$this->table_prefix.'location WHERE (round(sqrt(pow(('.$this->table_prefix.'location.latitude - '.$lat_center.')/0.0111, 2) + pow(('.$this->table_prefix.'location.longitude - '.$lon_center.')/0.0091, 2)), 1)) <= '.$_GET["distance"];
					$query = $db->query($query);
					foreach ( $query as $items )
					{
						$location_ids[] =  $items->id;
					}
				}
			}elseif(isset($geo_geocoding->choices)){
				$this->template->content->choices_flg = true;
				$dbget_flg = false;
			}
		//TOPの赤丸からのインシデント取得処理
		}elseif ( count($southwest) == 2 AND count($northeast) == 2 ){
			$lon_min = (float) $southwest[0];
			$lon_max = (float) $northeast[0];
			$lat_min = (float) $southwest[1];
			$lat_max = (float) $northeast[1];
			$lon_center = ($lon_min+$lon_max) / 2;
			$lat_center = ($lat_min+$lat_max) / 2;
			$dist1 = (round(sqrt(pow(($lat_max - $lat_center)/0.0111, 2) + pow(($lon_max - $lon_center)/0.0091, 2)), 1));
			$dist2 = (round(sqrt(pow(($lat_min - $lat_center)/0.0111, 2) + pow(($lon_min - $lon_center)/0.0091, 2)), 1));
			// http://www.finds.jp/を利用して中央地点の地名を取得
			$finds_url = 'http://www.finds.jp/ws/rgeocode.php?json&lat='.$lat_center.'&lon='.$lon_center;
		    $geo_finds = @file_get_contents($finds_url,false,stream_context_create(array('http' => array('timeout'=>$this->api_timeout))));
			// APIのエラーハンドリング
			if($geo_finds === FALSE){
				if(count($http_response_header) > 0){
					$stat_tokens = explode(' ', $http_response_header[0]);
					switch($stat_tokens[1]){
						case 404:
						// 404 Not found の場合
						break;
						case 500:
						// 500 Internal Server Error の場合
						break;
						default:
						// その他
						break;
					}
				}else{
					// タイムアウトの場合
				}
			}else{
				$geo_finds = json_decode($geo_finds,true);
			}
			if($geo_finds["status"]===200 || $geo_finds["status"]===201 ||$geo_finds["status"]===202){
				$area_name = str_replace(' ','',$geo_finds["result"]["prefecture"]["pname"].$geo_finds["result"]["municipality"]["mname"]);
				if(isset($area_name) && $area_name !== ""){
					$_GET["address"] = $this->template->content->area_name =  $area_name;
				}
			}
			//指定範囲内のインシデントを取得
			$query = 'SELECT id FROM '.$this->table_prefix.'location WHERE latitude >='.$lat_min.' AND latitude <='.$lat_max.' AND longitude >='.$lon_min.' AND longitude <='.$lon_max;
			$query = $db->query($query);
			foreach ( $query as $items )
			{
				$location_ids[] =  $items->id;
			}
		}elseif (isset($_GET['l']) AND !empty($_GET['l']) AND $_GET['l']!=0){
			$location_ids[] = (int) $_GET['l'];
		}
		// Get the count
		$incident_id_in = '1=1';
		if (count($allowed_ids) > 0)
		{
			$incident_id_in = 'incident.id IN ('.implode(',',$allowed_ids).')';
		}

		$location_id_in = '1=1';
		if (count($location_ids) > 0)
		{
			$location_id_in = 'location_id IN ('.implode(',',$location_ids).')';
		}
		// 検索キーワード取得
		if(isset($_GET["keyword"]) && trim($_GET["keyword"]) !==""){
			$keywords = array();
			$keyword = str_replace(" "," ",$_GET["keyword"]);
			$keywords = explode(" ",$keyword);
		}
		// キーワード検索の初期化(キーワードがない場合のエラー対応)
		$keyword_like = "1=1";
		if(isset($keywords) && count($keywords)){
			$keyword_like = array();
			foreach($keywords as $val){
				$keyword_like[] = "(incident_title like '%".addslashes($val)."%' OR incident_description like '%".addslashes($val)."%')";
			}
			$keyword_like = implode(' AND ',$keyword_like);
		}
		if($dbget_flg){
			// formからの送信の場合
			if(isset($_GET["mode"])){
				// 共通処理としてのページネーション
				// Pagination
				$pagination = new Pagination(array(
						'query_string' => 'page',
						'items_per_page' => (int) Kohana::config('settings.items_per_page'),
						'total_items' => ORM::factory("incident")
							->join($this->table_prefix.'location',$this->table_prefix.'location.id',$this->table_prefix.'incident.location_id',"LEFT OUTER")
							->where("incident_active", 1)
							->where($location_id_in)
							->where($incident_id_in)
							->where($keyword_like)
							->count_all()
						));
					// Reports
					// 中心座標が取得できていれば
					if(isset($lat_center)){
						// ソート順を定義
						if(isset($_GET["order"]) && $_GET["order"]=="new"){
							// 新着順
							$order = array(
								"incident_date"=>"desc",
								'(round(sqrt(pow(('.$this->table_prefix.'location.latitude - '.$lat_center.')/0.0111, 2) + pow(('.$this->table_prefix.'location.longitude - '.$lon_center.')/0.0091, 2)), 1))'=>"asc"
							);
						}elseif(isset($_GET["order"]) && $_GET["order"]=="dist"){
							// 近隣順
							$order = array(
								'(round(sqrt(pow(('.$this->table_prefix.'location.latitude - '.$lat_center.')/0.0111, 2) + pow(('.$this->table_prefix.'location.longitude - '.$lon_center.')/0.0091, 2)), 1))'=>"asc",
								"incident_date"=>"desc"
							);
						}
						// SELECT句に中心点からの距離を追加
						$select = $this->table_prefix.'incident.*,(round(sqrt(pow(('.$this->table_prefix.'location.latitude - '.$lat_center.')/0.0111, 2) + pow(('.$this->table_prefix.'location.longitude - '.$lon_center.')/0.0091, 2)), 1)) as dist';
					}else{
						// 中心座標が取れていなければ新着順で固定
						$order = array(
							"incident_date"=>"desc"
						);
						// SELECT句はincidentsの全レコード
						$select = $this->table_prefix.'incident.*';
					}
					if($_GET["mode"]=="areaorder"){
						$incidents = ORM::factory("incident")
							->select($select,false)
							->join($this->table_prefix.'location',$this->table_prefix.'location.id',$this->table_prefix.'incident.location_id',"LEFT OUTER")
							->where("incident_active", 1)
							->where($location_id_in)
							->where($incident_id_in)
							->where($keyword_like)
							->orderby('(round(sqrt(pow(('.$this->table_prefix.'location.latitude - '.$lat_center.')/0.0111, 2) + pow(('.$this->table_prefix.'location.longitude - '.$lon_center.')/0.0091, 2)), 1))', "asc",false)
							->find_all((int) Kohana::config('settings.items_per_page'), $pagination->sql_offset);
					}elseif($_GET["mode"]=="areasearch"){
						if(isset($_GET["order"]) && isset($_GET["order"])){
							$escape = false;
						}else{
							$escape = true;
						}
						// Reports
						$incidents = ORM::factory("incident")
							->select($select,$escape)
							->join($this->table_prefix.'location',$this->table_prefix.'location.id',$this->table_prefix.'incident.location_id',"LEFT OUTER")
							->where("incident_active", 1)
							->where($location_id_in)
							->where($incident_id_in)
							->where($keyword_like)
							->orderby($order,NULL,$escape)
							->find_all((int) Kohana::config('settings.items_per_page'), $pagination->sql_offset);
					}
			}else{
				// Pagination
				$pagination = new Pagination(array(
						'query_string' => 'page',
						'items_per_page' => (int) Kohana::config('settings.items_per_page'),
						'total_items' => ORM::factory("incident")
							->where("incident_active", 1)
							->where($location_id_in)
							->where($incident_id_in)
							->where($keyword_like)
							->count_all()
						));
				// Reports
				$incidents = ORM::factory("incident")
					->where("incident_active", 1)
					->where($location_id_in)
					->where($incident_id_in)
					->where($keyword_like)
					->orderby("incident_date", "desc")
					->find_all((int) Kohana::config('settings.items_per_page'), $pagination->sql_offset);
			}
		}else{
			$incidents = array();
			$pagination = new Pagination();
		}
		// Swap out category titles with their proper localizations using an array (cleaner way to do this?)

                // $query = 'SELECT id,category_title,category_color FROM category WHERE category_visible = 1 AND category_trusted = 0';
		$query = 'SELECT id,category_title,category_color,category_image_thumb FROM category ORDER BY category_type desc;';
		$query = $db->query($query);
		$category_master = array();
		$localized_categories = array();
		foreach($query as $row){
			$category_master[$row->id]['title'] = $row->category_title; 
			$category_master[$row->id]['color'] = $row->category_color; 
			$category_master[$row->id]['category_image_thumb'] = $row->category_image_thumb; 
			$localized_categories[(string)$row->category_title] = $row->category_title;
			$localized_categories[(string)$row->category_title]['title'] = $row->category_title;
			$localized_categories[(string)$row->category_title]['color'] = $row->category_title;
		}	
		$this->template->content->category_master = $category_master;

		$this->template->content->localized_categories = $localized_categories;

		$this->template->content->incidents = $incidents;

		//Set default as not showing pagination. Will change below if necessary.
		$this->template->content->pagination = "";

		// Pagination and Total Num of Report Stats
		if ($pagination->total_items == 1)
		{
			$plural = "";
		}
		else
		{
			$plural = "s";
		}

		if ($pagination->total_items > 0)
		{
			$current_page = ($pagination->sql_offset/ (int) Kohana::config('settings.items_per_page')) + 1;
			$total_pages = ceil($pagination->total_items/ (int) Kohana::config('settings.items_per_page'));

			if ($total_pages > 1)
			{ // If we want to show pagination
				if((isset($_GET["l"]) && ($_GET["l"] === "ja_JP" || $_GET["l"] === "")) OR (!isset($_GET["l"]))){
					$this->template->content->pagination_stats = "全".$total_pages."中".$current_page.Kohana::lang('ui_admin.showing_page');
				}else{
					$this->template->content->pagination_stats = Kohana::lang('ui_admin.showing_page').' '.$current_page.' '.Kohana::lang('ui_admin.of').' '.$total_pages.' '.Kohana::lang('ui_admin.pages');
				}
				$this->template->content->pagination = $pagination;
			}
			else
			{ // If we don't want to show pagination
				$this->template->content->pagination_stats = $pagination->total_items.' '.Kohana::lang('ui_admin.reports');
			}
		}
		else
		{
			$this->template->content->pagination_stats = '('.$pagination->total_items.' report'.$plural.')';
		}

		// Category Title, if Category ID available

		$category_id = ( isset($_GET['c']) AND !empty($_GET['c']) )
			? $_GET['c'] : "0";
		$category = ORM::factory('category')
			->find($category_id);

		if($category->loaded)
		{
			$translated_title = Category_Lang_Model::category_title($category_id,$l);
			if($translated_title)
			{
				$this->template->content->category_title = $translated_title;
			}else{
				$this->template->content->category_title = $category->category_title;
			}
		}else{
			$this->template->content->category_title = "";
		}

		// Collect report stats
		$this->template->content->report_stats = new View('reports_stats');
		// Total Reports

		$total_reports = Incident_Model::get_total_reports(TRUE);

		// Average Reports Per Day
		$oldest_timestamp = Incident_Model::get_oldest_report_timestamp();

		// Round the number of days up to the nearest full day
		$days_since = ceil((time() - $oldest_timestamp) / 86400);
		if ($days_since < 1) {
			$avg_reports_per_day = $total_reports;
		}else{
			$avg_reports_per_day = round(($total_reports / $days_since),2);
		}

		// Percent Verified
		$total_verified = Incident_Model::get_total_reports_by_verified(true);
		$percent_verified = ($total_reports == 0) ? '-' : round((($total_verified / $total_reports) * 100),2).'%';

		$this->template->content->report_stats->total_reports = $total_reports;
		$this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
		$this->template->content->report_stats->percent_verified = $percent_verified;

		$this->template->header->action_name = Kohana::lang('ui_main.reports_title_index');

		$this->template->header->header_block = $this->themes->header_block();
	}
예제 #7
0
 /**
  * Displays all reports.
  */
 public function index($cluster_id = 0)
 {
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports');
     $db = new Database();
     $filter = isset($_GET['c']) && !empty($_GET['c']) && $_GET['c'] != 0 ? " AND ( c.id='" . $_GET['c'] . "' OR \n\t\t\t\tc.parent_id='" . $_GET['c'] . "' )  " : " AND 1 = 1";
     if (isset($_GET['sw']) && !empty($_GET['sw']) && count($southwest = explode(",", $_GET['sw'])) > 1 && isset($_GET['ne']) && !empty($_GET['ne']) && count($northeast = explode(",", $_GET['ne'])) > 1) {
         list($longitude_min, $latitude_min) = $southwest;
         list($longitude_max, $latitude_max) = $northeast;
         $filter .= " AND l.latitude >=" . $latitude_min . " AND l.latitude <=" . $latitude_max;
         $filter .= " AND l.longitude >=" . $longitude_min . " AND l.longitude <=" . $longitude_max;
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page'), 'total_items' => $db->query("SELECT DISTINCT i.* FROM `" . $this->table_prefix . "incident` AS i JOIN `" . $this->table_prefix . "incident_category` AS ic ON (i.`id` = ic.`incident_id`) JOIN `" . $this->table_prefix . "category` AS c ON (c.`id` = ic.`category_id`) JOIN `" . $this->table_prefix . "location` AS l ON (i.`location_id` = l.`id`) WHERE `incident_active` = '1' {$filter}")->count()));
     $incidents = $db->query("SELECT DISTINCT i.*, l.`location_name` FROM `" . $this->table_prefix . "incident` AS i JOIN `" . $this->table_prefix . "incident_category` AS ic ON (i.`id` = ic.`incident_id`) JOIN `" . $this->table_prefix . "category` AS c ON (c.`id` = ic.`category_id`) JOIN `" . $this->table_prefix . "location` AS l ON (i.`location_id` = l.`id`) WHERE `incident_active` = '1' {$filter} ORDER BY incident_date DESC LIMIT " . (int) Kohana::config('settings.items_per_page') . " OFFSET " . $pagination->sql_offset);
     $this->template->content->incidents = $incidents;
     //Set default as not showing pagination. Will change below if necessary.
     $this->template->content->pagination = '';
     // Pagination and Total Num of Report Stats
     if ($pagination->total_items == 1) {
         $plural = '';
     } else {
         $plural = 's';
     }
     if ($pagination->total_items > 0) {
         $current_page = $pagination->sql_offset / (int) Kohana::config('settings.items_per_page') + 1;
         $total_pages = ceil($pagination->total_items / (int) Kohana::config('settings.items_per_page'));
         if ($total_pages > 1) {
             // If we want to show pagination
             $this->template->content->pagination_stats = Kohana::lang('ui_admin.showing_page') . ' ' . $current_page . ' ' . Kohana::lang('ui_admin.of') . ' ' . $total_pages . ' ' . Kohana::lang('ui_admin.pages');
             $this->template->content->pagination = $pagination;
         } else {
             // If we don't want to show pagination
             $this->template->content->pagination_stats = $pagination->total_items . ' ' . Kohana::lang('ui_admin.reports');
         }
     } else {
         $this->template->content->pagination_stats = '(' . $pagination->total_items . ' report' . $plural . ')';
     }
     $icon_html = array();
     $icon_html[1] = "<img src=\"" . url::base() . "media/img/image.png\">";
     //image
     $icon_html[2] = "<img src=\"" . url::base() . "media/img/video.png\">";
     //video
     $icon_html[3] = "";
     //audio
     $icon_html[4] = "";
     //news
     $icon_html[5] = "";
     //podcast
     //Populate media icon array
     $this->template->content->media_icons = array();
     foreach ($incidents as $incident) {
         $incident_id = $incident->id;
         if (ORM::factory('media')->where('incident_id', $incident_id)->count_all() > 0) {
             $medias = ORM::factory('media')->where('incident_id', $incident_id)->find_all();
             //Modifying a tmp var prevents Kohona from throwing an error
             $tmp = $this->template->content->media_icons;
             $tmp[$incident_id] = '';
             foreach ($medias as $media) {
                 $tmp[$incident_id] .= $icon_html[$media->media_type];
                 $this->template->content->media_icons = $tmp;
             }
         }
     }
     // Category Title, if Category ID available
     $category_id = isset($_GET['c']) && !empty($_GET['c']) ? $_GET['c'] : "0";
     $category = ORM::factory('category')->find($category_id);
     $this->template->content->category_title = $category->loaded ? $category->category_title : "";
     // Collect report stats
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Average Reports Per Day
     $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     $avg_reports_per_day = round($total_reports / $days_since, 2);
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(true);
     $percent_verified = $total_reports == 0 ? 'n/a' : round($total_verified / $total_reports * 100, 2) . '%';
     $this->template->content->total_reports = $total_reports;
     $this->template->content->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->percent_verified = $percent_verified;
 }
예제 #8
0
 public function index($cluster_id = 0)
 {
     // Cacheable Controller
     $this->is_cachable = TRUE;
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports');
     $this->themes->js = new View('reports_js');
     // Get locale
     $l = Kohana::config('locale.language.0');
     $db = new Database();
     // Get incident_ids if we are to filter by category
     $allowed_ids = array();
     $category_ids_in = NULL;
     $category_ids = array();
     $category_ids_string = array();
     $design_response_category = (int) Kohana::config('pps.design_response_category_id');
     if (isset($_GET['c']) and !empty($_GET['c']) and $_GET['c'] != 0) {
         $category_ids_string = is_array($_GET['c']) ? $_GET['c'] : array($_GET['c']);
     } else {
         // default to design response search
         $category_ids_string = array('' . $design_response_category);
     }
     if (!empty($category_ids_string)) {
         foreach ($category_ids_string as $category_id) {
             $category_ids[] = (int) str_replace("'", "", $db->escape($category_id));
         }
         $query = 'SELECT incident_id, count(*) AS count FROM incident_category WHERE category_id in (' . implode(',', $category_ids) . ') GROUP BY incident_id HAVING count=' . count($category_ids);
         $query = $db->query($query);
         if (count($query) === 0) {
             $allowed_ids[] = -1;
         } else {
             foreach ($query as $items) {
                 $allowed_ids[] = $items->incident_id;
             }
         }
     }
     // Get location_ids if we are to filter by location
     $location_ids = array();
     // Break apart location variables, if necessary
     $southwest = array();
     if (isset($_GET['sw'])) {
         $southwest = explode(",", $_GET['sw']);
     }
     $northeast = array();
     if (isset($_GET['ne'])) {
         $northeast = explode(",", $_GET['ne']);
     }
     if (count($southwest) == 2 and count($northeast) == 2) {
         $lon_min = (double) $southwest[0];
         $lon_max = (double) $northeast[0];
         $lat_min = (double) $southwest[1];
         $lat_max = (double) $northeast[1];
         $query = 'SELECT id FROM ' . $this->table_prefix . 'location WHERE latitude >=' . $lat_min . ' AND latitude <=' . $lat_max . ' AND longitude >=' . $lon_min . ' AND longitude <=' . $lon_max;
         $query = $db->query($query);
         foreach ($query as $items) {
             $location_ids[] = $items->id;
         }
     } elseif (isset($_GET['l']) and !empty($_GET['l']) and $_GET['l'] != 0) {
         $location_ids[] = (int) $_GET['l'];
     }
     // borough filtering
     if (isset($_GET['b']) and $_GET['b'] !== "any") {
         $query = 'SELECT id FROM location where borough=' . $db->escape($_GET['b']);
         $query = $db->query($query);
         $boro_ids = array();
         foreach ($query as $items) {
             $boro_ids[] = $items->id;
         }
         if (count($boro_ids) === 0) {
             // no boroughs found, trigger no results for query
             $location_ids = array(-1);
         } elseif (count($location_ids) > 0) {
             // make locations_ids the intersection of the two
             $new_locids = array();
             foreach ($location_ids as $locid) {
                 if (in_array($locid, $boro_ids)) {
                     $new_locids[] = $locid;
                 }
             }
             $location_ids = $new_locids;
         } else {
             $location_ids = $boro_ids;
         }
     }
     // Get the count
     $incident_id_in = '1=1';
     if (count($allowed_ids) > 0) {
         $incident_id_in = 'id IN (' . implode(',', $allowed_ids) . ')';
     }
     $location_id_in = '1=1';
     if (count($location_ids) > 0) {
         $location_id_in = 'location_id IN (' . implode(',', $location_ids) . ')';
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page'), 'total_items' => ORM::factory("incident")->where("incident_active", 1)->where($location_id_in)->where($incident_id_in)->count_all()));
     // Reports
     if (!isset($_GET['sort']) or $_GET['sort'] === 'comments') {
         $loc_id_in = count($location_ids) === 0 ? "1=1" : "location.id in (" . implode(',', $location_ids) . ")";
         $inc_id_in = str_replace('id', 'incident.id', $incident_id_in);
         $incident_query = 'SELECT incident.id, COUNT(comment.incident_id) as numcomments FROM incident LEFT JOIN comment on incident.id=comment.incident_id LEFT JOIN location on incident.location_id=location.id where incident_active=1 AND ' . $inc_id_in . ' AND ' . $loc_id_in . ' GROUP BY incident.id ORDER BY numcomments desc';
         $incident_query .= ' LIMIT ' . Kohana::config('settings.items_per_page_admin') . ' OFFSET ' . $pagination->sql_offset;
         $incident_id_results = $db->query($incident_query);
         $incidents = array();
         foreach ($incident_id_results as $incident) {
             // XXX geting each incident individually to populate the array is inefficient
             // but I can't figure out how to tell the orm to do this with our version of kohana
             $incident_obj = ORM::factory("incident")->where("id=" . $incident->id)->find_all();
             if (!empty($incident_obj)) {
                 $incidents[] = $incident_obj[0];
             }
         }
     } else {
         $incidents = ORM::factory("incident")->where("incident_active", 1)->where($location_id_in)->where($incident_id_in)->orderby("incident_dateadd", "desc")->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     }
     // Swap out category titles with their proper localizations using an array (cleaner way to do this?)
     $localized_categories = array();
     foreach ($incidents as $incident) {
         foreach ($incident->category as $category) {
             $ct = (string) $category->category_title;
             if (!isset($localized_categories[$ct])) {
                 $translated_title = Category_Lang_Model::category_title($category->id, $l);
                 $localized_categories[$ct] = $category->category_title;
                 if ($translated_title) {
                     $localized_categories[$ct] = $translated_title;
                 }
             }
         }
     }
     $this->template->content->localized_categories = $localized_categories;
     $this->template->content->incidents = $incidents;
     // add in person submitted information
     $person_submitted_info = array();
     foreach ($incidents as $incident) {
         $incident_person = ORM::factory('incident_person')->where('incident_id', $incident->id)->find();
         if ($incident_person->loaded) {
             $person_submitted_info[$incident->id] = array('first_name' => $incident_person->person_first, 'last_name' => $incident_person->person_last);
         }
     }
     $this->template->content->person_submitted_info = $person_submitted_info;
     //Set default as not showing pagination. Will change below if necessary.
     $this->template->content->pagination = "";
     // Pagination and Total Num of Report Stats
     if ($pagination->total_items == 1) {
         $plural = "";
     } else {
         $plural = "s";
     }
     if ($pagination->total_items > 0) {
         $current_page = $pagination->sql_offset / (int) Kohana::config('settings.items_per_page') + 1;
         $total_pages = ceil($pagination->total_items / (int) Kohana::config('settings.items_per_page'));
         if ($total_pages > 1) {
             // If we want to show pagination
             $this->template->content->pagination_stats = Kohana::lang('ui_admin.showing_page') . ' ' . $current_page . ' ' . Kohana::lang('ui_admin.of') . ' ' . $total_pages . ' ' . Kohana::lang('ui_admin.pages');
             $this->template->content->pagination = $pagination;
         } else {
             // If we don't want to show pagination
             $this->template->content->pagination_stats = $pagination->total_items . ' ' . Kohana::lang('ui_admin.reports');
         }
     } else {
         $this->template->content->pagination_stats = '(' . $pagination->total_items . ' report' . $plural . ')';
     }
     // this is used as text display for search results string
     $category_titles = array();
     if ($category_ids_in) {
         $categories = ORM::factory('category')->where('id ' . $category_ids_in)->find_all();
         foreach ($categories as $category) {
             $category_titles[] = $category->category_title;
         }
     }
     $this->template->content->category_titles = $category_titles;
     $scale_category = ORM::factory('category')->where('category_title', 'Scale')->find();
     $context_category = ORM::factory('category')->where('category_title', 'Context')->find();
     $visible_categories = ORM::factory('category')->where('parent_id IN (' . implode(",", array($scale_category->id, $context_category->id)) . ')')->find_all();
     $scale_categories = array();
     $context_categories = array();
     foreach ($visible_categories as $visible_category) {
         if ($visible_category->parent_id === $scale_category->id) {
             $scale_categories[] = $visible_category;
         } elseif ($visible_category->parent_id === $context_category->id) {
             // design response category should be first in context list
             // also assuming that the design response is under the context category
             if ($visible_category->id === $design_response_category) {
                 array_unshift($context_categories, $visible_category);
             } else {
                 array_push($context_categories, $visible_category);
             }
         }
     }
     $this->template->content->scale_categories = $scale_categories;
     $this->template->content->context_categories = $context_categories;
     $this->template->content->selected_categories = $category_ids;
     // Collect report stats
     $this->template->content->report_stats = new View('reports_stats');
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Average Reports Per Day
     $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     if ($days_since < 1) {
         $avg_reports_per_day = $total_reports;
     } else {
         $avg_reports_per_day = round($total_reports / $days_since, 2);
     }
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(true);
     $percent_verified = $total_reports == 0 ? '-' : round($total_verified / $total_reports * 100, 2) . '%';
     $this->template->content->report_stats->total_reports = $total_reports;
     $this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->report_stats->percent_verified = $percent_verified;
     $this->template->header->header_block = $this->themes->header_block();
 }