Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir.'/adminlib.php');
  27  require_once($CFG->libdir.'/tablelib.php');
  28  
  29  $removeid = optional_param('removecohortroleassignment', 0, PARAM_INT);
  30  
  31  admin_externalpage_setup('toolcohortroles');
  32  $context = context_system::instance();
  33  
  34  $pageurl = new moodle_url('/admin/tool/cohortroles/index.php');
  35  
  36  $output = $PAGE->get_renderer('tool_cohortroles');
  37  
  38  echo $output->header();
  39  $title = get_string('assignroletocohort', 'tool_cohortroles');
  40  echo $output->heading($title);
  41  
  42  $form = new tool_cohortroles\form\assign_role_cohort();
  43  
  44  if ($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');
  50          echo $output->notify_success($notification);
  51      } else {
  52          $notification = get_string('cohortroleassignmentnotremoved', 'tool_cohortroles');
  53          echo $output->notify_problem($notification);
  54      }
  55      echo $output->continue_button(new moodle_url($pageurl));
  56  } else if ($data = $form->get_data()) {
  57      require_sesskey();
  58      // We must create them all or none.
  59      $saved = 0;
  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                  }
  69              }
  70          }
  71      }
  72      if ($saved == 0) {
  73          $notification = get_string('nocohortroleassignmentssaved', 'tool_cohortroles');
  74          echo $output->notify_problem($notification);
  75      } else if ($saved == 1) {
  76          $notification = get_string('onecohortroleassignmentsaved', 'tool_cohortroles');
  77          echo $output->notify_success($notification);
  78      } else {
  79          $notification = get_string('acohortroleassignmentssaved', 'tool_cohortroles', $saved);
  80          echo $output->notify_success($notification);
  81      }
  82  
  83      echo $output->continue_button(new moodle_url($pageurl));
  84  } else {
  85      $form->display();
  86  
  87      $title = get_string('existingcohortroles', 'tool_cohortroles');
  88      echo $output->heading($title);
  89  
  90      $table = new tool_cohortroles\output\cohort_role_assignments_table('cohort-role-assignments', $pageurl);
  91      echo $table->out(50, true);
  92  
  93      echo $output->spacer();
  94      echo $output->notify_message(get_string('backgroundsync', 'tool_cohortroles'));
  95  }
  96  
  97  echo $output->footer();
  98