Esempio n. 1
0
 function display_editfield()
 {
     global $CFG, $DB;
     $level = $this->required_param('level', PARAM_ACTION);
     $ctxlvl = \local_eliscore\context\helper::get_level_from_name($level);
     if (!$ctxlvl) {
         print_error('invalid_context_level', 'local_elisprogram');
     }
     $id = $this->optional_param('id', NULL, PARAM_INT);
     require_once elispm::file('form/customfieldform.class.php');
     $tmppage = new customfieldpage(array('level' => $level, 'action' => 'editfield'), $this);
     $form = new customfieldform($tmppage->url, array('id' => $id, 'level' => $level, 'from' => optional_param('from', '', PARAM_CLEAN)));
     if ($form->is_cancelled()) {
         $tmppage = new customfieldpage(array('level' => $level));
         redirect($tmppage->url, get_string('edit_cancelled', 'local_elisprogram'));
     } else {
         if ($data = $form->get_data()) {
             $src = !empty($data->manual_field_options_source) ? $data->manual_field_options_source : '';
             // ELIS-8066: strip CRs "\r" from menu options & default data (below)
             if (!empty($data->manual_field_options)) {
                 $data->manual_field_options = str_replace("\r", '', $data->manual_field_options);
             }
             switch ($data->manual_field_control) {
                 case 'checkbox':
                     if (!$data->multivalued && !empty($src)) {
                         $elem = "defaultdata_radio_{$src}";
                         $data->defaultdata = isset($data->{$elem}) ? $data->{$elem} : '';
                         // radio buttons unset by default
                         // error_log("/local/elisprogram/customfieldpage.class.php:: defaultdata->{$elem} = {$data->defaultdata}");
                     } else {
                         if (!$data->multivalued && !empty($data->manual_field_options)) {
                             $data->defaultdata = str_replace("\r", '', $data->defaultdata_radio);
                         } else {
                             $data->defaultdata = $data->defaultdata_checkbox;
                         }
                     }
                     break;
                 case 'menu':
                     $elem = !empty($src) ? "defaultdata_menu_{$src}" : "defaultdata_menu";
                     $data->defaultdata = $data->{$elem};
                     if (empty($src)) {
                         $data->defaultdata = str_replace("\r", '', $data->defaultdata);
                     }
                     break;
                 case 'datetime':
                     $data->defaultdata = $data->defaultdata_datetime;
                     break;
                 default:
                     $data->defaultdata = $data->defaultdata_text;
                     break;
             }
             $field = new field($data);
             if ($id) {
                 $field->id = $id;
                 $field->save();
             } else {
                 $field->save();
                 // assume each field only belongs to one context level (for now)
                 $fieldcontext = new field_contextlevel();
                 $fieldcontext->fieldid = $field->id;
                 $fieldcontext->contextlevel = $ctxlvl;
                 $fieldcontext->save();
             }
             //don't use !empty here because we might be storing a 0 or similar value
             if ($data->defaultdata != '') {
                 // save the default value
                 $defaultdata = $data->defaultdata;
                 if ($field->multivalued && is_string($defaultdata)) {
                     // parse as a CSV string
                     // until we can use str_getcsv from PHP 5.3...
                     $temp = fopen("php://memory", "rw");
                     fwrite($temp, $defaultdata);
                     rewind($temp);
                     $defaultdata = fgetcsv($temp);
                     fclose($temp);
                 } else {
                     if (!$field->multivalued && is_array($defaultdata)) {
                         foreach ($defaultdata as $val) {
                             $defaultdata = $val;
                             break;
                         }
                     }
                 }
                 field_data::set_for_context_and_field(NULL, $field, $defaultdata);
             } else {
                 if ($field->multivalued) {
                     field_data::set_for_context_and_field(NULL, $field, array());
                 } else {
                     field_data::set_for_context_and_field(NULL, $field, NULL);
                 }
             }
             $plugins = core_component::get_plugin_list('elisfields');
             foreach ($plugins as $plugin => $dir) {
                 if (is_readable($CFG->dirroot . '/local/eliscore/fields/' . $plugin . '/custom_fields.php')) {
                     require_once elis::plugin_file('elisfields_' . $plugin, 'custom_fields.php');
                     if (function_exists("{$plugin}_field_save_form_data")) {
                         call_user_func("{$plugin}_field_save_form_data", $form, $field, $data);
                     }
                 }
             }
             $tmppage = new customfieldpage(array('level' => $level));
             redirect($tmppage->url, get_string('field_saved', 'local_elisprogram', $field));
         } else {
             if (!empty($id)) {
                 if ($this->optional_param('from', NULL, PARAM_CLEAN) == 'moodle' && $level == 'user') {
                     $moodlefield = $DB->get_record('user_info_field', array('id' => $id));
                     if (!$moodlefield) {
                         print_error('invalid_field_id', 'local_elisprogram');
                     }
                     unset($moodlefield->id);
                     $data = $moodlefield;
                     $data_array = (array) $moodlefield;
                     $data_array['datatype'] = 'text';
                     $data_array['manual_field_control'] = $moodlefield->datatype;
                     switch ($moodlefield->datatype) {
                         case field::CHECKBOX:
                             $data_array['datatype'] = 'bool';
                             break;
                         case field::DATETIME:
                             $data_array['datatype'] = 'datetime';
                             $data_array['manual_field_startyear'] = $moodlefield->param1;
                             $data_array['manual_field_stopyear'] = $moodlefield->param2;
                             $data_array['manual_field_inctime'] = $moodlefield->param3;
                             break;
                         case field::MENU:
                             $data_array['datatype'] = 'char';
                             $data_array['manual_field_options'] = $moodlefield->param1;
                             break;
                         case field::TEXTAREA:
                             $data_array['manual_field_columns'] = $moodlefield->param1;
                             $data_array['manual_field_rows'] = $moodlefield->param2;
                             break;
                         case field::TEXT:
                             if ($moodlefield->param3) {
                                 $data_array['manual_field_control'] = 'password';
                             }
                             $data_array['manual_field_columns'] = $moodlefield->param1;
                             $data_array['manual_field_maxlength'] = $moodlefield->param2;
                             break;
                     }
                 } else {
                     $data = new field($id);
                     $data->load();
                     $manual = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual']);
                     $menu_src = !empty($manual->options_source) ? $manual->options_source : 0;
                     $data_array = $data->to_array();
                     $field_record = $DB->get_record(field::TABLE, array('id' => $id));
                     if (!empty($field_record)) {
                         foreach ($field_record as $field_item => $field_value) {
                             $data_array[$field_item] = $field_value;
                         }
                     }
                     $defaultdata = field_data::get_for_context_and_field(NULL, $data);
                     if (!empty($defaultdata)) {
                         if ($data->multivalued) {
                             $values = array();
                             foreach ($defaultdata as $defdata) {
                                 $values[] = $defdata->data;
                             }
                             $defaultdata = $values;
                         } else {
                             foreach ($defaultdata as $defdata) {
                                 $defaultdata = $defdata->data;
                                 break;
                             }
                         }
                     }
                     $field = new field();
                     // Format decimal numbers
                     if ($data_array['datatype'] == 'num' && $manual->param_control != 'menu') {
                         $defaultdata = $field->format_number($defaultdata);
                     }
                     if (!is_object($defaultdata)) {
                         $data_array['defaultdata'] = $defaultdata;
                     }
                     $plugins = core_component::get_plugin_list('elisfields');
                     foreach ($plugins as $plugin => $dir) {
                         if (is_readable($CFG->dirroot . '/local/eliscore/fields/' . $plugin . '/custom_fields.php')) {
                             include_once $CFG->dirroot . '/local/eliscore/fields/' . $plugin . '/custom_fields.php';
                             if (function_exists("{$plugin}_field_get_form_data")) {
                                 $data_array += call_user_func("{$plugin}_field_get_form_data", $form, $data);
                             }
                         }
                     }
                 }
                 if (isset($data_array['defaultdata'])) {
                     // ELIS-6699 -- load the field to determine the data type used, $data may be a field_data_* or field object
                     if (isset($data->fieldid)) {
                         $field->id = $data->fieldid;
                         $field->load();
                     } else {
                         $field = $data;
                     }
                     $data_array['defaultdata_checkbox'] = !empty($data_array['defaultdata']);
                     // ELIS-6699 -- If this is not a datetime field, then we can't use the default data value as a timestamp
                     $data_array['defaultdata_datetime'] = $field->datatype == 'datetime' ? $data_array['defaultdata'] : time();
                     $data_array['defaultdata_text'] = strval($data_array['defaultdata']);
                     $data_array[empty($menu_src) ? 'defaultdata_menu' : "defaultdata_menu_{$menu_src}"] = $data_array['defaultdata'];
                     $data_array[empty($menu_src) ? 'defaultdata_radio' : "defaultdata_radio_{$menu_src}"] = $data_array['defaultdata'];
                 }
                 $form->set_data($data_array);
             }
             $form->display();
         }
     }
 }
 function action_editfield()
 {
     global $CURMAN;
     $level = $this->required_param('level', PARAM_ACTION);
     $ctxlvl = context_level_base::get_custom_context_level($level, 'block_curr_admin');
     if (!$ctxlvl) {
         print_error('invalid_context_level', 'block_curr_admin');
     }
     $id = $this->optional_param('id', NULL, PARAM_INT);
     require_once CURMAN_DIRLOCATION . '/form/customfieldform.class.php';
     $tmppage = new customfieldpage(array('level' => $level, 'action' => 'editfield'), $this);
     $form = new customfieldform($tmppage->get_moodle_url(), $this);
     if ($form->is_cancelled()) {
         $tmppage = new customfieldpage(array('level' => $level));
         redirect($tmppage->get_url(), get_string('edit_cancelled', 'block_curr_admin'));
     } else {
         if ($data = $form->get_data()) {
             $field = new field($data);
             if ($id) {
                 $field->id = $id;
                 $field->update();
             } else {
                 $field->add();
                 // assume each field only belongs to one context level (for now)
                 $fieldcontext = new field_contextlevel();
                 $fieldcontext->fieldid = $field->id;
                 $fieldcontext->contextlevel = $ctxlvl;
                 $fieldcontext->add();
             }
             //don't use !empty here because we might be storing a 0 or similar value
             if ($data->defaultdata != '') {
                 // save the default value
                 $defaultdata = $data->defaultdata;
                 if ($field->multivalued) {
                     // parse as a CSV string
                     // until we can use str_getcsv from PHP 5.3...
                     $temp = fopen("php://memory", "rw");
                     fwrite($temp, $defaultdata);
                     rewind($temp);
                     $defaultdata = fgetcsv($temp);
                     fclose($temp);
                 }
                 field_data::set_for_context_and_field(NULL, $field, $defaultdata);
             } else {
                 if ($field->multivalued) {
                     field_data::set_for_context_and_field(NULL, $field, array());
                 } else {
                     field_data::set_for_context_and_field(NULL, $field, NULL);
                 }
             }
             $plugins = get_list_of_plugins('curriculum/plugins');
             foreach ($plugins as $plugin) {
                 if (is_readable(CURMAN_DIRLOCATION . '/plugins/' . $plugin . '/custom_fields.php')) {
                     include_once CURMAN_DIRLOCATION . '/plugins/' . $plugin . '/custom_fields.php';
                     if (function_exists("{$plugin}_field_save_form_data")) {
                         call_user_func("{$plugin}_field_save_form_data", $form, $field, $data);
                     }
                 }
             }
             $tmppage = new customfieldpage(array('level' => $level));
             redirect($tmppage->get_url(), get_string('field_saved', 'block_curr_admin', $field));
         } else {
             if (!empty($id)) {
                 if ($this->optional_param('from', NULL, PARAM_CLEAN) == 'moodle' && $level == 'user') {
                     $moodlefield = $CURMAN->db->get_record('user_info_field', 'id', $id);
                     if (!$moodlefield) {
                         print_error('invalidfieldid', 'block_curr_admin');
                     }
                     unset($moodlefield->id);
                     $data_array = (array) $moodlefield;
                     $data_array['datatype'] = 'text';
                     $data_array['manual_field_control'] = $moodlefield->datatype;
                     switch ($moodlefield->datatype) {
                         case field::checkbox:
                             $data_array['datatype'] = 'bool';
                             break;
                         case field::menu:
                             $data_array['datatype'] = 'char';
                             $data_array['manual_field_options'] = $moodlefield->param1;
                             break;
                         case field::textarea:
                             $data_array['manual_field_columns'] = $moodlefield->param1;
                             $data_array['manual_field_rows'] = $moodlefield->param2;
                             break;
                         case field::text:
                             if ($moodlefield->param3) {
                                 $data_array['manual_field_control'] = 'password';
                             }
                             $data_array['manual_field_columns'] = $moodlefield->param1;
                             $data_array['manual_field_maxlength'] = $moodlefield->param2;
                             break;
                     }
                 } else {
                     $data = new field($id);
                     $data_array = $data->to_array();
                     $defaultdata = field_data::get_for_context_and_field(NULL, $data);
                     if (!empty($defaultdata)) {
                         if ($data->multivalued) {
                             $values = array();
                             // extract the data
                             foreach ($defaultdata as $data) {
                                 $values[] = $data->data;
                             }
                             // represent as a CSV string
                             $fh = fopen("php://memory", "rw");
                             fputcsv($fh, $values);
                             rewind($fh);
                             $defaultdata = fgets($fh);
                             fclose($fh);
                         } else {
                             $defaultdata = current($defaultdata);
                             $defaultdata = $defaultdata->data;
                         }
                     }
                     $data_array['defaultdata'] = $defaultdata;
                     $plugins = get_list_of_plugins('curriculum/plugins');
                     foreach ($plugins as $plugin) {
                         if (is_readable(CURMAN_DIRLOCATION . '/plugins/' . $plugin . '/custom_fields.php')) {
                             include_once CURMAN_DIRLOCATION . '/plugins/' . $plugin . '/custom_fields.php';
                             if (function_exists("{$plugin}_field_get_form_data")) {
                                 $data_array += call_user_func("{$plugin}_field_get_form_data", $form, $data);
                             }
                         }
                     }
                 }
                 $form->set_data($data_array);
             }
             $form->display();
         }
     }
 }