~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to user/index.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?PHP // $Id: index.php,v 1.162.2.15 2007/07/04 08:30:53 skodak Exp $
 
1
<?PHP // $Id: index.php,v 1.194.2.18 2008/12/01 20:46:13 skodak Exp $
2
2
 
3
3
//  Lists all the users within a given course
4
4
 
10
10
    define('DEFAULT_PAGE_SIZE', 20);
11
11
    define('SHOW_ALL_PAGE_SIZE', 5000);
12
12
 
13
 
    $group        = optional_param('group', -1, PARAM_INT);                   // Group to show
14
13
    $page         = optional_param('page', 0, PARAM_INT);                     // which page to show
15
14
    $perpage      = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);  // how many per page
16
15
    $mode         = optional_param('mode', NULL);                             // '0' for less details, '1' for more
17
 
    $accesssince  = optional_param('accesssince',0,PARAM_INT);               // filter by last access. -1 = never
 
16
    $accesssince  = optional_param('accesssince',0,PARAM_INT);                // filter by last access. -1 = never
18
17
    $search       = optional_param('search','',PARAM_CLEAN);
19
 
    $roleid       = optional_param('roleid', 0, PARAM_INT);                 // optional roleid
 
18
    $roleid       = optional_param('roleid', 0, PARAM_INT);                   // optional roleid, -1 means all site users on frontpage
20
19
 
21
 
    $contextid    = optional_param('contextid', 0, PARAM_INT);                 // one of this or
22
 
    $courseid     = optional_param('id', 0, PARAM_INT);                        // this are required
 
20
    $contextid    = optional_param('contextid', 0, PARAM_INT);                // one of this or
 
21
    $courseid     = optional_param('id', 0, PARAM_INT);                       // this are required
23
22
 
