scorm_admin.php 15 KB

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