scorm_admin.php 15 KB

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