Skip to content

Commit

Permalink
Merge pull request #131 from compucorp/integration
Browse files Browse the repository at this point in the history
Sync master with Integration
  • Loading branch information
davialexandre committed Jun 7, 2016
2 parents 140d2cc + eaf6b0a commit 978cf7f
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 86 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
145 changes: 71 additions & 74 deletions civihr_employee_portal/civihr_employee_portal.module
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ function _setup_modals() {
* Implements hook_init().
*/
function civihr_employee_portal_init() {

// @TODO -> init civi only on pages where we need to (e.g. Dashboard)
// Civi init + load the singleton needed for the AJAX calls
civicrm_initialize();
Expand All @@ -96,32 +95,22 @@ function civihr_employee_portal_init() {
$rebuild_absence_list_view = variable_get('rebuild_absence_list_view', 'TRUE');

if (isset($rebuild_absence_list_view) && $rebuild_absence_list_view == 'TRUE') {

try {

// Civi init
civicrm_initialize();

$res = civicrm_api3('HRAbsenceType', 'get', array());
$absenceTypes = $res['values'];

// Build the option values based on Absence types
$options = '';

foreach ($absenceTypes as $absenceType) {

if ($absenceType['allow_credits'] == '1') {

// Default credit types
$options .= $absenceType['credit_activity_type_id'] . ',';

}

if ($absenceType['allow_debits'] == '1') {

// Default debit types
$options .= $absenceType['debit_activity_type_id']. ',';

}
}

Expand All @@ -134,71 +123,65 @@ function civihr_employee_portal_init() {
$activityTypes = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, NULL, 'name');
$activityTypeId = CRM_Utils_Array::key('Absence', $activityTypes);
watchdog('Rebuild view TRUE', 'ABSENCE LIST MYSQL VIEW');
}

catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
$civi_settings = parse_url(CIVICRM_DSN);
$civi_db_name = trim($civi_settings['path'], '/');

/**
* Leave / sickness list view
*/
$result = db_query('DROP view if exists absence_list');
$result = db_query("CREATE VIEW absence_list AS
SELECT request.id, request.details, request.activity_type_id, absence_type_debit.title as debit_title, absence_type_credit.title as credit_title,
IF(absence_type_debit.title IS NOT NULL, absence_type_debit.title, absence_type_credit.title) as absence_title,
CASE absence_type_credit.credit_activity_type_id
WHEN request.activity_type_id THEN 1
ELSE 0 END AS is_credit, tgt.contact_id, tgt.record_type_id, min(absence.activity_date_time) as absence_start_date, max(absence.activity_date_time) as absence_end_date, drupal_contact.uf_id as drupal_uid,
SUM(CASE WHEN absence.status_id = 9 THEN 0 WHEN absence.status_id = 3 THEN 0 ELSE absence.duration END) as duration, request.status_id as absence_status,
UNIX_TIMESTAMP(STR_TO_DATE(min(absence.activity_date_time), '%Y-%m-%d')) as absence_start_date_timestamp,
UNIX_TIMESTAMP(STR_TO_DATE(max(absence.activity_date_time), '%Y-%m-%d')) as absence_end_date_timestamp
FROM " . $civi_db_name . ".civicrm_activity request
INNER JOIN " . $civi_db_name . ".civicrm_activity absence ON (absence.source_record_id = request.id AND absence.activity_type_id = " . $activityTypeId . ")
INNER JOIN " . $civi_db_name . ".civicrm_activity_contact tgt ON tgt.activity_id = request.id AND tgt.record_type_id = " . $targetID . "
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_debit
ON request.activity_type_id = absence_type_debit.debit_activity_type_id
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_credit
ON request.activity_type_id = absence_type_credit.credit_activity_type_id
INNER JOIN " . $civi_db_name . ".civicrm_uf_match drupal_contact
ON tgt.contact_id = drupal_contact.contact_id
WHERE (request.activity_type_id IN (" . rtrim($options, ',') . "))
GROUP BY request.id");
/**
* Manager approval view
*/
$result = db_query('DROP view if exists absence_approval_list');
$result = db_query("CREATE VIEW absence_approval_list AS
SELECT request.id, request.activity_type_id, absence_type_debit.title as debit_title, absence_type_credit.title as credit_title,
IF(absence_type_debit.title IS NOT NULL, absence_type_debit.title, absence_type_credit.title) as absence_title,
CASE absence_type_credit.credit_activity_type_id
WHEN request.activity_type_id THEN 1
ELSE 0 END AS is_credit, tgt.contact_id AS manager_id, tgt_contact.contact_id AS employee_id, tgt.record_type_id, min(absence.activity_date_time) as absence_start_date, max(absence.activity_date_time) as absence_end_date, drupal_contact.uf_id as drupal_uid,
SUM(CASE WHEN absence.status_id = 9 THEN 0 WHEN absence.status_id = 3 THEN 0 ELSE absence.duration END) as duration, request.status_id as absence_status,
UNIX_TIMESTAMP(STR_TO_DATE(min(absence.activity_date_time), '%Y-%m-%d')) as absence_start_date_timestamp,
UNIX_TIMESTAMP(STR_TO_DATE(max(absence.activity_date_time), '%Y-%m-%d')) as absence_end_date_timestamp
FROM " . $civi_db_name . ".civicrm_activity request
INNER JOIN " . $civi_db_name . ".civicrm_activity absence ON (absence.source_record_id = request.id AND absence.activity_type_id = " . $activityTypeId . ")
INNER JOIN " . $civi_db_name . ".civicrm_activity_contact tgt ON tgt.activity_id = request.id AND tgt.record_type_id = " . $assigneeID . "
INNER JOIN " . $civi_db_name . ".civicrm_activity_contact tgt_contact ON tgt_contact.activity_id = request.id AND tgt_contact.record_type_id = " . $targetID . "
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_debit
ON request.activity_type_id = absence_type_debit.debit_activity_type_id
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_credit
ON request.activity_type_id = absence_type_credit.credit_activity_type_id
INNER JOIN " . $civi_db_name . ".civicrm_uf_match drupal_contact
ON tgt.contact_id = drupal_contact.contact_id
WHERE (request.activity_type_id IN (" . rtrim($options, ',') . "))
GROUP BY request.id");
// Set to false so it will not rebuild until not asked to rebuild
variable_set('rebuild_absence_list_view', 'FALSE');
} catch (Exception $e) {
drupal_set_message('Cannot create database views: ' . $e->getMessage(), 'error');
}

$civi_settings = parse_url(CIVICRM_DSN);
$civi_db_name = trim($civi_settings['path'], '/');

/**
* Leave / sickness list view
*/
$result = db_query('DROP view if exists absence_list');
$result = db_query("CREATE VIEW absence_list AS
SELECT request.id, request.details, request.activity_type_id, absence_type_debit.title as debit_title, absence_type_credit.title as credit_title,
IF(absence_type_debit.title IS NOT NULL, absence_type_debit.title, absence_type_credit.title) as absence_title,
CASE absence_type_credit.credit_activity_type_id
WHEN request.activity_type_id THEN 1
ELSE 0 END AS is_credit, tgt.contact_id, tgt.record_type_id, min(absence.activity_date_time) as absence_start_date, max(absence.activity_date_time) as absence_end_date, drupal_contact.uf_id as drupal_uid,
SUM(CASE WHEN absence.status_id = 9 THEN 0 WHEN absence.status_id = 3 THEN 0 ELSE absence.duration END) as duration, request.status_id as absence_status,
UNIX_TIMESTAMP(STR_TO_DATE(min(absence.activity_date_time), '%Y-%m-%d')) as absence_start_date_timestamp,
UNIX_TIMESTAMP(STR_TO_DATE(max(absence.activity_date_time), '%Y-%m-%d')) as absence_end_date_timestamp
FROM " . $civi_db_name . ".civicrm_activity request
INNER JOIN " . $civi_db_name . ".civicrm_activity absence ON (absence.source_record_id = request.id AND absence.activity_type_id = " . $activityTypeId . ")
INNER JOIN " . $civi_db_name . ".civicrm_activity_contact tgt ON tgt.activity_id = request.id AND tgt.record_type_id = " . $targetID . "
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_debit
ON request.activity_type_id = absence_type_debit.debit_activity_type_id
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_credit
ON request.activity_type_id = absence_type_credit.credit_activity_type_id
INNER JOIN " . $civi_db_name . ".civicrm_uf_match drupal_contact
ON tgt.contact_id = drupal_contact.contact_id
WHERE (request.activity_type_id IN (" . rtrim($options, ',') . "))
GROUP BY request.id");

/**
* Manager approval view
*/
$result = db_query('DROP view if exists absence_approval_list');
$result = db_query("CREATE VIEW absence_approval_list AS
SELECT request.id, request.activity_type_id, absence_type_debit.title as debit_title, absence_type_credit.title as credit_title,
IF(absence_type_debit.title IS NOT NULL, absence_type_debit.title, absence_type_credit.title) as absence_title,
CASE absence_type_credit.credit_activity_type_id
WHEN request.activity_type_id THEN 1
ELSE 0 END AS is_credit, tgt.contact_id AS manager_id, tgt_contact.contact_id AS employee_id, tgt.record_type_id, min(absence.activity_date_time) as absence_start_date, max(absence.activity_date_time) as absence_end_date, drupal_contact.uf_id as drupal_uid,
SUM(CASE WHEN absence.status_id = 9 THEN 0 WHEN absence.status_id = 3 THEN 0 ELSE absence.duration END) as duration, request.status_id as absence_status,
UNIX_TIMESTAMP(STR_TO_DATE(min(absence.activity_date_time), '%Y-%m-%d')) as absence_start_date_timestamp,
UNIX_TIMESTAMP(STR_TO_DATE(max(absence.activity_date_time), '%Y-%m-%d')) as absence_end_date_timestamp
FROM " . $civi_db_name . ".civicrm_activity request
INNER JOIN " . $civi_db_name . ".civicrm_activity absence ON (absence.source_record_id = request.id AND absence.activity_type_id = " . $activityTypeId . ")
INNER JOIN " . $civi_db_name . ".civicrm_activity_contact tgt ON tgt.activity_id = request.id AND tgt.record_type_id = " . $assigneeID . "
INNER JOIN " . $civi_db_name . ".civicrm_activity_contact tgt_contact ON tgt_contact.activity_id = request.id AND tgt_contact.record_type_id = " . $targetID . "
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_debit
ON request.activity_type_id = absence_type_debit.debit_activity_type_id
LEFT JOIN " . $civi_db_name . ".civicrm_hrabsence_type absence_type_credit
ON request.activity_type_id = absence_type_credit.credit_activity_type_id
INNER JOIN " . $civi_db_name . ".civicrm_uf_match drupal_contact
ON tgt.contact_id = drupal_contact.contact_id
WHERE (request.activity_type_id IN (" . rtrim($options, ',') . "))
GROUP BY request.id");


// Set to false so it will not rebuild until not asked to rebuild
variable_set('rebuild_absence_list_view', 'FALSE');
}

}

/**
Expand Down Expand Up @@ -286,6 +269,14 @@ function civihr_employee_portal_permission() {
'title' => t('Create and edit Tasks'),
'description' => t('Availability to create and edit Tasks.'),
),
'access leave and absences in ssp' => array(
'title' => t('Access CiviHR Leave and Absences'),
'description' => t('Availability for the Staff to access leave block and calendar')
),
'manage leave and absences in ssp' => array(
'title' => t('Manage CiviHR Leave and Absences'),
'description' => t('Availability for the Manager to access leave block and calendar and manage leave requests')
),
);
}

Expand Down Expand Up @@ -891,6 +882,12 @@ function get_civihr_contact_data($contact_id = NULL, $user_id = '') {
* Implementation of hook_menu()
*/
function civihr_employee_portal_menu() {
//get all drupal roles except the default ones (anonymous user) and (authenticated user)
$all_roles = user_roles();
unset($all_roles[array_search('anonymous user', $all_roles)]);
unset($all_roles[array_search('authenticated user', $all_roles)]);
$all_roles = array_values($all_roles);

$items = array();
$items['request_new_account/%ctools_js'] = array(
'title' => 'Edit Document',
Expand Down Expand Up @@ -1154,7 +1151,7 @@ function civihr_employee_portal_menu() {
'title' => 'My Details',
'page callback' => 'civihr_employee_portal_my_details',
'access callback' => '_user_has_role',
'access arguments' => array(array('civihr_staff', 'civihr_manager', 'civihr_admin', 'administrator')),
'access arguments' => array($all_roles),
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'main-menu',
'weight' => 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,5 +644,15 @@ function civihr_employee_portal_features_user_default_permissions() {
'module' => 'civicrm',
);

// Exported permission: 'administer roles and teams'.
$permissions['administer roles and teams'] = array(
'name' => 'administer roles and teams',
'roles' => array(
'administrator' => 'administrator',
'civihr_admin' => 'civihr_admin',
),
'module' => 'civicrm',
);

return $permissions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ features[user_permission][] = view all manual batches
features[user_permission][] = view all notes
features[user_permission][] = view my contact
features[user_permission][] = view own manual batches
features[user_permission][] = administer roles and teams
features[user_role][] = civihr_admin
features[user_role][] = civihr_manager
features[user_role][] = civihr_staff
Expand Down
5 changes: 3 additions & 2 deletions civihr_employee_portal/src/Blocks/AbsenceLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public function generateBlock() {
$links = '';
$links .= '<div class="chr_panel__footer">';
$links .= '<div class="chr_actions-wrapper">';
$links .= civihr_employee_portal_make_link(t('Request TOIL'), 'credit', '', 'pull-left');
$links .= '<div class="chr_actions-wrapper__inline-duo">';
$links .= civihr_employee_portal_make_link(t('Request TOIL'), 'credit');
$links .= civihr_employee_portal_make_link(t('Request leave'), 'debit');
$links .= '</div>';
$links .= civihr_employee_portal_make_link(t('Use TOIL'), 'credit_use');
$links .= '</div>';
$links .= '</div>';
$links .= '</div>';


return $links;
}
Expand Down
19 changes: 17 additions & 2 deletions civihr_employee_portal/src/Blocks/ManagerCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@ public function generateBlock() {
$managerData = get_civihr_uf_match_data($uid);
$managerId = $managerData['contact_id'];

$result = db_query('SELECT aal.employee_id, aal.id, aal.activity_type_id, aal.absence_title, aal.duration, aal.absence_start_date_timestamp, aal.absence_end_date_timestamp, absence_status,
manager_id FROM {absence_approval_list} aal WHERE absence_status != :cancelled AND YEAR(absence_end_date) = YEAR(CURDATE())', array('cancelled' => 3));
$absencesQuery = '
SELECT
aal.employee_id,
aal.id,
aal.activity_type_id,
aal.absence_title,
aal.duration,
aal.absence_start_date_timestamp,
aal.absence_end_date_timestamp,
absence_status,
manager_id
FROM {absence_approval_list} aal
WHERE absence_status != :cancelled AND
absence_status != :rejected AND
YEAR(absence_end_date) = YEAR(CURDATE())';

$result = db_query($absencesQuery, array('cancelled' => 3, 'rejected' => 9));

// Result is returned as a iterable object that returns a stdClass object on each iteration
foreach ($result as $record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class civihr_employee_portal_filter_absence_period extends views_handler_filter_
$any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
$groups = array('All' => $any_label);
}

// Get civi defined date periods
$civi_date_periods = get_civihr_date_periods();

$filter_values = array();

foreach ($civi_date_periods as $civi_date_period) {

$filter_values[$civi_date_period['id']] = array('title' => $civi_date_period['title'], 'operator' => 'between', 'value' => array('type' => 'date', 'value' => '', 'min' => $civi_date_period['start_date'], 'max' => $civi_date_period['end_date']));

}

// Force rewrite default values (from CIVIcrm)
$this->options['group_info']['group_items'] = $filter_values;

Expand All @@ -44,8 +44,8 @@ class civihr_employee_portal_filter_absence_period extends views_handler_filter_
}

if (count($groups)) {
variable_set('default_date_period_id', $this->getDefaultDatePeriod($filter_values));
$value = $this->options['group_info']['identifier'];

$form[$value] = array(
'#type' => $this->options['group_info']['widget'],
'#default_value' => variable_get('default_date_period_id', '1'),
Expand All @@ -55,5 +55,29 @@ class civihr_employee_portal_filter_absence_period extends views_handler_filter_
$this->options['expose']['label'] = '';
}
}

/**
* Get the default period id from a list of periods based on current time
*
* @param array $periods_list Filtered absence periods
* @return int Absence period database id
*/

public static function getDefaultDatePeriod($periods_list) {
$current_date = new DateTime();
$current_timestamp = $current_date->getTimestamp();

foreach($periods_list as $period_id => $period) {
$start_date = new DateTime($period['value']['min']);
$end_date = new DateTime($period['value']['max']);
if ( $current_timestamp > $start_date->getTimestamp() &&
$current_timestamp < $end_date->getTimestamp() ) {
return $period_id;
}
}

return key($periods_list);

}

}
Loading

0 comments on commit 978cf7f

Please sign in to comment.