$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']; } } $evaluation_pod = pods('evaluation', $evaluation->ID); $role_questions_counter = ['period' => ['year' => $evaluation_pod->field('period_year'), 'quarter' => $evaluation_pod->field('period_quarter'), 'date' => (int) $evaluation_pod->field('period_year') . $evaluation_pod->field('period_quarter')], 'data' => ['OP' => ['cumulative_question_scores' => [], 'tot' => 0], 'OM' => ['cumulative_question_scores' => [], 'tot' => 0], 'TM' => ['cumulative_question_scores' => [], 'tot' => 0]], 'agency_data' => ['OP' => ['cumulative_question_scores' => [], 'tot' => 0], 'OM' => ['cumulative_question_scores' => [], 'tot' => 0], 'TM' => ['cumulative_question_scores' => [], 'tot' => 0]], 'company_data' => ['OP' => ['cumulative_question_scores' => [], 'tot' => 0], 'OM' => ['cumulative_question_scores' => [], 'tot' => 0], 'TM' => ['cumulative_question_scores' => [], 'tot' => 0]], 'groups' => $group_q]; // setup response structure $relevant_roles = LimesurveyModel::relevantUserRoles(); foreach ($relevant_roles as $rrkey => $curr_role) { $role_questions = $survey_questions[$curr_role]; foreach ($role_questions as $rqkey => $question) { $role_questions_counter['data'][$curr_role]['cumulative_question_scores'][$question['q']['title']] = 0.0; $role_questions_counter['agency_data'][$curr_role]['cumulative_question_scores'][$question['q']['title']] = 0.0; $role_questions_counter['company_data'][$curr_role]['cumulative_question_scores'][$question['q']['title']] = 0.0; } } // 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; }
/** * [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']]); } } }