Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  /**
  17   * @package   moodlecore
  18   * @subpackage backup-imscc
  19   * @copyright 2011 Darko Miletic (dmiletic@moodlerooms.com)
  20   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  24  
  25  class cc11_forum extends entities11 {
  26  
  27      public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) {
  28  
  29          $token = '$IMS-CC-FILEBASE$';
  30          $path = str_replace($token, '', $path);
  31  
  32          if (is_string($path) && ($path != '')) {
  33              $dir_sep;
  34              $dot_dir = '.';
  35              $up_dir = '..';
  36              $length = strlen($path);
  37              $rtemp = trim($path);
  38              $start = strrpos($path, $dir_sep);
  39              $can_continue = ($start !== false);
  40              $result = $can_continue ? '' : $path;
  41              $rcount = 0;
  42  
  43              while ($can_continue) {
  44  
  45                  $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
  46                  $can_continue = ($dir_part !== false);
  47  
  48                  if ($can_continue) {
  49                      if ($dir_part != $dot_dir) {
  50                          if ($dir_part == $up_dir) {
  51                              $rcount++;
  52                          } else {
  53                              if ($rcount > 0) {
  54                                  $rcount --;
  55                              } else {
  56                                  $result = ($result == '') ? $dir_part : $dir_part . $dir_sep . $result;
  57                              }
  58                          }
  59                      }
  60                      $rtemp = substr($path, 0, $start);
  61                      $start = strrpos($rtemp, $dir_sep);
  62                      $can_continue = (($start !== false) || (strlen($rtemp) > 0));
  63                  }
  64              }
  65          }
  66  
  67          return $result;
  68      }
  69  
  70      public function generate_node () {
  71  
  72          cc2moodle::log_action('Creating Forum mods');
  73  
  74          $response = '';
  75  
  76          if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_FORUM])) {
  77              foreach (cc2moodle::$instances['instances'][MOODLE_TYPE_FORUM] as $instance) {
  78                  $response .= $this->create_node_course_modules_mod_forum($instance);
  79              }
  80          }
  81  
  82          return $response;
  83      }
  84  
  85      private function create_node_course_modules_mod_forum ($instance) {
  86  
  87          $sheet_mod_forum = cc112moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_FORUM);
  88  
  89          $topic_data = $this->get_topic_data($instance);
  90  
  91          $result = '';
  92          if (!empty($topic_data)) {
  93  
  94              $find_tags = array('[#mod_instance#]',
  95                                     '[#mod_forum_title#]',
  96                                     '[#mod_forum_intro#]',
  97                                     '[#date_now#]');
  98  
  99              $replace_values = array($instance['instance'],
 100              //To be more true to the actual forum name we use only forum title
 101              self::safexml($topic_data['title']),
 102              self::safexml($topic_data['description']),
 103              time());
 104  
 105              $result = str_replace($find_tags, $replace_values, $sheet_mod_forum);
 106  
 107          }
 108  
 109          return $result;
 110      }
 111  
 112      public function get_topic_data ($instance) {
 113  
 114          $topic_data = array();
 115  
 116          $topic_file = $this->get_external_xml($instance['resource_indentifier']);
 117  
 118          if (!empty($topic_file)) {
 119  
 120              $topic_file_path = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $topic_file;
 121              $topic_file_dir = dirname($topic_file_path);
 122              $topic = $this->load_xml_resource($topic_file_path);
 123  
 124              if (!empty($topic)) {
 125  
 126                  $xpath = cc2moodle::newx_path($topic, cc112moodle::$forumns);
 127  
 128                  $topic_title = $xpath->query('/dt:topic/dt:title');
 129                  if ($topic_title->length > 0 && !empty($topic_title->item(0)->nodeValue)) {
 130                      $topic_title = $topic_title->item(0)->nodeValue;
 131                  } else {
 132                      $topic_title = 'Untitled Topic';
 133                  }
 134  
 135                  $topic_text = $xpath->query('/dt:topic/dt:text');
 136                  $topic_text = !empty($topic_text->item(0)->nodeValue) ? $this->update_sources($topic_text->item(0)->nodeValue, dirname($topic_file)) : '';
 137                  $topic_text = !empty($topic_text) ? str_replace("%24", "\$", $this->include_titles($topic_text)) : '';
 138  
 139                  if (!empty($topic_title)) {
 140                      $topic_data['title'] = $topic_title;
 141                      $topic_data['description'] = $topic_text;
 142                  }
 143              }
 144  
 145              $topic_attachments = $xpath->query('/dt:topic/dt:attachments/dt:attachment/@href');
 146  
 147              if ($topic_attachments->length > 0) {
 148  
 149                  $attachment_html = '';
 150  
 151                  foreach ($topic_attachments as $file) {
 152                      $attachment_html .= $this->generate_attachment_html($this->full_path($file->nodeValue,'/'));
 153                  }
 154  
 155                  $topic_data['description'] = !empty($attachment_html) ? $topic_text . '<p>Attachments:</p>' . $attachment_html : $topic_text;
 156              }
 157          }
 158  
 159          return $topic_data;
 160      }
 161  
 162      private function generate_attachment_html ($filename) {
 163  
 164          $images_extensions = array('gif' , 'jpeg' , 'jpg' , 'jif' , 'jfif' , 'png' , 'bmp');
 165  
 166          $fileinfo = pathinfo($filename);
 167  
 168          if (in_array($fileinfo['extension'], $images_extensions)) {
 169              return '<img src="$@FILEPHP@$/' . $filename . '" title="' . $fileinfo['basename'] . '" alt="' . $fileinfo['basename'] . '" /><br />';
 170          } else {
 171              return '<a href="$@FILEPHP@$/' . $filename . '" title="' . $fileinfo['basename'] . '" alt="' . $fileinfo['basename'] . '">' . $fileinfo['basename'] . '</a><br />';
 172          }
 173  
 174          return '';
 175      }
 176  }
 177