Ejemplo n.º 1
0
 /**
  * Set up an ELIS custom field category.
  * @return field_category The created field category.
  */
 protected function set_up_elis_field_category()
 {
     $data = new stdClass();
     $data->name = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USER) . ' Test';
     $category = new field_category($data);
     $category->save();
     $categorycontext = new field_category_contextlevel();
     $categorycontext->categoryid = $category->id;
     $categorycontext->contextlevel = CONTEXT_ELIS_USER;
     $categorycontext->save();
     return $category;
 }
 /**
  * Test that the function will return the existing category if one exists for the given name and contextlevel.
  */
 public function test_return_existing()
 {
     global $DB;
     $name = 'Test Category';
     $contextlevel = CONTEXT_ELIS_USER;
     $cat = new field_category();
     $cat->name = $name;
     $cat->save();
     $catctx = new field_category_contextlevel();
     $catctx->categoryid = $cat->id;
     $catctx->contextlevel = $contextlevel;
     $catctx->save();
     $this->assertNotEmpty($this->get_for_name_and_contextlevel($name, $contextlevel));
     $cat2 = field_category::ensure_exists_for_contextlevel($name, $contextlevel);
     $this->assertNotEmpty($cat2);
     $this->assertEquals($cat->id, $cat2->id);
     $this->assertEquals($cat->name, $cat2->name);
 }
 /**
  * Create the test custom profile field, category and owner
  *
  * @param string $contextlevelname The name of the custom context level to create the field at
  * @param string $datatype The string identifier of the data type to use
  * @param string $uitype The string identifier of the UI / control type to use
  * @param boolean $multivalued Set to true to make field multivalued, otherwise false
  * @param mixed $options Array of menu options, or null for none
  * @param int $maxlength The maximum data length, or null for none
  * @return int The id of the created field
  */
 private function create_test_field($contextlevelname, $datatype, $uitype, $multivalued, $options, $maxlength, $inctime)
 {
     global $CFG;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::lib('data/customfield.class.php');
     // Category.
     $fieldcategory = new field_category(array('name' => 'testcategoryname'));
     $fieldcategory->save();
     // Category contextlevel.
     $contextlevel = \local_eliscore\context\helper::get_level_from_name($contextlevelname);
     $fieldcategorycontextlevel = new field_category_contextlevel(array('categoryid' => $fieldcategory->id, 'contextlevel' => $contextlevel));
     $fieldcategorycontextlevel->save();
     // Field.
     $field = new field(array('shortname' => 'testfieldshortname', 'name' => 'testfieldname', 'categoryid' => $fieldcategory->id, 'datatype' => $datatype));
     if ($multivalued) {
         // Enable multivalued ability.
         $field->multivalued = true;
     }
     $field->save();
     // Field contextlevel.
     $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => $contextlevel));
     $fieldcontextlevel->save();
     // Field owner.
     $ownerdata = array('control' => $uitype);
     if ($options !== null) {
         // Set options.
         $ownerdata['options'] = implode("\n", $options);
     }
     if ($maxlength !== null) {
         // Set max length.
         $ownerdata['maxlength'] = $maxlength;
     }
     if ($inctime !== null) {
         $ownerdata['inctime'] = $inctime;
     }
     field_owner::ensure_field_owner_exists($field, 'manual', $ownerdata);
     return $field->id;
 }
Ejemplo n.º 4
0
/**
 * Function to move any custom fields with an invalid category
 * into a category called Miscellaneous
 *
 */
