MDL-37635 Assign: Prevent errors when viewing a feedback for an assignment with no...
authorDamyon Wiese <damyon@moodle.com>
Fri, 1 Feb 2013 05:20:33 +0000 (13:20 +0800)
committerDamyon Wiese <damyon@moodle.com>
Fri, 1 Feb 2013 05:46:08 +0000 (13:46 +0800)
Also adds unit tests to verify that:
Feedback is visible even if there is no grade
The grade is not shown if the gradeitem is hidden unless you have mod/assign:grade capability

mod/assign/locallib.php
mod/assign/renderer.php
mod/assign/tests/locallib_test.php

index b418500..c3df09c 100644 (file)
@@ -1806,7 +1806,8 @@ class assign {
                 }
             }
             $grading_info = grade_get_grades($course->id, 'mod', 'assign', $cm->instance, $USER->id);
-            if (isset($grading_info->items[0]) && !$grading_info->items[0]->grades[$USER->id]->hidden ) {
+            if (isset($grading_info->items[0]->grades[$USER->id]) &&
+                    !$grading_info->items[0]->grades[$USER->id]->hidden ) {
                 $grade = $grading_info->items[0]->grades[$USER->id]->str_grade;
             } else {
                 $grade = '-';
@@ -3040,8 +3041,12 @@ class assign {
                                         $instance->id,
                                         $user->id);
 
-            $gradingitem = $gradinginfo->items[0];
-            $gradebookgrade = $gradingitem->grades[$user->id];
+            $gradingitem = null;
+            $gradebookgrade = null;
+            if (isset($gradinginfo->items[0])) {
+                $gradingitem = $gradinginfo->items[0];
+                $gradebookgrade = $gradingitem->grades[$user->id];
+            }
 
             // Check to see if all feedback plugins are empty.
             $emptyplugins = true;
@@ -3055,25 +3060,34 @@ class assign {
                 }
             }
 
-            if (!($gradebookgrade->hidden) && ($gradebookgrade->grade !== null || !$emptyplugins)) {
+            $cangrade = has_capability('mod/assign:grade', $this->get_context());
+            // If there is feedback or a visible grade, show the summary.
+            if ((!empty($gradebookgrade->grade) && ($cangrade || !$gradebookgrade->hidden)) ||
+                    !$emptyplugins) {
 
-                $gradefordisplay = '';
+                $gradefordisplay = null;
+                $gradeddate = null;
+                $grader = null;
                 $gradingmanager = get_grading_manager($this->get_context(), 'mod_assign', 'submissions');
 
-                if ($controller = $gradingmanager->get_active_controller()) {
-                    $controller->set_grade_range(make_grades_menu($this->get_instance()->grade));
-                    $cangrade = has_capability('mod/assign:grade', $this->get_context());
-                    $gradefordisplay = $controller->render_grade($PAGE,
-                                                                 $grade->id,
-                                                                 $gradingitem,
-                                                                 $gradebookgrade->str_long_grade,
-                                                                 $cangrade);
-                } else {
-                    $gradefordisplay = $this->display_grade($gradebookgrade->grade, false);
+                // Only show the grade if it is not hidden in gradebook.
+                if (!empty($gradebookgrade->grade) && ($cangrade || !$gradebookgrade->hidden)) {
+                    if ($controller = $gradingmanager->get_active_controller()) {
+                        $controller->set_grade_range(make_grades_menu($this->get_instance()->grade));
+                        $gradefordisplay = $controller->render_grade($PAGE,
+                                                                     $grade->id,
+                                                                     $gradingitem,
+                                                                     $gradebookgrade->str_long_grade,
+                                                                     $cangrade);
+                    } else {
+                        $gradefordisplay = $this->display_grade($gradebookgrade->grade, false);
+                    }
+                    $gradeddate = $gradebookgrade->dategraded;
+                    if (isset($grade->grader)) {
+                        $grader = $DB->get_record('user', array('id'=>$grade->grader));
+                    }
                 }
 
-                $gradeddate = $gradebookgrade->dategraded;
-                $grader = $DB->get_record('user', array('id'=>$grade->grader));
 
                 $feedbackstatus = new assign_feedback_status($gradefordisplay,
                                                       $gradeddate,
@@ -4401,10 +4415,18 @@ class assign {
         if (has_all_capabilities($capabilitylist, $this->get_course_context())) {
             $urlparams = array('id'=>$this->get_course()->id);
             $url = new moodle_url('/grade/report/grader/index.php', $urlparams);
-            $usergrade = $gradinginfo->items[0]->grades[$userid]->str_grade;
+            $usergrade = '-';
+            if (isset($gradinginfo->items[0]->grades[$userid]->str_grade)) {
+                $usergrade = $gradinginfo->items[0]->grades[$userid]->str_grade;
+            }
             $gradestring = $this->get_renderer()->action_link($url, $usergrade);
         } else {
-            $gradestring = $gradinginfo->items[0]->grades[$userid]->str_grade;
+            $usergrade = '-';
+            if (isset($gradinginfo->items[0]->grades[$userid]) &&
+                    !$grading_info->items[0]->grades[$userid]->hidden) {
+                $usergrade = $gradinginfo->items[0]->grades[$userid]->str_grade;
+            }
+            $gradestring = $usergrade;
         }
         $name = get_string('currentgrade', 'assign') . ':' . $gradestring;
         $mform->addElement('static', 'finalgrade', $name);
index cbf7a56..181e353 100644 (file)
@@ -321,18 +321,20 @@ class mod_assign_renderer extends plugin_renderer_base {
         $t = new html_table();
 
         // Grade.
-        $row = new html_table_row();
-        $cell1 = new html_table_cell(get_string('grade'));
-        $cell2 = new html_table_cell($status->gradefordisplay);
-        $row->cells = array($cell1, $cell2);
-        $t->data[] = $row;
+        if (isset($status->gradefordisplay)) {
+            $row = new html_table_row();
+            $cell1 = new html_table_cell(get_string('grade'));
+            $cell2 = new html_table_cell($status->gradefordisplay);
+            $row->cells = array($cell1, $cell2);
+            $t->data[] = $row;
 
-        // Grade date.
-        $row = new html_table_row();
-        $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
-        $cell2 = new html_table_cell(userdate($status->gradeddate));
-        $row->cells = array($cell1, $cell2);
-        $t->data[] = $row;
+            // Grade date.
+            $row = new html_table_row();
+            $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
+            $cell2 = new html_table_cell(userdate($status->gradeddate));
+            $row->cells = array($cell1, $cell2);
+            $t->data[] = $row;
+        }
 
         if ($status->grader) {
             // Grader.
index a9b8ab2..6f533b8 100644 (file)
@@ -658,6 +658,63 @@ class mod_assign_locallib_testcase extends advanced_testcase {
         }
     }
 
+    public function test_show_student_summary() {
+        global $CFG;
+
+        $this->setUser($this->editingteachers[0]);
+        $assign = $this->create_instance();
+
+        // No feedback should be available because this student has not been graded.
+        $this->setUser($this->students[0]);
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade');
+        // Simulate adding a grade.
+        $this->setUser($this->teachers[0]);
+        $data = new stdClass();
+        $data->grade = '50.0';
+        $assign->testable_apply_grade_to_user($data, $this->students[0]->id);
+
+        // Now we should see the feedback
+        $this->setUser($this->students[0]);
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade');
+
+        // Now hide the grade in gradebook.
+        $this->setUser($this->teachers[0]);
+        require_once($CFG->libdir.'/gradelib.php');
+        $gradeitem = new grade_item(array(
+            'itemtype'      => 'mod',
+            'itemmodule'    => 'assign',
+            'iteminstance'  => $assign->get_instance()->id,
+            'courseid'      => $this->course->id));
+
+        $gradeitem->set_hidden(1, false);
+
+        // No feedback should be available because the grade is hidden.
+        $this->setUser($this->students[0]);
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook');
+
+        // Do the same but add feedback
+        $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1));
+
+        $this->setUser($this->teachers[0]);
+        $grade = $assign->get_user_grade($this->students[0]->id, true);
+        $data = new stdClass();
+        $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce',
+                                         'format'=>FORMAT_MOODLE);
+        $plugin = $assign->get_feedback_plugin_by_type('comments');
+        $plugin->save($grade, $data);
+
+        // Should have feedback but no grade
+        $this->setUser($this->students[0]);
+        $output = $assign->view_student_summary($this->students[0], true);
+        $this->assertNotEquals(false, strpos($output, 'Tomato sauce'), 'Show feedback even if there is no grade');
+        $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.');
+        $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.');
+    }
+
+
 }
 
 /**