[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/user/ -> user_bulk_cohortadd.php (source)

   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, 'deleted'=>0), 'id, ' . $namefields . ', username,
  70              email, country, lastaccess, city');
  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 (!$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          $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ? 'down' : 'up' )).'" alt="" />';
 117      }
 118      $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
 119      $table->align[] = 'left';
 120  }
 121  
 122  foreach ($users as $user) {
 123      $table->data[] = array (
 124          '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
 125          $user->email,
 126          $user->city,
 127          $user->country,
 128          $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
 129      );
 130  }
 131  
 132  echo $OUTPUT->header();
 133  echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
 134  
 135  echo html_writer::table($table);
 136  
 137  echo $OUTPUT->box_start();
 138  $mform->display();
 139  echo $OUTPUT->box_end();
 140  
 141  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1