function create_survey($pieces, $is_new_item, $post_id) { // $is_new_item is never new :( LimesurveyModel::createSurveys($post_id); }
} // get responses $all_responses = $myJSONRPCClient->export_responses($sessionKey, $limesurvey_id, 'json'); // error {status: No Data, could not get max id."} if (is_array($all_responses)) { error_log('No responses ' . json_encode($all_responses)); continue; } $parsed_responses = json_decode(base64_decode($all_responses)); // acquire values foreach ($parsed_responses->responses as $key => $resp) { foreach ($resp as $key => $val) { $curr_response_data = $resp->{$key}; $curr_token = $curr_response_data->token; // token $token = LimesurveyModel::getTokenByID($limesurvey_id, $curr_token); if (!$token) { continue; } $email = $token->email; $user = UserModel::getByEmail($email); // user got deleted if (!$user) { continue; } $user_type = UserModel::userType($user->ID); $curr_role = $user->roles[0]; if (!array_key_exists($curr_role, $survey_questions)) { continue; } $curr_role_questions = $survey_questions[$curr_role];
/** * [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); } } }
$filename = $type . '_' . $limesurvey_id . '_questions.json'; $questions = json_decode(file_get_contents(CABSPATH . 'cache/' . $filename), true); $user_role = $_POST['userRole']; // response output header("Content-Type: application/json"); echo json_encode($questions[$user_role]); exit; }); /** * */ Ajax::run('genRelationSurveys', 'both', function () { $evaluation_id = $_REQUEST['evaluation_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); $survey_ids = []; foreach ($relation_steps as $key => $curr_step) { $questions_type = $curr_step === '360' ? 'company' : 'agency'; $limesurvey_id = EvaluationModel::surveyID($evaluation_id, $curr_step); $survey_ids[] = $limesurvey_id; $groups = LimesurveyModel::generateGroupsJsonCache($limesurvey_id, $questions_type); LimesurveyModel::generateQuestionsJsonCache($limesurvey_id, $questions_type, $groups); } // response output header("Content-Type: application/json"); echo json_encode($survey_ids); exit; });
/** * [getIndex description] * @return [type] [description] */ public function getIndex() { $exploded_url = parseUrl(); $token = count($exploded_url) === 2 ? $exploded_url[1] : false; // not logged in if (is_user_logged_in() && $token) { wp_logout(); $url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}/{$_SERVER['REQUEST_URI']}"; $_SESSION['token'] = null; wp_redirect($url); } elseif (!is_user_logged_in() && $token) { $exploded_url = parseUrl(); if (count($exploded_url) < 2) { return View::make('survey.error', array('error' => 'You need a token to participate in this survey')); } $token = $exploded_url[1]; if (!$token) { return View::make('survey.error', array('error' => 'You need a token to participate in this survey')); } // get corresponding survey $survey = SurveyModel::getByAuthToken($token); if (!$survey) { return View::make('survey.error', array('error' => 'Could not find a matching survey for your token')); } // check if survey is complete $user = get_post_meta($survey['ID'], 'user', true); if (!$user) { return View::make('survey.error', array('error' => 'Could not match your token to a user')); } // login the user wp_set_current_user($user['ID'], $user['user_login']); wp_set_auth_cookie($user['ID']); do_action('wp_login', $user['user_login']); $_SESSION['token'] = $token; wp_redirect(get_bloginfo('url') . '/reporting'); } elseif (!is_user_logged_in() && !$token) { return View::make('survey.error', array('error' => 'Error: Failed to log you in')); } else { if (!isset($_SESSION['token'])) { return View::make('survey.error', array('error' => 'You need a token to access reporting')); } $survey_pod = pods('survey', SurveyModel::getByAuthToken($_SESSION['token'])['ID']); if (!$survey_pod) { return View::make('survey.error', array('error' => 'Could not match your token')); } else { // check for relevant user roles $current_user = wp_get_current_user(); $user_role = $current_user->roles[0]; $roles = LimesurveyModel::relevantUserRoles(); // relevant user roles $user_survey_role_match = in_array($user_role, $roles) ? $user_role : null; if (!$user_survey_role_match) { return View::make('survey.error', array('error' => 'We have no survey for your role ' . $user_role)); } $evaluation = $survey_pod->field('evaluation'); $evaluation_pod = pods('evaluation', $evaluation['ID']); $evaluation_type = $evaluation_pod->field('180_360'); $relation = $evaluation_pod->field('relation'); $relation_pod = pods('relation', $relation['ID']); $company = $relation_pod->field('company'); $company_pod = pods('company', $company['ID']); $logo = $company_pod->field('logo'); return View::make('reporting.index', ['evaluation' => $evaluation['ID'], 'company_logo' => $logo ? $logo['guid'] : false]); } } }
/** * [userSave - callback for generating limesurvey tokens and survey meta info when a user is created] * @param [int] $user_id [the user id of the created user] * @return [type] */ function userSave($user_id) { $user = UserModel::findByID($user_id); error_log('>>> got this far 1'); if ($user) { $relevant_roles = LimesurveyModel::relevantUserRoles(); if (!in_array($user->roles[0], $relevant_roles)) { return; } error_log('>>> got this far 2'); if (!array_key_exists('acf', $_POST)) { error_log('No acf data posted'); return; } // new relations the user is to be associated with $posted_evaluations = $_POST["acf"]["field_555eec0761d79"]; $new_user_evaluations = !empty($posted_evaluations) ? $posted_evaluations : []; $raw_old_user_evaluations = UserModel::getRelations($user_id); $old_user_evaluations = []; error_log('>>> got this far 3'); // push old user relation ID's to array to easily get stale relation bellow using array_diff if (!empty($raw_old_user_evaluations)) { foreach ($raw_old_user_evaluations as $key => $raw_old_relation) { $old_user_evaluations[] = (string) $raw_old_relation->ID; } } // clean-up stale user surveys $stale_evaluations = count($new_user_evaluations) ? array_diff($old_user_evaluations, $new_user_evaluations) : $old_user_evaluations; foreach ($stale_evaluations as $key => $stale_relation) { $stale_survey_post = SurveyModel::getByUserAndRelation($user_id, $stale_relation); if ($stale_survey_post) { $stale_survey_pod = pods('survey', $stale_survey_post['ID']); // only if no invitation was sent $invite_was_sent = $stale_survey_pod->field('invitation_send'); $delete_condition = DEBUG ? true : $invite_was_sent !== "1" && $invite_was_sent !== 1; if ($delete_condition) { $stale_survey_pod->delete(); } } } error_log('>>> got this far 4'); // generate tokens for new relations if required foreach ($new_user_evaluations as $nkey => $nur_id) { error_log('>>> got this far 4a ' . $nur_id); generateTokens($user, $nur_id); } } }
/** * [index - build index view] * @return [view] [display the questionaire] */ public function getQuestionaire() { $exploded_url = parseUrl(); $token = count($exploded_url) === 2 ? $exploded_url[1] : false; // not logged in if (is_user_logged_in() && $token) { wp_logout(); $url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}/{$_SERVER['REQUEST_URI']}"; $_SESSION['token'] = null; wp_redirect($url); } elseif (!is_user_logged_in() && $token) { $exploded_url = parseUrl(); if (count($exploded_url) < 2) { return View::make('survey.error', array('error' => 'You need a token to participate in this survey')); } $token = $exploded_url[1]; if (!$token) { return View::make('survey.error', array('error' => 'You need a token to participate in this survey')); } // get corresponding survey $survey = SurveyModel::getByAuthToken($token); if (!$survey) { return View::make('survey.error', array('error' => 'Could not find a matching survey for your token')); } // check if survey is complete $user = get_post_meta($survey['ID'], 'user', true); if (!$user) { return View::make('survey.error', array('error' => 'Could not match your token to a user')); } // login the user wp_set_current_user($user['ID'], $user['user_login']); wp_set_auth_cookie($user['ID']); do_action('wp_login', $user['user_login']); $_SESSION['token'] = $token; wp_redirect(get_bloginfo('url') . '/survey'); } elseif (!is_user_logged_in() && !$token) { return View::make('survey.error', array('error' => 'Error: Failed to log you in')); } else { if (!isset($_SESSION['token'])) { return View::make('survey.error', array('error' => 'You need a token to participate in this survey')); } $survey_pod = pods('survey', SurveyModel::getByAuthToken($_SESSION['token'])['ID']); if (!$survey_pod) { return View::make('survey.error', array('error' => 'Could not find a matching survey for your token')); } else { // check for relevant user roles $current_user = wp_get_current_user(); $user_role = $current_user->roles[0]; $roles = LimesurveyModel::relevantUserRoles(); // relevant user roles $user_survey_role_match = in_array($user_role, $roles) ? $user_role : null; if (!$user_survey_role_match) { return View::make('survey.error', array('error' => 'We have no survey for your role ' . $user_role)); } $evaluation = $survey_pod->field('evaluation'); $evaluation_pod = pods('evaluation', $evaluation['ID']); $relation = $evaluation_pod->field('relation'); $relation_pod = pods('relation', $relation['ID']); $relation_type = $evaluation_pod->display('180_360'); $user_type = UserModel::userType($current_user->ID); // check for valid tokens for all possible steps $relation_steps = EvaluationModel::getSteps($relation_type); $valid_steps = ['180' => false, '360' => false]; foreach ($relation_steps as $key => $step) { $limesurvey_id = EvaluationModel::surveyID($evaluation['ID'], $step); $curr_survey_token = LimesurveyModel::getTokenByRelation($limesurvey_id, $current_user->user_email, $evaluation['ID']); $valid_steps[$step] = $curr_survey_token ? true : false; } if (!$valid_steps['180'] && !$valid_steps['360']) { return View::make('survey.error', array('error' => 'You have already completed this survey')); } return View::make('survey.main', ['page-title' => 'Procync', 'user' => $current_user, 'user_role' => $user_role, 'user_type' => $user_type, 'user_relationship' => $evaluation['ID'], 'relationship_type' => $relation_type, 'company_or_agency' => get_user_meta($current_user->ID, 'company_or_agency', true), 'agency_surveyid' => EvaluationModel::surveyID($evaluation['ID'], '180'), 'company_surveyid' => EvaluationModel::surveyID($evaluation['ID'], '360'), 'agency_name' => $relation_pod->display('agency'), 'company_name' => $relation_pod->display('company'), 'brand_name' => $relation_pod->display('brand'), 'doStep180' => $valid_steps['180'], 'doStep360' => $valid_steps['360']]); } } }