/** * Save values in the *_field_values table * @param array Structured array with the values to save * @param boolean Whether to show the insert query (passed to the parent save() method) * @result mixed The result sent from the parent method * @assert (array()) === false */ public function save($params, $show_query = false) { $extra_field = new ExtraField($this->type); // Setting value to insert. $value = $params['field_value']; $value_to_insert = null; if (is_array($value)) { $value_to_insert = implode(';', $value); } else { $value_to_insert = Database::escape_string($value); } $params['field_value'] = $value_to_insert; //If field id exists $extra_field_info = $extra_field->get($params['field_id']); if ($extra_field_info) { switch ($extra_field_info['field_type']) { case ExtraField::FIELD_TYPE_RADIO: case ExtraField::FIELD_TYPE_SELECT: case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: //$field_options = $session_field_option->get_field_options_by_field($params['field_id']); //$params['field_value'] = split(';', $value_to_insert); /* if ($field_options) { $check = false; foreach ($field_options as $option) { if (in_array($option['option_value'], $values)) { $check = true; break; } } if (!$check) { return false; //option value not found } } else { return false; //enumerated type but no option found }*/ break; case ExtraField::FIELD_TYPE_TEXT: case ExtraField::FIELD_TYPE_TEXTAREA: break; case ExtraField::FIELD_TYPE_DOUBLE_SELECT: if (is_array($value)) { if (isset($value['extra_' . $extra_field_info['field_variable']]) && isset($value['extra_' . $extra_field_info['field_variable'] . '_second'])) { $value_to_insert = $value['extra_' . $extra_field_info['field_variable']] . '::' . $value['extra_' . $extra_field_info['field_variable'] . '_second']; } else { $value_to_insert = null; } } break; default: break; } if ($extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG) { $field_values = self::getAllValuesByItemAndFieldAndValue($params[$this->handler_id], $params['field_id'], $value); } else { $field_values = self::get_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']); } $params['field_value'] = $value_to_insert; $params['tms'] = api_get_utc_datetime(); $params[$this->author_id] = api_get_user_id(); // Insert if (empty($field_values)) { if ($extra_field_info['field_loggeable'] == 1) { switch ($this->type) { case 'question': $extraFieldValue = new QuestionFieldValues(); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setQuestionId($params[$this->handler_id]); break; case 'course': $extraFieldValue = new CourseFieldValues(); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setQuestionId($params[$this->handler_id]); break; case 'user': $extraFieldValue = new UserFieldValues(); $extraFieldValue->setUserId($params[$this->handler_id]); $extraFieldValue->setAuthorId(api_get_user_id()); break; case 'session': $extraFieldValue = new SessionFieldValues(); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setSessionId($params[$this->handler_id]); break; } if (isset($extraFieldValue)) { if (!empty($params['field_value'])) { $extraFieldValue->setComment($params['comment']); $extraFieldValue->setFieldValue($params['field_value']); $extraFieldValue->setFieldId($params['field_id']); $extraFieldValue->setTms(api_get_utc_datetime(null, false, true)); Database::getManager()->persist($extraFieldValue); Database::getManager()->flush(); } } } else { if ($extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG) { $option = new ExtraFieldOption($this->type); $optionExists = $option->get($params['field_value']); if (empty($optionExists)) { $optionParams = array('field_id' => $params['field_id'], 'option_value' => $params['field_value']); $optionId = $option->saveOptions($optionParams); } else { $optionId = $optionExists['id']; } $params['field_value'] = $optionId; if ($optionId) { return parent::save($params, $show_query); } } else { return parent::save($params, $show_query); } } } else { // Update if ($extra_field_info['field_loggeable'] == 1) { switch ($this->type) { case 'question': $extraFieldValue = Database::getManager()->getRepository('ChamiloCoreBundle:QuestionFieldValues')->find($field_values['id']); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setQuestionId($params[$this->handler_id]); break; case 'course': $extraFieldValue = Database::getManager()->getRepository('ChamiloCoreBundle:CourseFieldValues')->find($field_values['id']); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setCourseCode($params[$this->handler_id]); break; case 'user': $extraFieldValue = Database::getManager()->getRepository('ChamiloCoreBundle:UserFieldValues')->find($field_values['id']); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setAuthorId(api_get_user_id()); break; case 'session': $extraFieldValue = Database::getManager()->getRepository('ChamiloCoreBundle:SessionFieldValues')->find($field_values['id']); $extraFieldValue->setUserId(api_get_user_id()); $extraFieldValue->setSessionId($params[$this->handler_id]); break; } if (isset($extraFieldValue)) { if (!empty($params['field_value'])) { /* * If the field value is similar to the previous value then the comment will be the same in order to no save in the log an empty record */ if ($extraFieldValue->getFieldValue() == $params['field_value']) { if (empty($params['comment'])) { $params['comment'] = $extraFieldValue->getComment(); } } $extraFieldValue->setComment($params['comment']); $extraFieldValue->setFieldValue($params['field_value']); $extraFieldValue->setFieldId($params['field_id']); $extraFieldValue->setTms(api_get_utc_datetime(null, false, true)); Database::getManager()->persist($extraFieldValue); Database::getManager()->flush(); } } } else { $params['id'] = $field_values['id']; return parent::update($params, $show_query); } } } }
/** * 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; }
/** * @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); }
api_protect_admin_script(); //Add the JS needed to use the jqgrid $htmlHeadXtra[] = api_get_jqgrid_js(); // setting breadcrumbs $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin')); $tool_name = null; $action = isset($_GET['action']) ? $_GET['action'] : null; $field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null; if (empty($field_id)) { api_not_allowed(); } if (!in_array($type, ExtraField::getValidExtraFieldTypes())) { api_not_allowed(); } $extra_field = new ExtraField($type); $extra_field_info = $extra_field->get($field_id); $check = Security::check_token('request'); $token = Security::get_token(); if ($action == 'add') { $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extra_field->type, 'name' => $extra_field->pageName); $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extra_field->type . '&action=edit&id=' . $extra_field_info['id'], 'name' => $extra_field_info['field_display_text']); $interbreadcrumb[] = array('url' => 'extra_field_options.php?type=' . $extra_field->type . '&field_id=' . $extra_field_info['id'], 'name' => get_lang('EditExtraFieldOptions')); $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('Add')); } elseif ($action == 'edit') { $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extra_field->type, 'name' => $extra_field->pageName); $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extra_field->type . '&action=edit&id=' . $extra_field_info['id'], 'name' => $extra_field_info['field_display_text']); $interbreadcrumb[] = array('url' => 'extra_field_options.php?type=' . $extra_field->type . '&field_id=' . $extra_field_info['id'], 'name' => get_lang('EditExtraFieldOptions')); $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('Edit')); } else { $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extra_field->type, 'name' => $extra_field->pageName); $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extra_field->type . '&action=edit&id=' . $extra_field_info['id'], 'name' => $extra_field_info['field_display_text']);
/** * Get options for a specific field as string split by ; * @param int $field_id * @param string $ordered_by Extra query bit for reordering * @return string HTML string of options * @assert (0, '') === null */ public function get_field_options_by_field_to_string($field_id, $ordered_by = null) { $field = new ExtraField($this->type); $field_info = $field->get($field_id); $options = self::get_field_options_by_field($field_id, false, $ordered_by); $elements = array(); if (!empty($options)) { switch ($field_info['field_type']) { case ExtraField::FIELD_TYPE_DOUBLE_SELECT: $html = ExtraField::extra_field_double_select_convert_array_to_string($options); break; default: foreach ($options as $option) { $elements[] = $option['option_value']; } $html = implode(';', $elements); break; } return $html; } return null; }
$this_section = SECTION_PLATFORM_ADMIN; $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; api_protect_admin_script(); // setting breadcrumbs $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin')); $tool_name = null; $action = isset($_GET['action']) ? $_GET['action'] : null; $field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null; if (empty($field_id)) { api_not_allowed(); } if (!in_array($type, ExtraField::getValidExtraFieldTypes())) { api_not_allowed(); } $extraField = new ExtraField($type); $extraFieldInfo = $extraField->get($field_id); $check = Security::check_token('request'); $token = Security::get_token(); if ($action == 'add') { $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extraField->type, 'name' => $extraField->pageName); $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extraField->type . '&action=edit&id=' . $extraFieldInfo['id'], 'name' => $extraFieldInfo['field_display_text']); $interbreadcrumb[] = array('url' => 'extra_field_options.php?type=' . $extraField->type . '&field_id=' . $extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions')); $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('Add')); } elseif ($action == 'edit') { $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extraField->type, 'name' => $extraField->pageName); $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extraField->type . '&action=edit&id=' . $extraFieldInfo['id'], 'name' => $extraFieldInfo['field_display_text']); $interbreadcrumb[] = array('url' => 'extra_field_options.php?type=' . $extraField->type . '&field_id=' . $extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions')); $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('Edit')); } else { $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extraField->type, 'name' => $extraField->pageName); $interbreadcrumb[] = array('url' => 'extra_fields.php?type=' . $extraField->type . '&action=edit&id=' . $extraFieldInfo['id'], 'name' => $extraFieldInfo['field_display_text']);