[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/cohort/ -> 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   * Cohort enrolment plugin.
  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  /**
  28   * Cohort enrolment plugin implementation.
  29   * @author Petr Skoda
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class enrol_cohort_plugin extends enrol_plugin {
  33  
  34      /**
  35       * Is it possible to delete enrol instance via standard UI?
  36       *
  37       * @param stdClass $instance
  38       * @return bool
  39       */
  40      public function can_delete_instance($instance) {
  41          $context = context_course::instance($instance->courseid);
  42          return has_capability('enrol/cohort:config', $context);
  43      }
  44  
  45      /**
  46       * Returns localised name of enrol instance.
  47       *
  48       * @param stdClass $instance (null is accepted too)
  49       * @return string
  50       */
  51      public function get_instance_name($instance) {
  52          global $DB;
  53  
  54          if (empty($instance)) {
  55              $enrol = $this->get_name();
  56              return get_string('pluginname', 'enrol_'.$enrol);
  57  
  58          } else if (empty($instance->name)) {
  59              $enrol = $this->get_name();
  60              $cohort = $DB->get_record('cohort', array('id'=>$instance->customint1));
  61              if (!$cohort) {
  62                  return get_string('pluginname', 'enrol_'.$enrol);
  63              }
  64              $cohortname = format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid)));
  65              if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
  66                  $role = role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING));
  67                  return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ' - ' . $role .')';
  68              } else {
  69                  return get_string('pluginname', 'enrol_'.$enrol) . ' (' . $cohortname . ')';
  70              }
  71  
  72          } else {
  73              return format_string($instance->name, true, array('context'=>context_course::instance($instance->courseid)));
  74          }
  75      }
  76  
  77      /**
  78       * Returns link to page which may be used to add new instance of enrolment plugin in course.
  79       * @param int $courseid
  80       * @return moodle_url page url
  81       */
  82      public function get_newinstance_link($courseid) {
  83          if (!$this->can_add_new_instances($courseid)) {
  84              return NULL;
  85          }
  86          // Multiple instances supported - multiple parent courses linked.
  87          return new moodle_url('/enrol/cohort/edit.php', array('courseid'=>$courseid));
  88      }
  89  
  90      /**
  91       * Given a courseid this function returns true if the user is able to enrol or configure cohorts.
  92       * AND there are cohorts that the user can view.
  93       *
  94       * @param int $courseid
  95       * @return bool
  96       */
  97      protected function can_add_new_instances($courseid) {
  98          global $CFG;
  99          require_once($CFG->dirroot . '/cohort/lib.php');
 100          $coursecontext = context_course::instance($courseid);
 101          if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) {
 102              return false;
 103          }
 104          return cohort_get_available_cohorts($coursecontext) ? true : false;
 105      }
 106  
 107      /**
 108       * Returns edit icons for the page with list of instances.
 109       * @param stdClass $instance
 110       * @return array
 111       */
 112      public function get_action_icons(stdClass $instance) {
 113          global $OUTPUT;
 114  
 115          if ($instance->enrol !== 'cohort') {
 116              throw new coding_exception('invalid enrol instance!');
 117          }
 118          $context = context_course::instance($instance->courseid);
 119  
 120          $icons = array();
 121  
 122          if (has_capability('enrol/cohort:config', $context)) {
 123              $editlink = new moodle_url("/enrol/cohort/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
 124              $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
 125                      array('class' => 'iconsmall')));
 126          }
 127  
 128          return $icons;
 129      }
 130  
 131      /**
 132       * Called for all enabled enrol plugins that returned true from is_cron_required().
 133       * @return void
 134       */
 135      public function cron() {
 136          global $CFG;
 137  
 138          require_once("$CFG->dirroot/enrol/cohort/locallib.php");
 139          $trace = new null_progress_trace();
 140          enrol_cohort_sync($trace);
 141          $trace->finished();
 142      }
 143  
 144      /**
 145       * Called after updating/inserting course.
 146       *
 147       * @param bool $inserted true if course just inserted
 148       * @param stdClass $course
 149       * @param stdClass $data form data
 150       * @return void
 151       */
 152      public function course_updated($inserted, $course, $data) {
 153          // It turns out there is no need for cohorts to deal with this hook, see MDL-34870.
 154      }
 155  
 156      /**
 157       * Update instance status
 158       *
 159       * @param stdClass $instance
 160       * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
 161       * @return void
 162       */
 163      public function update_status($instance, $newstatus) {
 164          global $CFG;
 165  
 166          parent::update_status($instance, $newstatus);
 167  
 168          require_once("$CFG->dirroot/enrol/cohort/locallib.php");
 169          $trace = new null_progress_trace();
 170          enrol_cohort_sync($trace, $instance->courseid);
 171          $trace->finished();
 172      }
 173  
 174      /**
 175       * Does this plugin allow manual unenrolment of a specific user?
 176       * Yes, but only if user suspended...
 177       *
 178       * @param stdClass $instance course enrol instance
 179       * @param stdClass $ue record from user_enrolments table
 180       *
 181       * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
 182       */
 183      public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
 184          if ($ue->status == ENROL_USER_SUSPENDED) {
 185              return true;
 186          }
 187  
 188          return false;
 189      }
 190  
 191      /**
 192       * Gets an array of the user enrolment actions.
 193       *
 194       * @param course_enrolment_manager $manager
 195       * @param stdClass $ue A user enrolment object
 196       * @return array An array of user_enrolment_actions
 197       */
 198      public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
 199          $actions = array();
 200          $context = $manager->get_context();
 201          $instance = $ue->enrolmentinstance;
 202          $params = $manager->get_moodlepage()->url->params();
 203          $params['ue'] = $ue->id;
 204          if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/cohort:unenrol', $context)) {
 205              $url = new moodle_url('/enrol/unenroluser.php', $params);
 206              $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
 207          }
 208          return $actions;
 209      }
 210  
 211      /**
 212       * Restore instance and map settings.
 213       *
 214       * @param restore_enrolments_structure_step $step
 215       * @param stdClass $data
 216       * @param stdClass $course
 217       * @param int $oldid
 218       */
 219      public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
 220          global $DB, $CFG;
 221  
 222          if (!$step->get_task()->is_samesite()) {
 223              // No cohort restore from other sites.
 224              $step->set_mapping('enrol', $oldid, 0);
 225              return;
 226          }
 227  
 228          if (!empty($data->customint2)) {
 229              $data->customint2 = $step->get_mappingid('group', $data->customint2);
 230          }
 231  
 232          if ($data->roleid and $DB->record_exists('cohort', array('id'=>$data->customint1))) {
 233              $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
 234              if ($instance) {
 235                  $instanceid = $instance->id;
 236              } else {
 237                  $instanceid = $this->add_instance($course, (array)$data);
 238              }
 239              $step->set_mapping('enrol', $oldid, $instanceid);
 240  
 241              require_once("$CFG->dirroot/enrol/cohort/locallib.php");
 242              $trace = new null_progress_trace();
 243              enrol_cohort_sync($trace, $course->id);
 244              $trace->finished();
 245  
 246          } else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
 247              $data->customint1 = 0;
 248              $instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
 249  
 250              if ($instance) {
 251                  $instanceid = $instance->id;
 252              } else {
 253                  $data->status = ENROL_INSTANCE_DISABLED;
 254                  $instanceid = $this->add_instance($course, (array)$data);
 255              }
 256              $step->set_mapping('enrol', $oldid, $instanceid);
 257  
 258              require_once("$CFG->dirroot/enrol/cohort/locallib.php");
 259              $trace = new null_progress_trace();
 260              enrol_cohort_sync($trace, $course->id);
 261              $trace->finished();
 262  
 263          } else {
 264              $step->set_mapping('enrol', $oldid, 0);
 265          }
 266      }
 267  
 268      /**
 269       * Restore user enrolment.
 270       *
 271       * @param restore_enrolments_structure_step $step
 272       * @param stdClass $data
 273       * @param stdClass $instance
 274       * @param int $oldinstancestatus
 275       * @param int $userid
 276       */
 277      public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
 278          global $DB;
 279  
 280          if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) {
 281              // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
 282              return;
 283          }
 284  
 285          // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
 286          // but without roles and suspended.
 287  
 288          if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
 289              $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, ENROL_USER_SUSPENDED);
 290          }
 291      }
 292  
 293      /**
 294       * Restore user group membership.
 295       * @param stdClass $instance
 296       * @param int $groupid
 297       * @param int $userid
 298       */
 299      public function restore_group_member($instance, $groupid, $userid) {
 300          // Nothing to do here, the group members are added in $this->restore_group_restored()
 301          return;
 302      }
 303  
 304      /**
 305       * Is it possible to hide/show enrol instance via standard UI?
 306       *
 307       * @param stdClass $instance
 308       * @return bool
 309       */
 310      public function can_hide_show_instance($instance) {
 311          $context = context_course::instance($instance->courseid);
 312          return has_capability('enrol/cohort:config', $context);
 313      }
 314  }
 315  
 316  /**
 317   * Prevent removal of enrol roles.
 318   * @param int $itemid
 319   * @param int $groupid
 320   * @param int $userid
 321   * @return bool
 322   */
 323  function enrol_cohort_allow_group_member_remove($itemid, $groupid, $userid) {
 324      return false;
 325  }


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