original_string: protected function aggregate_grading_grades_process(array $assessments, $timegraded = null) {
          global $DB;
  
          $reviewerid = null; // the id of the reviewer being processed
          $current    = null; // the gradinggrade currently saved in database
          $finalgrade = null; // the new grade to be calculated
          $agid       = null; // aggregation id
          $sumgrades  = 0;
          $count      = 0;
  
          if (is_null($timegraded)) {
              $timegraded = time();
          }
  
          foreach ($assessments as $assessment) {
              if (is_null($reviewerid)) {
                  // the id is the same in all records, fetch it during the first loop cycle
                  $reviewerid = $assessment->reviewerid;
              }
              if (is_null($agid)) {
                  // the id is the same in all records, fetch it during the first loop cycle
                  $agid = $assessment->aggregationid;
              }
              if (is_null($current)) {
                  // the currently saved grade is the same in all records, fetch it during the first loop cycle
                  $current = $assessment->aggregatedgrade;
              }
              if (!is_null($assessment->gradinggradeover)) {
                  // the grading grade for this assessment is overridden by a teacher
                  $sumgrades += $assessment->gradinggradeover;
                  $count++;
              } else {
                  if (!is_null($assessment->gradinggrade)) {
                      $sumgrades += $assessment->gradinggrade;
                      $count++;
                  }
              }
          }
          if ($count > 0) {
              $finalgrade = grade_floatval($sumgrades / $count);
          }
  
          // Event information.
          $params = array(
              'context' => $this->context,
              'courseid' => $this->course->id,
              'relateduserid' => $reviewerid
          );
  
          // check if the new final grade differs from the one stored in the database
          if (grade_floats_different($finalgrade, $current)) {
              $params['other'] = array(
                  'currentgrade' => $current,
                  'finalgrade' => $finalgrade
              );
  
              // we need to save new calculation into the database
              if (is_null($agid)) {
                  // no aggregation record yet
                  $record = new stdclass();
                  $record->workshopid = $this->id;
                  $record->userid = $reviewerid;
                  $record->gradinggrade = $finalgrade;
                  $record->timegraded = $timegraded;
                  $record->id = $DB->insert_record('workshop_aggregations', $record);
                  $params['objectid'] = $record->id;
                  $event = \mod_workshop\event\assessment_evaluated::create($params);
                  $event->trigger();
              } else {
                  $record = new stdclass();
                  $record->id = $agid;
                  $record->gradinggrade = $finalgrade;
                  $record->timegraded = $timegraded;
                  $DB->update_record('workshop_aggregations', $record);
                  $params['objectid'] = $agid;
                  $event = \mod_workshop\event\assessment_reevaluated::create($params);
                  $event->trigger();
              }
          }
      }
code: protected function aggregate_grading_grades_process(array $assessments, $timegraded = null) {
          global $DB;
  
          $reviewerid = null; // the id of the reviewer being processed
          $current    = null; // the gradinggrade currently saved in database
          $finalgrade = null; // the new grade to be calculated
          $agid       = null; // aggregation id
          $sumgrades  = 0;
          $count      = 0;
  
          if (is_null($timegraded)) {
              $timegraded = time();
          }
  
          foreach ($assessments as $assessment) {
              if (is_null($reviewerid)) {
                  // the id is the same in all records, fetch it during the first loop cycle
                  $reviewerid = $assessment->reviewerid;
              }
              if (is_null($agid)) {
                  // the id is the same in all records, fetch it during the first loop cycle
                  $agid = $assessment->aggregationid;
              }
              if (is_null($current)) {
                  // the currently saved grade is the same in all records, fetch it during the first loop cycle
                  $current = $assessment->aggregatedgrade;
              }
              if (!is_null($assessment->gradinggradeover)) {
                  // the grading grade for this assessment is overridden by a teacher
                  $sumgrades += $assessment->gradinggradeover;
                  $count++;
              } else {
                  if (!is_null($assessment->gradinggrade)) {
                      $sumgrades += $assessment->gradinggrade;
                      $count++;
                  }
              }
          }
          if ($count > 0) {
              $finalgrade = grade_floatval($sumgrades / $count);
          }
  
          // Event information.
          $params = array(
              'context' => $this->context,
              'courseid' => $this->course->id,
              'relateduserid' => $reviewerid
          );
  
          // check if the new final grade differs from the one stored in the database
          if (grade_floats_different($finalgrade, $current)) {
              $params['other'] = array(
                  'currentgrade' => $current,
                  'finalgrade' => $finalgrade
              );
  
              // we need to save new calculation into the database
              if (is_null($agid)) {
                  // no aggregation record yet
                  $record = new stdclass();
                  $record->workshopid = $this->id;
                  $record->userid = $reviewerid;
                  $record->gradinggrade = $finalgrade;
                  $record->timegraded = $timegraded;
                  $record->id = $DB->insert_record('workshop_aggregations', $record);
                  $params['objectid'] = $record->id;
                  $event = \mod_workshop\event\assessment_evaluated::create($params);
                  $event->trigger();
              } else {
                  $record = new stdclass();
                  $record->id = $agid;
                  $record->gradinggrade = $finalgrade;
                  $record->timegraded = $timegraded;
                  $DB->update_record('workshop_aggregations', $record);
                  $params['objectid'] = $agid;
                  $event = \mod_workshop\event\assessment_reevaluated::create($params);
                  $event->trigger();
              }
          }
      }
