]> git.mjollnir.org Git - moodle.git/commitdiff
adding capabilities
authortoyomoyo <toyomoyo>
Wed, 6 Sep 2006 08:55:23 +0000 (08:55 +0000)
committertoyomoyo <toyomoyo>
Wed, 6 Sep 2006 08:55:23 +0000 (08:55 +0000)
13 files changed:
course/grades.php
course/report/log/lib.php
grade/lib.php
lib/moodlelib.php
mod/glossary/view.php
mod/resource/lib.php
question/export.php
question/preview.php
rss/file.php
user/edit.php
user/index.php
user/tabs.php
user/view.php

index 9b84b06c187c4307c919ae3b5fe00842ccb16e71..4509bcccee687fb5ba220f6646760b0055a13276 100644 (file)
@@ -14,9 +14,7 @@
         error("Course ID was incorrect");
     }
 
-    if (!isteacher($course->id)) {
-        error("Only teachers can use this page!");
-    }
+    require_capability('moodle/course:viewcoursegrades', get_context_instance(CONTEXT_COURSE, $id));
 
     $strgrades = get_string("grades");
     $strgrade = get_string("grade");
index 90952c679f8804f9ebc37a97ff3515f30b3fa579..3a984c0eb3ee8a1f3eeb81d0eef55b27b9ad5d8a 100644 (file)
@@ -11,10 +11,10 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate='today'
         $showcourses = 1;
     }
    
+    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+   
     /// Setup for group handling.
-    $isteacher = isteacher($course->id);
-    $isteacheredit = isteacheredit($course->id);
-    if ($course->groupmode == SEPARATEGROUPS and !$isteacheredit) {
+    if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
         $selectedgroup = get_current_group($course->id);
         $showgroups = false;
     }
