Пример #1
0
 /**
  * Makes sure that a custom field (identified by $field->shortname) exists
  * for the given context level.  If not, it will create a field, putting it
  * in the given category (identified by $category->name), creating it if
  * necessary.
  *
  * @param object a field object, specifying the field configuration if a
  * new field is created
  * @param mixed the context level
  * @param object a field_category object, specifying the category
  * configuration if a new category is created
  * @return object a field object
  */
 static function ensure_field_exists_for_context_level($field, $ctx_lvl, $category)
 {
     if (!is_numeric($ctx_lvl)) {
         $ctx_lvl = context_level_base::get_custom_context_level($ctx_lvl, 'block_curr_admin');
     }
     // see if we need to create a new field
     $fields = field::get_for_context_level($ctx_lvl);
     if (!empty($fields)) {
         foreach ($fields as $f) {
             if ($f->shortname === $field->shortname) {
                 return new field($f);
             }
         }
     }
     // No existing field found.  See if we need to create a category for it
     $categories = field_category::get_for_context_level($ctx_lvl);
     $found = false;
     if (!empty($categories)) {
         foreach ($categories as $c) {
             if ($c->name === $category->name) {
                 $category = $found = $c;
                 break;
             }
         }
     }
     if (!$found) {
         // create the category
         $category->add();
         $categorycontext = new field_category_contextlevel();
         $categorycontext->categoryid = $category->id;
         $categorycontext->contextlevel = $ctx_lvl;
         $categorycontext->add();
     }
     // create the field
     $field->categoryid = $category->id;
     $field->add();
     $fieldcontext = new field_contextlevel();
     $fieldcontext->fieldid = $field->id;
     $fieldcontext->contextlevel = $ctx_lvl;
     $fieldcontext->add();
     return $field;
 }
 function action_editcategory()
 {
     require_once CURMAN_DIRLOCATION . '/form/fieldcategoryform.class.php';
     $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', 0, PARAM_INT);
     $tmppage = new customfieldpage(array('level' => $level, 'id' => $id, 'action' => 'editcategory', 'level' => $level));
     $form = new fieldcategoryform($tmppage->get_moodle_url());
     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()) {
             $data->id = $id;
             $category = new field_category($data);
             if ($category->id) {
                 $category->update();
             } else {
                 $category->add();
                 // assume each category only belongs to one context level (for now)
                 $categorycontext = new field_category_contextlevel();
                 $categorycontext->categoryid = $category->id;
                 $categorycontext->contextlevel = $ctxlvl;
                 $categorycontext->add();
             }
             $tmppage = new customfieldpage(array('level' => $level));
             redirect($tmppage->get_url(), get_string('field_category_saved', 'block_curr_admin', $category));
         } else {
             if ($id) {
                 $category = new field_category($id);
                 $form->set_data($category);
             }
             $form->display();
         }
     }
 }