示例#1
0
 /**
  * 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
  */
 public static function save_grades($assignmentid, $applytoall = false, $grades)
 {
     global $CFG, $USER;
     require_once "{$CFG->dirroot}/mod/assign/locallib.php";
     $params = self::validate_parameters(self::save_grades_parameters(), array('assignmentid' => $assignmentid, 'applytoall' => $applytoall, 'grades' => $grades));
     $cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     $assignment = new assign($context, $cm, null);
     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;
 }
示例#2
0
    /**
     * Save a student grade for a single assignment.
     *
     * @param int $assignmentid The id of the assignment
     * @param int $userid The id of the user
     * @param float $grade The grade
     * @param int $attemptnumber The attempt number
     * @param bool $addattempt Allow another attempt
     * @param string $workflowstate New workflow state
     * @param bool $applytoall Apply the grade to all members of the group
     * @param array $plugindata Custom data used by plugins
     * @return null
     * @since Moodle 2.6
     */
    public static function save_grade($assignmentid,
                                      $userid,
                                      $grade,
                                      $attemptnumber,
                                      $addattempt,
                                      $workflowstate,
                                      $applytoall,
                                      $plugindata) {
        global $CFG, $USER;
        require_once("$CFG->dirroot/mod/assign/locallib.php");

        $params = self::validate_parameters(self::save_grade_parameters(),
                                            array('assignmentid' => $assignmentid,
                                                  'userid' => $userid,
                                                  'grade' => $grade,
                                                  'attemptnumber' => $attemptnumber,
                                                  'workflowstate' => $workflowstate,
                                                  'addattempt' => $addattempt,
                                                  'applytoall' => $applytoall,
                                                  'plugindata' => $plugindata));

        $cm = get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
        $context = context_module::instance($cm->id);

        $assignment = new assign($context, $cm, null);

        $gradedata = (object)$plugindata;

        $gradedata->addattempt = $addattempt;
        $gradedata->attemptnumber = $attemptnumber;
        $gradedata->workflowstate = $workflowstate;
        $gradedata->applytoall = $applytoall;
        $gradedata->grade = $grade;

        $assignment->save_grade($userid, $gradedata);

        return null;
    }