View source
<?php
class Course extends CourseHandler {
private $node;
private $user;
private $courseObjects = array();
private $tracker;
private $active = NULL;
private $next;
private $prev;
public function getTracker($account = NULL) {
if (empty($account)) {
$account = $GLOBALS['user'];
}
if ($entities = entity_load('course_report', FALSE, array(
'nid' => $this
->getNode()->nid,
'uid' => $account->uid,
))) {
return reset($entities);
}
else {
return entity_create('course_report', array(
'nid' => $this
->getNode()->nid,
'uid' => $account->uid,
));
}
}
public function getUrl() {
return "node/{$this->nid}/takecourse";
}
public function setActive($id = NULL) {
if (!$id && isset($_SESSION['course'][$this
->getNode()->nid]['taking']['active'])) {
$id = $_SESSION['course'][$this
->getNode()->nid]['taking']['active'];
}
$old = NULL;
$storeNext = FALSE;
foreach ($this
->getObjects() as $courseObject) {
if (!$courseObject
->getOption('enabled')) {
continue;
}
if ($id == $courseObject
->getId()) {
if ($old) {
$this->prev = $old;
}
$storeNext = TRUE;
$this->active = $courseObject;
}
elseif ($storeNext) {
$this->next = clone $courseObject;
$storeNext = FALSE;
}
$old = clone $courseObject;
}
}
public function getActive() {
if (!$this->active) {
$this
->setActive();
}
return $this->active;
}
public function getNext() {
if (!$this->active) {
$this
->setActive();
}
return $this->next;
}
public function getPrev() {
if (!$this->active) {
$this
->setActive();
}
return $this->prev;
}
public function getNavigation() {
$this
->setActive();
$prev = $this
->getPrev();
$next = $this
->getNext();
$links = array();
if ($prev && $prev
->access('take')) {
$links['prev'] = l(t('Previous'), $prev
->getUrl(), array(
'html' => TRUE,
));
}
$links['back'] = l(t('Back to course'), $this
->getUrl());
if ($next && $next
->access('take')) {
$links['next'] = l(t('Next'), $next
->getUrl(), array(
'html' => TRUE,
));
}
elseif (!$next && $this
->getTracker()
->getOption('complete')) {
$links['next'] = l(t('Next'), 'node/' . $this
->getOption('nid') . '/course-complete', array(
'html' => TRUE,
));
}
if ($active = $this
->getActive()) {
foreach ($active
->overrideNavigation() as $key => $link) {
$links[$key] = $link;
}
}
return $links;
}
public function getObjects() {
if (empty($this->courseObjects)) {
$efq = new EntityFieldQuery();
$result = $efq
->entityCondition('entity_type', 'course_object')
->propertyCondition('nid', $this
->getNode()->nid)
->propertyOrderBy('weight')
->execute();
if (!empty($result['course_object'])) {
$this->courseObjects = entity_load('course_object', array_keys($result['course_object']));
}
}
return $this->courseObjects;
}
function resetCache() {
$this->courseObjects = array();
return $this;
}
public function getNode() {
return node_load($this->nid);
}
}