Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/cohort/ -> edit.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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.'/course/lib.php');
  27  require_once($CFG->dirroot.'/cohort/lib.php');
  28  require_once($CFG->dirroot.'/cohort/edit_form.php');
  29  
  30  $id        = optional_param('id', 0, PARAM_INT);
  31  $contextid = optional_param('contextid', 0, PARAM_INT);
  32  $delete    = optional_param('delete', 0, PARAM_BOOL);
  33  $show      = optional_param('show', 0, PARAM_BOOL);
  34  $hide      = optional_param('hide', 0, PARAM_BOOL);
  35  $confirm   = optional_param('confirm', 0, PARAM_BOOL);
  36  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  37  
  38  require_login();
  39  
  40  $category = null;
  41  if ($id) {
  42      $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
  43      $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
  44  } else {
  45      $context = context::instance_by_id($contextid, MUST_EXIST);
  46      if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
  47          throw new \moodle_exception('invalidcontext');
  48      }
  49      $cohort = new stdClass();
  50      $cohort->id          = 0;
  51      $cohort->contextid   = $context->id;
  52      $cohort->name        = '';
  53      $cohort->description = '';
  54  }
  55  
  56  require_capability('moodle/cohort:manage', $context);
  57  
  58  if ($returnurl) {
  59      $returnurl = new moodle_url($returnurl);
  60  } else {
  61      $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
  62  }
  63  
  64  if (!empty($cohort->component)) {
  65      // We can not manually edit cohorts that were created by external systems, sorry.
  66      redirect($returnurl);
  67  }
  68  
  69  $PAGE->set_context($context);
  70  $baseurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id, 'id' => $cohort->id));
  71  $PAGE->set_url($baseurl);
  72  $PAGE->set_context($context);
  73  $PAGE->set_pagelayout('admin');
  74  
  75  if ($context->contextlevel == CONTEXT_COURSECAT) {
  76      core_course_category::page_setup();
  77      // Set the cohorts node active in the settings navigation block.
  78      if ($cohortsnode = $PAGE->settingsnav->find('cohort', navigation_node::TYPE_SETTING)) {
  79          $cohortsnode->make_active();
  80      }
  81  
  82      $PAGE->set_secondary_active_tab('cohort');
  83  
  84  } else {
  85      navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
  86      $PAGE->set_heading($COURSE->fullname);
  87  }
  88  
  89  if ($delete and $cohort->id) {
  90      $PAGE->url->param('delete', 1);
  91      if ($confirm and confirm_sesskey()) {
  92          cohort_delete_cohort($cohort);
  93          redirect($returnurl);
  94      }
  95      $strheading = get_string('delcohort', 'cohort');
  96      $PAGE->navbar->add($strheading);
  97      $PAGE->set_title($strheading);
  98      echo $OUTPUT->header();
  99      echo $OUTPUT->heading($strheading);
 100      $yesurl = new moodle_url('/cohort/edit.php', array('id' => $cohort->id, 'delete' => 1,
 101          'confirm' => 1, 'sesskey' => sesskey(), 'returnurl' => $returnurl->out_as_local_url()));
 102      $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
 103      echo $OUTPUT->confirm($message, $yesurl, $returnurl);
 104      echo $OUTPUT->footer();
 105      die;
 106  }
 107  
 108  if ($show && $cohort->id && confirm_sesskey()) {
 109      if (!$cohort->visible) {
 110          $record = (object)array('id' => $cohort->id, 'visible' => 1, 'contextid' => $cohort->contextid);
 111          cohort_update_cohort($record);
 112      }
 113      redirect($returnurl);
 114  }
 115  
 116  if ($hide && $cohort->id && confirm_sesskey()) {
 117      if ($cohort->visible) {
 118          $record = (object)array('id' => $cohort->id, 'visible' => 0, 'contextid' => $cohort->contextid);
 119          cohort_update_cohort($record);
 120      }
 121      redirect($returnurl);
 122  }
 123  
 124  $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
 125      'maxbytes' => $SITE->maxbytes, 'context' => $context);
 126  if ($cohort->id) {
 127      // Edit existing.
 128      $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions,
 129              $context, 'cohort', 'description', $cohort->id);
 130      $strheading = get_string('editcohort', 'cohort');
 131  
 132  } else {
 133      // Add new.
 134      $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions,
 135              $context, 'cohort', 'description', null);
 136      $strheading = get_string('addcohort', 'cohort');
 137  }
 138  
 139  $PAGE->set_title($strheading);
 140  $PAGE->navbar->add($strheading);
 141  
 142  $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort, 'returnurl'=>$returnurl));
 143  
 144  if ($editform->is_cancelled()) {
 145      redirect($returnurl);
 146  
 147  } else if ($data = $editform->get_data()) {
 148      $oldcontextid = $context->id;
 149      $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
 150  
 151      if ($data->id) {
 152          if ($data->contextid != $oldcontextid) {
 153              // Cohort was moved to another context.
 154              get_file_storage()->move_area_files_to_new_context($oldcontextid, $context->id,
 155                      'cohort', 'description', $data->id);
 156          }
 157          $data = file_postupdate_standard_editor($data, 'description', $editoroptions,
 158                  $context, 'cohort', 'description', $data->id);
 159          cohort_update_cohort($data);
 160      } else {
 161          $data->descriptionformat = $data->description_editor['format'];
 162          $data->description = $description = $data->description_editor['text'];
 163          $data->id = cohort_add_cohort($data);
 164          $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
 165          $data = file_postupdate_standard_editor($data, 'description', $editoroptions,
 166                  $context, 'cohort', 'description', $data->id);
 167          if ($description != $data->description) {
 168              $updatedata = (object)array('id' => $data->id,
 169                  'description' => $data->description, 'contextid' => $context->id);
 170              cohort_update_cohort($updatedata);
 171          }
 172      }
 173  
 174      if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) {
 175          // Redirect to where we were before.
 176          redirect($returnurl);
 177      } else {
 178          // Use new context id, it has been changed.
 179          redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
 180      }
 181  }
 182  
 183  echo $OUTPUT->header();
 184  echo $OUTPUT->heading($strheading);
 185  
 186  if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
 187      echo $OUTPUT->render($editcontrols);
 188  }
 189  
 190  echo $editform->display();
 191  echo $OUTPUT->footer();
 192