code_tokens: protected function aggregate_grading_grades_process ( array $ assessments , $ timegraded = null ) { global $ DB ; $ reviewerid = null ; // the id of the reviewer being processed $ current = null ; // the gradinggrade currently saved in database $ finalgrade = null ; // the new grade to be calculated $ agid = null ; // aggregation id $ sumgrades = 0 ; $ count = 0 ; if ( is_null ( $ timegraded ) ) { $ timegraded = time ( ) ; } foreach ( $ assessments as $ assessment ) { if ( is_null ( $ reviewerid ) ) { // the id is the same in all records, fetch it during the first loop cycle $ reviewerid = $ assessment -> reviewerid ; } if ( is_null ( $ agid ) ) { // the id is the same in all records, fetch it during the first loop cycle $ agid = $ assessment -> aggregationid ; } if ( is_null ( $ current ) ) { // the currently saved grade is the same in all records, fetch it during the first loop cycle $ current = $ assessment -> aggregatedgrade ; } if ( ! is_null ( $ assessment -> gradinggradeover ) ) { // the grading grade for this assessment is overridden by a teacher $ sumgrades += $ assessment -> gradinggradeover ; $ count ++ ; } else { if ( ! is_null ( $ assessment -> gradinggrade ) ) { $ sumgrades += $ assessment -> gradinggrade ; $ count ++ ; } } } if ( $ count > 0 ) { $ finalgrade = grade_floatval ( $ sumgrades / $ count ) ; } // Event information. $ params = array ( 'context' => $ this -> context , 'courseid' => $ this -> course -> id , 'relateduserid' => $ reviewerid ) ; // check if the new final grade differs from the one stored in the database if ( grade_floats_different ( $ finalgrade , $ current ) ) { $ params [ 'other' ] = array ( 'currentgrade' => $ current , 'finalgrade' => $ finalgrade ) ; // we need to save new calculation into the database if ( is_null ( $ agid ) ) { // no aggregation record yet $ record = new stdclass ( ) ; $ record -> workshopid = $ this -> id ; $ record -> userid = $ reviewerid ; $ record -> gradinggrade = $ finalgrade ; $ record -> timegraded = $ timegraded ; $ record -> id = $ DB -> insert_record ( 'workshop_aggregations' , $ record ) ; $ params [ 'objectid' ] = $ record -> id ; $ event = \ mod_workshop \ event \ assessment_evaluated :: create ( $ params ) ; $ event -> trigger ( ) ; } else { $ record = new stdclass ( ) ; $ record -> id = $ agid ; $ record -> gradinggrade = $ finalgrade ; $ record -> timegraded = $ timegraded ; $ DB -> update_record ( 'workshop_aggregations' , $ record ) ; $ params [ 'objectid' ] = $ agid ; $ event = \ mod_workshop \ event \ assessment_reevaluated :: create ( $ params ) ; $ event -> trigger ( ) ; } } }
docstring: Given an array of all assessments done by a single reviewer, calculates the final grading grade
  
  This calculates the simple mean of the passed grading grades. If, however, the grading grade
  was overridden by a teacher, the gradinggradeover value is returned and the rest of grades are ignored.
  
  @param array $assessments of stdclass(->reviewerid ->gradinggrade ->gradinggradeover ->aggregationid ->aggregatedgrade)
  @param null|int $timegraded explicit timestamp of the aggregation, defaults to the current time
  @return void
docstring_tokens: Given an array of all assessments done by a single reviewer calculates the final grading grade