1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18require_once('../../config.php');
19require_once(__DIR__ . '/create_form.php');
20require_once($CFG->dirroot . '/mod/wiki/lib.php');
21require_once($CFG->dirroot . '/mod/wiki/locallib.php');
22require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
23
24// this page accepts two actions: new and create
25// 'new' action will display a form contains page title and page format
26// selections
27// 'create' action will create a new page in db, and redirect to
28// page editing page.
29$action = optional_param('action', 'new', PARAM_TEXT);
30$wid = optional_param('wid', 0, PARAM_INT);
31$swid = optional_param('swid', 0, PARAM_INT);
32$group = optional_param('group', 0, PARAM_INT);
33$uid = optional_param('uid', 0, PARAM_INT);
34
35// 'create' action must be submitted by moodle form
36// so sesskey must be checked
37if ($action == 'create') {
38    if (!confirm_sesskey()) {
39        print_error('invalidsesskey');
40    }
41}
42
43if (!empty($swid)) {
44    $subwiki = wiki_get_subwiki($swid);
45
46    if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
47        print_error('incorrectwikiid', 'wiki');
48    }
49
50} else {
51    $subwiki = wiki_get_subwiki_by_group($wid, $group, $uid);
52
53    if (!$wiki = wiki_get_wiki($wid)) {
54        print_error('incorrectwikiid', 'wiki');
55    }
56
57}
58
59if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
60    print_error('invalidcoursemodule');
61}
62
63$groups = new stdClass();
64if (groups_get_activity_groupmode($cm)) {
65    $modulecontext = context_module::instance($cm->id);
66    $canaccessgroups = has_capability('moodle/site:accessallgroups', $modulecontext);
67    if ($canaccessgroups) {
68        $groups->availablegroups = groups_get_all_groups($cm->course);
69        $allpart = new stdClass();
70        $allpart->id = '0';
71        $allpart->name = get_string('allparticipants');
72        array_unshift($groups->availablegroups, $allpart);
73    } else {
74        $groups->availablegroups = groups_get_all_groups($cm->course, $USER->id);
75    }
76    if (!empty($group)) {
77        $groups->currentgroup = $group;
78    } else {
79        $groups->currentgroup = groups_get_activity_group($cm);
80    }
81}
82
83$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
84
85require_login($course, true, $cm);
86
87// The title of the new page, cannot be empty.
88$title = optional_param('title', get_string('newpage', 'wiki'), PARAM_TEXT);
89$wikipage = new page_wiki_create($wiki, $subwiki, $cm);
90
91if (!empty($swid)) {
92    $wikipage->set_gid($subwiki->groupid);
93    $wikipage->set_uid($subwiki->userid);
94    $wikipage->set_swid($swid);
95} else {
96    $wikipage->set_wid($wid);
97    $wikipage->set_gid($group);
98    $wikipage->set_uid($uid);
99}
100
101$wikipage->set_availablegroups($groups);
102$wikipage->set_title($title);
103
104// set page action, and initialise moodle form
105$wikipage->set_action($action);
106
107switch ($action) {
108case 'create':
109    $newpageid = $wikipage->create_page($title);
110    redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid='.$newpageid);
111    break;
112case 'new':
113    // Go straight to editing if we know the page title and we're in force format mode.
114    if ((int)$wiki->forceformat == 1 && $title != get_string('newpage', 'wiki')) {
115        $newpageid = $wikipage->create_page($title);
116        redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid='.$newpageid);
117    } else {
118        $wikipage->print_header();
119        // Create a new page.
120        $wikipage->print_content($title);
121    }
122    $wikipage->print_footer();
123    break;
124}
125