Exemple #1
0
     break;
 case 'colldelete':
     $confirm = optional_param('confirm', false, PARAM_BOOL);
     if (!$confirm) {
         echo $OUTPUT->header();
         $strconfirm = get_string('suredeletecoll', 'tag', core_tag_collection::display_name($tagcoll));
         $params = array('tc' => $tagcoll->id, 'confirm' => 1, 'sesskey' => sesskey(), 'action' => 'colldelete');
         $formcontinue = new single_button(new moodle_url($manageurl, $params), get_string('yes'));
         $formcancel = new single_button($manageurl, get_string('no'), 'get');
         echo $OUTPUT->confirm($strconfirm, $formcontinue, $formcancel);
         echo $OUTPUT->footer();
         die;
     }
     if ($tagcoll && !$tagcoll->component) {
         require_sesskey();
         core_tag_collection::delete($tagcoll);
         redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
     }
     redirect($manageurl);
     break;
 case 'collmoveup':
     if ($tagcoll) {
         require_sesskey();
         core_tag_collection::change_sortorder($tagcoll, -1);
         redirect(new moodle_url($manageurl, array('notice' => 'changessaved')));
     }
     redirect($manageurl);
     break;
 case 'collmovedown':
     if ($tagcoll) {
         require_sesskey();
Exemple #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'));
 }
Exemple #3
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);
     }
 }