AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / home / imscc / ims_export.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 define('TR_INCLUDE_PATH', '../../include/');
14 require_once(TR_INCLUDE_PATH.'vitals.inc.php');
15 global $_course_id, $_content_id;
16
17 require_once(TR_INCLUDE_PATH.'../home/classes/ContentManager.class.php');  /* content management class */
18 require_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
19 require_once(TR_INCLUDE_PATH.'classes/DAO/ContentDAO.class.php');
20 require_once(TR_INCLUDE_PATH.'classes/DAO/UsersDAO.class.php');
21
22 $_course_id = $course_id = (isset($_REQUEST['course_id']) ? intval($_REQUEST['course_id']) : $_course_id);
23 $_content_id = $cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : $_content_id; /* content id of an optional chapter */
24 $c   = isset($_REQUEST['c'])   ? intval($_REQUEST['c'])   : 0;
25
26 if ($course_id == 0 && $cid == 0)
27 {
28         $msg->addError('MISSING_COURSE_ID');
29         header('Location: ../index.php');
30         exit;   
31 }
32
33 if (isset($_REQUEST['to_tile']) && !isset($_POST['cancel'])) {
34         /* export from AContent to atutor */
35
36         require_once(TR_INCLUDE_PATH.'vitals.inc.php');
37         if (!authenticate(TR_PRIV_ADMIN, TR_PRIV_RETURN)) {
38                 /* user can't be authenticated */
39                 header('HTTP/1.1 404 Not Found');
40                 echo 'Document not found.';
41                 exit;
42         }
43
44         $m = md5(DB_PASSWORD . 'x' . ADMIN_PASSWORD . 'x' . $_SERVER['SERVER_ADDR'] . 'x' . $cid . 'x' . $_SESSION['course_id'] . 'x' . date('Ymd'));
45
46         header('Location: '.TR_TILE_IMPORT. '?cp='.urlencode(TR_BASE_HREF. 'tools/imscc/ims_export.php?cid='.$cid.'&c='.$_SESSION['course_id'].'&m='.$m));
47         exit;
48 } else if (isset($_GET['m'])) {
49         /* for TILE */
50
51         /* request (hopefully) coming from a TILE server, send the content package */
52
53         $_user_location = 'public';
54 //      require_once(TR_INCLUDE_PATH.'vitals.inc.php');
55         $m = md5(DB_PASSWORD . 'x' . ADMIN_PASSWORD . 'x' . $_SERVER['SERVER_ADDR'] . 'x' . $cid . 'x' . $c . 'x' . date('Ymd'));
56         if (($m != $_GET['m']) || !$c) {
57                 header('HTTP/1.1 404 Not Found');
58                 echo 'Document not found.';
59                 exit;
60         }
61         
62         $course_id = $c;
63 } else {
64         $use_a4a = false;
65         if (isset($_REQUEST['to_a4a'])){
66                 $use_a4a = true;
67         } 
68 //      require_once(TR_INCLUDE_PATH.'vitals.inc.php');
69 //      $course_id = $_SESSION['course_id'];
70 }
71 //load the following after vitals is included
72 require_once(TR_INCLUDE_PATH.'classes/testQuestions.class.php');
73 require_once(TR_INCLUDE_PATH.'classes/A4a/A4aExport.class.php');
74 require_once(TR_INCLUDE_PATH.'classes/Weblinks/Weblinks.class.php');
75 require_once(TR_INCLUDE_PATH.'classes/Weblinks/WeblinksExport.class.php');
76
77 $use_cc                  = true;
78
79 $coursesDAO = new CoursesDAO();
80 $course_row = $coursesDAO->get($course_id);
81
82 $contentManager = new ContentManager($course_id);
83
84 $instructor_id   = $course_row['user_id'];
85 $course_desc     = htmlspecialchars($course_row['description'], ENT_QUOTES, 'UTF-8');
86 $course_title    = htmlspecialchars($course_row['title'], ENT_QUOTES, 'UTF-8');
87 $course_language = $course_row['primary_language'];
88
89 $courseLanguage =& $languageManager->getLanguage($course_language);
90
91 //If course language cannot be found, use UTF-8 English
92 //@author harris, Oct 30,2008
93 if (!isset($courseLanguage) || $courseLanguage == null){
94         $courseLanguage =& $languageManager->getLanguage(DEFAULT_LANGUAGE_CODE);
95 }
96
97 $course_language_charset = $courseLanguage->getCharacterSet();
98 $course_language_code = $courseLanguage->getCode();
99
100 require_once(TR_INCLUDE_PATH.'classes/zipfile.class.php');                              /* for zipfile */
101 require_once(TR_INCLUDE_PATH.'classes/vcard.php');                                              /* for vcard */
102 require_once(TR_INCLUDE_PATH.'classes/XML/XML_HTMLSax/XML_HTMLSax.php');        /* for XML_HTMLSax */
103 require_once(TR_INCLUDE_PATH.'classes/ContentOutputParser.class.php');   /* to retrieve content resources/medias from at_content[text] */
104 require_once(TR_INCLUDE_PATH.'../home/imscc/include/ims_template.inc.php');                             /* for ims templates + print_organizations() */
105
106 if (isset($_POST['cancel'])) {
107         $msg->addFeedback('EXPORT_CANCELLED');
108         header('Location: ../index.php');
109         exit;
110 }
111
112 $zipfile = new zipfile(); 
113 $zipfile->create_dir('resources/');
114
115 /* get all the content */
116 $content = array();
117 $paths   = array();
118 $top_content_parent_id = 0;
119
120 $handler=new ContentOutputParser();
121 $parser = new XML_HTMLSax();
122 $parser->set_object($handler);
123 $parser->set_element_handler('openHandler','closeHandler');
124
125 $contentDAO = new ContentDAO();
126 $rows = $contentDAO->getContentByCourseID($course_id);
127
128 //if (authenticate(TR_PRIV_CONTENT, TR_PRIV_RETURN)) {
129 //      $sql = "SELECT *, UNIX_TIMESTAMP(last_modified) AS u_ts FROM ".TABLE_PREFIX."content WHERE course_id=$course_id ORDER BY content_parent_id, ordering";
130 //} else {
131 //      $sql = "SELECT *, UNIX_TIMESTAMP(last_modified) AS u_ts FROM ".TABLE_PREFIX."content WHERE course_id=$course_id ORDER BY content_parent_id, ordering";
132 //}
133 //$result = mysql_query($sql, $db);
134
135 //$cid = $_REQUEST['cid'];  //takes care of some system which lost the REQUEST[cid]
136 //while ($row = mysql_fetch_assoc($result)) {
137 //      if (authenticate(TR_PRIV_CONTENT, TR_PRIV_RETURN) || $contentManager->isReleased($row['content_id']) === TRUE) {
138 if (is_array($rows)) {
139         foreach ($rows as $row) {
140                 $content[$row['content_parent_id']][] = $row;
141                 if ($cid == $row['content_id']) {
142                         $top_content = $row;
143                         $top_content_parent_id = $row['content_parent_id'];
144                 }
145         }
146 }
147
148 if ($cid) {
149         /* filter out the top level sections that we don't want */
150         $top_level = $content[$top_content_parent_id];
151         foreach($top_level as $page) {
152                 if ($page['content_id'] == $cid) {
153                         $content[$top_content_parent_id] = array($page);
154                 } else {
155                         /* this is a page we don't want, so might as well remove it's children too */
156                         unset($content[$page['content_id']]);
157                 }
158         }
159         $ims_course_title = $course_title . ' - ' . $content[$top_content_parent_id][0]['title'];
160 } else {
161         $ims_course_title = $course_title;
162 }
163
164 $imsmanifest_xml = str_replace(array('{COURSE_TITLE}', '{COURSE_DESCRIPTION}', '{COURSE_PRIMARY_LANGUAGE_CHARSET}', '{COURSE_PRIMARY_LANGUAGE_CODE}'), 
165                                                           array($ims_course_title, $course_desc, $course_language_charset, $course_language_code),
166                                                           $ims_template_xml['header']);
167 //debug($imsmanifest_xml);
168 //exit;
169
170 /* get the first content page to default the body frame to */
171 $first = $content[$top_content_parent_id][0];
172
173 $test_ids = array();    //global array to store all the test ids
174
175 // Modified by Cindy Qi Li on Jan 12, 2010
176 //AContent does not support glossary
177 //TODO**************BOLOGNA***************REMOVE ME***************************/
178 /*
179 //Exoprt Forum:
180 global $forum_list;
181 $forum_list = array();
182
183 // generate the resources and save the HTML files
184 $used_glossary_terms = array();
185 ob_start();
186 print_organizations($top_content_parent_id, $content, 0, '', array(), $toc_html);
187
188 //Exoprt Forum:
189 print_resources_forum();
190
191 $organizations_str = ob_get_contents();
192 ob_end_clean();
193 if (count($used_glossary_terms)) {
194         $used_glossary_terms = array_unique($used_glossary_terms);
195         sort($used_glossary_terms);
196         reset($used_glossary_terms);
197         $terms_xml = '';
198         foreach ($used_glossary_terms as $term) {
199                 $term_key = htmlspecialchars($term, ENT_QUOTES, 'UTF-8');
200                 $glossary[$term_key] = htmlentities($glossary[$term_key], ENT_QUOTES, 'UTF-8');
201                 $glossary[$term_key] = str_replace('&', '&amp;', $glossary[$term_key]);
202                 $terms_xml .= str_replace(      array('{TERM}', '{DEFINITION}'),
203                                                                         array($term_key, $glossary[$term_key]),
204                                                                         $glossary_term_xml);
205         }
206         $glossary_xml = str_replace(array('{GLOSSARY_TERMS}', '{COURSE_PRIMARY_LANGUAGE_CHARSET}'),
207                                                             array($terms_xml, $course_language_charset),
208                                                                 $glossary_xml);
209         //add to resource
210         $resources .= $ims_template_xml['resource_glossary'];
211 } else {
212         unset($glossary_xml);
213 }
214
215 if ($glossary_xml){
216         $glossary_manifest_xml = $ims_template_xml['glossary'];
217 } else {
218         $glossary_manifest_xml = '';
219 }
220 */
221
222 ob_start();
223 print_organizations($top_content_parent_id, $content, 0, '', array(), $toc_html);
224
225 //Exoprt Forum:
226 print_resources_forum();
227
228 $organizations_str = ob_get_contents();
229 ob_end_clean();
230 // end of modified by Cindy Qi Li on Jan 12, 2010
231
232 /* append the Organizations and Resources to the imsmanifest */
233 $imsmanifest_xml .= str_replace(        array('{ORGANIZATIONS}', '{GLOSSARY}',  '{RESOURCES}', '{TEST_ITEMS}', '{COURSE_TITLE}'),
234                                                                         array($organizations_str, $glossary_manifest_xml,       $resources, $test_xml_items, $ims_course_title),
235                                                                         $ims_template_xml['final']);
236
237 // generate the vcard for the instructor/author
238 $usersDAO = new UsersDAO();
239 $row = $usersDAO->getUserByID($instructor_id);
240 //$sql = "SELECT first_name, last_name, email, website, login, phone FROM ".TABLE_PREFIX."members WHERE member_id=$instructor_id";
241 //$result = mysql_query($sql, $db);
242 $vcard = new vCard();
243 if (isset($row)) {
244         $vcard->setName($row['last_name'], $row['first_name'], $row['login']);
245         $vcard->setEmail($row['email']);
246         $vcard->setNote('Originated from an AContent at '.TR_BASE_HREF.'. See ATutor.ca for additional information.');
247         $vcard->setURL($row['website']);
248
249         $imsmanifest_xml = str_replace('{VCARD}', $vcard->getVCard(), $imsmanifest_xml);
250 } else {
251         $imsmanifest_xml = str_replace('{VCARD}', '', $imsmanifest_xml);
252 }
253
254 /* save the imsmanifest.xml file */
255 $zipfile->add_file($imsmanifest_xml, 'imsmanifest.xml');
256
257 /* Commented by Cindy Qi Li on Jan 12, 2010
258 // AContent does not have glossary and forums (discussion tools)
259 if ($glossary_xml) {
260         $zipfile->create_dir('resources/GlossaryItem/');
261         $zipfile->add_file($glossary_xml,  'resources/GlossaryItem/glossary.xml');
262 }
263 */
264
265 $zipfile->close(); // this is optional, since send_file() closes it anyway
266
267 $ims_course_title = str_replace(array(' ', ':'), '_', $ims_course_title);
268 /**
269  * A problem here with the preg_replace below.
270  * Originally was designed to remove all werid symbols to avoid file corruptions.
271  * In UTF-8, all non-english chars are considered to be 'werid symbols'
272  * We can still replace it as is, or add fileid to the filename to avoid these problems
273  * Well then again people won't be able to tell what this file is about
274  * If we are going to take out the preg_replace, some OS might not be able to understand
275  * these characters and will have problems importing.
276  */
277 $ims_course_title = preg_replace("{[^a-zA-Z0-9._-]}","", trim($ims_course_title));
278 $zipfile->send_file($ims_course_title . '_imscc');
279
280 exit;
281 ?>