/**
  * Delete a record, plus all associated custom fields.
  */
 public function delete()
 {
     $this->_load_context();
     $filter = new field_filter('contextid', $this->_context->id);
     field_data_int::delete_records($filter);
     field_data_num::delete_records($filter);
     field_data_char::delete_records($filter);
     field_data_text::delete_records($filter);
     parent::delete();
 }
 /**
  * Create data for our test field field.
  *
  * @param int   $fieldid The database id of the PM custom field
  * @param array $data    The data to set
  */
 protected function create_field_data($fieldid, $data)
 {
     global $CFG, $DB;
     $field = new field($fieldid);
     $field->load();
     $fieldcontext = $DB->get_field(field_contextlevel::TABLE, 'contextlevel', array('fieldid' => $fieldid));
     // Obtain the PM user context.
     switch ($fieldcontext) {
         case CONTEXT_ELIS_USER:
             $context = \local_elisprogram\context\user::instance(200);
             break;
         case CONTEXT_ELIS_COURSE:
             $context = \local_elisprogram\context\course::instance(200);
             break;
         case CONTEXT_ELIS_CLASS:
             $context = \local_elisprogram\context\pmclass::instance(200);
             break;
         case CONTEXT_ELIS_PROGRAM:
             $context = \local_elisprogram\context\program::instance(200);
             break;
     }
     field_data_char::set_for_context_and_field($context, $field, $data);
 }
 /**
  * Validate that an invalid category for a field causes a field
  * to be moved to the Miscellaneous category
  */
 public function test_orphanedcustomfieldsmovedtomiscellaneouscategory()
 {
     global $CFG, $DB;
     $contextlevel = CONTEXT_ELIS_USERSET;
     $misccat = get_string('misc_category', 'local_elisprogram');
     // Set up a custom field with an invalid category.
     $field = new field(array('name' => 'testcustomfieldname', 'datatype' => 'char', 'categoryid' => '99', 'multivalued' => 1));
     $field->save();
     // Set up the default data.
     $defaultparams = array('fieldid' => $field->id, 'contextid' => $contextlevel, 'data' => 'value1');
     $defaultdata = new field_data_char($defaultparams);
     $defaultdata->save();
     // Also create a contextlevel record for the field.
     $fieldcontext = new field_contextlevel();
     $fieldcontext->fieldid = $field->id;
     $fieldcontext->contextlevel = $contextlevel;
     $fieldcontext->save();
     // Call function to check if this is an orphaned field.
     pm_fix_orphaned_fields();
     // Assert that the field exists.
     $result = $DB->get_field(field::TABLE, 'id', array('id' => $field->id));
     $this->assertEquals($field->id, $result);
     // Assert that the field is in the correct context still.
     $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_contextlevel::TABLE . "} ctx\n                    ON ctx.fieldid = field.id\n                 WHERE ctx.contextlevel = ?\n                   AND field.id = ?";
     $params = array($contextlevel, $field->id);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($field->id, $result);
     // Assert that the Miscellaneous category exists.
     $result = $DB->get_field(field_category::TABLE, 'name', array('name' => $misccat));
     $this->assertEquals($misccat, $result);
     // Assert that the Miscellaneous category is in the correct context.
     $sql = "SELECT category.name\n                  FROM {" . field_category::TABLE . "} category\n                  JOIN {" . field_category_contextlevel::TABLE . "} category_context\n                    ON category.id = category_context.categoryid\n                 WHERE category_context.contextlevel = ?\n                   AND category.name = ?";
     $params = array($contextlevel, $misccat);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($misccat, $result);
     // Assert that the field is in the Miscellaneous category.
     $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_category::TABLE . "} category\n                    ON category.id = field.categoryid\n                 WHERE field.id = ?\n                   AND category.name = ?";
     $params = array($field->id, $misccat);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($field->id, $result);
     // Now check that an invalid field will be moved to the existing Miscellaneous category
     // and not create a second Miscellaneous category.
     // Set up a custom field with an invalid category.
     $field = new field(array('name' => 'testcustomfieldname2', 'datatype' => 'char', 'categoryid' => '109'));
     $field->save();
     // Set up the default data.
     $defaultparams = array('fieldid' => $field->id, 'contextid' => $contextlevel, 'data' => 'value2');
     $defaultdata = new field_data_char($defaultparams);
     $defaultdata->save();
     // Also create a contextlevel record for the field.
     $fieldcontext = new field_contextlevel();
     $fieldcontext->fieldid = $field->id;
     $fieldcontext->contextlevel = $contextlevel;
     $fieldcontext->save();
     // Call function to check if this is an orphaned field.
     pm_fix_orphaned_fields();
     // Assert that only one Miscellaneous category exists.
     $result = $DB->get_records(field_category::TABLE, array('name' => $misccat));
     $this->assertEquals(1, count($result));
     // Assert that the field is in the Miscellaneous category.
     $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_category::TABLE . "} category\n                    ON category.id = field.categoryid\n                 WHERE field.id = ?\n                   AND category.name = ?";
     $params = array($field->id, $misccat);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($field->id, $result);
 }
 /**
  * Validate that the field_data "get_for_context_and_field" method always excludes a custom field's default 
  * data when some data exists for the  appropriate context, regardless of the related parameter value.
  *
  * @param boolean $includedefault
  * @dataProvider include_default_provider
  */
 public function test_get_for_context_and_field_excludes_default_value_when_data_exists($includedefault)
 {
     // Set up our custom field data.
     $field = $this->init_custom_field();
     // Set up our data point.
     $fielddatachar = new field_data_char(array('fieldid' => $field->id, 'contextid' => self::CONTEXTID, 'data' => 'value3'));
     $fielddatachar->save();
     // Run get_for_context_and_field to obtain our data set.
     $context = new stdClass();
     $context->id = self::CONTEXTID;
     $data = field_data::get_for_context_and_field($context, $field, $includedefault);
     // Validate number of data records (one for specific context).
     $count = 0;
     $record = null;
     foreach ($data as $datum) {
         $count++;
         $record = $datum;
     }
     $this->assertEquals(1, $count);
     $this->assertEquals($field->id, $record->fieldid);
     $this->assertEquals('value3', $record->data);
 }