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.
/cohort/ -> index.php (source)

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

   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   * Cohort related management functions, this file needs to be included manually.
  19   *
  20   * @package    core_cohort
  21   * @copyright  2010 Petr Skoda  {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../config.php');
  26  require_once($CFG->dirroot.'/cohort/lib.php');
  27  require_once($CFG->libdir.'/adminlib.php');
  28  
  29  $contextid = optional_param('contextid', 0, PARAM_INT);
  30  $page = optional_param('page', 0, PARAM_INT);
  31  $searchquery  = optional_param('search', '', PARAM_RAW);
  32  $showall = optional_param('showall', false, PARAM_BOOL);
  33  
  34  require_login();
  35  
  36  if ($contextid) {
  37      $context = context::instance_by_id($contextid, MUST_EXIST);
  38  } else {
  39      $context = context_system::instance();
  40  }
  41  
  42  if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
  43      print_error('invalidcontext');
  44  }
  45  
  46  $category = null;
  47  if ($context->contextlevel == CONTEXT_COURSECAT) {
  48      $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
  49  }
  50  
  51  $manager = has_capability('moodle/cohort:manage', $context);
  52  $canassign = has_capability('moodle/cohort:assign', $context);
  53  if (!$manager) {
  54      require_capability('moodle/cohort:view', $context);
  55  }
  56  
  57  $strcohorts = get_string('cohorts', 'cohort');
  58  
  59  if ($category) {
  60      $PAGE->set_pagelayout('admin');
  61      $PAGE->set_context($context);
  62      $PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
  63      $PAGE->set_title($strcohorts);
  64      $PAGE->set_heading($COURSE->fullname);
  65      $showall = false;
  66  } else {
  67      admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
  68  }
  69  
  70  echo $OUTPUT->header();
  71  
  72  if ($showall) {
  73      $cohorts = cohort_get_all_cohorts($page, 25, $searchquery);
  74  } else {
  75      $cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
  76  }
  77  
  78  $count = '';
  79  if ($cohorts['allcohorts'] > 0) {
  80      if ($searchquery === '') {
  81          $count = ' ('.$cohorts['allcohorts'].')';
  82      } else {
  83          $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
  84      }
  85  }
  86  
  87  echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
  88  
  89  $params = array('page' => $page);
  90  if ($contextid) {
  91      $params['contextid'] = $contextid;
  92  }
  93  if ($searchquery) {
  94      $params['search'] = $searchquery;
  95  }
  96  if ($showall) {
  97      $params['showall'] = true;
  98  }
  99  $baseurl = new moodle_url('/cohort/index.php', $params);
 100  
 101  if ($editcontrols = cohort_edit_controls($context, $baseurl)) {
 102      echo $OUTPUT->render($editcontrols);
 103  }
 104  
 105  // Add search form.
 106  $hiddenfields = [
 107      (object) ['name' => 'contextid', 'value' => $contextid],
 108      (object) ['name' => 'showall', 'value' => $showall]
 109  ];
 110  
 111  $data = [
 112      'action' => new moodle_url('/cohort/index.php'),
 113      'inputname' => 'search',
 114      'searchstring' => get_string('search', 'cohort'),
 115      'query' => $searchquery,
 116      'hiddenfields' => $hiddenfields,
 117      'extraclasses' => 'mb-3'
 118  ];
 119  
 120  echo $OUTPUT->render_from_template('core/search_input', $data);
 121  
 122  // Output pagination bar.
 123  echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
 124  
 125  $data = array();
 126  $editcolumnisempty = true;
 127  foreach($cohorts['cohorts'] as $cohort) {
 128      $line = array();
 129      $cohortcontext = context::instance_by_id($cohort->contextid);
 130      $cohort->description = file_rewrite_pluginfile_urls($cohort->description, 'pluginfile.php', $cohortcontext->id,
 131              'cohort', 'description', $cohort->id);
 132      if ($showall) {
 133          if ($cohortcontext->contextlevel == CONTEXT_COURSECAT) {
 134              $line[] = html_writer::link(new moodle_url('/cohort/index.php' ,
 135                      array('contextid' => $cohort->contextid)), $cohortcontext->get_context_name(false));
 136          } else {
 137              $line[] = $cohortcontext->get_context_name(false);
 138          }
 139      }
 140      $tmpl = new \core_cohort\output\cohortname($cohort);
 141      $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
 142      $tmpl = new \core_cohort\output\cohortidnumber($cohort);
 143      $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
 144      $line[] = format_text($cohort->description, $cohort->descriptionformat);
 145  
 146      $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
 147  
 148      if (empty($cohort->component)) {
 149          $line[] = get_string('nocomponent', 'cohort');
 150      } else {
 151          $line[] = get_string('pluginname', $cohort->component);
 152      }
 153  
 154      $buttons = array();
 155      if (empty($cohort->component)) {
 156          $cohortmanager = has_capability('moodle/cohort:manage', $cohortcontext);
 157          $cohortcanassign = has_capability('moodle/cohort:assign', $cohortcontext);
 158  
 159          $urlparams = array('id' => $cohort->id, 'returnurl' => $baseurl->out_as_local_url(false));
 160          $showhideurl = new moodle_url('/cohort/edit.php', $urlparams + array('sesskey' => sesskey()));
 161          if ($cohortmanager) {
 162              if ($cohort->visible) {
 163                  $showhideurl->param('hide', 1);
 164                  $visibleimg = $OUTPUT->pix_icon('t/hide', get_string('hide'));
 165                  $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('hide')));
 166              } else {
 167                  $showhideurl->param('show', 1);
 168                  $visibleimg = $OUTPUT->pix_icon('t/show', get_string('show'));
 169                  $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('show')));
 170              }
 171              $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams + array('delete' => 1)),
 172                  $OUTPUT->pix_icon('t/delete', get_string('delete')),
 173                  array('title' => get_string('delete')));
 174              $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams),
 175                  $OUTPUT->pix_icon('t/edit', get_string('edit')),
 176                  array('title' => get_string('edit')));
 177              $editcolumnisempty = false;
 178          }
 179          if ($cohortcanassign) {
 180              $buttons[] = html_writer::link(new moodle_url('/cohort/assign.php', $urlparams),
 181                  $OUTPUT->pix_icon('i/users', get_string('assign', 'core_cohort')),
 182                  array('title' => get_string('assign', 'core_cohort')));
 183              $editcolumnisempty = false;
 184          }
 185      }
 186      $line[] = implode(' ', $buttons);
 187  
 188      $data[] = $row = new html_table_row($line);
 189      if (!$cohort->visible) {
 190          $row->attributes['class'] = 'dimmed_text';
 191      }
 192  }
 193  $table = new html_table();
 194  $table->head  = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
 195                        get_string('memberscount', 'cohort'), get_string('component', 'cohort'));
 196  $table->colclasses = array('leftalign name', 'leftalign id', 'leftalign description', 'leftalign size','centeralign source');
 197  if ($showall) {
 198      array_unshift($table->head, get_string('category'));
 199      array_unshift($table->colclasses, 'leftalign category');
 200  }
 201  if (!$editcolumnisempty) {
 202      $table->head[] = get_string('edit');
 203      $table->colclasses[] = 'centeralign action';
 204  } else {
 205      // Remove last column from $data.
 206      foreach ($data as $row) {
 207          array_pop($row->cells);
 208      }
 209  }
 210  $table->id = 'cohorts';
 211  $table->attributes['class'] = 'admintable generaltable';
 212  $table->data  = $data;
 213  echo html_writer::table($table);
 214  echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
 215  echo $OUTPUT->footer();