/**
  * Return contents of moodec_courses block
  *
  * @return stdClass contents of block
  */
 public function get_content()
 {
     global $USER, $CFG, $DB;
     require_once $CFG->dirroot . '/local/moodec/lib.php';
     if ($this->content !== NULL) {
         return $this->content;
     }
     // $config = get_config('block_moodec_courses');
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     if (!empty($this->config)) {
         // Check config for which courses to select
         if ((int) $this->config->course_selection === 0) {
             // Return latest courses
             $products = local_moodec_get_products(-1, null, 'timecreated', 'DESC');
             $products = array_slice($products, 0, $this->config->courses_shown);
         } elseif ((int) $this->config->course_selection === 1) {
             // Return random courses
             $products = local_moodec_get_random_products($this->config->courses_shown);
         } else {
             // TODO: Add return manual courses
             $products = array();
             for ($i = 1; $i <= $this->config->courses_shown; $i++) {
                 $manual = 'manual_course_' . $i;
                 $newProduct = local_moodec_get_product((int) $this->config->{$manual});
                 if (!!$newProduct) {
                     $products[] = $newProduct;
                 }
             }
         }
         // Render HTML
         $renderer = $this->page->get_renderer('block_moodec_courses');
         $this->content->text = $renderer->output_products($products, $this->config);
     }
     return $this->content;
 }
Beispiel #2
0
if (isset($_POST['action']) && $_POST['action'] === 'removeFromCart') {
    // Updates the cart var with the new addition
    $cart->remove($_POST['id']);
    // redirect back to the course page
    redirect(new moodle_url($CFG->wwwroot . '/local/moodec/pages/cart.php'));
}
if (isset($_POST['action']) && $_POST['action'] === 'emptyCart') {
    // Updates the cart var with the new addition
    $cart->clear();
    // redirect back to the course page
    redirect(new moodle_url($CFG->wwwroot . '/local/moodec/pages/catalogue.php'));
}
echo $OUTPUT->header();
// Render page title
printf('<h1 class="page__title">%s</h1>', get_string('cart_title', 'local_moodec'));
// Render the product page content
echo $renderer->moodec_cart($cart);
$relatedOutput = '';
// Check if there are any related products for each product in the cart
foreach ($cart->get() as $id => $variation) {
    // Get the product for the given ID
    $product = local_moodec_get_product($id);
    // Get the HTML output of the related products renderer
    $relatedOutput = $renderer->related_products($product);
    // If there is any output, then we are happy and can continue!
    if ($relatedOutput !== '') {
        break;
    }
}
echo $relatedOutput;
echo $OUTPUT->footer();
Beispiel #3
0
 /**
  * Remove a product from the cart, if it exists
  * @param  int 		$id 	product_id
  * @return bool     		success or fail
  */
 public function remove($id)
 {
     $id = (int) $id;
     // First, we need to check if the product is ACTUALLY in the cart
     if ($this->check($id)) {
         $productToRemove = local_moodec_get_product($id);
         // Get the variation id for this product
         $v = $this->_products[$id];
         // Now we deduct the price from the cart total
         if ($productToRemove->get_type() === PRODUCT_TYPE_VARIABLE) {
             $this->_cartTotal -= $productToRemove->get_variation($v)->get_price();
         } else {
             $this->_cartTotal -= $productToRemove->get_price();
         }
         // And unset the array value
         unset($this->_products[$id]);
         if (0 === count($this->_products)) {
             $this->_transactionId = null;
         }
         // update the cart storage
         $this->update();
         return true;
     }
     return false;
 }
Beispiel #4
0
/**
 * Returns an array of the products
 * @param  int 		$limit 		The number of random products to return
 * @param  int 		$category  	The category id to filter by
 * @return array            	The products
 */
