Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/wiki/view.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
00017 
00034 require_once('../../config.php');
00035 require_once($CFG->dirroot . '/mod/wiki/lib.php');
00036 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
00037 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
00038 
00039 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
00040 
00041 $pageid = optional_param('pageid', 0, PARAM_INT); // Page ID
00042 
00043 $wid = optional_param('wid', 0, PARAM_INT); // Wiki ID
00044 $title = optional_param('title', '', PARAM_TEXT); // Page Title
00045 $currentgroup = optional_param('group', 0, PARAM_INT); // Group ID
00046 $userid = optional_param('uid', 0, PARAM_INT); // User ID
00047 $groupanduser = optional_param('groupanduser', 0, PARAM_TEXT);
00048 
00049 $edit = optional_param('edit', -1, PARAM_BOOL);
00050 
00051 $action = optional_param('action', '', PARAM_ALPHA);
00052 $swid = optional_param('swid', 0, PARAM_INT); // Subwiki ID
00053 
00054 /*
00055  * Case 0:
00056  *
00057  * User that comes from a course. First wiki page must be shown
00058  *
00059  * URL params: id -> course module id
00060  *
00061  */
00062 if ($id) {
00063     // Cheacking course module instance
00064     if (!$cm = get_coursemodule_from_id('wiki', $id)) {
00065         print_error('invalidcoursemodule');
00066     }
00067 
00068     // Checking course instance
00069     $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
00070 
00071     // Checking wiki instance
00072     if (!$wiki = wiki_get_wiki($cm->instance)) {
00073         print_error('incorrectwikiid', 'wiki');
00074     }
00075     $PAGE->set_cm($cm);
00076 
00077     // Getting the subwiki corresponding to that wiki, group and user.
00078     //
00079     // Also setting the page if it exists or getting the first page title form
00080     // that wiki
00081 
00082     // Getting current group id
00083     $currentgroup = groups_get_activity_group($cm);
00084     $currentgroup = !empty($currentgroup) ? $currentgroup : 0;
00085     // Getting current user id
00086     if ($wiki->wikimode == 'individual') {
00087         $userid = $USER->id;
00088     } else {
00089         $userid = 0;
00090     }
00091 
00092     // Getting subwiki. If it does not exists, redirecting to create page
00093     if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid)) {
00094         $params = array('wid' => $wiki->id, 'gid' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
00095         $url = new moodle_url('/mod/wiki/create.php', $params);
00096         redirect($url);
00097     }
00098 
00099     // Getting first page. If it does not exists, redirecting to create page
00100     if (!$page = wiki_get_first_page($subwiki->id, $wiki)) {
00101         $params = array('swid'=>$subwiki->id, 'title'=>$wiki->firstpagetitle);
00102         $url = new moodle_url('/mod/wiki/create.php', $params);
00103         redirect($url);
00104     }
00105 
00106     /*
00107      * Case 1:
00108      *
00109      * A user wants to see a page.
00110      *
00111      * URL Params: pageid -> page id
00112      *
00113      */
00114 } elseif ($pageid) {
00115 
00116     // Checking page instance
00117     if (!$page = wiki_get_page($pageid)) {
00118         print_error('incorrectpageid', 'wiki');
00119     }
00120 
00121     // Checking subwiki
00122     if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
00123         print_error('incorrectsubwikiid', 'wiki');
00124     }
00125 
00126     // Checking wiki instance of that subwiki
00127     if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
00128         print_error('incorrectwikiid', 'wiki');
00129     }
00130 
00131     // Checking course module instance
00132     if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
00133         print_error('invalidcoursemodule');
00134     }
00135 
00136     // Checking course instance
00137     $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
00138 
00139     /*
00140      * Case 2:
00141      *
00142      * Trying to read a page from another group or user
00143      *
00144      * Page can exists or not.
00145      *  * If it exists, page must be shown
00146      *  * If it does not exists, system must ask for its creation
00147      *
00148      * URL params: wid -> subwiki id (required)
00149      *             title -> a page title (required)
00150      *             group -> group id (optional)
00151      *             uid -> user id (optional)
00152      *             groupanduser -> (optional)
00153      */
00154 } elseif ($wid && $title) {
00155 
00156     // Setting wiki instance
00157     if (!$wiki = wiki_get_wiki($wid)) {
00158         print_error('incorrectwikiid', 'wiki');
00159     }
00160 
00161     // Checking course module
00162     if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
00163         print_error('invalidcoursemodule');
00164     }
00165 
00166     // Checking course instance
00167     if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
00168         print_error('coursemisconf');
00169     }
00170 
00171     $groupmode = groups_get_activity_groupmode($cm);
00172     if (empty($currentgroup)) {
00173         $currentgroup = groups_get_activity_group($cm);
00174         $currentgroup = !empty($currentgroup) ? $currentgroup : 0;
00175     }
00176 
00177     if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
00178         list($gid, $uid) = explode('-', $groupanduser);
00179     } else if ($wiki->wikimode == 'individual') {
00180         $gid = 0;
00181         $uid = $userid;
00182     } else if ($groupmode == NOGROUPS) {
00183         $gid = 0;
00184         $uid = 0;
00185     } else {
00186         $gid = $currentgroup;
00187         $uid = 0;
00188     }
00189 
00190     // Getting subwiki instance. If it does not exists, redirect to create page
00191     if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
00192         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00193 
00194         $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
00195         $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
00196 
00197         $manage = has_capability('mod/wiki:managewiki', $context);
00198         $edit = has_capability('mod/wiki:editpage', $context);
00199         $manageandedit = $manage && $edit;
00200 
00201         if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
00202             print_error('nocontent','wiki');
00203         }
00204 
00205         $params = array('wid' => $wiki->id, 'gid' => $gid, 'uid' => $uid, 'title' => $title);
00206         $url = new moodle_url('/mod/wiki/create.php', $params);
00207         redirect($url);
00208     }
00209 
00210     // Checking is there is a page with this title. If it does not exists, redirect to first page
00211     if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
00212         $params = array('wid' => $wiki->id, 'gid' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
00213         $url = new moodle_url('/mod/wiki/view.php', $params);
00214         redirect($url);
00215     }
00216 
00217     //    /*
00218     //     * Case 3:
00219     //     *
00220     //     * A user switches group when is 'reading' a non-existent page.
00221     //     *
00222     //     * URL Params: wid -> wiki id
00223     //     *             title -> page title
00224     //     *             currentgroup -> group id
00225     //     *
00226     //     */
00227     //} elseif ($wid && $title && $currentgroup) {
00228     //
00229     //    // Checking wiki instance
00230     //    if (!$wiki = wiki_get_wiki($wid)) {
00231     //        print_error('incorrectwikiid', 'wiki');
00232     //    }
00233     //
00234     //    // Checking subwiki instance
00235     //    // @TODO: Fix call to wiki_get_subwiki_by_group
00236     //    if (!$currentgroup = groups_get_activity_group($cm)){
00237     //        $currentgroup = 0;
00238     //    }
00239     //    if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
00240     //        print_error('incorrectsubwikiid', 'wiki');
00241     //    }
00242     //
00243     //    // Checking page instance
00244     //    if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
00245     //        unset($title);
00246     //    }
00247     //
00248     //    // Checking course instance
00249     //    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
00250     //
00251     //    // Checking course module instance
00252     //    if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
00253     //        print_error('invalidcoursemodule');
00254     //    }
00255     //
00256     //    $subwiki = null;
00257     //    $page = null;
00258     //
00259     //    /*
00260     //     * Case 4:
00261     //     *
00262     //     * Error. No more options
00263     //     */
00264 } else {
00265     print_error('incorrectparameters');
00266 }
00267 require_login($course, true, $cm);
00268 
00269 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00270 require_capability('mod/wiki:viewpage', $context);
00271 
00272 add_to_log($course->id, 'wiki', 'view', 'view.php?id=' . $cm->id, $wiki->id);
00273 
00274 // Update 'viewed' state if required by completion system
00275 require_once($CFG->libdir . '/completionlib.php');
00276 $completion = new completion_info($course);
00277 $completion->set_module_viewed($cm);
00278 
00279 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
00280     $USER->editing = $edit;
00281 }
00282 
00283 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
00284 
00285 /*The following piece of code is used in order
00286  * to perform set_url correctly. It is necessary in order
00287  * to make page_wiki_view class know that this page
00288  * has been called via its id.
00289  */
00290 if ($id) {
00291     $wikipage->set_coursemodule($id);
00292 }
00293 
00294 $wikipage->set_gid($currentgroup);
00295 $wikipage->set_page($page);
00296 
00297 $wikipage->print_header();
00298 $wikipage->print_content();
00299 
00300 $wikipage->print_footer();
 All Data Structures Namespaces Files Functions Variables Enumerations