/** * Return the formated datas of a constant for making graphs with Flot * * @param string $constant_name The name of the constant * @param string $constant_type The type of the constant : line|cumul|formula|bandwidth * @param CConstantesMedicales[] $constant_values The CConstantesMedicales containing the values * * @return array */ function getDatasForConstant($constant_name, $constant_type, $constant_values) { $tick = 1; $datas = array('values' => array()); $values = array(); /* Initializing the period for the cumul */ if (($constant_type == 'cumul' || $constant_type == 'formula') && !empty($constant_values)) { $reset_hour = CConstantesMedicales::getResetHour($constant_name); $first_value = reset($constant_values); $start = strtotime(CMbDT::format($first_value->datetime, "%Y-%m-%d 0{$reset_hour}:00:00")); if (strtotime($first_value->datetime) < $start) { $start = $start - 24 * 3600; } $current_period = array('start' => $start, 'end' => $start + 24 * 3600); $current_value = null; $start_tick = 1; $periods = array(); } foreach ($constant_values as $_value) { /* Calculating the value for the cumul */ if ($constant_type == 'cumul' || $constant_type == 'formula') { if ($constant_type == 'formula') { $_value->{$constant_name} = $_value->applyFormula($constant_name); } $timestamp = strtotime($_value->datetime); /* If the current CConstantesMedicales's datetime is in the current period, we add the value to the cumuled value */ if ($timestamp > $current_period['start'] && $timestamp <= $current_period['end']) { $current_value += $_value->{$constant_name}; } else { /* If not, we add the cumuled value to the periods array, and initialize the current period */ $periods[] = array('start' => $start_tick, 'end' => $tick, 'start_time' => $current_period['start'], 'end_time' => $current_period['end'], 'value' => $current_value); $current_value = $_value->{$constant_name}; $start_tick = $tick; $start = $current_period['end']; if (strtotime($_value->datetime) - $start > 24 * 3600) { $start = strtotime(CMbDT::format($_value->datetime, '%Y-%m-%d' . sprintf('%02d', $reset_hour) . ':00:00')); if (strtotime($_value->datetime) < $start) { $start = $start - 24 * 3600; } } $current_period = array('start' => $start, 'end' => $start + 24 * 3600); } } if (!is_null($_value->{$constant_name}) && $constant_type != 'formula') { /* Get the last 5 user logs of the CConstantesMedicales object */ $users_logs = array(); if ($constant_name[0] !== "_") { $_value->loadRefUser(); if ($_value->_ref_user) { $users_logs[] = utf8_encode(CMbDT::format($_value->getCreationDate(), '%d/%m/%y %H:%M') . ' - ' . $_value->_ref_user->_view); } } if ($this->display['mode'] == 'time') { $entry = array(CMbDate::toUTCTimestamp($_value->datetime)); } else { $entry = array($tick); } if ($constant_type == 'bandwidth') { $field = CConstantesMedicales::$list_constantes[$constant_name]['formfields']; $entry[] = $_value->{$field}[0]; $entry[] = $_value->{$field}[1]; $values[] = $_value->{$field}[0]; $values[] = $_value->{$field}[1]; } else { if (isset(CConstantesMedicales::$list_constantes[$constant_name]['formfields'])) { $field = CConstantesMedicales::$list_constantes[$constant_name]['formfields']; $entry[] = $_value->{$field}[0]; $values[] = $_value->{$field}[0]; } else { $entry[] = $_value->{$constant_name}; $values[] = $_value->{$constant_name}; } } $entry['id'] = $_value->_id; $entry['date'] = utf8_encode(CMbDT::format($_value->datetime, '%d/%m/%y')); $entry['hour'] = utf8_encode(CMbDT::format($_value->datetime, '%Hh%M')); $entry['users'] = $users_logs; if ($_value->comment) { $entry['comment'] = utf8_encode($_value->comment); } if ("{$_value->context_class}-{$_value->context_id}" !== $this->context_guid) { $_value->loadRefContext(); if ($_value->_ref_context) { $_value->_ref_context->loadRefsFwd(); $entry['context'] = utf8_encode($_value->_ref_context->_view); $entry['context_guid'] = "{$_value->context_class}-{$_value->context_id}"; } } $datas['values'][] = $entry; } $tick++; } if ($constant_type == 'cumul' || $constant_type == 'formula') { $cumul_datas = array(); if (!empty($constant_values)) { $periods[] = array('start' => $start_tick, 'end' => $tick, 'start_time' => $current_period['start'], 'end_time' => $current_period['end'], 'value' => $current_value); $cumul_datas = array(); $dtz = new DateTimeZone('Europe/Paris'); $tz_ofsset = $dtz->getOffset(new DateTime('now', $dtz)); foreach ($periods as $_period) { if ($this->display['mode'] == 'time') { $x = ($_period['start_time'] + $tz_ofsset) * 1000; $bar_width = ($_period['end_time'] - $_period['start_time']) * 1000; } else { $x = $_period['start']; $bar_width = $_period['end'] - $_period['start']; } if ($this->widget) { $first_value = reset($constant_values); $start_date = strftime('%Y-%m-%d %H:%M:%S', $_period['start_time']); $end_date = strftime('%Y-%m-%d %H:%M:%S', $_period['end_time']); $query = new CRequest(); $query->addSelect("SUM(`{$constant_name}`)"); $query->addTable('constantes_medicales'); $query->addWhere(array("`patient_id` = {$first_value->patient_id}", "`context_class` = '{$first_value->context_class}'", "`context_id` = {$first_value->context_id}", "`datetime` >= '{$start_date}'", "`datetime` <= '{$end_date}'", "`{$constant_name}` IS NOT NULL")); $ds = CSQLDataSource::get('std'); $_period['value'] = $ds->loadResult($query->makeSelect()); } $cumul_datas[] = array($x, $_period['value'], 'date' => utf8_encode(strftime('%d/%m/%y %H:%M', $_period['start_time']) . ' au ' . strftime('%d/%m/%y %H:%M', $_period['end_time'])), 'barWidth' => $bar_width); $values[] = $_period['value']; } } $datas['cumul'] = $cumul_datas; } if (!empty($values)) { $datas['min'] = floor(min($values)); $datas['max'] = ceil(max($values)); } return $datas; }