/** * @param array $options * @return array */ public function parseConditions($options) { $inject_extra_fields = null; $extraFieldOption = new ExtraFieldOption($this->type); $double_fields = array(); if (isset($options['extra'])) { $extra_fields = $options['extra']; if (!empty($extra_fields)) { $counter = 1; foreach ($extra_fields as &$extra) { $extra_field_obj = new ExtraField($this->type); $extra_field_info = $extra_field_obj->get($extra['id']); $extra['extra_field_info'] = $extra_field_info; if (isset($extra_field_info['field_type']) && in_array($extra_field_info['field_type'], array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT))) { $inject_extra_fields .= " fvo{$counter}.option_display_text as {$extra['field']}, "; } else { $inject_extra_fields .= " fv{$counter}.field_value as {$extra['field']}, "; } if (isset($extra_fields_info[$extra['id']])) { $info = $extra_fields_info[$extra['id']]; } else { $info = $this->get($extra['id']); $extra_fields_info[$extra['id']] = $info; } if (isset($info['field_type']) && $info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { $double_fields[$info['id']] = $info; } $counter++; } } } $options_by_double = array(); foreach ($double_fields as $double) { $my_options = $extraFieldOption->get_field_options_by_field($double['id'], true); $options_by_double['extra_' . $double['field_variable']] = $my_options; } $field_value_to_join = array(); //filter can be all/any = and/or $inject_joins = null; $inject_where = null; $where = null; if (!empty($options['where'])) { if (!empty($options['extra'])) { // Removing double 1=1 $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']); // Always OR $counter = 1; foreach ($extra_fields as $extra_info) { $extra_field_info = $extra_info['extra_field_info']; $inject_joins .= " INNER JOIN {$this->table_field_values} fv{$counter} ON (s." . $this->primaryKey . " = fv{$counter}." . $this->handler_id . ") "; //Add options if (isset($extra_field_info['field_type']) && in_array($extra_field_info['field_type'], array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT))) { $options['where'] = str_replace($extra_info['field'], 'fv' . $counter . '.field_id = ' . $extra_info['id'] . ' AND fvo' . $counter . '.option_value', $options['where']); $inject_joins .= " INNER JOIN {$this->table_field_options} fvo{$counter} " . " ON (fv{$counter}.field_id = fvo{$counter}.field_id AND fv{$counter}.field_value = fvo{$counter}.option_value) "; } else { //text, textarea, etc $options['where'] = str_replace($extra_info['field'], 'fv' . $counter . '.field_id = ' . $extra_info['id'] . ' AND fv' . $counter . '.field_value', $options['where']); } $field_value_to_join[] = " fv{$counter}.{$this->handler_id} "; $counter++; } if (!empty($field_value_to_join)) { //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join); } } $where .= ' AND ' . $options['where']; } $order = null; if (!empty($options['order'])) { $order = " ORDER BY " . $options['order']; } $limit = null; if (!empty($options['limit'])) { $limit = " LIMIT " . $options['limit']; } return array('order' => $order, 'limit' => $limit, 'where' => $where, 'inject_where' => $inject_where, 'inject_joins' => $inject_joins, 'field_value_to_join' => $field_value_to_join, 'inject_extra_fields' => $inject_extra_fields); }
/** * Gets the admin session list callback of the session/session_list.php * page with all user/details in the right fomat * @param array * @result array Array of rows results * @asset ('a') === false */ public static function get_sessions_admin_complete($options = array()) { if (!is_array($options)) { return false; } $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); $tbl_user = Database::get_main_table(TABLE_MAIN_USER); $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); $tbl_session_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); $tbl_session_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS); $where = 'WHERE 1 = 1 '; $user_id = api_get_user_id(); if (!api_is_platform_admin()) { if (api_is_session_admin() && api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false') { $where .= " AND s.session_admin_id = {$user_id} "; } } $coach_name = " CONCAT(u.lastname , ' ', u.firstname) as coach_name "; if (api_is_western_name_order()) { $coach_name = " CONCAT(u.firstname, ' ', u.lastname) as coach_name "; } $today = api_get_utc_datetime(); $inject_extra_fields = null; $extra_fields = array(); $extra_fields_info = array(); //for now only sessions $extra_field = new ExtraField('session'); $double_fields = array(); $extra_field_option = new ExtraFieldOption('session'); if (isset($options['extra'])) { $extra_fields = $options['extra']; if (!empty($extra_fields)) { foreach ($extra_fields as $extra) { $inject_extra_fields .= " IF (fv.field_id = {$extra['id']}, fvo.option_display_text, NULL ) as {$extra['field']} , "; if (isset($extra_fields_info[$extra['id']])) { $info = $extra_fields_info[$extra['id']]; } else { $info = $extra_field->get($extra['id']); $extra_fields_info[$extra['id']] = $info; } if ($info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { $double_fields[$info['id']] = $info; } } } } $options_by_double = array(); foreach ($double_fields as $double) { $my_options = $extra_field_option->get_field_options_by_field($double['id'], true); $options_by_double['extra_' . $double['field_variable']] = $my_options; } //sc.name as category_name, $select = "\n SELECT * FROM (\n SELECT DISTINCT\n IF (\n (s.access_start_date <= '{$today}' AND '{$today}' < s.access_end_date) OR\n (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR\n (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR\n (s.access_start_date <= '{$today}' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR\n ('{$today}' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )\n , 1, 0) as session_active,\n s.name,\n s.nbr_courses,\n s.nbr_users,\n s.display_start_date,\n s.display_end_date,\n {$coach_name},\n access_start_date,\n access_end_date,\n s.visibility,\n u.user_id,\n {$inject_extra_fields}\n c.title as course_title,\n s.id "; if (!empty($options['where'])) { if (!empty($options['extra'])) { $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']); $options['where'] = str_replace('AND', 'OR', $options['where']); foreach ($options['extra'] as $extra) { $options['where'] = str_replace($extra['field'], 'fv.field_id = ' . $extra['id'] . ' AND fvo.option_value', $options['where']); } } $options['where'] = str_replace('course_title', 'c.title', $options['where']); $where .= ' AND ' . $options['where']; } if (!empty($options['limit'])) { $where .= " LIMIT " . $options['limit']; } $query = "{$select} FROM {$tbl_session} s\n LEFT JOIN {$tbl_session_field_values} fv ON (fv.session_id = s.id)\n LEFT JOIN {$tbl_session_field_options} fvo ON (fv.field_id = fvo.field_id)\n LEFT JOIN {$tbl_session_rel_course} src ON (src.id_session = s.id)\n LEFT JOIN {$tbl_course} c ON (src.c_id = c.id)\n LEFT JOIN {$tbl_session_category} sc ON (s.session_category_id = sc.id)\n INNER JOIN {$tbl_user} u ON (s.id_coach = u.user_id) " . $where; if (api_is_multiple_url_enabled()) { $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); $access_url_id = api_get_current_access_url_id(); if ($access_url_id != -1) { $where .= " AND ar.access_url_id = {$access_url_id} "; $query = "{$select}\n FROM {$tbl_session} s\n LEFT JOIN {$tbl_session_field_values} fv ON (fv.session_id = s.id)\n LEFT JOIN {$tbl_session_field_options} fvo ON (fv.field_id = fvo.field_id)\n LEFT JOIN {$tbl_session_rel_course} src ON (src.id_session = s.id)\n LEFT JOIN {$tbl_course} c ON (src.c_id = c.id)\n LEFT JOIN {$tbl_session_category} sc ON (s.session_category_id = sc.id)\n INNER JOIN {$tbl_user} u ON (s.id_coach = u.user_id)\n INNER JOIN {$table_access_url_rel_session} ar ON (ar.session_id = s.id)\n {$where}"; } } $query .= ") AS session_table"; if (!empty($options['order'])) { $query .= " ORDER BY " . $options['order']; } //error_log($query); //echo $query; $result = Database::query($query); $formatted_sessions = array(); if (Database::num_rows($result)) { $sessions = Database::store_result($result, 'ASSOC'); foreach ($sessions as $session) { $session_id = $session['id']; $session['name'] = Display::url($session['name'], "resume_session.php?id_session=" . $session['id']); $session['coach_name'] = Display::url($session['coach_name'], "user_information.php?user_id=" . $session['user_id']); if ($session['session_active'] == 1) { $session['session_active'] = Display::return_icon('accept.png', get_lang('Active'), array(), ICON_SIZE_SMALL); } else { $session['session_active'] = Display::return_icon('error.png', get_lang('Inactive'), array(), ICON_SIZE_SMALL); } $session = self::convert_dates_to_local($session); switch ($session['visibility']) { case SESSION_VISIBLE_READ_ONLY: //1 $session['visibility'] = get_lang('ReadOnly'); break; case SESSION_VISIBLE: //2 //2 case SESSION_AVAILABLE: //4 $session['visibility'] = get_lang('Visible'); break; case SESSION_INVISIBLE: //3 $session['visibility'] = api_ucfirst(get_lang('Invisible')); break; } //Cleaning double selects foreach ($session as $key => &$value) { if (isset($options_by_double[$key]) || isset($options_by_double[$key . '_second'])) { $options = explode('::', $value); } $original_key = $key; if (strpos($key, '_second') === false) { } else { $key = str_replace('_second', '', $key); } if (isset($options_by_double[$key])) { if (isset($options[0])) { if (isset($options_by_double[$key][$options[0]])) { if (strpos($original_key, '_second') === false) { $value = $options_by_double[$key][$options[0]]['option_display_text']; } else { $value = $options_by_double[$key][$options[1]]['option_display_text']; } } } } } //Magic filter if (isset($formatted_sessions[$session_id])) { $formatted_sessions[$session_id] = self::compare_arrays_to_merge($formatted_sessions[$session_id], $session); } else { $formatted_sessions[$session_id] = $session; } } } return $formatted_sessions; }
static function transaction_extra_field_eliminar_generic($extra_field_variable, $original_data, $web_service_details, $type = 'session') { //horario $extra_field = new ExtraField($type); $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable); $extra_field_option = new ExtraFieldOption($type); $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $original_data['item_id']); if (!empty($extra_field_option_info)) { $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']); $deleting_option_ids = array(); foreach ($extra_field_option_info as $option) { //@todo Delete all horario in sessions? $result = $extra_field_option->delete($option['id']); if ($result) { $deleting_option_ids[] = $option['id']; } } $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']); if (!empty($deleting_option_ids)) { $deleting_option_ids = implode(',', $deleting_option_ids); return array('entity' => $extra_field_variable, 'before' => $info_before, 'after' => $info_after, 'message' => "Extra field options were deleted for the field_variable: {$extra_field_variable}, options id deleted: {$deleting_option_ids}", 'status_id' => self::TRANSACTION_STATUS_FAILED); } else { return array('message' => "Extra field option was NOT deleted. No field options ids where found for variable: '{$extra_field_variable}' with id:'" . $original_data['item_id'] . "'", 'status_id' => self::TRANSACTION_STATUS_FAILED); } } else { return array('message' => "Extra field option was NOT deleted - Extra field not found in DB. Trying to locate field_variable: '{$extra_field_variable}' with id: '{$original_data['item_id']}'", 'status_id' => self::TRANSACTION_STATUS_FAILED); } }