Merge branch 'MDL-51628-master' of git://github.com/damyon/moodle
authorDan Poltawski <dan@moodle.com>
Mon, 9 Nov 2015 11:01:21 +0000 (11:01 +0000)
committerDan Poltawski <dan@moodle.com>
Mon, 9 Nov 2015 11:01:21 +0000 (11:01 +0000)
1  2 
mod/assign/locallib.php

diff --combined mod/assign/locallib.php
@@@ -146,9 -146,6 +146,9 @@@ class assign 
      /** @var array cached list of user groups when team submissions are enabled. The cache key will be the user. */
      private $usersubmissiongroups = array();
  
 +    /** @var array cached list of user groups. The cache key will be the user. */
 +    private $usergroups = array();
 +
      /**
       * Constructor for the base assign class.
       *
                          s.assignment = :assignid AND
                          s.timemodified IS NOT NULL AND
                          s.status = :submitted AND
 -                        (s.timemodified > g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL)';
 +                        (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL)';
  
          return $DB->count_records_sql($sql, $params);
      }
              return $this->usersubmissiongroups[$userid];
          }
  
 -        $grouping = $this->get_instance()->teamsubmissiongroupingid;
 -        $groups = groups_get_all_groups($this->get_course()->id, $userid, $grouping);
 +        $groups = $this->get_all_groups($userid);
          if (count($groups) != 1) {
              $return = false;
          } else {
          return $return;
      }
  
 +    /**
 +     * Gets all groups the user is a member of.
 +     *
 +     * @param int $userid Teh id of the user who's groups we are checking
 +     * @return array The group objects
 +     */
 +    public function get_all_groups($userid) {
 +        if (isset($this->usergroups[$userid])) {
 +            return $this->usergroups[$userid];
 +        }
 +
 +        $grouping = $this->get_instance()->teamsubmissiongroupingid;
 +        $return = groups_get_all_groups($this->get_course()->id, $userid, $grouping);
 +
 +        $this->usergroups[$userid] = $return;
 +
 +        return $return;
 +    }
 +
  
      /**
       * Display the submission that is used by a plugin.
          // More efficient to load this here.
          require_once($CFG->libdir.'/filelib.php');
  
 +        // Increase the server timeout to handle the creation and sending of large zip files.
 +        core_php_time_limit::raise();
 +
          $this->require_view_grades();
  
          // Load all users with submit.
          if (!$userid) {
              $userid = $USER->id;
          }
 +        $submission = null;
  
          $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid);
 -        if ($attemptnumber < 0) {
 +        if ($attemptnumber < 0 || $create) {
              // Make sure this grade matches the latest submission attempt.
              if ($this->get_instance()->teamsubmission) {
                  $submission = $this->get_group_submission($userid, 0, true);
              $grade->assignment   = $this->get_instance()->id;
              $grade->userid       = $userid;
              $grade->timecreated = time();
 -            $grade->timemodified = $grade->timecreated;
 +            // If we are "auto-creating" a grade - and there is a submission
 +            // the new grade should not have a more recent timemodified value
 +            // than the submission.
 +            if ($submission) {
 +                $grade->timemodified = $submission->timemodified;
 +            } else {
 +                $grade->timemodified = $grade->timecreated;
 +            }
              $grade->grade = -1;
              $grade->grader = $USER->id;
              if ($attemptnumber >= 0) {
          if ($rownum == count($useridlist) - 1) {
              $last = true;
          }
 +        // This variation on the url will link direct to this student, with no next/previous links.
 +        // The benefit is the url will be the same every time for this student, so Atto autosave drafts can match up.
 +        $returnparams = array('userid' => $userid, 'rownum' => 0, 'useridlistid' => 0);
 +        $this->register_return_link('grade', $returnparams);
 +
          $user = $DB->get_record('user', array('id' => $userid));
          if ($user) {
              $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
              }
              $showedit = $this->submissions_open($userid) && ($this->is_any_submission_plugin_enabled());
              $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
 +            $usergroups = $this->get_all_groups($user->id);
  
              $submissionstatus = new assign_submission_status($instance->allowsubmissionsfromdate,
                                                               $instance->alwaysshowdescription,
                                                               $instance->attemptreopenmethod,
                                                               $instance->maxattempts,
                                                               $this->get_grading_status($userid),
 -                                                             $instance->preventsubmissionnotingroup);
 +                                                             $instance->preventsubmissionnotingroup,
 +                                                             $usergroups);
              $o .= $this->get_renderer()->render($submissionstatus);
          }
  
              $viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_course_context());
  
              $gradingstatus = $this->get_grading_status($user->id);
 +            $usergroups = $this->get_all_groups($user->id);
              $submissionstatus = new assign_submission_status($instance->allowsubmissionsfromdate,
                                                                $instance->alwaysshowdescription,
                                                                $submission,
                                                                $instance->attemptreopenmethod,
                                                                $instance->maxattempts,
                                                                $gradingstatus,
 -                                                              $instance->preventsubmissionnotingroup);
 +                                                              $instance->preventsubmissionnotingroup,
 +                                                              $usergroups);
              if (has_capability('mod/assign:submit', $this->get_context(), $user)) {
                  $o .= $this->get_renderer()->render($submissionstatus);
              }
          require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php');
  
          // Need submit permission to submit an assignment.
-         require_capability('mod/assign:grade', $this->context);
+         $this->require_view_grades();
          require_sesskey();
  
          // Is advanced grading enabled?
          global $USER, $CFG, $DB;
  
          $grade = $this->get_user_grade($userid, true, $attemptnumber);
 +        $originalgrade = $grade->grade;
          $gradingdisabled = $this->grading_disabled($userid);
          $gradinginstance = $this->get_grading_instance($userid, $grade, $gradingdisabled);
          if (!$gradingdisabled) {
                  }
              }
          }
 -        $this->update_grade($grade, !empty($formdata->addattempt));
 +        // We do not want to update the timemodified if no grade was added.
 +        if (!empty($formdata->addattempt) ||
 +                ($originalgrade !== null && $originalgrade != -1) ||
 +                ($grade->grade !== null && $grade->grade != -1)) {
 +            $this->update_grade($grade, !empty($formdata->addattempt));
 +        }
          // Note the default if not provided for this option is true (e.g. webservices).
          // This is for backwards compatibility.
          if (!isset($formdata->sendstudentnotifications) || $formdata->sendstudentnotifications) {