original_string: public static function save_grades($assignmentid, $applytoall = false, $grades) {
          global $CFG, $USER;
  
          $params = self::validate_parameters(self::save_grades_parameters(),
                                              array('assignmentid' => $assignmentid,
                                                    'applytoall' => $applytoall,
                                                    'grades' => $grades));
  
          list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);
  
          if ($assignment->get_instance()->teamsubmission && $params['applytoall']) {
              // Check that only 1 user per submission group is provided.
              $groupids = array();
              foreach ($params['grades'] as $gradeinfo) {
                  $group = $assignment->get_submission_group($gradeinfo['userid']);
                  if (in_array($group->id, $groupids)) {
                      throw new invalid_parameter_exception('Multiple grades for the same team have been supplied '
                                                            .' this is not permitted when the applytoall flag is set');
                  } else {
                      $groupids[] = $group->id;
                  }
              }
          }
  
          foreach ($params['grades'] as $gradeinfo) {
              $gradedata = (object)$gradeinfo['plugindata'];
              $gradedata->addattempt = $gradeinfo['addattempt'];
              $gradedata->attemptnumber = $gradeinfo['attemptnumber'];
              $gradedata->workflowstate = $gradeinfo['workflowstate'];
              $gradedata->applytoall = $params['applytoall'];
              $gradedata->grade = $gradeinfo['grade'];
  
              if (!empty($gradeinfo['advancedgradingdata'])) {
                  $advancedgrading = array();
                  $criteria = reset($gradeinfo['advancedgradingdata']);
                  foreach ($criteria as $key => $criterion) {
                      $details = array();
                      foreach ($criterion as $value) {
                          foreach ($value['fillings'] as $filling) {
                              $details[$value['criterionid']] = $filling;
                          }
                      }
                      $advancedgrading[$key] = $details;
                  }
                  $gradedata->advancedgrading = $advancedgrading;
              }
              $assignment->save_grade($gradeinfo['userid'], $gradedata);
          }
  
          return null;
      }
code: public static function save_grades($assignmentid, $applytoall = false, $grades) {
          global $CFG, $USER;
  
          $params = self::validate_parameters(self::save_grades_parameters(),
                                              array('assignmentid' => $assignmentid,
                                                    'applytoall' => $applytoall,
                                                    'grades' => $grades));
  
          list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);
  
          if ($assignment->get_instance()->teamsubmission && $params['applytoall']) {
              // Check that only 1 user per submission group is provided.
              $groupids = array();
              foreach ($params['grades'] as $gradeinfo) {
                  $group = $assignment->get_submission_group($gradeinfo['userid']);
                  if (in_array($group->id, $groupids)) {
                      throw new invalid_parameter_exception('Multiple grades for the same team have been supplied '
                                                            .' this is not permitted when the applytoall flag is set');
                  } else {
                      $groupids[] = $group->id;
                  }
              }
          }
  
          foreach ($params['grades'] as $gradeinfo) {
              $gradedata = (object)$gradeinfo['plugindata'];
              $gradedata->addattempt = $gradeinfo['addattempt'];
              $gradedata->attemptnumber = $gradeinfo['attemptnumber'];
              $gradedata->workflowstate = $gradeinfo['workflowstate'];
              $gradedata->applytoall = $params['applytoall'];
              $gradedata->grade = $gradeinfo['grade'];
  
              if (!empty($gradeinfo['advancedgradingdata'])) {
                  $advancedgrading = array();
                  $criteria = reset($gradeinfo['advancedgradingdata']);
                  foreach ($criteria as $key => $criterion) {
                      $details = array();
                      foreach ($criterion as $value) {
                          foreach ($value['fillings'] as $filling) {
                              $details[$value['criterionid']] = $filling;
                          }
                      }
                      $advancedgrading[$key] = $details;
                  }
                  $gradedata->advancedgrading = $advancedgrading;
              }
              $assignment->save_grade($gradeinfo['userid'], $gradedata);
          }
  
          return null;
      }
code_tokens: public static function save_grades ( $ assignmentid , $ applytoall = false , $ grades ) { global $ CFG , $ USER ; $ params = self :: validate_parameters ( self :: save_grades_parameters ( ) , array ( 'assignmentid' => $ assignmentid , 'applytoall' => $ applytoall , 'grades' => $ grades ) ) ; list ( $ assignment , $ course , $ cm , $ context ) = self :: validate_assign ( $ params [ 'assignmentid' ] ) ; if ( $ assignment -> get_instance ( ) -> teamsubmission && $ params [ 'applytoall' ] ) { // Check that only 1 user per submission group is provided. $ groupids = array ( ) ; foreach ( $ params [ 'grades' ] as $ gradeinfo ) { $ group = $ assignment -> get_submission_group ( $ gradeinfo [ 'userid' ] ) ; if ( in_array ( $ group -> id , $ groupids ) ) { throw new invalid_parameter_exception ( 'Multiple grades for the same team have been supplied ' . ' this is not permitted when the applytoall flag is set' ) ; } else { $ groupids [ ] = $ group -> id ; } } } foreach ( $ params [ 'grades' ] as $ gradeinfo ) { $ gradedata = ( object ) $ gradeinfo [ 'plugindata' ] ; $ gradedata -> addattempt = $ gradeinfo [ 'addattempt' ] ; $ gradedata -> attemptnumber = $ gradeinfo [ 'attemptnumber' ] ; $ gradedata -> workflowstate = $ gradeinfo [ 'workflowstate' ] ; $ gradedata -> applytoall = $ params [ 'applytoall' ] ; $ gradedata -> grade = $ gradeinfo [ 'grade' ] ; if ( ! empty ( $ gradeinfo [ 'advancedgradingdata' ] ) ) { $ advancedgrading = array ( ) ; $ criteria = reset ( $ gradeinfo [ 'advancedgradingdata' ] ) ; foreach ( $ criteria as $ key => $ criterion ) { $ details = array ( ) ; foreach ( $ criterion as $ value ) { foreach ( $ value [ 'fillings' ] as $ filling ) { $ details [ $ value [ 'criterionid' ] ] = $ filling ; } } $ advancedgrading [ $ key ] = $ details ; } $ gradedata -> advancedgrading = $ advancedgrading ; } $ assignment -> save_grade ( $ gradeinfo [ 'userid' ] , $ gradedata ) ; } return null ; }
docstring: Save multiple student grades for a single assignment.
  
  @param int $assignmentid The id of the assignment
  @param boolean $applytoall If set to true and this is a team assignment,
  apply the grade to all members of the group
  @param array $grades grade data for one or more students that includes
  userid - The id of the student being graded
  grade - The grade (ignored if the assignment uses advanced grading)
  attemptnumber - The attempt number
  addattempt - Allow another attempt
  workflowstate - New workflow state
  plugindata - Custom data used by plugins
  advancedgradingdata - Optional Advanced grading data
  @throws invalid_parameter_exception if multiple grades are supplied for
  a team assignment that has $applytoall set to true
  @return null
  @since Moodle 2.7
docstring_tokens: Save multiple student grades for a single assignment .