Beispiel #1
0
    public function page_index() {
        global $CFG;
        $output = '';
        // Checking wiki instance
        if (!$wiki = wiki_get_wiki($this->page->cm->instance)) {
            return false;
        }

        // @TODO: Fix call to wiki_get_subwiki_by_group
        $gid = groups_get_activity_group($this->page->cm);
        $gid = !empty($gid) ? $gid : 0;
        if (!$subwiki = wiki_get_subwiki_by_group($this->page->cm->instance, $gid)) {
            return false;
        }
        $swid = $subwiki->id;
        $pages = wiki_get_page_list($swid);
        $selectoptions = array();
        foreach ($pages as $page) {
            $selectoptions[$page->id] = $page->title;
        }
        $label = get_string('pageindex', 'wiki') . ': ';
        $select = new single_select(new moodle_url('/mod/wiki/view.php'), 'pageid', $selectoptions);
        $select->label = $label;
        return $this->output->container($this->output->render($select), 'wiki_index');
    }
Beispiel #2
0
/**
 * Checks if current user can edit a subwiki
 *
 * @param $subwiki
 */
function wiki_user_can_edit($subwiki)
{
    global $USER;
    $wiki = wiki_get_wiki($subwiki->wikiid);
    $cm = get_coursemodule_from_instance('wiki', $wiki->id);
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    // Working depending on activity groupmode
    switch (groups_get_activity_groupmode($cm)) {
        case NOGROUPS:
            if ($wiki->wikimode == 'collaborative') {
                // Collaborative Mode:
                // There is a wiki for all the class.
                //
                // Only edit capbility needed
                return has_capability('mod/wiki:editpage', $context);
            } else {
                if ($wiki->wikimode == 'individual') {
                    // Individual Mode
                    // There is a wiki per user
                    // Only the owner of that wiki can edit it
                    if ($subwiki->userid == $USER->id) {
                        return has_capability('mod/wiki:editpage', $context);
                    } else {
                        // Current user is not the owner of that wiki.
                        // User must have:
                        //      mod/wiki:editpage capability
                        // and
                        //      mod/wiki:managewiki capability
                        $edit = has_capability('mod/wiki:editpage', $context);
                        $manage = has_capability('mod/wiki:managewiki', $context);
                        return $edit && $manage;
                    }
                } else {
                    //Error
                    return false;
                }
            }
        case SEPARATEGROUPS:
            if ($wiki->wikimode == 'collaborative') {
                // Collaborative Mode:
                // There is one wiki per group.
                //
                // Only members of subwiki group could edit that wiki
                if ($subwiki->groupid == groups_get_activity_group($cm)) {
                    // Only edit capability needed
                    return has_capability('mod/wiki:editpage', $context);
                } else {
                    // User is not part of that group
                    // User must have:
                    //      mod/wiki:managewiki capability
                    // and
                    //      moodle/site:accessallgroups capability
                    // and
                    //      mod/wiki:editpage capability
                    $manage = has_capability('mod/wiki:managewiki', $context);
                    $access = has_capability('moodle/site:accessallgroups', $context);
                    $edit = has_capability('mod/wiki:editpage', $context);
                    return $manage && $access && $edit;
                }
            } else {
                if ($wiki->wikimode == 'individual') {
                    // Individual Mode:
                    // Each person owns a wiki.
                    //
                    // Only the owner of that wiki can edit it
                    if ($subwiki->userid == $USER->id) {
                        return has_capability('mod/wiki:editpage', $context);
                    } else {
                        // Current user is not the owner of that wiki.
                        // User must have:
                        //      mod/wiki:managewiki capability
                        // and
                        //      moodle/site:accessallgroups capability
                        // and
                        //      mod/wiki:editpage capability
                        $manage = has_capability('mod/wiki:managewiki', $context);
                        $access = has_capability('moodle/site:accessallgroups', $context);
                        $edit = has_capability('mod/wiki:editpage', $context);
                        return $manage && $access && $edit;
                    }
                } else {
                    //Error
                    return false;
                }
            }
        case VISIBLEGROUPS:
            if ($wiki->wikimode == 'collaborative') {
                // Collaborative Mode:
                // There is one wiki per group.
                //
                // Only members of subwiki group could edit that wiki
                if (groups_is_member($subwiki->groupid)) {
                    // Only edit capability needed
                    return has_capability('mod/wiki:editpage', $context);
                } else {
                    // User is not part of that group
                    // User must have:
                    //      mod/wiki:managewiki capability
                    // and
                    //      mod/wiki:editpage capability
                    $manage = has_capability('mod/wiki:managewiki', $context);
                    $edit = has_capability('mod/wiki:editpage', $context);
                    return $manage && $edit;
                }
            } else {
                if ($wiki->wikimode == 'individual') {
                    // Individual Mode:
                    // Each person owns a wiki.
                    //
                    // Only the owner of that wiki can edit it
                    if ($subwiki->userid == $USER->id) {
                        return has_capability('mod/wiki:editpage', $context);
                    } else {
                        // Current user is not the owner of that wiki.
                        // User must have:
                        //      mod/wiki:managewiki capability
                        // and
                        //      mod/wiki:editpage capability
                        $manage = has_capability('mod/wiki:managewiki', $context);
                        $edit = has_capability('mod/wiki:editpage', $context);
                        return $manage && $edit;
                    }
                } else {
                    //Error
                    return false;
                }
            }
        default:
            // Error
            return false;
    }
}
Beispiel #3
0
function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm)
{
    global $CFG, $PAGE, $USER;
    require_once $CFG->dirroot . '/mod/wiki/locallib.php';
    $context = context_module::instance($cm->id);
    $url = $PAGE->url;
    $userid = 0;
    if ($module->wikimode == 'individual') {
        $userid = $USER->id;
    }
    if (!($wiki = wiki_get_wiki($cm->instance))) {
        return false;
    }
    if (!($gid = groups_get_activity_group($cm))) {
        $gid = 0;
    }
    if (!($subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid))) {
        return null;
    } else {
        $swid = $subwiki->id;
    }
    $pageid = $url->param('pageid');
    $cmid = $url->param('id');
    if (empty($pageid) && !empty($cmid)) {
        // wiki main page
        $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
        $pageid = $page->id;
    }
    if (wiki_can_create_pages($context)) {
        $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
        $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
    }
    if (is_numeric($pageid)) {
        if (has_capability('mod/wiki:viewpage', $context)) {
            $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
        if (wiki_user_can_edit($subwiki)) {
            $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
        if (has_capability('mod/wiki:viewcomment', $context)) {
            $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
        if (has_capability('mod/wiki:viewpage', $context)) {
            $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
        if (has_capability('mod/wiki:viewpage', $context)) {
            $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
        if (has_capability('mod/wiki:viewpage', $context)) {
            $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
        if (has_capability('mod/wiki:managewiki', $context)) {
            $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
            $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
        }
    }
}
Beispiel #4
0
    // in group mode
    if (!($wiki = wiki_get_wiki($wid))) {
        print_error('incorrectwikiid', 'wiki');
    }
    if (!($subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid))) {
        // create subwiki if doesn't exist
        $subwikiid = wiki_add_subwiki($wiki->id, $currentgroup, $userid);
        $subwiki = wiki_get_subwiki($subwikiid);
    }
} else {
    // no group
    if (!($subwiki = wiki_get_subwiki($page->subwikiid))) {
        print_error('incorrectsubwikiid', 'wiki');
    }
    // Checking wiki instance of that subwiki
    if (!($wiki = wiki_get_wiki($subwiki->wikiid))) {
        print_error('incorrectwikiid', 'wiki');
    }
}
// Checking course module instance
if (!($cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid))) {
    print_error('invalidcoursemodule');
}
// Checking course instance
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$context = context_module::instance($cm->id);
$PAGE->set_url('/mod/wiki/files.php', array('pageid' => $pageid));
require_login($course, true, $cm);
if (!wiki_user_can_view($subwiki, $wiki)) {
    print_error('cannotviewfiles', 'wiki');
}
Beispiel #5
0
$uid = optional_param('uid', 0, PARAM_INT);
// 'create' action must be submitted by moodle form
// so sesskey must be checked
if ($action == 'create') {
    if (!confirm_sesskey()) {
        print_error('invalidsesskey');
    }
}
if (!empty($swid)) {
    $subwiki = wiki_get_subwiki($swid);
    if (!($wiki = wiki_get_wiki($subwiki->wikiid))) {
        print_error('incorrectwikiid', 'wiki');
    }
} else {
    $subwiki = wiki_get_subwiki_by_group($wid, $group, $uid);
    if (!($wiki = wiki_get_wiki($wid))) {
        print_error('incorrectwikiid', 'wiki');
    }
}
if (!($cm = get_coursemodule_from_instance('wiki', $wiki->id))) {
    print_error('invalidcoursemodule');
}
$groups = new stdClass();
if (groups_get_activity_groupmode($cm)) {
    $modulecontext = context_module::instance($cm->id);
    $canaccessgroups = has_capability('moodle/site:accessallgroups', $modulecontext);
    if ($canaccessgroups) {
        $groups->availablegroups = groups_get_all_groups($cm->course);
        $allpart = new stdClass();
        $allpart->id = '0';
        $allpart->name = get_string('allparticipants');
Beispiel #6
0
 /**
  * Creates a new page.
  *
  * @param string $title New page title.
  * @param string $content Page contents.
  * @param int $contentformat Page contents format. If an invalid format is provided, default wiki format is used.
  * @param int $subwikiid The Subwiki ID where to store the page.
  * @param int $wikiid Page\'s wiki ID. Used if subwiki does not exists.
  * @param int $userid Subwiki\'s user ID. Used if subwiki does not exists.
  * @param int $groupid Subwiki\'s group ID. Used if subwiki does not exists.
  * @return array of warnings and page data.
  * @since Moodle 3.1
  */
 public static function new_page($title, $content, $contentformat = null, $subwikiid = null, $wikiid = null, $userid = null, $groupid = null)
 {
     global $USER;
     $params = self::validate_parameters(self::new_page_parameters(), array('title' => $title, 'content' => $content, 'contentformat' => $contentformat, 'subwikiid' => $subwikiid, 'wikiid' => $wikiid, 'userid' => $userid, 'groupid' => $groupid));
     $warnings = array();
     // Get wiki and subwiki instances.
     if (!empty($params['subwikiid'])) {
         if (!($subwiki = wiki_get_subwiki($params['subwikiid']))) {
             throw new moodle_exception('incorrectsubwikiid', 'wiki');
         }
         if (!($wiki = wiki_get_wiki($subwiki->wikiid))) {
             throw new moodle_exception('incorrectwikiid', 'wiki');
         }
         // Permission validation.
         $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
         $context = context_module::instance($cm->id);
         self::validate_context($context);
     } else {
         if (!($wiki = wiki_get_wiki($params['wikiid']))) {
             throw new moodle_exception('incorrectwikiid', 'wiki');
         }
         // Permission validation.
         $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
         $context = context_module::instance($cm->id);
         self::validate_context($context);
         // Determine groupid and userid to use.
         list($groupid, $userid) = self::determine_group_and_user($cm, $wiki, $params['groupid'], $params['userid']);
         // Get subwiki and validate it.
         $subwiki = wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid);
         if ($subwiki === false) {
             // User cannot view page.
             throw new moodle_exception('cannoteditpage', 'wiki');
         } else {
             if ($subwiki->id < 0) {
                 // Subwiki needed to check edit permissions.
                 if (!wiki_user_can_edit($subwiki)) {
                     throw new moodle_exception('cannoteditpage', 'wiki');
                 }
                 // Subwiki does not exists and it can be created.
                 $swid = wiki_add_subwiki($wiki->id, $groupid, $userid);
                 if (!($subwiki = wiki_get_subwiki($swid))) {
                     throw new moodle_exception('incorrectsubwikiid', 'wiki');
                 }
             }
         }
     }
     // Subwiki needed to check edit permissions.
     if (!wiki_user_can_edit($subwiki)) {
         throw new moodle_exception('cannoteditpage', 'wiki');
     }
     if ($page = wiki_get_page_by_title($subwiki->id, $params['title'])) {
         throw new moodle_exception('pageexists', 'wiki');
     }
     // Ignore invalid formats and use default instead.
     if (!$params['contentformat'] || $wiki->forceformat) {
         $params['contentformat'] = $wiki->defaultformat;
     } else {
         $formats = wiki_get_formats();
         if (!in_array($params['contentformat'], $formats)) {
             $params['contentformat'] = $wiki->defaultformat;
         }
     }
     $newpageid = wiki_create_page($subwiki->id, $params['title'], $params['contentformat'], $USER->id);
     if (!($page = wiki_get_page($newpageid))) {
         throw new moodle_exception('incorrectpageid', 'wiki');
     }
     // Save content.
     $save = wiki_save_page($page, $params['content'], $USER->id);
     if (!$save) {
         throw new moodle_exception('savingerror', 'wiki');
     }
     $result = array();
     $result['pageid'] = $page->id;
     $result['warnings'] = $warnings;
     return $result;
 }
Beispiel #7
0
require_once $CFG->dirroot . '/mod/wiki/pagelib.php';
$search = optional_param('searchstring', null, PARAM_ALPHANUMEXT);
$courseid = optional_param('courseid', 0, PARAM_INT);
$searchcontent = optional_param('searchwikicontent', 0, PARAM_INT);
$cmid = optional_param('cmid', 0, PARAM_INT);
$subwikiid = optional_param('subwikiid', 0, PARAM_INT);
$userid = optional_param('uid', 0, PARAM_INT);
if (!($course = $DB->get_record('course', array('id' => $courseid)))) {
    print_error('invalidcourseid');
}
if (!($cm = get_coursemodule_from_id('wiki', $cmid))) {
    print_error('invalidcoursemodule');
}
require_login($course, true, $cm);
// Checking wiki instance
if (!($wiki = wiki_get_wiki($cm->instance))) {
    print_error('incorrectwikiid', 'wiki');
}
if ($subwikiid) {
    // Subwiki id is specified.
    $subwiki = wiki_get_subwiki($subwikiid);
    if (!$subwiki || $subwiki->wikiid != $wiki->id) {
        print_error('incorrectsubwikiid', 'wiki');
    }
} else {
    // Getting current group id
    $gid = groups_get_activity_group($cm);
    // Getting current user id
    if ($wiki->wikimode == 'individual') {
        $userid = $userid ? $userid : $USER->id;
    } else {
Beispiel #8
0
function wiki_git_commit($arr)
{
    $files = array_key_exists('files', $arr) ? $arr['files'] : null;
    $commit_msg = array_key_exists('commit_msg', $arr) ? $arr['commit_msg'] : 'Repo updated';
    $resource_id = array_key_exists('resource_id', $arr) ? $arr['resource_id'] : json_return_and_die(array('message' => 'Wiki resource_id required for git commit', 'success' => false));
    $observer = array_key_exists('observer', $arr) ? $arr['observer'] : json_return_and_die(array('message' => 'Observer required for git commit', 'success' => false));
    $w = wiki_get_wiki($resource_id);
    if (!$w['path']) {
        return array('message' => 'Error reading wiki', 'success' => false);
    }
    $reponame = array_key_exists('title', $w['wiki']) ? urlencode($w['wiki']['title']) : 'repo';
    if ($reponame === '') {
        $reponame = 'repo';
    }
    $git = new GitRepo($observer['xchan_addr'], null, false, $w['wiki']['title'], $w['path']);
    try {
        $git->setIdentity($observer['xchan_name'], $observer['xchan_addr']);
        if ($files === null) {
            $options = array('all' => true);
            // git commit option to include all changes
        } else {
            $options = array();
            // git commit options
            foreach ($files as $file) {
                if (!$git->git->add($file)) {
                    // add specified files to the git repo stage
                    if (!$git->git->reset->hard()) {
                        json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false));
                    }
                    json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file, 'success' => false));
                }
            }
        }
        if ($git->commit($commit_msg, $options)) {
            json_return_and_die(array('message' => 'Wiki repo commit succeeded', 'success' => true));
        } else {
            json_return_and_die(array('message' => 'Wiki repo commit failed', 'success' => false));
        }
    } catch (\PHPGit\Exception\GitException $e) {
        json_return_and_die(array('message' => 'GitRepo error thrown', 'success' => false));
    }
}
Beispiel #9
0
 /**
  * Trigger the course module viewed event and update the module completion status.
  *
  * @param int $wikiid The wiki instance ID.
  * @return array of warnings and status result.
  * @since Moodle 3.1
  */
 public static function view_wiki($wikiid)
 {
     $params = self::validate_parameters(self::view_wiki_parameters(), array('wikiid' => $wikiid));
     $warnings = array();
     // Get wiki instance.
     if (!($wiki = wiki_get_wiki($params['wikiid']))) {
         throw new moodle_exception('incorrectwikiid', 'wiki');
     }
     // Permission validation.
     list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     // Check if user can view this wiki.
     // We don't use wiki_user_can_view because it requires to have a valid subwiki for the user.
     if (!has_capability('mod/wiki:viewpage', $context)) {
         throw new moodle_exception('cannotviewpage', 'wiki');
     }
     // Trigger course_module_viewed event and completion.
     wiki_view($wiki, $course, $cm, $context);
     $result = array();
     $result['status'] = true;
     $result['warnings'] = $warnings;
     return $result;
 }
Beispiel #10
0
 function post()
 {
     require_once 'include/wiki.php';
     // /wiki/channel/preview
     // Render mardown-formatted text in HTML for preview
     if (argc() > 2 && argv(2) === 'preview') {
         $content = $_POST['content'];
         $resource_id = $_POST['resource_id'];
         require_once 'library/markdown.php';
         $html = wiki_generate_toc(purify_html(Markdown($content)));
         $w = wiki_get_wiki($resource_id);
         $wikiURL = argv(0) . '/' . argv(1) . '/' . $w['urlName'];
         $html = wiki_convert_links($html, $wikiURL);
         json_return_and_die(array('html' => $html, 'success' => true));
     }
     // Create a new wiki
     // /wiki/channel/create/wiki
     if (argc() > 3 && argv(2) === 'create' && argv(3) === 'wiki') {
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         // Determine if observer has permission to create wiki
         $observer_hash = get_observer_hash();
         // Only the channel owner can create a wiki, at least until we create a
         // more detail permissions framework
         if (local_channel() !== intval($channel['channel_id'])) {
             goaway('/' . argv(0) . '/' . $nick . '/');
         }
         $wiki = array();
         // Generate new wiki info from input name
         $wiki['postVisible'] = intval($_POST['postVisible']) === 0 ? 0 : 1;
         $wiki['rawName'] = $_POST['wikiName'];
         $wiki['htmlName'] = escape_tags($_POST['wikiName']);
         $wiki['urlName'] = urlencode($_POST['wikiName']);
         if ($wiki['urlName'] === '') {
             notice('Error creating wiki. Invalid name.');
             goaway('/wiki');
         }
         // Get ACL for permissions
         $acl = new \Zotlabs\Access\AccessList($channel);
         $acl->set_from_array($_POST);
         $r = wiki_create_wiki($channel, $observer_hash, $wiki, $acl);
         if ($r['success']) {
             $homePage = wiki_create_page('Home', $r['item']['resource_id']);
             if (!$homePage['success']) {
                 notice('Wiki created, but error creating Home page.');
                 goaway('/wiki/' . $nick . '/' . $wiki['urlName']);
             }
             goaway('/wiki/' . $nick . '/' . $wiki['urlName'] . '/' . $homePage['page']['urlName']);
         } else {
             notice('Error creating wiki');
             goaway('/wiki');
         }
     }
     // Delete a wiki
     if (argc() > 3 && argv(2) === 'delete' && argv(3) === 'wiki') {
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         // Only the channel owner can delete a wiki, at least until we create a
         // more detail permissions framework
         if (local_channel() !== intval($channel['channel_id'])) {
             logger('Wiki delete permission denied.' . EOL);
             json_return_and_die(array('message' => 'Wiki delete permission denied.', 'success' => false));
         }
         $resource_id = $_POST['resource_id'];
         $deleted = wiki_delete_wiki($resource_id);
         if ($deleted['success']) {
             json_return_and_die(array('message' => '', 'success' => true));
         } else {
             logger('Error deleting wiki: ' . $resource_id);
             json_return_and_die(array('message' => 'Error deleting wiki', 'success' => false));
         }
     }
     // Create a page
     if (argc() === 4 && argv(2) === 'create' && argv(3) === 'page') {
         $nick = argv(1);
         $resource_id = $_POST['resource_id'];
         // Determine if observer has permission to create a page
         $channel = get_channel_by_nick($nick);
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['write']) {
                 logger('Wiki write permission denied. ' . EOL);
                 json_return_and_die(array('success' => false));
             }
         }
         $name = $_POST['name'];
         //Get new page name
         if (urlencode(escape_tags($_POST['name'])) === '') {
             json_return_and_die(array('message' => 'Error creating page. Invalid name.', 'success' => false));
         }
         $page = wiki_create_page($name, $resource_id);
         if ($page['success']) {
             json_return_and_die(array('url' => '/' . argv(0) . '/' . argv(1) . '/' . $page['wiki']['urlName'] . '/' . urlencode($page['page']['urlName']), 'success' => true));
         } else {
             logger('Error creating page');
             json_return_and_die(array('message' => 'Error creating page.', 'success' => false));
         }
     }
     // Fetch page list for a wiki
     if (argc() === 5 && argv(2) === 'get' && argv(3) === 'page' && argv(4) === 'list') {
         $resource_id = $_POST['resource_id'];
         // resource_id for wiki in db
         $channel = get_channel_by_nick(argv(1));
         $observer_hash = get_observer_hash();
         if (local_channel() !== intval($channel['channel_id'])) {
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['read']) {
                 logger('Wiki read permission denied.' . EOL);
                 json_return_and_die(array('pages' => null, 'message' => 'Permission denied.', 'success' => false));
             }
         }
         $page_list_html = widget_wiki_pages(array('resource_id' => $resource_id, 'refresh' => true, 'channel' => argv(1)));
         json_return_and_die(array('pages' => $page_list_html, 'message' => '', 'success' => true));
     }
     // Save a page
     if (argc() === 4 && argv(2) === 'save' && argv(3) === 'page') {
         $resource_id = $_POST['resource_id'];
         $pageUrlName = $_POST['name'];
         $pageHtmlName = escape_tags($_POST['name']);
         $content = $_POST['content'];
         //Get new content
         $commitMsg = $_POST['commitMsg'];
         if ($commitMsg === '') {
             $commitMsg = 'Updated ' . $pageHtmlName;
         }
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         // Determine if observer has permission to save content
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['write']) {
                 logger('Wiki write permission denied. ' . EOL);
                 json_return_and_die(array('success' => false));
             }
         }
         $saved = wiki_save_page(array('resource_id' => $resource_id, 'pageUrlName' => $pageUrlName, 'content' => $content));
         if ($saved['success']) {
             $ob = \App::get_observer();
             $commit = wiki_git_commit(array('commit_msg' => $commitMsg, 'resource_id' => $resource_id, 'observer' => $ob, 'files' => array($pageUrlName . '.md')));
             if ($commit['success']) {
                 json_return_and_die(array('message' => 'Wiki git repo commit made', 'success' => true));
             } else {
                 json_return_and_die(array('message' => 'Error making git commit', 'success' => false));
             }
         } else {
             json_return_and_die(array('message' => 'Error saving page', 'success' => false));
         }
     }
     // Update page history
     // /wiki/channel/history/page
     if (argc() === 4 && argv(2) === 'history' && argv(3) === 'page') {
         $resource_id = $_POST['resource_id'];
         $pageUrlName = $_POST['name'];
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         // Determine if observer has permission to read content
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['read']) {
                 logger('Wiki read permission denied.' . EOL);
                 json_return_and_die(array('historyHTML' => '', 'message' => 'Permission denied.', 'success' => false));
             }
         }
         $historyHTML = widget_wiki_page_history(array('resource_id' => $resource_id, 'pageUrlName' => $pageUrlName));
         json_return_and_die(array('historyHTML' => $historyHTML, 'message' => '', 'success' => true));
     }
     // Delete a page
     if (argc() === 4 && argv(2) === 'delete' && argv(3) === 'page') {
         $resource_id = $_POST['resource_id'];
         $pageUrlName = $_POST['name'];
         if ($pageUrlName === 'Home') {
             json_return_and_die(array('message' => 'Cannot delete Home', 'success' => false));
         }
         // Determine if observer has permission to delete pages
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['write']) {
                 logger('Wiki write permission denied. ' . EOL);
                 json_return_and_die(array('success' => false));
             }
         }
         $deleted = wiki_delete_page(array('resource_id' => $resource_id, 'pageUrlName' => $pageUrlName));
         if ($deleted['success']) {
             $ob = \App::get_observer();
             $commit = wiki_git_commit(array('commit_msg' => 'Deleted ' . $pageUrlName, 'resource_id' => $resource_id, 'observer' => $ob, 'files' => null));
             if ($commit['success']) {
                 json_return_and_die(array('message' => 'Wiki git repo commit made', 'success' => true));
             } else {
                 json_return_and_die(array('message' => 'Error making git commit', 'success' => false));
             }
         } else {
             json_return_and_die(array('message' => 'Error deleting page', 'success' => false));
         }
     }
     // Revert a page
     if (argc() === 4 && argv(2) === 'revert' && argv(3) === 'page') {
         $resource_id = $_POST['resource_id'];
         $pageUrlName = $_POST['name'];
         $commitHash = $_POST['commitHash'];
         // Determine if observer has permission to revert pages
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['write']) {
                 logger('Wiki write permission denied.' . EOL);
                 json_return_and_die(array('success' => false));
             }
         }
         $reverted = wiki_revert_page(array('commitHash' => $commitHash, 'resource_id' => $resource_id, 'pageUrlName' => $pageUrlName));
         if ($reverted['success']) {
             json_return_and_die(array('content' => $reverted['content'], 'message' => '', 'success' => true));
         } else {
             json_return_and_die(array('content' => '', 'message' => 'Error reverting page', 'success' => false));
         }
     }
     // Compare page revisions
     if (argc() === 4 && argv(2) === 'compare' && argv(3) === 'page') {
         $resource_id = $_POST['resource_id'];
         $pageUrlName = $_POST['name'];
         $compareCommit = $_POST['compareCommit'];
         $currentCommit = $_POST['currentCommit'];
         // Determine if observer has permission to revert pages
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['read']) {
                 logger('Wiki read permission denied.' . EOL);
                 json_return_and_die(array('success' => false));
             }
         }
         $compare = wiki_compare_page(array('currentCommit' => $currentCommit, 'compareCommit' => $compareCommit, 'resource_id' => $resource_id, 'pageUrlName' => $pageUrlName));
         if ($compare['success']) {
             $diffHTML = '<table class="text-center" width="100%"><tr><td class="lead" width="50%">Current Revision</td><td class="lead" width="50%">Selected Revision</td></tr></table>' . $compare['diff'];
             json_return_and_die(array('diff' => $diffHTML, 'message' => '', 'success' => true));
         } else {
             json_return_and_die(array('diff' => '', 'message' => 'Error comparing page', 'success' => false));
         }
     }
     // Rename a page
     if (argc() === 4 && argv(2) === 'rename' && argv(3) === 'page') {
         $resource_id = $_POST['resource_id'];
         $pageUrlName = $_POST['oldName'];
         $pageNewName = $_POST['newName'];
         if ($pageUrlName === 'Home') {
             json_return_and_die(array('message' => 'Cannot rename Home', 'success' => false));
         }
         if (urlencode(escape_tags($pageNewName)) === '') {
             json_return_and_die(array('message' => 'Error renaming page. Invalid name.', 'success' => false));
         }
         // Determine if observer has permission to rename pages
         $nick = argv(1);
         $channel = get_channel_by_nick($nick);
         if (local_channel() !== intval($channel['channel_id'])) {
             $observer_hash = get_observer_hash();
             $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash);
             if (!$perms['write']) {
                 logger('Wiki write permission denied. ' . EOL);
                 json_return_and_die(array('success' => false));
             }
         }
         $renamed = wiki_rename_page(array('resource_id' => $resource_id, 'pageUrlName' => $pageUrlName, 'pageNewName' => $pageNewName));
         if ($renamed['success']) {
             $ob = \App::get_observer();
             $commit = wiki_git_commit(array('commit_msg' => 'Renamed ' . urldecode($pageUrlName) . ' to ' . $renamed['page']['htmlName'], 'resource_id' => $resource_id, 'observer' => $ob, 'files' => array($pageUrlName . '.md', $renamed['page']['fileName']), 'all' => true));
             if ($commit['success']) {
                 json_return_and_die(array('name' => $renamed['page'], 'message' => 'Wiki git repo commit made', 'success' => true));
             } else {
                 json_return_and_die(array('message' => 'Error making git commit', 'success' => false));
             }
         } else {
             json_return_and_die(array('message' => 'Error renaming page', 'success' => false));
         }
     }
     //notice('You must be authenticated.');
     json_return_and_die(array('message' => 'You must be authenticated.', 'success' => false));
 }
