First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,102 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class is to build the form for deleting a Survey.
*/
class CRM_Campaign_Form_Survey_Delete extends CRM_Core_Form {
/**
* The id of the object being deleted
*
* @var int
*/
protected $_surveyId;
/**
* SurveyTitle
*
* @var string
*/
protected $_surveyTitle;
/**
* Set variables up before form is built.
*/
public function preProcess() {
if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
CRM_Utils_System::permissionDenied();
}
$this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
$params = array('id' => $this->_surveyId);
CRM_Campaign_BAO_Survey::retrieve($params, $surveyInfo);
$this->_surveyTitle = $surveyInfo['title'];
$this->assign('surveyTitle', $this->_surveyTitle);
CRM_Utils_System::setTitle(ts('Delete Survey') . ' - ' . $this->_surveyTitle);
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Delete'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
}
/**
* Process the form when submitted.
*/
public function postProcess() {
if ($this->_surveyId) {
CRM_Campaign_BAO_Survey::del($this->_surveyId);
CRM_Core_Session::setStatus('', ts("'%1' survey has been deleted.", array(1 => $this->_surveyTitle)), 'success');
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'));
}
else {
CRM_Core_Error::fatal(ts('Delete action is missing expected survey ID.'));
}
}
}

View file

@ -0,0 +1,224 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for processing a survey.
*/
class CRM_Campaign_Form_Survey_Main extends CRM_Campaign_Form_Survey {
/* values
*
* @var array
*/
public $_values;
/**
* Context.
*
* @var string
*/
protected $_context;
/**
* Explicitly declare the entity api name.
*/
public function getDefaultEntity() {
return 'Survey';
}
public function preProcess() {
parent::preProcess();
$this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
$this->assign('context', $this->_context);
$this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
if ($this->_action & CRM_Core_Action::UPDATE) {
CRM_Utils_System::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle);
}
// when custom data is included in this page
if (!empty($_POST['hidden_custom'])) {
CRM_Custom_Form_CustomData::preProcess($this);
CRM_Custom_Form_CustomData::buildQuickForm($this);
}
if ($this->_name != 'Petition') {
$url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey Dashboard'), 'url' => $url)));
}
$this->_values = $this->get('values');
if (!is_array($this->_values)) {
$this->_values = array();
if ($this->_surveyId) {
$params = array('id' => $this->_surveyId);
CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
}
$this->set('values', $this->_values);
}
$this->assign('action', $this->_action);
$this->assign('surveyId', $this->_surveyId);
// for custom data
$this->assign('entityID', $this->_surveyId);
}
/**
* Set default values for the form. Note that in edit/view mode
* the default values are retrieved from the database
*
* @return array
* array of default values
*/
public function setDefaultValues() {
$defaults = $this->_values;
if ($this->_surveyId) {
if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
$resultId = $defaults['result_id'];
$recontactInterval = unserialize($defaults['recontact_interval']);
unset($defaults['recontact_interval']);
$defaults['option_group_id'] = $resultId;
}
}
if (!isset($defaults['is_active'])) {
$defaults['is_active'] = 1;
}
$defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
$defaults['is_default'] = 1;
}
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
$surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType();
// Activity Type id
$this->addSelect('activity_type_id', array('option_url' => 'civicrm/admin/campaign/surveyType'), TRUE);
// Campaign id
$campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values));
$this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns);
// script / instructions
$this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40));
// release frequency
$this->add('text', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
$this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
// max reserved contacts at a time
$this->add('text', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts'));
$this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger');
// total reserved per interviewer
$this->add('text', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts'));
$this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger');
// is active ?
$this->add('checkbox', 'is_active', ts('Active?'));
// is default ?
$this->add('checkbox', 'is_default', ts('Default?'));
parent::buildQuickForm();
}
/**
* Process the form.
*/
public function postProcess() {
// store the submitted values in an array
$params = $this->controller->exportValues($this->_name);
$session = CRM_Core_Session::singleton();
$params['last_modified_id'] = $session->get('userID');
$params['last_modified_date'] = date('YmdHis');
if ($this->_surveyId) {
$params['id'] = $this->_surveyId;
}
else {
$params['created_id'] = $session->get('userID');
$params['created_date'] = date('YmdHis');
}
$params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
$params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
$this->_surveyId,
'Survey'
);
$survey = CRM_Campaign_BAO_Survey::create($params);
$this->_surveyId = $survey->id;
if (!empty($this->_values['result_id'])) {
$query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1";
$countSurvey = (int) CRM_Core_DAO::singleValueQuery($query,
array(
1 => array(
$this->_values['result_id'],
'Positive',
),
)
);
// delete option group if no any survey is using it.
if (!$countSurvey) {
CRM_Core_BAO_OptionGroup::del($this->_values['result_id']);
}
}
parent::endPostProcess();
}
}

