original_string: public static function update_grades($source, $courseid, $component, $activityid,
          $itemnumber, $grades = array(), $itemdetails = array()) {
          global $CFG;
  
          $params = self::validate_parameters(
              self::update_grades_parameters(),
              array(
                  'source' => $source,
                  'courseid' => $courseid,
                  'component' => $component,
                  'activityid' => $activityid,
                  'itemnumber' => $itemnumber,
                  'grades' => $grades,
                  'itemdetails' => $itemdetails
              )
          );
  
          list($itemtype, $itemmodule) = normalize_component($params['component']);
  
          if (! $cm = get_coursemodule_from_id($itemmodule, $activityid)) {
              throw new moodle_exception('invalidcoursemodule');
          }
          $iteminstance = $cm->instance;
  
          $coursecontext = context_course::instance($params['courseid']);
  
          try {
              self::validate_context($coursecontext);
          } catch (Exception $e) {
              $exceptionparam = new stdClass();
              $exceptionparam->message = $e->getMessage();
              $exceptionparam->courseid = $params['courseid'];
              throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
          }
  
          $hidinggrades = false;
          $editinggradeitem = false;
          $editinggrades = false;
  
          $gradestructure = array();
          foreach ($grades as $grade) {
              $editinggrades = true;
              $gradestructure[ $grade['studentid'] ] = array('userid' => $grade['studentid'], 'rawgrade' => $grade['grade']);
          }
          if (!empty($params['itemdetails'])) {
              if (isset($params['itemdetails']['hidden'])) {
                  $hidinggrades = true;
              } else {
                  $editinggradeitem = true;
              }
          }
  
          if ($editinggradeitem && !has_capability('moodle/grade:manage', $coursecontext)) {
              throw new moodle_exception('nopermissiontoviewgrades', 'error', '', null,
                  'moodle/grade:manage required to edit grade information');
          }
          if ($hidinggrades && !has_capability('moodle/grade:hide', $coursecontext) &&
              !has_capability('moodle/grade:hide', $coursecontext)) {
              throw new moodle_exception('nopermissiontoviewgrades', 'error', '', null,
                  'moodle/grade:hide required to hide grade items');
          }
          if ($editinggrades && !has_capability('moodle/grade:edit', $coursecontext)) {
              throw new moodle_exception('nopermissiontoviewgrades', 'error', '', null,
                  'moodle/grade:edit required to edit grades');
          }
  
          return grade_update($params['source'], $params['courseid'], $itemtype,
              $itemmodule, $iteminstance, $itemnumber, $gradestructure, $params['itemdetails']);
      }
code: public static function update_grades($source, $courseid, $component, $activityid,
          $itemnumber, $grades = array(), $itemdetails = array()) {
          global $CFG;
  
          $params = self::validate_parameters(
              self::update_grades_parameters(),
              array(
                  'source' => $source,
                  'courseid' => $courseid,
                  'component' => $component,
                  'activityid' => $activityid,
                  'itemnumber' => $itemnumber,
                  'grades' => $grades,
                  'itemdetails' => $itemdetails
              )
          );
  
          list($itemtype, $itemmodule) = normalize_component($params['component']);
  
          if (! $cm = get_coursemodule_from_id($itemmodule, $activityid)) {
              throw new moodle_exception('invalidcoursemodule');
          }
          $iteminstance = $cm->instance;
  
          $coursecontext = context_course::instance($params['courseid']);
  
          try {
              self::validate_context($coursecontext);
          } catch (Exception $e) {
              $exceptionparam = new stdClass();
              $exceptionparam->message = $e->getMessage();
              $exceptionparam->courseid = $params['courseid'];
              throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
          }
  
          $hidinggrades = false;
          $editinggradeitem = false;
          $editinggrades = false;
  
          $gradestructure = array();
          foreach ($grades as $grade) {
              $editinggrades = true;
              $gradestructure[ $grade['studentid'] ] = array('userid' => $grade['studentid'], 'rawgrade' => $grade['grade']);
          }
          if (!empty($params['itemdetails'])) {
              if (isset($params['itemdetails']['hidden'])) {
                  $hidinggrades = true;
              } else {
                  $editinggradeitem = true;
              }
          }
  
          if ($editinggradeitem && !has_capability('moodle/grade:manage', $coursecontext)) {
              throw new moodle_exception('nopermissiontoviewgrades', 'error', '', null,
                  'moodle/grade:manage required to edit grade information');
          }
          if ($hidinggrades && !has_capability('moodle/grade:hide', $coursecontext) &&
              !has_capability('moodle/grade:hide', $coursecontext)) {
              throw new moodle_exception('nopermissiontoviewgrades', 'error', '', null,
                  'moodle/grade:hide required to hide grade items');
          }
          if ($editinggrades && !has_capability('moodle/grade:edit', $coursecontext)) {
              throw new moodle_exception('nopermissiontoviewgrades', 'error', '', null,
                  'moodle/grade:edit required to edit grades');
          }
  
          return grade_update($params['source'], $params['courseid'], $itemtype,
              $itemmodule, $iteminstance, $itemnumber, $gradestructure, $params['itemdetails']);
      }
