Exemple #1
0
 /**
  * Preview an asset
  *
  * @return array
  **/
 public function preview($id)
 {
     // Look up asset type from id
     require_once dirname(__DIR__) . DS . 'asset.php';
     $asset = new Asset($id);
     // Classname
     $class = __NAMESPACE__ . '\\' . ucfirst($asset->get('type'));
     if ($class != __NAMESPACE__ . '\\' && class_exists($class) && method_exists($class, 'preview')) {
         $object = $class::newWithoutHandlers($this->db);
         return $object->preview($asset);
     } else {
         // Default edit page
         return array('type' => 'default');
     }
 }
 /**
  * Deletes an asset file
  *
  * @apiMethod POST
  * @apiUri    /courses/asset/deletefile
  * @apiParameter {
  * 		"name":        "id",
  * 		"description": "ID of asset owning file",
  * 		"type":        "integer",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "filename",
  * 		"description": "Name of file to delete",
  * 		"type":        "string",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @return    void
  */
 public function deletefileTask()
 {
     // Require authentication and authorization
     $this->authorizeOrFail();
     // Grab incoming id, if applicable
     $id = Request::getInt('id', null);
     $filename = Request::getVar('filename', null);
     // Create our object
     $asset = new Asset($id);
     if ($asset->get('course_id') != $this->course->get('id')) {
         App::abort(500, 'Asset is not a part of this course.');
     }
     $basePath = $asset->path($this->course->get('id'));
     $path = $basePath . $filename;
     $dirname = dirname($path);
     if (!is_file(PATH_APP . $path) || $dirname != rtrim($basePath, DS)) {
         App::abort(500, 'Illegal file path');
     }
     unlink(PATH_APP . $path);
     // Return message
     $this->send('File deleted');
 }
 /**
  * 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);
 }
 /**
  * See if item prerequisite has been fulfilled
  *
  * @TODO: For now, we're going to place all of the logic here for checking
  * whether or not different types of items have been fulfilled.
  * Eventually this should be abstracted out elsewhere.
  *
  * @return bool
  **/
 public function hasMet($scope, $scope_id)
 {
     $return = true;
     switch ($scope) {
         case 'unit':
             $key = $scope . '.' . $scope_id;
             if (isset($this->prerequisites[$key]) && count($this->prerequisites[$key]) > 0) {
                 foreach ($this->prerequisites[$key] as $prerequisite) {
                     if (!isset($this->progress[$this->member_id]) || !isset($this->progress[$this->member_id][$prerequisite['scope_id']]) || $this->progress[$this->member_id][$prerequisite['scope_id']]['percentage_complete'] != 100) {
                         $return = false;
                         continue;
                     }
                 }
             }
             break;
         case 'asset':
             $key = $scope . '.' . $scope_id;
             if (isset($this->prerequisites[$key]) && count($this->prerequisites[$key]) > 0) {
                 foreach ($this->prerequisites[$key] as $prerequisite) {
                     $asset = new Asset($prerequisite['scope_id']);
                     switch ($asset->get('type')) {
                         case 'form':
                             if (!isset($this->grades[$prerequisite['scope_id']])) {
                                 $return = false;
                                 continue;
                             }
                             break;
                         default:
                             if (!isset($this->views[$this->member_id]) || !is_array($this->views[$this->member_id]) || !in_array($prerequisite['scope_id'], $this->views[$this->member_id])) {
                                 $return = false;
                                 continue;
                             }
                             break;
                     }
                 }
             }
             break;
     }
     return $return;
 }