Skip to content

Commit

Permalink
Merge branch 'MDL-52503_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Jan 5, 2016
2 parents 3348fa7 + 1a51661 commit 3772736
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
43 changes: 24 additions & 19 deletions mod/assign/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class assign {
/** @var bool whether to exclude users with inactive enrolment */
private $showonlyactiveenrol = null;

/** @var string A key used to identify cached userlists created by this object. */
/** @var string A key used to identify userlists created by this object. */
private $useridlistid = null;

/** @var array cached list of participants for this assignment. The cache key will be group, showactive and the context id */
Expand Down Expand Up @@ -170,6 +170,8 @@ class assign {
* otherwise this class will load one from the context as required.
*/
public function __construct($coursemodulecontext, $coursemodule, $course) {
global $SESSION;

$this->context = $coursemodulecontext;
$this->course = $course;

Expand All @@ -184,6 +186,10 @@ public function __construct($coursemodulecontext, $coursemodule, $course) {

// Extra entropy is required for uniqid() to work on cygwin.
$this->useridlistid = clean_param(uniqid('', true), PARAM_ALPHANUM);

if (!isset($SESSION->mod_assign_useridlist)) {
$SESSION->mod_assign_useridlist = [];
}
}

/**
Expand Down Expand Up @@ -3031,7 +3037,7 @@ protected function get_grade($gradeid) {
* @return string
*/
protected function view_single_grade_page($mform) {
global $DB, $CFG;
global $DB, $CFG, $SESSION;

$o = '';
$instance = $this->get_instance();
Expand All @@ -3054,12 +3060,12 @@ protected function view_single_grade_page($mform) {
$userid = optional_param('userid', 0, PARAM_INT);
$attemptnumber = optional_param('attemptnumber', -1, PARAM_INT);

$cache = cache::make_from_params(cache_store::MODE_SESSION, 'mod_assign', 'useridlist');
if (!$userid) {
if (!$useridlist = $cache->get($this->get_useridlist_key($useridlistid))) {
$useridlist = $this->get_grading_userid_list();
$useridlistkey = $this->get_useridlist_key($useridlistid);
if (empty($SESSION->mod_assign_useridlist[$useridlistkey])) {
$SESSION->mod_assign_useridlist[$useridlistkey] = $this->get_grading_userid_list();
}
$cache->set($this->get_useridlist_key($useridlistid), $useridlist);
$useridlist = $SESSION->mod_assign_useridlist[$useridlistkey];
} else {
$rownum = 0;
$useridlist = array($userid);
Expand Down Expand Up @@ -3277,7 +3283,7 @@ protected function view_return_links() {
* @return string
*/
protected function view_grading_table() {
global $USER, $CFG;
global $USER, $CFG, $SESSION;

// Include grading options form.
require_once($CFG->dirroot . '/mod/assign/gradingoptionsform.php');
Expand Down Expand Up @@ -3438,11 +3444,10 @@ protected function view_grading_table() {
}

if ($this->can_grade()) {
// We need to cache the order of uses in the table as the person may wish to grade them.
// We need to store the order of uses in the table as the person may wish to grade them.
// This is done based on the row number of the user.
$cache = cache::make_from_params(cache_store::MODE_SESSION, 'mod_assign', 'useridlist');
$useridlist = $gradingtable->get_column_data('userid');
$cache->set($this->get_useridlist_key(), $useridlist);
$SESSION->mod_assign_useridlist[$this->get_useridlist_key()] = $useridlist;
}

$currentgroup = groups_get_activity_group($this->get_course_module(), true);
Expand Down Expand Up @@ -6121,7 +6126,7 @@ protected function get_grading_instance($userid, $grade, $gradingdisabled) {
* @return void
*/
public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data, $params) {
global $USER, $CFG;
global $USER, $CFG, $SESSION;
$settings = $this->get_instance();

$rownum = $params['rownum'];
Expand All @@ -6130,11 +6135,11 @@ public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data,
$userid = $params['userid'];
$attemptnumber = $params['attemptnumber'];
if (!$userid) {
$cache = cache::make_from_params(cache_store::MODE_SESSION, 'mod_assign', 'useridlist');
if (!$useridlist = $cache->get($this->get_useridlist_key($useridlistid))) {
$useridlist = $this->get_grading_userid_list();
$cache->set($this->get_useridlist_key($useridlistid), $useridlist);
$useridlistkey = $this->get_useridlist_key($useridlistid);
if (empty($SESSION->mod_assign_useridlist[$useridlistkey])) {
$SESSION->mod_assign_useridlist[$useridlistkey] = $this->get_grading_userid_list();
}
$useridlist = $SESSION->mod_assign_useridlist[$useridlistkey];
} else {
$useridlist = array($userid);
$rownum = 0;
Expand Down Expand Up @@ -6976,7 +6981,7 @@ public function save_grade($userid, $data) {
* @return bool - was the grade saved
*/
protected function process_save_grade(&$mform) {
global $CFG;
global $CFG, $SESSION;
// Include grade form.
require_once($CFG->dirroot . '/mod/assign/gradeform.php');

Expand All @@ -6987,14 +6992,14 @@ protected function process_save_grade(&$mform) {
$attemptnumber = optional_param('attemptnumber', -1, PARAM_INT);
$useridlistid = optional_param('useridlistid', $this->get_useridlist_key_id(), PARAM_ALPHANUM);
$userid = optional_param('userid', 0, PARAM_INT);
$cache = cache::make_from_params(cache_store::MODE_SESSION, 'mod_assign', 'useridlist');
if (!$userid) {
if (!$useridlist = $cache->get($this->get_useridlist_key($useridlistid))) {
// If the userid list is not cached we must not save, as it is possible that the user in a
if (empty($SESSION->mod_assign_useridlist[$this->get_useridlist_key($useridlistid)])) {
// If the userid list is not stored we must not save, as it is possible that the user in a
// given row position may not be the same now as when the grading page was generated.
$url = new moodle_url('/mod/assign/view.php', array('id' => $this->get_course_module()->id));
throw new moodle_exception('useridlistnotcached', 'mod_assign', $url);
}
$useridlist = $SESSION->mod_assign_useridlist[$this->get_useridlist_key($useridlistid)];
} else {
$useridlist = array($userid);
$rownum = 0;
Expand Down
25 changes: 0 additions & 25 deletions mod/assign/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2329,30 +2329,5 @@ public function test_get_shared_group_members() {
$this->assertTrue(in_array($this->extrastudents[0]->id, $allgroupmembers));
$this->assertTrue(in_array($this->extrastudents[1]->id , $allgroupmembers));
}

/**
* Test that the useridlist cache will retive the correct values
* when using assign::get_useridlist_key and assign::get_useridlist_key_id.
*/
public function test_useridlist_cache() {
// Create an assignment object, we will use this to test the key generation functions.
$course = self::getDataGenerator()->create_course();
$assign = self::getDataGenerator()->create_module('assign', array('course' => $course->id));
list($courserecord, $cm) = get_course_and_cm_from_instance($assign->id, 'assign');
$context = context_module::instance($cm->id);
$assign = new assign($context, $cm, $courserecord);
// Create the cache.
$cache = cache::make_from_params(cache_store::MODE_SESSION, 'mod_assign', 'useridlist');
// Create an entry that we will insert into the cache.
$entry = array(0 => '5', 1 => '6325', 2 => '67783');
// Insert the value into the cache.
$cache->set($assign->get_useridlist_key(), $entry);
// Now test we can retrive the entry.
$this->assertEquals($entry, $cache->get($assign->get_useridlist_key()));
$useridlistid = clean_param($assign->get_useridlist_key_id(), PARAM_ALPHANUM);
$this->assertEquals($entry, $cache->get($assign->get_useridlist_key($useridlistid)));
// Check it will not retrive anything on an invalid key.
$this->assertFalse($cache->get($assign->get_useridlist_key('notvalid')));
}
}

0 comments on commit 3772736

Please sign in to comment.