@@ -47,7 +47,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate='today'
     if ($showusers) {
         if ($courseusers) {
             foreach ($courseusers as $courseuser) {
-                $users[$courseuser->id] = fullname($courseuser, $isteacher);
+                $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', $context));
             }
         }
         if ($guest = get_guest()) {
index 5ff0fa809c1c03b42b7c05ea2588eea5c80e0b01..c8a2200848ddbfa834ed510fc4e8965e06779141 100644 (file)
@@ -1003,7 +1003,7 @@ function grade_get_preferences($courseid) {
 
     // Construct some other ones about which fields are shown
 
-    $isteacher = isteacher($courseid);
+    $isteacher = has_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $courseid));
 
     $preferences->show_weighted = (($preferences->display_weighted > 0  && $isteacher) || 
                                    ($preferences->display_weighted > 1 && !$isteacher));
@@ -1063,7 +1063,7 @@ function grade_set_preferences($course, $newprefs) {
 
 function grade_preferences_menu($action, $course, $group=0) {
 
-    if (!isteacher($course->id)) {
+    has_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $courseid)) {
         return;
     }
 
@@ -1133,7 +1133,7 @@ function grade_nav($course, $action='grades') {
     $strgrades = get_string('grades', 'grades');
     $gradenav = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
 
-    if (isteacher($course->id)) {
+    if (has_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $course->id))) {
         switch ($action) {
             case 'prefs':
             case 'set_grade_preferences':
index a6b3b85065342b418e9d5c8f548515e3a3d20843..6d6c32f0675c51ddb7d7604edaff6000fc50264b 100644 (file)
@@ -1535,7 +1535,7 @@ function require_login($courseid=0, $autologinguest=true, $cm=null) {
     // Next, check if the user can be in a particular course
     if ($courseid) {
         if ($courseid == SITEID) { // Anyone can be in the site course
-            if (isset($cm) and !$cm->visible and !isteacher(SITEID)) { // Not allowed to see module, send to course page
+            if (isset($cm) and !$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Not allowed to see module, send to course page
                 redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
             }
             return;
@@ -1552,12 +1552,12 @@ function require_login($courseid=0, $autologinguest=true, $cm=null) {
 
         if (has_capability('moodle/course:view', $context)) {
             if (isset($USER->realuser)) {   // Make sure the REAL person can also access this course
-                if (!isteacher($courseid, $USER->realuser)) {
+                if (!has_capability('moodle/course:view', $context, $USER->realuser)) {
                     print_header();
                     notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/');
                 }
             }
-            if (isset($cm) and !$cm->visible and !isteacher($courseid)) { // Not allowed to see module, send to course page
+            if (isset($cm) and !$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) { // Not allowed to see module, send to course page
                 redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
             }
             return;   // user is a member of this course.
@@ -1950,7 +1950,7 @@ function isediting($courseid, $user=NULL) {
     if (empty($user->editing)) {
         return false;
     }
-    return ($user->editing and isteacher($courseid, $user->id));
+    return ($user->editing and has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $courseid)));
 }
 
 /**
@@ -2972,7 +2972,7 @@ function get_current_group($courseid, $full=false) {
     global $SESSION, $USER;
 
     if (!isset($SESSION->currentgroup[$courseid])) {
-        if (empty($USER->groupmember[$courseid]) or isteacheredit($courseid)) {
+        if (empty($USER->groupmember[$courseid]) or has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $courseid))) {
 
             return 0;
         } else {
@@ -3016,7 +3016,7 @@ function get_and_set_current_group($course, $groupmode, $groupid=-1) {
 
     if ($groupid) {      // Try to change the current group to this groupid
         if ($group = get_record('groups', 'id', $groupid, 'courseid', $course->id)) { // Exists
-            if (isteacheredit($course->id)) {          // Sets current default group
+            if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id))) {          // Sets current default group
                 $currentgroupid = set_current_group($course->id, $group->id);
 
             } else if ($groupmode == VISIBLEGROUPS) {
@@ -3037,7 +3037,7 @@ function get_and_set_current_group($course, $groupmode, $groupid=-1) {
         }
     } else {             // When groupid = 0 it means show ALL groups
         //this is changed, non editting teacher needs access to group 0 as well, for viewing work in visible groups (need to set current group for multiple pages)
-        if (isteacheredit($course->id) OR (isteacher($course->id) AND ($groupmode == VISIBLEGROUPS))) {          // Sets current default group
+        if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)) AND ($groupmode == VISIBLEGROUPS)) {          // Sets current default group
             $currentgroupid = set_current_group($course->id, 0);
 
         } else if ($groupmode == VISIBLEGROUPS) {  // All groups are visible
@@ -3075,7 +3075,7 @@ function setup_and_print_groups($course, $groupmode, $urlroot) {
         return false;
     }
 
-    if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id) and !$currentgroup) {
+    if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)) and !$currentgroup) {
         //we are in separate groups and the current group is group 0, as last set.
         //this can mean that either, this guy has no group
         //or, this guy just came from a visible all forum, and he left when he set his current group to 0 (show all)
@@ -3092,7 +3092,7 @@ function setup_and_print_groups($course, $groupmode, $urlroot) {
         }
     }
 
-    if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
+    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)))) {
         if ($groups = get_records_menu('groups', 'courseid', $course->id, 'name ASC', 'id,name')) {
             echo '<div align="center">';
             print_group_menu($groups, $groupmode, $currentgroup, $urlroot);
@@ -3100,7 +3100,7 @@ function setup_and_print_groups($course, $groupmode, $urlroot) {
         }
     }//added code here to allow non-editting teacher to swap in-between his own groups
     //added code for students in separategrous to swtich groups
-    else if ($groupmode == SEPARATEGROUPS and (isteacher($course->id) or isstudent($course->id))) {
+    else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
         $validgroups = array();
         //get all the groups this guy is in in this course
         if ($p = user_group($course->id,$USER->id)){
index 165a3ec526f5f467c771f1fb7a9e77819b89172f..5d9e2c5dc7afc6b2e6d0e19683ce1ba3599ec4fd 100644 (file)
 
                         $user = get_record("user","id",$entry->uid);
                         print_user_picture($user->id, $course->id, $user->picture);
-                        $pivottoshow = fullname($user, isteacher($course->id));;
+                        $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
                     } else {
                         echo '<td align="center">';
                     }
index 071209f4145bf779cf45aacc3e1ec483d347dcaf..3a5d81abcc3b4bb5c10953ca97a830b0446cf59f 100644 (file)
@@ -112,7 +112,7 @@ function resource_base($cmid=0) {
             $this->navigation = "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strresources</a> ->";
         }
 
-        if (!$this->cm->visible and !isteacher($this->course->id)) {
+        if (!$this->cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $this->cm->id))) {
             $pagetitle = strip_tags($this->course->shortname.': '.$this->strresource);
             print_header($pagetitle, $this->course->fullname, "$this->navigation $this->strresource", "", "", true, '', navmenu($this->course, $this->cm));
             notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
index a6893390cbb4cb8d1464c26560a66f712488302e..7ff74b57a8a795173116add3fcf2e86d7c28390e 100644 (file)
     }
 
     require_login($course->id, false);
-
-    if (!isteacher($course->id)) {
-        error( get_string('onlyteachersexport','quiz') );
-    }
+    
+    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+    require_capability('moodle/question:export', $context);
 
     // ensure the files area exists for this course
     make_upload_directory( "$course->id" );
@@ -59,7 +58,7 @@
     /// Header:
 
     if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
-        $strupdatemodule = isteacheredit($course->id)
+        $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
             ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
             : "";
         print_header_simple($strexportquestions, '',
index 2dd1ec534e621213dc2239739502e8df6382a7eb..f8e628acba0b97206a1ecac25e56c29e6ac6c5be 100644 (file)
@@ -41,6 +41,7 @@
 
     require_login();
 
+    // this might break things in the future
     if (!isteacherinanycourse()) {
         error('This page is for teachers only');
     }
@@ -81,7 +82,7 @@
         error("This question doesn't belong to a valid category!");
     }
 
-    if (!isteacher($category->course) and !$category->publish) {
+    if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_COURSE, $category->course)) and !$category->publish) {
         error("You can't preview these questions!");
     }
     $quiz->course = $category->course;
index ae0a81b405949198bc288661529abdc1d5e91fa0..c1a01ee65bc159f3f78b115ecdb434ad22de8c86 100644 (file)
         rss_not_found();
     }
 
-    $isstudent = isstudent($courseid,$userid);
-    $isteacher = isteacher($courseid,$userid);
-
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    $isuser = has_capability('moodle/course:view', $context, $userid);
+    
     //Check for "security" if !course->guest or course->password
     if ($course->id != SITEID) {
-        if ((!$course->guest || $course->password) && (!($isstudent || $isteacher))) {
+        if ((!$course->guest || $course->password) && (!$isuser)) {
             rss_not_found();
         }
     }
 
     //Check for "security" if the course is hidden or the activity is hidden
-    if (!$isblog and (!$course->visible || !$cm->visible) && (!$isteacher)) {
+    if (!$isblog and (!$course->visible || !$cm->visible) && (!has_capability('moodle/course:viewhiddenactivities', $context))) {
         rss_not_found();
     }
 
index 89fd7cd6f8f80a134f43edd7762ab24c239c084d..da6499eb5e50c440ea929f883eab89e3840ce0af 100644 (file)
         if ($newaccount) {
             $userfullname = $strnewuser;
         } else {
-            $userfullname = fullname($user, isteacher($course->id));
+            $userfullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
         }
         if ($course->category) {
             print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
index 8c9334f21feaaefd6686c0879e4bc0be46044583..16f15484fa5becb0b791b7b6c16070415439e18c 100644 (file)
@@ -58,7 +58,7 @@
     require_capability('moodle/course:viewparticipants', $context);
 
     if (!$course->category) {
-        if (!$CFG->showsiteparticipantslist and !isteacher(SITEID)) {
+        if (!$CFG->showsiteparticipantslist and !has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
             print_header("$course->shortname: ".get_string('participants'), $course->fullname,
                          get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
             notice(get_string('sitepartlist0'));
     }
 
     $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
-                         !isteacheredit($course->id));
+                         !has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)));
 
     if ($isseparategroups and (!$currentgroup) ) {  //XXX
         print_heading(get_string("notingroup", "forum"));
     popup_form($CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&amp;roleid='.$roleid.'&amp;id=',$my_course,'courseform',$course->id);
     echo '</td>';
     
-    if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
+    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)))) {
         if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
             echo '<td class="left">';
             print_group_menu($groups, $groupmode, $currentgroup, $baseurl);
     echo popup_form($baseurl.'&amp;mode=', $formatmenu, 'formatmenu', $fullmode, '', '', '', true);
     echo '</td></tr></table>';
 
-    if ($currentgroup and (!$isseparategroups or isteacheredit($course->id))) {    /// Display info about the group
+    if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)))) {    /// Display info about the group
         if ($group = get_record('groups', 'id', $currentgroup)) {              
             if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) { 
                 echo '<table class="groupinfobox"><tr><td class="left side picture">';
                 print_group_picture($group, $course->id, true, false, false);
                 echo '</td><td class="content">';
                 echo '<h3>'.$group->name;
-                if (isteacheredit($course->id)) {
+                if (has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
                     echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="../course/groups.php?id='.$course->id.'&amp;group='.$group->id.'">';
                     echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt="" border="0">';
                     echo '</a>';
@@ -431,7 +431,7 @@ function checkchecked(form) {
         $a->count = $totalcount;
         $a->items = $currentrole->name;
         echo '<h2>'.get_string('counteditems', '', $a);
-        if (isteacheredit($course->id)) {
+        if (user_can_assign($context, $roleid)) {
             echo ' <a href="'.$CFG->wwwroot.'/admin/roles/assign.php?roleid='.$roleid.'&amp;contextid='.$context->id.'">';
             echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" /></a>';
         }
index 24a496bc0dfc6c81539b379f0d59f6dd1a69deee..0299c007e8ff2d5b9745d890d61b391688259e25 100644 (file)
@@ -85,7 +85,7 @@
         if (isset($userid)) {
             $user = get_record('user','id', $userid);
         }
-        print_heading(fullname($user, isteacher($course->id)));
+        print_heading(fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))));
 
         $toprow[] = new tabobject('profile', $CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id, get_string('profile'));
 
index d25ce02dfae81ff44198f2834e35f1083bdc37a3..6c916939b23e25e37b9939d57fd6a41558186d8c 100644 (file)
@@ -45,7 +45,7 @@
         }
     }
 
-    $fullname = fullname($user, isteacher($course->id));
+    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
     $personalprofile = get_string("personalprofile");
     $participants = get_string("participants");
 
@@ -55,7 +55,7 @@
        $currentuser = ($user->id == $USER->id);
     }
 
-    if (groupmode($course) == SEPARATEGROUPS and !isteacheredit($course->id)) {   // Groups must be kept separate
+    if (groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {   // Groups must be kept separate
         require_login();
 
         ///this is changed because of mygroupid
 
 
     if ($course->category and ! isguest() ) {   // Need to have access to a course to see that info
-        if (!isstudent($course->id, $user->id) && !isteacher($course->id, $user->id)) {
+        if (!has_capability('moodle/course:view', get_context_instance(CONTEXT_COURSE, $course->id))) {
             print_heading(get_string("notenrolled", "", $fullname));
             print_footer($course);
             die;
         print_heading(get_string("userdeleted"));
     }
 
-/// Get the hidden field list
-    if (isteacher($course->id) || isadmin()) {
+/// Get the hidden field list (user must have update capability to see hidden files?)
+    if (has_capability('moodle/user:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
         $hiddenfields = array();  // teachers and admins are allowed to see everything
     } else {
         $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
         print_row(get_string("location").":", $location);
     }
 
-    if (isteacher($course->id)) {
+    if (has_capability('moodle/user:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
         if ($user->address) {
             print_row(get_string("address").":", "$user->address");
         }
 
 /// Printing groups
     $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
-                             !isteacheredit($course->id));
+                             !has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id)));
     if (!$isseparategroups){
         if ($usergroups = user_group($course->id, $user->id)){
             $groupstr = '';