View file

@ -0,0 +1,140 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for processing a survey.
*/
class CRM_Campaign_Form_Survey_Questions extends CRM_Campaign_Form_Survey {
/**
* Set default values for the form.
*
* Note that in edit/view mode the default values are retrieved from the database.
*
* @return array
* array of default values
*/
public function setDefaultValues() {
$defaults = array();
$ufJoinParams = array(
'entity_table' => 'civicrm_survey',
'module' => 'CiviCampaign',
'entity_id' => $this->_surveyId,
);
list($defaults['contact_profile_id'], $second)
= CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
$defaults['activity_profile_id'] = $second ? array_shift($second) : '';
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$subTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $this->_surveyId, 'activity_type_id');
if (!CRM_Core_BAO_CustomGroup::autoCreateByActivityType($subTypeId)) {
$activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, FALSE); // everything
// FIXME: Displays weird "/\ Array" message; doesn't work with tabs
CRM_Core_Session::setStatus(
ts(
'There are no custom data sets for activity type "%1". To create one, <a href="%2" target="%3">click here</a>.',
array(
1 => $activityTypes[$subTypeId],
2 => CRM_Utils_System::url('civicrm/admin/custom/group', 'action=add&reset=1'),
3 => '_blank',
)
)
);
}
$allowCoreTypes = CRM_Campaign_BAO_Survey::surveyProfileTypes();
$allowSubTypes = array(
'ActivityType' => array($subTypeId),
);
$entities = array(
array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'),
array('entity_name' => 'activity_1', 'entity_type' => 'ActivityModel', 'entity_sub_type' => $subTypeId),
);
$this->addProfileSelector('contact_profile_id', ts('Contact Info'), $allowCoreTypes, $allowSubTypes, $entities);
$this->addProfileSelector('activity_profile_id', ts('Questions'), $allowCoreTypes, $allowSubTypes, $entities);
// Note: Because this is in a tab, we also preload the schema via CRM_Campaign_Form_Survey::preProcess
parent::buildQuickForm();
}
/**
* Process the form.
*/
public function postProcess() {
// store the submitted values in an array
$params = $this->controller->exportValues($this->_name);
// also update the ProfileModule tables
$ufJoinParams = array(
'is_active' => 1,
'module' => 'CiviCampaign',
'entity_table' => 'civicrm_survey',
'entity_id' => $this->_surveyId,
);
// first delete all past entries
CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
$uf = array();
$wt = 2;
if (!empty($params['contact_profile_id'])) {
$uf[1] = $params['contact_profile_id'];
$wt = 1;
}
if (!empty($params['activity_profile_id'])) {
$uf[2] = $params['activity_profile_id'];
}
$uf = array_values($uf);
if (!empty($uf)) {
foreach ($uf as $weight => $ufGroupId) {
$ufJoinParams['weight'] = $weight + $wt;
$ufJoinParams['uf_group_id'] = $ufGroupId;
CRM_Core_BAO_UFJoin::create($ufJoinParams);
unset($ufJoinParams['id']);
}
}
parent::endPostProcess();
}
}

