} // delete relation add_action('pods_api_pre_delete_pod_item_relation', 'delete_relation', 99999, 3); function delete_relation($params, $pod) { error_log('delete relation ' . json_encode($params) . '-' . json_encode($pod)); $evaluations = RelationModel::evaluations($params->id); error_log('rel evaluations ' . json_encode($evaluations)); foreach ($evaluations as $key => $evaluation) { wp_delete_post($evaluation->ID, true); } } // delete user add_action('delete_user', 'remove_user'); function remove_user($user_id) { $survey = pods('survey', ['limit' => -1, 'where' => 'user.ID = ' . $user_id]); while ($survey->fetch()) { $candidate_user = $survey->field('user'); error_log(json_encode($candidate_user)); if ($candidate_user) { error_log(json_encode($candidate_user)); //wp_delete_post($user_survey->ID, true); } } } add_action('wp_ajax_response_seeder', function () { ResponseSeeder::run(); AjaxResponse::stream(['success' => 'success'], true); exit; });
/** * [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); } } }