scorm_admin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // This file is probably deprecated - 2009-05-14 - ywarnier
  4. // No calls found in Chamilo file is deprecated. Adding an exit - 2011-06 jmontoya
  5. /**
  6. * This script handles SCO administration features
  7. * @package chamilo.learnpath.scorm
  8. * @author Denes Nagy, principal author
  9. * @author Isthvan Mandak, several new features
  10. * @author Roan Embrechts, code improvements and refactoring
  11. * @author Yannick Warnier, complete refactoring <ywarnier@beeznest.org>
  12. */
  13. exit;
  14. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  15. $use_anonymous = true;
  16. // Name of the language file that needs to be included.
  17. $language_file = 'scormdocument';
  18. $uncompress = 1; // TODO: This variable shouldn't be found here (find its usage before removal).
  19. require_once 'back_compat.inc.php';
  20. include 'learnpath_functions.inc.php';
  21. include_once 'scorm.lib.php';
  22. $is_allowedToEdit = api_is_allowed_to_edit();
  23. /* Variables */
  24. // Escapable integers.
  25. if ($_REQUEST['id'] != strval(intval($_REQUEST['id']))) { $id = $_REQUEST['id']; } else { $id = null; }
  26. // Imported strings.
  27. $path = (!empty($_REQUEST['path']) ? $_REQUEST['path'] : null);
  28. $Submit = (!empty($_POST['Submit']) ? $_POST['Submit'] : null);
  29. $submitImage = (!empty($_POST['submitImage']) ? $_POST['submitImage'] : null);
  30. $cancelSubmitImage = (!empty($_POST['cancelSubmitImage']) ? $_POST['cancelSubmitImage'] : null);
  31. $action = (!empty($_REQUEST['action']) ? $_REQUEST['action'] : null);
  32. $delete = (!empty($_REQUEST['delete']) ? $_REQUEST['delete'] : null);
  33. $createDir = (!empty($_REQUEST['createDir']) ? $_REQUEST['createDir'] : null);
  34. $make_directory_visible = (!empty($_REQUEST['make_directory_visible']) ? $_REQUEST['make_directory_visible'] : '');
  35. $make_directory_invisible = (!empty($_REQUEST['make_directory_invisible']) ? $_REQUEST['make_directory_invisible'] : '');
  36. // Values from POST form to add directory.
  37. $newDirPath = (!empty($_POST['newDirPath']) ? $_POST['newDirPath'] : null);
  38. $newDirName = (!empty($_POST['newDirName']) ? $_POST['newDirName'] : null);
  39. // Initialising internal variables.
  40. $dialogbox = '';
  41. if (! $is_allowed_in_course) api_not_allowed();
  42. $is_allowedToUnzip = $is_courseAdmin;
  43. /* Main code */
  44. switch ($action) {
  45. case 'exportpath':
  46. if (!empty($id)) {
  47. $export = exportpath($id);
  48. $dialogBox .= "This LP has been exported to the Document folder "
  49. ."of your course.";
  50. }
  51. break;
  52. case 'exportscorm':
  53. exportSCORM($path);
  54. break;
  55. case 'deletepath':
  56. /*
  57. DELETE A DOKEOS LEARNPATH
  58. and all the items in it
  59. */
  60. if (!empty($id)){
  61. $l="learnpath/learnpath_handler.php?learnpath_id=$id";
  62. $sql="DELETE FROM $tbl_tool where (link='$l' AND image='scormbuilder.gif')";
  63. $result=Database::query($sql);
  64. $sql="SELECT * FROM $tbl_learnpath_chapter where learnpath_id=$id";
  65. $result=Database::query($sql);
  66. while ($row=Database::fetch_array($result))
  67. {
  68. $c=$row['id'];
  69. $sql2="DELETE FROM $tbl_learnpath_item where chapter_id=$c";
  70. $result2=Database::query($sql2);
  71. }
  72. $sql="DELETE FROM $tbl_learnpath_chapter where learnpath_id=$id";
  73. $result=Database::query($sql);
  74. deletepath($id);
  75. $dialogBox=get_lang('_learnpath_deleted');
  76. }
  77. break;
  78. case 'publishpath':
  79. /* PUBLISHING (SHOWING) A DOKEOS LEARNPATH */
  80. if (!empty($id)){
  81. $sql = "SELECT * FROM $tbl_learnpath_main where learnpath_id=$id";
  82. $result = Database::query($sql);
  83. $row = Database::fetch_array($result);
  84. $name = domesticate($row['learnpath_name']);
  85. if ($set_visibility == 'i') {
  86. $s = $name.' '.get_lang('_no_published');
  87. $dialogBox = $s;
  88. $v = 0;
  89. }
  90. if ($set_visibility == 'v') {
  91. $s=$name.' '.get_lang('_published');
  92. $dialogBox = $s;
  93. $v = 1;
  94. }
  95. $sql = "SELECT * FROM $tbl_tool where (name='$name' and image='scormbuilder.gif')";
  96. $result = Database::query($sql);
  97. $row2 = Database::fetch_array($result);
  98. $num = Database::num_rows($result);
  99. if (($set_visibility == 'i') && ($num > 0)) {
  100. // It is visible or hidden but once was published.
  101. if (($row2['visibility']) == 1) {
  102. $sql = "DELETE FROM $tbl_tool WHERE (name='$name' and image='scormbuilder.gif')";
  103. } else {
  104. $sql = "UPDATE $tbl_tool set visibility=1 WHERE (name='$name' and image='scormbuilder.gif')";
  105. }
  106. } elseif (($set_visibility == 'v') && ($num == 0)) {
  107. $sql ="INSERT INTO $tbl_tool (id, name, link, image, visibility, admin, address, added_tool) VALUES ('$theid','$name','learnpath/learnpath_handler.php?learnpath_id=$id','scormbuilder.gif','$v','0','pastillegris.gif',0)";
  108. } else {
  109. // Parameter and database incompatible, do nothing.
  110. }
  111. $result = Database::query($sql);
  112. }
  113. break;
  114. case 'editpath':
  115. /* EDITING A DOKEOS NEW LEARNPATH */
  116. if (!empty($Submit)) {
  117. $l = "learnpath/learnpath_handler.php?learnpath_id=$id";
  118. $sql = "UPDATE $tbl_tool set name='".domesticate($learnpath_name)."' where (link='$l' and image='scormbuilder.gif')";
  119. $result = Database::query($sql);
  120. $sql = "UPDATE $tbl_learnpath_main SET learnpath_name='".domesticate($learnpath_name)."', learnpath_description='".domesticate($learnpath_description)."' WHERE learnpath_id=$id";
  121. $result = Database::query($sql);
  122. $dialogBox = get_lang('_learnpath_edited');
  123. }
  124. break;
  125. case 'add':
  126. /* ADDING A NEW LEARNPATH : treating the form */
  127. if (!empty($Submit)) {
  128. $sql = "INSERT INTO $tbl_learnpath_main (learnpath_name, learnpath_description) VALUES ('".domesticate($learnpath_name)."','".domesticate($learnpath_description)."')";
  129. Database::query($sql);
  130. $my_lp_id = Database::insert_id();
  131. $sql = "INSERT INTO $tbl_tool (name, link, image, visibility, admin, address, added_tool) VALUES ('".domesticate($learnpath_name)."','learnpath/learnpath_handler.php?learnpath_id=$my_lp_id','scormbuilder.gif','1','0','pastillegris.gif',0)";
  132. Database::query($sql);
  133. // Instead of displaying this info text, get the user directly to the learnpath edit page.
  134. //$dialogBox = get_lang('_learnpath_added');
  135. header('location:../learnpath/learnpath_handler.php?'.api_get_cidreq().'&learnpath_id='.$my_lp_id);
  136. exit();
  137. }
  138. break;
  139. case 'editscorm':
  140. /* EDITING A SCORM PACKAGE */
  141. if (!empty($Submit)) {
  142. $sql = "UPDATE $tbl_document SET comment='".domesticate($learnpath_description)."', name='".domesticate($learnpath_name)."' WHERE path='$path'";
  143. $result = Database::query($sql);
  144. $dialogBox = get_lang('_learnpath_edited');
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. if ($is_allowedToEdit) { // TEACHER ONLY
  151. /* UPLOAD SCORM */
  152. /*
  153. * Check the request method instead of a variable from POST
  154. * because if the file size exceeds the maximum file upload
  155. * size set in php.ini, all variables from POST are cleared !
  156. */
  157. if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_FILES) > 0 && empty($submitImage) && empty($cancelSubmitImage) && empty($action)) {
  158. // A SCORM upload has been detected, now deal with the file...
  159. // Directory creation.
  160. $s = $_FILES['userFile']['name'];
  161. $pathInfo = pathinfo($s);
  162. // Check the filename has at least several letters in it :-)
  163. // This is a very loose check as later on we might accept other formats of packages.
  164. // Sent than just "zip".
  165. if (preg_match('/[\w-_]+/', $pathInfo['basename'])) {
  166. // get the basename without extension.
  167. $newDirName = substr($pathInfo['basename'], 0, strlen($pathInfo['basename']) - (strlen($pathInfo['extension']) + 1));
  168. $newDirName = replace_dangerous_char(trim($newDirName), 'strict');
  169. if (check_name_exist($baseWorkDir.$newDirPath.$openDir.'/'.$newDirName)) {
  170. /** @todo change this output. Inaccurate at least in french. In this case, the
  171. * file might not exist or the transfer might have been wrong (no $_FILES at all)
  172. * but we still get the error message
  173. */
  174. $dialogBox = get_lang('FileExists');
  175. $createDir = $newDirPath; unset($newDirPath); // Return to step 1.
  176. } else {
  177. if (mkdir($baseWorkDir.$newDirPath.$openDir.'/'.$newDirName, api_get_permissions_for_new_directories())) {
  178. FileManager::set_default_settings($newDirPath.$openDir, $newDirName, 'folder', $tbl_document);
  179. // RH: was: set_default_settings($newDirPath.$openDir, $newDirName, 'folder');
  180. $dialogBox = get_lang('DirCr');
  181. } else {
  182. //Display msg "could not create dir..."
  183. //exit();
  184. }
  185. // Directory creation end.
  186. $uploadPath = $openDir.'/'.$newDirName;
  187. if (!$_FILES['userFile']['size']) {
  188. $dialogBox .= get_lang('FileError').'<br />'.get_lang('Notice').' : '.get_lang('MaxFileSize').' '.ini_get('upload_max_filesize');
  189. } else { // The file size is alright, we can assume the file is OK too.
  190. if ($uncompress == 1 && $is_allowedToUnzip) {
  191. $unzip = 'unzip';
  192. } else {
  193. $unzip = '';
  194. }
  195. if (treat_uploaded_file($_FILES['userFile'], $baseWorkDir, $uploadPath, $maxFilledSpace, $unzip)) {
  196. if ($uncompress == 1) {
  197. //$dialogBox .= get_lang('DownloadAndZipEnd');
  198. // Modified by darkden : I omitted this part, so the user can see
  199. // the scorm content message at once.
  200. } else {
  201. $dialogBox = get_lang('DownloadEnd');
  202. }
  203. // "WHAT'S NEW" notification: update table last_tooledit.
  204. //update_last_tooledit($_course, $nameTools, $id, get_lang('_new_document'), $_user['user_id']);
  205. item_property_update($_course, TOOL_LEARNPATH, $id, "LearnpathAdded", $_user['user_id']);
  206. } else {
  207. if (api_failure::get_last_failure() == 'not_enough_space') {
  208. $dialogBox = get_lang('NoSpace');
  209. } elseif (api_failure::get_last_failure() == 'php_file_in_zip_file') {
  210. $dialogBox = get_lang('ZipNoPhp');
  211. } elseif (api_failure::get_last_failure() == 'not_scorm_content') {
  212. $dialogBox = get_lang('NotScormContent');
  213. }
  214. }
  215. }
  216. $uploadPath = '';
  217. if (api_failure::get_last_failure()) {
  218. rmdir($baseWorkDir.$newDirPath.$openDir.'/'.$newDirName);
  219. }
  220. }
  221. } else { // The filename doesn't contain any alphanum chars (empty filename?)
  222. // Get a more detailed message?
  223. $dialogBox .= get_lang('FileError').'<br />';
  224. }
  225. /* DELETE FILE OR DIRECTORY */
  226. if (isset($delete)) {
  227. if ( scorm_delete($baseWorkDir.$delete)) {
  228. //$tbl_document = substr($tbl_document, 1, strlen($tbl_document) - 2); // RH...
  229. update_db_info('delete', $delete);
  230. $dialogBox = get_lang('DocDeleted');
  231. }
  232. }
  233. /* CREATE DIRECTORY */
  234. /*
  235. * The code begin with STEP 2 so it allows to return to STEP 1 if STEP 2 unsucceds.
  236. */
  237. /* STEP 2 */
  238. if (isset($newDirPath) && isset($newDirName)) {
  239. // echo $newDirPath . $newDirName;
  240. $newDirName = replace_dangerous_char(trim(stripslashes($newDirName)), 'strict');
  241. if (check_name_exist($baseWorkDir.$newDirPath.'/'.$newDirName)) {
  242. $dialogBox = get_lang('FileExists');
  243. $createDir = $newDirPath; unset($newDirPath);// return to step 1
  244. } else {
  245. if (mkdir($baseWorkDir.$newDirPath.'/'.$newDirName, api_get_permissions_for_new_directories()))
  246. FileManager::set_default_settings($newDirPath, $newDirName, 'folder', $tbl_document);
  247. // RH: was: set_default_settings($newDirPath, $newDirName, 'folder');
  248. $dialogBox = get_lang('DirCr');
  249. }
  250. }
  251. /* STEP 1 */
  252. if (isset($createDir)) {
  253. $dialogBox .= "<!-- create dir -->\n"
  254. ."<form name='createdir' action='' method='POST'>\n"
  255. ."<input type=\"hidden\" name=\"newDirPath\" value=\"$createDir\" />\n"
  256. .get_lang('NameDir')." : \n"
  257. ."<input type=\"text\" name=\"newDirName\" />\n"
  258. ."<input type=\"submit\" value=\"".get_lang('Ok')."\" />\n"
  259. ."</form>\n";
  260. }
  261. /* VISIBILITY COMMANDS */
  262. if (!empty($make_directory_visible) || !empty($make_directory_invisible)) {
  263. $visibilityPath = $make_directory_visible.$make_directory_invisible;
  264. // At least one of these variables are empty. So it's okay to proceed this way
  265. /* Check if there is yet a record for this file in the DB */
  266. $result = Database::query ("SELECT * FROM $tbl_document WHERE path LIKE '".$visibilityPath."'");
  267. while($row = Database::fetch_array($result, 'ASSOC')) {
  268. $attribute['path' ] = $row['path' ];
  269. $attribute['visibility'] = $row['visibility'];
  270. $attribute['comment' ] = $row['comment' ];
  271. }
  272. if ($make_directory_visible) {
  273. $newVisibilityStatus = 'v';
  274. } elseif ($make_directory_invisible) {
  275. $newVisibilityStatus = 'i';
  276. }
  277. $query = "UPDATE $tbl_document SET visibility='$newVisibilityStatus' WHERE path=\"".$visibilityPath."\""; // Added by Toon.
  278. Database::query($query);
  279. if (Database::affected_rows() == 0) { // Extra check added by Toon, normally not necessary anymore because all files are in the db.
  280. Database::query("INSERT INTO $tbl_document SET path=\"".$visibilityPath."\", visibility=\"".$newVisibilityStatus."\"");
  281. }
  282. unset($attribute);
  283. $dialogBox = get_lang('ViMod');
  284. }
  285. } // END is Allowed to edit;.
  286. }