/** * 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>'; }
} } foreach ($role_questions_counter['company_data'] as $crole_key => $crole_value) { foreach ($crole_value['cumulative_question_scores'] as $cqkey => $cqvalue) { $role_questions_counter['company_data'][$crole_key]['cumulative_question_scores'][$cqkey] = $cqvalue > 0 ? $cqvalue / $crole_value['tot'] : 0; } } foreach ($role_questions_counter['agency_data'] as $arole_key => $arole_value) { foreach ($arole_value['cumulative_question_scores'] as $aqkey => $aqvalue) { $role_questions_counter['agency_data'][$arole_key]['cumulative_question_scores'][$aqkey] = $aqvalue > 0 ? $aqvalue / $arole_value['tot'] : 0; } } $response[] = $role_questions_counter; } // release the session key $myJSONRPCClient->release_session_key($sessionKey); if (count($response) > 0) { usort($response, function ($a, $b) { return $a['period']['date'] - $b['period']['date']; }); } // response output header("Content-Type: application/json"); echo json_encode($response); exit; }); Ajax::run('getRelation', 'both', function () { $evaluation_id = array_key_exists('evaluation_id', $_REQUEST) ? (int) $_REQUEST['evaluation_id'] : null; $ret = []; if ($evaluation_id) { $evaluation_pod = pods('evaluation', (int) $evaluation_id);
/** * [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); } } }
/** * [getQuestions - acquire questions belonging to a given survey] * @param [int] $limesurvey_id [the limesurvey id] * @param [string] $user_role [role name] * @return [array] [the filtered questionaire for a given user role] */ public static function getQuestions($limesurvey_id, $user_role, $groups) { $start = microtime_float(); $myJSONRPCClient = new jsonRPCClient(LS_BASEURL); $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); // get survey questions $questions_out = []; foreach ($groups as $group) { // get group questions $group_id = $group['group']['id']['gid']; $questions = $myJSONRPCClient->list_questions($sessionKey, $limesurvey_id, $group_id); $group_questions_out = []; foreach ($questions as $question) { $question_id = $question['id']['qid']; $question_properties = $myJSONRPCClient->get_question_properties($sessionKey, $question_id, ['relevance', 'help', 'question_order']); $question_out = $question; $raw_question = explode('|', strip_tags($question_out['question'])); if (count($raw_question) !== 2) { throw new Exception('Error: wrong number of parameters when extracting question info ' . count($raw_question)); } $question_out['question'] = $raw_question[0]; $question_out['benchmark'] = $raw_question[1]; $question_out['help'] = (double) $question_properties['help']; $question_out['order'] = isset($question_properties['question_order']) ? $question_properties['question_order'] : false; $question_out['relevance'] = self::getRoleFromLimesurveyRelevanceAttribute($question_properties['relevance']); $group_questions_out[] = ['q' => $question_out, 'g' => $group['group'], 'q_num' => 0]; } usort($group_questions_out, function ($a, $b) { return $a['q']['order'] - $b['q']['order']; }); $question_number = 1; foreach ($group_questions_out as $key => $curr_question_out) { $group_questions_out[$key]['q_num'] = $question_number; $question_number++; } $questions_out = array_merge($questions_out, $group_questions_out); } $myJSONRPCClient->release_session_key($sessionKey); $time_elapsed = microtime_float() - $start; $roles = ['OP', 'OM', 'TM']; $questions_by_role = []; foreach ($roles as $role) { foreach ($questions_out as $question) { if (in_array($role, $question['q']['relevance'])) { $questions_by_role[$role][] = $question; } } } return $questions_by_role; }