Esempio n. 1
0
 /**
  * Updates the value in database and returns itself, called from inplace_editable callback
  *
  * @param int $itemid
  * @param mixed $newvalue
  * @return \self
  */
 public static function update($itemid, $newvalue)
 {
     global $DB;
     require_capability('moodle/tag:manage', context_system::instance());
     $tagcoll = $DB->get_record('tag_coll', array('id' => $itemid), '*', MUST_EXIST);
     \core_tag_collection::update($tagcoll, array('name' => $newvalue));
     return new self($tagcoll);
 }
Esempio n. 2
0
 /**
  * Very basic test for create/move/update/delete actions, without any itemtype movements.
  */
 public function test_tag_coll_basic()
 {
     global $DB;
     // Make sure there is one and only one tag coll that is marked as default.
     $tagcolls = core_tag_collection::get_collections();
     $this->assertEquals(1, count($DB->get_records('tag_coll', array('isdefault' => 1))));
     $defaulttagcoll = core_tag_collection::get_default();
     // Create a new tag coll to store user tags and something else.
     $data = (object) array('name' => 'new tag coll');
     $tagcollid1 = core_tag_collection::create($data)->id;
     $tagcolls = core_tag_collection::get_collections();
     $this->assertEquals('new tag coll', $tagcolls[$tagcollid1]->name);
     // Create a new tag coll to store post tags.
     $data = (object) array('name' => 'posts');
     $tagcollid2 = core_tag_collection::create($data)->id;
     $tagcolls = core_tag_collection::get_collections();
     $this->assertEquals('posts', $tagcolls[$tagcollid2]->name);
     $this->assertEquals($tagcolls[$tagcollid1]->sortorder + 1, $tagcolls[$tagcollid2]->sortorder);
     // Illegal tag colls sortorder changing.
     $this->assertFalse(core_tag_collection::change_sortorder($tagcolls[$defaulttagcoll], 1));
     $this->assertFalse(core_tag_collection::change_sortorder($tagcolls[$defaulttagcoll], -1));
     $this->assertFalse(core_tag_collection::change_sortorder($tagcolls[$tagcollid2], 1));
     // Move the very last tag coll one position up.
     $this->assertTrue(core_tag_collection::change_sortorder($tagcolls[$tagcollid2], -1));
     $tagcolls = core_tag_collection::get_collections();
     $this->assertEquals($tagcolls[$tagcollid2]->sortorder + 1, $tagcolls[$tagcollid1]->sortorder);
     // Move the second last tag coll one position down.
     $this->assertTrue(core_tag_collection::change_sortorder($tagcolls[$tagcollid2], 1));
     $tagcolls = core_tag_collection::get_collections();
     $this->assertEquals($tagcolls[$tagcollid1]->sortorder + 1, $tagcolls[$tagcollid2]->sortorder);
     // Edit tag coll.
     $this->assertTrue(core_tag_collection::update($tagcolls[$tagcollid2], (object) array('name' => 'posts2')));
     $tagcolls = core_tag_collection::get_collections();
     $this->assertEquals('posts2', $tagcolls[$tagcollid2]->name);
     // Delete tag coll.
     $count = $DB->count_records('tag_coll');
     $this->assertFalse(core_tag_collection::delete($tagcolls[$defaulttagcoll]));
     $this->assertTrue(core_tag_collection::delete($tagcolls[$tagcollid1]));
     $this->assertEquals($count - 1, $DB->count_records('tag_coll'));
 }
