Beispiel #1
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())
 {
     $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 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) {
         throw new moodle_exception('cannotviewpage', 'wiki');
     } else {
         if ($subwiki->id != -1) {
             // 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);
                 // Refresh page cached content if needed.
                 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
                     if ($content = wiki_refresh_cachedcontent($page)) {
                         $page = $content['page'];
                     }
                 }
                 list($cachedcontent, $contentformat) = external_format_text($page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
                 if ($options['includecontent']) {
                     // Return the page content.
                     $retpage['cachedcontent'] = $cachedcontent;
                     $retpage['contentformat'] = $contentformat;
                 } else {
                     // Return the size of the content.
                     if (function_exists('mb_strlen') && (int) ini_get('mbstring.func_overload') & 2) {
                         $retpage['contentsize'] = mb_strlen($cachedcontent, '8bit');
                     } else {
                         $retpage['contentsize'] = strlen($cachedcontent);
                     }
                 }
                 $returnedpages[] = $retpage;
             }
         }
     }
     $result = array();
     $result['pages'] = $returnedpages;
     $result['warnings'] = $warnings;
     return $result;
 }
Beispiel #2
0
    // Getting current user id
    if ($wiki->wikimode == 'individual') {
        $userid = $USER->id;
    } else {
        $userid = 0;
    }

    // Getting subwiki. If it does not exists, redirecting to create page
    if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid)) {
        $params = array('wid' => $wiki->id, 'group' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
        $url = new moodle_url('/mod/wiki/create.php', $params);
        redirect($url);
    }

    // Getting first page. If it does not exists, redirecting to create page
    if (!$page = wiki_get_first_page($subwiki->id, $wiki)) {
        $params = array('swid'=>$subwiki->id, 'title'=>$wiki->firstpagetitle);
        $url = new moodle_url('/mod/wiki/create.php', $params);
        redirect($url);
    }

    /*
     * Case 1:
     *
     * A user wants to see a page.
     *
     * URL Params: pageid -> page id
     *
     */
} elseif ($pageid) {
Beispiel #3
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;
 }