Ejemplo n.º 1
0
 /**
  * Create a collection of pages
  * @param array $record
  * @throws SystemException if creating failed
  * @return int new collection id
  */
 public function create_collection($record)
 {
     // Validation
     switch ($record['ownertype']) {
         case 'institution':
             if (empty($record['ownername'])) {
                 $record['institution'] = 'mahara';
                 break;
             }
             if ($institutionid = $this->get_institution_id($record['ownername'])) {
                 $record['institution'] = $record['ownername'];
             } else {
                 throw new SystemException("The institution '" . $record['ownername'] . "' does not exist.");
             }
             break;
         case 'group':
             if ($groupid = $this->get_group_id($record['ownername'])) {
                 $record['group'] = $groupid;
             } else {
                 throw new SystemException("The group '" . $record['ownername'] . "' does not exist.");
             }
             break;
         case 'user':
         default:
             if ($ownerid = get_field('usr', 'id', 'username', $record['ownername'])) {
                 $record['owner'] = $ownerid;
             } else {
                 throw new SystemException("The user '" . $record['ownername'] . "' does not exist.");
             }
             break;
     }
     // Check if the given pages exist and belong to the collection's owner
     $addviews = array();
     if (!empty($record['pages'])) {
         $record['pages'] = trim($record['pages']);
         $viewtitles = !empty($record['pages']) ? explode(',', $record['pages']) : false;
         if (!empty($viewtitles)) {
             foreach ($viewtitles as $viewtitle) {
                 if (!empty($viewtitle) && !($view = get_record_sql('
                         SELECT v.id
                         FROM {view} v
                             INNER JOIN {usr} u ON u.id = v.owner
                         WHERE v.title = ?
                             AND u.username = ?', array(trim($viewtitle), $record['ownername'])))) {
                     throw new SystemException("The page '" . $viewtitle . "' does not exist or not belong to the user '" . $record['ownername'] . "'.");
                 }
                 $addviews['view_' . $view->id] = true;
             }
         }
     }
     // Create a new collection
     require_once 'collection.php';
     $data = new StdClass();
     $data->name = $record['title'];
     $data->description = $record['description'];
     if (!empty($record['group'])) {
         $data->group = $record['group'];
     } else {
         if (!empty($record['institution'])) {
             $data->institution = $record['institution'];
         } else {
             if (!empty($record['owner'])) {
                 $data->owner = $record['owner'];
             }
         }
     }
     $collection = new Collection(0, $data);
     $collection->commit();
     // Add views to the collection
     if (!empty($addviews)) {
         $collection->add_views($addviews);
     }
 }
Ejemplo n.º 2
0
 /**
  * Helper function to create or update a Collection from the supplied data.
  *
  * @param array $data
  * @return collection           The newly created/updated collection
  */
 public static function save($data)
 {
     if (array_key_exists('id', $data)) {
         $id = $data['id'];
     } else {
         $id = 0;
     }
     $collection = new Collection($id, $data);
     $collection->set('mtime', time());
     $collection->commit();
     return $collection;
     // return newly created Collections id
 }
Ejemplo n.º 3
0
 /**
  */
 public function import_using_strategy(SimpleXMLElement $entry, $strategy, array $otherentries)
 {
     switch ($strategy) {
         case self::STRATEGY_IMPORT_AS_VIEW:
             require_once 'view.php';
             if (!$this->import_entry_as_mahara_view($entry)) {
                 // Not a Mahara view, just do basic import
                 $this->trace('Not a Mahara view, doing basic import', self::LOG_LEVEL_VERBOSE);
                 $viewdata = array('title' => (string) $entry->title, 'description' => (string) $entry->summary, 'type' => 'portfolio', 'layout' => null, 'tags' => self::get_entry_tags($entry), 'numrows' => 1, 'ownerformat' => FORMAT_NAME_DISPLAYNAME, 'owner' => $this->get('usr'));
                 if ($published = strtotime((string) $entry->published)) {
                     $viewdata['ctime'] = $published;
                 }
                 if ($updated = strtotime((string) $entry->updated)) {
                     $viewdata['mtime'] = $updated;
                 }
                 $view = View::create($viewdata, $this->get('usr'));
                 safe_require('blocktype', 'textbox');
                 $bi = new BlockInstance(0, array('blocktype' => 'textbox', 'title' => '', 'row' => 1, 'column' => 1, 'order' => 1, 'configdata' => array('text' => self::get_entry_content($entry, $this))));
                 $view->addblockinstance($bi);
                 $this->viewids[(string) $entry->id] = $view->get('id');
             }
             break;
         case self::STRATEGY_IMPORT_AS_COLLECTION:
             require_once 'collection.php';
             $collectiondata = array('name' => (string) $entry->title, 'description' => (string) $entry->summary, 'tags' => self::get_entry_tags($entry), 'owner' => $this->get('usr'));
             if ($published = strtotime((string) $entry->published)) {
                 $collectiondata['ctime'] = $published;
             }
             if ($updated = strtotime((string) $entry->updated)) {
                 $collectiondata['mtime'] = $updated;
             }
             $collection = new Collection(0, $collectiondata);
             $collection->commit();
             $this->collectionids[(string) $entry->id] = $collection->get('id');
             // Remember entry ids that form part of this entry, and use them later
             // to put views into collections.
             foreach ($entry->link as $link) {
                 if ($this->curie_equals($link['rel'], '', 'has_part') && isset($link['href'])) {
                     $this->collectionviewentries[$collection->get('id')][] = (string) $link['href'];
                 }
             }
             break;
         default:
             throw new ImportException($this, 'TODO: get_string: unknown strategy chosen for importing entry');
     }
 }
 /**
  * Creates a new Collection for the given user.
  *
  * @param array $data
  * @return collection           The newly created Collection
  */
 public static function save($data)
 {
     $collection = new Collection(0, $data);
     $collection->commit();
     return $collection;
     // return newly created Collections id
 }