/**
  * constructor
  *
  * @param object $field record from "data_fields" table
  * @param object $data record from "data" table
  * @param object $cm record from "course_modules" table
  */
 function __construct($field = 0, $data = 0, $cm = 0)
 {
     global $CFG, $DB, $datarecord;
     // set up this field in the normal way
     parent::__construct($field, $data, $cm);
     $subparam = $this->subparam;
     $accessparam = $this->accessparam;
     $disabledifparam = $this->disabledifparam;
     $displaytextparam = $this->displaytextparam;
     $sortorderparam = $this->sortorderparam;
     $this->is_special_field = $this->field->name == 'unapprove' || $this->field->name == 'fixdisabledfields' || $this->field->name == 'setdefaultvalues';
     // set view and edit permissions for this user
     if ($this->field && $this->is_special_field) {
         // special fields are not viewable or editable by anyone
         $this->is_editable = false;
         $this->is_viewable = false;
         // field-specific processing for new fields
         if (self::is_new_record()) {
             switch ($this->field->name) {
                 case 'fixdisabledfields':
                     // prevent "missing property" error in data/lib.php
                     // caused by disabled fields in form
                     if (isset($datarecord) && is_object($datarecord)) {
                         $select = 'dataid = ?';
                         $params = array('field_', $this->data->id);
                         $name = $DB->sql_concat('?', 'id') . ' AS formfieldname';
                         if ($names = $DB->get_records_select_menu('data_fields', $select, $params, 'id', "id, {$name}")) {
                             foreach ($names as $name) {
                                 if (!property_exists($datarecord, $name)) {
                                     $datarecord->{$name} = null;
                                 }
                             }
                         }
                     }
                     break;
                 case 'unapprove':
                     // By default records added by teachers and admins
                     // have their "approved" flag set to "1".
                     // We want to detect this situation, so that we can
                     // override it later, in the update_content() method
                     $this->unapprove = has_capability('mod/data:approve', $this->context);
                     break;
                 case 'setdefaultvalues':
                     // setting this flag to true has two effects:
                     // (1) values from the user profile are inserted into the ADD form
                     // (2) any user profile fields that are empty will be updated from
                     //     values in the ADD form
                     $this->setdefaultvalues = true;
                     break;
             }
         }
     } else {
         if (has_capability('mod/data:managetemplates', $this->context)) {
             $this->is_editable = true;
             $this->is_viewable = true;
         } else {
             if (isset($field->{$accessparam})) {
                 $this->is_viewable = $field->{$accessparam} >= self::ACCESS_VIEW;
                 $this->is_editable = $field->{$accessparam} >= self::ACCESS_EDIT;
             }
         }
     }
     // fetch the subfield if there is one
     if (isset($field->{$subparam})) {
         $subtype = $field->{$subparam};
         $subclass = 'data_field_' . $subtype;
         $subfolder = $CFG->dirroot . '/mod/data/field/' . $subtype;
         $filepath = $subfolder . '/field.class.php';
         if (file_exists($filepath)) {
             require_once $filepath;
             $this->subtype = $subtype;
             $this->subclass = $subclass;
             $this->subfolder = $subfolder;
             $this->subfield = new $subclass($field, $data, $cm);
         }
     }
 }