Skip to content

Commit

Permalink
MDL-50851 mod_wiki: use new tag API
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Jan 10, 2016
1 parent c026a28 commit b0b2c93
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 60 deletions.
2 changes: 2 additions & 0 deletions mod/wiki/backup/moodle2/backup_wiki_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ protected function define_structure() {
FROM {tag} t
JOIN {tag_instance} ti ON ti.tagid = t.id
WHERE ti.itemtype = ?
AND ti.component = ?
AND ti.itemid = ?', array(
backup_helper::is_sqlparam('wiki_pages'),
backup_helper::is_sqlparam('mod_wiki'),
backup::VAR_PARENTID));
}

Expand Down
6 changes: 3 additions & 3 deletions mod/wiki/backup/moodle2/restore_wiki_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ protected function process_wiki_tag($data) {
$data = (object)$data;
$oldid = $data->id;

if (empty($CFG->usetags)) { // tags disabled in server, nothing to process
if (!core_tag_tag::is_enabled('mod_wiki', 'wiki_pages')) { // Tags disabled in server, nothing to process.
return;
}

$tag = $data->rawname;
$itemid = $this->get_new_parentid('wiki_page');
$wikiid = $this->get_new_parentid('wiki');

$cm = get_coursemodule_from_instance('wiki', $wikiid);
tag_set_add('wiki_pages', $itemid, $tag, 'mod_wiki', context_module::instance($cm->id)->id);
$context = context_module::instance($this->task->get_moduleid());
core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $itemid, $context, $tag);
}

protected function after_execute() {
Expand Down
33 changes: 33 additions & 0 deletions mod/wiki/db/tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tag areas in component mod_wiki
*
* @package mod_wiki
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();


$tagareas = array(
array(
'itemtype' => 'wiki_pages',
'component' => 'mod_wiki',
),
);
6 changes: 3 additions & 3 deletions mod/wiki/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ protected function definition() {
$mform->addElement('hidden', 'contentformat', $format);
$mform->setType('contentformat', PARAM_ALPHANUMEXT);

if (!empty($CFG->usetags)) {
if (core_tag_tag::is_enabled('mod_wiki', 'wiki_pages')) {
$mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
$mform->addElement('tags', 'tags', get_string('tags'));
$mform->setType('tags', PARAM_TEXT);
}
$mform->addElement('tags', 'tags', get_string('tags'),
array('itemtype' => 'wiki_pages', 'component' => 'mod_wiki'));

$buttongroup = array();
$buttongroup[] = $mform->createElement('submit', 'editoption', get_string('save', 'wiki'), array('id' => 'save'));
Expand Down
1 change: 1 addition & 0 deletions mod/wiki/lang/en/wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
$string['searchwikis'] = 'Search wikis';
$string['special'] = 'Special';
$string['tableofcontents'] = 'Table of contents';
$string['tagarea_wiki_pages'] = 'Wiki pages';
$string['tagsdeleted'] = 'Wiki tags have been deleted';
$string['tagtitle'] = 'See the "{$a}" tag';
$string['teacherrating'] = 'Teacher rating';
Expand Down
39 changes: 23 additions & 16 deletions mod/wiki/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,15 @@ function wiki_delete_instance($id) {
return $result;
}

/**
* Implements callback to reset course
*
* @param stdClass $data
* @return boolean|array
*/
function wiki_reset_userdata($data) {
global $CFG,$DB;
require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
require_once($CFG->dirroot . '/tag/lib.php');
require_once($CFG->dirroot . "/mod/wiki/locallib.php");

$componentstr = get_string('modulenameplural', 'wiki');
Expand All @@ -154,10 +159,12 @@ function wiki_reset_userdata($data) {
if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
return false;
}
$errors = false;
foreach ($wikis as $wiki) {
if (empty($data->reset_wiki_comments) && empty($data->reset_wiki_tags) && empty($data->reset_wiki_pages)) {
return $status;
}

if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
foreach ($wikis as $wiki) {
if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $data->courseid)) {
continue;
}
$context = context_module::instance($cm->id);
Expand All @@ -175,14 +182,7 @@ function wiki_reset_userdata($data) {
if (empty($data->reset_wiki_pages)) {
// Go through each page and delete the tags.
foreach ($pages as $page) {

$tags = tag_get_tags_array('wiki_pages', $page->id);
foreach ($tags as $tagid => $tagname) {
// Delete the related tag_instances related to the wiki page.
$errors = tag_delete_instance('wiki_pages', $page->id, $tagid);
$status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'),
'error' => $errors);
}
core_tag_tag::remove_all_item_tags('mod_wiki', 'wiki_pages', $page->id);
}
} else {
// Otherwise we are removing pages and tags.
Expand All @@ -196,17 +196,24 @@ function wiki_reset_userdata($data) {
// Delete any attached files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_wiki', 'attachments');

$status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
'error' => $errors);
}
}

if (!empty($data->reset_wiki_pages)) {
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
'error' => false);
}
if (!empty($data->reset_wiki_tags)) {
$status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => false);
}
}