function local_moodec_get_random_products($limit = 1, $category = null, $exclude = 0)
{
    global $DB;
    // An array to store the products (this will be returned)
    $products = array();
    // VALIDATE PARAMETERS
    // If default, we won't filter by category
    if ($category == 'default') {
        $category = null;
    }
    // Ensure page is an int
    if (!is_int($limit)) {
        $limit = (int) $limit;
    }
    // BUILD THE QUERY
    $query = sprintf('SELECT DISTINCT lmp.id as productid
		FROM 	{local_moodec_product} lmp, 
				{local_moodec_variation} lmv, 
				{course} c
		WHERE 	lmp.id = lmv.product_id
		AND 	lmp.course_id = c.id
		AND		lmp.is_enabled = 1
		AND 	lmp.id != %d
		%s
	 	ORDER BY rand()', $exclude, $category !== null ? 'AND c.category = ' . $category : '');
    // RUN THE QUERY
    $records = $DB->get_records_sql($query, null, 0, $limit);
    if (!!$records) {
        foreach ($records as $record) {
            // Add the product matching this id to the array
            $products[] = local_moodec_get_product($record->productid);
        }
    }
    return $products;
}
    public function render()
    {
        global $CFG;
        // output form
        $html = sprintf('<form action="%s" method="POST" class="payment-gateway gateway--paypal">', $this->_gatewayURL);
        $html .= sprintf('<input type="hidden" name="cmd" value="_cart">
				<input type="hidden" name="charset" value="utf-8">
				<input type="hidden" name="upload" value="1">
				<input type="hidden" name="for_auction" value="false">
				<input type="hidden" name="no_note" value="1">
				<input type="hidden" name="no_shipping" value="1">
				<input type="hidden" name="business" value="%s">
				<input type="hidden" name="currency_code" value="%s">
				<input type="hidden" name="custom" value="%d">
				<input type="hidden" name="notify_url" value="%s">
				<input type="hidden" name="return" value="%s">
				<input type="hidden" name="cancel_return" value="%s">', get_config('local_moodec', 'payment_paypal_email'), get_config('local_moodec', 'currency'), $this->_transaction->get_id(), new moodle_url($CFG->wwwroot . '/local/moodec/payment/paypal/ipn.php'), new moodle_url($CFG->wwwroot . '/my'), new moodle_url($CFG->wwwroot . '/local/moodec/pages/cart.php'));
        // Count is used to incrementally name the item fields
        $count = 1;
        foreach ($this->_transaction->get_items() as $item) {
            $product = local_moodec_get_product($item->get_product_id());
            // Output name
            $html .= sprintf('<input type="hidden" name="%s" value="%s">', 'item_name_' . $count, $product->get_type() === PRODUCT_TYPE_SIMPLE ? $product->get_fullname() : $product->get_fullname() . ' - ' . $product->get_variation($item->get_variation_id())->get_name());
            // Output name
            $html .= sprintf('<input type="hidden" name="%s" value="%s">', 'amount_' . $count, $item->get_cost());
            $count++;
        }
        $html .= sprintf('<input type="submit" name="submit"  value="%s">', get_string('button_paypal_label', 'local_moodec'));
        $html .= sprintf('</form>');
        return $html;
    }
    // Get the existing transaction if the cart has recorded one
    $transaction = new MoodecTransaction($cart->get_transaction_id());
    // We reset the transaction, in case the items in the cart have changed
    $transaction->reset();
} else {
    // Otherwise create a new transaction
    $transaction = new MoodecTransaction();
}
// Set the transactionId in the cart to that of the transaction
// We do this in case the transaction reset created a new transaction
// Or, to add the transaction id to the cart if one didn't already exist
$cart->set_transaction_id($transaction->get_id());
// We need to add all the products in the cart to the transaction
foreach ($cart->get() as $pID => $vID) {
    // Get the product in the cart
    $product = local_moodec_get_product($pID);
    // Add the product to the transaction, relative to variation
    if ($product->get_type() === PRODUCT_TYPE_VARIABLE) {
        $transaction->add($pID, $product->get_variation($vID)->get_price(), $vID);
    } else {
        $transaction->add($pID, $product->get_price());
    }
}
echo $OUTPUT->header();
?>

<h1 class="page__title"><?php 
echo get_string('checkout_title', 'local_moodec');
?>
</h1>
Beispiel #7
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();
 }
    function single_transaction($transaction)
    {
        global $CFG, $DB;
        $fieldDate = date('H:i:s d/m/Y', $transaction->get_date());
        $user = $DB->get_record('user', array('id' => $transaction->get_user_id()));
        $fieldUser = sprintf('<a href="%s">%s %s</a>', new moodle_url($CFG->wwwroot . '/user/profile.php', array('id' => $user->id)), $user->firstname, $user->lastname);
        $fieldAmount = local_moodec_get_currency_symbol(get_config('local_moodec', 'currency')) . number_format($transaction->get_cost(), 2, '.', ',');
        $fieldItemCount = count($transaction->get_items());
        $html = sprintf('<div class="moodec-transaction__details span5 desktop-first-column">
				<h4>%s</h4>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
				<div class="row-fluid">
					<div class="column span3"><strong>%s:</strong></div>
					<div class="column span9">%s</div>
				</div>
			</div>', get_string('transaction_section_details', 'local_moodec'), get_string('transaction_field_id', 'local_moodec'), $transaction->get_id(), get_string('transaction_field_user', 'local_moodec'), $fieldUser, get_string('transaction_field_amount', 'local_moodec'), $fieldAmount, get_string('transaction_field_date', 'local_moodec'), $fieldDate, get_string('transaction_field_gateway', 'local_moodec'), $transaction->get_gateway(true), get_string('transaction_field_txn', 'local_moodec'), $transaction->get_txn_id(), get_string('transaction_field_status', 'local_moodec'), $transaction->get_status(true));
        $html .= sprintf('<div class="moodec-transaction__items span7 pull-right">
				<h4>%s (%s)</h4>', get_string('transaction_section_items', 'local_moodec'), count($transaction->get_items()));
        if (0 < count($transaction->get_items())) {
            foreach ($transaction->get_items() as $item) {
                $product = local_moodec_get_product($item->get_product_id());
                $name = $product->get_fullname();
                if ($product->get_type() === PRODUCT_TYPE_VARIABLE) {
                    $name .= ' - ' . $product->get_variation($item->get_variation_id())->get_name();
                }
                $html .= sprintf('<div class="row-fluid">
						<div class="column span8"><a href="%s">%s</a></div>
						<div class="column span2">%s</div>
						<div class="column span2 align-right"><a href="%s">%s</a></div>
					</div>', new moodle_url($CFG->wwwroot . '/local/moodec/pages/product.php', array('id' => $item->get_product_id())), $name, local_moodec_get_currency_symbol(get_config('local_moodec', 'currency')) . number_format($item->get_cost(), 2, '.', ','), new moodle_url($CFG->wwwroot . '/course/view.php', array('id' => $product->get_course_id())), get_string('transaction_field_actions_course', 'local_moodec'));
            }
        }
        $html .= '</div>';
        return $html;
    }