コード例 #1
0
 /**
  * Override parent to adjust comment records
  */
 public function update($data)
 {
     global $DB, $OUTPUT;
     $oldname = $this->name;
     if (parent::update($data)) {
         // Adjust comment area in comment records.
         if ($oldname != $this->name) {
             $context = $this->df->context;
             if ($comments = $DB->get_records('comments', array('contextid' => $context->id, 'commentarea' => $oldname))) {
                 foreach ($comments as $comment) {
                     $DB->set_field('comments', 'commentarea', $this->name, array('id' => $comment->id));
                 }
             }
         }
     }
     return true;
 }
コード例 #2
0
 /**
  * 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;
 }