<?php

// 【区域管理】获取指定学校的志愿者活动列表
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_get_school_detail.php....Start.", 0);

//获取参数
$schoolNo = ParamUtil::getRequestString("schoolNo");
$originalSource = ParamUtil::getRequestNumber("originalSource", 0);

//参数检查
if(empty($schoolNo) || empty($originalSource)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

$result = array();

//查询学校是否存在
//todo 以后改为 从家校或者家园系统查询
$param = array();
$param['school_no'] = $schoolNo;
$param['original_source'] = $originalSource;
$param['delete_flg'] = false;
$schoolList = SchoolMst::getList($param,'id','desc', 0, 1);
if(empty($schoolList)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

$schoolMst = $schoolList[0];

//查询该学校的志愿者人数
$memberCount = 0;
$sql = "select count(*) as member_count from user_mst where delete_flg = false and school_no='{$schoolNo}' and original_source='{$originalSource}'";
$db = &CompassDBManager::getInstance();
$tmpList = $db->executeQuery($sql);
if(!empty($tmpList)) {
	$memberCount = $tmpList[0]['member_count'];
}

//查询该校能力分汇总
$abilityPoint = 0;
$sql = "select sum(ability_point) as ability_point from user_mst where delete_flg = false and school_no='{$schoolNo}' and original_source='{$originalSource}'";
$db = &CompassDBManager::getInstance();
$tmpList = $db->executeQuery($sql);
if(!empty($tmpList)) {
	$abilityPoint = $tmpList[0]['ability_point'];
}

//查询该学校的志愿者活动列表
$volunteerEventList = array();
$param = array();
$param['school_no'] = $schoolNo;
$param['original_source'] = $originalSource;
$param['status_NOT'] = "NEW";
$param['status_NOT'] = "NG";
$param['delete_flg'] = false;
$tmpVolunteerEventList = VolunteerEventDat::getList($param,'id','desc');

//加工返回的数据
//设置状态和招募范围
foreach($tmpVolunteerEventList as $tmp) {
	$tmp->status_title = "征集中";
	$tmp->scope = "校内";
	if($tmp->include_social_user) {
		$tmp->scope = "校内.社会人士";
	}
	$volunteerEventList[] = $tmp;
}

//接口返回数据
$result["schoolTitle"] = $schoolMst->title;
$result["memberCount"] = $memberCount;
$result["abilityPoint"] = $abilityPoint;
$result["volunteerEventList"] = $volunteerEventList;


ErrorLogger::doOutput("Compass...ajax_get_school_detail.php....End.", 0);


//返回结果
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>