9e47313dc181348a665e9904d74601ab3f69896c
[moodle.git] / user / tabs.php
1 <?php  // $Id$
2 /// This file to be included so we can assume config.php has already been included.
3 /// We also assume that $user, $course, $currenttab have been set
5     if (!isset($filtertype)) {
6         $filtertype = '';
7     }
8     if (!isset($filterselect)) {
9         $filterselect = '';
10     }
12     //make sure everything is cleaned properly
13     $filtertype   = clean_param($filtertype, PARAM_ALPHA);
14     $filterselect = clean_param($filterselect, PARAM_INT);
16     if (empty($currenttab) or empty($user) or empty($course)) {
17         //error('You cannot call this script in that way');
18     }
20     if (($filtertype == 'site' && $filterselect) || ($filtertype=='user' && $filterselect)) {
21         $user = get_record('user','id',$filterselect);
22     }
24     $inactive = NULL;
25     $activetwo = NULL;
26     $toprow = array();
28     /**************************************
29      * Site Level participation or Blogs  *
30      **************************************/
31     if ($filtertype == 'site') {
33         $site = get_site();
34         print_heading(format_string($site->fullname));
35         
36         if ($CFG->bloglevel >= 4) {
37             if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
38                 $toprow[] = new tabobject('participants', $CFG->wwwroot.'/user/index.php?id='.SITEID,
39                     get_string('participants'));
40             }
42             $toprow[] = new tabobject('blogs', $CFG->wwwroot.'/blog/index.php?filtertype=site&amp;',
43                 get_string('blogs','blog'));
44         }
46     /**************************************
47      * Course Level participation or Blogs  *
48      **************************************/
49     } else if ($filtertype == 'course' && $filterselect) {
51         $course = get_record('course','id',$filterselect);
52         print_heading(format_string($course->fullname));
54         if ($CFG->bloglevel >= 3) {
56             $toprow[] = new tabobject('participants', $CFG->wwwroot.'/user/index.php?id='.$filterselect.'&amp;group=0',
57                 get_string('participants'));    //the groupid hack is necessary, otherwise the group in the session willbe used
58         
59             $toprow[] = new tabobject('blogs', $CFG->wwwroot.'/blog/index.php?filtertype=course&amp;filterselect='.$filterselect, get_string('blogs','blog'));
60         }
62     /**************************************
63      * Group Level participation or Blogs  *
64      **************************************/
65     } else if ($filtertype == 'group' && $filterselect) {
67         $group_name = groups_get_group_name($filterselect); //TODO:
68         print_heading($group_name);
70         if ($CFG->bloglevel >= 2) {
72             $toprow[] = new tabobject('participants', $CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$filterselect,
73                 get_string('participants'));
75         
76             $toprow[] = new tabobject('blogs', $CFG->wwwroot.'/blog/index.php?filtertype=group&amp;filterselect='.$filterselect, get_string('blogs','blog'));
77         }
79     /**************************************
80      * User Level participation or Blogs  *
81      **************************************/
82     } else {
83         if (isset($userid)) {
84             $user = get_record('user','id', $userid);
85         }
86         print_heading(fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))));
88         $toprow[] = new tabobject('profile', $CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id, get_string('profile'));
90         $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
91         $coursecontext   = get_context_instance(CONTEXT_COURSE, $course->id);
92         $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
94     /// Can only edit profile if it belongs to user or current user is admin and not editing primary admin
96         $mainadmin = get_admin();
98         if(empty($CFG->loginhttps)) {
99             $wwwroot = $CFG->wwwroot;
100         } else {
101             $wwwroot = str_replace('http:','https:',$CFG->wwwroot);
102         }
104         $edittype = 'none';
105         if (isguestuser($user)) {
106             // guest account can not be edited
108         } else if (is_mnet_remote_user($user)) {
109             // cannot edit remote users
111         } else if (isguestuser() or !isloggedin()) {
112             // guests and not logged in can not edit own profile
114         } else if ($USER->id == $user->id) {
115             if (has_capability('moodle/user:update', $systemcontext)) {
116                 $edittype = 'advanced';
117             } else if (has_capability('moodle/user:editownprofile', $systemcontext)) {
118                 $edittype = 'normal';
119             }
121         } else if ($user->id != $mainadmin->id) {
122             //no editing of primary admin!
123             if (has_capability('moodle/user:update', $systemcontext)) {
124                 $edittype = 'advanced';
125             } else if (has_capability('moodle/user:editprofile', $personalcontext)) {
126                 //teachers, parents, etc.
127                 $edittype = 'normal';
128             }
129         }
131         if ($edittype == 'advanced') {
132             $toprow[] = new tabobject('editprofile', $wwwroot.'/user/editadvanced.php?id='.$user->id.'&amp;course='.$course->id, get_string('editmyprofile'));
133         } else if ($edittype == 'normal') {
134             $toprow[] = new tabobject('editprofile', $wwwroot.'/user/edit.php?id='.$user->id.'&amp;course='.$course->id, get_string('editmyprofile'));
135         }
137     /// Everyone can see posts for this user
138     
139     /// add logic to see course read posts permission
140         if (has_capability('moodle/user:readuserposts', $personalcontext) || has_capability('mod/forum:viewdiscussion', get_context_instance(CONTEXT_COURSE, $course->id))) {
141             $toprow[] = new tabobject('forumposts', $CFG->wwwroot.'/mod/forum/user.php?id='.$user->id.'&amp;course='.$course->id,
142                         get_string('forumposts', 'forum'));
144             if (in_array($currenttab, array('posts', 'discussions'))) {
145                 $inactive = array('forumposts');
146                 $activetwo = array('forumposts');
148                 $secondrow = array();
149                 $secondrow[] = new tabobject('posts', $CFG->wwwroot.'/mod/forum/user.php?course='.$course->id.
150                                       '&amp;id='.$user->id.'&amp;mode=posts', get_string('posts', 'forum'));
151                 $secondrow[] = new tabobject('discussions', $CFG->wwwroot.'/mod/forum/user.php?course='.$course->id.
152                                       '&amp;id='.$user->id.'&amp;mode=discussions', get_string('discussions', 'forum'));
153             }
155         }
157     /// Personal blog entries tab
158         require_once($CFG->dirroot.'/blog/lib.php');
159         if ($CFG->bloglevel >= BLOG_USER_LEVEL and // blogs must be enabled
160             (has_capability('moodle/user:readuserblogs', $personalcontext) // can review posts (parents etc)
161             or has_capability('moodle/blog:manageentries', $systemcontext)     // entry manager can see all posts
162             or ($user->id == $USER->id and has_capability('moodle/blog:create', $systemcontext)) // viewing self
163             or (has_capability('moodle/blog:view', $systemcontext) or has_capability('moodle/blog:view', $coursecontext))
164             ) // able to read blogs in site or course context
165         ) { //end if
167             $toprow[] = new tabobject('blogs', $CFG->wwwroot.'/blog/index.php?userid='.$user->id.'&amp;courseid='.$course->id, get_string('blog', 'blog'));
168         }
170     /// Current user must be teacher of the course or the course allows user to view their reports
171     
172     //print_object($course);
173     //print_object($user);
174     
175         // add in logic to check course read report
176         if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) || ($course->showreports and $USER->id == $user->id) || has_capability('moodle/user:viewuseractivitiesreport', $coursecontext)) {
178             $toprow[] = new tabobject('reports', $CFG->wwwroot.'/course/user.php?id='.$course->id.
179                                       '&amp;user='.$user->id.'&amp;mode=outline', get_string('activityreports'));
181             if (in_array($currenttab, array('outline', 'complete', 'todaylogs', 'alllogs', 'stats', 'grade'))) {
182                 $inactive = array('reports');
183                 $activetwo = array('reports');
185                 $secondrow = array();
186                 $secondrow[] = new tabobject('outline', $CFG->wwwroot.'/course/user.php?id='.$course->id.
187                                           '&amp;user='.$user->id.'&amp;mode=outline', get_string('outlinereport'));
188                 $secondrow[] = new tabobject('complete', $CFG->wwwroot.'/course/user.php?id='.$course->id.
189                                           '&amp;user='.$user->id.'&amp;mode=complete', get_string('completereport'));
190                 $secondrow[] = new tabobject('todaylogs', $CFG->wwwroot.'/course/user.php?id='.$course->id.
191                                           '&amp;user='.$user->id.'&amp;mode=todaylogs', get_string('todaylogs'));
192                 $secondrow[] = new tabobject('alllogs', $CFG->wwwroot.'/course/user.php?id='.$course->id.
193                                           '&amp;user='.$user->id.'&amp;mode=alllogs', get_string('alllogs'));
194                 if (!empty($CFG->enablestats)) {
195                     $secondrow[] = new tabobject('stats',$CFG->wwwroot.'/course/user.php?id='.$course->id.
196                                                  '&amp;user='.$user->id.'&amp;mode=stats',get_string('stats'));
197                 }
198                 
199                 if ($course->showgrades) {
200                     $secondrow[] = new tabobject('grade', $CFG->wwwroot.'/course/user.php?id='.$course->id.
201                                           '&amp;user='.$user->id.'&amp;mode=grade', get_string('grade'));
202                 }
203                                 
204             }
206         }
208     }    //close last bracket (individual tags)
211     /// this needs permission checkings
213     
214     if (!empty($showroles) and !empty($user)) { // this variable controls whether this roles is showed, or not, so only user/view page should set this flag
215         $usercontext = get_context_instance(CONTEXT_USER, $user->id);
216         if (has_capability('moodle/role:assign',$usercontext)) {
217             $toprow[] = new tabobject('roles', $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
218                                   ,get_string('roles'));
219                                   
220             if (in_array($currenttab, array('assign', 'override'))) {
221                 $inactive = array('roles');
222                 $activetwo = array('roles');
223     
224                 $secondrow = array();
225                 $secondrow[] = new tabobject('assign', $CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
226                                   ,get_string('assignroles', 'role'));
227                 $secondrow[] = new tabobject('override', $CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$usercontext->id.'&amp;userid='.$user->id.'&amp;courseid='.$course->id
228                                   ,get_string('overrideroles', 'role'));
229                                     
230             }
231         }                                                                                                       
232     }
233 /// Add second row to display if there is one
235     if (!empty($secondrow)) {
236         $tabs = array($toprow, $secondrow);
237     } else {
238         $tabs = array($toprow);
239     }
241 /// Print out the tabs and continue!
243     print_tabs($tabs, $currenttab, $inactive, $activetwo);
245 ?>