/** * Construct a data object. * @param mixed $src record source. It can be * - false: an empty object is created * - an integer: loads the record that has record id equal to $src * - an object: creates an object with field data taken from the members * of $src * - an array: creates an object with the field data taken from the * elements of $src * @param mixed $field_map mapping for field names from $src. If it is a * string, then it will be treated as a prefix for field names. If it is * an array, then it is a mapping of destination field names to source * field names. * @param array $associations pre-fetched associated objects (to avoid * needing to re-fetch) * @param boolean $from_db whether or not the record source object/array * comes from the database * @param array $extradatafields extra data from the $src object/array * associated with the record that should be kept in the data object (such * as counts of related records) * @param moodle_database $database database object to use (null for the * default database) */ public function __construct($src = false, $field_map = null, array $associations = array(), $from_db = false, array $extradatafields = array(), moodle_database $database = null) { global $DB; if (!isset(self::$_unset)) { self::$_unset = new stdClass(); } // mark all the fields as unset $reflect = new ReflectionClass(get_class($this)); $prefix_len = strlen(self::FIELD_PREFIX); foreach ($reflect->getProperties() as $prop) { if (strncmp($prop->getName(), self::FIELD_PREFIX, $prefix_len) === 0) { $field_name = $prop->getName(); $this->{$field_name} = self::$_unset; } } if ($database === null) { $this->_db = $DB; } else { $this->_db = $database; } // initialize the object fields if ($src === false) { $this->_setup_extradata((object) array(), $extradatafields); } else { if (is_numeric($src)) { $this->_dbfield_id = $src; $this->_setup_extradata((object) array(), $extradatafields); } else { if (is_object($src)) { $this->_load_data_from_record($src, false, $field_map, $from_db, $extradatafields); } else { if (is_array($src)) { $this->_load_data_from_record((object) $src, false, $field_map, $from_db, $extradatafields); } else { throw new data_object_exception('data_object_construct_invalid_source', 'local_eliscore'); } } } } $this->_associated_objects = $associations; }