Exemplo n.º 1
0
 /**
  * Display an offering asset
  *
  * @return     void
  */
 public function assetTask()
 {
     $sparams = new Registry($this->course->offering()->section()->get('params'));
     $section_id = $this->course->offering()->section()->get('id');
     $asset = new Models\Asset(Request::getInt('asset_id', null));
     $asset->set('section_id', $section_id);
     // First, check if current user has access to course
     if (!$this->course->offering()->access('view')) {
         // Is a preview available?
         $preview = $sparams->get('preview', 0);
         // If no preview is available or if type  is form (i.e. you can never preview forms)
         if (!$preview || $asset->get('type') == 'form') {
             // Check if they're logged in
             if (User::isGuest()) {
                 $this->loginTask(Lang::txt('COM_COURSES_ENROLLMENT_REQUIRED_FOR_ASSET'));
                 return;
             } else {
                 // Redirect back to the course outline
                 App::redirect(Route::url($this->course->offering()->link()), Lang::txt('COM_COURSES_ENROLLMENT_REQUIRED_FOR_ASSET'), 'warning');
                 return;
             }
         } elseif ($preview == 2) {
             $units = $asset->units();
             if ($units && count($units) > 0) {
                 foreach ($units as $unit) {
                     if ($unit->get('ordering') > 1) {
                         // Check if they're logged in
                         if (User::isGuest()) {
                             $this->loginTask(Lang::txt('COM_COURSES_ENROLLMENT_REQUIRED_FOR_ASSET'));
                             return;
                         } else {
                             // Redirect back to the course outline
                             App::redirect(Route::url($this->course->offering()->link()), Lang::txt('COM_COURSES_ENROLLMENT_REQUIRED_FOR_ASSET'), 'warning');
                             return;
                         }
                     }
                 }
             }
         }
     }
     // If not a manager and either the offering or section is unpublished...
     if (!$this->course->offering()->access('manage') && (!$this->course->offering()->isPublished() || !$this->course->offering()->section()->isPublished())) {
         return App::abort(403, Lang::txt('COM_COURSES_ERROR_ASSET_UNAVAILABLE'));
     }
     if (!$this->course->offering()->access('manage') && !$asset->isAvailable()) {
         // Allow expired forms to pass through (i.e. so students can see their results)
         if (!$asset->get('type') == 'form' || !$asset->ended()) {
             // Redirect back to the course outline
             App::redirect(Route::url($this->course->offering()->link()), Lang::txt('COM_COURSES_ERROR_ASSET_UNAVAILABLE'), 'warning');
             return;
         }
     }
     // Check prerequisites
     $member = $this->course->offering()->section()->member(User::get('id'));
     if (is_null($member->get('section_id'))) {
         $member->set('section_id', $section_id);
     }
     $prerequisites = $member->prerequisites($this->course->offering()->gradebook());
     if (!$this->course->offering()->access('manage') && !$prerequisites->hasMet('asset', $asset->get('id'))) {
         $prereqs = $prerequisites->get('asset', $asset->get('id'));
         $requirements = array();
         foreach ($prereqs as $pre) {
             $reqAsset = new Models\Asset($pre['scope_id']);
             $requirements[] = $reqAsset->get('title');
         }
         $requirements = implode(', ', $requirements);
         // Redirect back to the course outline
         App::redirect(Route::url($this->course->offering()->link()), Lang::txt('COM_COURSES_ERROR_ASSET_HAS_PREREQ', $requirements), 'warning');
         return;
     }
     // If requesting a file from a wiki type asset, then serve that up directly
     if ($asset->get('subtype') == 'wiki' && Request::getVar('file', false)) {
         echo $asset->download($this->course);
     }
     echo $asset->render($this->course);
 }