Esempio n. 3
0
    $PAGE->navbar->add(core_tag_collection::display_name($tagcoll), new moodle_url($manageurl, array('tc' => $tagcoll->id)));
}
$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
switch ($action) {
    case 'colladd':
    case 'colledit':
        if ($action === 'colladd' || $action === 'colledit' && $tagcoll && empty($tagcoll->component)) {
            $form = new core_tag_collection_form($manageurl, $tagcoll);
            if ($form->is_cancelled()) {
                redirect($manageurl);
            } else {
                if ($data = $form->get_data()) {
                    if ($action === 'colladd') {
                        core_tag_collection::create($data);
                    } else {
                        core_tag_collection::update($tagcoll, $data);
                    }
                    redirect($manageurl);
                } else {
                    $title = $action === 'colladd' ? get_string('addtagcoll', 'tag') : get_string('edittagcoll', 'tag', core_tag_collection::display_name($tagcoll));
                    $PAGE->navbar->add($title);
                    echo $OUTPUT->header();
                    echo $OUTPUT->heading($title, 2);
                    $form->display();
                    echo $OUTPUT->footer();
                    exit;
                }
            }
        }
        break;
    case 'colldelete':
Esempio n. 4
0
 /**
  * Update the database to contain a list of tagged areas for a component.
  * The list of tagged areas is read from [plugindir]/db/tag.php
  *
  * @param string $componentname - The frankenstyle component name.
  */
 public static function reset_definitions_for_component($componentname)
 {
     global $DB;
     $dir = core_component::get_component_directory($componentname);
     $file = $dir . '/db/tag.php';
     $tagareas = null;
     if (file_exists($file)) {
         require_once $file;
     }
     list($a, $b) = core_component::normalize_component($componentname);
     $component = $b ? $a . '_' . $b : $a;
     list($existingareas, $existingcolls) = self::get_definitions_for_component($componentname);
     $itemtypes = array();
     $collections = array();
     $needcleanup = false;
     if ($tagareas) {
         foreach ($tagareas as $tagarea) {
             $record = (object) $tagarea;
             if ($component !== 'core' || empty($record->component)) {
                 if (isset($record->component) && $record->component !== $component) {
                     debugging("Item type {$record->itemtype} has illegal component {$record->component}", DEBUG_DEVELOPER);
                 }
                 $record->component = $component;
             }
             unset($record->tagcollid);
             if (!empty($record->collection)) {
                 // Create collection if it does not exist, or update 'searchable' and/or 'customurl' if needed.
                 $key = $record->collection . ':' . $record->component;
                 $collectiondata = array_intersect_key((array) $record, array('component' => 1, 'searchable' => 1, 'customurl' => 1));
                 $collectiondata['name'] = $record->collection;
                 if (!array_key_exists($key, $existingcolls)) {
                     $existingcolls[$key] = core_tag_collection::create($collectiondata);
                 } else {
                     core_tag_collection::update($existingcolls[$key], $collectiondata);
                 }
                 $record->tagcollid = $existingcolls[$key]->id;
                 $collections[$key] = $existingcolls[$key];
                 unset($record->collection);
             }
             unset($record->searchable);
             unset($record->customurl);
             if (!isset($record->callback)) {
                 $record->callback = null;
             }
             if (!isset($record->callbackfile)) {
                 $record->callbackfile = null;
             }
             $itemtypes[$record->itemtype . ':' . $record->component] = $record;
         }
     }
     $todeletearea = array_diff_key($existingareas, $itemtypes);
     $todeletecoll = array_diff_key($existingcolls, $collections);
     // Delete tag areas that are no longer needed.
     foreach ($todeletearea as $key => $record) {
         self::delete($record);
     }
     // Update tag areas if changed.
     $toupdatearea = array_intersect_key($existingareas, $itemtypes);
     foreach ($toupdatearea as $key => $tagarea) {
         if (!isset($itemtypes[$key]->tagcollid)) {
             foreach ($todeletecoll as $tagcoll) {
                 if ($tagcoll->id == $tagarea->tagcollid) {
                     $itemtypes[$key]->tagcollid = core_tag_collection::get_default();
                 }
             }
         }
         self::update($tagarea, $itemtypes[$key]);
     }
     // Create new tag areas.
     $toaddarea = array_diff_key($itemtypes, $existingareas);
     foreach ($toaddarea as $record) {
         self::create($record);
     }
     // Delete tag collections that are no longer needed.
     foreach ($todeletecoll as $key => $tagcoll) {
         core_tag_collection::delete($tagcoll);
     }
 }