// Remove all comments.
if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) {
$DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id));
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
if (!empty($data->reset_wiki_comments)) {
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
}
}
}
return $status;
Expand Down
21 changes: 3 additions & 18 deletions mod/wiki/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,6 @@ function wiki_delete_pages($context, $pageids = null, $subwikiid = null) {
return;
}

require_once($CFG->dirroot . '/tag/lib.php');

/// Delete page and all it's relevent data
foreach ($pageids as $pageid) {
if (is_object($pageid)) {
Expand All @@ -1178,10 +1176,7 @@ function wiki_delete_pages($context, $pageids = null, $subwikiid = null) {
}

//Delete page tags
$tags = tag_get_tags_array('wiki_pages', $pageid);
foreach ($tags as $tagid => $tagvalue) {
tag_delete_instance('wiki_pages', $pageid, $tagid);
}
core_tag_tag::remove_all_item_tags('mod_wiki', 'wiki_pages', $pageid);

//Delete Synonym
wiki_delete_synonym($subwikiid, $pageid);
Expand Down Expand Up @@ -1386,18 +1381,8 @@ function wiki_print_page_content($page, $context, $subwikiid) {
$html = format_text($html, FORMAT_MOODLE, array('overflowdiv'=>true, 'allowid'=>true));
echo $OUTPUT->box($html);

if (!empty($CFG->usetags)) {
$tags = tag_get_tags_array('wiki_pages', $page->id);
echo $OUTPUT->container_start('wiki-tags');
echo '<span class="wiki-tags-title">'.get_string('tags').': </span>';
$links = array();
foreach ($tags as $tagid=>$tag) {
$url = new moodle_url('/tag/index.php', array('tag'=>$tag));
$links[] = html_writer::link($url, $tag, array('title'=>get_string('tagtitle', 'wiki', $tag)));
}
echo join($links, ", ");
echo $OUTPUT->container_end();
}
echo $OUTPUT->tag_list(core_tag_tag::get_item_tags('mod_wiki', 'wiki_pages', $page->id),
null, 'wiki-tags');

wiki_increment_pageviews($page);
}
Expand Down
18 changes: 3 additions & 15 deletions mod/wiki/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/

require_once($CFG->dirroot . '/mod/wiki/edit_form.php');
require_once($CFG->dirroot . '/tag/lib.php');

/**
* Class page_wiki contains the common code between all pages
Expand Down Expand Up @@ -570,18 +569,9 @@ protected function print_edit($content = null) {
$params['filearea'] = 'attachments';
}

$form = new mod_wiki_edit_form($url, $params);

if ($formdata = $form->get_data()) {
if (!empty($CFG->usetags)) {
$data->tags = $formdata->tags;
}
} else {
if (!empty($CFG->usetags)) {
$data->tags = tag_get_tags_array('wiki_pages', $this->page->id);
}
}
$data->tags = core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $this->page->id);

$form = new mod_wiki_edit_form($url, $params);
$form->set_data($data);
$form->display();
}
Expand Down Expand Up @@ -2060,9 +2050,7 @@ protected function print_save() {
}

if ($save && $data) {
if (!empty($CFG->usetags)) {
tag_set('wiki_pages', $this->page->id, $data->tags, 'mod_wiki', $this->modcontext->id);
}
core_tag_tag::set_item_tags('mod_wiki', 'wiki_pages', $this->page->id, $this->modcontext, $data->tags);

$message = '<p>' . get_string('saving', 'wiki') . '</p>';

Expand Down
4 changes: 0 additions & 4 deletions mod/wiki/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@
text-align: right;
}

.wiki-tags span {
font-weight: bold;
}

.wiki_modifieduser p {
line-height: 35px;
}
Expand Down
2 changes: 1 addition & 1 deletion mod/wiki/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2015111602; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016011100; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2015111000; // Requires this Moodle version
$plugin->component = 'mod_wiki'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;

0 comments on commit b0b2c93

Please sign in to comment.