[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/meta/ -> addinstance_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_meta
  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_meta_addinstance_form extends moodleform {
  30      protected $course;
  31  
  32      function definition() {
  33          global $CFG, $DB;
  34  
  35          $mform  = $this->_form;
  36          $course = $this->_customdata;
  37          $this->course = $course;
  38  
  39          $existing = $DB->get_records('enrol', array('enrol'=>'meta', 'courseid'=>$course->id), '', 'customint1, id');
  40  
  41          // TODO: this has to be done via ajax or else it will fail very badly on large sites!
  42          $courses = array('' => get_string('choosedots'));
  43          $select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
  44          $join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
  45          $sql = "SELECT c.id, c.fullname, c.shortname, c.visible $select FROM {course} c $join ORDER BY c.sortorder ASC";
  46          $rs = $DB->get_recordset_sql($sql, array('contextlevel' => CONTEXT_COURSE));
  47          foreach ($rs as $c) {
  48              if ($c->id == SITEID or $c->id == $course->id or isset($existing[$c->id])) {
  49                  continue;
  50              }
  51              context_helper::preload_from_record($c);
  52              $coursecontext = context_course::instance($c->id);
  53              if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
  54                  continue;
  55              }
  56              if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
  57                  continue;
  58              }
  59              $courses[$c->id] = $coursecontext->get_context_name(false);
  60          }
  61          $rs->close();
  62  
  63          $mform->addElement('header','general', get_string('pluginname', 'enrol_meta'));
  64  
  65          $mform->addElement('select', 'link', get_string('linkedcourse', 'enrol_meta'), $courses);
  66          $mform->addRule('link', get_string('required'), 'required', null, 'client');
  67  
  68          $mform->addElement('hidden', 'id', null);
  69          $mform->setType('id', PARAM_INT);
  70  
  71          $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
  72  
  73          $this->set_data(array('id'=>$course->id));
  74      }
  75  
  76      function validation($data, $files) {
  77          global $DB, $CFG;
  78  
  79          // TODO: this is duplicated here because it may be necessary once we implement ajax course selection element
  80  
  81          $errors = parent::validation($data, $files);
  82          if (!$c = $DB->get_record('course', array('id'=>$data['link']))) {
  83              $errors['link'] = get_string('required');
  84          } else {
  85              $coursecontext = context_course::instance($c->id);
  86              $existing = $DB->get_records('enrol', array('enrol'=>'meta', 'courseid'=>$this->course->id), '', 'customint1, id');
  87              if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
  88                  $errors['link'] = get_string('error');
  89              } else if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
  90                  $errors['link'] = get_string('error');
  91              } else if ($c->id == SITEID or $c->id == $this->course->id or isset($existing[$c->id])) {
  92                  $errors['link'] = get_string('error');
  93              }
  94          }
  95  
  96          return $errors;
  97      }
  98  }
  99  


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