View file

@ -0,0 +1,503 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class generates form components for processing a survey.
*/
class CRM_Campaign_Form_Survey_Results extends CRM_Campaign_Form_Survey {
protected $_reportId;
protected $_reportTitle;
/* values
*
* @var array
*/
public $_values;
const NUM_OPTION = 11;
public function preProcess() {
parent::preProcess();
$this->_values = $this->get('values');
if (!is_array($this->_values)) {
$this->_values = array();
if ($this->_surveyId) {
$params = array('id' => $this->_surveyId);
CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
}
$this->set('values', $this->_values);
}
$query = "SELECT MAX(id) as id, title FROM civicrm_report_instance WHERE name = %1 GROUP BY id";
$params = array(1 => array("survey_{$this->_surveyId}", 'String'));
$result = CRM_Core_DAO::executeQuery($query, $params);
if ($result->fetch()) {
$this->_reportId = $result->id;
$this->_reportTitle = $result->title;
}
}
/**
* Set default values for the form.
*
* Note that in edit/view mode the default values are retrieved from the database.
*
* @return array
* array of default values
*/
public function setDefaultValues() {
$defaults = $this->_values;
// set defaults for weight.
for ($i = 1; $i <= self::NUM_OPTION; $i++) {
$defaults["option_weight[{$i}]"] = $i;
}
$defaults['create_report'] = 1;
if ($this->_reportId) {
$defaults['report_title'] = $this->_reportTitle;
}
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$optionGroups = CRM_Campaign_BAO_Survey::getResultSets();
if (empty($optionGroups)) {
$optionTypes = array('1' => ts('Create new result set'));
}
else {
$optionTypes = array(
'1' => ts('Create new result set'),
'2' => ts('Use existing result set'),
);
$this->add('select',
'option_group_id',
ts('Select Result Set'),
array(
'' => ts('- select -'),
) + $optionGroups, FALSE,
array('onChange' => 'loadOptionGroup( )')
);
}
$element = &$this->addRadio('option_type',
ts('Survey Responses'),
$optionTypes,
array(
'onclick' => "showOptionSelect();",
), '<br/>', TRUE
);
if (empty($optionGroups) || empty($this->_values['result_id'])) {
$this->setdefaults(array('option_type' => 1));
}
elseif (!empty($this->_values['result_id'])) {
$this->setdefaults(array(
'option_type' => 2,
'option_group_id' => $this->_values['result_id'],
));
}
// form fields of Custom Option rows
$defaultOption = array();
$_showHide = new CRM_Core_ShowHideBlocks('', '');
$optionAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue');
$optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25;
for ($i = 1; $i <= self::NUM_OPTION; $i++) {
//the show hide blocks
$showBlocks = 'optionField_' . $i;
if ($i > 2) {
$_showHide->addHide($showBlocks);
if ($i == self::NUM_OPTION) {
$_showHide->addHide('additionalOption');
}
}
else {
$_showHide->addShow($showBlocks);
}
$this->add('text', 'option_label[' . $i . ']', ts('Label'),
$optionAttributes['label']
);
// value
$this->add('text', 'option_value[' . $i . ']', ts('Value'),
$optionAttributes['value']
);
// weight
$this->add('text', "option_weight[$i]", ts('Order'),
$optionAttributes['weight']
);
$this->add('text', 'option_interval[' . $i .
']', ts('Recontact Interval'),
CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')
);
$defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
}
//default option selection
$this->addGroup($defaultOption, 'default_option');
$_showHide->addToTemplate();
$this->addElement('checkbox', 'create_report', ts('Create Report'));
$this->addElement('text', 'report_title', ts('Report Title'));
if ($this->_reportId) {
$this->freeze('create_report');
$this->freeze('report_title');
}
$this->addFormRule(array(
'CRM_Campaign_Form_Survey_Results',
'formRule',
), $this);
parent::buildQuickForm();
}
/**
* Global validation rules for the form.
*
* @param $fields
* @param $files
* @param $form
*
* @return array|bool
*/
public static function formRule($fields, $files, $form) {
$errors = array();
if (!empty($fields['option_label']) && !empty($fields['option_value']) &&
(count(array_filter($fields['option_label'])) == 0) &&
(count(array_filter($fields['option_value'])) == 0)
) {
$errors['option_label[1]'] = ts('Enter at least one result option.');
return $errors;
}
elseif (empty($fields['option_label']) && empty($fields['option_value'])) {
return $errors;
}
if (
$fields['option_type'] == 2 && empty($fields['option_group_id'])
) {
$errors['option_group_id'] = ts("Please select a Survey Result Set.");
return $errors;
}
$_flagOption = $_rowError = 0;
$_showHide = new CRM_Core_ShowHideBlocks('', '');
//capture duplicate Custom option values
if (!empty($fields['option_value'])) {
$countValue = count($fields['option_value']);
$uniqueCount = count(array_unique($fields['option_value']));
if ($countValue > $uniqueCount) {
$start = 1;
while ($start < self::NUM_OPTION) {
$nextIndex = $start + 1;
while ($nextIndex <= self::NUM_OPTION) {
if ($fields['option_value'][$start] ==
$fields['option_value'][$nextIndex] &&
!empty($fields['option_value'][$nextIndex])
) {
$errors['option_value[' . $start .
']'] = ts('Duplicate Option values');
$errors['option_value[' . $nextIndex .
']'] = ts('Duplicate Option values');
$_flagOption = 1;
}
$nextIndex++;
}
$start++;
}
}
}
//capture duplicate Custom Option label
if (!empty($fields['option_label'])) {
$countValue = count($fields['option_label']);
$uniqueCount = count(array_unique($fields['option_label']));
if ($countValue > $uniqueCount) {
$start = 1;
while ($start < self::NUM_OPTION) {
$nextIndex = $start + 1;
while ($nextIndex <= self::NUM_OPTION) {
if ($fields['option_label'][$start] ==
$fields['option_label'][$nextIndex] &&
!empty($fields['option_label'][$nextIndex])
) {
$errors['option_label[' . $start .
']'] = ts('Duplicate Option label');
$errors['option_label[' . $nextIndex .
']'] = ts('Duplicate Option label');
$_flagOption = 1;
}
$nextIndex++;
}
$start++;
}
}
}
for ($i = 1; $i <= self::NUM_OPTION; $i++) {
if (!$fields['option_label'][$i]) {
if ($fields['option_value'][$i]) {
$errors['option_label[' . $i .
']'] = ts('Option label cannot be empty');
$_flagOption = 1;
}
else {
$_emptyRow = 1;
}
}
elseif (!strlen(trim($fields['option_value'][$i]))) {
if (!$fields['option_value'][$i]) {
$errors['option_value[' . $i .
']'] = ts('Option value cannot be empty');
$_flagOption = 1;
}
}
if (!empty($fields['option_interval'][$i]) &&
!CRM_Utils_Rule::integer($fields['option_interval'][$i])
) {
$_flagOption = 1;
$errors['option_interval[' . $i .
']'] = ts('Please enter a valid integer.');
}
$showBlocks = 'optionField_' . $i;
if ($_flagOption) {
$_showHide->addShow($showBlocks);
$_rowError = 1;
}
if (!empty($_emptyRow)) {
$_showHide->addHide($showBlocks);
}
else {
$_showHide->addShow($showBlocks);
}
if ($i == self::NUM_OPTION) {
$hideBlock = 'additionalOption';
$_showHide->addHide($hideBlock);
}
$_flagOption = $_emptyRow = 0;
}
$_showHide->addToTemplate();
return empty($errors) ? TRUE : $errors;
}
/**
* Process the form.
*/
public function postProcess() {
// store the submitted values in an array
$status = '';
$params = $this->controller->exportValues($this->_name);
$params['id'] = $this->_surveyId;
$updateResultSet = FALSE;
$resultSetOptGrpId = NULL;
if ((CRM_Utils_Array::value('option_type', $params) == 2) &&
!empty($params['option_group_id'])
) {
$updateResultSet = TRUE;
$resultSetOptGrpId = $params['option_group_id'];
}
$recontactInterval = array();
if ($updateResultSet) {
$optionValue = new CRM_Core_DAO_OptionValue();
$optionValue->option_group_id = $resultSetOptGrpId;
$optionValue->delete();
$params['result_id'] = $resultSetOptGrpId;
}
else {
$opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis');
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->name = $opGroupName;
$optionGroup->title = $this->_values['title'] . ' Result Set';
$optionGroup->is_active = 1;
$optionGroup->save();
$params['result_id'] = $optionGroup->id;
}
foreach ($params['option_value'] as $k => $v) {
if (strlen(trim($v))) {
$optionValue = new CRM_Core_DAO_OptionValue();
$optionValue->option_group_id = $params['result_id'];
$optionValue->label = $params['option_label'][$k];
$optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]);
$optionValue->value = trim($v);
$optionValue->weight = $params['option_weight'][$k];
$optionValue->is_active = 1;
if (!empty($params['default_option']) &&
$params['default_option'] == $k
) {
$optionValue->is_default = 1;
}
$optionValue->save();
// using is_numeric since 0 is a valid value for option_interval
if (is_numeric($params['option_interval'][$k])) {
$recontactInterval[$optionValue->label] = $params['option_interval'][$k];
}
}
}
$params['recontact_interval'] = serialize($recontactInterval);
$survey = CRM_Campaign_BAO_Survey::create($params);
// create report if required.
if (!$this->_reportId && $survey->id && !empty($params['create_report'])) {
$activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
$activityStatus = array_flip($activityStatus);
$this->_params = array(
'name' => "survey_{$survey->id}",
'title' => $params['report_title'] ? $params['report_title'] : $this->_values['title'],
'status_id_op' => 'eq',
'status_id_value' => $activityStatus['Scheduled'], // reserved status
'survey_id_value' => array($survey->id),
'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'),
);
//Default value of order by
$this->_params['order_bys'] = array(
1 => array(
'column' => 'sort_name',
'order' => 'ASC',
),
);
// for WalkList or default
$displayFields = array(
'id',
'sort_name',
'result',
'street_number',
'street_name',
'street_unit',
'survey_response',
);
if (CRM_Core_OptionGroup::getValue('activity_type', 'WalkList') ==
$this->_values['activity_type_id']
) {
$this->_params['order_bys'] = array(
1 => array(
'column' => 'street_name',
'order' => 'ASC',
),
2 => array(
'column' => 'street_number_odd_even',
'order' => 'ASC',
),
3 => array(
'column' => 'street_number',
'order' => 'ASC',
),
4 => array(
'column' => 'sort_name',
'order' => 'ASC',
),
);
}
elseif (CRM_Core_OptionGroup::getValue('activity_type', 'PhoneBank') ==
$this->_values['activity_type_id']
) {
array_push($displayFields, 'phone');
}
elseif ((CRM_Core_OptionGroup::getValue('activity_type', 'Survey') ==
$this->_values['activity_type_id']) ||
(CRM_Core_OptionGroup::getValue('activity_type', 'Canvass') ==
$this->_values['activity_type_id'])
) {
array_push($displayFields, 'phone', 'city', 'state_province_id', 'postal_code', 'email');
}
foreach ($displayFields as $key) {
$this->_params['fields'][$key] = 1;
}
$this->_createNew = TRUE;
$this->_id = CRM_Report_Utils_Report::getInstanceIDForValue('survey/detail');
CRM_Report_Form_Instance::postProcess($this, FALSE);
$query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1";
$reportID = CRM_Core_DAO::singleValueQuery($query, array(
1 => array(
"survey_{$survey->id}",
'String',
),
));
if ($reportID) {
$url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
$status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.",
array(1 => $url, 2 => $this->_params['title']));
}
}
if ($status) {
// reset status as we don't want status set by Instance::postProcess
$session = CRM_Core_Session::singleton();
$session->getStatus(TRUE);
// set new status
CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
}
parent::endPostProcess();
}
}

