Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  •    1  <?php
       2  /**
       3  * script for bulk user multi enrol operations
       4  */
       5  
       6  die('this needs to be rewritten to use new enrol framework, sorry');  //TODO: MDL-24064
       7  
       8  require_once('../../config.php');
       9  require_once($CFG->libdir.'/adminlib.php');
      10  $processed = optional_param('processed', '', PARAM_BOOL);
      11  $sort = optional_param('sort', 'fullname', PARAM_ALPHA); //Sort by full name
      12  $dir  = optional_param('dir', 'asc', PARAM_ALPHA);       //Order to sort (ASC)
      13  
      14  require_login();
      15  admin_externalpage_setup('userbulk');
      16  require_capability('moodle/role:assign', context_system::instance()); //TODO: use some enrol cap
      17  $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
      18  //If no users selected then return to user_bulk.php
      19  if (empty($SESSION->bulk_users)) {
      20      redirect($return);
      21  }
      22  $users = $SESSION->bulk_users; //Get users to display
      23  $usertotal = get_users(false); //Total number of users registered
      24  $usercount = count($users);    //number of users
      25  
      26  echo $OUTPUT->header();
      27  
      28  //take user info
      29  foreach ($users as $key => $id) {
      30      $user = $DB->get_record('user', array('id'=>$id));
      31      $user->fullname = fullname($user, true);
      32      unset($user->firstname);
      33      unset($user->lastname);
      34      $users[$key] = $user;
      35  }
      36  
      37  // Need to sort by date
      38  function sort_compare($a, $b) {
      39      global $sort, $dir;
      40      if($sort == 'lastaccess') {
      41          $rez = $b->lastaccess - $a->lastaccess;
      42      } else {
      43          $rez = strcasecmp(@$a->$sort, @$b->$sort);
      44      }
      45      return $dir == 'desc' ? -$rez : $rez;
      46  }
      47  usort($users, 'sort_compare');
      48  
      49  //Take courses data (id, shortname, and fullname)
      50  $courses = get_courses_page(1, 'c.sortorder ASC', 'c.id,c.shortname,c.fullname,c.visible', $totalcount);
      51  $table = new html_table();
      52  $table->width = "95%";
      53  $columns = array('fullname');
      54  foreach ($courses as $v)
      55  {
      56      $columns[] = $v->shortname;
      57  }
      58  
      59  //Print columns headers from table
      60  foreach ($columns as $column) {
      61      $strtitle = $column;
      62      if ($sort != $column) {
      63          $columnicon = '';
      64          $columndir = 'asc';
      65      } else {
      66          $columndir = ($dir == 'asc') ? 'desc' : 'asc';
      67          $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ? 'down' : 'up' )).'" alt="" />';
      68      }
      69      $table->head[] = '<a href="user_bulk_enrol.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
      70      $table->align[] = 'left';
      71  }
      72  
      73  // process data submitting
      74  if(!empty($processed)) {
      75      //Process data form here
      76      $total = count($courses) * count($users);
      77  
      78      for ( $i = 0; $i < $total; $i++ )
      79      {
      80          $param = "selected".$i;
      81          $info = optional_param($param, '', PARAM_SEQUENCE);
      82          /**
      83           * user id:    ids[0]
      84           * course id:  ids[1]
      85           * enrol stat: ids[2]
      86           */
      87          $ids = explode(',', $info);
      88          if(!empty($ids[2])) {
      89              $context = context_course::instance($ids[1]);
      90              role_assign(5, $ids[0], $context->id); //TODO: horrible!!
      91          } else {
      92              if( empty($ids[1] ) ) {
      93                  continue;
      94              }
      95              $context = context_course::instance($ids[1]);
      96              role_unassign(5, $ids[0], $context->id);
      97          }
      98      }
      99      redirect($return, get_string('changessaved')); //TODO: horrible!!
     100  }
     101  
     102  //Form beginning
     103  echo '<form id="multienrol" name="multienrol" method="post" action="user_bulk_enrol.php">';
     104  echo '<input type="hidden" name="processed" value="yes" />';
     105  $count = 0;
     106  foreach($users as $user) {
     107      $temparray = array (
     108          '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>'
     109      );
     110      $mycourses = enrol_get_users_courses($user->id, false);
     111      foreach($courses as $acourse) {
     112          $state = '';
     113          if (isset($mycourses[$acourse->id])) {
     114              $state = 'checked="checked"';
     115          }
     116          $temparray[] = '<input type="hidden" name="selected' . $count .
     117                  '" value="' . $user->id . ',' . $acourse->id . ',0" />' .
     118                  '<input type="checkbox" name="selected' . $count .
     119                  '" value="' . $user->id . ',' . $acourse->id . ',1" ' . $state . '/>';
     120          $count++;
     121      }
     122      $table->data[] = $temparray;
     123  }
     124  echo $OUTPUT->heading("$usercount / $usertotal ".get_string('users'));
     125  echo html_writer::table($table);
     126  echo '<div class="continuebutton">';
     127  echo '<input type="submit" name="multienrolsubmit" value="save changes" />';
     128  echo '</div>';
     129  echo '</form>';
     130  
     131  echo $OUTPUT->footer();
    

    Search This Site: