Merge branch 'MDL-42392-26' of git://github.com/andrewnicols/moodle into MOODLE_26_STABLE
[moodle.git] / backup / cc / entity.forum.class.php
blob828bca965a90418853bd533cb2c6c9eab93e009f
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.
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
24 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
26 class cc_forum extends entities {
28 public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) {
30 $token = '$IMS-CC-FILEBASE$';
31 $path = str_replace($token, '', $path);
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;
44 while ($can_continue) {
46 $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
47 $can_continue = ($dir_part !== false);
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;
61 $rtemp = substr($path, 0, $start);
62 $start = strrpos($rtemp, $dir_sep);
63 $can_continue = (($start !== false) || (strlen($rtemp) > 0));
68 return $result;
71 public function generate_node () {
73 cc2moodle::log_action('Creating Forum mods');
75 $response = '';
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);
83 return $response;
86 private function create_node_course_modules_mod_forum ($instance) {
88 $sheet_mod_forum = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_FORUM);
90 $topic_data = $this->get_topic_data($instance);
92 $result = '';
93 if (!empty($topic_data)) {
95 $find_tags = array('[#mod_instance#]',
96 '[#mod_forum_title#]',
97 '[#mod_forum_intro#]',
98 '[#date_now#]');
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());
106 $result = str_replace($find_tags, $replace_values, $sheet_mod_forum);
110 return $result;
113 public function get_topic_data ($instance) {
115 $topic_data = '';
117 $topic_file = $this->get_external_xml($instance['resource_indentifier']);
119 if (!empty($topic_file)) {
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);
125 if (!empty($topic)) {
127 $xpath = cc2moodle::newx_path($topic, cc2moodle::getforumns());
129 $topic_title = $xpath->query('/dt:topic/title');
130 $topic_title = !empty($topic_title->item(0)->nodeValue) ? $topic_title->item(0)->nodeValue : 'Untitled Topic';
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)) : '';
136 if (!empty($topic_title)) {
137 $topic_data['title'] = $topic_title;
138 $topic_data['description'] = $topic_text;
142 $topic_attachments = $xpath->query('/dt:topic/attachments/attachment/@href');
144 if ($topic_attachments->length > 0) {
146 $attachment_html = '';
148 foreach ($topic_attachments as $file) {
149 $attachment_html .= $this->generate_attachment_html($this->full_path($file->nodeValue,'/'));
152 $topic_data['description'] = !empty($attachment_html) ? $topic_text . '<p>Attachments:</p>' . $attachment_html : $topic_text;
156 return $topic_data;
159 private function generate_attachment_html ($filename) {
161 $images_extensions = array('gif' , 'jpeg' , 'jpg' , 'jif' , 'jfif' , 'png' , 'bmp');
163 $fileinfo = pathinfo($filename);
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 />';
171 return '';