code_tokens: public static function update_grades ( $ source , $ courseid , $ component , $ activityid , $ itemnumber , $ grades = array ( ) , $ itemdetails = array ( ) ) { global $ CFG ; $ params = self :: validate_parameters ( self :: update_grades_parameters ( ) , array ( 'source' => $ source , 'courseid' => $ courseid , 'component' => $ component , 'activityid' => $ activityid , 'itemnumber' => $ itemnumber , 'grades' => $ grades , 'itemdetails' => $ itemdetails ) ) ; list ( $ itemtype , $ itemmodule ) = normalize_component ( $ params [ 'component' ] ) ; if ( ! $ cm = get_coursemodule_from_id ( $ itemmodule , $ activityid ) ) { throw new moodle_exception ( 'invalidcoursemodule' ) ; } $ iteminstance = $ cm -> instance ; $ coursecontext = context_course :: instance ( $ params [ 'courseid' ] ) ; try { self :: validate_context ( $ coursecontext ) ; } catch ( Exception $ e ) { $ exceptionparam = new stdClass ( ) ; $ exceptionparam -> message = $ e -> getMessage ( ) ; $ exceptionparam -> courseid = $ params [ 'courseid' ] ; throw new moodle_exception ( 'errorcoursecontextnotvalid' , 'webservice' , '' , $ exceptionparam ) ; } $ hidinggrades = false ; $ editinggradeitem = false ; $ editinggrades = false ; $ gradestructure = array ( ) ; foreach ( $ grades as $ grade ) { $ editinggrades = true ; $ gradestructure [ $ grade [ 'studentid' ] ] = array ( 'userid' => $ grade [ 'studentid' ] , 'rawgrade' => $ grade [ 'grade' ] ) ; } if ( ! empty ( $ params [ 'itemdetails' ] ) ) { if ( isset ( $ params [ 'itemdetails' ] [ 'hidden' ] ) ) { $ hidinggrades = true ; } else { $ editinggradeitem = true ; } } if ( $ editinggradeitem && ! has_capability ( 'moodle/grade:manage' , $ coursecontext ) ) { throw new moodle_exception ( 'nopermissiontoviewgrades' , 'error' , '' , null , 'moodle/grade:manage required to edit grade information' ) ; } if ( $ hidinggrades && ! has_capability ( 'moodle/grade:hide' , $ coursecontext ) && ! has_capability ( 'moodle/grade:hide' , $ coursecontext ) ) { throw new moodle_exception ( 'nopermissiontoviewgrades' , 'error' , '' , null , 'moodle/grade:hide required to hide grade items' ) ; } if ( $ editinggrades && ! has_capability ( 'moodle/grade:edit' , $ coursecontext ) ) { throw new moodle_exception ( 'nopermissiontoviewgrades' , 'error' , '' , null , 'moodle/grade:edit required to edit grades' ) ; } return grade_update ( $ params [ 'source' ] , $ params [ 'courseid' ] , $ itemtype , $ itemmodule , $ iteminstance , $ itemnumber , $ gradestructure , $ params [ 'itemdetails' ] ) ; }
docstring: Update a grade item and, optionally, student grades
  
  @param  string $source       The source of the grade update
  @param  int $courseid        The course id
  @param  string $component    Component name
  @param  int $activityid      The activity id
  @param  int $itemnumber      The item number
  @param  array  $grades      Array of grades
  @param  array  $itemdetails Array of item details
  @return int                  A status flag
  @since Moodle 2.7
docstring_tokens: Update a grade item and optionally student grades