Skip to content

Commit

Permalink
Dev Created BigData library.
Browse files Browse the repository at this point in the history
Dev Created new Json server that uses big data library.
Dev Created new get_response_ids function for retrieving response ids for a token.
  • Loading branch information
SamMousa committed Mar 19, 2013
1 parent ba19aea commit bfb53b8
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 12 deletions.
8 changes: 1 addition & 7 deletions application/controllers/admin/participantsaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,7 @@ public function runWithParams($params)
*/
private function _loadjqGrid($sScript = '', $aData = array())
{
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('third_party') . 'jqgrid/js/jquery.jqGrid.min.js');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('third_party') . 'jqgrid/js/i18n/grid.locale-en.js');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('third_party') . 'jqgrid/plugins/jquery.searchFilter.js');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('third_party') . 'jqgrid/src/grid.celledit.js');
App()->getClientScript()->registerCssFile(Yii::app()->getConfig('third_party') . 'jqgrid/css/ui.jqgrid.css');


App()->getClientScript()->registerPackage('jqgrid');
if (!empty($sScript))
{
$this->getController()->_js_admin_includes(Yii::app()->getConfig('adminscripts') . $sScript . '.js');
Expand Down
24 changes: 24 additions & 0 deletions application/controllers/admin/remotecontrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,30 @@ public function import_group($sSessionKey, $iSurveyID, $sImportData, $sImportDat
return array('status' => 'Invalid session key');
}

/**
* RPC Routine to find response IDs given a survey ID and a token.
* @param string $sSessionKey
* @param int $iSurveyID
* @param string $sToken
*/
public function get_response_ids($sSessionKey, $iSurveyID, $sToken)
{
if ($this->_checkSessionKey($sSessionKey))
{
$responses = Survey_dynamic::model($iSurveyID)->findByAttributes(array('token' => $sToken));
$result = array();
foreach ($responses as $response)
{
$result[] = $response->id;
}
return $result;
}
else
{
return array('status' => 'Invalid Session Key');
}

}

/**
* RPC Routine to return properties of a group of a survey .
Expand Down
5 changes: 0 additions & 5 deletions application/controllers/admin/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,8 @@ function browse($iSurveyId, $limit = 50, $start = 0, $order = false, $searchstri
// Javascript
$this->getController()->_js_admin_includes(Yii::app()->getConfig('adminscripts') . "tokens.js");
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/jquery.multiselect.min.js");
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/jqGrid/js/i18n/grid.locale-en.js");
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/jqGrid/js/jquery.jqGrid.min.js");
$this->getController()->_js_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/jquery-ui-timepicker-addon.js");
// CSS
// $this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/css/jquery.multiselect.css");
// $this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/css/jquery.multiselect.filter.css");
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/jqGrid/css/ui.jqgrid.css");
$this->getController()->_css_admin_includes(Yii::app()->getConfig('generalscripts') . "jquery/jqGrid/css/jquery.ui.datepicker.css");
$this->getController()->_css_admin_includes(Yii::app()->getConfig('adminstyleurl') . "displayParticipants.css");
$this->getController()->_css_admin_includes(Yii::app()->getConfig('adminstyleurl') . "jquery-ui/jquery-timepicker.css");
Expand Down
143 changes: 143 additions & 0 deletions application/libraries/BigData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/**
* Class containing helper functions for dealing with "big data".
* @author Sam Mousa <sam@befound.nl>
*/
class BigData {

/**
* This function combines json_encode and echo.
* If a stream is passed (or is part of the array) it's content will be
* directly streamed instead of reading it into memory first.
* Supported flags:
* JSON_FORCE_OBJECT
* @param array $json
* @param int $options Same flags used in JSON_ENCODE.
*/
public static function json_echo($json, $options)
{
// Scan array for any streams.
$hasStream = array_reduce($json, array('BigData', 'hasStream'), false);

// If there is no stream we are done.
if (!$hasStream)
{
echo json_encode($json, $options);
}
else
{
self::json_echo_data($json, ($options & JSON_FORCE_OBJECT) == JSON_FORCE_OBJECT);
}

}

protected static function hasStream(&$result, $item)
{
if ($result === true)
{
return true;
}
elseif(is_array($item))
{
return array_reduce($item, array('BigData', 'hasStream'), false);
}
// Should use get_resource_type to do stricter check.
elseif (self::isStream($item))
{
return true;
}
else
{
return false;
}
}


protected static function isStream($item)
{
return is_resource($item);
}

protected static function isAssociative($array)
{
foreach ($array as $key => $value)
{
if (is_string($key))
{
return true;
}
}
return false;
}


protected static function json_echo_data($json)
{
if ((is_array($json) && self::isAssociative($json)) || is_object($json))
{
self::json_echo_object($json);
}
elseif (is_array($json))
{
self::json_echo_array($json);
}
elseif (is_numeric($json))
{
self::json_echo_number($json);
}
elseif (is_string($json))
{
self::json_echo_string($json);
}
elseif (self::isStream($json))
{
self::json_echo_stream($json);
}
}

private static function json_echo_array($json)
{
echo '[';
foreach ($json as $key => $entry)
{
echo json_encode($key) . ':';
self::json_echo_data($entry);
echo ', '; // The extra comma is allowed: { 1: 'test', 2: 'test',} is valid.
}
echo ']';
}

private static function json_echo_number($json)
{
echo $json;
}

private static function json_echo_object($json)
{
echo '{';
foreach ($json as $key => $entry)
{
echo json_encode($key) . ':';
self::json_echo_data($entry);
echo ', '; // The extra comma is allowed: { 1: 'test', 2: 'test',} is valid.
}
echo '}';
}

private static function json_echo_string($json)
{
echo json_encode($json);
}

private static function json_echo_stream($json)
{
// Encode stream to base64.
echo "'";
stream_filter_append($json, 'convert.base64-encode', STREAM_FILTER_READ, array('line-length' => 50, 'line-break-chars' => "\n"));
fpassthru($json);
echo "'";
}
}

?>
59 changes: 59 additions & 0 deletions application/libraries/LSjsonRPCServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
Yii::import('application.libraries.BigData', true);
class LSjsonRPCServer extends jsonRPCServer
{
/**
* This function handle a request binding it to a given object
*
* @param object $object
* @return boolean
*/
public static function handle($object) {

// checks if a JSON-RCP request has been received
if (
$_SERVER['REQUEST_METHOD'] != 'POST' ||
empty($_SERVER['CONTENT_TYPE']) ||
$_SERVER['CONTENT_TYPE'] != 'application/json'
) {
// This is not a JSON-RPC request
return false;
}

// reads the input data
$request = json_decode(file_get_contents('php://input'),true);

// executes the task on local object
try {
if ($result = @call_user_func_array(array($object,$request['method']),$request['params'])) {
$response = array (
'id' => $request['id'],
'result' => $result,
'error' => NULL
);
} else {
$response = array (
'id' => $request['id'],
'result' => NULL,
'error' => 'unknown method or incorrect parameters'
);
}
} catch (Exception $e) {
$response = array (
'id' => $request['id'],
'result' => NULL,
'error' => $e->getMessage()
);
}

// output the response
if (!empty($request['id'])) { // notifications don't want response
header('content-type: text/javascript');
BigData::json_echo($response);
}

// finish
return true;
}
}
?>

0 comments on commit bfb53b8

Please sign in to comment.