<?
	include_once("config.php");
	class SimpleAsk extends Fly5 {
		function execute(&$view, $path, $args)
		{
				$a = $_REQUEST;
				switch($path) {
				case 'SimpleAsk/api/fetchChoices':
					$result = $this->fetchChoices($a);
					break;
				default:
						$view = "$path.pug";
						return;
				}
				reply_json($result);
		}
		function fetchChoices($a)
		{
			if($a[ask])
				$ask = ' and ask=:ask ';
			if($a[startdate])
				$sd = ' and date_format(answertime,"%Y-%m-%d") >= :startdate ';
			if($a[enddate])
				$ed = ' and date_format(answertime,"%Y-%m-%d") <= :enddate ';
			//print_r($a);
			$rows = query("
				select 
					b.*, a.*, (a.choice+1) choice 
				from 
					simple_answer a
				left join uis.ocs_patient b on patientno=b.no
				where 1=1 $ask $sd $ed
				order by answertime desc
				limit 1000
			", $a);
			$result = array();
			foreach($rows as $r)
				$result[$r[patientno].$r[ask]] = $r;
			return array_values($result);
		}

		function __construct()
		{
				$this->hid = "IM39";
		}
	}
