assign.php 16.4 KB
Newer Older
1
<?php
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
20

/**
 * Lets you assign roles to users in a particular context.
 *
21
22
23
24
25
 * @package    moodlecore
 * @subpackage role
 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
26

27
28
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
29

30
define("MAX_USERS_TO_LIST_PER_ROLE", 10);
31

32
33
34
35
$contextid      = required_param('contextid',PARAM_INT);
$roleid         = optional_param('roleid', 0, PARAM_INT);
$extendperiod   = optional_param('extendperiod', 0, PARAM_INT);
$extendbase     = optional_param('extendbase', 3, PARAM_INT);
36

37
list($context, $course, $cm) = get_context_info_array($contextid);
38

39
$url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
40

41
if (!$course) {
42
    if ($context->contextlevel == CONTEXT_USER) {
43
44
45
46
        $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
        $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
        $url->param('courseid', $course->id);
        $url->param('userid', $user->id);
47
    } else {
48
        $course = $SITE;
49
    }
50
51
}

52

53
54
55
// security
require_login($course, false, $cm);
require_capability('moodle/role:assign', $context);
56
$PAGE->set_url($url);
57
58
59
60
61
$PAGE->set_context($context);

$contextname = print_context_name($context);
$courseid = $course->id;
$inmeta = $course->metacourse;
62
$isfrontpage = ($course->id == SITEID);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

// These are needed early because of tabs.php
list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);

// Make sure this user can assign this role
if ($roleid && !isset($assignableroles[$roleid])) {
    $a = new stdClass;
    $a->roleid = $roleid;
    $a->context = $contextname;
    print_error('cannotassignrolehere', '', get_context_url($context), $a);
}

// Work out an appropriate page title.
if ($roleid) {
    $a = new stdClass;
    $a->role = $assignableroles[$roleid];
    $a->context = $contextname;
    $title = get_string('assignrolenameincontext', 'role', $a);
} else {
    if ($isfrontpage) {
        $title = get_string('frontpageroles', 'admin');
85
    } else {
86
        $title = get_string('assignrolesin', 'role', $contextname);
87
    }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
}

// Build the list of options for the enrolment period dropdown.
$unlimitedperiod = get_string('unlimited');
for ($i=1; $i<=365; $i++) {
    $seconds = $i * 86400;
    $periodmenu[$seconds] = get_string('numdays', '', $i);
}
// Work out the apropriate default setting.
if ($extendperiod) {
    $defaultperiod = $extendperiod;
} else {
    $defaultperiod = $course->enrolperiod;
}

// Build the list of options for the starting from dropdown.
$timeformat = get_string('strftimedatefullshort');
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);

// MDL-12420, preventing course start date showing up as an option at system context and front page roles.
if ($course->startdate > 0) {
    $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $timeformat) . ')';
}
if ($course->enrollable != 2 || ($course->enrolstartdate == 0 || $course->enrolstartdate <= $today) && ($course->enrolenddate == 0 || $course->enrolenddate > $today)) {
    $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
}
if ($course->enrollable == 2) {
    if($course->enrolstartdate > 0) {
        $basemenu[4] = get_string('courseenrolstart') . ' (' . userdate($course->enrolstartdate, $timeformat) . ')';
118
    }
119
120
    if($course->enrolenddate > 0) {
        $basemenu[5] = get_string('courseenrolend') . ' (' . userdate($course->enrolenddate, $timeformat) . ')';
121
    }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

// Process any incoming role assignments before printing the header.
if ($roleid) {

    // Create the user selector objects.
    $options = array('context' => $context, 'roleid' => $roleid);

    $potentialuserselector = roles_get_potential_user_selector($context, 'addselect', $options);
    $currentuserselector = new existing_role_holders('removeselect', $options);

    // Process incoming role assignments
    $errors = array();
    if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
        $userstoassign = $potentialuserselector->get_selected_users();
        if (!empty($userstoassign)) {

            foreach ($userstoassign as $adduser) {
                $allow = true;
                if ($inmeta) {
                    if (has_capability('moodle/course:managemetacourse', $context, $adduser->id)) {
                        //ok
                    } else {
                        $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context);
                        if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) {
                            $erruser = $DB->get_record('user', array('id'=>$adduser->id), 'id, firstname, lastname');
                            $errors[] = get_string('metaassignerror', 'role', fullname($erruser));
                            $allow = false;
150
151
152
                        }
                    }
                }
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
                if ($allow) {
                    switch($extendbase) {
                        case 2:
                            $timestart = $course->startdate;
                            break;
                        case 3:
                            $timestart = $today;
                            break;
                        case 4:
                            $timestart = $course->enrolstartdate;
                            break;
                        case 5:
                            $timestart = $course->enrolenddate;
                            break;
                    }
169

170
171
172
173
174
175
                    if($extendperiod > 0) {
                        $timeend = $timestart + $extendperiod;
                    } else {
                        $timeend = 0;
                    }
                    if (! role_assign($roleid, $adduser->id, 0, $context->id, $timestart, $timeend)) {
176
                        $a = new stdClass;
177
                        $a->role = $assignableroles[$roleid];
178
179
                        $a->user = fullname($adduser);
                        $errors[] = get_string('assignerror', 'role', $a);
180
181
                    }
                }
182
            }
toyomoyo's avatar
toyomoyo committed
183

184
185
            $potentialuserselector->invalidate_selected_users();
            $currentuserselector->invalidate_selected_users();
186

187
188
189
190
            $rolename = $assignableroles[$roleid];
            add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
            // Counts have changed, so reload.
            list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
191
        }
192
193
    }

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    // Process incoming role unassignments
    if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
        $userstounassign = $currentuserselector->get_selected_users();
        if (!empty($userstounassign)) {

            foreach ($userstounassign as $removeuser) {
                if (! role_unassign($roleid, $removeuser->id, 0, $context->id)) {
                    $a = new stdClass;
                    $a->role = $assignableroles[$roleid];
                    $a->user = fullname($removeuser);
                    $errors[] = get_string('unassignerror', 'role', $a);
                } else if ($inmeta) {
                    sync_metacourse($courseid);
                    $newroles = get_user_roles($context, $removeuser->id, false);
                    if (empty($newroles) || array_key_exists($roleid, $newroles)) {
                        $errors[] = get_string('metaunassignerror', 'role', fullname($removeuser));
                    }
                }
212
213
            }

214
215
            $potentialuserselector->invalidate_selected_users();
            $currentuserselector->invalidate_selected_users();
216

217
218
219
220
221
            $rolename = $assignableroles[$roleid];
            add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
            // Counts have changed, so reload.
            list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
        }
222
    }
223
224
}

225
226
227
228
229
230
231
232
233
$PAGE->set_pagelayout('admin');
$PAGE->set_title($title);
$tabfile = $CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php';

switch ($context->contextlevel) {
    case CONTEXT_SYSTEM:
        admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
        break;
    case CONTEXT_USER:
234
        $tabfile = null;
235
236
237
238
239
        if ($isfrontpage) {
            $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
            $PAGE->set_heading($fullname);
        } else {
            $PAGE->set_heading($course->fullname);
240
        }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        $showroles = 1;
        break;
    case CONTEXT_COURSECAT:
        $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
        break;
    case CONTEXT_COURSE:
        if ($isfrontpage) {
            admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
        } else {
            $PAGE->set_heading($course->fullname);
        }
        break;
    case CONTEXT_MODULE:
        $PAGE->set_heading(print_context_name($context, false));
        $PAGE->set_cacheable(false);
        break;
    case CONTEXT_BLOCK:
        $PAGE->set_heading($PAGE->course->fullname);
        break;
260
261
}

262
echo $OUTPUT->header();
263
264
265
266
if ($tabfile) {
    $currenttab = 'assign';
    include($tabfile);
}
267

268
// Print heading.
269
echo $OUTPUT->heading_with_help($title, 'assignroles', 'role');
270
271
272
273
274
275
276

if ($roleid) {
    // Show UI for assigning a particular role to users.
    // Print a warning if we are assigning system roles.
    if ($context->contextlevel == CONTEXT_SYSTEM) {
        echo $OUTPUT->box(get_string('globalroleswarning', 'role'));
    }
277

278
279
    // Print the form.
$assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
280
?>
281
<form id="assignform" method="post" action="<?php echo $assignurl ?>"><div>
282
  <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
283
284
285
286
287
288
289
290
291

  <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
    <tr>
      <td id="existingcell">
          <p><label for="removeselect"><?php print_string('extusers', 'role'); ?></label></p>
          <?php $currentuserselector->display() ?>
      </td>
      <td id="buttonscell">
          <div id="addcontrols">
292
              <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
293

294
              <?php print_collapsible_region_start('', 'assignoptions', get_string('enrolmentoptions', 'role'),
295
                    'assignoptionscollapse', true); ?>
296
297

              <p><label for="extendperiod"><?php print_string('enrolperiod') ?></label><br />
298
              <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
299
300

              <p><label for="extendbase"><?php print_string('startingfrom') ?></label><br />
301
              <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
302
              <?php print_collapsible_region_end(); ?>
303
304
305
          </div>

          <div id="removecontrols">
306
              <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
307
308
309
310
311
312
313
314
          </div>
      </td>
      <td id="potentialcell">
          <p><label for="addselect"><?php print_string('potusers', 'role'); ?></label></p>
          <?php $potentialuserselector->display() ?>
      </td>
    </tr>
  </table>
315
</div></form>
316
317

<?php
318
    $PAGE->requires->js_init_call('M.core_role.init_add_assign_page');
319

320
321
322
323
    if (!empty($errors)) {
        $msg = '<p>';
        foreach ($errors as $e) {
            $msg .= $e.'<br />';
skodak's avatar
skodak committed
324
        }
325
326
327
328
329
        $msg .= '</p>';
        echo $OUTPUT->box_start();
        echo $OUTPUT->notification($msg);
        echo $OUTPUT->box_end();
    }
skodak's avatar
skodak committed
330

331
332
    // Print a form to swap roles, and a link back to the all roles list.
    echo '<div class="backlink">';
333

334
335
336
337
338
    $select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
    $select->label = get_string('assignanotherrole', 'role');
    echo $OUTPUT->render($select);
    echo '<p><a href="' . $PAGE->url . '">' . get_string('backtoallroles', 'role') . '</a></p>';
    echo '</div>';
339

340
341
342
} else if (empty($assignableroles)) {
    // Print a message that there are no roles that can me assigned here.
    echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'role'), 3);
343

344
345
} else {
    // Show UI for choosing a role to assign.
346

347
348
349
350
    // Print a warning if we are assigning system roles.
    if ($context->contextlevel == CONTEXT_SYSTEM) {
        echo $OUTPUT->box(get_string('globalroleswarning', 'role'));
    }
351

352
353
    // Print instruction
    echo $OUTPUT->heading(get_string('chooseroletoassign', 'role'), 3);
354

355
356
357
358
    // sync metacourse enrolments if needed
    if ($inmeta) {
        sync_metacourse($course);
    }
359

360
361
362
363
364
365
366
367
368
369
370
371
372
    // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users,
    // and so determine whether to show the extra column.
    $roleholdernames = array();
    $strmorethanmax = get_string('morethan', 'role', MAX_USERS_TO_LIST_PER_ROLE);
    $showroleholders = false;
    foreach ($assignableroles as $roleid => $notused) {
        $roleusers = '';
        if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
            $roleusers = get_role_users($roleid, $context, false, 'u.id, u.lastname, u.firstname');
            if (!empty($roleusers)) {
                $strroleusers = array();
                foreach ($roleusers as $user) {
                    $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>';
373
                }
374
375
                $roleholdernames[$roleid] = implode('<br />', $strroleusers);
                $showroleholders = true;
376
            }
377
378
379
380
381
        } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) {
            $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
            $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
        } else {
            $roleholdernames[$roleid] = '';
382
        }
383
    }
384

385
386
387
388
389
390
391
392
393
394
395
396
    // Print overview table
    $table = new html_table();
    $table->tablealign = 'center';
    $table->width = '60%';
    $table->head = array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'role'));
    $table->wrap = array('nowrap', '', 'nowrap');
    $table->align = array('left', 'left', 'center');
    if ($showroleholders) {
        $table->headspan = array(1, 1, 2);
        $table->wrap[] = 'nowrap';
        $table->align[] = 'left';
    }
397

398
399
400
401
402
403
404
    foreach ($assignableroles as $roleid => $rolename) {
        $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
        $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
        $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
                $description, $assigncounts[$roleid]);
        if ($showroleholders) {
            $row[] = $roleholdernames[$roleid];
405
        }
406
407
        $table->data[] = $row;
    }
408

409
    echo html_writer::table($table);
410

411
    if ($context->contextlevel > CONTEXT_USER) {
412
413
414
        echo html_writer::start_tag('div', array('class'=>'backlink'));
        echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
        echo html_writer::end_tag('div');
moodler's avatar
moodler committed
415
    }
416
}
417

418
echo $OUTPUT->footer();