Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'wip-MDL-55107-master' of git://github.com/abgreeve/moodle
  • Loading branch information
David Monllao committed Jul 12, 2016
2 parents b60d4fc + 97a85bf commit 64c1e70
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 56 deletions.
68 changes: 41 additions & 27 deletions lib/db/upgradelib.php
Expand Up @@ -383,8 +383,23 @@ function upgrade_course_letter_boundary($courseid = null) {
$systemcontext = context_system::instance();
$systemneedsfreeze = upgrade_letter_boundary_needs_freeze($systemcontext);

// Check the setting for showing the letter grade in a column (default is false).
$usergradelettercolumnsetting = 0;
if (isset($CFG->grade_report_user_showlettergrade)) {
$usergradelettercolumnsetting = (int)$CFG->grade_report_user_showlettergrade;
}
$lettercolumnsql = '';
if ($usergradelettercolumnsetting) {
// the system default is to show a column with letters (and the course uses the defaults).
$lettercolumnsql = '(gss.value is NULL OR ' . $DB->sql_compare_text('gss.value') . ' <> \'0\')';
} else {
// the course displays a column with letters.
$lettercolumnsql = $DB->sql_compare_text('gss.value') . ' = \'1\'';
}

// 3, 13, 23, 31, and 32 are the grade display types that incorporate showing letters. See lib/grade/constants/php.
$systemletters = (isset($CFG->grade_displaytype) && in_array($CFG->grade_displaytype, array(3, 13, 23, 31, 32)));
$systemusesletters = (int) (isset($CFG->grade_displaytype) && in_array($CFG->grade_displaytype, array(3, 13, 23, 31, 32)));
$systemletters = $systemusesletters || $usergradelettercolumnsetting;

$contextselect = context_helper::get_preload_record_columns_sql('ctx');

Expand All @@ -397,40 +412,39 @@ function upgrade_course_letter_boundary($courseid = null) {
JOIN {grade_items} gi ON c.id = gi.courseid
JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
LEFT JOIN {grade_settings} gs ON gs.courseid = c.id AND gs.name = 'displaytype'
LEFT JOIN {grade_settings} gss ON gss.courseid = c.id AND gss.name = 'report_user_showlettergrade'
LEFT JOIN {grade_letters} gl ON gl.contextid = ctx.id
WHERE gi.display = 0 AND (gs.value is NULL)
WHERE gi.display = 0
AND ((gs.value is NULL)
AND ($lettercolumnsql))
AND gl.id is NULL $coursesql";
$affectedcourseids = $DB->get_recordset_sql($sql, $params);
foreach ($affectedcourseids as $courseid) {
set_config('gradebook_calculations_freeze_' . $courseid->courseid, 20160518);
}
$affectedcourseids->close();

}

if ($systemletters || $systemneedsfreeze) {

// If the system letter boundary is okay proceed to check grade item and course grade display settings.
$sql = "SELECT DISTINCT c.id AS courseid, $contextselect
FROM {course} c
JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
JOIN {grade_items} gi ON c.id = gi.courseid
LEFT JOIN {grade_settings} gs ON c.id = gs.courseid AND gs.name = 'displaytype'
LEFT JOIN {grade_letters} gl ON gl.contextid = ctx.id
WHERE gi.display IN (3, 13, 23, 31, 32)
OR (" . $DB->sql_compare_text('gs.value') . " IN ('3', '13', '23', '31', '32'))
OR gl.id is NOT NULL
$coursesql";
} else {

// There is no site setting for letter grades. Just check the modified letter boundaries.
$sql = "SELECT DISTINCT c.id AS courseid, $contextselect
FROM {grade_letters} l, {course} c
JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
WHERE l.contextid = ctx.id
AND ctx.instanceid = c.id
$coursesql";
}
// If the system letter boundary is okay proceed to check grade item and course grade display settings.
$sql = "SELECT DISTINCT c.id AS courseid, $contextselect
FROM {course} c
JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
JOIN {grade_items} gi ON c.id = gi.courseid
LEFT JOIN {grade_settings} gs ON c.id = gs.courseid AND gs.name = 'displaytype'
LEFT JOIN {grade_settings} gss ON gss.courseid = c.id AND gss.name = 'report_user_showlettergrade'
WHERE
(
-- A grade item is using letters
(gi.display IN (3, 13, 23, 31, 32))
-- OR the course is using letters
OR (" . $DB->sql_compare_text('gs.value') . " IN ('3', '13', '23', '31', '32')
-- OR the course using the system default which is letters
OR (gs.value IS NULL AND $systemusesletters = 1)
)
OR ($lettercolumnsql)
)
-- AND the course matches
$coursesql";

$potentialcourses = $DB->get_recordset_sql($sql, $params);

Expand Down Expand Up @@ -475,7 +489,7 @@ function upgrade_letter_boundary_needs_freeze($context) {
if (!empty($letters)) {
foreach ($letters as $boundary => $notused) {
$standardisedboundary = upgrade_standardise_score($boundary, 0, 100, 0, 100);
if ($boundary != $standardisedboundary) {
if ($standardisedboundary < $boundary) {
return true;
}
}
Expand Down
95 changes: 66 additions & 29 deletions lib/tests/upgradelib_test.php
Expand Up @@ -591,7 +591,7 @@ public function test_upgrade_course_letter_boundary() {
// Create some courses.
$courses = array();
$contexts = array();
for ($i = 0; $i < 37; $i++) {
for ($i = 0; $i < 45; $i++) {
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
if (in_array($i, array(2, 5, 10, 13, 14, 19, 23, 25, 30, 34, 36))) {
Expand All @@ -603,38 +603,44 @@ public function test_upgrade_course_letter_boundary() {
$this->assign_bad_letter_boundary($context->id);
}

if (in_array($i, array(9, 10, 11, 18, 19, 20, 29, 30, 31))) {
if (in_array($i, array(3, 9, 10, 11, 18, 19, 20, 29, 30, 31, 40))) {
grade_set_setting($course->id, 'displaytype', '3');
} else if (in_array($i, array(8, 17, 28))) {
grade_set_setting($course->id, 'displaytype', '2');
}

if ($i >= 7) {
$assignrow = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'name' => 'Test!'));
$gi = grade_item::fetch(
array('itemtype' => 'mod',
'itemmodule' => 'assign',
'iteminstance' => $assignrow->id,
'courseid' => $course->id));
if (in_array($i, array(13, 14, 15, 23, 24, 34, 35, 36))) {
grade_item::set_properties($gi, array('display', 3));
$gi->update();
} else if (in_array($i, array(12, 21, 32))) {
grade_item::set_properties($gi, array('display', 2));
$gi->update();
}
$gradegrade = new grade_grade();
$gradegrade->itemid = $gi->id;
$gradegrade->userid = $user->id;
$gradegrade->rawgrade = 55.5563;
$gradegrade->finalgrade = 55.5563;
$gradegrade->rawgrademax = 100;
$gradegrade->rawgrademin = 0;
$gradegrade->timecreated = time();
$gradegrade->timemodified = time();
$gradegrade->insert();
if (in_array($i, array(37, 43))) {
// Show.
grade_set_setting($course->id, 'report_user_showlettergrade', '1');
} else if (in_array($i, array(38, 42))) {
// Hide.
grade_set_setting($course->id, 'report_user_showlettergrade', '0');
}

$assignrow = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'name' => 'Test!'));
$gi = grade_item::fetch(
array('itemtype' => 'mod',
'itemmodule' => 'assign',
'iteminstance' => $assignrow->id,
'courseid' => $course->id));
if (in_array($i, array(6, 13, 14, 15, 23, 24, 34, 35, 36, 41))) {
grade_item::set_properties($gi, array('display' => 3));
$gi->update();
} else if (in_array($i, array(12, 21, 32))) {
grade_item::set_properties($gi, array('display' => 2));
$gi->update();
}
$gradegrade = new grade_grade();
$gradegrade->itemid = $gi->id;
$gradegrade->userid = $user->id;
$gradegrade->rawgrade = 55.5563;
$gradegrade->finalgrade = 55.5563;
$gradegrade->rawgrademax = 100;
$gradegrade->rawgrademin = 0;
$gradegrade->timecreated = time();
$gradegrade->timemodified = time();
$gradegrade->insert();

$contexts[] = $context;
$courses[] = $course;
}
Expand All @@ -659,7 +665,7 @@ public function test_upgrade_course_letter_boundary() {

// System setting for grade letter boundaries (default).
set_config('grade_displaytype', '3');
for ($i = 0; $i < 37; $i++) {
for ($i = 0; $i < 45; $i++) {
unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
}
upgrade_course_letter_boundary();
Expand All @@ -686,7 +692,7 @@ public function test_upgrade_course_letter_boundary() {
// System setting for grade letter boundaries (custom with problem).
$systemcontext = context_system::instance();
$this->assign_bad_letter_boundary($systemcontext->id);
for ($i = 0; $i < 37; $i++) {
for ($i = 0; $i < 45; $i++) {
unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
}
upgrade_course_letter_boundary();
Expand Down Expand Up @@ -716,7 +722,7 @@ public function test_upgrade_course_letter_boundary() {

// System setting not showing letters.
set_config('grade_displaytype', '2');
for ($i = 0; $i < 37; $i++) {
for ($i = 0; $i < 45; $i++) {
unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
}
upgrade_course_letter_boundary();
Expand All @@ -741,6 +747,37 @@ public function test_upgrade_course_letter_boundary() {
$this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[35]->id});
// [36] A course with grade display settings of letters with modified and good boundary (not 57) Should not be frozen.
$this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[36]->id}));

// Previous site conditions still exist.
for ($i = 0; $i < 45; $i++) {
unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
}
upgrade_course_letter_boundary();

// [37] Site setting for not showing the letter column and course setting set to show (frozen).
$this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[37]->id});
// [38] Site setting for not showing the letter column and course setting set to hide.
$this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[38]->id}));
// [39] Site setting for not showing the letter column and course setting set to default.
$this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[39]->id}));
// [40] Site setting for not showing the letter column and course setting set to default. Course display set to letters (frozen).
$this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[40]->id});
// [41] Site setting for not showing the letter column and course setting set to default. Grade item display set to letters (frozen).
$this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[41]->id});

// Previous site conditions still exist.
for ($i = 0; $i < 45; $i++) {
unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
}
set_config('grade_report_user_showlettergrade', '1');
upgrade_course_letter_boundary();

// [42] Site setting for showing the letter column, but course setting set to hide.
$this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[42]->id}));
// [43] Site setting for showing the letter column and course setting set to show (frozen).
$this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[43]->id});
// [44] Site setting for showing the letter column and course setting set to default (frozen).
$this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[44]->id});
}

/**
Expand Down

0 comments on commit 64c1e70

Please sign in to comment.