weekly release 4.5dev
[moodle.git] / backup / cc / entity11.forum.class.php
blob988b8fd8db2625508cabbca06ef21340ab2cdb0b
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 2011 Darko Miletic (dmiletic@moodlerooms.com)
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
25 class cc11_forum extends entities11 {
27 public function full_path($path, $dir_sep = DIRECTORY_SEPARATOR) {
29 $token = '$IMS-CC-FILEBASE$';
30 $path = str_replace($token, '', $path);
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;
43 while ($can_continue) {
45 $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
46 $can_continue = ($dir_part !== false);
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;
60 $rtemp = substr($path, 0, $start);
61 $start = strrpos($rtemp, $dir_sep);
62 $can_continue = (($start !== false) || (strlen($rtemp) > 0));
67 return $result;
70 public function generate_node() {
72 cc2moodle::log_action('Creating Forum mods');
74 $response = '';
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);
82 return $response;
85 private function create_node_course_modules_mod_forum($instance) {
87 $sheet_mod_forum = cc112moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_FORUM);
89 $topic_data = $this->get_topic_data($instance);
91 $result = '';
92 if (!empty($topic_data)) {
94 $find_tags = array('[#mod_instance#]',
95 '[#mod_forum_title#]',
96 '[#mod_forum_intro#]',
97 '[#date_now#]');
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());
105 $result = str_replace($find_tags, $replace_values, $sheet_mod_forum);
109 return $result;
112 public function get_topic_data($instance) {
114 $topic_data = array();
116 $topic_file = $this->get_external_xml($instance['resource_indentifier']);
118 if (!empty($topic_file)) {
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);
124 if (!empty($topic)) {
126 $xpath = cc2moodle::newx_path($topic, cc112moodle::$forumns);
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';
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)) : '';
139 if (!empty($topic_title)) {
140 $topic_data['title'] = $topic_title;
141 $topic_data['description'] = $topic_text;
145 $topic_attachments = $xpath->query('/dt:topic/dt:attachments/dt:attachment/@href');
147 if ($topic_attachments->length > 0) {
149 $attachment_html = '';
151 foreach ($topic_attachments as $file) {
152 $attachment_html .= $this->generate_attachment_html($this->full_path($file->nodeValue,'/'));
155 $topic_data['description'] = !empty($attachment_html) ? $topic_text . '<p>Attachments:</p>' . $attachment_html : $topic_text;
159 return $topic_data;
162 private function generate_attachment_html($filename) {
164 $images_extensions = array('gif' , 'jpeg' , 'jpg' , 'jif' , 'jfif' , 'png' , 'bmp');
166 $fileinfo = pathinfo($filename);
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 />';
174 return '';