Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/cohort/ -> index.php (source)

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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   * 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  
  64      core_course_category::page_setup();
  65      // Set the cohorts node active in the settings navigation block.
  66      if ($cohortsnode = $PAGE->settingsnav->find('cohort', navigation_node::TYPE_SETTING)) {
  67          $cohortsnode->make_active();
  68      }
  69  
  70      $PAGE->set_title($strcohorts);
  71      $showall = false;
  72  } else {
  73      admin_externalpage_setup('cohorts', '', null, '', array('pagelayout'=>'report'));
  74      $PAGE->set_primary_active_tab('siteadminnode');
  75      if ($showall == 1) {
  76          $PAGE->navbar->add(get_string('allcohorts', 'cohort'), $PAGE->url);
  77      } else if (!$showall) {
  78          $PAGE->navbar->add(get_string('systemcohorts', 'cohort'), $PAGE->url);
  79      }
  80  }
  81  
  82  echo $OUTPUT->header();
  83  
  84  if ($showall) {
  85      $cohorts = cohort_get_all_cohorts($page, 25, $searchquery);
  86  } else {
  87      $cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);
  88  }
  89  
  90  $count = '';
  91  if ($cohorts['allcohorts'] > 0) {
  92      if ($searchquery === '') {
  93          $count = ' ('.$cohorts['allcohorts'].')';
  94      } else {
  95          $count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
  96      }
  97  }
  98  
  99  echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);
 100  
 101  $params = array('page' => $page);
 102  if ($contextid) {
 103      $params['contextid'] = $contextid;
 104  }
 105  if ($searchquery) {
 106      $params['search'] = $searchquery;
 107  }
 108  if ($showall) {
 109      $params['showall'] = true;
 110  }
 111  $baseurl = new moodle_url('/cohort/index.php', $params);
 112  
 113  if ($editcontrols = cohort_edit_controls($context, $baseurl)) {
 114      echo $OUTPUT->render($editcontrols);
 115  }
 116  
 117  // Add search form.
 118  $hiddenfields = [
 119      (object) ['name' => 'contextid', 'value' => $contextid],
 120      (object) ['name' => 'showall', 'value' => $showall]
 121  ];
 122  
 123  $data = [
 124      'action' => new moodle_url('/cohort/index.php'),
 125      'inputname' => 'search',
 126      'searchstring' => get_string('search', 'cohort'),
 127      'query' => $searchquery,
 128      'hiddenfields' => $hiddenfields,
 129      'extraclasses' => 'mb-3'
 130  ];
 131  
 132  echo $OUTPUT->render_from_template('core/search_input', $data);
 133  
 134  // Output pagination bar.
 135  echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
 136  
 137  $data = array();
 138  $editcolumnisempty = true;
 139  foreach($cohorts['cohorts'] as $cohort) {
 140      $line = array();
 141      $cohortcontext = context::instance_by_id($cohort->contextid);
 142      $cohort->description = file_rewrite_pluginfile_urls($cohort->description, 'pluginfile.php', $cohortcontext->id,
 143              'cohort', 'description', $cohort->id);
 144      if ($showall) {
 145          if ($cohortcontext->contextlevel == CONTEXT_COURSECAT) {
 146              $line[] = html_writer::link(new moodle_url('/cohort/index.php' ,
 147                      array('contextid' => $cohort->contextid)), $cohortcontext->get_context_name(false));
 148          } else {
 149              $line[] = $cohortcontext->get_context_name(false);
 150          }
 151      }
 152      $tmpl = new \core_cohort\output\cohortname($cohort);
 153      $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
 154      $tmpl = new \core_cohort\output\cohortidnumber($cohort);
 155      $line[] = $OUTPUT->render_from_template('core/inplace_editable', $tmpl->export_for_template($OUTPUT));
 156      $line[] = format_text($cohort->description, $cohort->descriptionformat);
 157  
 158      $line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
 159  
 160      if (empty($cohort->component)) {
 161          $line[] = get_string('nocomponent', 'cohort');
 162      } else {
 163          $line[] = get_string('pluginname', $cohort->component);
 164      }
 165  
 166      $buttons = array();
 167      if (empty($cohort->component)) {
 168          $cohortmanager = has_capability('moodle/cohort:manage', $cohortcontext);
 169          $cohortcanassign = has_capability('moodle/cohort:assign', $cohortcontext);
 170  
 171          $urlparams = array('id' => $cohort->id, 'returnurl' => $baseurl->out_as_local_url(false));
 172          $showhideurl = new moodle_url('/cohort/edit.php', $urlparams + array('sesskey' => sesskey()));
 173          if ($cohortmanager) {
 174              if ($cohort->visible) {
 175                  $showhideurl->param('hide', 1);
 176                  $visibleimg = $OUTPUT->pix_icon('t/hide', get_string('hide'));
 177                  $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('hide')));
 178              } else {
 179                  $showhideurl->param('show', 1);
 180                  $visibleimg = $OUTPUT->pix_icon('t/show', get_string('show'));
 181                  $buttons[] = html_writer::link($showhideurl, $visibleimg, array('title' => get_string('show')));
 182              }
 183              $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams + array('delete' => 1)),
 184                  $OUTPUT->pix_icon('t/delete', get_string('delete')),
 185                  array('title' => get_string('delete')));
 186              $buttons[] = html_writer::link(new moodle_url('/cohort/edit.php', $urlparams),
 187                  $OUTPUT->pix_icon('t/edit', get_string('edit')),
 188                  array('title' => get_string('edit')));
 189              $editcolumnisempty = false;
 190          }
 191          if ($cohortcanassign) {
 192              $buttons[] = html_writer::link(new moodle_url('/cohort/assign.php', $urlparams),
 193                  $OUTPUT->pix_icon('i/users', get_string('assign', 'core_cohort')),
 194                  array('title' => get_string('assign', 'core_cohort')));
 195              $editcolumnisempty = false;
 196          }
 197      }
 198      $line[] = implode(' ', $buttons);
 199  
 200      $data[] = $row = new html_table_row($line);
 201      if (!$cohort->visible) {
 202          $row->attributes['class'] = 'dimmed_text';
 203      }
 204  }
 205  $table = new html_table();
 206  $table->head  = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
 207                        get_string('memberscount', 'cohort'), get_string('component', 'cohort'));
 208  $table->colclasses = array('leftalign name', 'leftalign id', 'leftalign description', 'leftalign size','centeralign source');
 209  if ($showall) {
 210      array_unshift($table->head, get_string('category'));
 211      array_unshift($table->colclasses, 'leftalign category');
 212  }
 213  if (!$editcolumnisempty) {
 214      $table->head[] = get_string('edit');
 215      $table->colclasses[] = 'centeralign action';
 216  } else {
 217      // Remove last column from $data.
 218      foreach ($data as $row) {
 219          array_pop($row->cells);
 220      }
 221  }
 222  $table->id = 'cohorts';
 223  $table->attributes['class'] = 'admintable generaltable';
 224  $table->data  = $data;
 225  echo html_writer::table($table);
 226  echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);
 227  echo $OUTPUT->footer();