Beispiel #1
0
 /**
  * Save a course page
  *
  * @return  void
  */
 public function saveTask()
 {
     // Check for request forgeries
     Request::checkToken();
     // load the request vars
     $fields = Request::getVar('fields', array(), 'post', 'none', 2);
     $tmpl = Request::getVar('tmpl', '');
     // instatiate course page object for saving
     $row = new Tables\Asset($this->database);
     if (!$row->bind($fields)) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     if (!$row->check()) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     if (!$row->store()) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     $fields['asset_id'] = $row->get('id');
     $row2 = new Tables\AssetAssociation($this->database);
     $row2->loadByAssetScope($fields['asset_id'], $fields['scope_id'], $fields['scope']);
     if (!$row2->id) {
         if (!$row2->bind($fields)) {
             $this->setError($row2->getError());
             $this->editTask($row);
             return;
         }
         if (!$row2->check()) {
             $this->setError($row2->getError());
             $this->editTask($row);
             return;
         }
         if (!$row2->store()) {
             $this->setError($row2->getError());
             $this->editTask($row);
             return;
         }
     }
     // Rename the temporary upload directory if it exist
     $lid = $fields['lid'];
     if ($lid != $row->get('id')) {
         $path = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/courses'), DS) . DS . $fields['course_id'];
         if (is_dir($path . DS . $lid)) {
             if (!Filesystem::move($path . DS . $lid, $path . DS . $row->get('id'))) {
                 $this->setError(Lang::txt('UNABLE_TO_MOVE_PATH'));
             }
         }
     }
     // Incoming file
     /*$file = Request::getVar('upload', '', 'files', 'array');
     		if ($file['name'])
     		{
     			$path = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/courses'), DS) . DS . $fields['course_id'] . DS . $row->id;
     			// Make sure the upload path exist
     			if (!is_dir($path))
     			{
     				if (!\Filesystem::makeDirectory($path))
     				{
     					$this->setError(Lang::txt('UNABLE_TO_CREATE_UPLOAD_PATH').' '.$path);
     					$this->editTask($row);
     					return;
     				}
     			}
     
     			// Make the filename safe
     			$file['name'] = Filesystem::clean($file['name']);
     			// Ensure file names fit.
     			$ext = Filesystem::extension($file['name']);
     			$file['name'] = str_replace(' ', '_', $file['name']);
     			if (strlen($file['name']) > 230)
     			{
     				$file['name'] = substr($file['name'], 0, 230);
     				$file['name'] .= '.' . $ext;
     			}
     
     			// Perform the upload
     			if (!\Filesystem::upload($file['tmp_name'], $path . DS . $file['name']))
     			{
     				$this->setError(Lang::txt('ERROR_UPLOADING'));
     			}
     			else
     			{
     				if (strtolower($ext) == 'zip')
     				{
     					require_once(PATH_CORE . DS . 'includes' . DS . 'pcl' . DS . 'pclzip.lib.php');
     
     					if (!extension_loaded('zlib'))
     					{
     						$this->setError(Lang::txt('ZLIB_PACKAGE_REQUIRED'));
     					}
     					else
     					{
     						$zip = new PclZip($path . DS . $file['name']);
     
     						// unzip the file
     						if (!($do = $zip->extract($path)))
     						{
     							$this->setError(Lang::txt('UNABLE_TO_EXTRACT_PACKAGE'));
     						}
     						else
     						{
     							@unlink($path . DS . $file['name']);
     							$file['name'] = 'presentation.json';
     						}
     					}
     				}
     
     				// Set the url
     				$row->set('url', $file['name']);
     				$row->store();
     			}
     		}*/
     if ($tmpl == 'component') {
         if ($this->getError()) {
             echo '<p class="error">' . $this->getError() . '</p>';
         } else {
             echo '<p class="message">' . Lang::txt('COM_COURSES_ITEM_SAVED') . '</p>';
         }
         return;
     }
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&tmpl=' . $tmpl . '&scope=' . $fields['scope'] . '&scope_id=' . $fields['scope_id'] . '&course_id=' . $fields['course_id'], false));
 }
