Search moodle.org's
Developer Documentation

  • 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/ -> edit.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   * 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          print_error('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      $category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
      77      navigation_node::override_active_url(new moodle_url('/cohort/index.php', array('contextid'=>$cohort->contextid)));
      78  
      79  } else {
      80      navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
      81  }
      82  
      83  if ($delete and $cohort->id) {
      84      $PAGE->url->param('delete', 1);
      85      if ($confirm and confirm_sesskey()) {
      86          cohort_delete_cohort($cohort);
      87          redirect($returnurl);
      88      }
      89      $strheading = get_string('delcohort', 'cohort');
      90      $PAGE->navbar->add($strheading);
      91      $PAGE->set_title($strheading);
      92      $PAGE->set_heading($COURSE->fullname);
      93      echo $OUTPUT->header();
      94      echo $OUTPUT->heading($strheading);
      95      $yesurl = new moodle_url('/cohort/edit.php', array('id' => $cohort->id, 'delete' => 1,
      96          'confirm' => 1, 'sesskey' => sesskey(), 'returnurl' => $returnurl->out_as_local_url()));
      97      $message = get_string('delconfirm', 'cohort', format_string($cohort->name));
      98      echo $OUTPUT->confirm($message, $yesurl, $returnurl);
      99      echo $OUTPUT->footer();
     100      die;
     101  }
     102  
     103  if ($show && $cohort->id && confirm_sesskey()) {
     104      if (!$cohort->visible) {
     105          $record = (object)array('id' => $cohort->id, 'visible' => 1, 'contextid' => $cohort->contextid);
     106          cohort_update_cohort($record);
     107      }
     108      redirect($returnurl);
     109  }
     110  
     111  if ($hide && $cohort->id && confirm_sesskey()) {
     112      if ($cohort->visible) {
     113          $record = (object)array('id' => $cohort->id, 'visible' => 0, 'contextid' => $cohort->contextid);
     114          cohort_update_cohort($record);
     115      }
     116      redirect($returnurl);
     117  }
     118  
     119  $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
     120      'maxbytes' => $SITE->maxbytes, 'context' => $context);
     121  if ($cohort->id) {
     122      // Edit existing.
     123      $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions,
     124              $context, 'cohort', 'description', $cohort->id);
     125      $strheading = get_string('editcohort', 'cohort');
     126  
     127  } else {
     128      // Add new.
     129      $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions,
     130              $context, 'cohort', 'description', null);
     131      $strheading = get_string('addcohort', 'cohort');
     132  }
     133  
     134  $PAGE->set_title($strheading);
     135  $PAGE->set_heading($COURSE->fullname);
     136  $PAGE->navbar->add($strheading);
     137  
     138  $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort, 'returnurl'=>$returnurl));
     139  
     140  if ($editform->is_cancelled()) {
     141      redirect($returnurl);
     142  
     143  } else if ($data = $editform->get_data()) {
     144      $oldcontextid = $context->id;
     145      $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
     146  
     147      if ($data->id) {
     148          if ($data->contextid != $oldcontextid) {
     149              // Cohort was moved to another context.
     150              get_file_storage()->move_area_files_to_new_context($oldcontextid, $context->id,
     151                      'cohort', 'description', $data->id);
     152          }
     153          $data = file_postupdate_standard_editor($data, 'description', $editoroptions,
     154                  $context, 'cohort', 'description', $data->id);
     155          cohort_update_cohort($data);
     156      } else {
     157          $data->descriptionformat = $data->description_editor['format'];
     158          $data->description = $description = $data->description_editor['text'];
     159          $data->id = cohort_add_cohort($data);
     160          $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
     161          $data = file_postupdate_standard_editor($data, 'description', $editoroptions,
     162                  $context, 'cohort', 'description', $data->id);
     163          if ($description != $data->description) {
     164              $updatedata = (object)array('id' => $data->id,
     165                  'description' => $data->description, 'contextid' => $context->id);
     166              cohort_update_cohort($updatedata);
     167          }
     168      }
     169  
     170      if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) {
     171          // Redirect to where we were before.
     172          redirect($returnurl);
     173      } else {
     174          // Use new context id, it has been changed.
     175          redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
     176      }
     177  }
     178  
     179  echo $OUTPUT->header();
     180  echo $OUTPUT->heading($strheading);
     181  
     182  if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
     183      echo $OUTPUT->render($editcontrols);
     184  }
     185  
     186  echo $editform->display();
     187  echo $OUTPUT->footer();
     188