24
23
    if ($contextid) {
25
24
        if (! $context = get_context_instance_by_id($contextid)) {
43
42
    require_login($course);
44
43
 
45
44
    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
 
45
    $frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID);
46
46
 
47
 
    if (!has_capability('moodle/course:viewparticipants', $context)) {
48
 
        print_error('nopermissions');
 
47
    if ($context->id != $frontpagectx->id) {
 
48
        require_capability('moodle/course:viewparticipants', $context);
 
49
    } else {
 
50
        require_capability('moodle/site:viewparticipants', $sitecontext);
 
51
        // override the default on frontpage
 
52
        $roleid = optional_param('roleid', -1, PARAM_INT);
49
53
    }
50
54
 
 
55
    /// front page course is different
51
56
    $rolenames = array();
52
57
    $avoidroles = array();
53
58
 
54
59
    if ($roles = get_roles_used_in_context($context, true)) {
55
 
        // We should ONLY allow roles with moodle/course:view because otherwise we get little niggly issues 
 
60
        // We should ONLY allow roles with moodle/course:view because otherwise we get little niggly issues
56
61
        // like MDL-8093
57
 
        // We should further exclude "admin" users (those with "doanything" at site level) because 
 
62
        // We should further exclude "admin" users (those with "doanything" at site level) because
58
63
        // Otherwise they appear in every participant list
59
64
 
60
65
        $canviewroles    = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
61
66
        $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
62
67
 
 
68
        if ($context->id == $frontpagectx->id) {
 
69
            //we want admins listed on frontpage too
 
70
            foreach ($doanythingroles as $dar) {
 
71
                $canviewroles[$dar->id] = $dar;
 
72
            }
 
73
            $doanythingroles = array();
 
74
        }
 
75
 
63
76
        foreach ($roles as $role) {
64
77
            if (!isset($canviewroles[$role->id])) {   // Avoid this role (eg course creator)
65
78
                $avoidroles[] = $role->id;
71
84
                unset($roles[$role->id]);
72
85
                continue;
73
86
            }
 
87
            $rolenames[$role->id] = strip_tags(role_get_name($role, $context));   // Used in menus etc later on
74
88
        }
 
89
    }
75
90
 
76
 
        foreach ($roles as $role) {
77
 
            $rolenames[$role->id] = strip_tags(format_string($role->name));   // Used in menus etc later on
78
 
        }
 
91
    if ($context->id == $frontpagectx->id and $CFG->defaultfrontpageroleid) {
 
92
        // default frontpage role is assigned to all site users
 
93
        unset($rolenames[$CFG->defaultfrontpageroleid]);
79
94
    }
80
95
 
81
96
    // no roles to display yet?
82
 
    if (empty($rolenames)) {    
83
 
        if (has_capability('moodle/user:assign', $context)) {
 
97
    // frontpage course is an exception, on the front page course we should display all users
 
98
    if (empty($rolenames) && $context->id != $frontpagectx->id) {
 
99
        if (has_capability('moodle/role:assign', $context)) {
84
100
            redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
85
101
        } else {
86
102
            error ('No participants found for this course');
114
130
        $fullmode = false;
115
131
    }
116
132
 
117
 
/// Check to see if groups are being used in this forum
 
133
/// Check to see if groups are being used in this course
118
134
/// and if so, set $currentgroup to reflect the current group
119
135
 
120
 
    $groupmode    = groupmode($course);   // Groups are being used
121
 
    $currentgroup = get_and_set_current_group($course, $groupmode, $group);
 
136
    $groupmode    = groups_get_course_groupmode($course);   // Groups are being used
 
137
    $currentgroup = groups_get_course_group($course, true);
122
138
 
123
139
    if (!$currentgroup) {      // To make some other functions work better later
124
140
        $currentgroup  = NULL;
127
143
    $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
128
144
                         !has_capability('moodle/site:accessallgroups', $context));
129
145
 
130
 
    if ($isseparategroups and (!$currentgroup) ) { 
131
 
        print_header("$course->shortname: ".get_string('participants'), $course->fullname,
132
 
                     "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
133
 
                     get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
134
 
        print_heading(get_string("notingroup", "forum"));
 
146
    if ($isseparategroups and (!$currentgroup) ) {
 
147
        $navlinks = array();
 
148
        $navlinks[] = array('name' => get_string('participants'), 'link' => null, 'type' => 'misc');
 
149
        $navigation = build_navigation($navlinks);
 
150
 
 
151
        print_header("$course->shortname: ".get_string('participants'), $course->fullname, $navigation, "", "", true, "&nbsp;", navmenu($course));
 
152
        print_heading(get_string("notingroup"));
135
153
        print_footer($course);
136
154
        exit;
137
155
    }
138
156
 
139
157
    // Should use this variable so that we don't break stuff every time a variable is added or changed.
140
 
    $baseurl = $CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&amp;roleid='.$roleid.'&amp;id='.$course->id.'&amp;group='.$currentgroup.'&amp;perpage='.$perpage.'&amp;accesssince='.$accesssince.'&amp;search='.s($search);
 
158
    $baseurl = $CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&amp;roleid='.$roleid.'&amp;id='.$course->id.'&amp;perpage='.$perpage.'&amp;accesssince='.$accesssince.'&amp;search='.s($search);
141
159
 
142
160
/// Print headers
143
161
 
144
 
    if ($course->id != SITEID) {
145
 
        print_header("$course->shortname: ".get_string('participants'), $course->fullname,
146
 
                     "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
147
 
                     get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
148
 
    } else {
149
 
        print_header("$course->shortname: ".get_string('participants'), $course->fullname,
150
 
                     get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
151
 
    }
152
 
 
153
 
 
 
162
    $navlinks = array();
 
163
    $navlinks[] = array('name' => get_string('participants'), 'link' => null, 'type' => 'misc');
 
164
    $navigation = build_navigation($navlinks);
 
165
 
 
166
    print_header("$course->shortname: ".get_string('participants'), $course->fullname, $navigation, "", "", true, "&nbsp;", navmenu($course));
154
167
 
155
168
/// setting up tags
156
169
    if ($course->id == SITEID) {
175
188
        $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
176
189
    }
177
190
 
 
191
    if (isset($hiddenfields['lastaccess'])) {
 
192
        // do not allow access since filtering
 
193
        $accesssince = 0;
 
194
    }
178
195
 
179
196
/// Print settings and things in a table across the top
180
197
 
187
204
        foreach ($mycourses as $mycourse) {
188
205
            $courselist[$mycourse->id] = format_string($mycourse->shortname);
189
206
        }
 
207
        if (has_capability('moodle/site:viewparticipants', $sitecontext)) {
 
208
            unset($courselist[SITEID]);
 
209
            $courselist = array(SITEID => format_string($SITE->shortname)) + $courselist;
 
210
        }
190
211
        popup_form($CFG->wwwroot.'/user/index.php?roleid='.$roleid.'&amp;sifirst=&amp;silast=&amp;id=',
191
212
                   $courselist, 'courseform', $course->id, '', '', '', false, 'self', get_string('mycourses'));
192
213
        echo '</td>';
193
214
    }
194
215
 
195
216
    echo '<td class="left">';
196
 
    setup_and_print_groups($course, $groupmode, $baseurl);
 
217
    groups_print_course_menu($course, $baseurl);
197
218
    echo '</td>';
198
219
 
199
 
    // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
200
 
    // this might not work anymore because you always going to get yourself as the most recent entry? added $USER!=$user ch
201
 
    $minlastaccess = get_field_sql('SELECT min(timeaccess) FROM '.$CFG->prefix.'user_lastaccess WHERE courseid = '.$course->id.' AND timeaccess != 0 AND userid!='.$USER->id);
202
 
    $lastaccess0exists = record_exists('user_lastaccess','courseid',$course->id,'timeaccess',0);
203
 
    $now = usergetmidnight(time());
204
 
    $timeaccess = array();
205
 
 
206
 
    // makes sense for this to go first.
207
 
    $timeoptions[0] = get_string('selectperiod');
208
 
 
209
 
    // days
210
 
    for ($i = 1; $i < 7; $i++) {
211
 
        if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
212
 
            $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
213
 
        }
214
 
    }
215
 
    // weeks
216
 
    for ($i = 1; $i < 10; $i++) {
217
 
        if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
218
 
            $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
219
 
        }
220
 
    }
221
 
    // months
222
 
    for ($i = 2; $i < 12; $i++) {
223
 
        if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
224
 
            $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
225
 
        }
226
 
    }
227
 
    // try a year
228
 
    if (strtotime('-1 year',$now) >= $minlastaccess) {
229
 
        $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
230
 
    }
231
 
 
232
 
    if (!empty($lastaccess0exists)) {
233
 
        $timeoptions[-1] = get_string('never');
234
 
    }
235
 
 
236
 
    if (count($timeoptions) > 1) {
237
 
        echo '<td class="left">';
238
 
        $baseurl = preg_replace('/&amp;accesssince='.$accesssince.'/','',$baseurl);
239
 
        popup_form($baseurl.'&amp;accesssince=',$timeoptions,'timeoptions',$accesssince, '', '', '', false, 'self', get_string('usersnoaccesssince'));
240
 
        echo '</td>';
 
220
    if (!isset($hiddenfields['lastaccess'])) {
 
221
        // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
 
222
        // we need to make it diferently for normal courses and site course
 
223
        if ($context->id != $frontpagectx->id) {
 
224
            $minlastaccess = get_field_sql('SELECT min(timeaccess)
 
225
                                              FROM '.$CFG->prefix.'user_lastaccess
 
226
                                             WHERE courseid = '.$course->id.'
 
227
                                               AND timeaccess != 0');
 
228
            $lastaccess0exists = record_exists('user_lastaccess', 'courseid', $course->id, 'timeaccess', 0);
 
229
        } else {
 
230
            $minlastaccess = get_field_sql('SELECT min(lastaccess)
 
231
                                              FROM '.$CFG->prefix.'user
 
232
                                             WHERE lastaccess != 0');
 
233
            $lastaccess0exists = record_exists('user','lastaccess',0);
 
234
        }
 
235
 
 
236
        $now = usergetmidnight(time());
 
237
        $timeaccess = array();
 
238
 
 
239
        // makes sense for this to go first.
 
240
        $timeoptions[0] = get_string('selectperiod');
 
241
 
 
242
        // days
 
243
        for ($i = 1; $i < 7; $i++) {
 
244
            if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
 
245
                $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
 
246
            }
 
247
        }
 
248
        // weeks
 
249
        for ($i = 1; $i < 10; $i++) {
 
250
            if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
 
251
                $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
 
252
            }
 
253
        }
 
254
        // months
 
255
        for ($i = 2; $i < 12; $i++) {
 
256
            if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
 
257
                $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
 
258
            }
 
259
        }
 
260
        // try a year
 
261
        if (strtotime('-1 year',$now) >= $minlastaccess) {
 
262
            $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
 
263
        }
 
264
 
 
265
        if (!empty($lastaccess0exists)) {
 
266
            $timeoptions[-1] = get_string('never');
 
267
        }
 
268
 
 
269
        if (count($timeoptions) > 1) {
 
270
            echo '<td class="left">';
 
271
            $baseurl = preg_replace('/&amp;accesssince='.$accesssince.'/','',$baseurl);
 
272
            popup_form($baseurl.'&amp;accesssince=',$timeoptions,'timeoptions',$accesssince, '', '', '', false, 'self', get_string('usersnoaccesssince'));
 
273
            echo '</td>';
 
274
        }
241
275
    }
242
276
 
243
277
 
248
282
    echo '</td></tr></table>';
249
283
 
250
284
    if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) {    /// Display info about the group
251
 
        if ($group = groups_get_group($currentgroup)) { //TODO:
 
285
        if ($group = groups_get_group($currentgroup)) {
252
286
            if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
253
287
                echo '<table class="groupinfobox"><tr><td class="left side picture">';
254
288
                print_group_picture($group, $course->id, true, false, false);
255
289
                echo '</td><td class="content">';
256
290
                echo '<h3>'.$group->name;
257
291
                if (has_capability('moodle/course:managegroups', $context)) {
258
 
                    echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="'.groups_group_edit_url($course->id, $group->id).'">';
 
292
                    echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="'.$CFG->wwwroot.'/group/group.php?id='.$group->id.'&amp;courseid='.$group->courseid.'">';
259
293
                    echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt="'.get_string('editgroupprofile').'" />';
260
294
                    echo '</a>';
261
295
                }
266
300
        }
267
301
    }
268
302
 
269
 
 
270
303
    /// Define a table showing a list of users in the current role selection
271
304
 
272
305
    $tablecolumns = array('userpic', 'fullname');
299
332
    $table->define_columns($tablecolumns);
300
333
    $table->define_headers($tableheaders);
301
334
    $table->define_baseurl($baseurl);
302
 
    
303
 
    $table->sortable(true, 'lastaccess', SORT_DESC);
 
335
 
 
336
    if (!isset($hiddenfields['lastaccess'])) {
 
337
        $table->sortable(true, 'lastaccess', SORT_DESC);
 
338
    }
304
339
 
305
340
    $table->set_attribute('cellspacing', '0');
306
341
    $table->set_attribute('id', 'participants');
323
358
    } else {
324
359
        $listofcontexts = '('.$sitecontext->id.')'; // must be site
325
360
    }
326
 
    if ($roleid) {
 
361
    if ($roleid > 0) {
327
362
        $selectrole = " AND r.roleid = $roleid ";
328
363
    } else {
329
364
        $selectrole = " ";
330
365
    }
331
 
    $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, u.city, u.country, u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, ul.timeaccess AS lastaccess, r.hidden '; // s.lastaccess
332
 
    $select .= $course->enrolperiod?', r.timeend ':'';
333
 
 
334
 
    $from   = "FROM {$CFG->prefix}user u INNER JOIN
335
 
    {$CFG->prefix}role_assignments r on u.id=r.userid LEFT OUTER JOIN
336
 
    {$CFG->prefix}user_lastaccess ul on (r.userid=ul.userid and ul.courseid = $course->id)"; 
337
 
    
338
 
    $hiddensql = has_capability('moodle/role:viewhiddenassigns', $context)? '':' AND r.hidden = 0 ';   
 
366
 
 
367
    if ($context->id != $frontpagectx->id) {
 
368
        $select = 'SELECT DISTINCT u.id, u.username, u.firstname, u.lastname,
 
369
                      u.email, u.city, u.country, u.picture,
 
370
                      u.lang, u.timezone, u.emailstop, u.maildisplay, u.imagealt,
 
371
                      COALESCE(ul.timeaccess, 0) AS lastaccess,
 
372
                      r.hidden,
 
373
                      ctx.id AS ctxid, ctx.path AS ctxpath,
 
374
                      ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel ';
 
375
        $select .= $course->enrolperiod?', r.timeend ':'';
 
376
    } else {
 
377
        if ($roleid >= 0) {
 
378
            $select = 'SELECT u.id, u.username, u.firstname, u.lastname,
 
379
                          u.email, u.city, u.country, u.picture,
 
380
                          u.lang, u.timezone, u.emailstop, u.maildisplay, u.imagealt,
 
381
                          u.lastaccess, r.hidden,
 
382
                          ctx.id AS ctxid, ctx.path AS ctxpath,
 
383
                          ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel ';
 
384
        } else {
 
385
            $select = 'SELECT u.id, u.username, u.firstname, u.lastname,
 
386
                          u.email, u.city, u.country, u.picture,
 
387
                          u.lang, u.timezone, u.emailstop, u.maildisplay, u.imagealt,
 
388
                          u.lastaccess,
 
389
                          ctx.id AS ctxid, ctx.path AS ctxpath,
 
390
                          ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel ';
 
391
        }
 
392
    }
 
393
 
 
394
    if ($context->id != $frontpagectx->id or $roleid >= 0) {
 
395
        $from   = "FROM {$CFG->prefix}user u
 
396
                LEFT OUTER JOIN {$CFG->prefix}context ctx
 
397
                    ON (u.id=ctx.instanceid AND ctx.contextlevel = ".CONTEXT_USER.")
 
398
                JOIN {$CFG->prefix}role_assignments r
 
399
                    ON u.id=r.userid
 
400
                LEFT OUTER JOIN {$CFG->prefix}user_lastaccess ul
 
401
                    ON (r.userid=ul.userid and ul.courseid = $course->id) ";
 
402
    } else {
 
403
        // on frontpage and we want all registered users
 
404
        $from = "FROM {$CFG->prefix}user u
 
405
                LEFT OUTER JOIN {$CFG->prefix}context ctx
 
406
                    ON (u.id=ctx.instanceid AND ctx.contextlevel = ".CONTEXT_USER.") ";
 
407
    }
 
408
 
 
409
    $hiddensql = has_capability('moodle/role:viewhiddenassigns', $context)? '':' AND r.hidden = 0 ';
339
410
 
340
411
    // exclude users with roles we are avoiding
341
412
    if ($avoidroles) {
345
416
    } else {
346
417
        $adminroles = '';
347
418
    }
348
 
    
 
419
 
349
420
    // join on 2 conditions
350
421
    // otherwise we run into the problem of having records in ul table, but not relevant course
351
422
    // and user record is not pulled out
352
 
    $where  = "WHERE (r.contextid = $context->id OR r.contextid in $listofcontexts)
353
 
        AND u.deleted = 0 $selectrole
354
 
        AND (ul.courseid = $course->id OR ul.courseid IS NULL)
355
 
        AND u.username != 'guest'
356
 
        $adminroles
357
 
        $hiddensql ";
358
 
        $where .= get_lastaccess_sql($accesssince);
359
423
 
 
424
    if ($context->id != $frontpagectx->id) {
 
425
        $where  = "WHERE (r.contextid = $context->id OR r.contextid in $listofcontexts)
 
426
            AND u.deleted = 0 $selectrole
 
427
            AND (ul.courseid = $course->id OR ul.courseid IS NULL)
 
428
            AND u.username != 'guest'
 
429
            $adminroles
 
430
            $hiddensql ";
 
431
            $where .= get_course_lastaccess_sql($accesssince);
 
432
    } else {
 
433
        if ($roleid >= 0) {
 
434
            $where = "WHERE (r.contextid = $context->id OR r.contextid in $listofcontexts)
 
435
                AND u.deleted = 0 $selectrole
 
436
                AND u.username != 'guest'";
 
437
                $where .= get_user_lastaccess_sql($accesssince);
 
438
        } else {
 
439
            $where = "WHERE u.deleted = 0
 
440
                AND u.username != 'guest'";
 
441
                $where .= get_user_lastaccess_sql($accesssince);
 
442
        }
 
443
    }
360
444
    $wheresearch = '';
361
445
 
362
446
    if (!empty($search)) {
378
462
        $where .= ' AND '.$table->get_sql_where();
379
463
    }
380
464
 
 
465
    /// Always add r.hidden to sort in order to guarantee hiddens to "win"
 
466
    /// in the resolution of duplicates later - MDL-13935
 
467
    /// Only exception is frontpage that doesn't have such r.hidden info
 
468
    /// because it retrieves ALL users (without role checking) - MDL-14034
381
469
    if ($table->get_sql_sort()) {
382
470
        $sort = ' ORDER BY '.$table->get_sql_sort();
 
471
        if ($context->id != $frontpagectx->id or $roleid >= 0) {
 
472
            $sort .= ', r.hidden DESC';
 
473
        }
383
474
    } else {
384
475
        $sort = '';
 
476
        if ($context->id != $frontpagectx->id or $roleid >= 0) {
 
477
            $sort .= ' ORDER BY r.hidden DESC';
 
478
        }
385
479
    }
386
480
 
387
481
    $matchcount = count_records_sql('SELECT COUNT(distinct u.id) '.$from.$where.$wheresearch);
388
482
 
389
 
    $table->initialbars($totalcount > $perpage);
 
483
    $table->initialbars(true);
390
484
    $table->pagesize($perpage, $matchcount);
391
485
 
392
 
    $userlist = get_records_sql($select.$from.$where.$wheresearch.$sort,
 
486
    $userlist = get_recordset_sql($select.$from.$where.$wheresearch.$sort,
393
487
            $table->get_page_start(),  $table->get_page_size());
394
488
 
 
489
    if ($context->id == $frontpagectx->id) {
 
490
        $strallsiteusers = get_string('allsiteusers', 'role');
 
491
        if ($CFG->defaultfrontpageroleid) {
 
492
            if ($fprole = get_record('role', 'id', $CFG->defaultfrontpageroleid)) {
 
493
                $fprole = role_get_name($fprole, $frontpagectx);
 
494
                $strallsiteusers = "$strallsiteusers ($fprole)";
 
495
            }
 
496
        }
 
497
        $rolenames = array(-1 => $strallsiteusers) + $rolenames;
 
498
    }
 
499
 
395
500
    /// If there are multiple Roles in the course, then show a drop down menu for switching
396
 
 
397
501
    if (count($rolenames) > 1) {
398
502
        echo '<div class="rolesform">';
399
 
        echo get_string('currentrole', 'role').': ';
400
 
        $rolenames = array(0 => get_string('all')) + $rolenames;
 
503
        echo '<label for="rolesform_jump">'.get_string('currentrole', 'role').'&nbsp;</label>';
 
504
        if ($context->id != $frontpagectx->id) {
 
505
            $rolenames = array(0 => get_string('all')) + $rolenames;
 
506
        } else {
 
507
            if (!$CFG->defaultfrontpageroleid) {
 
508
                // we do not want "All users with role" - we already have all users in defualt frontpage role option
 
509
                $rolenames = array(0 => get_string('userswithrole', 'role')) + $rolenames;
 
510
            }
 
511
        }
401
512
        popup_form("$CFG->wwwroot/user/index.php?contextid=$context->id&amp;sifirst=&amp;silast=&amp;roleid=", $rolenames,
402
513
                   'rolesform', $roleid, '');
 
514
        echo '</div>';
 
515
 
 
516
    } else if (count($rolenames) == 1) {
 
517
        // when all users with the same role - print its name
 
518
        echo '<div class="rolesform">';
 
519
        echo get_string('role').': ';
 
520
        $rolename = reset($rolenames);
 
521
        echo $rolename;
 
522
        echo '</div>';
403
523
    }
404
524
 
405
 
    if ($roleid) {
 
525
    if ($roleid > 0) {
406
526
        if (!$currentrole = get_record('role','id',$roleid)) {
407
527
            error('That role does not exist');
408
528
        }
409
529
        $a->number = $totalcount;
410
 
        $a->role = $currentrole->name;
 
530
        // MDL-12217, use course specific rolename
 
531
        if (isset($rolenames[$currentrole->id])){
 
532
            $a->role = $rolenames[$currentrole->id];
 
533
        }else{
 
534
            $a->role = $currentrole->name;//safety net
 
535
        }
411
536
        $heading = format_string(get_string('xuserswiththerole', 'role', $a));
 
537
 
 
538
        if ($currentgroup and $group) {
 
539
            $a->group = $group->name;
 
540
            $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
 
541
        }
 
542
 
 
543
        if ($accesssince) {
 
544
            $a->timeperiod = $timeoptions[$accesssince];
 
545
            $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
 
546
        }
 
547
 
 
548
        $heading .= ": $a->number";
412
549
        if (user_can_assign($context, $roleid)) {
413
550
            $heading .= ' <a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?roleid='.$roleid.'&amp;contextid='.$context->id.'">';
414
551
            $heading .= '<img src="'.$CFG->pixpath.'/i/edit.gif" class="icon" alt="" /></a>';
415
552
        }
416
553
        print_heading($heading, 'center', 3);
417
554
    } else {
 
555
        if ($course->id != SITEID && has_capability('moodle/role:assign', $context)) {
 
556
            $editlink  = ' <a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id.'">';
 
557
            $editlink .= '<img src="'.$CFG->pixpath.'/i/edit.gif" class="icon" alt="" /></a>';
 
558
        } else {
 
559
            $editlink = '';
 
560
        }
 
561
        if ($course->id == SITEID and $roleid < 0) {
 
562
            $strallparticipants = get_string('allsiteusers', 'role');
 
563
        } else {
 
564
            $strallparticipants = get_string('allparticipants');
 
565
        }
418
566
        if ($matchcount < $totalcount) {
419
 
            print_heading(get_string('allparticipants').': '.$matchcount.'/'.$totalcount, '', 3);
 
567
            print_heading($strallparticipants.': '.$matchcount.'/'.$totalcount . $editlink, '', 3);
420
568
        } else {
421
 
            print_heading(get_string('allparticipants').': '.$matchcount, '', 3);
 
569
            print_heading($strallparticipants.': '.$matchcount . $editlink, '', 3);
422
570
        }
423
571
    }
424
572
 
454
602
        echo '<form action="action_redir.php" method="post" id="participantsform" onsubmit="return checksubmit(this);">';
455
603
        echo '<div>';
456
604
        echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
457
 
        echo '<input type="hidden" name="returnto" value="'.s($_SERVER['REQUEST_URI']).'" />';
 
605
        echo '<input type="hidden" name="returnto" value="'.s(me()).'" />';
458
606
    }
459
607
 
460
608
    if ($CFG->longtimenosee > 0 && $CFG->longtimenosee < 1000 && $totalcount > 0) {
510
658
            }
511
659
 
512
660
            if ($matchcount > 0) {
513
 
                foreach ($userlist as $user) {
 
661
                $usersprinted = array();
 
662
                while ($user = rs_fetch_next_record($userlist)) {
 
663
                    if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
 
664
                        continue;
 
665
                    }
 
666
                    $usersprinted[] = $user->id; /// Add new user to the array of users printed
 
667
 
 
668
                    $user = make_context_subobj($user);
514
669
                    print_user($user, $course, $bulkoperations);
515
670
                }
516
671
 
524
679
        $timeformat = get_string('strftimedate');
525
680
 
526
681
 
527
 
        if (!empty($userlist))  {
528
 
            foreach ($userlist as $user) {
529
 
                if ($user->hidden) {        
 
682
        if ($userlist)  {
 
683
            $usersprinted = array();
 
684
            while ($user = rs_fetch_next_record($userlist)) {
 
685
                if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
 
686
                    continue;
 
687
                }
 
688
                $usersprinted[] = $user->id; /// Add new user to the array of users printed
 
689
 
 
690
                $user = make_context_subobj($user);
 
691
                if ( !empty($user->hidden) ) {
530
692
                // if the assignment is hidden, display icon
531
 
                    $hidden = "<img src=\"{$CFG->pixpath}/t/hide.gif\" alt=\"".get_string('hiddenassign')."\" class=\"hide-show-image\"/>";
 
693
                    $hidden = " <img src=\"{$CFG->pixpath}/t/show.gif\" title=\"".get_string('userhashiddenassignments', 'role')."\" alt=\"".get_string('hiddenassign')."\" class=\"hide-show-image\"/>";
532
694
                } else {
533
 
                    $hidden = '';  
 
695
                    $hidden = '';
534
696
                }
535
 
                
 
697
 
536
698
                if ($user->lastaccess) {
537
699
                    $lastaccess = format_time(time() - $user->lastaccess, $datestring);
538
700
                } else {
550
712
                        $country = $countries[$user->country];
551
713
                    }
552
714
                }
553
 
                
554
 
                $usercontext = get_context_instance(CONTEXT_USER, $user->id);
555
 
                
556
 
                if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) ||has_capability('moodle/user:viewdetails', $context))) {
 
715
 
 
716
                if (!isset($user->context)) {
 
717
                    $usercontext = get_context_instance(CONTEXT_USER, $user->id);
 
718
                } else {
 
719
                    $usercontext = $user->context;
 
720
                }
 
721
 
 
722
                if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
557
723
                    $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
558
724
                } else {
559
 
                    $profilelink = '<strong>'.fullname($user).'</strong>';   
 
725
                    $profilelink = '<strong>'.fullname($user).'</strong>';
560
726
                }
561
 
                               
 
727
 
562
728
                $data = array (
563
 
                        print_user_picture($user->id, $course->id, $user->picture, false, true, $piclink),
564
 
                        $profilelink);
 
729
                        print_user_picture($user, $course->id, $user->picture, false, true, $piclink),
 
730
                        $profilelink . $hidden);
565
731
 
566
732
                if (!isset($hiddenfields['city'])) {
567
733
                    $data[] = $user->city;
596
762
        echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
597
763
        echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
598
764
        $displaylist = array();
599
 
        // fix for MDL-8885, only show this if user has capability
600
 
        if (has_capability('moodle/site:readallmessages', $context) && !empty($CFG->messaging)) {
601
 
            $displaylist['messageselect.php'] = get_string('messageselectadd');
 
765
        $displaylist['messageselect.php'] = get_string('messageselectadd');
 
766
        if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
 
767
            $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
 
768
            $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
602
769
        }
603
 
        if ($course->enrolperiod) {
 
770
 
 
771
        if ($context->id != $frontpagectx->id) {
604
772
            $displaylist['extendenrol.php'] = get_string('extendenrol');
 
773
            $displaylist['groupextendenrol.php'] = get_string('groupextendenrol');
605
774
        }
 
775
 
606
776
        helpbutton("participantswithselectedusers", get_string("withselectedusers"));
607
777
        choose_from_menu ($displaylist, "formaction", "", get_string("withselectedusers"), "if(checksubmit(this.form))this.form.submit();", "");
608
778
        echo '<input type="hidden" name="id" value="'.$course->id.'" />';
609
 
        echo '<input type="submit" value="' . get_string('ok') . '" />';
 
779
        echo '<div id="noscriptparticipantsform" style="display: inline;">';
 
780
        echo '<input type="submit" value="'.get_string('ok').'" /></div>';
 
781
        echo '<script type="text/javascript">'.
 
782
               "\n//<![CDATA[\n".
 
783
               'document.getElementById("noscriptparticipantsform").style.display = "none";'.
 
784
               "\n//]]>\n".'</script>';
610
785
        echo '</div>';
611
786
        echo '</div>';
612
787
        echo '</form>';
628
803
 
629
804
    print_footer($course);
630
805
 
631
 
 
632
 
 
633
 
 
634
 
function get_lastaccess_sql($accesssince='') {
 
806
    if ($userlist) {
 
807
        rs_close($userlist);
 
808
    }
 
809
 
 
810
 
 
811
function get_course_lastaccess_sql($accesssince='') {
635
812
    if (empty($accesssince)) {
636
813
        return '';
637
814
    }
638
815
    if ($accesssince == -1) { // never
639
816
        return ' AND ul.timeaccess = 0';
640
817
    } else {
641
 
        return ' AND ul.timeaccess != 0 AND timeaccess < '.$accesssince;
 
818
        return ' AND ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
 
819
    }
 
820
}
 
821
 
 
822
function get_user_lastaccess_sql($accesssince='') {
 
823
    if (empty($accesssince)) {
 
824
        return '';
 
825
    }
 
826
    if ($accesssince == -1) { // never
 
827
        return ' AND u.lastaccess = 0';
 
828
    } else {
 
829
        return ' AND u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
642
830
    }
643
831
}
644
832