/** * Simple procedure to test most RPC functions * */ public function test() { $sFileToImport = dirname(Yii::app()->basePath) . DIRECTORY_SEPARATOR . 'docs' . DIRECTORY_SEPARATOR . 'demosurveys' . DIRECTORY_SEPARATOR . 'limesurvey2_sample_survey_english.lss'; // $sFileToImport=dirname(Yii::app()->basePath).DIRECTORY_SEPARATOR.'docs'.DIRECTORY_SEPARATOR.'demosurveys'.DIRECTORY_SEPARATOR.'survey_archive_example_feedback_survey.zip'; Yii::app()->loadLibrary('jsonRPCClient'); $myJSONRPCClient = new jsonRPCClient(Yii::app()->getBaseUrl(true) . '/' . dirname(Yii::app()->request->getPathInfo())); $sSessionKey = $myJSONRPCClient->get_session_key('admin', 'password'); if (is_array($sSessionKey)) { echo $sSessionKey['status']; die; } else { echo 'Retrieved session key' . '<br>'; } $sLSSData = base64_encode(file_get_contents($sFileToImport)); $iSurveyID = $myJSONRPCClient->import_survey($sSessionKey, $sLSSData, 'zip', 'Test import by JSON_RPC', 1000); echo 'Created new survey SID:' . $iSurveyID . '<br>'; /* Very simple example to export responses as Excel file $aResult=$myJSONRPCClient->export_reponses($sSessionKey,$iSurveyID,'xls'); file_put_contents('d:\test.xls',base64_decode(chunk_split($aResult))); */ $aResult = $myJSONRPCClient->activate_survey($sSessionKey, $iSurveyID); if ($aResult['status'] == 'OK') { echo 'Survey ' . $iSurveyID . ' successfully activated.<br>'; } $aResult = $myJSONRPCClient->activate_participant_tokens($sSessionKey, $iSurveyID, array(1, 2)); if ($aResult['status'] == 'OK') { echo 'Tokens for Survey ID ' . $iSurveyID . ' successfully activated.<br>'; } $aResult = $myJSONRPCClient->set_survey_properties($sSessionKey, $iSurveyID, array('faxto' => '0800-LIMESURVEY')); if ($aResult['status'] == 'OK') { echo 'Modified survey settings for survey ' . $iSurveyID . '<br>'; } $aResult = $myJSONRPCClient->add_survey_language($sSessionKey, $iSurveyID, 'ar'); if ($aResult['status'] == 'OK') { echo 'Added Arabian as additional language' . '<br>'; } $aResult = $myJSONRPCClient->set_survey_language_properties($sSessionKey, $iSurveyID, array('surveyls_welcometext' => 'An Arabian welcome text!'), 'ar'); if ($aResult['status'] == 'OK') { echo 'Modified survey locale setting welcometext for Arabian in survey ID ' . $iSurveyID . '<br>'; } $aResult = $myJSONRPCClient->delete_survey_language($sSessionKey, $iSurveyID, 'ar'); if ($aResult['status'] == 'OK') { echo 'Removed Arabian as additional language' . '<br>'; } die; $aResult = $myJSONRPCClient->delete_survey($sSessionKey, $iSurveyID); echo 'Deleted survey SID:' . $iSurveyID . '-' . $aResult['status'] . '<br>'; // Release the session key - close the session $Result = $myJSONRPCClient->release_session_key($sSessionKey); echo 'Closed the session' . '<br>'; }
die(print_r(LS_RPC_PATH, true)); } // get all evaluations for a given relation $relation_id = array_key_exists('relation_id', $_REQUEST) ? (int) $_REQUEST['relation_id'] : null; $relation_step = array_key_exists('step', $_REQUEST) ? (int) $_REQUEST['step'] : null; $evaluation_id = array_key_exists('evaluation_id', $_REQUEST) ? (int) $_REQUEST['evaluation_id'] : null; $evaluations = []; if ($relation_id) { $evaluations = RelationModel::evaluations($relation_id); } elseif ($evaluation_id) { $evaluations[] = pods('evaluation', $evaluation_id); } // instanciate a new client $myJSONRPCClient = new jsonRPCClient(LS_BASEURL); // receive session key $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); // calculate averages for current evaluation $response = []; foreach ($evaluations as $key => $evaluation) { $limesurvey_id = EvaluationModel::surveyID($evaluation->ID, $relation_step); $type = $relation_step === '360' ? 'company' : 'agency'; // questions $questions_filename = $type . '_' . $limesurvey_id . '_questions.json'; $survey_questions = json_decode(file_get_contents(CABSPATH . 'cache/' . $questions_filename), true); $group_q = ['OP' => [], 'OM' => [], 'TM' => []]; foreach ($survey_questions as $rolekey => $rolevalue) { foreach ($rolevalue as $sqkey => $sqvalue) { if (!array_key_exists($sqvalue['g']['id']['gid'], $group_q[$rolekey])) { $group_q[$rolekey][$sqvalue['g']['id']['gid']] = ['name' => $sqvalue['g']['group_name'], 'gid' => $sqvalue['g']['id']['gid'], 'questions' => [], 'question_benchmarks' => [], 'questions_txt' => []]; } $group_q[$rolekey][$sqvalue['g']['id']['gid']]['questions'][] = $sqvalue['q']['title'];
/** * [run - do all the seedings] * @return [json] [AjaxResponse] */ public static function run() { // this function should do: // get the relationship // get all related user // fill in all surveys // output summary with Reponse::output $evaluation_id = (int) $_POST['evaluation_id']; $relation_users = EvaluationModel::users($evaluation_id); AjaxResponse::stream(['message' => 'Initiating seeding', 'status' => 'OK', 'log' => true]); foreach ($relation_users as $key => $user_id) { if (!is_numeric($relation_users[$key])) { error_log('Invalid user - ' . json_encode($relation_users[$key])); } // company / agency $user_type = UserModel::userType($user_id); $evaluation_pod = pods('evaluation', $evaluation_id); $relation = $evaluation_pod->field('relation'); $relation_pod = pods('relation', $relation['ID']); $relation_type = $evaluation_pod->field('180_360'); $relation_steps = EvaluationModel::getSteps($relation_type); foreach ($relation_steps as $relation_step) { // get the user $user = UserModel::findByID($user_id); // get survey id $limesurvey_id = EvaluationModel::surveyID($evaluation_id, $relation_step); $questions_type = $relation_step === '360' ? 'company' : 'agency'; $filename = $questions_type . '_' . $limesurvey_id . '_questions.json'; $all_questions = json_decode(file_get_contents(CABSPATH . 'cache/' . $filename), true); $user_role = $user->roles[0]; $questions = null; if (!array_key_exists($user_role, $all_questions)) { // status response AjaxResponse::stream(['message' => 'ERROR: invalid role (could not load questions for user): ' . $user_id . ' ' . $relation_step, 'status' => 'failure', 'error' => true, 'log' => true], false); continue; } // pich the right user role $questions = $all_questions[$user_role]; // get question ids $question_ids = self::getQuestionIDs($limesurvey_id, $questions); // generate some random values for each question $question_answers = self::generateAnswers($question_ids); if (!count($question_answers)) { // status response AjaxResponse::stream(['message' => 'ERROR: could not generate answers processing user: '******' ' . $relation_step, 'status' => 'failure', 'error' => true, 'log' => true], false); continue; } $user_token = LimesurveyModel::getTokenByRelation($limesurvey_id, $user->user_email, $evaluation_id); if (!$user_token) { // status response AjaxResponse::stream(['message' => 'ERROR: could not match token for : ' . $user_id, 'status' => 'failure', 'error' => true, 'log' => true], false); continue; } $res = array_merge($question_answers, ['token' => $user_token->tid]); // instanciate a new client $myJSONRPCClient = new jsonRPCClient(LS_BASEURL); // receive session key $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); // save values $success_status = $myJSONRPCClient->add_response($sessionKey, $limesurvey_id, $res); // set participant status $participant_status = $myJSONRPCClient->set_participant_properties($sessionKey, $limesurvey_id, $user_token->tid, ['completed' => 'Y', 'usesleft' => 0]); // release session key $myJSONRPCClient->release_session_key($sessionKey); // status response AjaxResponse::stream(['message' => 'processing user: '******' ' . $relation_step, 'status' => 'OK', 'log' => true], false); } } }
/** * creates a single new limesurvey * @param [type] $post_id [description] * @param [type] $relation_args [description] * @param [type] $relation_step [description] * @return [type] [description] */ public static function create($post_id, $relation_args, $relation_step) { // require jsonRPCClient.php if (file_exists(LS_RPC_PATH)) { require_once LS_RPC_PATH; } else { die(print_r(LS_RPC_PATH, true)); } // create a new client $myJSONRPCClient = new jsonRPCClient(LS_BASEURL); // get session key $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); // check survey metadata $limesurveyid_field_name = 'survey_id_' . $relation_step; $limesurvey_id = $relation_args->evaluation_pod->field($limesurveyid_field_name); // check there isn't another relation with the same core relation and periods(year/quarter) to do // check survey id $survey_exists = !empty($limesurvey_id) && is_numeric($limesurvey_id); // if not create one if ($survey_exists) { error_log('>>> activating existig survey'); self::activate_survey($sessionKey, $limesurvey_id, $relation_args, $myJSONRPCClient); $myJSONRPCClient->release_session_key($sessionKey); return; } // generate base64 string from import file $import_file = $relation_step . '.lss'; $import_data = base64_encode(file_get_contents(CABSPATH . 'cache/' . $import_file)); $survey_name = self::getSurveyName($relation_args, $relation_step); // import data $import_success_status = $myJSONRPCClient->import_survey($sessionKey, $import_data, 'lss', $survey_name); if (is_array($import_success_status)) { throw new Exception('Failed to import the survey ' . json_encode($import_success_status)); } else { // save the newly created survey id to the relation pod $new_survey_id = $import_success_status; update_post_meta($post_id, $limesurveyid_field_name, $new_survey_id); // activate survey if required self::activate_survey($sessionKey, $new_survey_id, $relation_args, $myJSONRPCClient); // generate corresponding cache self::generateJsonCaches($new_survey_id, $relation_step === '180' ? 'agency' : 'company'); } // release key $myJSONRPCClient->release_session_key($sessionKey); }