Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 2009 Mauro Rondinelli (mauro.rondinelli [AT] uvcms.com)
  20   * @copyright 2011 Darko Miletic (dmiletic@moodlerooms.com)
  21   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  25  
  26  class cc_forum extends entities {
  27  
  28      public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) {
  29  
  30          $token = '$IMS-CC-FILEBASE$';
  31          $path = str_replace($token, '', $path);
  32  
  33          if (is_string($path) && ($path != '')) {
  34              $dir_sep;
  35              $dot_dir = '.';
  36              $up_dir = '..';
  37              $length = strlen($path);
  38              $rtemp = trim($path);
  39              $start = strrpos($path, $dir_sep);
  40              $can_continue = ($start !== false);
  41              $result = $can_continue ? '' : $path;
  42              $rcount = 0;
  43  
  44              while ($can_continue) {
  45  
  46                  $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
  47                  $can_continue = ($dir_part !== false);
  48  
  49                  if ($can_continue) {
  50                      if ($dir_part != $dot_dir) {
  51                          if ($dir_part == $up_dir) {
  52                              $rcount++;
  53                          } else {
  54                              if ($rcount > 0) {
  55                                  $rcount --;
  56                              } else {
  57                                  $result = ($result == '') ? $dir_part : $dir_part . $dir_sep . $result;
  58                              }
  59                          }
  60                      }
  61                      $rtemp = substr($path, 0, $start);
  62                      $start = strrpos($rtemp, $dir_sep);
  63                      $can_continue = (($start !== false) || (strlen($rtemp) > 0));
  64                  }
  65              }
  66          }
  67  
  68          return $result;
  69      }
  70  
  71      public function generate_node () {
  72  
  73          cc2moodle::log_action('Creating Forum mods');
  74  
  75          $response = '';
  76  
  77          if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_FORUM])) {
  78              foreach (cc2moodle::$instances['instances'][MOODLE_TYPE_FORUM] as $instance) {
  79                  $response .= $this->create_node_course_modules_mod_forum($instance);
  80              }
  81          }
  82  
  83          return $response;
  84      }
  85  
  86      private function create_node_course_modules_mod_forum ($instance) {
  87  
  88          $sheet_mod_forum = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_FORUM);
  89  
  90          $topic_data = $this->get_topic_data($instance);
  91  
  92          $result = '';
  93          if (!empty($topic_data)) {
  94  
  95              $find_tags = array('[#mod_instance#]',
  96                                 '[#mod_forum_title#]',
  97                                 '[#mod_forum_intro#]',
  98                                 '[#date_now#]');
  99  
 100              $replace_values = array($instance['instance'],
 101                                      //To be more true to the actual forum name we use only forum title
 102                                      self::safexml($topic_data['title']),
 103                                      self::safexml($topic_data['description']),
 104                                      time());
 105  
 106              $result = str_replace($find_tags, $replace_values, $sheet_mod_forum);
 107  
 108          }
 109  
 110          return $result;
 111      }
 112  
 113      public function get_topic_data ($instance) {
 114  
 115          $topic_data = array();
 116  
 117          $topic_file = $this->get_external_xml($instance['resource_indentifier']);
 118  
 119          if (!empty($topic_file)) {
 120  
 121              $topic_file_path = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $topic_file;
 122              $topic_file_dir = dirname($topic_file_path);
 123              $topic = $this->load_xml_resource($topic_file_path);
 124  
 125              if (!empty($topic)) {
 126  
 127                  $xpath = cc2moodle::newx_path($topic, cc2moodle::getforumns());
 128  
 129                  $topic_title = $xpath->query('/dt:topic/title');
 130                  $topic_title = !empty($topic_title->item(0)->nodeValue) ? $topic_title->item(0)->nodeValue : 'Untitled Topic';
 131  
 132                  $topic_text = $xpath->query('/dt:topic/text');
 133                  $topic_text = !empty($topic_text->item(0)->nodeValue) ? $this->update_sources($topic_text->item(0)->nodeValue, dirname($topic_file)) : '';
 134                  $topic_text = !empty($topic_text) ? str_replace("%24", "\$", $this->include_titles($topic_text)) : '';
 135  
 136                  if (!empty($topic_title)) {
 137                      $topic_data['title'] = $topic_title;
 138                      $topic_data['description'] = $topic_text;
 139                  }
 140              }
 141  
 142              $topic_attachments = $xpath->query('/dt:topic/attachments/attachment/@href');
 143  
 144              if ($topic_attachments->length > 0) {
 145  
 146                  $attachment_html = '';
 147  
 148                  foreach ($topic_attachments as $file) {
 149                      $attachment_html .= $this->generate_attachment_html($this->full_path($file->nodeValue,'/'));
 150                  }
 151  
 152                  $topic_data['description'] = !empty($attachment_html) ? $topic_text . '<p>Attachments:</p>' . $attachment_html : $topic_text;
 153              }
 154          }
 155  
 156          return $topic_data;
 157      }
 158  
 159      private function generate_attachment_html ($filename) {
 160  
 161          $images_extensions = array('gif' , 'jpeg' , 'jpg' , 'jif' , 'jfif' , 'png' , 'bmp');
 162  
 163          $fileinfo = pathinfo($filename);
 164  
 165          if (in_array($fileinfo['extension'], $images_extensions)) {
 166              return '<img src="$@FILEPHP@$/' . $filename . '" title="' . $fileinfo['basename'] . '" alt="' . $fileinfo['basename'] . '" /><br />';
 167          } else {
 168              return '<a href="$@FILEPHP@$/' . $filename . '" title="' . $fileinfo['basename'] . '" alt="' . $fileinfo['basename'] . '">' . $fileinfo['basename'] . '</a><br />';
 169          }
 170  
 171          return '';
 172      }
 173  }