[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/meta/ -> lib.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   * Meta course enrolment plugin.
  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  /**
  28   * Meta course enrolment plugin.
  29   * @author Petr Skoda
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class enrol_meta_plugin extends enrol_plugin {
  33  
  34      /**
  35       * Returns localised name of enrol instance
  36       *
  37       * @param stdClass $instance (null is accepted too)
  38       * @return string
  39       */
  40      public function get_instance_name($instance) {
  41          global $DB;
  42  
  43          if (empty($instance)) {
  44              $enrol = $this->get_name();
  45              return get_string('pluginname', 'enrol_'.$enrol);
  46          } else if (empty($instance->name)) {
  47              $enrol = $this->get_name();
  48              $course = $DB->get_record('course', array('id'=>$instance->customint1));
  49              if ($course) {
  50                  $coursename = format_string(get_course_display_name_for_list($course));
  51              } else {
  52                  // Use course id, if course is deleted.
  53                  $coursename = $instance->customint1;
  54              }
  55              return get_string('pluginname', 'enrol_' . $enrol) . ' (' . $coursename . ')';
  56          } else {
  57              return format_string($instance->name);
  58          }
  59      }
  60  
  61      /**
  62       * Returns link to page which may be used to add new instance of enrolment plugin in course.
  63       * @param int $courseid
  64       * @return moodle_url page url
  65       */
  66      public function get_newinstance_link($courseid) {
  67          $context = context_course::instance($courseid, MUST_EXIST);
  68          if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/meta:config', $context)) {
  69              return NULL;
  70          }
  71          // multiple instances supported - multiple parent courses linked
  72          return new moodle_url('/enrol/meta/addinstance.php', array('id'=>$courseid));
  73      }
  74  
  75      /**
  76       * Does this plugin allow manual unenrolment of a specific user?
  77       * Yes, but only if user suspended...
  78       *
  79       * @param stdClass $instance course enrol instance
  80       * @param stdClass $ue record from user_enrolments table
  81       *
  82       * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
  83       */
  84      public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
  85          if ($ue->status == ENROL_USER_SUSPENDED) {
  86              return true;
  87          }
  88  
  89          return false;
  90      }
  91  
  92      /**
  93       * Gets an array of the user enrolment actions
  94       *
  95       * @param course_enrolment_manager $manager
  96       * @param stdClass $ue A user enrolment object
  97       * @return array An array of user_enrolment_actions
  98       */
  99      public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
 100          $actions = array();
 101          $context = $manager->get_context();
 102          $instance = $ue->enrolmentinstance;
 103          $params = $manager->get_moodlepage()->url->params();
 104          $params['ue'] = $ue->id;
 105          if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/meta:unenrol', $context)) {
 106              $url = new moodle_url('/enrol/unenroluser.php', $params);
 107              $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
 108          }
 109          return $actions;
 110      }
 111  
 112      /**
 113       * Called after updating/inserting course.
 114       *
 115       * @param bool $inserted true if course just inserted
 116       * @param stdClass $course
 117       * @param stdClass $data form data
 118       * @return void
 119       */
 120      public function course_updated($inserted, $course, $data) {
 121          // Meta sync updates are slow, if enrolments get out of sync teacher will have to wait till next cron.
 122          // We should probably add some sync button to the course enrol methods overview page.
 123      }
 124  
 125      /**
 126       * Update instance status
 127       *
 128       * @param stdClass $instance
 129       * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
 130       * @return void
 131       */
 132      public function update_status($instance, $newstatus) {
 133          global $CFG;
 134  
 135          parent::update_status($instance, $newstatus);
 136  
 137          require_once("$CFG->dirroot/enrol/meta/locallib.php");
 138          enrol_meta_sync($instance->courseid);
 139      }
 140  
 141      /**
 142       * Called for all enabled enrol plugins that returned true from is_cron_required().
 143       * @return void
 144       */
 145      public function cron() {
 146          global $CFG;
 147  
 148          require_once("$CFG->dirroot/enrol/meta/locallib.php");
 149          enrol_meta_sync();
 150      }
 151  
 152      /**
 153       * Is it possible to delete enrol instance via standard UI?
 154       *
 155       * @param stdClass $instance
 156       * @return bool
 157       */
 158      public function can_delete_instance($instance) {
 159          $context = context_course::instance($instance->courseid);
 160          return has_capability('enrol/meta:config', $context);
 161      }
 162  
 163      /**
 164       * Is it possible to hide/show enrol instance via standard UI?
 165       *
 166       * @param stdClass $instance
 167       * @return bool
 168       */
 169      public function can_hide_show_instance($instance) {
 170          $context = context_course::instance($instance->courseid);
 171          return has_capability('enrol/meta:config', $context);
 172      }
 173  }


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