Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  $namefields = get_all_user_name_fields(true);
  68  foreach ($users as $key => $id) {
  69      $user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
  70              email, country, lastaccess, city, deleted');
  71      $user->fullname = fullname($user, true);
  72      $user->country = @$countries[$user->country];
  73      unset($user->firstname);
  74      unset($user->lastname);
  75      $users[$key] = $user;
  76  }
  77  unset($countries);
  78  
  79  $mform = new user_bulk_cohortadd_form(null, $cohorts);
  80  
  81  if (empty($users) or $mform->is_cancelled()) {
  82      redirect(new moodle_url('/admin/user/user_bulk.php'));
  83  
  84  } else if ($data = $mform->get_data()) {
  85      // process request
  86      foreach ($users as $user) {
  87          if (!$user->deleted && !$DB->record_exists('cohort_members', array('cohortid' => $data->cohort, 'userid' => $user->id))) {
  88              cohort_add_member($data->cohort, $user->id);
  89          }
  90      }
  91      redirect(new moodle_url('/admin/user/user_bulk.php'));
  92  }
  93  
  94  // Need to sort by date
  95  function sort_compare($a, $b) {
  96      global $sort, $dir;
  97      if ($sort == 'lastaccess') {
  98          $rez = $b->lastaccess - $a->lastaccess;
  99      } else {
 100          $rez = strcasecmp(@$a->$sort, @$b->$sort);
 101      }
 102      return $dir == 'desc' ? -$rez : $rez;
 103  }
 104  usort($users, 'sort_compare');
 105  
 106  $table = new html_table();
 107  $table->width = "95%";
 108  $columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
 109  foreach ($columns as $column) {
 110      $strtitle = get_string($column);
 111      if ($sort != $column) {
 112          $columnicon = '';
 113          $columndir = 'asc';
 114      } else {
 115          $columndir = ($dir == 'asc') ? 'desc' : 'asc';
 116          $icon = 't/down';
 117          $iconstr = $columndir;
 118          if ($dir != 'asc') {
 119              $icon = 't/up';
 120          }
 121          $columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
 122      }
 123      $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
 124      $table->align[] = 'left';
 125  }
 126  
 127  foreach ($users as $user) {
 128      if ($user->deleted) {
 129          $table->data[] = array (
 130              $user->fullname,
 131              '',
 132              '',
 133              '',
 134              get_string('deleteduser', 'bulkusers')
 135          );
 136      } else {
 137          $table->data[] = array(
 138              '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . SITEID . '">' .
 139              $user->fullname .
 140              '</a>',
 141              s($user->email),
 142              $user->city,
 143              $user->country,
 144              $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
 145          );
 146      }
 147  }
 148  
 149  echo $OUTPUT->header();
 150  echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
 151  
 152  echo html_writer::table($table);
 153  
 154  echo $OUTPUT->box_start();
 155  $mform->display();
 156  echo $OUTPUT->box_end();
 157  
 158  echo $OUTPUT->footer();