View file

@ -0,0 +1,189 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Helper class to build navigation links
*/
class CRM_Campaign_Form_Survey_TabHeader {
/**
* Build tab header.
*
* @param CRM_Core_Form $form
*
* @return array
*/
public static function build(&$form) {
$tabs = $form->get('tabHeader');
if (!$tabs || empty($_GET['reset'])) {
$tabs = self::process($form);
$form->set('tabHeader', $tabs);
}
$form->assign_by_ref('tabHeader', $tabs);
CRM_Core_Resources::singleton()
->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
->addSetting(array(
'tabSettings' => array(
'active' => self::getCurrentTab($tabs),
),
));
return $tabs;
}
/**
* @param CRM_Core_Form $form
*
* @return array
*/
public static function process(&$form) {
if ($form->getVar('_surveyId') <= 0) {
return NULL;
}
$tabs = array(
'main' => array(
'title' => ts('Main Information'),
'link' => NULL,
'valid' => FALSE,
'active' => FALSE,
'current' => FALSE,
),
'questions' => array(
'title' => ts('Questions'),
'link' => NULL,
'valid' => FALSE,
'active' => FALSE,
'current' => FALSE,
),
'results' => array(
'title' => ts('Results'),
'link' => NULL,
'valid' => FALSE,
'active' => FALSE,
'current' => FALSE,
),
);
$surveyID = $form->getVar('_surveyId');
$class = $form->getVar('_name');
$class = CRM_Utils_String::getClassName($class);
$class = strtolower($class);
if (array_key_exists($class, $tabs)) {
$tabs[$class]['current'] = TRUE;
$qfKey = $form->get('qfKey');
if ($qfKey) {
$tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
}
}
if ($surveyID) {
$reset = !empty($_GET['reset']) ? 'reset=1&' : '';
foreach ($tabs as $key => $value) {
if (!isset($tabs[$key]['qfKey'])) {
$tabs[$key]['qfKey'] = NULL;
}
$tabs[$key]['link'] = CRM_Utils_System::url("civicrm/survey/configure/{$key}",
"{$reset}action=update&id={$surveyID}{$tabs[$key]['qfKey']}"
);
$tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
}
}
return $tabs;
}
/**
* @param CRM_Core_Form $form
*/
public static function reset(&$form) {
$tabs = self::process($form);
$form->set('tabHeader', $tabs);
}
/**
* @param $tabs
*
* @return int|string
*/
public static function getCurrentTab($tabs) {
static $current = FALSE;
if ($current) {
return $current;
}
if (is_array($tabs)) {
foreach ($tabs as $subPage => $pageVal) {
if ($pageVal['current'] === TRUE) {
$current = $subPage;
break;
}
}
}
$current = $current ? $current : 'main';
return $current;
}
/**
* @param $form
*
* @return int|string
*/
public static function getNextTab(&$form) {
static $next = FALSE;
if ($next) {
return $next;
}
$tabs = $form->get('tabHeader');
if (is_array($tabs)) {
$current = FALSE;
foreach ($tabs as $subPage => $pageVal) {
if ($current) {
$next = $subPage;
break;
}
if ($pageVal['current'] === TRUE) {
$current = $subPage;
}
}
}
$next = $next ? $next : 'main';
return $next;
}
}