Beispiel #11
0
 /**
  * Returns the list of subwikis the user can see in a specific wiki.
  *
  * @param int $wikiid The wiki instance ID.
  * @return array Containing a list of warnings and a list of subwikis.
  * @since Moodle 3.1
  */
 public static function get_subwikis($wikiid)
 {
     global $USER;
     $warnings = array();
     $params = self::validate_parameters(self::get_subwikis_parameters(), array('wikiid' => $wikiid));
     // Get wiki instance.
     if (!($wiki = wiki_get_wiki($params['wikiid']))) {
         throw new moodle_exception('incorrectwikiid', 'wiki');
     }
     // Validate context and capabilities.
     list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     require_capability('mod/wiki:viewpage', $context);
     $returnedsubwikis = wiki_get_visible_subwikis($wiki, $cm, $context);
     foreach ($returnedsubwikis as $subwiki) {
         $subwiki->canedit = wiki_user_can_edit($subwiki);
     }
     $result = array();
     $result['subwikis'] = $returnedsubwikis;
     $result['warnings'] = $warnings;
     return $result;
 }
Beispiel #12
0
 /**
  * Returns the list of pages from a specific subwiki.
  *
  * @param int $wikiid The wiki instance ID.
  * @param int $groupid The group ID. If not defined, use current group.
  * @param int $userid The user ID. If not defined, use current user.
  * @param array $options Several options like sort by, sort direction, ...
  * @return array Containing a list of warnings and a list of pages.
  * @since Moodle 3.1
  */
 public static function get_subwiki_pages($wikiid, $groupid = -1, $userid = 0, $options = array())
 {
     global $USER, $DB;
     $returnedpages = array();
     $warnings = array();
     $params = self::validate_parameters(self::get_subwiki_pages_parameters(), array('wikiid' => $wikiid, 'groupid' => $groupid, 'userid' => $userid, 'options' => $options));
     // Get wiki instance.
     if (!($wiki = wiki_get_wiki($params['wikiid']))) {
         throw new moodle_exception('incorrectwikiid', 'wiki');
     }
     list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     // Determine group.
     $groupmode = groups_get_activity_groupmode($cm);
     if ($groupmode == NOGROUPS) {
         $groupid = 0;
     } else {
         if ($params['groupid'] == -1) {
             // Use current group.
             $groupid = groups_get_activity_group($cm);
             $groupid = !empty($groupid) ? $groupid : 0;
         } else {
             $groupid = $params['groupid'];
         }
     }
     // Determine user.
     if ($wiki->wikimode == 'collaborative') {
         // Collaborative wikis don't use userid in subwikis.
         $userid = 0;
     } else {
         if (empty($params['userid'])) {
             // Use current user.
             $userid = $USER->id;
         } else {
             $userid = $params['userid'];
         }
     }
     // Get subwiki based on group and user.
     if (!($subwiki = wiki_get_subwiki_by_group($cm->instance, $groupid, $userid))) {
         // The subwiki doesn't exist.
         // Validate if user is valid.
         if ($userid != 0 && $userid != $USER->id && !($user = $DB->get_record('user', array('id' => $userid)))) {
             throw new moodle_exception('invaliduserid', 'error');
         }
         // Validate that groupid is valid.
         if ($groupid != 0 && !groups_group_exists($groupid)) {
             throw new moodle_exception('cannotfindgroup', 'error');
         }
         // Valid data but subwiki not found. We'll simulate a subwiki object to check if the user would be able to see it
         // if it existed. If he's able to see it then we'll return an empty array because the subwiki has no pages.
         $subwiki = new stdClass();
         $subwiki->wikiid = $wiki->id;
         $subwiki->userid = $userid;
         $subwiki->groupid = $groupid;
         // Check that the user can view the subwiki. This function checks capabilities.
         if (!wiki_user_can_view($subwiki, $wiki)) {
             throw new moodle_exception('cannotviewpage', 'wiki');
         }
     } else {
         // Check that the user can view the subwiki. This function checks capabilities.
         if (!wiki_user_can_view($subwiki, $wiki)) {
             throw new moodle_exception('cannotviewpage', 'wiki');
         }
         // Set sort param.
         $options = $params['options'];
         if (!empty($options['sortby'])) {
             if ($options['sortdirection'] != 'ASC' && $options['sortdirection'] != 'DESC') {
                 // Invalid sort direction. Use default.
                 $options['sortdirection'] = 'ASC';
             }
             $sort = $options['sortby'] . ' ' . $options['sortdirection'];
         }
         $pages = wiki_get_page_list($subwiki->id, $sort);
         $caneditpages = wiki_user_can_edit($subwiki);
         $firstpage = wiki_get_first_page($subwiki->id);
         foreach ($pages as $page) {
             $retpage = array('id' => $page->id, 'subwikiid' => $page->subwikiid, 'title' => external_format_string($page->title, $context->id), 'timecreated' => $page->timecreated, 'timemodified' => $page->timemodified, 'timerendered' => $page->timerendered, 'userid' => $page->userid, 'pageviews' => $page->pageviews, 'readonly' => $page->readonly, 'caneditpage' => $caneditpages, 'firstpage' => $page->id == $firstpage->id);
             if ($options['includecontent']) {
                 // Refresh page cached content if needed.
                 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
                     if ($content = wiki_refresh_cachedcontent($page)) {
                         $page = $content['page'];
                     }
                 }
                 list($retpage['cachedcontent'], $retpage['contentformat']) = external_format_text($page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
             }
             $returnedpages[] = $retpage;
         }
     }
     $result = array();
     $result['pages'] = $returnedpages;
     $result['warnings'] = $warnings;
     return $result;
 }