Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

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

    Search This Site: