Ejemplo n.º 1
0
 /**
  * Goes through all the items in the transaction and enrols the user, 
  * given the product's duration. Also adds them into a group, if necessary.
  * @return void
  */
 protected function complete_enrolment()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/enrollib.php';
     require_once $CFG->dirroot . '/group/lib.php';
     // ENROL USER INTO EACH OF THE TRANSACTION ITEMS
     foreach ($this->_transaction->get_items() as $item) {
         $product = local_moodec_get_product($item->get_product_id());
         // We set the start time to be 1 min earlier (this is so the course will immediately show up in the course overview block - otherwise you need to wait til the current minute ticks over)
         $timestart = time() - 60;
         $timeend = 0;
         $instance = $DB->get_record('enrol', array('courseid' => $product->get_course_id(), 'enrol' => 'moodec'));
         if (!$instance) {
             // Notify admin that the enrolment method is not active on the course
             $this->send_error_to_admin("Moodec enrolment method not active on course " . $product->get_course_id() . ". Transaction #" . $this->_transaction->get_id() . " defaulted to manual enrolment method");
             // set the enrol plugin to use manual
             $this->_enrolPlugin = enrol_get_plugin('manual');
             // and get the manual enrolment method instance for the course instead
             $instance = $DB->get_record('enrol', array('courseid' => $product->get_course_id(), 'enrol' => 'manual'));
         }
         // Check if the product is simple, or variable
         // And retrieve the enrolment duration for this product
         if ($product->get_type() === PRODUCT_TYPE_SIMPLE) {
             $enrolmentDuration = $product->get_duration(false);
         } else {
             $enrolmentDuration = $product->get_variation($item->get_variation_id())->get_duration(false);
         }
         // If the course is not unlimited, set the duration to be the current time, plus the number of days, converted to seconds. (from product settings)
         if ($enrolmentDuration !== 0) {
             $timeend = $timestart + $enrolmentDuration * 86400;
         }
         // This will enrol the user! yay!
         // We set the user enrolment to be 'active', because any users that were previously
         // enrolled will be marked as 'suspended' automatically when their enrolment expires
         $this->_enrolPlugin->enrol_user($instance, $this->_transaction->get_user_id(), $instance->roleid, $timestart, $timeend, ENROL_USER_ACTIVE);
         // if there is a group set (ie NOT 0), then add them to it
         if ($product->get_type() === PRODUCT_TYPE_SIMPLE) {
             if (!!$product->get_group()) {
                 groups_add_member($product->get_group(), $this->_transaction->get_user_id());
             }
         } else {
             if (!!$product->get_variation($item->get_variation_id())->get_group()) {
                 groups_add_member($product->get_variation($item->get_variation_id())->get_group(), $this->_transaction->get_user_id());
             }
         }
     }
     // Mark the transaction as complete! :D
     $this->_transaction->complete();
 }