public function onCareerPortalQuestionnaire() { if ($this->_realAccessLevel < ACCESS_LEVEL_DEMO) { CommonErrors::fatal(COMMONERROR_PERMISSION, $this); return; } if (!isset($_SESSION['CATS_QUESTIONNAIRE']) || empty($_SESSION['CATS_QUESTIONNAIRE'])) { CommonErrors::fatal(COMMONERROR_BADINDEX, 'Please return to your careers website ' . 'and load the questionnaire a second time as your session has ' . 'expired.'); } // Get the title $title = isset($_POST[$id = 'title']) ? substr(trim($_POST[$id]), 0, 255) : ''; if (!strlen($title)) { $title = ''; } // Get the description $description = isset($_POST[$id = 'description']) ? substr(trim($_POST[$id]), 0, 255) : ''; if (!strlen($description)) { $description = ''; } // Is this active? $active = isset($_POST[$id = 'isActive']) ? !strcasecmp($_POST[$id], 'yes') : 0; $_SESSION['CATS_QUESTIONNAIRE']['title'] = $title; $_SESSION['CATS_QUESTIONNAIRE']['description'] = $description; $_SESSION['CATS_QUESTIONNAIRE']['isActive'] = $active ? true : false; $questionnaire = new Questionnaire($this->_siteID); $questions = $_SESSION['CATS_QUESTIONNAIRE']['questions']; /** * STEP 1 * Check for changes to question and answer texts, mark questions or * answers that the user specified to remove as "remove" which will be done * in the final step to prevent index changes. */ for ($questionIndex = 0; $questionIndex < count($questions); $questionIndex++) { // Update the position of the question $field = sprintf('question%dPosition', $questionIndex); if (isset($_POST[$field])) { $position = intval(trim($_POST[$field])); $questions[$questionIndex]['questionPosition'] = $position; } // Update the text of the question $field = sprintf('question%dTextValue', $questionIndex); if (isset($_POST[$field])) { if (strlen($text = substr(trim($_POST[$field]), 0, 255))) { $questions[$questionIndex]['questionText'] = $text; } } // Update the type of the question $field = sprintf('question%dTypeValue', $questionIndex); if (isset($_POST[$field])) { $type = $questionnaire->convertQuestionTypeToConstant($_POST[$field]); $questions[$questionIndex]['questionType'] = $type; $questions[$questionIndex]['questionTypeLabel'] = $questionnaire->convertQuestionConstantToType($type); } // Check if this question should be removed (user checked the box) $field = sprintf('question%dRemove', $questionIndex); if (isset($_POST[$field]) && !strcasecmp($_POST[$field], 'yes')) { $questions[$questionIndex]['remove'] = true; } else { $questions[$questionIndex]['remove'] = false; } for ($answerIndex = 0; $answerIndex < count($questions[$questionIndex]['answers']); $answerIndex++) { // Update the position of the question $field = sprintf('question%dAnswer%dPosition', $questionIndex, $answerIndex); if (isset($_POST[$field])) { $position = intval(trim($_POST[$field])); $questions[$questionIndex]['answers'][$answerIndex]['answerPosition'] = $position; } // Update the text of the answer $field = sprintf('question%dAnswer%dTextValue', $questionIndex, $answerIndex); if (isset($_POST[$field])) { if (strlen($text = substr(trim($_POST[$field]), 0, 255))) { $questions[$questionIndex]['answers'][$answerIndex]['answerText'] = $text; } } // Check if this answer should be removed (user checked the box) $field = sprintf('question%dAnswer%dRemove', $questionIndex, $answerIndex); if (isset($_POST[$field]) && !strcasecmp($_POST[$field], 'yes')) { $questions[$questionIndex]['answers'][$answerIndex]['remove'] = true; } else { $questions[$questionIndex]['answers'][$answerIndex]['remove'] = false; } // Check the actions for whether or not they should exist $actionSourceField = sprintf('question%dAnswer%dActionSource', $questionIndex, $answerIndex); $actionNotesField = sprintf('question%dAnswer%dActionNotes', $questionIndex, $answerIndex); $actionIsHotField = sprintf('question%dAnswer%dActionIsHot', $questionIndex, $answerIndex); $actionIsActiveField = sprintf('question%dAnswer%dActionIsActive', $questionIndex, $answerIndex); $actionCanRelocateField = sprintf('question%dAnswer%dActionCanRelocate', $questionIndex, $answerIndex); $actionKeySkillsField = sprintf('question%dAnswer%dActionKeySkills', $questionIndex, $answerIndex); $actionSourceActive = isset($_POST[$id = $actionSourceField . 'Active']) ? $_POST[$id] : ''; $actionNotesActive = isset($_POST[$id = $actionNotesField . 'Active']) ? $_POST[$id] : ''; $actionIsHotActive = isset($_POST[$id = $actionIsHotField . 'Active']) ? $_POST[$id] : ''; $actionIsActiveActive = isset($_POST[$id = $actionIsActiveField . 'Active']) ? $_POST[$id] : ''; $actionCanRelocateActive = isset($_POST[$id = $actionCanRelocateField . 'Active']) ? $_POST[$id] : ''; $actionKeySkillsActive = isset($_POST[$id = $actionKeySkillsField . 'Active']) ? $_POST[$id] : ''; $actionSourceValue = isset($_POST[$id = $actionSourceField . 'Value']) ? $_POST[$id] : ''; $actionNotesValue = isset($_POST[$id = $actionNotesField . 'Value']) ? $_POST[$id] : ''; $actionIsHotValue = isset($_POST[$id = $actionIsHotField . 'Value']) ? $_POST[$id] : ''; $actionIsActiveValue = isset($_POST[$id = $actionIsActiveField . 'Value']) ? $_POST[$id] : ''; $actionCanRelocateValue = isset($_POST[$id = $actionCanRelocateField . 'Value']) ? $_POST[$id] : ''; $actionKeySkillsValue = isset($_POST[$id = $actionKeySkillsField . 'Value']) ? $_POST[$id] : ''; $questions[$questionIndex]['answers'][$answerIndex]['actionSource'] = strcasecmp($actionSourceActive, 'yes') ? '' : $actionSourceValue; $questions[$questionIndex]['answers'][$answerIndex]['actionNotes'] = strcasecmp($actionNotesActive, 'yes') ? '' : $actionNotesValue; $questions[$questionIndex]['answers'][$answerIndex]['actionIsHot'] = strcasecmp($actionIsHotActive, 'yes') ? 0 : 1; $questions[$questionIndex]['answers'][$answerIndex]['actionIsActive'] = strcasecmp($actionIsActiveActive, 'yes') ? 1 : 0; $questions[$questionIndex]['answers'][$answerIndex]['actionCanRelocate'] = strcasecmp($actionCanRelocateActive, 'yes') ? 0 : 1; $questions[$questionIndex]['answers'][$answerIndex]['actionKeySkills'] = strcasecmp($actionKeySkillsActive, 'yes') ? '' : $actionKeySkillsValue; } } /** * STEP 2 * Perform addition requests like add question, answer or action. We do this before * performing the removal step because if a user removes a question and adds a answer * to it in the same step, the indexes will be misaligned. This way, the addition is * processed and then immediately removed if requested by the user (which is naughty). */ $restrictAction = isset($_POST[$id = 'restrictAction']) ? $_POST[$id] : ''; $restrictQuestionID = isset($_POST[$id = 'restrictActionQuestionID']) ? intval($_POST[$id]) : ''; $restrictAnswerID = isset($_POST[$id = 'restrictActionAnswerID']) ? intval($_POST[$id]) : ''; if (!strcasecmp($restrictAction, 'question')) { // Adding a new question to the questionnaire $questionText = isset($_POST[$id = 'questionText']) ? trim($_POST[$id]) : ''; $questionTypeText = isset($_POST[$id = 'questionType']) ? $_POST[$id] : ''; // Make sure the question doesn't already exist (re-submit) for ($i = 0, $exists = false; $i < count($questions); $i++) { if (!strcmp($questions[$i]['questionText'], $questionText)) { $exists = true; } } if (strlen($questionText) && !$exists) { $questions[] = array('questionID' => -1, 'questionType' => QUESTIONNAIRE_QUESTION_TYPE_TEXT, 'questionTypeLabel' => $questionnaire->convertQuestionConstantToType(QUESTIONNAIRE_QUESTION_TYPE_TEXT), 'questionText' => $questionText, 'minimumLength' => 0, 'maximumLength' => 255, 'questionPosition' => 1000, 'answers' => array()); } } else { if (!strcasecmp($restrictAction, 'answer') && isset($questions[$restrictQuestionID])) { // Adding a new answer to an existing question $field = sprintf('question%dAnswerText', $restrictQuestionID); $answerText = substr(trim(isset($_POST[$field]) ? $_POST[$field] : ''), 0, 255); if (strlen($answerText)) { $questions[$restrictQuestionID]['answers'][] = array('answerID' => -1, 'answerText' => $answerText, 'actionSource' => '', 'actionNotes' => '', 'actionIsHot' => 0, 'actionIsActive' => 1, 'actionCanRelocate' => 0, 'actionKeySkills' => '', 'answerPosition' => 1000); } } else { if (!strcasecmp($restrictAction, 'action') && isset($questions[$restrictQuestionID]) && isset($questions[$restrictQuestionID]['answers'][$restrictAnswerID])) { // Adding a new action to an existing answer of an existing question $field = sprintf('question%dAnswer%d', $restrictQuestionID, $restrictAnswerID); $newAction = isset($_POST[$id = $field . 'NewAction']) ? $_POST[$id] : ''; $actionText = substr(trim(isset($_POST[$id = $field . 'NewActionText']) ? $_POST[$id] : ''), 0, 255); if (isset($questions[$restrictQuestionID]['answers'][$restrictAnswerID][$newAction])) { switch ($newAction) { case 'actionSource': case 'actionNotes': case 'actionKeySkills': $value = $actionText; break; case 'actionIsActive': $value = 0; break; default: $value = 1; break; } $questions[$restrictQuestionID]['answers'][$restrictAnswerID][$newAction] = $value; } } } } /** * STEP 5 * Remove any questions/answers that have "remove" checked prior to sorting/positioning */ $savedQuestions = array(); for ($questionIndex = 0, $savedQuestionIndex = 0; $questionIndex < count($questions); $questionIndex++) { if (isset($questions[$questionIndex]['remove']) && $questions[$questionIndex]['remove']) { continue; } $savedQuestions[$savedQuestionIndex] = $questions[$questionIndex]; $savedQuestions[$savedQuestionIndex]['answers'] = array(); for ($answerIndex = 0; $answerIndex < count($questions[$questionIndex]['answers']); $answerIndex++) { if (isset($questions[$questionIndex]['answers'][$answerIndex]['remove']) && $questions[$questionIndex]['answers'][$answerIndex]['remove']) { continue; } $savedQuestions[$savedQuestionIndex]['answers'][] = $questions[$questionIndex]['answers'][$answerIndex]; } $savedQuestionIndex++; } $questions = $savedQuestions; /** * STEP 6 * Corrections. Any removals or changes that have altered the "way of things" need to * be fixed before sort. */ for ($questionIndex = 0; $questionIndex < count($questions); $questionIndex++) { // If the question has no answers it is a TEXT automatically if (!count($questions[$questionIndex]['answers'])) { $questions[$questionIndex]['questionType'] = QUESTIONNAIRE_QUESTION_TYPE_TEXT; $questions[$questionIndex]['questionTypeLabel'] = $questionnaire->convertQuestionConstantToType(QUESTIONNAIRE_QUESTION_TYPE_TEXT); } else { if ($questions[$questionIndex]['questionType'] == QUESTIONNAIRE_QUESTION_TYPE_TEXT) { $questions[$questionIndex]['questionType'] = QUESTIONNAIRE_QUESTION_TYPE_SELECT; $questions[$questionIndex]['questionTypeLabel'] = $questionnaire->convertQuestionConstantToType(QUESTIONNAIRE_QUESTION_TYPE_SELECT); } } } /** * STEP 7 * Perform a bubble sort on the questions and answers. Then provide real values * (1, 2, 3) based on the results. */ for ($questionIndex2 = 0; $questionIndex2 < count($questions); $questionIndex2++) { if ($questionIndex2 < count($questions) - 1) { for ($questionIndex3 = 0; $questionIndex3 < count($questions) - 1; $questionIndex3++) { if (intval($questions[$questionIndex3]['questionPosition']) > intval($questions[$questionIndex3 + 1]['questionPosition'])) { $tmp = $questions[$questionIndex3]; $questions[$questionIndex3] = $questions[$questionIndex3 + 1]; $questions[$questionIndex3 + 1] = $tmp; } } } // Bubble sort the answers for each question using the same method for ($answerIndex2 = 0; $answerIndex2 < count($questions[$questionIndex2]['answers']) - 1; $answerIndex2++) { for ($answerIndex3 = 0; $answerIndex3 < count($questions[$questionIndex2]['answers']) - 1; $answerIndex3++) { if (intval($questions[$questionIndex2]['answers'][$answerIndex3]['answerPosition']) > intval($questions[$questionIndex2]['answers'][$answerIndex3 + 1]['answerPosition'])) { $tmp = $questions[$questionIndex2]['answers'][$answerIndex3]; $questions[$questionIndex2]['answers'][$answerIndex3] = $questions[$questionIndex2]['answers'][$answerIndex3 + 1]; $questions[$questionIndex2]['answers'][$answerIndex3 + 1] = $tmp; } } } } // Now define real position values (never trust the naughty user) for ($questionIndex2 = 0; $questionIndex2 < count($questions); $questionIndex2++) { $questions[$questionIndex2]['questionPosition'] = $questionIndex2 + 1; for ($answerIndex2 = 0; $answerIndex2 < count($questions[$questionIndex2]['answers']); $answerIndex2++) { $questions[$questionIndex2]['answers'][$answerIndex2]['answerPosition'] = $answerIndex2 + 1; } } if (isset($_POST[$id = 'startOver']) && !strcasecmp($_POST[$id], 'yes')) { // User wants to start over $_SESSION['CATS_QUESTIONNAIRE']['questions'] = array(); } else { if (isset($_POST[$id = 'saveChanges']) && !strcasecmp($_POST[$id], 'yes')) { // User wants to add the new questionnaire if (($id = intval($_SESSION['CATS_QUESTIONNAIRE']['id'])) != 0) { $questionnaire->update($id, $_SESSION['CATS_QUESTIONNAIRE']['title'], $_SESSION['CATS_QUESTIONNAIRE']['description'], $_SESSION['CATS_QUESTIONNAIRE']['isActive']); } else { $id = $questionnaire->add($_SESSION['CATS_QUESTIONNAIRE']['title'], $_SESSION['CATS_QUESTIONNAIRE']['description'], $_SESSION['CATS_QUESTIONNAIRE']['isActive']); } if ($id !== false) { // Delete all existing questions/answers (replace with session values) $questionnaire->deleteQuestions($id); // Save the questions to the new or old questionnaire $questionnaire->addQuestions($id, $_SESSION['CATS_QUESTIONNAIRE']['questions']); CATSUtility::transferRelativeURI('m=settings&a=careerPortalSettings'); return; } } else { // Now save changes to the session $_SESSION['CATS_QUESTIONNAIRE']['questions'] = $questions; } } // Now view the page as if we've just loaded it from the database $this->careerPortalQuestionnaire(true); }