/**
  * Delete a field completely
  */
 public function delete()
 {
     // Delete the comments.
     $params = array('contextid' => $this->df->context->id, 'commentarea' => $this->name);
     comment::delete_comments($params);
     return parent::delete();
 }
 /**
  * Update a field in the database.
  * Overriding parent to adjust ratings where scale changes.
  *
  * @return bool
  */
 public function update($data)
 {
     global $DB;
     // The old scale id should still be in _scaleid.
     $oldscaleid = $this->_scaleid;
     if ($result = parent::update($data)) {
         // Adjust ratings if needed.
         $newscaleid = $this->param1;
         if ($newscaleid != $oldscaleid) {
             // Get all the rating records for this field instance.
             $params = array('contextid' => $this->df->context->id, 'component' => 'mod_dataform', 'ratingarea' => $this->name);
             if ($ratings = $DB->get_records('rating', $params)) {
                 foreach ($ratings as $rid => $rating) {
                     // Adjust the rating scale id.
                     $rating->scaleid = $newscaleid;
                     if ($newscaleid < 0) {
                         // Adjust rating rating for custom scale.
                         if ($oldscaleid > 0) {
                             // When switching from point to scale, delete 0 ratings,
                             // because they have no meaning in scales.
                             if ($rating->rating == 0) {
                                 $DB->delete_records('rating', array('id' => $rid));
                                 continue;
                             }
                         }
                         $scale = $DB->get_record('scale', array('id' => -$newscaleid), '*', MUST_EXIST);
                         $scalearray = explode(',', $scale->scale);
                         if ($rating->rating > count($scalearray)) {
                             $rating->rating = count($scalearray);
                         }
                     } else {
                         if ($rating->rating > $newscaleid) {
                             // Adjust rating rating for point scale.
                             $rating->rating = $newscaleid;
                         }
                     }
                     // Update rating in DB.
                     $DB->update_record('rating', $rating);
                 }
             }
         }
     }
     return $result;
 }
 /**
  *
  */
 public function get_search_sql($search)
 {
     global $USER;
     $state = $search[3];
     // State may be searched by name.
     $statekey = array_search($state, $this->states);
     if ($statekey !== false) {
         $search[3] = $statekey;
         return parent::get_search_sql($search);
     }
     // State may be searched by index.
     if (is_numeric($state) and $state < count($this->states)) {
         return parent::get_search_sql($search);
     }
     return null;
 }