From dadc9d991d099cfacfb00241799ca323c01638f6 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 13 Aug 2014 14:23:59 +0800 Subject: [PATCH] MDL-46139 Grades: Add contibution to course total column to user report --- grade/report/user/lib.php | 79 +++++++++++++++++++++++++++++++--- grade/report/user/settings.php | 1 + lang/en/grades.php | 3 ++ lib/grade/grade_grade.php | 9 ++-- 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/grade/report/user/lib.php b/grade/report/user/lib.php index 91ac625e74c..71356040a8b 100644 --- a/grade/report/user/lib.php +++ b/grade/report/user/lib.php @@ -129,6 +129,12 @@ class grade_report_user extends grade_report { */ public $showlettergrade = false; + /** + * Show the calculated contribution to the course total column. + * @var bool + */ + public $showcontributiontocoursetotal = false; + /** * Show average grades in the report, default false. * @var false @@ -191,6 +197,7 @@ class grade_report_user extends grade_report { $this->showrange = grade_get_setting($this->courseid, 'report_user_showrange', !empty($CFG->grade_report_user_showrange)); $this->showfeedback = grade_get_setting($this->courseid, 'report_user_showfeedback', !empty($CFG->grade_report_user_showfeedback)); $this->showweight = grade_get_setting($this->courseid, 'report_user_showweight', !empty($CFG->grade_report_user_showweight)); + $this->showcontributiontocoursetotal = grade_get_setting($this->courseid, 'report_user_showcontributiontocoursetotal', !empty($CFG->grade_report_user_showcontributiontocoursetotal)); $this->showlettergrade = grade_get_setting($this->courseid, 'report_user_showlettergrade', !empty($CFG->grade_report_user_showlettergrade)); $this->showaverage = grade_get_setting($this->courseid, 'report_user_showaverage', !empty($CFG->grade_report_user_showaverage)); @@ -327,6 +334,11 @@ class grade_report_user extends grade_report { $this->tablecolumns[] = 'feedback'; $this->tableheaders[] = $this->get_lang_string('feedback', 'grades'); } + + if ($this->showcontributiontocoursetotal) { + $this->tablecolumns[] = 'contributiontocoursetotal'; + $this->tableheaders[] = $this->get_lang_string('contributiontocoursetotal', 'grades'); + } } function fill_table() { @@ -459,13 +471,13 @@ class grade_report_user extends grade_report { $data['weight']['headers'] = "$header_cat $header_row weight"; // has a weight assigned, might be extra credit - $hints = $grade_grade->get_aggregation_hint($grade_object); - if ($hints) { + $hint = $grade_grade->get_aggregation_hint($grade_object); + if ($hint) { // This obliterates the weight because it provides a more informative description. - if (intval($hints)) { - $hints = format_float(intval($hints) / 100.0, 2) . ' %'; + if (intval($hint)) { + $hint = format_float(intval($hint) / 100.0, 2) . ' %'; } - $data['weight']['content'] = $hints; + $data['weight']['content'] = $hint; } } @@ -594,6 +606,54 @@ class grade_report_user extends grade_report { } $data['feedback']['headers'] = "$header_cat $header_row feedback"; } + // Contribution to the course total column. + if ($this->showcontributiontocoursetotal) { + $data['contributiontocoursetotal']['class'] = $class; + $data['contributiontocoursetotal']['content'] = '-'; + $data['contributiontocoursetotal']['headers'] = "$header_cat $header_row contributiontocoursetotal"; + + if (($type != 'categoryitem') && ($type != 'courseitem')) { + $hint = $grade_grade->get_aggregation_hint($grade_object); + if ($hint && is_numeric($hint)) { + $me = $grade_grade->grade_item; + $percentoftotal = intval($hint) / 10000.0; + $validpercent = true; + $limit = 0; + $parent = null; + while ((!$me->is_course_item()) && ($validpercent)) { + // The parent of a category grade item is itself (yes - how odd). + // This means we need to use the parent of the grade_category if it exists. + if (!empty($parent)) { + $parent = $parent->get_parent_category(); + } else { + $parent = $me->get_parent_category(); + } + $parentgradeitem = $parent->load_grade_item(); + $parentgradegrade = grade_grade::fetch(array('itemid'=>$parentgradeitem->id, 'userid'=>$this->user->id)); + if (!$parentgradegrade) { + $validpercent = false; + continue; + } + $hint = $parentgradegrade->get_aggregation_hint($parentgradeitem); + $me = $parentgradeitem; + if (!is_numeric($hint)) { + // It's OK for the course grade item to not have a usedinaggregation value. + $validpercent = $parentgradeitem->is_course_item(); + continue; + } + $thispercent = intval($hint) / 10000.0; + $percentoftotal *= $thispercent; + $limit++; + if ($limit > 20) { + die(); + } + } + if ($validpercent) { + $data['contributiontocoursetotal']['content'] = format_float($percentoftotal * 100.0, 2) . ' %'; + } + } + } + } } } @@ -909,6 +969,14 @@ function grade_report_user_settings_definition(&$mform) { } $mform->addElement('select', 'report_user_showlettergrade', get_string('showlettergrade', 'grades'), $options); + if (empty($CFG->grade_report_user_showcontributiontocoursetotal)) { + $options[-1] = get_string('defaultprev', 'grades', $options[0]); + } else { + $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showcontributiontocoursetotal]); + } + + $mform->addElement('select', 'report_user_showcontributiontocoursetotal', get_string('showcontributiontocoursetotal', 'grades'), $options); + $mform->addHelpButton('report_user_showcontributiontocoursetotal', 'showcontributiontocoursetotal', 'grades'); if (empty($CFG->grade_report_user_showrange)) { $options[-1] = get_string('defaultprev', 'grades', $options[0]); @@ -952,6 +1020,7 @@ function grade_report_user_settings_definition(&$mform) { $mform->addElement('select', 'report_user_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options); $mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades'); + } /** diff --git a/grade/report/user/settings.php b/grade/report/user/settings.php index d469f68f08a..c85f4c899ab 100644 --- a/grade/report/user/settings.php +++ b/grade/report/user/settings.php @@ -47,4 +47,5 @@ if ($ADMIN->fulltree) { array(GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'), GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'), GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades')))); + $settings->add(new admin_setting_configcheckbox('grade_report_user_showcontributiontocoursetotal', get_string('showcontributiontocoursetotal', 'grades'), get_string('showcontributiontocoursetotal_help', 'grades'), 0)); } diff --git a/lang/en/grades.php b/lang/en/grades.php index 96ac3f50a10..f5e0305392a 100644 --- a/lang/en/grades.php +++ b/lang/en/grades.php @@ -121,6 +121,7 @@ $string['combo'] = 'Tabs and Dropdown menu'; $string['compact'] = 'Compact'; $string['componentcontrolsvisibility'] = 'Whether this grade item is hidden is controlled by the activity settings.'; $string['contract'] = 'Contract category'; +$string['contributiontocoursetotal'] = 'Contribution to course total'; $string['controls'] = 'Controls'; $string['courseavg'] = 'Course average'; $string['coursegradecategory'] = 'Course grade category'; @@ -592,6 +593,8 @@ $string['showanalysisicon_desc'] = 'Whether to show grade analysis icon by defau $string['showanalysisicon_help'] = 'If the activity module supports it, the grade analysis icon links to a page with more detailed explanation of the grade and how it was obtained.'; $string['showaverage'] = 'Show average'; $string['showaverage_help'] = 'Show the average column? Students may be able to estimate other student\'s grades if the average is calculated from a small number of grades. For performance reasons the average is approximate if it is dependent on any hidden items.'; +$string['showcontributiontocoursetotal'] = 'Show contribution to course total'; +$string['showcontributiontocoursetotal_help'] = 'Show an additional column containing the calculated contribution to the course total?'; $string['showfeedback'] = 'Show feedback'; $string['showfeedback_help'] = 'Show the feedback column?'; $string['showgrade'] = 'Show grades'; diff --git a/lib/grade/grade_grade.php b/lib/grade/grade_grade.php index 53ade2e3f0d..70d43d93d70 100644 --- a/lib/grade/grade_grade.php +++ b/lib/grade/grade_grade.php @@ -977,9 +977,12 @@ class grade_grade extends grade_object { $item = $this->grade_item; if (!$item->is_course_item()) { - $parent_category = $item->get_parent_category(); - $parent_category->apply_forced_settings(); - if ($parent_category->is_extracredit_used() && ($item->aggregationcoef > 0)) { + $parentcategory = $item->get_parent_category(); + // This is needed because get_parent_category() does not do the "parent" bit very well. + if ($item->is_category_item()) { + $parentcategory = $parentcategory->load_parent_category(); + } + if ($parentcategory->is_extracredit_used() && ($item->aggregationcoef > 0)) { $hint = get_string('aggregationcoefextra', 'grades'); } }