Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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   * script for bulk user multi cohort add
  19   *
  20   * @package    core
  21   * @subpackage user
  22   * @copyright  2011 Petr Skoda (http://skodak.org)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../../config.php');
  27  require_once($CFG->libdir.'/adminlib.php');
  28  require_once ('user_bulk_cohortadd_form.php');
  29  require_once("$CFG->dirroot/cohort/lib.php");
  30  
  31  $sort = optional_param('sort', 'fullname', PARAM_ALPHA);
  32  $dir  = optional_param('dir', 'asc', PARAM_ALPHA);
  33  
  34  admin_externalpage_setup('userbulk');
  35  require_capability('moodle/cohort:assign', context_system::instance());
  36  
  37  $users = $SESSION->bulk_users;
  38  
  39  $strnever = get_string('never');
  40  
  41  $cohorts = array(''=>get_string('choosedots'));
  42  $allcohorts = $DB->get_records('cohort', null, 'name');
  43  
  44  foreach ($allcohorts as $c) {
  45      if (!empty($c->component)) {
  46          // external cohorts can not be modified
  47          continue;
  48      }
  49      $context = context::instance_by_id($c->contextid);
  50      if (!has_capability('moodle/cohort:assign', $context)) {
  51          continue;
  52      }
  53  
  54      if (empty($c->idnumber)) {
  55          $cohorts[$c->id] = format_string($c->name);
  56      } else {
  57          $cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']';
  58      }
  59  }
  60  unset($allcohorts);
  61  
  62  if (count($cohorts) < 2) {
  63      redirect(new moodle_url('/admin/user/user_bulk.php'), get_string('bulknocohort', 'core_cohort'));
  64  }
  65  
  66  $countries = get_string_manager()->get_list_of_countries(true);
  67  $userfieldsapi = \core_user\fields::for_name();
  68  $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
  69  foreach ($users as $key => $id) {
  70      $user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
  71              email, country, lastaccess, city, deleted');
  72      $user->fullname = fullname($user, true);
  73      $user->country = @$countries[$user->country];
  74      unset($user->firstname);
  75      unset($user->lastname);
  76      $users[$key] = $user;
  77  }
  78  unset($countries);
  79  
  80  $mform = new user_bulk_cohortadd_form(null, $cohorts);
  81  
  82  if (empty($users) or $mform->is_cancelled()) {
  83      redirect(new moodle_url('/admin/user/user_bulk.php'));
  84  
  85  } else if ($data = $mform->get_data()) {
  86      // process request
  87      foreach ($users as $user) {
  88          if (!$user->deleted && !$DB->record_exists('cohort_members', array('cohortid' => $data->cohort, 'userid' => $user->id))) {
  89              cohort_add_member($data->cohort, $user->id);
  90          }
  91      }
  92      redirect(new moodle_url('/admin/user/user_bulk.php'));
  93  }
  94  
  95  // Need to sort by date
  96  function sort_compare($a, $b) {
  97      global $sort, $dir;
  98      if ($sort == 'lastaccess') {
  99          $rez = $b->lastaccess - $a->lastaccess;
 100      } else {
 101          $rez = strcasecmp(@$a->$sort, @$b->$sort);
 102      }
 103      return $dir == 'desc' ? -$rez : $rez;
 104  }
 105  usort($users, 'sort_compare');
 106  
 107  $table = new html_table();
 108  $table->width = "95%";
 109  $columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
 110  foreach ($columns as $column) {
 111      $strtitle = get_string($column);
 112      if ($sort != $column) {
 113          $columnicon = '';
 114          $columndir = 'asc';
 115      } else {
 116          $columndir = ($dir == 'asc') ? 'desc' : 'asc';
 117          $icon = 't/down';
 118          $iconstr = $columndir;
 119          if ($dir != 'asc') {
 120              $icon = 't/up';
 121          }
 122          $columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
 123      }
 124      $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
 125      $table->align[] = 'left';
 126  }
 127  
 128  foreach ($users as $user) {
 129      if ($user->deleted) {
 130          $table->data[] = array (
 131              $user->fullname,
 132              '',
 133              '',
 134              '',
 135              get_string('deleteduser', 'bulkusers')
 136          );
 137      } else {
 138          $table->data[] = array(
 139              '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . SITEID . '">' .
 140              $user->fullname .
 141              '</a>',
 142              s($user->email),
 143              $user->city,
 144              $user->country,
 145              $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
 146          );
 147      }
 148  }
 149  
 150  echo $OUTPUT->header();
 151  echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
 152  
 153  echo html_writer::table($table);
 154  
 155  echo $OUTPUT->box_start();
 156  $mform->display();
 157  echo $OUTPUT->box_end();
 158  
 159  echo $OUTPUT->footer();