[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/wiki/ -> pagelib.php (source)

   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  
  18  /**
  19   * This file contains several classes uses to render the diferent pages
  20   * of the wiki module
  21   *
  22   * @package mod_wiki
  23   * @copyright 2009 Marc Alier, Jordi Piguillem [email protected]
  24   * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  25   *
  26   * @author Jordi Piguillem
  27   * @author Marc Alier
  28   * @author David Jimenez
  29   * @author Josep Arus
  30   * @author Daniel Serrano
  31   * @author Kenneth Riba
  32   *
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  
  36  require_once($CFG->dirroot . '/mod/wiki/edit_form.php');
  37  require_once($CFG->dirroot . '/tag/lib.php');
  38  
  39  /**
  40   * Class page_wiki contains the common code between all pages
  41   *
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  abstract class page_wiki {
  45  
  46      /**
  47       * @var object Current subwiki
  48       */
  49      protected $subwiki;
  50  
  51      /**
  52       * @var int Current page
  53       */
  54      protected $page;
  55  
  56      /**
  57       * @var string Current page title
  58       */
  59      protected $title;
  60  
  61      /**
  62       * @var int Current group ID
  63       */
  64      protected $gid;
  65  
  66      /**
  67       * @var object module context object
  68       */
  69      protected $modcontext;
  70  
  71      /**
  72       * @var int Current user ID
  73       */
  74      protected $uid;
  75      /**
  76       * @var array The tabs set used in wiki module
  77       */
  78      protected $tabs = array('view' => 'view', 'edit' => 'edit', 'comments' => 'comments',
  79                              'history' => 'history', 'map' => 'map', 'files' => 'files',
  80                              'admin' => 'admin');
  81      /**
  82       * @var array tabs options
  83       */
  84      protected $tabs_options = array();
  85      /**
  86       * @var object wiki renderer
  87       */
  88      protected $wikioutput;
  89      /**
  90       * @var stdClass course module.
  91       */
  92      protected $cm;
  93  
  94      /**
  95       * page_wiki constructor
  96       *
  97       * @param $wiki. Current wiki
  98       * @param $subwiki. Current subwiki.
  99       * @param $cm. Current course_module.
 100       */
 101      function __construct($wiki, $subwiki, $cm) {
 102          global $PAGE, $CFG;
 103          $this->subwiki = $subwiki;
 104          $this->cm = $cm;
 105          $this->modcontext = context_module::instance($this->cm->id);
 106  
 107          // initialise wiki renderer
 108          $this->wikioutput = $PAGE->get_renderer('mod_wiki');
 109          $PAGE->set_cacheable(true);
 110          $PAGE->set_cm($cm);
 111          $PAGE->set_activity_record($wiki);
 112          // the search box
 113          if (!empty($subwiki->id)) {
 114              $search = optional_param('searchstring', null, PARAM_TEXT);
 115              $PAGE->set_button(wiki_search_form($cm, $search, $subwiki));
 116          }
 117      }
 118  
 119      /**
 120       * This method prints the top of the page.
 121       */
 122      function print_header() {
 123          global $OUTPUT, $PAGE, $CFG, $USER, $SESSION;
 124  
 125          $PAGE->set_heading($PAGE->course->fullname);
 126  
 127          $this->set_url();
 128  
 129          if (isset($SESSION->wikipreviousurl) && is_array($SESSION->wikipreviousurl)) {
 130              $this->process_session_url();
 131          }
 132          $this->set_session_url();
 133  
 134          $this->create_navbar();
 135          $this->setup_tabs();
 136  
 137          echo $OUTPUT->header();
 138          $wiki = $PAGE->activityrecord;
 139          echo $OUTPUT->heading($wiki->name);
 140  
 141          echo $this->wikioutput->wiki_info();
 142  
 143          // tabs are associated with pageid, so if page is empty, tabs should be disabled
 144          if (!empty($this->page) && !empty($this->tabs)) {
 145              echo $this->wikioutput->tabs($this->page, $this->tabs, $this->tabs_options);
 146          }
 147      }
 148  
 149      /**
 150       * Protected method to print current page title.
 151       */
 152      protected function print_pagetitle() {
 153          global $OUTPUT;
 154          $html = '';
 155  
 156          $html .= $OUTPUT->container_start('wiki_headingtitle');
 157          $html .= $OUTPUT->heading(format_string($this->title), 3);
 158          $html .= $OUTPUT->container_end();
 159          echo $html;
 160      }
 161  
 162      /**
 163       * Setup page tabs, if options is empty, will set up active tab automatically
 164       * @param array $options, tabs options
 165       */
 166      protected function setup_tabs($options = array()) {
 167          global $CFG, $PAGE;
 168          $groupmode = groups_get_activity_groupmode($this->cm);
 169  
 170          if (empty($CFG->usecomments) || !has_capability('mod/wiki:viewcomment', $PAGE->context)){
 171              unset($this->tabs['comments']);
 172          }
 173  
 174          if (!has_capability('mod/wiki:editpage', $PAGE->context)){
 175              unset($this->tabs['edit']);
 176          }
 177  
 178          if ($groupmode and $groupmode == VISIBLEGROUPS) {
 179              $currentgroup = groups_get_activity_group($this->cm);
 180              $manage = has_capability('mod/wiki:managewiki', $this->modcontext);
 181              $edit = has_capability('mod/wiki:editpage', $PAGE->context);
 182              if (!$manage and !($edit and groups_is_member($currentgroup))) {
 183                  unset($this->tabs['edit']);
 184              }
 185          }
 186  
 187          if (empty($options)) {
 188              $this->tabs_options = array('activetab' => substr(get_class($this), 10));
 189          } else {
 190              $this->tabs_options = $options;
 191          }
 192  
 193      }
 194  
 195      /**
 196       * This method must be overwritten to print the page content.
 197       */
 198      function print_content() {
 199          throw new coding_exception('Page wiki class does not implement method print_content()');
 200      }
 201  
 202      /**
 203       * Method to set the current page
 204       *
 205       * @param object $page Current page
 206       */
 207      function set_page($page) {
 208          global $PAGE;
 209  
 210          $this->page = $page;
 211          $this->title = $page->title;
 212          // set_title calls format_string itself so no probs there
 213          $PAGE->set_title($this->title);
 214      }
 215  
 216      /**
 217       * Method to set the current page title.
 218       * This method must be called when the current page is not created yet.
 219       * @param string $title Current page title.
 220       */
 221      function set_title($title) {
 222          global $PAGE;
 223  
 224          $this->page = null;
 225          $this->title = $title;
 226          // set_title calls format_string itself so no probs there
 227          $PAGE->set_title($this->title);
 228      }
 229  
 230      /**
 231       * Method to set current group id
 232       * @param int $gid Current group id
 233       */
 234      function set_gid($gid) {
 235          $this->gid = $gid;
 236      }
 237  
 238      /**
 239       * Method to set current user id
 240       * @param int $uid Current user id
 241       */
 242      function set_uid($uid) {
 243          $this->uid = $uid;
 244      }
 245  
 246      /**
 247       * Method to set the URL of the page.
 248       * This method must be overwritten by every type of page.
 249       */
 250      protected function set_url() {
 251          throw new coding_exception('Page wiki class does not implement method set_url()');
 252      }
 253  
 254      /**
 255       * Protected method to create the common items of the navbar in every page type.
 256       */
 257      protected function create_navbar() {
 258          global $PAGE, $CFG;
 259  
 260          $PAGE->navbar->add(format_string($this->title), $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
 261      }
 262  
 263      /**
 264       * This method print the footer of the page.
 265       */
 266      function print_footer() {
 267          global $OUTPUT;
 268          echo $OUTPUT->footer();
 269      }
 270  
 271      protected function process_session_url() {
 272          global $USER, $SESSION;
 273  
 274          //delete locks if edit
 275          $url = $SESSION->wikipreviousurl;
 276          switch ($url['page']) {
 277          case 'edit':
 278              wiki_delete_locks($url['params']['pageid'], $USER->id, $url['params']['section'], false);
 279              break;
 280          }
 281      }
 282  
 283      protected function set_session_url() {
 284          global $SESSION;
 285          unset($SESSION->wikipreviousurl);
 286      }
 287  
 288  }
 289  
 290  /**
 291   * View a wiki page
 292   *
 293   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 294   */
 295  class page_wiki_view extends page_wiki {
 296  
 297      function print_header() {
 298          global $PAGE;
 299  
 300          parent::print_header();
 301  
 302          $this->wikioutput->wiki_print_subwiki_selector($PAGE->activityrecord, $this->subwiki, $this->page, 'view');
 303  
 304          if (!empty($this->page)) {
 305              echo $this->wikioutput->prettyview_link($this->page);
 306          }
 307  
 308          //echo $this->wikioutput->page_index();
 309  
 310          $this->print_pagetitle();
 311      }
 312  
 313      function print_content() {
 314          global $PAGE, $CFG;
 315  
 316          if (wiki_user_can_view($this->subwiki)) {
 317  
 318              if (!empty($this->page)) {
 319                  wiki_print_page_content($this->page, $this->modcontext, $this->subwiki->id);
 320                  $wiki = $PAGE->activityrecord;
 321              } else {
 322                  print_string('nocontent', 'wiki');
 323                  // TODO: fix this part
 324                  $swid = 0;
 325                  if (!empty($this->subwiki)) {
 326                      $swid = $this->subwiki->id;
 327                  }
 328              }
 329          } else {
 330              echo get_string('cannotviewpage', 'wiki');
 331          }
 332      }
 333  
 334      function set_url() {
 335          global $PAGE, $CFG;
 336          $params = array();
 337  
 338          if (isset($this->cm->id)) {
 339              $params['id'] = $this->cm->id;
 340          } else if (!empty($this->page) and $this->page != null) {
 341              $params['pageid'] = $this->page->id;
 342          } else if (!empty($this->gid)) {
 343              $params['wid'] = $this->cm->instance;
 344              $params['group'] = $this->gid;
 345          } else if (!empty($this->title)) {
 346              $params['swid'] = $this->subwiki->id;
 347              $params['title'] = $this->title;
 348          } else {
 349              print_error(get_string('invalidparameters', 'wiki'));
 350          }
 351          $PAGE->set_url(new moodle_url($CFG->wwwroot . '/mod/wiki/view.php', $params));
 352      }
 353  
 354      protected function create_navbar() {
 355          global $PAGE;
 356  
 357          $PAGE->navbar->add(format_string($this->title));
 358          $PAGE->navbar->add(get_string('view', 'wiki'));
 359      }
 360  }
 361  
 362  /**
 363   * Wiki page editing page
 364   *
 365   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 366   */
 367  class page_wiki_edit extends page_wiki {
 368  
 369      public static $attachmentoptions;
 370  
 371      protected $sectioncontent;
 372      /** @var string the section name needed to be edited */
 373      protected $section;
 374      protected $overridelock = false;
 375      protected $versionnumber = -1;
 376      protected $upload = false;
 377      protected $attachments = 0;
 378      protected $deleteuploads = array();
 379      protected $format;
 380  
 381      function __construct($wiki, $subwiki, $cm) {
 382          global $CFG, $PAGE;
 383          parent::__construct($wiki, $subwiki, $cm);
 384          self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => '*');
 385          $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true);
 386      }
 387  
 388      protected function print_pagetitle() {
 389          global $OUTPUT;
 390  
 391          $title = $this->title;
 392          if (isset($this->section)) {
 393              $title .= ' : ' . $this->section;
 394          }
 395          echo $OUTPUT->container_start('wiki_clear wiki_headingtitle');
 396          echo $OUTPUT->heading(format_string($title), 3);
 397          echo $OUTPUT->container_end();
 398      }
 399  
 400      function print_header() {
 401          global $OUTPUT, $PAGE;
 402          $PAGE->requires->data_for_js('wiki', array('renew_lock_timeout' => LOCK_TIMEOUT - 5, 'pageid' => $this->page->id, 'section' => $this->section));
 403  
 404          parent::print_header();
 405  
 406          $this->print_pagetitle();
 407  
 408          print '<noscript>' . $OUTPUT->box(get_string('javascriptdisabledlocks', 'wiki'), 'errorbox') . '</noscript>';
 409      }
 410  
 411      function print_content() {
 412          global $PAGE;
 413  
 414          if (wiki_user_can_edit($this->subwiki)) {
 415              $this->print_edit();
 416          } else {
 417              echo get_string('cannoteditpage', 'wiki');
 418          }
 419      }
 420  
 421      protected function set_url() {
 422          global $PAGE, $CFG;
 423  
 424          $params = array('pageid' => $this->page->id);
 425  
 426          if (isset($this->section)) {
 427              $params['section'] = $this->section;
 428          }
 429  
 430          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
 431      }
 432  
 433      protected function set_session_url() {
 434          global $SESSION;
 435  
 436          $SESSION->wikipreviousurl = array('page' => 'edit', 'params' => array('pageid' => $this->page->id, 'section' => $this->section));
 437      }
 438  
 439      protected function process_session_url() {
 440      }
 441  
 442      function set_section($sectioncontent, $section) {
 443          $this->sectioncontent = $sectioncontent;
 444          $this->section = $section;
 445      }
 446  
 447      public function set_versionnumber($versionnumber) {
 448          $this->versionnumber = $versionnumber;
 449      }
 450  
 451      public function set_overridelock($override) {
 452          $this->overridelock = $override;
 453      }
 454  
 455      function set_format($format) {
 456          $this->format = $format;
 457      }
 458  
 459      public function set_upload($upload) {
 460          $this->upload = $upload;
 461      }
 462  
 463      public function set_attachments($attachments) {
 464          $this->attachments = $attachments;
 465      }
 466  
 467      public function set_deleteuploads($deleteuploads) {
 468          $this->deleteuploads = $deleteuploads;
 469      }
 470  
 471      protected function create_navbar() {
 472          global $PAGE, $CFG;
 473  
 474          parent::create_navbar();
 475  
 476          $PAGE->navbar->add(get_string('edit', 'wiki'));
 477      }
 478  
 479      protected function check_locks() {
 480          global $OUTPUT, $USER, $CFG;
 481  
 482          if (!wiki_set_lock($this->page->id, $USER->id, $this->section, true)) {
 483              print $OUTPUT->box(get_string('pageislocked', 'wiki'), 'generalbox boxwidthnormal boxaligncenter');
 484  
 485              if ($this->overridelock) {
 486                  $params = 'pageid=' . $this->page->id;
 487  
 488                  if ($this->section) {
 489                      $params .= '&section=' . urlencode($this->section);
 490                  }
 491  
 492                  $form = '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/overridelocks.php?' . $params . '">';
 493                  $form .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
 494                  $form .= '<input type="submit" value="' . get_string('overridelocks', 'wiki') . '" />';
 495                  $form .= '</form>';
 496  
 497                  print $OUTPUT->box($form, 'generalbox boxwidthnormal boxaligncenter');
 498              }
 499              return false;
 500          }
 501          return true;
 502      }
 503  
 504      protected function print_edit($content = null) {
 505          global $CFG, $OUTPUT, $USER, $PAGE;
 506  
 507          if (!$this->check_locks()) {
 508              return;
 509          }
 510  
 511          //delete old locks (> 1 hour)
 512          wiki_delete_old_locks();
 513  
 514          $version = wiki_get_current_version($this->page->id);
 515          $format = $version->contentformat;
 516  
 517          if ($content == null) {
 518              if (empty($this->section)) {
 519                  $content = $version->content;
 520              } else {
 521                  $content = $this->sectioncontent;
 522              }
 523          }
 524  
 525          $versionnumber = $version->version;
 526          if ($this->versionnumber >= 0) {
 527              if ($version->version != $this->versionnumber) {
 528                  print $OUTPUT->box(get_string('wrongversionlock', 'wiki'), 'errorbox');
 529                  $versionnumber = $this->versionnumber;
 530              }
 531          }
 532  
 533          $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
 534          if (!empty($this->section)) {
 535              $url .= "&section=" . urlencode($this->section);
 536          }
 537  
 538          $params = array(
 539              'attachmentoptions' => page_wiki_edit::$attachmentoptions,
 540              'format' => $version->contentformat,
 541              'version' => $versionnumber,
 542              'pagetitle' => $this->page->title,
 543              'contextid' => $this->modcontext->id
 544          );
 545  
 546          $data = new StdClass();
 547          $data->newcontent = $content;
 548          $data->version = $versionnumber;
 549          $data->format = $format;
 550  
 551          switch ($format) {
 552          case 'html':
 553              $data->newcontentformat = FORMAT_HTML;
 554              // Append editor context to editor options, giving preference to existing context.
 555              page_wiki_edit::$attachmentoptions = array_merge(array('context' => $this->modcontext), page_wiki_edit::$attachmentoptions);
 556              $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
 557              break;
 558          default:
 559              break;
 560          }
 561  
 562          if ($version->contentformat != 'html') {
 563              $params['fileitemid'] = $this->subwiki->id;
 564              $params['component']  = 'mod_wiki';
 565              $params['filearea']   = 'attachments';
 566          }
 567  
 568          $form = new mod_wiki_edit_form($url, $params);
 569  
 570          if ($formdata = $form->get_data()) {
 571              if (!empty($CFG->usetags)) {
 572                  $data->tags = $formdata->tags;
 573              }
 574          } else {
 575              if (!empty($CFG->usetags)) {
 576                  $data->tags = tag_get_tags_array('wiki_pages', $this->page->id);
 577              }
 578          }
 579  
 580          $form->set_data($data);
 581          $form->display();
 582      }
 583  
 584  }
 585  
 586  /**
 587   * Class that models the behavior of wiki's view comments page
 588   *
 589   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 590   */
 591  class page_wiki_comments extends page_wiki {
 592  
 593      function print_header() {
 594  
 595          parent::print_header();
 596  
 597          $this->print_pagetitle();
 598  
 599      }
 600  
 601      function print_content() {
 602          global $CFG, $OUTPUT, $USER, $PAGE;
 603          require_once($CFG->dirroot . '/mod/wiki/locallib.php');
 604  
 605          $page = $this->page;
 606          $subwiki = $this->subwiki;
 607          $wiki = $PAGE->activityrecord;
 608          list($context, $course, $cm) = get_context_info_array($this->modcontext->id);
 609  
 610          require_capability('mod/wiki:viewcomment', $this->modcontext, NULL, true, 'noviewcommentpermission', 'wiki');
 611  
 612          $comments = wiki_get_comments($this->modcontext->id, $page->id);
 613  
 614          if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
 615              echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&amp;pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>';
 616          }
 617  
 618          $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id);
 619          $version = wiki_get_current_version($this->page->id);
 620          $format = $version->contentformat;
 621  
 622          if (empty($comments)) {
 623              echo html_writer::tag('p', get_string('nocomments', 'wiki'), array('class' => 'bold'));
 624          }
 625  
 626          foreach ($comments as $comment) {
 627  
 628              $user = wiki_get_user_info($comment->userid);
 629  
 630              $fullname = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
 631              $by = new stdclass();
 632              $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . $course->id . '">' . $fullname . '</a>';
 633              $by->date = userdate($comment->timecreated);
 634  
 635              $t = new html_table();
 636              $t->id = 'wiki-comments';
 637              $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true)));
 638              $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by));
 639              $cell3 = new html_table_cell();
 640              $cell3->atributtes ['width'] = "80%";
 641              $cell4 = new html_table_cell();
 642              $cell5 = new html_table_cell();
 643  
 644              $row1 = new html_table_row();
 645              $row1->cells[] = $cell1;
 646              $row1->cells[] = $cell2;
 647              $row2 = new html_table_row();
 648              $row2->cells[] = $cell3;
 649  
 650              if ($format != 'html') {
 651                  if ($format == 'creole') {
 652                      $parsedcontent = wiki_parse_content('creole', $comment->content, $options);
 653                  } else if ($format == 'nwiki') {
 654                      $parsedcontent = wiki_parse_content('nwiki', $comment->content, $options);
 655                  }
 656  
 657                  $cell4->text = format_text(html_entity_decode($parsedcontent['parsed_text'], ENT_QUOTES, 'UTF-8'), FORMAT_HTML);
 658              } else {
 659                  $cell4->text = format_text($comment->content, FORMAT_HTML);
 660              }
 661  
 662              $row2->cells[] = $cell4;
 663  
 664              $t->data = array($row1, $row2);
 665  
 666              $canedit = $candelete = false;
 667              if ((has_capability('mod/wiki:editcomment', $this->modcontext)) and ($USER->id == $user->id)) {
 668                  $candelete = $canedit = true;
 669              }
 670              if ((has_capability('mod/wiki:managecomment', $this->modcontext))) {
 671                  $candelete = true;
 672              }
 673  
 674              $editicon = $deleteicon = '';
 675              if ($canedit) {
 676                  $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
 677                  $editicon = $OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit'), '', array('class' => 'iconsmall')));
 678              }
 679              if ($candelete) {
 680                  $urldelete = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
 681                  $deleteicon = $OUTPUT->action_icon($urldelete,
 682                                                    new pix_icon('t/delete',
 683                                                                 get_string('delete'),
 684                                                                 '',
 685                                                                 array('class' => 'iconsmall')));
 686              }
 687  
 688              if ($candelete || $canedit) {
 689                  $cell6 = new html_table_cell($editicon.$deleteicon);
 690                  $row3 = new html_table_row();
 691                  $row3->cells[] = $cell5;
 692                  $row3->cells[] = $cell6;
 693                  $t->data[] = $row3;
 694              }
 695  
 696              echo html_writer::tag('div', html_writer::table($t), array('class'=>'no-overflow'));
 697  
 698          }
 699      }
 700  
 701      function set_url() {
 702          global $PAGE, $CFG;
 703          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
 704      }
 705  
 706      protected function create_navbar() {
 707          global $PAGE, $CFG;
 708  
 709          parent::create_navbar();
 710          $PAGE->navbar->add(get_string('comments', 'wiki'));
 711      }
 712  
 713  }
 714  
 715  /**
 716   * Class that models the behavior of wiki's edit comment
 717   *
 718   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 719   */
 720  class page_wiki_editcomment extends page_wiki {
 721      private $comment;
 722      private $action;
 723      private $form;
 724      private $format;
 725  
 726      function set_url() {
 727          global $PAGE, $CFG;
 728          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
 729      }
 730  
 731      function print_header() {
 732          parent::print_header();
 733          $this->print_pagetitle();
 734      }
 735  
 736      function print_content() {
 737          global $PAGE;
 738  
 739          require_capability('mod/wiki:editcomment', $this->modcontext, NULL, true, 'noeditcommentpermission', 'wiki');
 740  
 741          if ($this->action == 'add') {
 742              $this->add_comment_form();
 743          } else if ($this->action == 'edit') {
 744              $this->edit_comment_form($this->comment);
 745          }
 746      }
 747  
 748      function set_action($action, $comment) {
 749          global $CFG;
 750          require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
 751  
 752          $this->action = $action;
 753          $this->comment = $comment;
 754          $version = wiki_get_current_version($this->page->id);
 755          $this->format = $version->contentformat;
 756  
 757          if ($this->format == 'html') {
 758              $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $this->page->id;
 759              $this->form = new mod_wiki_comments_form($destination);
 760          }
 761      }
 762  
 763      protected function create_navbar() {
 764          global $PAGE, $CFG;
 765  
 766          $PAGE->navbar->add(get_string('comments', 'wiki'), $CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id);
 767  
 768          if ($this->action == 'add') {
 769              $PAGE->navbar->add(get_string('insertcomment', 'wiki'));
 770          } else {
 771              $PAGE->navbar->add(get_string('editcomment', 'wiki'));
 772          }
 773      }
 774  
 775      protected function setup_tabs($options = array()) {
 776          parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
 777      }
 778  
 779      private function add_comment_form() {
 780          global $CFG;
 781          require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
 782  
 783          $pageid = $this->page->id;
 784  
 785          if ($this->format == 'html') {
 786              $com = new stdClass();
 787              $com->action = 'add';
 788              $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
 789              $this->form->set_data($com);
 790              $this->form->display();
 791          } else {
 792              wiki_print_editor_wiki($this->page->id, null, $this->format, -1, null, false, null, 'addcomments');
 793          }
 794      }
 795  
 796      private function edit_comment_form($com) {
 797          global $CFG;
 798          require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
 799          require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
 800  
 801          if ($this->format == 'html') {
 802              $com->action = 'edit';
 803              $com->entrycomment_editor['text'] = $com->content;
 804              $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
 805  
 806              $this->form->set_data($com);
 807              $this->form->display();
 808          } else {
 809              wiki_print_editor_wiki($this->page->id, $com->content, $this->format, -1, null, false, array(), 'editcomments', $com->id);
 810          }
 811  
 812      }
 813  
 814  }
 815  
 816  /**
 817   * Wiki page search page
 818   *
 819   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 820   */
 821  class page_wiki_search extends page_wiki {
 822      private $search_result;
 823  
 824      protected function create_navbar() {
 825          global $PAGE, $CFG;
 826  
 827          $PAGE->navbar->add(format_string($this->title));
 828      }
 829  
 830      function set_search_string($search, $searchcontent) {
 831          $swid = $this->subwiki->id;
 832          if ($searchcontent) {
 833              $this->search_result = wiki_search_all($swid, $search);
 834          } else {
 835              $this->search_result = wiki_search_title($swid, $search);
 836          }
 837  
 838      }
 839  
 840      function set_url() {
 841          global $PAGE, $CFG;
 842          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/search.php');
 843      }
 844  
 845      function print_header() {
 846          global $PAGE;
 847  
 848          parent::print_header();
 849  
 850          $wiki = $PAGE->activityrecord;
 851          $page = (object)array('title' => $wiki->firstpagetitle);
 852          $this->wikioutput->wiki_print_subwiki_selector($wiki, $this->subwiki, $page, 'search');
 853      }
 854  
 855      function print_content() {
 856          global $PAGE;
 857  
 858          require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
 859  
 860          echo $this->wikioutput->search_result($this->search_result, $this->subwiki);
 861      }
 862  }
 863  
 864  /**
 865   *
 866   * Class that models the behavior of wiki's
 867   * create page
 868   *
 869   */
 870  class page_wiki_create extends page_wiki {
 871  
 872      private $format;
 873      private $swid;
 874      private $wid;
 875      private $action;
 876      private $mform;
 877      private $groups;
 878  
 879      function print_header() {
 880          $this->set_url();
 881          parent::print_header();
 882      }
 883  
 884      function set_url() {
 885          global $PAGE, $CFG;
 886  
 887          $params = array();
 888          $params['swid'] = $this->swid;
 889          if ($this->action == 'new') {
 890              $params['action'] = 'new';
 891              $params['wid'] = $this->wid;
 892              if ($this->title != get_string('newpage', 'wiki')) {
 893                  $params['title'] = $this->title;
 894              }
 895          } else {
 896              $params['action'] = 'create';
 897          }
 898          $PAGE->set_url(new moodle_url('/mod/wiki/create.php', $params));
 899      }
 900  
 901      function set_format($format) {
 902          $this->format = $format;
 903      }
 904  
 905      function set_wid($wid) {
 906          $this->wid = $wid;
 907      }
 908  
 909      function set_swid($swid) {
 910          $this->swid = $swid;
 911      }
 912  
 913      function set_availablegroups($group) {
 914          $this->groups = $group;
 915      }
 916  
 917      function set_action($action) {
 918          global $PAGE;
 919          $this->action = $action;
 920  
 921          require_once(dirname(__FILE__) . '/create_form.php');
 922          $url = new moodle_url('/mod/wiki/create.php', array('action' => 'create', 'wid' => $PAGE->activityrecord->id, 'group' => $this->gid, 'uid' => $this->uid));
 923          $formats = wiki_get_formats();
 924          $options = array('formats' => $formats, 'defaultformat' => $PAGE->activityrecord->defaultformat, 'forceformat' => $PAGE->activityrecord->forceformat, 'groups' => $this->groups);
 925          if ($this->title != get_string('newpage', 'wiki')) {
 926              $options['disable_pagetitle'] = true;
 927          }
 928          $this->mform = new mod_wiki_create_form($url->out(false), $options);
 929      }
 930  
 931      protected function create_navbar() {
 932          global $PAGE;
 933          // navigation_node::get_content formats this before printing.
 934          $PAGE->navbar->add($this->title);
 935      }
 936  
 937      function print_content($pagetitle = '') {
 938          global $PAGE;
 939  
 940          // @TODO: Change this to has_capability and show an alternative interface.
 941          require_capability('mod/wiki:createpage', $this->modcontext, NULL, true, 'nocreatepermission', 'wiki');
 942          $data = new stdClass();
 943          if (!empty($pagetitle)) {
 944              $data->pagetitle = $pagetitle;
 945          }
 946          $data->pageformat = $PAGE->activityrecord->defaultformat;
 947  
 948          $this->mform->set_data($data);
 949          $this->mform->display();
 950      }
 951  
 952      function create_page($pagetitle) {
 953          global $USER, $PAGE;
 954  
 955          $data = $this->mform->get_data();
 956          if (isset($data->groupinfo)) {
 957              $groupid = $data->groupinfo;
 958          } else if (!empty($this->gid)) {
 959              $groupid = $this->gid;
 960          } else {
 961              $groupid = '0';
 962          }
 963          if (empty($this->subwiki)) {
 964              // If subwiki is not set then try find one and set else create one.
 965              if (!$this->subwiki = wiki_get_subwiki_by_group($this->wid, $groupid, $this->uid)) {
 966                  $swid = wiki_add_subwiki($PAGE->activityrecord->id, $groupid, $this->uid);
 967                  $this->subwiki = wiki_get_subwiki($swid);
 968              }
 969          }
 970          if ($data) {
 971              $this->set_title($data->pagetitle);
 972              $id = wiki_create_page($this->subwiki->id, $data->pagetitle, $data->pageformat, $USER->id);
 973          } else {
 974              $this->set_title($pagetitle);
 975              $id = wiki_create_page($this->subwiki->id, $pagetitle, $PAGE->activityrecord->defaultformat, $USER->id);
 976          }
 977          $this->page = $id;
 978          return $id;
 979      }
 980  }
 981  
 982  class page_wiki_preview extends page_wiki_edit {
 983  
 984      private $newcontent;
 985  
 986      function __construct($wiki, $subwiki, $cm) {
 987          global $PAGE, $CFG, $OUTPUT;
 988          parent::__construct($wiki, $subwiki, $cm);
 989          $buttons = $OUTPUT->update_module_button($cm->id, 'wiki');
 990          $PAGE->set_button($buttons);
 991  
 992      }
 993  
 994      function print_header() {
 995          global $PAGE, $CFG;
 996  
 997          parent::print_header();
 998  
 999      }
1000  
1001      function print_content() {
1002          global $PAGE;
1003  
1004          require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
1005  
1006          $this->print_preview();
1007      }
1008  
1009      function set_newcontent($newcontent) {
1010          $this->newcontent = $newcontent;
1011      }
1012  
1013      function set_url() {
1014          global $PAGE, $CFG;
1015  
1016          $params = array('pageid' => $this->page->id
1017          );
1018  
1019          if (isset($this->section)) {
1020              $params['section'] = $this->section;
1021          }
1022  
1023          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
1024      }
1025  
1026      protected function setup_tabs($options = array()) {
1027          parent::setup_tabs(array('linkedwhenactive' => 'view', 'activetab' => 'view'));
1028      }
1029  
1030      protected function check_locks() {
1031          return true;
1032      }
1033  
1034      protected function print_preview() {
1035          global $CFG, $PAGE, $OUTPUT;
1036  
1037          $version = wiki_get_current_version($this->page->id);
1038          $format = $version->contentformat;
1039          $content = $version->content;
1040  
1041          $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1042          if (!empty($this->section)) {
1043              $url .= "&section=" . urlencode($this->section);
1044          }
1045          $params = array(
1046              'attachmentoptions' => page_wiki_edit::$attachmentoptions,
1047              'format' => $this->format,
1048              'version' => $this->versionnumber,
1049              'contextid' => $this->modcontext->id
1050          );
1051  
1052          if ($this->format != 'html') {
1053              $params['component'] = 'mod_wiki';
1054              $params['filearea'] = 'attachments';
1055              $params['fileitemid'] = $this->page->id;
1056          }
1057          $form = new mod_wiki_edit_form($url, $params);
1058  
1059  
1060          $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
1061  
1062          if ($data = $form->get_data()) {
1063              if (isset($data->newcontent)) {
1064                  // wiki fromat
1065                  $text = $data->newcontent;
1066              } else {
1067                  // html format
1068                  $text = $data->newcontent_editor['text'];
1069              }
1070              $parseroutput = wiki_parse_content($data->contentformat, $text, $options);
1071              $this->set_newcontent($text);
1072              echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem');
1073              $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true, 'filter'=>false));
1074              echo $OUTPUT->box($content, 'generalbox wiki_previewbox');
1075              $content = $this->newcontent;
1076          }
1077  
1078          $this->print_edit($content);
1079      }
1080  
1081  }
1082  
1083  /**
1084   *
1085   * Class that models the behavior of wiki's
1086   * view differences
1087   *
1088   */
1089  class page_wiki_diff extends page_wiki {
1090  
1091      private $compare;
1092      private $comparewith;
1093  
1094      function print_header() {
1095          global $OUTPUT;
1096  
1097          parent::print_header();
1098  
1099          $this->print_pagetitle();
1100          $vstring = new stdClass();
1101          $vstring->old = $this->compare;
1102          $vstring->new = $this->comparewith;
1103          echo html_writer::tag('div', get_string('comparewith', 'wiki', $vstring), array('class' => 'wiki_headingtitle'));
1104      }
1105  
1106      /**
1107       * Print the diff view
1108       */
1109      function print_content() {
1110          global $PAGE;
1111  
1112          require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1113  
1114          $this->print_diff_content();
1115      }
1116  
1117      function set_url() {
1118          global $PAGE, $CFG;
1119  
1120          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/diff.php', array('pageid' => $this->page->id, 'comparewith' => $this->comparewith, 'compare' => $this->compare));
1121      }
1122  
1123      function set_comparison($compare, $comparewith) {
1124          $this->compare = $compare;
1125          $this->comparewith = $comparewith;
1126      }
1127  
1128      protected function create_navbar() {
1129          global $PAGE, $CFG;
1130  
1131          parent::create_navbar();
1132          $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $this->page->id);
1133          $PAGE->navbar->add(get_string('diff', 'wiki'));
1134      }
1135  
1136      protected function setup_tabs($options = array()) {
1137          parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1138      }
1139  
1140      /**
1141       * Given two versions of a page, prints a page displaying the differences between them.
1142       *
1143       * @global object $CFG
1144       * @global object $OUTPUT
1145       * @global object $PAGE
1146       */
1147      private function print_diff_content() {
1148          global $CFG, $OUTPUT, $PAGE;
1149  
1150          $pageid = $this->page->id;
1151          $total = wiki_count_wiki_page_versions($pageid) - 1;
1152  
1153          $oldversion = wiki_get_wiki_page_version($pageid, $this->compare);
1154  
1155          $newversion = wiki_get_wiki_page_version($pageid, $this->comparewith);
1156  
1157          if ($oldversion && $newversion) {
1158  
1159              $oldtext = format_text(file_rewrite_pluginfile_urls($oldversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
1160              $newtext = format_text(file_rewrite_pluginfile_urls($newversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
1161              list($diff1, $diff2) = ouwiki_diff_html($oldtext, $newtext);
1162              $oldversion->diff = $diff1;
1163              $oldversion->user = wiki_get_user_info($oldversion->userid);
1164              $newversion->diff = $diff2;
1165              $newversion->user = wiki_get_user_info($newversion->userid);
1166  
1167              echo $this->wikioutput->diff($pageid, $oldversion, $newversion, array('total' => $total));
1168          } else {
1169              print_error('versionerror', 'wiki');
1170          }
1171      }
1172  }
1173  
1174  /**
1175   *
1176   * Class that models the behavior of wiki's history page
1177   *
1178   */
1179  class page_wiki_history extends page_wiki {
1180      /**
1181       * @var int $paging current page
1182       */
1183      private $paging;
1184  
1185      /**
1186       * @var int @rowsperpage Items per page
1187       */
1188      private $rowsperpage = 10;
1189  
1190      /**
1191       * @var int $allversion if $allversion != 0, all versions will be printed in a signle table
1192       */
1193      private $allversion;
1194  
1195      function __construct($wiki, $subwiki, $cm) {
1196          global $PAGE;
1197          parent::__construct($wiki, $subwiki, $cm);
1198          $PAGE->requires->js_init_call('M.mod_wiki.history', null, true);
1199      }
1200  
1201      function print_header() {
1202          parent::print_header();
1203          $this->print_pagetitle();
1204      }
1205  
1206      function print_pagetitle() {
1207          global $OUTPUT;
1208          $html = '';
1209  
1210          $html .= $OUTPUT->container_start('wiki_headingtitle');
1211          $html .= $OUTPUT->heading_with_help(format_string($this->title), 'history', 'wiki', '', '', 3);
1212          $html .= $OUTPUT->container_end();
1213          echo $html;
1214      }
1215  
1216      function print_content() {
1217          global $PAGE;
1218  
1219          require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1220  
1221          $this->print_history_content();
1222      }
1223  
1224      function set_url() {
1225          global $PAGE, $CFG;
1226          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
1227      }
1228  
1229      function set_paging($paging) {
1230          $this->paging = $paging;
1231      }
1232  
1233      function set_allversion($allversion) {
1234          $this->allversion = $allversion;
1235      }
1236  
1237      protected function create_navbar() {
1238          global $PAGE, $CFG;
1239  
1240          parent::create_navbar();
1241          $PAGE->navbar->add(get_string('history', 'wiki'));
1242      }
1243  
1244      /**
1245       * Prints the history for a given wiki page
1246       *
1247       * @global object $CFG
1248       * @global object $OUTPUT
1249       * @global object $PAGE
1250       */
1251      private function print_history_content() {
1252          global $CFG, $OUTPUT, $PAGE;
1253  
1254          $pageid = $this->page->id;
1255          $offset = $this->paging * $this->rowsperpage;
1256          // vcount is the latest version
1257          $vcount = wiki_count_wiki_page_versions($pageid) - 1;
1258          if ($this->allversion) {
1259              $versions = wiki_get_wiki_page_versions($pageid, 0, $vcount);
1260          } else {
1261              $versions = wiki_get_wiki_page_versions($pageid, $offset, $this->rowsperpage);
1262          }
1263          // We don't want version 0 to be displayed
1264          // version 0 is blank page
1265          if (end($versions)->version == 0) {
1266              array_pop($versions);
1267          }
1268  
1269          $contents = array();
1270  
1271          $version0page = wiki_get_wiki_page_version($this->page->id, 0);
1272          $creator = wiki_get_user_info($version0page->userid);
1273          $a = new StdClass;
1274          $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
1275          $a->username = fullname($creator);
1276          echo html_writer::tag ('div', get_string('createddate', 'wiki', $a), array('class' => 'wiki_headingtime'));
1277          if ($vcount > 0) {
1278  
1279              /// If there is only one version, we don't need radios nor forms
1280              if (count($versions) == 1) {
1281  
1282                  $row = array_shift($versions);
1283  
1284                  $username = wiki_get_user_info($row->userid);
1285                  $picture = $OUTPUT->user_picture($username);
1286                  $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
1287                  $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
1288                  $versionid = wiki_get_version($row->id);
1289                  $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1290                  $userlink = new moodle_url('/user/view.php', array('id' => $username->id, 'course' => $this->cm->course));
1291                  $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picture . html_writer::link($userlink->out(false), fullname($username)), $time, $OUTPUT->container($date, 'wiki_histdate'));
1292  
1293                  $table = new html_table();
1294                  $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
1295                  $table->data = $contents;
1296                  $table->attributes['class'] = 'mdl-align';
1297  
1298                  echo html_writer::table($table);
1299  
1300              } else {
1301  
1302                  $checked = $vcount - $offset;
1303                  $rowclass = array();
1304  
1305                  foreach ($versions as $version) {
1306                      $user = wiki_get_user_info($version->userid);
1307                      $picture = $OUTPUT->user_picture($user, array('popup' => true));
1308                      $date = userdate($version->timecreated, get_string('strftimedate'));
1309                      $rowclass[] = 'wiki_histnewdate';
1310                      $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
1311                      $versionid = wiki_get_version($version->id);
1312                      if ($versionid) {
1313                          $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1314                          $viewlink = html_writer::link($url->out(false), $version->version);
1315                      } else {
1316                          $viewlink = $version->version;
1317                      }
1318                      $userlink = new moodle_url('/user/view.php', array('id' => $version->userid, 'course' => $this->cm->course));
1319                      $contents[] = array($this->choose_from_radio(array($version->version  => null), 'compare', 'M.mod_wiki.history()', $checked - 1, true) . $this->choose_from_radio(array($version->version  => null), 'comparewith', 'M.mod_wiki.history()', $checked, true), $viewlink, $picture . html_writer::link($userlink->out(false), fullname($user)), $time, $OUTPUT->container($date, 'wiki_histdate'));
1320                  }
1321  
1322                  $table = new html_table();
1323  
1324                  $icon = $OUTPUT->help_icon('diff', 'wiki');
1325  
1326                  $table->head = array(get_string('diff', 'wiki') . $icon, get_string('version'), get_string('user'), get_string('modified'), '');
1327                  $table->data = $contents;
1328                  $table->attributes['class'] = 'generaltable mdl-align';
1329                  $table->rowclasses = $rowclass;
1330  
1331                  // Print the form.
1332                  echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/diff.php'), 'method'=>'get', 'id'=>'diff'));
1333                  echo html_writer::tag('div', html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$pageid)));
1334                  echo html_writer::table($table);
1335                  echo html_writer::start_tag('div', array('class'=>'mdl-align'));
1336                  echo html_writer::empty_tag('input', array('type'=>'submit', 'class'=>'wiki_form-button', 'value'=>get_string('comparesel', 'wiki')));
1337                  echo html_writer::end_tag('div');
1338                  echo html_writer::end_tag('form');
1339              }
1340          } else {
1341              print_string('nohistory', 'wiki');
1342          }
1343          if (!$this->allversion) {
1344              //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;');
1345              // $pagingbar->pagevar = $pagevar;
1346              echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&amp;');
1347              //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;','paging');
1348              } else {
1349              $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
1350              $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
1351          }
1352          if ($vcount > $this->rowsperpage && !$this->allversion) {
1353              $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
1354              $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
1355          }
1356      }
1357  
1358      /**
1359       * Given an array of values, creates a group of radio buttons to be part of a form
1360       *
1361       * @param array  $options  An array of value-label pairs for the radio group (values as keys).
1362       * @param string $name     Name of the radiogroup (unique in the form).
1363       * @param string $onclick  Function to be executed when the radios are clicked.
1364       * @param string $checked  The value that is already checked.
1365       * @param bool   $return   If true, return the HTML as a string, otherwise print it.
1366       *
1367       * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
1368       */
1369      private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
1370  
1371          static $idcounter = 0;
1372  
1373          if (!$name) {
1374              $name = 'unnamed';
1375          }
1376  
1377          $output = '<span class="radiogroup ' . $name . "\">\n";
1378  
1379          if (!empty($options)) {
1380              $currentradio = 0;
1381              foreach ($options as $value => $label) {
1382                  $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
1383                  $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
1384                  $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
1385                  if ($value == $checked) {
1386                      $output .= ' checked="checked"';
1387                  }
1388                  if ($onclick) {
1389                      $output .= ' onclick="' . $onclick . '"';
1390                  }
1391                  if ($label === '') {
1392                      $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
1393                  } else {
1394                      $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
1395                  }
1396                  $currentradio = ($currentradio + 1) % 2;
1397              }
1398          }
1399  
1400          $output .= '</span>' . "\n";
1401  
1402          if ($return) {
1403              return $output;
1404          } else {
1405              echo $output;
1406          }
1407      }
1408  }
1409  
1410  /**
1411   * Class that models the behavior of wiki's map page
1412   *
1413   */
1414  class page_wiki_map extends page_wiki {
1415  
1416      /**
1417       * @var int wiki view option
1418       */
1419      private $view;
1420  
1421      function print_header() {
1422          parent::print_header();
1423          $this->print_pagetitle();
1424      }
1425  
1426      function print_content() {
1427          global $CFG, $PAGE;
1428  
1429          require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1430  
1431          if ($this->view > 0) {
1432              //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
1433          }
1434  
1435          switch ($this->view) {
1436          case 1:
1437              echo $this->wikioutput->menu_map($this->page->id, $this->view);
1438              $this->print_contributions_content();
1439              break;
1440          case 2:
1441              echo $this->wikioutput->menu_map($this->page->id, $this->view);
1442              $this->print_navigation_content();
1443              break;
1444          case 3:
1445              echo $this->wikioutput->menu_map($this->page->id, $this->view);
1446              $this->print_orphaned_content();
1447              break;
1448          case 4:
1449              echo $this->wikioutput->menu_map($this->page->id, $this->view);
1450              $this->print_index_content();
1451              break;
1452          case 6:
1453              echo $this->wikioutput->menu_map($this->page->id, $this->view);
1454              $this->print_updated_content();
1455              break;
1456          case 5:
1457          default:
1458              echo $this->wikioutput->menu_map($this->page->id, $this->view);
1459              $this->print_page_list_content();
1460          }
1461      }
1462  
1463      function set_view($option) {
1464          $this->view = $option;
1465      }
1466  
1467      function set_url() {
1468          global $PAGE, $CFG;
1469          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
1470      }
1471  
1472      protected function create_navbar() {
1473          global $PAGE;
1474  
1475          parent::create_navbar();
1476          $PAGE->navbar->add(get_string('map', 'wiki'));
1477      }
1478  
1479      /**
1480       * Prints the contributions tab content
1481       *
1482       * @uses $OUTPUT, $USER
1483       *
1484       */
1485      private function print_contributions_content() {
1486          global $CFG, $OUTPUT, $USER;
1487          $page = $this->page;
1488  
1489          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1490              $fresh = wiki_refresh_cachedcontent($page);
1491              $page = $fresh['page'];
1492          }
1493  
1494          $swid = $this->subwiki->id;
1495  
1496          $table = new html_table();
1497          $table->head = array(get_string('contributions', 'wiki') . $OUTPUT->help_icon('contributions', 'wiki'));
1498          $table->attributes['class'] = 'wiki_editor generalbox';
1499          $table->data = array();
1500          $table->rowclasses = array();
1501  
1502          $lastversions = array();
1503          $pages = array();
1504          $users = array();
1505  
1506          if ($contribs = wiki_get_contributions($swid, $USER->id)) {
1507              foreach ($contribs as $contrib) {
1508                  if (!array_key_exists($contrib->pageid, $pages)) {
1509                      $page = wiki_get_page($contrib->pageid);
1510                      $pages[$contrib->pageid] = $page;
1511                  } else {
1512                      continue;
1513                  }
1514  
1515                  if (!array_key_exists($page->id, $lastversions)) {
1516                      $version = wiki_get_last_version($page->id);
1517                      $lastversions[$page->id] = $version;
1518                  } else {
1519                      $version = $lastversions[$page->id];
1520                  }
1521  
1522                  if (!array_key_exists($version->userid, $users)) {
1523                      $user = wiki_get_user_info($version->userid);
1524                      $users[$version->userid] = $user;
1525                  } else {
1526                      $user = $users[$version->userid];
1527                  }
1528  
1529                  $link = wiki_parser_link($page->title, array('swid' => $swid));
1530                  $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1531  
1532                  $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content'], true, array('context' => $this->modcontext)) . '</a>';
1533                  $icon = $OUTPUT->user_picture($user, array('popup' => true));
1534  
1535                  $table->data[] = array("$icon&nbsp;$linkpage");
1536              }
1537          } else {
1538              $table->data[] = array(get_string('nocontribs', 'wiki'));
1539          }
1540          echo html_writer::table($table);
1541      }
1542  
1543      /**
1544       * Prints the navigation tab content
1545       *
1546       * @uses $OUTPUT
1547       *
1548       */
1549      private function print_navigation_content() {
1550          global $OUTPUT;
1551          $page = $this->page;
1552  
1553          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1554              $fresh = wiki_refresh_cachedcontent($page);
1555              $page = $fresh['page'];
1556          }
1557  
1558          $tolinks = wiki_get_linked_to_pages($page->id);
1559          $fromlinks = wiki_get_linked_from_pages($page->id);
1560  
1561          $table = new html_table();
1562          $table->attributes['class'] = 'wiki_navigation_from';
1563          $table->head = array(get_string('navigationfrom', 'wiki') . $OUTPUT->help_icon('navigationfrom', 'wiki') . ':');
1564          $table->data = array();
1565          $table->rowclasses = array();
1566          foreach ($fromlinks as $link) {
1567              $lpage = wiki_get_page($link->frompageid);
1568              $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1569              $table->data[] = array(html_writer::link($link->out(false), format_string($lpage->title)));
1570              $table->rowclasses[] = 'mdl-align';
1571          }
1572  
1573          $table_left = html_writer::table($table);
1574  
1575          $table = new html_table();
1576          $table->attributes['class'] = 'wiki_navigation_to';
1577          $table->head = array(get_string('navigationto', 'wiki') . $OUTPUT->help_icon('navigationto', 'wiki') . ':');
1578          $table->data = array();
1579          $table->rowclasses = array();
1580          foreach ($tolinks as $link) {
1581              if ($link->tomissingpage) {
1582                  $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
1583                  $table->data[] = array(html_writer::link($viewlink->out(false), format_string($link->tomissingpage), array('class' => 'wiki_newentry')));
1584              } else {
1585                  $lpage = wiki_get_page($link->topageid);
1586                  $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1587                  $table->data[] = array(html_writer::link($viewlink->out(false), format_string($lpage->title)));
1588              }
1589              $table->rowclasses[] = 'mdl-align';
1590          }
1591          $table_right = html_writer::table($table);
1592          echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
1593      }
1594  
1595      /**
1596       * Prints the index page tab content
1597       *
1598       *
1599       */
1600      private function print_index_content() {
1601          global $OUTPUT;
1602          $page = $this->page;
1603  
1604          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1605              $fresh = wiki_refresh_cachedcontent($page);
1606              $page = $fresh['page'];
1607          }
1608  
1609          // navigation_node get_content calls format string for us
1610          $node = new navigation_node($page->title);
1611  
1612          $keys = array();
1613          $tree = array();
1614          $tree = wiki_build_tree($page, $node, $keys);
1615  
1616          $table = new html_table();
1617          $table->head = array(get_string('pageindex', 'wiki') . $OUTPUT->help_icon('pageindex', 'wiki'));
1618          $table->attributes['class'] = 'wiki_editor generalbox';
1619          $table->data[] = array($this->render_navigation_node($tree));
1620  
1621          echo html_writer::table($table);
1622      }
1623  
1624      /**
1625       * Prints the page list tab content
1626       *
1627       *
1628       */
1629      private function print_page_list_content() {
1630          global $OUTPUT;
1631          $page = $this->page;
1632  
1633          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1634              $fresh = wiki_refresh_cachedcontent($page);
1635              $page = $fresh['page'];
1636          }
1637  
1638          $pages = wiki_get_page_list($this->subwiki->id);
1639  
1640          $stdaux = new stdClass();
1641          $strspecial = get_string('special', 'wiki');
1642  
1643          foreach ($pages as $page) {
1644              // We need to format the title here to account for any filtering
1645              $letter = format_string($page->title, true, array('context' => $this->modcontext));
1646              $letter = core_text::substr($letter, 0, 1);
1647              if (preg_match('/^[a-zA-Z]$/', $letter)) {
1648                  $letter = core_text::strtoupper($letter);
1649                  $stdaux->{$letter}[] = wiki_parser_link($page);
1650              } else {
1651                  $stdaux->{$strspecial}[] = wiki_parser_link($page);
1652              }
1653          }
1654  
1655          $table = new html_table();
1656          $table->head = array(get_string('pagelist', 'wiki') . $OUTPUT->help_icon('pagelist', 'wiki'));
1657          $table->attributes['class'] = 'wiki_editor generalbox';
1658          $table->align = array('center');
1659          foreach ($stdaux as $key => $elem) {
1660              $table->data[] = array($key);
1661              foreach ($elem as $e) {
1662                  $table->data[] = array(html_writer::link($e['url'], format_string($e['content'], true, array('context' => $this->modcontext))));
1663              }
1664          }
1665          echo html_writer::table($table);
1666      }
1667  
1668      /**
1669       * Prints the orphaned tab content
1670       *
1671       *
1672       */
1673      private function print_orphaned_content() {
1674          global $OUTPUT;
1675  
1676          $page = $this->page;
1677  
1678          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1679              $fresh = wiki_refresh_cachedcontent($page);
1680              $page = $fresh['page'];
1681          }
1682  
1683          $swid = $this->subwiki->id;
1684  
1685          $table = new html_table();
1686          $table->head = array(get_string('orphaned', 'wiki') . $OUTPUT->help_icon('orphaned', 'wiki'));
1687          $table->attributes['class'] = 'wiki_editor generalbox';
1688          $table->data = array();
1689          $table->rowclasses = array();
1690  
1691          if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
1692              foreach ($orphanedpages as $page) {
1693                  $link = wiki_parser_link($page->title, array('swid' => $swid));
1694                  $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1695                  $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>');
1696              }
1697          } else {
1698              $table->data[] = array(get_string('noorphanedpages', 'wiki'));
1699          }
1700  
1701          echo html_writer::table($table);
1702      }
1703  
1704      /**
1705       * Prints the updated tab content
1706       *
1707       * @uses $COURSE, $OUTPUT
1708       *
1709       */
1710      private function print_updated_content() {
1711          global $COURSE, $OUTPUT;
1712          $page = $this->page;
1713  
1714          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1715              $fresh = wiki_refresh_cachedcontent($page);
1716              $page = $fresh['page'];
1717          }
1718  
1719          $swid = $this->subwiki->id;
1720  
1721          $table = new html_table();
1722          $table->head = array(get_string('updatedpages', 'wiki') . $OUTPUT->help_icon('updatedpages', 'wiki'));
1723          $table->attributes['class'] = 'wiki_editor generalbox';
1724          $table->data = array();
1725          $table->rowclasses = array();
1726  
1727          if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
1728              $strdataux = '';
1729              foreach ($pages as $page) {
1730                  $user = wiki_get_user_info($page->userid);
1731                  $strdata = strftime('%d %b %Y', $page->timemodified);
1732                  if ($strdata != $strdataux) {
1733                      $table->data[] = array($OUTPUT->heading($strdata, 4));
1734                      $strdataux = $strdata;
1735                  }
1736                  $link = wiki_parser_link($page->title, array('swid' => $swid));
1737                  $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1738  
1739                  $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
1740                  $icon = $OUTPUT->user_picture($user, array($COURSE->id));
1741                  $table->data[] = array("$icon&nbsp;$linkpage");
1742              }
1743          } else {
1744              $table->data[] = array(get_string('noupdatedpages', 'wiki'));
1745          }
1746  
1747          echo html_writer::table($table);
1748      }
1749  
1750      protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
1751  
1752          // exit if empty, we don't want an empty ul element
1753          if (count($items) == 0) {
1754              return '';
1755          }
1756  
1757          // array of nested li elements
1758          $lis = array();
1759          foreach ($items as $item) {
1760              if (!$item->display) {
1761                  continue;
1762              }
1763              $content = $item->get_content();
1764              $title = $item->get_title();
1765              if ($item->icon instanceof renderable) {
1766                  $icon = $this->wikioutput->render($item->icon);
1767                  $content = $icon . '&nbsp;' . $content; // use CSS for spacing of icons
1768                  }
1769              if ($item->helpbutton !== null) {
1770                  $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
1771              }
1772  
1773              if ($content === '') {
1774                  continue;
1775              }
1776  
1777              if ($item->action instanceof action_link) {
1778                  //TODO: to be replaced with something else
1779                  $link = $item->action;
1780                  if ($item->hidden) {
1781                      $link->add_class('dimmed');
1782                  }
1783                  $content = $this->output->render($link);
1784              } else if ($item->action instanceof moodle_url) {
1785                  $attributes = array();
1786                  if ($title !== '') {
1787                      $attributes['title'] = $title;
1788                  }
1789                  if ($item->hidden) {
1790                      $attributes['class'] = 'dimmed_text';
1791                  }
1792                  $content = html_writer::link($item->action, $content, $attributes);
1793  
1794              } else if (is_string($item->action) || empty($item->action)) {
1795                  $attributes = array();
1796                  if ($title !== '') {
1797                      $attributes['title'] = $title;
1798                  }
1799                  if ($item->hidden) {
1800                      $attributes['class'] = 'dimmed_text';
1801                  }
1802                  $content = html_writer::tag('span', $content, $attributes);
1803              }
1804  
1805              // this applies to the li item which contains all child lists too
1806              $liclasses = array($item->get_css_type(), 'depth_' . $depth);
1807              if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
1808                  $liclasses[] = 'collapsed';
1809              }
1810              if ($item->isactive === true) {
1811                  $liclasses[] = 'current_branch';
1812              }
1813              $liattr = array('class' => join(' ', $liclasses));
1814              // class attribute on the div item which only contains the item content
1815              $divclasses = array('tree_item');
1816              if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
1817                  $divclasses[] = 'branch';
1818              } else {
1819                  $divclasses[] = 'leaf';
1820              }
1821              if (!empty($item->classes) && count($item->classes) > 0) {
1822                  $divclasses[] = join(' ', $item->classes);
1823              }
1824              $divattr = array('class' => join(' ', $divclasses));
1825              if (!empty($item->id)) {
1826                  $divattr['id'] = $item->id;
1827              }
1828              $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
1829              if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
1830                  $content = html_writer::empty_tag('hr') . $content;
1831              }
1832              $content = html_writer::tag('li', $content, $liattr);
1833              $lis[] = $content;
1834          }
1835  
1836          if (count($lis)) {
1837              return html_writer::tag('ul', implode("\n", $lis), $attrs);
1838          } else {
1839              return '';
1840          }
1841      }
1842  
1843  }
1844  
1845  /**
1846   * Class that models the behavior of wiki's restore version page
1847   *
1848   */
1849  class page_wiki_restoreversion extends page_wiki {
1850      private $version;
1851  
1852      function print_header() {
1853          parent::print_header();
1854          $this->print_pagetitle();
1855      }
1856  
1857      function print_content() {
1858          global $PAGE;
1859  
1860          $wiki = $PAGE->activityrecord;
1861          if (wiki_user_can_edit($this->subwiki, $wiki)) {
1862              $this->print_restoreversion();
1863          } else {
1864              echo get_string('cannoteditpage', 'wiki');
1865          }
1866  
1867      }
1868  
1869      function set_url() {
1870          global $PAGE, $CFG;
1871          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1872      }
1873  
1874      function set_versionid($versionid) {
1875          $this->version = wiki_get_version($versionid);
1876      }
1877  
1878      protected function create_navbar() {
1879          global $PAGE, $CFG;
1880  
1881          parent::create_navbar();
1882          $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
1883      }
1884  
1885      protected function setup_tabs($options = array()) {
1886          parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1887      }
1888  
1889      /**
1890       * Prints the restore version content
1891       *
1892       * @uses $CFG
1893       *
1894       * @param page $page The page whose version will be restored
1895       * @param int  $versionid The version to be restored
1896       * @param bool $confirm If false, shows a yes/no confirmation page.
1897       *     If true, restores the old version and redirects the user to the 'view' tab.
1898       */
1899      private function print_restoreversion() {
1900          global $OUTPUT;
1901  
1902          $version = wiki_get_version($this->version->id);
1903  
1904          $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'versionid'=>$version->id, 'sesskey'=>sesskey());
1905          $restoreurl = new moodle_url('/mod/wiki/restoreversion.php', $optionsyes);
1906          $return = new moodle_url('/mod/wiki/viewversion.php', array('pageid'=>$this->page->id, 'versionid'=>$version->id));
1907  
1908          echo $OUTPUT->container_start('wiki-form-center');
1909          echo html_writer::tag('div', get_string('restoreconfirm', 'wiki', $version->version));
1910          echo $OUTPUT->container_start(false, 'wiki_restoreform');
1911          echo '<form class="wiki_restore_yes" action="' . $restoreurl . '" method="post" id="restoreversion">';
1912          echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
1913          echo '</form>';
1914          echo '<form class="wiki_restore_no" action="' . $return . '" method="post">';
1915          echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1916          echo '</form>';
1917          echo $OUTPUT->container_end();
1918          echo $OUTPUT->container_end();
1919      }
1920  }
1921  /**
1922   * Class that models the behavior of wiki's delete comment confirmation page
1923   *
1924   */
1925  class page_wiki_deletecomment extends page_wiki {
1926      private $commentid;
1927  
1928      function print_header() {
1929          parent::print_header();
1930          $this->print_pagetitle();
1931      }
1932  
1933      function print_content() {
1934          $this->printconfirmdelete();
1935      }
1936  
1937      function set_url() {
1938          global $PAGE;
1939          $PAGE->set_url('/mod/wiki/instancecomments.php', array('pageid' => $this->page->id, 'commentid' => $this->commentid));
1940      }
1941  
1942      public function set_action($action, $commentid, $content) {
1943          $this->action = $action;
1944          $this->commentid = $commentid;
1945          $this->content = $content;
1946      }
1947  
1948      protected function create_navbar() {
1949          global $PAGE;
1950  
1951          parent::create_navbar();
1952          $PAGE->navbar->add(get_string('deletecommentcheck', 'wiki'));
1953      }
1954  
1955      protected function setup_tabs($options = array()) {
1956          parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
1957      }
1958  
1959      /**
1960       * Prints the comment deletion confirmation form
1961       *
1962       * @param page $page The page whose version will be restored
1963       * @param int  $versionid The version to be restored
1964       * @param bool $confirm If false, shows a yes/no confirmation page.
1965       *     If true, restores the old version and redirects the user to the 'view' tab.
1966       */
1967      private function printconfirmdelete() {
1968          global $OUTPUT;
1969  
1970          $strdeletecheck = get_string('deletecommentcheck', 'wiki');
1971          $strdeletecheckfull = get_string('deletecommentcheckfull', 'wiki');
1972  
1973          //ask confirmation
1974          $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'action'=>'delete', 'commentid'=>$this->commentid, 'sesskey'=>sesskey());
1975          $deleteurl = new moodle_url('/mod/wiki/instancecomments.php', $optionsyes);
1976          $return = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$this->page->id));
1977  
1978          echo $OUTPUT->container_start('wiki-form-center');
1979          echo html_writer::tag('p', $strdeletecheckfull);
1980          echo $OUTPUT->container_start(false, 'wiki_deletecommentform');
1981          echo '<form class="wiki_deletecomment_yes" action="' . $deleteurl . '" method="post" id="deletecomment">';
1982          echo '<div><input type="submit" name="confirmdeletecomment" value="' . get_string('yes') . '" /></div>';
1983          echo '</form>';
1984          echo '<form class="wiki_deletecomment_no" action="' . $return . '" method="post">';
1985          echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1986          echo '</form>';
1987          echo $OUTPUT->container_end();
1988          echo $OUTPUT->container_end();
1989      }
1990  }
1991  
1992  /**
1993   * Class that models the behavior of wiki's
1994   * save page
1995   *
1996   */
1997  class page_wiki_save extends page_wiki_edit {
1998  
1999      private $newcontent;
2000  
2001      function print_header() {
2002      }
2003  
2004      function print_content() {
2005          global $PAGE;
2006  
2007          $context = context_module::instance($this->cm->id);
2008          require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
2009  
2010          $this->print_save();
2011      }
2012  
2013      function set_newcontent($newcontent) {
2014          $this->newcontent = $newcontent;
2015      }
2016  
2017      protected function set_session_url() {
2018      }
2019  
2020      protected function print_save() {
2021          global $CFG, $USER, $OUTPUT, $PAGE;
2022  
2023          $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
2024          if (!empty($this->section)) {
2025              $url .= "&section=" . urlencode($this->section);
2026          }
2027  
2028          $params = array(
2029              'attachmentoptions' => page_wiki_edit::$attachmentoptions,
2030              'format' => $this->format,
2031              'version' => $this->versionnumber,
2032              'contextid' => $this->modcontext->id
2033          );
2034  
2035          if ($this->format != 'html') {
2036              $params['fileitemid'] = $this->page->id;
2037              $params['component']  = 'mod_wiki';
2038              $params['filearea']   = 'attachments';
2039          }
2040  
2041          $form = new mod_wiki_edit_form($url, $params);
2042  
2043          $save = false;
2044          $data = false;
2045          if ($data = $form->get_data()) {
2046              if ($this->format == 'html') {
2047                  $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
2048              }
2049  
2050              if (isset($this->section)) {
2051                  $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
2052              } else {
2053                  $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
2054              }
2055          }
2056  
2057          if ($save && $data) {
2058              if (!empty($CFG->usetags)) {
2059                  tag_set('wiki_pages', $this->page->id, $data->tags, 'mod_wiki', $this->modcontext->id);
2060              }
2061  
2062              $message = '<p>' . get_string('saving', 'wiki') . '</p>';
2063  
2064              if (!empty($save['sections'])) {
2065                  foreach ($save['sections'] as $s) {
2066                      $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
2067                  }
2068              }
2069  
2070              if ($this->versionnumber + 1 != $save['version']) {
2071                  $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
2072              }
2073  
2074              if (isset($errors) && !empty($errors)) {
2075                  foreach ($errors as $e) {
2076                      $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
2077                  }
2078              }
2079  
2080              //deleting old locks
2081              wiki_delete_locks($this->page->id, $USER->id, $this->section);
2082              $url = new moodle_url('/mod/wiki/view.php', array('pageid' => $this->page->id, 'group' => $this->subwiki->groupid));
2083              redirect($url);
2084          } else {
2085              print_error('savingerror', 'wiki');
2086          }
2087      }
2088  }
2089  
2090  /**
2091   * Class that models the behavior of wiki's view an old version of a page
2092   *
2093   */
2094  class page_wiki_viewversion extends page_wiki {
2095  
2096      private $version;
2097  
2098      function print_header() {
2099          parent::print_header();
2100          $this->print_pagetitle();
2101      }
2102  
2103      function print_content() {
2104          global $PAGE;
2105  
2106          require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2107  
2108          $this->print_version_view();
2109      }
2110  
2111      function set_url() {
2112          global $PAGE, $CFG;
2113          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2114      }
2115  
2116      function set_versionid($versionid) {
2117          $this->version = wiki_get_version($versionid);
2118      }
2119  
2120      protected function create_navbar() {
2121          global $PAGE, $CFG;
2122  
2123          parent::create_navbar();
2124          $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $this->page->id);
2125          $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
2126      }
2127  
2128      protected function setup_tabs($options = array()) {
2129          parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
2130      }
2131  
2132      /**
2133       * Given an old page version, output the version content
2134       *
2135       * @global object $CFG
2136       * @global object $OUTPUT
2137       * @global object $PAGE
2138       */
2139      private function print_version_view() {
2140          global $CFG, $OUTPUT, $PAGE;
2141          $pageversion = wiki_get_version($this->version->id);
2142  
2143          if ($pageversion) {
2144              $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2145              echo html_writer::tag('div', get_string('viewversion', 'wiki', $pageversion->version) . '<br />' .
2146                  html_writer::link($restorelink->out(false), '(' . get_string('restorethis', 'wiki') .
2147                  ')', array('class' => 'wiki_restore')) . '&nbsp;', array('class' => 'wiki_headingtitle'));
2148              $userinfo = wiki_get_user_info($pageversion->userid);
2149              $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong>&nbsp;' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
2150              $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
2151              $heading .= '&nbsp;&nbsp;&nbsp;<strong>' . get_string('user') . ':</strong>&nbsp;' . html_writer::link($viewlink->out(false), fullname($userinfo));
2152              $heading .= '&nbsp;&nbsp;&rarr;&nbsp;' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
2153              echo $OUTPUT->container($heading, 'wiki_headingtime', 'mdl-align wiki_modifieduser');
2154              $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
2155  
2156              $pageversion->content = file_rewrite_pluginfile_urls($pageversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id);
2157  
2158              $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
2159              $content = $OUTPUT->container(format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)), false, '', '', true);
2160              echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
2161  
2162          } else {
2163              print_error('versionerror', 'wiki');
2164          }
2165      }
2166  }
2167  
2168  class page_wiki_confirmrestore extends page_wiki_save {
2169  
2170      private $version;
2171  
2172      function set_url() {
2173          global $PAGE, $CFG;
2174          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2175      }
2176  
2177      function print_header() {
2178          $this->set_url();
2179      }
2180  
2181      function print_content() {
2182          global $CFG, $PAGE;
2183  
2184          $version = wiki_get_version($this->version->id);
2185          $wiki = $PAGE->activityrecord;
2186          if (wiki_user_can_edit($this->subwiki, $wiki) &&
2187                  wiki_restore_page($this->page, $version, $this->modcontext)) {
2188              redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
2189          } else {
2190              print_error('restoreerror', 'wiki', $version->version);
2191          }
2192      }
2193  
2194      function set_versionid($versionid) {
2195          $this->version = wiki_get_version($versionid);
2196      }
2197  }
2198  
2199  class page_wiki_prettyview extends page_wiki {
2200  
2201      function __construct($wiki, $subwiki, $cm) {
2202          global $PAGE;
2203          $PAGE->set_pagelayout('embedded');
2204          parent::__construct($wiki, $subwiki, $cm);
2205      }
2206  
2207      function print_header() {
2208          global $OUTPUT;
2209          $this->set_url();
2210  
2211          echo $OUTPUT->header();
2212          // Print dialog link.
2213          $printtext = get_string('print', 'wiki');
2214          $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'printicon');
2215          $printiconlink = html_writer::link('#', $printtext, $printlinkatt);
2216          echo html_writer::tag('div', $printiconlink, array('class' => 'displayprinticon'));
2217          echo html_writer::tag('h1', format_string($this->title), array('id' => 'wiki_printable_title'));
2218      }
2219  
2220      function print_content() {
2221          global $PAGE;
2222  
2223          require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2224  
2225          $this->print_pretty_view();
2226      }
2227  
2228      function set_url() {
2229          global $PAGE, $CFG;
2230  
2231          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
2232      }
2233  
2234      private function print_pretty_view() {
2235          $version = wiki_get_current_version($this->page->id);
2236  
2237          $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
2238  
2239          $html = $content['parsed_text'];
2240          $id = $this->subwiki->wikiid;
2241          if ($cm = get_coursemodule_from_instance("wiki", $id)) {
2242              $context = context_module::instance($cm->id);
2243              $html = file_rewrite_pluginfile_urls($html, 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $this->subwiki->id);
2244          }
2245          echo '<div id="wiki_printable_content">';
2246          echo format_text($html, FORMAT_HTML);
2247          echo '</div>';
2248      }
2249  }
2250  
2251  class page_wiki_handlecomments extends page_wiki {
2252      private $action;
2253      private $content;
2254      private $commentid;
2255      private $format;
2256  
2257      function print_header() {
2258          $this->set_url();
2259      }
2260  
2261      public function print_content() {
2262          global $CFG, $PAGE, $USER;
2263  
2264          if ($this->action == 'add') {
2265              require_capability('mod/wiki:editcomment', $this->modcontext);
2266              $this->add_comment($this->content, $this->commentid);
2267          } else if ($this->action == 'edit') {
2268              require_capability('mod/wiki:editcomment', $this->modcontext);
2269  
2270              $comment = wiki_get_comment($this->commentid);
2271              $owner = ($comment->userid == $USER->id);
2272  
2273              if ($owner) {
2274                  $this->add_comment($this->content, $this->commentid);
2275              }
2276          } else if ($this->action == 'delete') {
2277              $comment = wiki_get_comment($this->commentid);
2278  
2279              $manage = has_capability('mod/wiki:managecomment', $this->modcontext);
2280              $edit = has_capability('mod/wiki:editcomment', $this->modcontext);
2281              $owner = ($comment->userid == $USER->id);
2282  
2283              if ($manage || ($owner && $edit)) {
2284                  $this->delete_comment($this->commentid);
2285                  redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id, get_string('deletecomment', 'wiki'), 2);
2286              } else {
2287                  print_error('nopermissiontoeditcomment');
2288              }
2289          }
2290  
2291      }
2292  
2293      public function set_url() {
2294          global $PAGE, $CFG;
2295          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
2296      }
2297  
2298      public function set_action($action, $commentid, $content) {
2299          $this->action = $action;
2300          $this->commentid = $commentid;
2301          $this->content = $content;
2302  
2303          $version = wiki_get_current_version($this->page->id);
2304          $format = $version->contentformat;
2305  
2306          $this->format = $format;
2307      }
2308  
2309      private function add_comment($content, $idcomment) {
2310          global $CFG, $PAGE;
2311          require_once($CFG->dirroot . "/mod/wiki/locallib.php");
2312  
2313          $pageid = $this->page->id;
2314  
2315          wiki_add_comment($this->modcontext, $pageid, $content, $this->format);
2316  
2317          if (!$idcomment) {
2318              redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
2319          } else {
2320              $this->delete_comment($idcomment);
2321              redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('editingcomment', 'wiki'), 2);
2322          }
2323      }
2324  
2325      private function delete_comment($commentid) {
2326          global $CFG, $PAGE;
2327  
2328          $pageid = $this->page->id;
2329  
2330          wiki_delete_comment($commentid, $this->modcontext, $pageid);
2331      }
2332  
2333  }
2334  
2335  class page_wiki_lock extends page_wiki_edit {
2336  
2337      public function print_header() {
2338          $this->set_url();
2339      }
2340  
2341      protected function set_url() {
2342          global $PAGE, $CFG;
2343  
2344          $params = array('pageid' => $this->page->id);
2345  
2346          if ($this->section) {
2347              $params['section'] = $this->section;
2348          }
2349  
2350          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
2351      }
2352  
2353      protected function set_session_url() {
2354      }
2355  
2356      public function print_content() {
2357          global $USER, $PAGE;
2358  
2359          require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
2360  
2361          wiki_set_lock($this->page->id, $USER->id, $this->section);
2362      }
2363  
2364      public function print_footer() {
2365      }
2366  }
2367  
2368  class page_wiki_overridelocks extends page_wiki_edit {
2369      function print_header() {
2370          $this->set_url();
2371      }
2372  
2373      function print_content() {
2374          global $CFG, $PAGE;
2375  
2376          require_capability('mod/wiki:overridelock', $this->modcontext, NULL, true, 'nooverridelockpermission', 'wiki');
2377  
2378          wiki_delete_locks($this->page->id, null, $this->section, true, true);
2379  
2380          $args = "pageid=" . $this->page->id;
2381  
2382          if (!empty($this->section)) {
2383              $args .= "&section=" . urlencode($this->section);
2384          }
2385  
2386          redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2387      }
2388  
2389      function set_url() {
2390          global $PAGE, $CFG;
2391  
2392          $params = array('pageid' => $this->page->id);
2393  
2394          if (!empty($this->section)) {
2395              $params['section'] = $this->section;
2396          }
2397  
2398          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
2399      }
2400  
2401      protected function set_session_url() {
2402      }
2403  
2404      private function print_overridelocks() {
2405          global $CFG;
2406  
2407          wiki_delete_locks($this->page->id, null, $this->section, true, true);
2408  
2409          $args = "pageid=" . $this->page->id;
2410  
2411          if (!empty($this->section)) {
2412              $args .= "&section=" . urlencode($this->section);
2413          }
2414  
2415          redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2416      }
2417  
2418  }
2419  
2420  /**
2421   * This class will let user to delete wiki pages and page versions
2422   *
2423   */
2424  class page_wiki_admin extends page_wiki {
2425  
2426      public $view, $action;
2427      public $listorphan = false;
2428  
2429      /**
2430       * Constructor
2431       *
2432       * @global object $PAGE
2433       * @param mixed $wiki instance of wiki
2434       * @param mixed $subwiki instance of subwiki
2435       * @param stdClass $cm course module
2436       */
2437      function __construct($wiki, $subwiki, $cm) {
2438          global $PAGE;
2439          parent::__construct($wiki, $subwiki, $cm);
2440          $PAGE->requires->js_init_call('M.mod_wiki.deleteversion', null, true);
2441      }
2442  
2443      /**
2444       * Prints header for wiki page
2445       */
2446      function print_header() {
2447          parent::print_header();
2448          $this->print_pagetitle();
2449      }
2450  
2451      /**
2452       * This function will display administration view to users with managewiki capability
2453       */
2454      function print_content() {
2455          //make sure anyone trying to access this page has managewiki capabilities
2456          require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2457  
2458          //update wiki cache if timedout
2459          $page = $this->page;
2460          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
2461              $fresh = wiki_refresh_cachedcontent($page);
2462              $page = $fresh['page'];
2463          }
2464  
2465          //dispaly admin menu
2466          echo $this->wikioutput->menu_admin($this->page->id, $this->view);
2467  
2468          //Display appropriate admin view
2469          switch ($this->view) {
2470              case 1: //delete page view
2471                  $this->print_delete_content($this->listorphan);
2472                  break;
2473              case 2: //delete version view
2474                  $this->print_delete_version();
2475                  break;
2476              default: //default is delete view
2477                  $this->print_delete_content($this->listorphan);
2478                  break;
2479          }
2480      }
2481  
2482      /**
2483       * Sets admin view option
2484       *
2485       * @param int $view page view id
2486       * @param bool $listorphan is only valid for view 1.
2487       */
2488      public function set_view($view, $listorphan = true) {
2489          $this->view = $view;
2490          $this->listorphan = $listorphan;
2491      }
2492  
2493      /**
2494       * Sets page url
2495       *
2496       * @global object $PAGE
2497       * @global object $CFG
2498       */
2499      function set_url() {
2500          global $PAGE, $CFG;
2501          $PAGE->set_url($CFG->wwwroot . '/mod/wiki/admin.php', array('pageid' => $this->page->id));
2502      }
2503  
2504      /**
2505       * sets navigation bar for the page
2506       *
2507       * @global object $PAGE
2508       */
2509      protected function create_navbar() {
2510          global $PAGE;
2511  
2512          parent::create_navbar();
2513          $PAGE->navbar->add(get_string('admin', 'wiki'));
2514      }
2515  
2516      /**
2517       * Show wiki page delete options
2518       *
2519       * @param bool $showorphan
2520       */
2521      protected function print_delete_content($showorphan = true) {
2522          $contents = array();
2523          $table = new html_table();
2524          $table->head = array('', get_string('pagename','wiki'));
2525          $table->attributes['class'] = 'generaltable mdl-align';
2526          $swid = $this->subwiki->id;
2527          if ($showorphan) {
2528              if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
2529                  $this->add_page_delete_options($orphanedpages, $swid, $table);
2530              } else {
2531                  $table->data[] = array('', get_string('noorphanedpages', 'wiki'));
2532              }
2533          } else {
2534              if ($pages = wiki_get_page_list($swid)) {
2535                  $this->add_page_delete_options($pages, $swid, $table);
2536              } else {
2537                  $table->data[] = array('', get_string('nopages', 'wiki'));
2538              }
2539          }
2540  
2541          ///Print the form
2542          echo html_writer::start_tag('form', array(
2543                                                  'action' => new moodle_url('/mod/wiki/admin.php'),
2544                                                  'method' => 'post'));
2545          echo html_writer::tag('div', html_writer::empty_tag('input', array(
2546                                                                           'type'  => 'hidden',
2547                                                                           'name'  => 'pageid',
2548                                                                           'value' => $this->page->id)));
2549  
2550          echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
2551          echo html_writer::table($table);
2552          echo html_writer::start_tag('div', array('class' => 'mdl-align'));
2553          if (!$showorphan) {
2554              echo html_writer::empty_tag('input', array(
2555                                                       'type'    => 'submit',
2556                                                       'class'   => 'wiki_form-button',
2557                                                       'value'   => get_string('listorphan', 'wiki'),
2558                                                       'sesskey' => sesskey()));
2559          } else {
2560              echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'listall', 'value'=>'1'));
2561              echo html_writer::empty_tag('input', array(
2562                                                       'type'    => 'submit',
2563                                                       'class'   => 'wiki_form-button',
2564                                                       'value'   => get_string('listall', 'wiki'),
2565                                                       'sesskey' => sesskey()));
2566          }
2567          echo html_writer::end_tag('div');
2568          echo html_writer::end_tag('form');
2569      }
2570  
2571      /**
2572       * helper function for print_delete_content. This will add data to the table.
2573       *
2574       * @global object $OUTPUT
2575       * @param array $pages objects of wiki pages in subwiki
2576       * @param int $swid id of subwiki
2577       * @param object $table reference to the table in which data needs to be added
2578       */
2579      protected function add_page_delete_options($pages, $swid, &$table) {
2580          global $OUTPUT;
2581          foreach ($pages as $page) {
2582              $link = wiki_parser_link($page->title, array('swid' => $swid));
2583              $class = ($link['new']) ? 'class="wiki_newentry"' : '';
2584              $pagelink = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
2585              $urledit = new moodle_url('/mod/wiki/edit.php', array('pageid' => $page->id, 'sesskey' => sesskey()));
2586              $urldelete = new moodle_url('/mod/wiki/admin.php', array(
2587                                                                     'pageid'  => $this->page->id,
2588                                                                     'delete'  => $page->id,
2589                                                                     'option'  => $this->view,
2590                                                                     'listall' => !$this->listorphan?'1': '',
2591                                                                     'sesskey' => sesskey()));
2592  
2593              $editlinks = $OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit')));
2594              $editlinks .= $OUTPUT->action_icon($urldelete, new pix_icon('t/delete', get_string('delete')));
2595              $table->data[] = array($editlinks, $pagelink);
2596          }
2597      }
2598  
2599      /**
2600       * Prints lists of versions which can be deleted
2601       *
2602       * @global core_renderer $OUTPUT
2603       * @global moodle_page $PAGE
2604       */
2605      private function print_delete_version() {
2606          global $OUTPUT, $PAGE;
2607          $pageid = $this->page->id;
2608  
2609          // versioncount is the latest version
2610          $versioncount = wiki_count_wiki_page_versions($pageid) - 1;
2611          $versions = wiki_get_wiki_page_versions($pageid, 0, $versioncount);
2612  
2613          // We don't want version 0 to be displayed
2614          // version 0 is blank page
2615          if (end($versions)->version == 0) {
2616              array_pop($versions);
2617          }
2618  
2619          $contents = array();
2620          $version0page = wiki_get_wiki_page_version($this->page->id, 0);
2621          $creator = wiki_get_user_info($version0page->userid);
2622          $a = new stdClass();
2623          $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
2624          $a->username = fullname($creator);
2625          echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4);
2626          if ($versioncount > 0) {
2627              /// If there is only one version, we don't need radios nor forms
2628              if (count($versions) == 1) {
2629                  $row = array_shift($versions);
2630                  $username = wiki_get_user_info($row->userid);
2631                  $picture = $OUTPUT->user_picture($username);
2632                  $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
2633                  $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
2634                  $versionid = wiki_get_version($row->id);
2635                  $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
2636                  $userlink = new moodle_url('/user/view.php', array('id' => $username->id, 'course' => $this->cm->course));
2637                  $picturelink = $picture . html_writer::link($userlink->out(false), fullname($username));
2638                  $historydate = $OUTPUT->container($date, 'wiki_histdate');
2639                  $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picturelink, $time, $historydate);
2640  
2641                  //Show current version
2642                  $table = new html_table();
2643                  $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
2644                  $table->data = $contents;
2645                  $table->attributes['class'] = 'mdl-align';
2646  
2647                  echo html_writer::table($table);
2648              } else {
2649                  $lastdate = '';
2650                  $rowclass = array();
2651  
2652                  foreach ($versions as $version) {
2653                      $user = wiki_get_user_info($version->userid);
2654                      $picture = $OUTPUT->user_picture($user, array('popup' => true));
2655                      $date = userdate($version->timecreated, get_string('strftimedate'));
2656                      if ($date == $lastdate) {
2657                          $date = '';
2658                          $rowclass[] = '';
2659                      } else {
2660                          $lastdate = $date;
2661                          $rowclass[] = 'wiki_histnewdate';
2662                      }
2663  
2664                      $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
2665                      $versionid = wiki_get_version($version->id);
2666                      if ($versionid) {
2667                          $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
2668                          $viewlink = html_writer::link($url->out(false), $version->version);
2669                      } else {
2670                          $viewlink = $version->version;
2671                      }
2672  
2673                      $userlink = new moodle_url('/user/view.php', array('id' => $version->userid, 'course' => $this->cm->course));
2674                      $picturelink = $picture . html_writer::link($userlink->out(false), fullname($user));
2675                      $historydate = $OUTPUT->container($date, 'wiki_histdate');
2676                      $radiofromelement = $this->choose_from_radio(array($version->version  => null), 'fromversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
2677                      $radiotoelement = $this->choose_from_radio(array($version->version  => null), 'toversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
2678                      $contents[] = array( $radiofromelement . $radiotoelement, $viewlink, $picturelink, $time, $historydate);
2679                  }
2680  
2681                  $table = new html_table();
2682                  $table->head = array(get_string('deleteversions', 'wiki'), get_string('version'), get_string('user'), get_string('modified'), '');
2683                  $table->data = $contents;
2684                  $table->attributes['class'] = 'generaltable mdl-align';
2685                  $table->rowclasses = $rowclass;
2686  
2687                  ///Print the form
2688                  echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/admin.php'), 'method' => 'post'));
2689                  echo html_writer::tag('div', html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pageid', 'value' => $pageid)));
2690                  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
2691                  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' =>  sesskey()));
2692                  echo html_writer::table($table);
2693                  echo html_writer::start_tag('div', array('class' => 'mdl-align'));
2694                  echo html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'wiki_form-button', 'value' => get_string('deleteversions', 'wiki')));
2695                  echo html_writer::end_tag('div');
2696                  echo html_writer::end_tag('form');
2697              }
2698          } else {
2699              print_string('nohistory', 'wiki');
2700          }
2701      }
2702  
2703      /**
2704       * Given an array of values, creates a group of radio buttons to be part of a form
2705       * helper function for print_delete_version
2706       *
2707       * @param array  $options  An array of value-label pairs for the radio group (values as keys).
2708       * @param string $name     Name of the radiogroup (unique in the form).
2709       * @param string $onclick  Function to be executed when the radios are clicked.
2710       * @param string $checked  The value that is already checked.
2711       * @param bool   $return   If true, return the HTML as a string, otherwise print it.
2712       *
2713       * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
2714       */
2715      private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
2716  
2717          static $idcounter = 0;
2718  
2719          if (!$name) {
2720              $name = 'unnamed';
2721          }
2722  
2723          $output = '<span class="radiogroup ' . $name . "\">\n";
2724  
2725          if (!empty($options)) {
2726              $currentradio = 0;
2727              foreach ($options as $value => $label) {
2728                  $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
2729                  $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
2730                  $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
2731                  if ($value == $checked) {
2732                      $output .= ' checked="checked"';
2733                  }
2734                  if ($onclick) {
2735                      $output .= ' onclick="' . $onclick . '"';
2736                  }
2737                  if ($label === '') {
2738                      $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
2739                  } else {
2740                      $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
2741                  }
2742                  $currentradio = ($currentradio + 1) % 2;
2743              }
2744          }
2745  
2746          $output .= '</span>' . "\n";
2747  
2748          if ($return) {
2749              return $output;
2750          } else {
2751              echo $output;
2752          }
2753      }
2754  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1