/** * 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(); }
$params = array(); // Get the ID of the user to be displayed $transactionID = optional_param('id', 0, PARAM_INT); if (!!$transactionID) { $params['id'] = $transactionID; } $context = context_system::instance(); // Set PAGE variables $PAGE->set_context($context); $PAGE->set_url($CFG->wwwroot . '/local/moodec/pages/transaction/view.php', $params); $PAGE->navbar->add(get_string('transactions_title', 'local_moodec'), new moodle_url($CFG->wwwroot . '/local/moodec/pages/transaction/index.php')); $PAGE->navbar->add(get_string('transaction_view_title', 'local_moodec', array('id' => $transactionID))); // Force the user to login/create an account to access this page require_login(); $transaction = new MoodecTransaction($transactionID); if ((int) $USER->id !== $transaction->get_user_id()) { require_capability('local/moodec:viewalltransactions', $context); } // Check if the theme has a moodec pagelayout defined, otherwise use standard if (array_key_exists('moodec_transaction_view', $PAGE->theme->layouts)) { $PAGE->set_pagelayout('moodec_transaction_view'); } else { if (array_key_exists('moodec', $PAGE->theme->layouts)) { $PAGE->set_pagelayout('moodec'); } else { $PAGE->set_pagelayout('standard'); } } // Get the renderer for this page $renderer = $PAGE->get_renderer('local_moodec'); //needs to have the product verified before setting page heading & title