|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00026 defined('MOODLE_INTERNAL') || die(); 00027 00028 class mod_wiki_renderer extends plugin_renderer_base { 00029 public function page_index() { 00030 global $CFG; 00031 $output = ''; 00032 // Checking wiki instance 00033 if (!$wiki = wiki_get_wiki($this->page->cm->instance)) { 00034 return false; 00035 } 00036 00037 // @TODO: Fix call to wiki_get_subwiki_by_group 00038 $gid = groups_get_activity_group($this->page->cm); 00039 $gid = !empty($gid) ? $gid : 0; 00040 if (!$subwiki = wiki_get_subwiki_by_group($this->page->cm->instance, $gid)) { 00041 return false; 00042 } 00043 $swid = $subwiki->id; 00044 $pages = wiki_get_page_list($swid); 00045 $selectoptions = array(); 00046 foreach ($pages as $page) { 00047 $selectoptions[$page->id] = $page->title; 00048 } 00049 $label = get_string('pageindex', 'wiki') . ': '; 00050 $select = new single_select(new moodle_url('/mod/wiki/view.php'), 'pageid', $selectoptions); 00051 $select->label = $label; 00052 return $this->output->container($this->output->render($select), 'wiki_index'); 00053 } 00054 00055 public function search_result($records, $subwiki) { 00056 global $CFG, $PAGE; 00057 $table = new html_table(); 00058 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id); 00059 $strsearchresults = get_string('searchresult', 'wiki'); 00060 $totalcount = count($records); 00061 $html = $this->output->heading("$strsearchresults $totalcount"); 00062 foreach ($records as $page) { 00063 $table->head = array('title' => format_string($page->title) . ' (' . html_writer::link($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $page->id, get_string('view', 'wiki')) . ')'); 00064 $table->align = array('title' => 'left'); 00065 $table->width = '100%'; 00066 $table->data = array(array(file_rewrite_pluginfile_urls(format_text($page->cachedcontent, FORMAT_HTML), 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $subwiki->id))); 00067 $table->colclasses = array('wikisearchresults'); 00068 $html .= html_writer::table($table); 00069 } 00070 $html = html_writer::tag('div', $html, array('class'=>'no-overflow')); 00071 return $this->output->container($html); 00072 } 00073 00074 public function diff($pageid, $old, $new, $options = array()) { 00075 global $CFG; 00076 if (!empty($options['total'])) { 00077 $total = $options['total']; 00078 } else { 00079 $total = 0; 00080 } 00081 $diff1 = format_text($old->diff, FORMAT_HTML, array('overflowdiv'=>true)); 00082 $diff2 = format_text($new->diff, FORMAT_HTML, array('overflowdiv'=>true)); 00083 $strdatetime = get_string('strftimedatetime', 'langconfig'); 00084 00085 $olduser = $old->user; 00086 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $old->id)); 00087 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $pageid, 'versionid' => $old->id)); 00088 $userlink = new moodle_url('/user/view.php', array('id' => $olduser->id)); 00089 // view version link 00090 $oldversionview = ' '; 00091 $oldversionview .= html_writer::link($versionlink->out(false), get_string('view', 'wiki'), array('class' => 'wiki_diffview')); 00092 $oldversionview .= ' '; 00093 // restore version link 00094 $oldversionview .= html_writer::link($restorelink->out(false), get_string('restore', 'wiki'), array('class' => 'wiki_diffview')); 00095 00096 // userinfo container 00097 $oldheading = $this->output->container_start('wiki_diffuserleft'); 00098 // username 00099 $oldheading .= html_writer::link($CFG->wwwroot . '/user/view.php?id=' . $olduser->id, fullname($olduser)) . ' '; 00100 // user picture 00101 $oldheading .= html_writer::link($userlink->out(false), $this->output->user_picture($olduser, array('popup' => true)), array('class' => 'notunderlined')); 00102 $oldheading .= $this->output->container_end(); 00103 00104 // version number container 00105 $oldheading .= $this->output->container_start('wiki_diffversion'); 00106 $oldheading .= get_string('version') . ' ' . $old->version . $oldversionview; 00107 $oldheading .= $this->output->container_end(); 00108 // userdate container 00109 $oldheading .= $this->output->container_start('wiki_difftime'); 00110 $oldheading .= userdate($old->timecreated, $strdatetime); 00111 $oldheading .= $this->output->container_end(); 00112 00113 $newuser = $new->user; 00114 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $new->id)); 00115 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $pageid, 'versionid' => $new->id)); 00116 $userlink = new moodle_url('/user/view.php', array('id' => $newuser->id)); 00117 00118 $newversionview = ' '; 00119 $newversionview .= html_writer::link($versionlink->out(false), get_string('view', 'wiki'), array('class' => 'wiki_diffview')); 00120 // new user info 00121 $newheading = $this->output->container_start('wiki_diffuserright'); 00122 $newheading .= $this->output->user_picture($newuser, array('popup' => true)); 00123 00124 $newheading .= html_writer::link($userlink->out(false), fullname($newuser), array('class' => 'notunderlined')); 00125 $newheading .= $this->output->container_end(); 00126 00127 // version 00128 $newheading .= $this->output->container_start('wiki_diffversion'); 00129 $newheading .= get_string('version') . ' ' . $new->version . $newversionview; 00130 $newheading .= $this->output->container_end(); 00131 // userdate 00132 $newheading .= $this->output->container_start('wiki_difftime'); 00133 $newheading .= userdate($new->timecreated, $strdatetime); 00134 $newheading .= $this->output->container_end(); 00135 00136 $oldheading = html_writer::tag('div', $oldheading, array('class'=>'wiki-diff-heading header clearfix')); 00137 $newheading = html_writer::tag('div', $newheading, array('class'=>'wiki-diff-heading header clearfix')); 00138 00139 $output = ''; 00140 $output .= html_writer::start_tag('div', array('class'=>'wiki-diff-container clearfix')); 00141 $output .= html_writer::tag('div', $oldheading.$diff1, array('class'=>'wiki-diff-leftside')); 00142 $output .= html_writer::tag('div', $newheading.$diff2, array('class'=>'wiki-diff-rightside')); 00143 $output .= html_writer::end_tag('div'); 00144 00145 if (!empty($total)) { 00146 $output .= '<div class="wiki_diff_paging">'; 00147 $output .= $this->output->container($this->diff_paging_bar(1, $new->version - 1, $old->version, $CFG->wwwroot . '/mod/wiki/diff.php?pageid=' . $pageid . '&comparewith=' . $new->version . '&', 'compare', false, true), 'wiki_diff_oldpaging'); 00148 $output .= $this->output->container($this->diff_paging_bar($old->version + 1, $total, $new->version, $CFG->wwwroot . '/mod/wiki/diff.php?pageid=' . $pageid . '&compare=' . $old->version . '&', 'comparewith', false, true), 'wiki_diff_newpaging'); 00149 $output .= '</div>'; 00150 } 00151 00152 return $output; 00153 } 00154 00168 public function diff_paging_bar($minpage, $maxpage, $page, $baseurl, $pagevar = 'page', $nocurr = false) { 00169 $totalcount = $maxpage - $minpage; 00170 $maxdisplay = 2; 00171 $output = ''; 00172 00173 if ($totalcount > 0) { 00174 $output .= '<div class="paging">'; 00175 $output .= get_string('version', 'wiki') . ':'; 00176 if ($page - $minpage > 0) { 00177 $pagenum = $page - 1; 00178 if (!is_a($baseurl, 'moodle_url')) { 00179 $output .= ' (<a class="previous" href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string('previous') . '</a>) '; 00180 } else { 00181 $output .= ' (<a class="previous" href="' . $baseurl->out(false, array($pagevar => $pagenum)) . '">' . get_string('previous') . '</a>) '; 00182 } 00183 } 00184 00185 if ($page - $minpage > 4) { 00186 $startpage = $page - 3; 00187 if (!is_a($baseurl, 'moodle_url')) { 00188 $output .= ' <a href="' . $baseurl . $pagevar . '=' . $minpage . '">' . $minpage . '</a> ...'; 00189 } else { 00190 $output .= ' <a href="' . $baseurl->out(false, array($pagevar => $minpage)) . '">' . $minpage . '</a> ...'; 00191 } 00192 } else { 00193 $startpage = $minpage; 00194 } 00195 $currpage = $startpage; 00196 $displaycount = 0; 00197 while ($displaycount < $maxdisplay and $currpage <= $maxpage) { 00198 if ($page == $currpage && empty($nocurr)) { 00199 $output .= ' ' . $currpage; 00200 } else { 00201 if (!is_a($baseurl, 'moodle_url')) { 00202 $output .= ' <a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $currpage . '</a>'; 00203 } else { 00204 $output .= ' <a href="' . $baseurl->out(false, array($pagevar => $currpage)) . '">' . $currpage . '</a>'; 00205 } 00206 00207 } 00208 $displaycount++; 00209 $currpage++; 00210 } 00211 if ($currpage < $maxpage) { 00212 if (!is_a($baseurl, 'moodle_url')) { 00213 $output .= ' ...<a href="' . $baseurl . $pagevar . '=' . $maxpage . '">' . $maxpage . '</a> '; 00214 } else { 00215 $output .= ' ...<a href="' . $baseurl->out(false, array($pagevar => $maxpage)) . '">' . $maxpage . '</a> '; 00216 } 00217 } else if ($currpage == $maxpage) { 00218 if (!is_a($baseurl, 'moodle_url')) { 00219 $output .= ' <a href="' . $baseurl . $pagevar . '=' . $currpage . '">' . $currpage . '</a>'; 00220 } else { 00221 $output .= ' <a href="' . $baseurl->out(false, array($pagevar => $currpage)) . '">' . $currpage . '</a>'; 00222 } 00223 } 00224 $pagenum = $page + 1; 00225 if ($page != $maxpage) { 00226 if (!is_a($baseurl, 'moodle_url')) { 00227 $output .= ' (<a class="next" href="' . $baseurl . $pagevar . '=' . $pagenum . '">' . get_string('next') . '</a>)'; 00228 } else { 00229 $output .= ' (<a class="next" href="' . $baseurl->out(false, array($pagevar => $pagenum)) . '">' . get_string('next') . '</a>)'; 00230 } 00231 } 00232 $output .= '</div>'; 00233 } 00234 00235 return $output; 00236 } 00237 public function wiki_info() { 00238 global $PAGE; 00239 return $this->output->box(format_module_intro('wiki', $this->page->activityrecord, $PAGE->cm->id), 'generalbox', 'intro'); 00240 } 00241 00242 public function tabs($page, $tabitems, $options) { 00243 global $CFG; 00244 $tabs = array(); 00245 $baseurl = $CFG->wwwroot . '/mod/wiki/'; 00246 $context = get_context_instance(CONTEXT_MODULE, $this->page->cm->id); 00247 00248 $pageid = null; 00249 if (!empty($page)) { 00250 $pageid = $page->id; 00251 } 00252 00253 $selected = $options['activetab']; 00254 00255 // make specific tab linked even it is active 00256 if (!empty($options['linkedwhenactive'])) { 00257 $linked = $options['linkedwhenactive']; 00258 } else { 00259 $linked = ''; 00260 } 00261 00262 if (!empty($options['inactivetabs'])) { 00263 $inactive = $options['inactivetabs']; 00264 } else { 00265 $inactive = array(); 00266 } 00267 00268 foreach ($tabitems as $tab) { 00269 if ($tab == 'edit' && !has_capability('mod/wiki:editpage', $context)) { 00270 continue; 00271 } 00272 if ($tab == 'comments' && !has_capability('mod/wiki:viewcomment', $context)) { 00273 continue; 00274 } 00275 if ($tab == 'files' && !has_capability('mod/wiki:viewpage', $context)) { 00276 continue; 00277 } 00278 if (($tab == 'view' || $tab == 'map' || $tab == 'history') && !has_capability('mod/wiki:viewpage', $context)) { 00279 continue; 00280 } 00281 if ($tab == 'admin' && !has_capability('mod/wiki:managewiki', $context)) { 00282 continue; 00283 } 00284 $link = $baseurl . $tab . '.php?pageid=' . $pageid; 00285 if ($linked == $tab) { 00286 $tabs[] = new tabobject($tab, $link, get_string($tab, 'wiki'), '', true); 00287 } else { 00288 $tabs[] = new tabobject($tab, $link, get_string($tab, 'wiki')); 00289 } 00290 } 00291 00292 return print_tabs(array($tabs), $selected, $inactive, null, true); 00293 } 00294 00295 public function prettyview_link($page) { 00296 $html = ''; 00297 $link = new moodle_url('/mod/wiki/prettyview.php', array('pageid' => $page->id)); 00298 $html .= $this->output->container_start('wiki_right'); 00299 $html .= $this->output->action_link($link, get_string('prettyprint', 'wiki'), new popup_action('click', $link)); 00300 $html .= $this->output->container_end(); 00301 return $html; 00302 } 00303 00304 public function wiki_print_subwiki_selector($wiki, $subwiki, $page, $pagetype = 'view') { 00305 global $CFG, $USER; 00306 require_once($CFG->dirroot . '/user/lib.php'); 00307 switch ($pagetype) { 00308 case 'files': 00309 $baseurl = new moodle_url('/mod/wiki/files.php'); 00310 break; 00311 case 'view': 00312 default: 00313 $baseurl = new moodle_url('/mod/wiki/view.php'); 00314 break; 00315 } 00316 00317 $cm = get_coursemodule_from_instance('wiki', $wiki->id); 00318 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00319 // @TODO: A plenty of duplicated code below this lines. 00320 // Create private functions. 00321 switch (groups_get_activity_groupmode($cm)) { 00322 case NOGROUPS: 00323 if ($wiki->wikimode == 'collaborative') { 00324 // No need to print anything 00325 return; 00326 } else if ($wiki->wikimode == 'individual') { 00327 // We have private wikis here 00328 00329 $view = has_capability('mod/wiki:viewpage', $context); 00330 $manage = has_capability('mod/wiki:managewiki', $context); 00331 00332 // Only people with these capabilities can view all wikis 00333 if ($view && $manage) { 00334 // @TODO: Print here a combo that contains all users. 00335 $users = get_enrolled_users($context); 00336 $options = array(); 00337 foreach ($users as $user) { 00338 $options[$user->id] = fullname($user); 00339 } 00340 00341 echo $this->output->container_start('wiki_right'); 00342 $params = array('wid' => $wiki->id, 'title' => $page->title); 00343 if ($pagetype == 'files') { 00344 $params['pageid'] = $page->id; 00345 } 00346 $baseurl->params($params); 00347 $name = 'uid'; 00348 $selected = $subwiki->userid; 00349 echo $this->output->single_select($baseurl, $name, $options, $selected); 00350 echo $this->output->container_end(); 00351 } 00352 return; 00353 } else { 00354 // error 00355 return; 00356 } 00357 case SEPARATEGROUPS: 00358 if ($wiki->wikimode == 'collaborative') { 00359 // We need to print a select to choose a course group 00360 00361 $params = array('wid'=>$wiki->id, 'title'=>$page->title); 00362 if ($pagetype == 'files') { 00363 $params['pageid'] = $page->id; 00364 } 00365 $baseurl->params($params); 00366 00367 echo $this->output->container_start('wiki_right'); 00368 groups_print_activity_menu($cm, $baseurl->out()); 00369 echo $this->output->container_end(); 00370 return; 00371 } else if ($wiki->wikimode == 'individual') { 00372 // @TODO: Print here a combo that contains all users of that subwiki. 00373 $view = has_capability('mod/wiki:viewpage', $context); 00374 $manage = has_capability('mod/wiki:managewiki', $context); 00375 00376 // Only people with these capabilities can view all wikis 00377 if ($view && $manage) { 00378 $users = get_enrolled_users($context); 00379 $options = array(); 00380 foreach ($users as $user) { 00381 $groups = groups_get_all_groups($cm->course, $user->id); 00382 if (!empty($groups)) { 00383 foreach ($groups as $group) { 00384 $options[$group->id][$group->name][$group->id . '-' . $user->id] = fullname($user); 00385 } 00386 } else { 00387 $name = get_string('notingroup', 'wiki'); 00388 $options[0][$name]['0' . '-' . $user->id] = fullname($user); 00389 } 00390 } 00391 } else { 00392 $group = groups_get_group($subwiki->groupid); 00393 $users = groups_get_members($subwiki->groupid); 00394 foreach ($users as $user) { 00395 $options[$group->id][$group->name][$group->id . '-' . $user->id] = fullname($user); 00396 } 00397 } 00398 echo $this->output->container_start('wiki_right'); 00399 $params = array('wid' => $wiki->id, 'title' => $page->title); 00400 if ($pagetype == 'files') { 00401 $params['pageid'] = $page->id; 00402 } 00403 $baseurl->params($params); 00404 $name = 'groupanduser'; 00405 $selected = $subwiki->groupid . '-' . $subwiki->userid; 00406 echo $this->output->single_select($baseurl, $name, $options, $selected); 00407 echo $this->output->container_end(); 00408 00409 return; 00410 00411 } else { 00412 // error 00413 return; 00414 } 00415 CASE VISIBLEGROUPS: 00416 if ($wiki->wikimode == 'collaborative') { 00417 // We need to print a select to choose a course group 00418 $params = array('wid'=>$wiki->id, 'title'=>urlencode($page->title)); 00419 if ($pagetype == 'files') { 00420 $params['pageid'] = $page->id; 00421 } 00422 $baseurl->params($params); 00423 00424 echo $this->output->container_start('wiki_right'); 00425 groups_print_activity_menu($cm, $baseurl->out()); 00426 echo $this->output->container_end(); 00427 return; 00428 00429 } else if ($wiki->wikimode == 'individual') { 00430 $users = get_enrolled_users($context); 00431 $options = array(); 00432 foreach ($users as $user) { 00433 $groups = groups_get_all_groups($cm->course, $user->id); 00434 if (!empty($groups)) { 00435 foreach ($groups as $group) { 00436 $options[$group->id][$group->name][$group->id . '-' . $user->id] = fullname($user); 00437 } 00438 } else { 00439 $name = get_string('notingroup', 'wiki'); 00440 $options[0][$name]['0' . '-' . $user->id] = fullname($user); 00441 } 00442 } 00443 00444 echo $this->output->container_start('wiki_right'); 00445 $params = array('wid' => $wiki->id, 'title' => $page->title); 00446 if ($pagetype == 'files') { 00447 $params['pageid'] = $page->id; 00448 } 00449 $baseurl->params($params); 00450 $name = 'groupanduser'; 00451 $selected = $subwiki->groupid . '-' . $subwiki->userid; 00452 echo $this->output->single_select($baseurl, $name, $options, $selected); 00453 echo $this->output->container_end(); 00454 00455 return; 00456 00457 } else { 00458 // error 00459 return; 00460 } 00461 default: 00462 // error 00463 return; 00464 00465 } 00466 00467 } 00468 00469 function menu_map($pageid, $currentselect) { 00470 $options = array('contributions', 'links', 'orphaned', 'pageindex', 'pagelist', 'updatedpages'); 00471 $items = array(); 00472 foreach ($options as $opt) { 00473 $items[] = get_string($opt, 'wiki'); 00474 } 00475 $selectoptions = array(); 00476 foreach ($items as $key => $item) { 00477 $selectoptions[$key + 1] = $item; 00478 } 00479 $select = new single_select(new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid)), 'option', $selectoptions, $currentselect); 00480 $select->label = get_string('mapmenu', 'wiki') . ': '; 00481 return $this->output->container($this->output->render($select), 'midpad'); 00482 } 00483 public function wiki_files_tree($context, $subwiki) { 00484 return $this->render(new wiki_files_tree($context, $subwiki)); 00485 } 00486 public function render_wiki_files_tree(wiki_files_tree $tree) { 00487 if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) { 00488 $html = $this->output->box(get_string('nofilesavailable', 'repository')); 00489 } else { 00490 $htmlid = 'wiki_files_tree_'.uniqid(); 00491 $module = array('name'=>'mod_wiki', 'fullpath'=>'/mod/wiki/module.js'); 00492 $this->page->requires->js_init_call('M.mod_wiki.init_tree', array(false, $htmlid), false, $module); 00493 $html = '<div id="'.$htmlid.'">'; 00494 $html .= $this->htmllize_tree($tree, $tree->dir); 00495 $html .= '</div>'; 00496 } 00497 return $html; 00498 } 00499 00500 function menu_admin($pageid, $currentselect) { 00501 $options = array('removepages', 'deleteversions'); 00502 $items = array(); 00503 foreach ($options as $opt) { 00504 $items[] = get_string($opt, 'wiki'); 00505 } 00506 $selectoptions = array(); 00507 foreach ($items as $key => $item) { 00508 $selectoptions[$key + 1] = $item; 00509 } 00510 $select = new single_select(new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid)), 'option', $selectoptions, $currentselect); 00511 $select->label = get_string('adminmenu', 'wiki') . ': '; 00512 return $this->output->container($this->output->render($select), 'midpad'); 00513 } 00514 00518 protected function htmllize_tree($tree, $dir) { 00519 global $CFG; 00520 $yuiconfig = array(); 00521 $yuiconfig['type'] = 'html'; 00522 00523 if (empty($dir['subdirs']) and empty($dir['files'])) { 00524 return ''; 00525 } 00526 $result = '<ul>'; 00527 foreach ($dir['subdirs'] as $subdir) { 00528 $image = $this->output->pix_icon("f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon')); 00529 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>'; 00530 } 00531 foreach ($dir['files'] as $file) { 00532 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/mod_wiki/attachments/' . $tree->subwiki->id . '/'. $file->get_filepath() . $file->get_filename(), true); 00533 $filename = $file->get_filename(); 00534 $icon = mimeinfo("icon", $filename); 00535 $image = $this->output->pix_icon("f/$icon", $filename, 'moodle', array('class'=>'icon')); 00536 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>'; 00537 } 00538 $result .= '</ul>'; 00539 00540 return $result; 00541 } 00542 } 00543 00544 class wiki_files_tree implements renderable { 00545 public $context; 00546 public $dir; 00547 public $subwiki; 00548 public function __construct($context, $subwiki) { 00549 $fs = get_file_storage(); 00550 $this->context = $context; 00551 $this->subwiki = $subwiki; 00552 $this->dir = $fs->get_area_tree($context->id, 'mod_wiki', 'attachments', $subwiki->id); 00553 } 00554 }