Merge branch 'MDL-48861-master' of git://github.com/damyon/moodle
authorDan Poltawski <dan@moodle.com>
Mon, 12 Oct 2015 15:53:18 +0000 (16:53 +0100)
committerDan Poltawski <dan@moodle.com>
Mon, 12 Oct 2015 15:53:18 +0000 (16:53 +0100)
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)';
  
          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
 +     */
 +    protected 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.
          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) {
              }
              $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);
              }