function pm_fix_orphaned_fields()
{
    global $DB;
    $misc_cat = get_string('misc_category', 'local_elisprogram');
    //set up context array
    $context_array = \local_eliscore\context\helper::get_all_levels();
    foreach ($context_array as $contextlevel => $contextname) {
        //find all fields with non existant category assignments
        $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_contextlevel::TABLE . "} ctx ON ctx.fieldid = field.id AND ctx.contextlevel = ?\n                  WHERE NOT EXISTS (\n                    SELECT 'x' FROM {" . field_category::TABLE . "} category\n                    WHERE category.id = field.categoryid)";
        $params = array($contextlevel);
        $rs = $DB->get_recordset_sql($sql, $params);
        //if any are found - then check if miscellaneous category exists - if not, create it
        foreach ($rs as $field) {
            $sql = "SELECT category.id\n                    FROM {" . field_category::TABLE . "} category\n                    JOIN {" . field_category_contextlevel::TABLE . "} categorycontext\n                      ON categorycontext.categoryid = category.id\n                    WHERE categorycontext.contextlevel = ?\n                      AND category.name = ?";
            $params = array($contextlevel, $misc_cat);
            $categoryid = $DB->get_field_sql($sql, $params);
            //create a miscellaneous category if it doesn't already exist
            if (!$categoryid) {
                // create an empty category
                $category = new field_category(array('name' => $misc_cat));
                $category->save();
                $categorycontext = new field_category_contextlevel();
                $categorycontext->categoryid = $category->id;
                $categorycontext->contextlevel = $contextlevel;
                $categorycontext->save();
                $categoryid = $category->id;
            }
            $field = new field($field->id);
            // set the field category to the Miscellaneous category
            $field->categoryid = $categoryid;
            $field->save();
        }
        $rs->close();
    }
}
 /**
  * Create the test custom profile field and owner
  *
  * @param string $contextlevelname The name of the custom context level to create the field at
  * @param string $name PM custom field shortname
  * @param string $datatype The string identifier of the data type to use
  * @param string $uitype The string identifier of the UI / control type to use
  * @param int $categoryid PM custom field category id
  * @param string $options Extra parameter, used for select options
  * @param string $defaultdata Default value.
  *
  * @return int The id of the created field
  */
 private function create_test_field($contextlevelname = 'user', $name = 'testfieldname', $datatype, $uitype, $categoryid, $options = null, $defaultdata = null)
 {
     global $CFG;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::lib('data/customfield.class.php');
     // Category contextlevel.
     $contextlevel = \local_eliscore\context\helper::get_level_from_name($contextlevelname);
     $fieldcategorycontextlevel = new field_category_contextlevel(array('categoryid' => $categoryid, 'contextlevel' => $contextlevel));
     $fieldcategorycontextlevel->save();
     // Field.
     $field = new field(array('shortname' => 'testfieldshortname', 'name' => $name, 'categoryid' => $categoryid, 'datatype' => $datatype));
     $field->save();
     // Field_data if a default value needs to be set.
     if ($defaultdata !== null) {
         $classname = 'field_data_' . $datatype;
         $fielddata = new $classname(array('fieldid' => $field->id, 'data' => $defaultdata));
         $fielddata->save();
     }
     // Field contextlevel.
     $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => $contextlevel));
     $fieldcontextlevel->save();
     // Field owner.
     $ownerdata = array('control' => $uitype);
     if ($options !== null) {
         // Set options.
         $options = is_array($options) ? implode("\n", $options) : $options;
         $ownerdata['options'] = $options;
     }
     field_owner::ensure_field_owner_exists($field, 'manual', $ownerdata);
     return $field;
 }
Ejemplo n.º 6
0
 /**
  * Create a test custom field category.
  * @return field_category The test category.
  */
 protected function create_field_category($context)
 {
     $data = new stdClass();
     $data->name = \local_eliscore\context\helper::get_class_for_level($context) . ' Test';
     $category = new field_category($data);
     $category->save();
     $categorycontext = new field_category_contextlevel();
     $categorycontext->categoryid = $category->id;
     $categorycontext->contextlevel = $context;
     $categorycontext->save();
     return $category;
 }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
0
 /**
  * Ensure a category exists with a given name for a given contextlevel.
  * @param string $name The name of the category.
  * @param int $contextlevel The contextlevel.
  * @return field_category Either the existing or the new field_category object.
  */
 public static function ensure_exists_for_contextlevel($name, $contextlevel)
 {
     if (empty($name)) {
         return null;
     }
     if (empty($contextlevel) || !is_int($contextlevel)) {
         return null;
     }
     global $DB;
     $sql = 'SELECT cat.id
               FROM {' . static::TABLE . '} cat
               JOIN {' . field_category_contextlevel::TABLE . '} catctx ON cat.id = catctx.categoryid
              WHERE cat.name = ?
                    AND catctx.contextlevel = ?';
     $existingcat = $DB->get_records_sql($sql, array($name, $contextlevel));
     if (!empty($existingcat)) {
         $existingcat = current($existingcat);
         $category = new field_category($existingcat->id);
         $category->load();
         return $category;
     } else {
         $category = new field_category();
         $category->name = $name;
         $category->save();
         $catctx = new field_category_contextlevel();
         $catctx->categoryid = $category->id;
         $catctx->contextlevel = $contextlevel;
         $catctx->save();
         return $category;
     }
 }
Ejemplo n.º 9
0
 function display_editcategory()
 {
     require_once elispm::file('form/fieldcategoryform.class.php');
     $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', 0, PARAM_INT);
     $tmppage = new customfieldpage(array('level' => $level, 'id' => $id, 'action' => 'editcategory', 'level' => $level));
     $form = new fieldcategoryform($tmppage->url);
     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()) {
             $data->id = $id;
             $category = new field_category($data);
             if ($category->id) {
                 $category->save();
             } else {
                 $category->save();
                 // assume each category only belongs to one context level (for now)
                 $categorycontext = new field_category_contextlevel();
                 $categorycontext->categoryid = $category->id;
                 $categorycontext->contextlevel = $ctxlvl;
                 $categorycontext->save();
             }
             $tmppage = new customfieldpage(array('level' => $level));
             redirect($tmppage->url, get_string('field_category_saved', 'local_elisprogram', $category->name));
         } else {
             if ($id) {
                 $category = new field_category($id);
                 $form->set_data($category);
             }
             $form->display();
         }
     }
 }
Ejemplo n.º 10
0
 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();
         }
     }
 }