Skip to content

Commit

Permalink
MDL-36255 core_grade: fix to ensure correct context used for filters
Browse files Browse the repository at this point in the history
Some grade object (outcomes,scales) can be created at site or course
context, so this patch just makes sure we use the respective context
when applying format_string to the name in the get_name() function.
  • Loading branch information
snake committed Aug 22, 2018
1 parent d5bf22e commit 7e93539
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
7 changes: 4 additions & 3 deletions grade/edit/scale/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
'sesskey' => sesskey(),
'deleteconfirmed'=> 1));

echo $OUTPUT->confirm(get_string('scaleconfirmdelete', 'grades', $scale->name), $confirmurl, "index.php?id={$courseid}");
echo $OUTPUT->confirm(get_string('scaleconfirmdelete', 'grades', $scale->get_name()), $confirmurl,
"index.php?id={$courseid}");
echo $OUTPUT->footer();
die;
} else {
Expand All @@ -115,7 +116,7 @@
$data = array();
foreach($scales as $scale) {
$line = array();
$line[] = format_string($scale->name).'<div class="scale_options">'.str_replace(",",", ",$scale->scale).'</div>';
$line[] = $scale->get_name() .'<div class="scale_options">'.str_replace(",", ", ", $scale->scale).'</div>';

$used = $scale->is_used();
$line[] = $used ? get_string('yes') : get_string('no');
Expand All @@ -141,7 +142,7 @@
$data = array();
foreach($scales as $scale) {
$line = array();
$line[] = format_string($scale->name).'<div class="scale_options">'.str_replace(",",", ",$scale->scale).'</div>';
$line[] = $scale->get_name().'<div class="scale_options">'.str_replace(",", ", ", $scale->scale).'</div>';

$used = $scale->is_used();
$line[] = $used ? get_string('yes') : get_string('no');
Expand Down
10 changes: 6 additions & 4 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,16 +1172,18 @@ function get_my_remotehosts() {
function get_scales_menu($courseid=0) {
global $DB;

$sql = "SELECT id, name
$sql = "SELECT id, name, courseid
FROM {scale}
WHERE courseid = 0 or courseid = ?
ORDER BY courseid ASC, name ASC";
$params = array($courseid);
$scales = array();
$results = $DB->get_records_sql_menu($sql, $params);
foreach ($results as $i => $scalename) {
$scales[$i] = format_string($scalename, false, array("context" => context_course::instance($courseid)));
$results = $DB->get_records_sql($sql, $params);
foreach ($results as $index => $record) {
$context = empty($record->courseid) ? context_system::instance() : context_course::instance($record->courseid);
$scales[$index] = format_string($record->name, false, ["context" => $context]);
}
// Format: [id => 'scale name'].
return $scales;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/grade/grade_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,8 @@ public function get_name() {
return format_string($course->fullname, false, array("context" => context_course::instance($this->courseid)));

} else {
// Grade categories can't be set up at system context (unlike scales and outcomes)
// We therefore must have a courseid, and don't need to handle system contexts when filtering.
return format_string($this->fullname, false, array("context" => context_course::instance($this->courseid)));
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/grade/grade_outcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ public static function fetch_all_available($courseid) {
* @return string name
*/
public function get_name() {
return format_string($this->fullname, false, array("context" => context_course::instance($this->courseid)));
// Grade outcomes can be created at site or course context, so set the filter context appropriately.
$context = empty($this->courseid) ? context_system::instance() : context_course::instance($this->courseid);
return format_string($this->fullname, false, ["context" => $context]);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/grade/grade_scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ public function delete($source=null) {
* @return string name
*/
public function get_name() {
return format_string($this->name);
// Grade scales can be created at site or course context, so set the filter context appropriately.
$context = empty($this->courseid) ? context_system::instance() : context_course::instance($this->courseid);
return format_string($this->name, false, ['context' => $context]);
}

/**
Expand Down

0 comments on commit 7e93539

Please sign in to comment.