[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/cohort/ -> edit_form.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   * Adds instance form
  19   *
  20   * @package    enrol_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  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->libdir/formslib.php");
  28  
  29  class enrol_cohort_edit_form extends moodleform {
  30  
  31      function definition() {
  32          global $CFG, $DB;
  33  
  34          $mform  = $this->_form;
  35  
  36          list($instance, $plugin, $course) = $this->_customdata;
  37          $coursecontext = context_course::instance($course->id);
  38  
  39          $enrol = enrol_get_plugin('cohort');
  40  
  41  
  42          $groups = array(0 => get_string('none'));
  43          foreach (groups_get_all_groups($course->id) as $group) {
  44              $groups[$group->id] = format_string($group->name, true, array('context'=>$coursecontext));
  45          }
  46  
  47          $mform->addElement('header','general', get_string('pluginname', 'enrol_cohort'));
  48  
  49          $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
  50          $mform->setType('name', PARAM_TEXT);
  51  
  52          $options = array(ENROL_INSTANCE_ENABLED  => get_string('yes'),
  53                           ENROL_INSTANCE_DISABLED => get_string('no'));
  54          $mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options);
  55  
  56          if ($instance->id) {
  57              if ($cohort = $DB->get_record('cohort', array('id'=>$instance->customint1))) {
  58                  $cohorts = array($instance->customint1=>format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid))));
  59              } else {
  60                  $cohorts = array($instance->customint1=>get_string('error'));
  61              }
  62              $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
  63              $mform->setConstant('customint1', $instance->customint1);
  64              $mform->hardFreeze('customint1', $instance->customint1);
  65  
  66          } else {
  67              $cohorts = array('' => get_string('choosedots'));
  68              $allcohorts = cohort_get_available_cohorts($coursecontext);
  69              foreach ($allcohorts as $c) {
  70                  $cohorts[$c->id] = format_string($c->name);
  71              }
  72              $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
  73              $mform->addRule('customint1', get_string('required'), 'required', null, 'client');
  74          }
  75  
  76          $roles = get_assignable_roles($coursecontext);
  77          $roles[0] = get_string('none');
  78          $roles = array_reverse($roles, true); // Descending default sortorder.
  79          $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles);
  80          $mform->setDefault('roleid', $enrol->get_config('roleid'));
  81          if ($instance->id and !isset($roles[$instance->roleid])) {
  82              if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
  83                  $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS, true);
  84                  $roles[$instance->roleid] = role_get_name($role, $coursecontext);
  85              } else {
  86                  $roles[$instance->roleid] = get_string('error');
  87              }
  88          }
  89          $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups);
  90  
  91          $mform->addElement('hidden', 'courseid', null);
  92          $mform->setType('courseid', PARAM_INT);
  93  
  94          $mform->addElement('hidden', 'id', null);
  95          $mform->setType('id', PARAM_INT);
  96  
  97          if ($instance->id) {
  98              $this->add_action_buttons(true);
  99          } else {
 100              $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
 101          }
 102  
 103          $this->set_data($instance);
 104      }
 105  
 106      function validation($data, $files) {
 107          global $DB;
 108  
 109          $errors = parent::validation($data, $files);
 110  
 111          $params = array('roleid'=>$data['roleid'], 'customint1'=>$data['customint1'], 'courseid'=>$data['courseid'], 'id'=>$data['id']);
 112          if ($DB->record_exists_select('enrol', "roleid = :roleid AND customint1 = :customint1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id", $params)) {
 113              $errors['roleid'] = get_string('instanceexists', 'enrol_cohort');
 114          }
 115  
 116          return $errors;
 117      }
 118  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1