Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-29501 Gradebook should not show links to activities that aren't a…

…vailable to user
  • Loading branch information...
commit d33c06621a61a3f63e3a92e50f545adb05e9de9a 1 parent 75371a7
sam marshall authored October 10, 2011

Showing 1 changed file with 54 additions and 18 deletions. Show diff stats Hide diff stats

  1. 72  grade/lib.php
72  grade/lib.php
@@ -1003,6 +1003,14 @@ class grade_structure {
1003 1003
     public $courseid;
1004 1004
 
1005 1005
     /**
  1006
+    * Reference to modinfo for current course (for performance, to save
  1007
+    * retrieving it from courseid every time). Not actually set except for
  1008
+    * the grade_tree type.
  1009
+    * @var course_modinfo
  1010
+    */
  1011
+    public $modinfo;
  1012
+
  1013
+    /**
1006 1014
      * 1D array of grade items only
1007 1015
      */
1008 1016
     public $items;
@@ -1104,8 +1112,6 @@ public function get_element_icon(&$element, $spacerifnone=false) {
1104 1112
      * @return string header
1105 1113
      */
1106 1114
     public function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) {
1107  
-        global $CFG;
1108  
-
1109 1115
         $header = '';
1110 1116
 
1111 1117
         if ($icon) {
@@ -1119,31 +1125,54 @@ public function get_element_header(&$element, $withlink=false, $icon=true, $spac
1119 1125
             return $header;
1120 1126
         }
1121 1127
 
1122  
-        $itemtype     = $element['object']->itemtype;
1123  
-        $itemmodule   = $element['object']->itemmodule;
1124  
-        $iteminstance = $element['object']->iteminstance;
1125  
-
1126  
-        if ($withlink and $itemtype=='mod' and $iteminstance and $itemmodule) {
1127  
-            if ($cm = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->courseid)) {
1128  
-
  1128
+        if ($withlink) {
  1129
+            $url = $this->get_activity_link($element);
  1130
+            if ($url) {
1129 1131
                 $a = new stdClass();
1130 1132
                 $a->name = get_string('modulename', $element['object']->itemmodule);
1131 1133
                 $title = get_string('linktoactivity', 'grades', $a);
1132  
-                $dir = $CFG->dirroot.'/mod/'.$itemmodule;
1133 1134
 
1134  
-                if (file_exists($dir.'/grade.php')) {
1135  
-                    $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/grade.php?id='.$cm->id;
1136  
-                } else {
1137  
-                    $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/view.php?id='.$cm->id;
1138  
-                }
1139  
-
1140  
-                $header = '<a href="'.$url.'" title="'.s($title).'">'.$header.'</a>';
  1135
+                $header = html_writer::link($url, $header, array('title' => $title));
1141 1136
             }
1142 1137
         }
1143 1138
 
1144 1139
         return $header;
1145 1140
     }
1146 1141
 
  1142
+    private function get_activity_link($element) {
  1143
+        global $CFG;
  1144
+
  1145
+        $itemtype = $element['object']->itemtype;
  1146
+        $itemmodule = $element['object']->itemmodule;
  1147
+        $iteminstance = $element['object']->iteminstance;
  1148
+
  1149
+        // Links only for module items that have valid instance, module and are
  1150
+        // called from grade_tree with valid modinfo
  1151
+        if ($itemtype != 'mod' || !$iteminstance || !$itemmodule || !$this->modinfo) {
  1152
+            return null;
  1153
+        }
  1154
+
  1155
+        // Get $cm efficiently and with visibility information using modinfo
  1156
+        $instances = $this->modinfo->get_instances();
  1157
+        if (empty($instances[$itemmodule][$iteminstance])) {
  1158
+            return null;
  1159
+        }
  1160
+        $cm = $instances[$itemmodule][$iteminstance];
  1161
+
  1162
+        // Do not add link if activity is not visible to the current user
  1163
+        if (!$cm->uservisible) {
  1164
+            return null;
  1165
+        }
  1166
+
  1167
+        // If module has grade.php, link to that, otherwise view.php
  1168
+        $dir = $CFG->dirroot . '/mod/' . $itemmodule;
  1169
+        if (file_exists($dir.'/grade.php')) {
  1170
+            return new moodle_url('/mod/' . $itemmodule . '/grade.php', array('id' => $cm->id));
  1171
+        } else {
  1172
+            return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id));
  1173
+        }
  1174
+    }
  1175
+
1147 1176
     /**
1148 1177
      * Returns the grade eid - the grade may not exist yet.
1149 1178
      *
@@ -1600,12 +1629,19 @@ class grade_tree extends grade_structure {
1600 1629
      */
1601 1630
     public function grade_tree($courseid, $fillers=true, $category_grade_last=false,
1602 1631
                                $collapsed=null, $nooutcomes=false) {
1603  
-        global $USER, $CFG;
  1632
+        global $USER, $CFG, $COURSE, $DB;
1604 1633
 
1605 1634
         $this->courseid   = $courseid;
1606 1635
         $this->levels     = array();
1607 1636
         $this->context    = get_context_instance(CONTEXT_COURSE, $courseid);
1608 1637
 
  1638
+        if (!empty($COURSE->id) && $COURSE->id == $this->courseid) {
  1639
+            $course = $COURSE;
  1640
+        } else {
  1641
+            $course = $DB->get_record('course', array('id' => $this->courseid));
  1642
+        }
  1643
+        $this->modinfo = get_fast_modinfo($course);
  1644
+
1609 1645
         // get course grade tree
1610 1646
         $this->top_element = grade_category::fetch_course_tree($courseid, true);
1611 1647
 

0 notes on commit d33c066

Please sign in to comment.
Something went wrong with that request. Please try again.