function delete_evaluation($params, $pod) { error_log('delete evaluation ' . json_encode($params) . '-' . json_encode($pod)); $surveys = EvaluationModel::surveys($params->id); error_log('eval surveys ' . json_encode($surveys)); foreach ($surveys as $key => $survey) { wp_delete_post($survey->ID, true); } }
$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']; $group_q[$rolekey][$sqvalue['g']['id']['gid']]['question_benchmarks'][$sqvalue['q']['title']] = (double) $sqvalue['q']['benchmark']; $group_q[$rolekey][$sqvalue['g']['id']['gid']]['questions_txt'][] = $sqvalue['q']['question']; } }
/** * [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; });
/** * [generate survey tokens and auth token for invitation auto-login] * @param [type] $user [description] * @param [type] $evaluation_id [description] * @return [type] [description] */ function generateTokens($user, $evaluation_id) { error_log('got this far 1'); // is relationship 180 or 360 ? $evaluation_pod = pods('evaluation', $evaluation_id); $relation_pod = pods('relation', $evaluation_pod->field('relation')['ID']); $relation_type = $evaluation_pod->field('180_360'); $relation_steps = EvaluationModel::getSteps($relation_type); error_log('got this far 2 ' . json_encode($relation_steps)); // survey meta info $survey_args = ['user' => $user->ID, 'evaluation' => $evaluation_id, 'user_type' => get_field('company_or_agency', 'user_' . $user->ID), 'token_180' => false, 'token_360' => false, 'survey_id_180' => false, 'survey_id_360' => false, 'auth_token' => randomstring(16)]; // get survey metadata pod $survey = SurveyModel::getByUserAndRelation($user->ID, $evaluation_id); error_log('got this far 3 ' . json_encode($survey)); // for each relation step foreach ($relation_steps as $key => $relation_step) { error_log('got this far 4'); $limesurvey_id = EvaluationModel::surveyID($evaluation_id, $relation_step); error_log('got this far 4a ' . $limesurvey_id); if (empty($limesurvey_id) || !$limesurvey_id) { error_log('Error: generateTokens ' . $limesurvey_id . '-' . $evaluation_id . '-' . $relation_step); return; } $ls_token = LimesurveyModel::getTokenByRelation($limesurvey_id, $user->user_email, $evaluation_id, false); error_log('got this far 4b'); // create a new limesuvey survey token if one doesn't exist if ($ls_token) { error_log('A token already exists ' . LS_USER . ' - ' . LS_PASSWORD); continue; } error_log('got this far 4z '); //limesurvey token $limesurvey_token = [(object) ['email' => $user->user_email, 'firstname' => $user->first_name, 'lastname' => $user->last_name, 'attribute_1' => $user->roles[0], 'attribute_2' => $evaluation_id]]; error_log('got this far 4b2 ' . json_encode($limesurvey_token)); // instantiate a new client $myJSONRPCClient = new jsonRPCClient(LS_BASEURL); error_log('got this far 4b3 ' . json_encode($myJSONRPCClient)); error_log('Le base url ' . LS_BASEURL); // receive session key $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); error_log('got this far 4b4 ' . json_encode($sessionKey)); // create limesurvey token $success_status = $myJSONRPCClient->add_participants($sessionKey, $limesurvey_id, $limesurvey_token); error_log('got this far 4b5 ' . json_encode($success_status)); // release session key $myJSONRPCClient->release_session_key($sessionKey); error_log('got this far 4c '); // check status if (isset($success_status['error'])) { error_log('Error: ' . $success_status['error']); } $token_str = isset($success_status[0]['token']) ? $success_status[0]['token'] : ""; if (empty($token_str)) { error_log('generateTokens() - Token str is empty'); continue; } // update survey meta $survey_args['user'] = $user->ID; $survey_args['token_' . $relation_step] = $token_str; $survey_args['survey_id_' . $relation_step] = $limesurvey_id; // this will add/update the survey meta pod using pods save SurveyModel::save($user->ID, $evaluation_id, $survey_args); error_log('got this far 4'); } error_log('got this far 5'); }
/** * [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']]); } } }
/** * Creates new limesurveys for each step of a new relation i.e. (step1 = 180, step2 = 360) * This is called when a new relation is created * @param [type] $post_id [description] * @return [type] [description] */ public static function createSurveys($post_id) { // get relationship args $evaluation_pod = pods('evaluation', $post_id); $relation_pod = pods('relation', $evaluation_pod->field('relation')['ID']); $relation_type = $evaluation_pod->field('180_360'); $relation_args = (object) ['evaluation_pod' => $evaluation_pod, 'relation_pod' => $relation_pod]; // for each stage of the relationship generate a new limesurvey $relation_steps = EvaluationModel::getSteps($relation_type); foreach ($relation_steps as $key => $relation_step) { self::create($post_id, $relation_args, $relation_step); } }