Beispiel #2
0
 /**
  * Copy an entry and associated data
  *
  * @param   integer $offering_id New offering to copy to
  * @param   boolean $deep        Copy associated data?
  * @return  boolean True on success, false on error
  */
 public function copy($offering_id = null, $deep = true)
 {
     // Get some old info we may need
     //  - Unit ID
     //  - Offering ID
     $u_id = $this->get('id');
     $o_id = $this->get('offering_id');
     // Reset the ID. This will force store() to create a new record.
     $this->set('id', 0);
     // Are we copying to a new offering?
     if ($offering_id) {
         $this->set('offering_id', $offering_id);
     } else {
         // Copying to the same offering so we want to distinguish
         // this unit from the one we copied from
         $this->set('title', $this->get('title') . ' (copy)');
         $this->set('alias', $this->get('alias') . '_copy');
     }
     if (!$this->store()) {
         return false;
     }
     if ($deep) {
         // Copy assets
         $tbl = new Tables\AssetAssociation($this->_db);
         //foreach ($this->assets(array('asset_scope_id' => $u_id)) as $asset)
         foreach ($tbl->find(array('scope_id' => $u_id, 'scope' => 'unit')) as $asset) {
             $tbl->bind($asset);
             $tbl->id = 0;
             $tbl->scope_id = $this->get('id');
             //if (!$asset->copy($this->get('id')))
             if (!$tbl->store()) {
                 $this->setError($tbl->getError());
             }
         }
         // Copy asset groups
         foreach ($this->assetgroups(null, array('unit_id' => $u_id, 'parent' => 0)) as $assetgroup) {
             if (!$assetgroup->copy($this->get('id'), $deep)) {
                 $this->setError($assetgroup->getError());
             }
         }
     }
     return true;
 }
Beispiel #3
0
 /**
  * Copy an entry and associated data
  *
  * @param   integer $course_id New course to copy to
  * @param   boolean $deep      Copy associated data?
  * @return  boolean True on success, false on error
  */
 public function copy($course_id = null, $deep = true)
 {
     // Get some old info we may need
     //  - Offering ID
     //  - Course ID
     $o_id = $this->get('id');
     $c_id = $this->get('course_id');
     $oldOfferingAssets = $this->assets();
     // Reset the ID. This will force store() to create a new record.
     $this->set('id', 0);
     // Are we copying to a new course?
     if ($course_id) {
         $this->set('course_id', $course_id);
     } else {
         // Copying to the same course so we want to distinguish
         // this offering from the one we copied from
         $this->set('title', $this->get('title') . ' (copy)');
         $this->set('alias', $this->get('alias') . '_copy');
     }
     if (!$this->store()) {
         return false;
     }
     if ($deep) {
         // Copy pages
         foreach ($this->pages(array('offering_id' => $o_id, 'active' => array(0, 1)), true) as $page) {
             if (!$page->copy($this->get('course_id'), $this->get('id'))) {
                 $this->setError($page->getError());
             }
         }
         // Copy units
         foreach ($this->units(array('offering_id' => $o_id, 'section_id' => -1), true) as $unit) {
             if (!$unit->copy($this->get('id'))) {
                 $this->setError($unit->getError());
             }
         }
         // Copy logo
         if ($file = $this->logo('file')) {
             $src = DS . trim($this->config('uploadpath', '/site/courses'), '/') . DS . $c_id . '/offerings/' . $o_id . DS . $file;
             if (file_exists(PATH_APP . $src)) {
                 $dest = DS . trim($this->config('uploadpath', '/site/courses'), '/') . DS . $this->get('course_id') . '/offerings/' . $this->get('id');
                 if (!is_dir(PATH_APP . $dest)) {
                     if (!Filesystem::makeDirectory(PATH_APP . $dest)) {
                         $this->setError(Lang::txt('UNABLE_TO_CREATE_UPLOAD_PATH'));
                     }
                 }
                 $dest .= DS . $file;
                 if (!Filesystem::copy(PATH_APP . $src, PATH_APP . $dest)) {
                     $this->setError(Lang::txt('Failed to copy offering logo.'));
                 }
             }
         }
         // Copy assets (grab the assets from the original offering)
         if ($oldOfferingAssets) {
             foreach ($oldOfferingAssets as $asset) {
                 $oldAssetId = $asset->get('id');
                 if (!$asset->copy()) {
                     $this->setError($asset->getError());
                 } else {
                     // Copy asset associations
                     $tbl = new Tables\AssetAssociation($this->_db);
                     foreach ($tbl->find(array('scope_id' => $o_id, 'scope' => 'offering', 'asset_id' => $oldAssetId)) as $aa) {
                         $tbl->bind($aa);
                         $tbl->id = 0;
                         $tbl->scope_id = $this->get('id');
                         $tbl->asset_id = $asset->get('id');
                         if (!$tbl->store()) {
                             $this->setError($tbl->getError());
                         }
                     }
                 }
             }
         }
     }
     return true;
 }