$response = array_merge($_POST, $extra_response_attrs); // instanciate a new client $myJSONRPCClient = new jsonRPCClient(LS_BASEURL); // receive session key $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); // save response $success_status = $myJSONRPCClient->add_response($sessionKey, $limesurvey_id, $response); // reset token properties (completed = Y; usesleft = 0) if ($success_status) { $evaluation_pod = pods('evaluation', $evaluation_id); $relation = $evaluation_pod->field('relation'); $relation_pod = pods('relation', $relation['ID']); // survey completed $survey_completed = false; $evaluation_type = $evaluation_pod->field('180_360'); $survey_post = SurveyModel::getByUserAndRelation($current_user->ID, $evaluation_id); $survey_pod = pods('survey', $survey_post['ID']); error_log('survey completed sanity check ' . json_encode($evaluation_type) . ' - ' . json_encode($relation_step)); if ((int) $evaluation_type === 180 && (int) $relation_step === 180) { error_log('survey completed sanity check pass'); $survey_completed = true; } else { $response_180 = $survey_pod->field('response_180'); $response_360 = $survey_pod->field('response_360'); if ((int) $evaluation_type === 360 && (int) $relation_step === 180 && !empty($response_360)) { $survey_completed === true; } if ((int) $evaluation_type === 360 && (int) $relation_step === 360 && !empty($response_180)) { $survey_completed === true; } }
/** * [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); } } }
/** * [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]); } } }
/** * [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']]); } } }