MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / cohortroles / index.php
CommitLineData
08f4278c
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Assign roles for a user.
19 *
20 * @package tool_cohortroles
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
1fcf0ca8 25require_once(__DIR__ . '/../../../config.php');
08f4278c
DW
26require_once($CFG->libdir.'/adminlib.php');
27require_once($CFG->libdir.'/tablelib.php');
28
29$removeid = optional_param('removecohortroleassignment', 0, PARAM_INT);
30
31admin_externalpage_setup('toolcohortroles');
32$context = context_system::instance();
33
34$pageurl = new moodle_url('/admin/tool/cohortroles/index.php');
35
55487897 36$output = $PAGE->get_renderer('tool_cohortroles');
08f4278c 37
55487897 38echo $output->header();
08f4278c 39$title = get_string('assignroletocohort', 'tool_cohortroles');
55487897 40echo $output->heading($title);
08f4278c
DW
41
42$form = new tool_cohortroles\form\assign_role_cohort();
43
44if ($removeid) {
45 require_sesskey();
46
47 $result = \tool_cohortroles\api::delete_cohort_role_assignment($removeid);
48 if ($result) {
49 $notification = get_string('cohortroleassignmentremoved', 'tool_cohortroles');
55487897 50 echo $output->notify_success($notification);
08f4278c
DW
51 } else {
52 $notification = get_string('cohortroleassignmentnotremoved', 'tool_cohortroles');
55487897 53 echo $output->notify_problem($notification);
08f4278c 54 }
55487897 55 echo $output->continue_button(new moodle_url($pageurl));
08f4278c
DW
56} else if ($data = $form->get_data()) {
57 require_sesskey();
58 // We must create them all or none.
59 $saved = 0;
d238016e
JP
60 // Loop through userids and cohortids only if both of them are not empty.
61 if (!empty($data->userids) && !empty($data->cohortids)) {
62 foreach ($data->userids as $userid) {
63 foreach ($data->cohortids as $cohortid) {
64 $params = (object) array('userid' => $userid, 'cohortid' => $cohortid, 'roleid' => $data->roleid);
65 $result = \tool_cohortroles\api::create_cohort_role_assignment($params);
66 if ($result) {
67 $saved++;
68 }
08f4278c
DW
69 }
70 }
71 }
72 if ($saved == 0) {
73 $notification = get_string('nocohortroleassignmentssaved', 'tool_cohortroles');
55487897 74 echo $output->notify_problem($notification);
08f4278c
DW
75 } else if ($saved == 1) {
76 $notification = get_string('onecohortroleassignmentsaved', 'tool_cohortroles');
55487897 77 echo $output->notify_success($notification);
08f4278c
DW
78 } else {
79 $notification = get_string('acohortroleassignmentssaved', 'tool_cohortroles', $saved);
55487897 80 echo $output->notify_success($notification);
08f4278c
DW
81 }
82
55487897 83 echo $output->continue_button(new moodle_url($pageurl));
08f4278c
DW
84} else {
85 $form->display();
86
87 $title = get_string('existingcohortroles', 'tool_cohortroles');
55487897 88 echo $output->heading($title);
08f4278c
DW
89 $url = new moodle_url('/admin/tool/cohortroles/index.php');
90 $table = new tool_cohortroles\output\cohort_role_assignments_table(uniqid(), $url);
91 echo $table->out(50, true);
92
55487897
IT
93 echo $output->spacer();
94 echo $output->notify_message(get_string('backgroundsync', 'tool_cohortroles'));
08f4278c
DW
95}
96
55487897 97echo $output->footer();
08f4278c 98