Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function resource_stats($arrExtra=0, $strResource)
- {
- if ($strResource == "navigation")
- {
- //if any parameter is passed select appropriate tables
- $strSql = "
- SELECT
- *
- FROM
- ";
- $arrTable = array();
- if(
- isset($arrExtra["institution_id"])
- || isset($arrExtra["network_id"])
- || isset($arrExtra["host_id"])
- ){
- $arrTable[] = "institutions";
- }
- else if(
- isset($arrExtra["class_id"])
- ){
- $arrTable[] = "classes";
- }
- else if(
- isset($arrExtra["user_id"])
- ){
- $arrTable[] = "users";
- }
- else if(
- isset($arrExtra["campaign_id"])
- ){
- $arrTable[] = "campaigns";
- }
- else if(
- isset($arrExtra["mission_id"])
- ){
- $arrTable[] = "missions";
- }
- else
- {
- $arrTable[] = "institutions,campaigns,missions";
- }
- }
- else if ($strResource == "count")
- {
- //if any parameter is passed select appropriate tables
- //the count has to be one level deeper. If campaign_id is passed, then we
- //must calculate amount of missions that have that campaign_id
- $strSql = "
- SELECT
- COUNT(1) as row_count
- FROM
- ";
- $arrTable = array();
- if(
- isset($arrExtra["institution_id"])
- || isset($arrExtra["network_id"])
- || isset($arrExtra["host_id"])
- ){
- $arrTable[] = "institutions";
- }
- else if(
- isset($arrExtra["class_id"])
- ){
- $arrTable[] = "classes";
- }
- else if(
- isset($arrExtra["user_id"])
- ){
- $arrTable[] = "users";
- }
- else if(
- isset($arrExtra["campaign_id"])
- ){
- $arrTable[] = "campaigns";
- }
- else if(
- isset($arrExtra["mission_id"])
- ){
- $arrTable[] = "missions";
- }
- else
- {
- $arrTable[] = "institutions,campaigns,missions";
- }
- } else
- {
- print "Sorry, there was an error: MI-RS101-SDJ383";
- exit;
- }
- //sub queries
- $arrSql = array();
- if (
- isset($arrExtra["institution_id"])
- ){
- $arrSql[] = "(institutions.institution_id = " . $arrExtra["institution_id"].")";
- }
- if (
- isset($arrExtra["network_id"])
- ){
- $arrSql[] = "
- (
- institutions.institution_id = " . $arrExtra["network_id"] . "
- AND institutions.institution_type = 'Network'
- )";
- }
- if (
- isset($arrExtra["host_id"])
- ){
- $arrSql[] = "
- (
- institutions.institution_id = " . $arrExtra["host_id"] . "
- AND institutions.institution_type = 'Host'
- )";
- }
- if (
- isset($arrExtra["campaign_id"])
- ){
- $arrSql[] = "
- (campaigns.campaign_id = " . $arrExtra["campaign_id"].")";
- }
- if (
- isset($arrExtra["mission_id"])
- ){
- $arrSql[] = "(missions.mission_id = " . $arrExtra["mission_id"].")";
- }
- if (
- count($arrSql)
- && count($arrTable)
- ){
- $strSql .= join("," , $arrTable) ." WHERE " . join(" OR ", $arrSql).";";
- }
- else
- {
- //print 'tables selected: ' .join(",", $arrTable) . ' and count table: ' .count($arrSql);
- print "Sorry, there was an error: MI-RS101-D6554DH";
- exit;
- }
- //print $strSql .'<br />'; //exit;
- if($strResource == "navigation")
- {
- $arrResult = $this->_db->fetchAll($strSql);
- return $arrResult;
- }
- if($strResource == "count")
- {
- $intResult = $this->_db->fetchAll($strSql);
- return $intResult;
- }
- }
- /* Function resource_count
- * Params: arrExtra
- * Return: arrResult that will hold the count of itema based on
- * what was passed in the arrExtra
- */
- function resource_count($arrExtra=0)
- {
- $strSql = "
- SELECT
- COUNT(1) as row_count
- FROM
- ";
- //if any parameter is passed select appropriate tables
- $arrTable = array();
- if(
- isset($arrExtra["institution_id"])
- && !isset($arrExtra["network_id"])
- && !isset($arrExtra["host_id"])
- ){
- $arrTable[] = "campaigns";
- }
- else if(
- isset($arrExtra["host_id"])
- && isset($arrExtra["network_id"])
- && !isset($arrExtra["institution_id"])
- ){
- $arrTable[] = "campaigns,missions,tasks";
- }
- else if(
- isset($arrExtra["host_id"])
- || isset($arrExtra["network_id"])
- ){
- $arrTable[] = "institutions";
- }
- else if(
- isset($arrExtra["class_id"])
- ){
- $arrTable[] = "classes";
- }
- else if(
- isset($arrExtra["user_id"])
- ){
- $arrTable[] = "users";
- }
- else if(
- isset($arrExtra["campaign_id"])
- ){
- $arrTable[] = "missions";
- }
- else if(
- isset($arrExtra["mission_id"])
- ){
- $arrTable[] = "tasks";
- }
- else
- {
- $arrTable[] = "institutions,classes,users,campaigns,missions,tasks";
- }
- //chech if arrTable contains anything
- $arrSql= array();
- //see how many networks under a host
- if (
- isset($arrExtra["host_id"])
- ){
- $arrSql[] = "
- (
- institutions.host_id = " . $arrExtra["host_id"] . "
- AND institution_type='Network'
- )";
- }
- //see how many institutions under a network
- if (
- isset($arrExtra["network_id"])
- ){
- $arrSql[] = "
- (
- institutions.network_id = " . $arrExtra["network_id"] . "
- AND institution_type!='Host'
- AND institution_type!='Network'
- )";
- }
- //see how many campaigns under an institution
- if (
- isset($arrExtra["institution_id"])
- ){
- $strSqlCampaign = "
- SELECT
- COUNT(1) as campaign_count
- FROM
- campaigns
- WHERE
- ";
- /*
- Using arrExtra, an institution can be selected by using a host id
- and/or network id.
- */
- /***************** SUB QUESRIES FOR CAMPAIGNS, MISSIONS AND TASKS **************/
- //start campaign_count handler
- $arrSqlCampaigns = array(); // All exceptions within this array will be OR joined
- if (
- isset($arrExtra["institution_id"])
- && !isset($arrExtra["host_id"])
- && !isset($arrExtra["network_id"])
- )
- $arrSqlCampaigns[] = "institution_id = " . $arrExtra["institution_id"];
- if (
- isset($arrExtra["host_id"])
- || isset($arrExtra["network_id"])
- ){
- $strSubSql = "
- SELECT
- institution_id
- FROM
- institutions
- WHERE
- ";
- $arrSubSql = array();
- if ( // Select host
- isset($arrExtra["host_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["host_id"]}
- AND host_id = 0
- AND network_id = 0
- )";
- }
- if ( // Select network
- isset($arrExtra["network_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["network_id"]}
- AND network_id = 0
- AND host_id != 0
- )";
- }
- if ( // Select institution
- isset($arrExtra["institution_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["institution_id"]}
- AND network_id != 0
- AND host_id != 0
- )";
- }
- else
- {
- $arrSubSql[] = "(
- institution_id!=0
- )";
- }
- $arrSqlCampaigns[] = "institution_id IN (" . $strSubSql . "(" . join(" OR ", $arrSubSql) . "))";
- }
- if (count($arrSqlCampaigns)) {
- $strSqlCampaign .= " (" . join(" OR ", $arrSqlCampaigns) . ")";
- }
- print $strSqlCampaign; //exit;
- $arrResult = $this->_db->fetchAll($strSqlCampaign);
- return $arrResult;
- exit;
- //end of campaign_count handler
- }
- //see see how many missions under a campaign
- if (
- isset($arrExtra["campaign_id"])
- && !isset($arrExtra["institution_id"])
- && !isset($arrExtra["network_id"])
- && !isset($arrExtra["host_id"])
- ){
- //start mission_count handler
- $arrSqlMissions = array(); // All exceptions within this array will be AND joined
- $arrSqlMissions[] = "campaign_id = " . $arrExtra["campaign_id"];
- $strSqlMissions = "
- SELECT
- COUNT(1) as mission_count
- FROM
- missions
- WHERE
- ";
- /*
- Using arrExtra, an institution can be selected by id or using a host id
- and/or network id.
- */
- if (
- isset($arrExtra["institution_id"])
- && !isset($arrExtra["host_id"])
- && !isset($arrExtra["network_id"])
- ){
- $arrSqlMissions[] = "(institution_id = " . $arrExtra["institution_id"].")";
- }
- if (
- isset($arrExtra["host_id"])
- || isset($arrExtra["network_id"])
- ){
- $strSubSql = "
- SELECT
- institution_id
- FROM
- institutions
- WHERE
- ";
- $arrSubSql = array();
- if ( // Select host
- isset($arrExtra["host_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["host_id"]}
- AND host_id = 0
- AND network_id = 0
- )";
- }
- if ( // Select network
- isset($arrExtra["network_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["network_id"]}
- AND network_id = 0
- AND host_id != 0
- )";
- }
- if ( // Select institution
- isset($arrExtra["institution_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["institution_id"]}
- AND network_id != 0
- AND host_id != 0
- )";
- }
- if(!empty($arrSqlMissions))
- {
- $arrSqlMissions[] = "institution_id IN (" . $strSubSql . "(" . join(" OR ", $arrSubSql) . "))";
- }
- }
- if (count($arrSqlMissions))
- {
- $strSqlMissions .= " (" . join(" OR ", $arrSqlMissions) . ")";
- }
- //print 'mission_count: ';
- //print $strSqlMissions .'<br />'; //exit;
- $arrResult = $this->_db->fetchAll($strSqlMissions);
- return $arrResult;
- exit;
- //end of mission_count handler
- }
- //see how many tasks under a mission
- if (
- isset($arrExtra["mission_id"])
- && !isset($arrExtra["campaign_id"])
- && !isset($arrExtra["institution_id"])
- && !isset($arrExtra["network_id"])
- && !isset($arrExtra["host_id"])
- ){
- //start task_count handler
- $arrSqlTasks = array();
- $arrSqlTasks[] = "
- (
- tasks.mission_id = " . $arrExtra["mission_id"]."
- )";
- $strSqlTasks = "
- SELECT
- COUNT(1) as task_count
- FROM
- tasks
- WHERE
- ";
- /*
- Using arrExtra, an institution can be selected by id or using a host id
- and/or network id.
- */
- if (
- isset($arrExtra["institution_id"])
- && !isset($arrExtra["host_id"])
- && !isset($arrExtra["network_id"])
- )
- $arrSqltasks[] = "(institution_id = " . $arrExtra["institution_id"].")";
- if(isset($arrExtra["campaign_id"]))
- {
- $arrSqltasks[] = "(campaign_id = " . $arrExtra["campaign_id"].")";
- }
- if(isset($arrExtra["mission_id"]))
- {
- $arrSqltasks[] = "(mission_id = " . $arrExtra["mission_id"].")";
- }
- if(!empty($arrSqltasks))
- {
- $strSqlTasks .= " (" . join(" AND ", $arrSqltasks) . ")";
- }
- if (
- isset($arrExtra["host_id"])
- || isset($arrExtra["network_id"])
- ) {
- $strSubSql = "
- SELECT
- institution_id
- FROM
- institutions
- WHERE
- ";
- $arrSubSql = array();
- if ( // Select host
- isset($arrExtra["host_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["host_id"]}
- AND host_id = 0
- AND network_id = 0
- )";
- }
- if ( // Select network
- isset($arrExtra["network_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["network_id"]}
- AND network_id = 0
- AND host_id != 0
- )";
- }
- if ( // Select institution
- isset($arrExtra["institution_id"])
- ) {
- $arrSubSql[] = "(
- institution_id={$arrExtra["institution_id"]}
- AND network_id != 0
- AND host_id != 0
- )";
- }
- $arrSqltasks[] = "institution_id IN (" . $strSubSql . "(" . join(" OR ", $arrSubSql) . "))";
- }
- if (count($arrSqltasks)) {
- $strSqlTasks .= " AND (" . join(" OR ", $arrSqltasks) . ")";
- }
- //print 'strSqlTasks: ' .$strSqlTasks; exit;
- $arrResult = $this->_db->fetchAll($strSqlTasks);
- return $arrResult;
- exit;
- //end of task_count handler
- }
- if(//if no tables are selected throw an error
- count($arrSql)
- && !empty($arrTable)
- ){
- $strSql .= join(" , ",$arrTable) ." WHERE " .join(" OR " , $arrSql).";";
- }
- else
- {
- print "Sorry, there was an error: MI-RC101-123KJG";
- }
- print 'resource_count: ';
- print $strSql .'<br />'; //exit;
- $arrResult = $this->_db->fetchAll($strSql);
- return $arrResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement