add_course.lib.inc.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class AddCourse
  5. */
  6. class AddCourse
  7. {
  8. /**
  9. * Defines the four needed keys to create a course based on several parameters.
  10. * @param string The code you want for this course
  11. * @param string Prefix added for ALL keys
  12. * @param string Prefix added for databases only
  13. * @param string Prefix added for paths only
  14. * @param bool Add unique prefix
  15. * @param bool Use code-independent keys
  16. * @return array An array with the needed keys ['currentCourseCode'], ['currentCourseId'], ['currentCourseDbName'], ['currentCourseRepository']
  17. * @todo Eliminate the global variables.
  18. * @assert (null) === false
  19. */
  20. public static function define_course_keys($wanted_code, $prefix_for_all = '', $prefix_for_base_name = '', $prefix_for_path = '', $add_unique_prefix = false, $use_code_indepedent_keys = true)
  21. {
  22. global $prefixAntiNumber, $_configuration;
  23. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  24. $wanted_code = CourseManager::generate_course_code($wanted_code);
  25. $keys_course_code = $wanted_code;
  26. if (!$use_code_indepedent_keys) {
  27. $wanted_code = '';
  28. }
  29. if ($add_unique_prefix) {
  30. $unique_prefix = substr(md5(uniqid(rand())), 0, 10);
  31. } else {
  32. $unique_prefix = '';
  33. }
  34. $keys = array();
  35. $final_suffix = array('CourseId' => '', 'CourseDb' => '', 'CourseDir' => '');
  36. $limit_numb_try = 100;
  37. $keys_are_unique = false;
  38. $try_new_fsc_id = $try_new_fsc_db = $try_new_fsc_dir = 0;
  39. while (!$keys_are_unique) {
  40. $keys_course_id = $prefix_for_all . $unique_prefix . $wanted_code . $final_suffix['CourseId'];
  41. //$keys_course_db_name = $prefix_for_base_name . $unique_prefix . strtoupper($keys_course_id) . $final_suffix['CourseDb'];
  42. $keys_course_repository = $prefix_for_path . $unique_prefix . $wanted_code . $final_suffix['CourseDir'];
  43. $keys_are_unique = true;
  44. // Check whether they are unique.
  45. $query = "SELECT 1 FROM ".$course_table." WHERE code='".$keys_course_id."' LIMIT 0,1";
  46. $result = Database::query($query);
  47. if (Database::num_rows($result)) {
  48. $keys_are_unique = false;
  49. $try_new_fsc_id ++;
  50. $final_suffix['CourseId'] = substr(md5(uniqid(rand())), 0, 4);
  51. }
  52. if (file_exists(api_get_path(SYS_COURSE_PATH).$keys_course_repository)) {
  53. $keys_are_unique = false;
  54. $try_new_fsc_dir ++;
  55. $final_suffix['CourseDir'] = substr(md5(uniqid(rand())), 0, 4);
  56. }
  57. if (($try_new_fsc_id + $try_new_fsc_db + $try_new_fsc_dir) > $limit_numb_try) {
  58. return $keys;
  59. }
  60. }
  61. $keys['currentCourseCode'] = $keys_course_code;
  62. $keys['currentCourseId'] = $keys_course_id;
  63. $keys['currentCourseRepository'] = $keys_course_repository;
  64. return $keys;
  65. }
  66. /**
  67. * Initializes a file repository for a newly created course.
  68. * @param string Course repository
  69. * @param string Course code
  70. * @return int 0
  71. * @assert (null,null) === false
  72. */
  73. public static function prepare_course_repository($course_repository, $course_code)
  74. {
  75. $perm = api_get_permissions_for_new_directories();
  76. $perm_file = api_get_permissions_for_new_files();
  77. $htmlpage = "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <title>Not authorized</title>\n </head>\n <body>\n </body>\n</html>";
  78. $cp = api_get_path(SYS_COURSE_PATH) . $course_repository;
  79. //Creating document folder
  80. mkdir($cp, $perm);
  81. mkdir($cp . '/document', $perm);
  82. $cpt = $cp . '/document/index.html';
  83. $fd = fopen($cpt, 'w');
  84. fwrite($fd, $htmlpage);
  85. fclose($fd);
  86. /*
  87. @chmod($cpt, $perm_file);
  88. @copy($cpt, $cp . '/document/index.html');
  89. mkdir($cp . '/document/images', $perm);
  90. @copy($cpt, $cp . '/document/images/index.html');
  91. mkdir($cp . '/document/images/gallery/', $perm);
  92. @copy($cpt, $cp . '/document/images/gallery/index.html');
  93. mkdir($cp . '/document/shared_folder/', $perm);
  94. @copy($cpt, $cp . '/document/shared_folder/index.html');
  95. mkdir($cp . '/document/audio', $perm);
  96. @copy($cpt, $cp . '/document/audio/index.html');
  97. mkdir($cp . '/document/flash', $perm);
  98. @copy($cpt, $cp . '/document/flash/index.html');
  99. mkdir($cp . '/document/video', $perm);
  100. @copy($cpt, $cp . '/document/video/index.html'); */
  101. //Creatind dropbox folder
  102. mkdir($cp . '/dropbox', $perm);
  103. $cpt = $cp . '/dropbox/index.html';
  104. $fd = fopen($cpt, 'w');
  105. fwrite($fd, $htmlpage);
  106. fclose($fd);
  107. @chmod($cpt, $perm_file);
  108. mkdir($cp . '/group', $perm);
  109. @copy($cpt, $cp . '/group/index.html');
  110. mkdir($cp . '/page', $perm);
  111. @copy($cpt, $cp . '/page/index.html');
  112. mkdir($cp . '/scorm', $perm);
  113. @copy($cpt, $cp . '/scorm/index.html');
  114. mkdir($cp . '/upload', $perm);
  115. @copy($cpt, $cp . '/upload/index.html');
  116. mkdir($cp . '/upload/forum', $perm);
  117. @copy($cpt, $cp . '/upload/forum/index.html');
  118. mkdir($cp . '/upload/forum/images', $perm);
  119. @copy($cpt, $cp . '/upload/forum/images/index.html');
  120. mkdir($cp . '/upload/test', $perm);
  121. @copy($cpt, $cp . '/upload/test/index.html');
  122. mkdir($cp . '/upload/blog', $perm);
  123. @copy($cpt, $cp . '/upload/blog/index.html');
  124. mkdir($cp . '/upload/learning_path', $perm);
  125. @copy($cpt, $cp . '/upload/learning_path/index.html');
  126. mkdir($cp . '/upload/learning_path/images', $perm);
  127. @copy($cpt, $cp . '/upload/learning_path/images/index.html');
  128. mkdir($cp . '/upload/calendar', $perm);
  129. @copy($cpt, $cp . '/upload/calendar/index.html');
  130. mkdir($cp . '/upload/calendar/images', $perm);
  131. @copy($cpt, $cp . '/upload/calendar/images/index.html');
  132. mkdir($cp . '/work', $perm);
  133. @copy($cpt, $cp . '/work/index.html');
  134. mkdir($cp . '/upload/announcements', $perm);
  135. @copy($cpt, $cp . '/upload/announcements/index.html');
  136. mkdir($cp . '/upload/announcements/images', $perm);
  137. @copy($cpt, $cp . '/upload/announcements/images/index.html');
  138. //Oral expression question type
  139. mkdir($cp . '/exercises', $perm);
  140. @copy($cpt, $cp . '/exercises/index.html');
  141. // Create .htaccess in the dropbox directory.
  142. $fp = fopen($cp . '/dropbox/.htaccess', 'w');
  143. fwrite(
  144. $fp,
  145. "AuthName AllowLocalAccess
  146. AuthType Basic
  147. order deny,allow
  148. deny from all
  149. php_flag zlib.output_compression off"
  150. );
  151. fclose($fp);
  152. // Build index.php of the course.
  153. /*$fd = fopen($cp . '/index.php', 'w');
  154. // str_replace() removes \r that cause squares to appear at the end of each line
  155. //@todo fix the harcoded include
  156. $string = str_replace(
  157. "\r",
  158. "",
  159. "<?" . "php
  160. \$cidReq = \"$course_code\";
  161. \$dbname = \"$course_code\";
  162. include(\"" . api_get_path(SYS_CODE_PATH) . "course_home/course_home.php\");
  163. ?>"
  164. );
  165. fwrite($fd, $string);
  166. @chmod($cp . '/index.php', $perm_file);*/
  167. return 0;
  168. }
  169. /**
  170. * Gets an array with all the course tables (deprecated?)
  171. * @return array
  172. * @assert (null) !== null
  173. */
  174. public static function get_course_tables()
  175. {
  176. $tables = array();
  177. $tables[] = 'tool';
  178. $tables[] = 'tool_intro';
  179. $tables[] = 'group_info';
  180. $tables[] = 'group_category';
  181. $tables[] = 'group_rel_user';
  182. $tables[] = 'group_rel_tutor';
  183. $tables[] = 'item_property';
  184. $tables[] = 'userinfo_content';
  185. $tables[] = 'userinfo_def';
  186. $tables[] = 'course_description';
  187. $tables[] = 'calendar_event';
  188. $tables[] = 'calendar_event_repeat';
  189. $tables[] = 'calendar_event_repeat_not';
  190. $tables[] = 'calendar_event_attachment';
  191. $tables[] = 'announcement';
  192. $tables[] = 'announcement_attachment';
  193. $tables[] = 'resource';
  194. $tables[] = 'student_publication';
  195. $tables[] = 'student_publication_assignment';
  196. $tables[] = 'document';
  197. $tables[] = 'forum_category';
  198. $tables[] = 'forum_forum';
  199. $tables[] = 'forum_thread';
  200. $tables[] = 'forum_post';
  201. $tables[] = 'forum_mailcue';
  202. $tables[] = 'forum_attachment';
  203. $tables[] = 'forum_notification';
  204. $tables[] = 'forum_thread_qualify';
  205. $tables[] = 'forum_thread_qualify_log';
  206. $tables[] = 'link';
  207. $tables[] = 'link_category';
  208. $tables[] = 'online_connected';
  209. $tables[] = 'online_link';
  210. $tables[] = 'chat_connected';
  211. $tables[] = 'quiz';
  212. $tables[] = 'quiz_rel_question';
  213. $tables[] = 'quiz_question';
  214. $tables[] = 'quiz_answer';
  215. $tables[] = 'quiz_question_option';
  216. $tables[] = 'quiz_question_category';
  217. $tables[] = 'quiz_question_rel_category';
  218. $tables[] = 'dropbox_post';
  219. $tables[] = 'dropbox_file';
  220. $tables[] = 'dropbox_person';
  221. $tables[] = 'dropbox_category';
  222. $tables[] = 'dropbox_feedback';
  223. $tables[] = 'lp';
  224. $tables[] = 'lp_item';
  225. $tables[] = 'lp_view';
  226. $tables[] = 'lp_item_view';
  227. $tables[] = 'lp_iv_interaction';
  228. $tables[] = 'lp_iv_objective';
  229. $tables[] = 'blog';
  230. $tables[] = 'blog_comment';
  231. $tables[] = 'blog_post';
  232. $tables[] = 'blog_rating';
  233. $tables[] = 'blog_rel_user';
  234. $tables[] = 'blog_task';
  235. $tables[] = 'blog_task_rel_user';
  236. $tables[] = 'blog_attachment';
  237. $tables[] = 'permission_group';
  238. $tables[] = 'permission_user';
  239. $tables[] = 'permission_task';
  240. $tables[] = 'role';
  241. $tables[] = 'role_group';
  242. $tables[] = 'role_permissions';
  243. $tables[] = 'role_user';
  244. $tables[] = 'survey';
  245. $tables[] = 'survey_question';
  246. $tables[] = 'survey_question_option';
  247. $tables[] = 'survey_invitation';
  248. $tables[] = 'survey_answer';
  249. $tables[] = 'survey_group';
  250. $tables[] = 'wiki';
  251. $tables[] = 'wiki_conf';
  252. $tables[] = 'wiki_discuss';
  253. $tables[] = 'wiki_mailcue';
  254. $tables[] = 'course_setting';
  255. $tables[] = 'glossary';
  256. $tables[] = 'notebook';
  257. $tables[] = 'attendance';
  258. $tables[] = 'attendance_sheet';
  259. $tables[] = 'attendance_calendar';
  260. $tables[] = 'attendance_result';
  261. $tables[] = 'attendance_sheet_log';
  262. $tables[] = 'thematic';
  263. $tables[] = 'thematic_plan';
  264. $tables[] = 'thematic_advance';
  265. return $tables;
  266. }
  267. /**
  268. * Executed only before create_course_tables()
  269. * @return void
  270. * @assert (null) === null
  271. */
  272. public static function drop_course_tables()
  273. {
  274. $list = self::get_course_tables();
  275. foreach ($list as $table) {
  276. $sql = "DROP TABLE IF EXISTS " . DB_COURSE_PREFIX . $table;
  277. Database::query($sql);
  278. }
  279. }
  280. /**
  281. * Returns a list of all files in the given course directory. The requested
  282. * directory will be checked against a "checker" directory to avoid access to
  283. * protected/unauthorized files
  284. * @param string Complete path to directory we want to list
  285. * @param array A list of files to which we want to add the files found
  286. * @param string Type of base directory from which we want to recover the files
  287. * @return array
  288. * @assert (null,null,null) === false
  289. * @assert ('abc',array(),'') === array()
  290. */
  291. public static function browse_folders($path, $files, $media)
  292. {
  293. if ($media == 'images') {
  294. $code_path = api_get_path(
  295. SYS_CODE_PATH
  296. ) . 'default_course_document/images/';
  297. }
  298. if ($media == 'audio') {
  299. $code_path = api_get_path(
  300. SYS_CODE_PATH
  301. ) . 'default_course_document/audio/';
  302. }
  303. if ($media == 'flash') {
  304. $code_path = api_get_path(
  305. SYS_CODE_PATH
  306. ) . 'default_course_document/flash/';
  307. }
  308. if ($media == 'video') {
  309. $code_path = api_get_path(
  310. SYS_CODE_PATH
  311. ) . 'default_course_document/video/';
  312. }
  313. if ($media == 'certificates') {
  314. $code_path = api_get_path(
  315. SYS_CODE_PATH
  316. ) . 'default_course_document/certificates/';
  317. }
  318. if (is_dir($path)) {
  319. $handle = opendir($path);
  320. while (false !== ($file = readdir($handle))) {
  321. if (is_dir($path . $file) && strpos($file, '.') !== 0) {
  322. $files[]['dir'] = str_replace(
  323. $code_path,
  324. '',
  325. $path . $file . '/'
  326. );
  327. $files = self::browse_folders(
  328. $path . $file . '/',
  329. $files,
  330. $media
  331. );
  332. } elseif (is_file($path . $file) && strpos($file, '.') !== 0) {
  333. $files[]['file'] = str_replace(
  334. $code_path,
  335. '',
  336. $path . $file
  337. );
  338. }
  339. }
  340. }
  341. return $files;
  342. }
  343. /**
  344. * Sorts pictures by type (used?)
  345. * @param array List of files (sthg like array(0=>array('png'=>1)))
  346. * @param string File type
  347. * @return array The received array without files not matching type
  348. * @assert (array(),null) === array()
  349. */
  350. public static function sort_pictures($files, $type)
  351. {
  352. $pictures = array();
  353. foreach ($files as $key => $value) {
  354. if (isset($value[$type]) && $value[$type] != '') {
  355. $pictures[][$type] = $value[$type];
  356. }
  357. }
  358. return $pictures;
  359. }
  360. /**
  361. * Function to convert a string from the language files to a string ready
  362. * to insert into the database (escapes single quotes)
  363. * @author Bart Mollet (bart.mollet@hogent.be)
  364. * @param string $string The string to convert
  365. * @return string The string converted to insert into the database
  366. * @assert ('a\'b') === 'ab'
  367. */
  368. public static function lang2db($string)
  369. {
  370. $string = str_replace("\\'", "'", $string);
  371. $string = Database::escape_string($string);
  372. return $string;
  373. }
  374. /**
  375. * Fills the course database with some required content and example content.
  376. * @param int Course (int) ID
  377. * @param string Course directory name (e.g. 'ABC')
  378. * @param string Language used for content (e.g. 'spanish')
  379. * @param bool Whether to fill the course with example content
  380. * @return bool False on error, true otherwise
  381. * @version 1.2
  382. * @assert (null, '', '', null) === false
  383. * @assert (1, 'ABC', null, null) === false
  384. * @assert (1, 'TEST', 'spanish', true) === true
  385. */
  386. public static function fill_db_course(
  387. $course_id,
  388. $course_repository,
  389. $language,
  390. $fill_with_exemplary_content = null
  391. ) {
  392. if (is_null($fill_with_exemplary_content)) {
  393. $fill_with_exemplary_content = api_get_setting(
  394. 'example_material_course_creation'
  395. ) != 'false';
  396. }
  397. global $_configuration;
  398. $course_id = intval($course_id);
  399. if (empty($course_id)) {
  400. return false;
  401. }
  402. $courseInfo = api_get_course_info_by_id($course_id);
  403. $now = api_get_utc_datetime(time());
  404. $tbl_course_homepage = Database::get_course_table(TABLE_TOOL_LIST);
  405. $TABLEINTROS = Database::get_course_table(TABLE_TOOL_INTRO);
  406. $TABLEGROUPCATEGORIES = Database::get_course_table(
  407. TABLE_GROUP_CATEGORY
  408. );
  409. $TABLEITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  410. $TABLETOOLAGENDA = Database::get_course_table(TABLE_AGENDA);
  411. $TABLETOOLANNOUNCEMENTS = Database::get_course_table(
  412. TABLE_ANNOUNCEMENT
  413. );
  414. $TABLETOOLDOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  415. $TABLETOOLLINK = Database::get_course_table(TABLE_LINK);
  416. $TABLEQUIZ = Database::get_course_table(TABLE_QUIZ_TEST);
  417. $TABLEQUIZQUESTION = Database::get_course_table(
  418. TABLE_QUIZ_TEST_QUESTION
  419. );
  420. $TABLEQUIZQUESTIONLIST = Database::get_course_table(
  421. TABLE_QUIZ_QUESTION
  422. );
  423. $TABLEQUIZANSWERSLIST = Database::get_course_table(TABLE_QUIZ_ANSWER);
  424. $TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING);
  425. $TABLEFORUMCATEGORIES = Database::get_course_table(
  426. TABLE_FORUM_CATEGORY
  427. );
  428. $TABLEFORUMS = Database::get_course_table(TABLE_FORUM);
  429. $TABLEFORUMTHREADS = Database::get_course_table(TABLE_FORUM_THREAD);
  430. $TABLEFORUMPOSTS = Database::get_course_table(TABLE_FORUM_POST);
  431. $TABLEGRADEBOOK = Database::get_main_table(
  432. TABLE_MAIN_GRADEBOOK_CATEGORY
  433. );
  434. $TABLEGRADEBOOKLINK = Database::get_main_table(
  435. TABLE_MAIN_GRADEBOOK_LINK
  436. );
  437. $TABLEGRADEBOOKCERT = Database::get_main_table(
  438. TABLE_MAIN_GRADEBOOK_CERTIFICATE
  439. );
  440. include_once api_get_path(SYS_CODE_PATH) . 'lang/english/trad4all.inc.php';
  441. $file_to_include = api_get_path(SYS_CODE_PATH) . 'lang/' . $language . '/trad4all.inc.php';
  442. if (file_exists($file_to_include)) {
  443. include_once $file_to_include;
  444. }
  445. $visible_for_all = 1;
  446. $visible_for_course_admin = 0;
  447. $visible_for_platform_admin = 2;
  448. /* Course tools */
  449. Database::query(
  450. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  451. VALUES ($course_id, 1, '" . TOOL_COURSE_DESCRIPTION . "','course_description/','info.gif','" . self::string2binary(
  452. api_get_setting(
  453. 'course_create_active_tools',
  454. 'course_description'
  455. )
  456. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  457. );
  458. Database::query(
  459. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  460. VALUES ($course_id, 2, '" . TOOL_CALENDAR_EVENT . "','calendar/agenda.php','agenda.gif','" . self::string2binary(
  461. api_get_setting('course_create_active_tools', 'agenda')
  462. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  463. );
  464. Database::query(
  465. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  466. VALUES ($course_id, 3, '" . TOOL_DOCUMENT . "','document/document.php','folder_document.gif','" . self::string2binary(
  467. api_get_setting('course_create_active_tools', 'documents')
  468. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  469. );
  470. Database::query(
  471. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  472. VALUES ($course_id, 4, '" . TOOL_LEARNPATH . "','newscorm/lp_controller.php','scorms.gif','" . self::string2binary(
  473. api_get_setting('course_create_active_tools', 'learning_path')
  474. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  475. );
  476. Database::query(
  477. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  478. VALUES ($course_id, 5, '" . TOOL_LINK . "','link/link.php','links.gif','" . self::string2binary(
  479. api_get_setting('course_create_active_tools', 'links')
  480. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  481. );
  482. Database::query(
  483. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  484. VALUES ($course_id, 6, '" . TOOL_QUIZ . "','exercice/exercice.php','quiz.gif','" . self::string2binary(
  485. api_get_setting('course_create_active_tools', 'quiz')
  486. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  487. );
  488. Database::query(
  489. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  490. VALUES ($course_id, 7, '" . TOOL_ANNOUNCEMENT . "','announcements/announcements.php','valves.gif','" . self::string2binary(
  491. api_get_setting('course_create_active_tools', 'announcements')
  492. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  493. );
  494. Database::query(
  495. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  496. VALUES ($course_id, 8, '" . TOOL_FORUM . "','forum/index.php','forum.gif','" . self::string2binary(
  497. api_get_setting('course_create_active_tools', 'forums')
  498. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  499. );
  500. Database::query(
  501. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  502. VALUES ($course_id, 9, '" . TOOL_DROPBOX . "','dropbox/index.php','dropbox.gif','" . self::string2binary(
  503. api_get_setting('course_create_active_tools', 'dropbox')
  504. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  505. );
  506. Database::query(
  507. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  508. VALUES ($course_id, 10, '" . TOOL_USER . "','user/user.php','members.gif','" . self::string2binary(
  509. api_get_setting('course_create_active_tools', 'users')
  510. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  511. );
  512. Database::query(
  513. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  514. VALUES ($course_id, 11, '" . TOOL_GROUP . "','group/group.php','group.gif','" . self::string2binary(
  515. api_get_setting('course_create_active_tools', 'groups')
  516. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  517. );
  518. Database::query(
  519. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  520. VALUES ($course_id, 12, '" . TOOL_CHAT . "','chat/chat.php','chat.gif','" . self::string2binary(
  521. api_get_setting('course_create_active_tools', 'chat')
  522. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  523. );
  524. Database::query(
  525. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  526. VALUES ($course_id, 13, '" . TOOL_STUDENTPUBLICATION . "','work/work.php','works.gif','" . self::string2binary(
  527. api_get_setting(
  528. 'course_create_active_tools',
  529. 'student_publications'
  530. )
  531. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  532. );
  533. Database::query(
  534. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  535. VALUES ($course_id, 14, '" . TOOL_SURVEY . "','survey/survey_list.php','survey.gif','" . self::string2binary(
  536. api_get_setting('course_create_active_tools', 'survey')
  537. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  538. );
  539. Database::query(
  540. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  541. VALUES ($course_id, 15, '" . TOOL_WIKI . "','wiki/index.php','wiki.gif','" . self::string2binary(
  542. api_get_setting('course_create_active_tools', 'wiki')
  543. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  544. );
  545. Database::query(
  546. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  547. VALUES ($course_id, 16, '" . TOOL_GRADEBOOK . "','gradebook/index.php','gradebook.gif','" . self::string2binary(
  548. api_get_setting('course_create_active_tools', 'gradebook')
  549. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  550. );
  551. Database::query(
  552. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  553. VALUES ($course_id, 17, '" . TOOL_GLOSSARY . "','glossary/index.php','glossary.gif','" . self::string2binary(
  554. api_get_setting('course_create_active_tools', 'glossary')
  555. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  556. );
  557. Database::query(
  558. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  559. VALUES ($course_id, 18, '" . TOOL_NOTEBOOK . "','notebook/index.php','notebook.gif','" . self::string2binary(
  560. api_get_setting('course_create_active_tools', 'notebook')
  561. ) . "','0','squaregrey.gif','NO','_self','interaction','0')"
  562. );
  563. Database::query(
  564. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  565. VALUES ($course_id, 19, '" . TOOL_ATTENDANCE . "','attendance/index.php','attendance.gif','" . self::string2binary(
  566. api_get_setting('course_create_active_tools', 'attendances')
  567. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  568. );
  569. Database::query(
  570. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  571. VALUES ($course_id, 20, '" . TOOL_COURSE_PROGRESS . "','course_progress/index.php','course_progress.gif','" . self::string2binary(
  572. api_get_setting('course_create_active_tools', 'course_progress')
  573. ) . "','0','squaregrey.gif','NO','_self','authoring','0')"
  574. );
  575. if (api_get_setting('service_visio', 'active') == 'true') {
  576. $mycheck = api_get_setting('service_visio', 'visio_host');
  577. if (!empty($mycheck)) {
  578. Database::query(
  579. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  580. VALUES ($course_id, 21, '" . TOOL_VISIO_CONFERENCE . "','conference/index.php?type=conference','visio_meeting.gif','1','0','squaregrey.gif','NO','_self','interaction','0')"
  581. );
  582. Database::query(
  583. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  584. VALUES ($course_id, 22, '" . TOOL_VISIO_CLASSROOM . "','conference/index.php?type=classroom','visio.gif','1','0','squaregrey.gif','NO','_self','authoring','0')"
  585. );
  586. }
  587. }
  588. if (api_get_setting('search_enabled') == 'true') {
  589. Database::query(
  590. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  591. VALUES ($course_id, 23, '" . TOOL_SEARCH . "','search/','info.gif','" . self::string2binary(
  592. api_get_setting(
  593. 'course_create_active_tools',
  594. 'enable_search'
  595. )
  596. ) . "','0','search.gif','NO','_self','authoring','0')"
  597. );
  598. }
  599. $sql = "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  600. VALUES ($course_id, 24,'" . TOOL_BLOGS . "','blog/blog_admin.php','blog_admin.gif','" . self::string2binary(
  601. api_get_setting('course_create_active_tools', 'blogs')
  602. ) . "','1','squaregrey.gif','NO','_self','admin','0')";
  603. Database::query($sql);
  604. /* Course homepage tools for course admin only */
  605. Database::query(
  606. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  607. VALUES ($course_id, 25, '" . TOOL_TRACKING . "','tracking/courseLog.php','statistics.gif','$visible_for_course_admin','1','', 'NO','_self','admin','0')"
  608. );
  609. Database::query(
  610. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  611. VALUES ($course_id, 26, '" . TOOL_COURSE_SETTING . "','course_info/infocours.php','reference.gif','$visible_for_course_admin','1','', 'NO','_self','admin','0')"
  612. );
  613. Database::query(
  614. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  615. VALUES ($course_id, 27, '" . TOOL_COURSE_MAINTENANCE . "','course_info/maintenance.php','backup.gif','$visible_for_course_admin','1','','NO','_self', 'admin','0')"
  616. );
  617. $defaultEmailExerciseAlert = 1;
  618. $alert = api_get_setting('email_alert_manager_on_new_quiz');
  619. if ($alert === 'true') {
  620. $defaultEmailExerciseAlert = 1;
  621. } else {
  622. $defaultEmailExerciseAlert = 0;
  623. }
  624. /* course_setting table (courseinfo tool) */
  625. $settings = [
  626. 'email_alert_manager_on_new_doc' => ['default' => 0, 'category' => 'work'],
  627. 'email_alert_on_new_doc_dropbox' => ['default' => 0, 'category' => 'dropbox'],
  628. 'allow_user_edit_agenda' => ['default' => 0, 'category' => 'agenda'],
  629. 'allow_user_edit_announcement' => ['default' => 0, 'category' => 'announcement'],
  630. 'email_alert_manager_on_new_quiz' => ['default' => $defaultEmailExerciseAlert, 'category' => 'quiz'],
  631. 'allow_user_image_forum' => ['default' => 1, 'category' => 'forum'],
  632. 'course_theme' => ['default' => '', 'category' => 'theme'],
  633. 'allow_learning_path_theme' => ['default' => 1, 'category' => 'theme'],
  634. 'allow_open_chat_window' => ['default' => 1, 'category' => 'chat'],
  635. 'email_alert_to_teacher_on_new_user_in_course' => ['default' => 0, 'category' =>'registration'],
  636. 'allow_user_view_user_list' => ['default' =>1, 'category' =>'user'],
  637. 'display_info_advance_inside_homecourse' => ['default' => 1, 'category' =>'thematic_advance'],
  638. 'email_alert_students_on_new_homework' => ['default' => 0, 'category' =>'work'],
  639. 'enable_lp_auto_launch' => ['default' => 0, 'category' =>'learning_path'],
  640. 'pdf_export_watermark_text' => ['default' =>'', 'category' =>'learning_path'],
  641. 'allow_public_certificates' => ['default' => '', 'category' =>'certificates'],
  642. 'documents_default_visibility' => ['default' =>'visible', 'category' =>'document']
  643. ];
  644. $counter = 1;
  645. foreach ($settings as $variable => $setting) {
  646. Database::query(
  647. "INSERT INTO $TABLESETTING (id, c_id, variable, value, category)
  648. VALUES ($counter, $course_id, '".$variable."', '".$setting['default']."', '".$setting['category']."')"
  649. );
  650. $counter++;
  651. }
  652. /* Course homepage tools for platform admin only */
  653. /* Group tool */
  654. Database::query(
  655. "INSERT INTO $TABLEGROUPCATEGORIES (c_id, id, title , description, max_student, self_reg_allowed, self_unreg_allowed, groups_per_user, display_order)
  656. VALUES ($course_id, '2', '" . self::lang2db(get_lang('DefaultGroupCategory')) . "', '', '8', '0', '0', '0', '0');"
  657. );
  658. /* Example Material */
  659. global $language_interface;
  660. $language_interface = !empty($language_interface) ? $language_interface : api_get_setting(
  661. 'platformLanguage'
  662. );
  663. // Example material should be in the same language as the course is.
  664. $language_interface_original = $language_interface;
  665. $language_interface = $language;
  666. $now = api_get_utc_datetime();
  667. $files = [
  668. ['path' => '/shared_folder', 'title' => get_lang('UserFolders'), 'filetype' => 'folder', 'size' => 0],
  669. ['path' => '/chat_files', 'title' => get_lang('ChatFiles'), 'filetype' => 'folder', 'size' => 0],
  670. ];
  671. $counter = 1;
  672. foreach ($files as $file) {
  673. self::insertDocument($course_id, $counter, $file);
  674. $counter++;
  675. }
  676. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  677. $perm = api_get_permissions_for_new_directories();
  678. $perm_file = api_get_permissions_for_new_files();
  679. $chat_path = $sys_course_path . $course_repository . '/document/chat_files';
  680. if (!is_dir($chat_path)) {
  681. @mkdir($chat_path, api_get_permissions_for_new_directories());
  682. }
  683. /* Documents */
  684. if ($fill_with_exemplary_content) {
  685. $files = [
  686. ['path' => '/images', 'title' => get_lang('Images'), 'filetype' => 'folder', 'size' => 0],
  687. ['path' => '/images/gallery', 'title' => get_lang('DefaultCourseImages'), 'filetype' => 'folder', 'size' => 0],
  688. ['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0],
  689. ['path' => '/flash', 'title' => get_lang('Flash'), 'filetype' => 'folder', 'size' => 0],
  690. ['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0],
  691. ['path' => '/certificates', 'title' => get_lang('Certificates'), 'filetype' => 'folder', 'size' => 0]
  692. ];
  693. foreach ($files as $file) {
  694. self::insertDocument($course_id, $counter, $file);
  695. $counter++;
  696. }
  697. // FILL THE COURSE DOCUMENT WITH DEFAULT COURSE PICTURES
  698. $folders_to_copy_from_default_course = array(
  699. 'images',
  700. 'audio',
  701. 'flash',
  702. 'video',
  703. 'certificates',
  704. );
  705. $default_course_path = api_get_path(SYS_CODE_PATH) . 'default_course_document/';
  706. $default_document_array = array();
  707. foreach ($folders_to_copy_from_default_course as $folder) {
  708. $default_course_folder_path = $default_course_path . $folder . '/';
  709. $files = self::browse_folders(
  710. $default_course_folder_path,
  711. array(),
  712. $folder
  713. );
  714. $sorted_array = self::sort_pictures($files, 'dir');
  715. $sorted_array = array_merge(
  716. $sorted_array,
  717. self::sort_pictures($files, 'file')
  718. );
  719. $default_document_array[$folder] = $sorted_array;
  720. }
  721. //echo '<pre>'; print_r($default_document_array);exit;
  722. //Light protection (adding index.html in every document folder)
  723. $htmlpage = "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <title>Not authorized</title>\n </head>\n <body>\n </body>\n</html>";
  724. $example_cert_id = 0;
  725. if (is_array($default_document_array) && count(
  726. $default_document_array
  727. ) > 0
  728. ) {
  729. foreach ($default_document_array as $media_type => $array_media) {
  730. $path_documents = "/$media_type/";
  731. //hack until feature #5242 is implemented
  732. if ($media_type == 'images') {
  733. $media_type = 'images/gallery';
  734. $images_folder = $sys_course_path . $course_repository . "/document/images/";
  735. if (!is_dir($images_folder)) {
  736. //Creating index.html
  737. mkdir($images_folder, $perm);
  738. $fd = fopen($images_folder . 'index.html', 'w');
  739. fwrite($fd, $htmlpage);
  740. @chmod($images_folder . 'index.html', $perm_file);
  741. }
  742. }
  743. $course_documents_folder = $sys_course_path . $course_repository . "/document/$media_type/";
  744. $default_course_path = api_get_path(SYS_CODE_PATH) . 'default_course_document' . $path_documents;
  745. if (!is_dir($course_documents_folder)) {
  746. // Creating index.html
  747. mkdir($course_documents_folder, $perm);
  748. $fd = fopen(
  749. $course_documents_folder . 'index.html',
  750. 'w'
  751. );
  752. fwrite($fd, $htmlpage);
  753. @chmod(
  754. $course_documents_folder . 'index.html',
  755. $perm_file
  756. );
  757. }
  758. if (is_array($array_media) && count($array_media) > 0) {
  759. foreach ($array_media as $key => $value) {
  760. if (isset($value['dir']) && !empty($value['dir'])) {
  761. if (!is_dir($course_documents_folder . $value['dir'])) {
  762. //Creating folder
  763. mkdir(
  764. $course_documents_folder . $value['dir'],
  765. $perm
  766. );
  767. //Creating index.html (for light protection)
  768. $index_html = $course_documents_folder . $value['dir'] . '/index.html';
  769. $fd = fopen($index_html, 'w');
  770. fwrite($fd, $htmlpage);
  771. @chmod($index_html, $perm_file);
  772. //Inserting folder in the DB
  773. $folder_path = substr(
  774. $value['dir'],
  775. 0,
  776. strlen($value['dir']) - 1
  777. );
  778. $temp = explode('/', $folder_path);
  779. $title = $temp[count($temp) - 1];
  780. //hack until feature #5242 is implemented
  781. if ($title == 'gallery') {
  782. $title = get_lang(
  783. 'DefaultCourseImages'
  784. );
  785. }
  786. if ($media_type == 'images/gallery') {
  787. $folder_path = 'gallery/' . $folder_path;
  788. }
  789. Database::query(
  790. "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
  791. VALUES ($course_id,'$path_documents" . $folder_path . "','" . $title . "','folder','0')"
  792. );
  793. $image_id = Database:: insert_id();
  794. Database::query(
  795. "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
  796. VALUES ($course_id,'document',1,'$now','$now',$image_id,'DocumentAdded',1,NULL,NULL,0)"
  797. );
  798. }
  799. }
  800. if (isset($value['file']) && !empty($value['file'])) {
  801. if (!file_exists(
  802. $course_documents_folder . $value['file']
  803. )
  804. ) {
  805. //Copying file
  806. copy(
  807. $default_course_path . $value['file'],
  808. $course_documents_folder . $value['file']
  809. );
  810. chmod(
  811. $course_documents_folder . $value['file'],
  812. $perm_file
  813. );
  814. //echo $default_course_path.$value['file']; echo ' - '; echo $course_documents_folder.$value['file']; echo '<br />';
  815. $temp = explode('/', $value['file']);
  816. $file_size = filesize(
  817. $course_documents_folder . $value['file']
  818. );
  819. //hack until feature #5242 is implemented
  820. if ($media_type == 'images/gallery') {
  821. $value["file"] = 'gallery/' . $value["file"];
  822. }
  823. //Inserting file in the DB
  824. Database::query(
  825. "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
  826. VALUES ($course_id,'$path_documents" . $value["file"] . "','" . $temp[count($temp) - 1] . "','file','$file_size')"
  827. );
  828. $image_id = Database:: insert_id();
  829. if ($image_id) {
  830. $sql = "UPDATE $TABLETOOLDOCUMENT SET id = iid WHERE iid = $image_id";
  831. Database::query($sql);
  832. if ($path_documents . $value['file'] == '/certificates/default.html') {
  833. $example_cert_id = $image_id;
  834. }
  835. Database::query(
  836. "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
  837. VALUES ($course_id,'document',1,'$now','$now',$image_id,'DocumentAdded',1,NULL,NULL,1)"
  838. );
  839. $docId = Database:: insert_id();
  840. if ($docId) {
  841. $sql = "UPDATE $TABLEITEMPROPERTY SET id = iid WHERE iid = $docId";
  842. Database::query($sql);
  843. }
  844. }
  845. }
  846. }
  847. }
  848. }
  849. }
  850. }
  851. $agenda = new Agenda();
  852. $agenda->setType('course');
  853. $agenda->set_course($courseInfo);
  854. $agenda->addEvent(
  855. $now,
  856. $now,
  857. 0,
  858. get_lang('AgendaCreationTitle'),
  859. get_lang('AgendaCreationContenu')
  860. );
  861. /* Links tool */
  862. $link = new Link();
  863. $link->setCourse($courseInfo);
  864. $links = [
  865. [
  866. 'c_id' => $course_id,
  867. 'url' => 'http://www.google.com',
  868. 'title' => 'Google',
  869. 'description' => get_lang('Google') ,
  870. 'category_id' => 0,
  871. 'on_homepage' => 0,
  872. 'target' => '_self',
  873. 'session_id' => 0
  874. ],
  875. [
  876. 'c_id' => $course_id,
  877. 'url' => 'http://www.wikipedia.org',
  878. 'title' => 'Wikipedia',
  879. 'description' => get_lang('Wikipedia') ,
  880. 'category_id' => 0,
  881. 'on_homepage' => 0,
  882. 'target' => '_self',
  883. 'session_id' => 0
  884. ]
  885. ];
  886. foreach($links as $params) {
  887. $link->save($params);
  888. }
  889. /* Announcement tool */
  890. AnnouncementManager::add_announcement(
  891. get_lang('AnnouncementExampleTitle'),
  892. get_lang('AnnouncementEx'),
  893. ['everyone' => 'everyone'],
  894. null,
  895. null,
  896. $now
  897. );
  898. $manager = Database::getManager();
  899. /* Introduction text */
  900. $intro_text = '<p style="text-align: center;">
  901. <img src="' . api_get_path(REL_CODE_PATH) . 'img/mascot.png" alt="Mr. Chamilo" title="Mr. Chamilo" />
  902. <h2>' . self::lang2db(get_lang('IntroductionText')) . '</h2>
  903. </p>';
  904. $toolIntro = new Chamilo\CourseBundle\Entity\CToolIntro();
  905. $toolIntro
  906. ->setCId($course_id)
  907. ->setId(TOOL_COURSE_HOMEPAGE)
  908. ->setSessionId(0)
  909. ->setIntroText($intro_text);
  910. $manager->persist($toolIntro);
  911. $toolIntro = new Chamilo\CourseBundle\Entity\CToolIntro();
  912. $toolIntro
  913. ->setCId($course_id)
  914. ->setId(TOOL_STUDENTPUBLICATION)
  915. ->setSessionId(0)
  916. ->setIntroText(get_lang('IntroductionTwo'));
  917. $manager->persist($toolIntro);
  918. $toolIntro = new Chamilo\CourseBundle\Entity\CToolIntro();
  919. $toolIntro
  920. ->setCId($course_id)
  921. ->setId(TOOL_WIKI)
  922. ->setSessionId(0)
  923. ->setIntroText(get_lang('IntroductionWiki'));
  924. $manager->persist($toolIntro);
  925. $manager->flush();
  926. /* Exercise tool */
  927. $exercise = new Exercise($course_id);
  928. $exercise->exercise = get_lang('ExerciceEx');
  929. $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0">
  930. <tr>
  931. <td width="110" valign="top" align="left">
  932. <img src="' . api_get_path(WEB_CODE_PATH) . 'default_course_document/images/mr_dokeos/thinking.jpg">
  933. </td>
  934. <td valign="top" align="left">' . get_lang('Antique') . '</td></tr>
  935. </table>';
  936. $exercise->type = 1;
  937. $exercise->setRandom(0);
  938. $exercise->active = 1;
  939. $exercise->results_disabled = 0;
  940. $exercise->description = $html;
  941. $exercise->save();
  942. $exercise_id = $exercise->id;
  943. $question = new MultipleAnswer();
  944. $question->question = get_lang('SocraticIrony');
  945. $question->description = get_lang('ManyAnswers');
  946. $question->weighting = 10;
  947. $question->position = 1;
  948. $question->course = $courseInfo;
  949. $question->save($exercise_id);
  950. $questionId = $question->id;
  951. $answer = new Answer($questionId, $courseInfo['real_id']);
  952. $answer->createAnswer(get_lang('Ridiculise'), 0, get_lang('NoPsychology'), -5, 1);
  953. $answer->createAnswer(get_lang('AdmitError'), 0, get_lang('NoSeduction'), -5, 2);
  954. $answer->createAnswer(get_lang('Force'), 1, get_lang('Indeed'), 5, 3);
  955. $answer->createAnswer(get_lang('Contradiction'), 1, get_lang('NotFalse'), 5, 4);
  956. $answer->save();
  957. /* Forum tool */
  958. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  959. $params = [
  960. 'forum_category_title' => get_lang('ExampleForumCategory'),
  961. 'forum_category_comment' => ''
  962. ];
  963. $forumCategoryId = store_forumcategory($params, $courseInfo, false);
  964. $params = [
  965. 'forum_category' => $forumCategoryId,
  966. 'forum_title' => get_lang('ExampleForum'),
  967. 'forum_comment' => '',
  968. 'default_view_type_group' => ['default_view_type' => 'flat'],
  969. ];
  970. $forumId = store_forum($params, $courseInfo, true);
  971. $forumInfo = get_forum_information($forumId, $courseInfo['real_id']);
  972. $params = [
  973. 'post_title' => get_lang('ExampleThread'),
  974. 'forum_id' => $forumId,
  975. 'post_text' => get_lang('ExampleThreadContent'),
  976. 'calification_notebook_title' => '',
  977. 'numeric_calification' => '',
  978. 'weight_calification' => '',
  979. 'forum_category' => $forumCategoryId,
  980. 'thread_peer_qualify' => 0,
  981. ];
  982. store_thread($forumInfo, $params, $courseInfo, false);
  983. /* Gradebook tool */
  984. $course_code = $courseInfo['code'];
  985. // father gradebook
  986. Database::query(
  987. "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id)
  988. VALUES ('$course_code','',1,'$course_code',0,100,0,75,NULL,$example_cert_id)"
  989. );
  990. $gbid = Database:: insert_id();
  991. Database::query(
  992. "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id)
  993. VALUES ('$course_code','',1,'$course_code',$gbid,100,1,75,NULL,$example_cert_id)"
  994. );
  995. $gbid = Database:: insert_id();
  996. Database::query(
  997. "INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, course_code, category_id, created_at, weight, visible, locked)
  998. VALUES (1,$exercise_id,1,'$course_code',$gbid,'$now',100,1,0)"
  999. );
  1000. }
  1001. //Installing plugins in course
  1002. $app_plugin = new AppPlugin();
  1003. $app_plugin->install_course_plugins($course_id);
  1004. $language_interface = $language_interface_original;
  1005. return true;
  1006. }
  1007. /**
  1008. * @param int $course_id
  1009. * @param int $counter
  1010. * @param array $file
  1011. */
  1012. public static function insertDocument($course_id, $counter, $file)
  1013. {
  1014. $tableItem = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1015. $tableDocument = Database::get_course_table(TABLE_DOCUMENT);
  1016. $now = api_get_utc_datetime();
  1017. $sql = "INSERT INTO $tableDocument (id, c_id, path,title,filetype,size)
  1018. VALUES ($counter, $course_id, '".$file['path']."', '".$file['title']."', '".$file['filetype']."', '".$file['size']."')";
  1019. Database::query($sql);
  1020. $docId = Database:: insert_id();
  1021. if ($docId) {
  1022. $sql = "UPDATE $tableDocument SET id = iid WHERE iid = $docId";
  1023. Database::query($sql);
  1024. Database::query(
  1025. "INSERT INTO $tableItem (id, c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
  1026. VALUES ($counter, $course_id,'document',1,'$now', '$now', $docId, 'DocumentAdded', 1, NULL, NULL, 0)"
  1027. );
  1028. $id = Database:: insert_id();
  1029. if ($id) {
  1030. $sql = "UPDATE $tableItem SET id = iid WHERE iid = $id";
  1031. Database::query($sql);
  1032. }
  1033. }
  1034. }
  1035. /**
  1036. * string2binary converts the string "true" or "false" to the boolean true false (0 or 1)
  1037. * This is used for the Chamilo Config Settings as these store true or false as string
  1038. * and the api_get_setting('course_create_active_tools') should be 0 or 1 (used for
  1039. * the visibility of the tool)
  1040. * @param string $variable
  1041. * @author Patrick Cool, patrick.cool@ugent.be
  1042. * @assert ('true') === true
  1043. * @assert ('false') === false
  1044. */
  1045. public static function string2binary($variable)
  1046. {
  1047. if ($variable == 'true') {
  1048. return true;
  1049. }
  1050. if ($variable == 'false') {
  1051. return false;
  1052. }
  1053. }
  1054. /**
  1055. * Function register_course to create a record in the course table of the main database
  1056. * @param array Course details (see code for details)
  1057. * @return int Created course ID
  1058. * @todo use an array called $params instead of lots of params
  1059. * @assert (null) === false
  1060. */
  1061. public static function register_course($params)
  1062. {
  1063. global $error_msg, $firstExpirationDelay;
  1064. $title = $params['title'];
  1065. $code = $params['code'];
  1066. $visual_code = $params['visual_code'];
  1067. $directory = $params['directory'];
  1068. $tutor_name = isset($params['tutor_name']) ? $params['tutor_name'] : null;
  1069. //$description = $params['description'];
  1070. $category_code = isset($params['course_category']) ? $params['course_category'] : '';
  1071. $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : api_get_setting(
  1072. 'platformLanguage'
  1073. );
  1074. $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']);
  1075. $department_name = isset($params['department_name']) ?
  1076. $params['department_name'] : null;
  1077. $department_url = isset($params['department_url']) ?
  1078. $params['department_url'] : null;
  1079. $disk_quota = isset($params['disk_quota']) ?
  1080. $params['disk_quota'] : null;
  1081. if (!isset($params['visibility'])) {
  1082. $default_course_visibility = api_get_setting(
  1083. 'courses_default_creation_visibility'
  1084. );
  1085. if (isset($default_course_visibility)) {
  1086. $visibility = $default_course_visibility;
  1087. } else {
  1088. $visibility = COURSE_VISIBILITY_OPEN_PLATFORM;
  1089. }
  1090. } else {
  1091. $visibility = $params['visibility'];
  1092. }
  1093. $subscribe = isset($params['subscribe']) ? intval(
  1094. $params['subscribe']
  1095. ) : ($visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0);
  1096. $unsubscribe = isset($params['unsubscribe']) ? intval(
  1097. $params['unsubscribe']
  1098. ) : 0;
  1099. $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null;
  1100. $teachers = isset($params['teachers']) ? $params['teachers'] : null;
  1101. $status = isset($params['status']) ? $params['status'] : null;
  1102. $TABLECOURSE = Database:: get_main_table(TABLE_MAIN_COURSE);
  1103. $TABLECOURSUSER = Database:: get_main_table(TABLE_MAIN_COURSE_USER);
  1104. $ok_to_register_course = true;
  1105. // Check whether all the needed parameters are present.
  1106. if (empty($code)) {
  1107. $error_msg[] = 'courseSysCode is missing';
  1108. $ok_to_register_course = false;
  1109. }
  1110. if (empty($visual_code)) {
  1111. $error_msg[] = 'courseScreenCode is missing';
  1112. $ok_to_register_course = false;
  1113. }
  1114. if (empty($directory)) {
  1115. $error_msg[] = 'courseRepository is missing';
  1116. $ok_to_register_course = false;
  1117. }
  1118. if (empty($title)) {
  1119. $error_msg[] = 'title is missing';
  1120. $ok_to_register_course = false;
  1121. }
  1122. if (empty($expiration_date)) {
  1123. $expiration_date = api_get_utc_datetime(
  1124. time() + $firstExpirationDelay
  1125. );
  1126. } else {
  1127. $expiration_date = api_get_utc_datetime($expiration_date);
  1128. }
  1129. if ($visibility < 0 || $visibility > 4) {
  1130. $error_msg[] = 'visibility is invalid';
  1131. $ok_to_register_course = false;
  1132. }
  1133. if (empty($disk_quota)) {
  1134. $disk_quota = api_get_setting('default_document_quotum');
  1135. }
  1136. $time = api_get_utc_datetime();
  1137. if (stripos($department_url, 'http://') === false && stripos(
  1138. $department_url,
  1139. 'https://'
  1140. ) === false
  1141. ) {
  1142. $department_url = 'http://' . $department_url;
  1143. }
  1144. //just in case
  1145. if ($department_url == 'http://') {
  1146. $department_url = '';
  1147. }
  1148. $course_id = 0;
  1149. if ($ok_to_register_course) {
  1150. // Here we must add 2 fields.
  1151. $sql = "INSERT INTO " . $TABLECOURSE . " SET
  1152. code = '".Database:: escape_string($code)."',
  1153. directory = '".Database:: escape_string($directory)."',
  1154. course_language = '".Database:: escape_string($course_language)."',
  1155. title = '".Database:: escape_string($title)."',
  1156. description = '".self::lang2db(get_lang('CourseDescription'))."',
  1157. category_code = '".Database:: escape_string($category_code)."',
  1158. visibility = '" . $visibility . "',
  1159. show_score = '1',
  1160. disk_quota = '" . intval($disk_quota) . "',
  1161. creation_date = '$time',
  1162. expiration_date = '" . $expiration_date . "',
  1163. last_edit = '$time',
  1164. last_visit = NULL,
  1165. tutor_name = '" . Database:: escape_string($tutor_name) . "',
  1166. department_name = '" . Database:: escape_string($department_name) . "',
  1167. department_url = '" . Database:: escape_string($department_url) . "',
  1168. subscribe = '" . intval($subscribe) . "',
  1169. unsubscribe = '" . intval($unsubscribe) . "',
  1170. visual_code = '" . Database:: escape_string($visual_code) . "'";
  1171. Database::query($sql);
  1172. $course_id = Database::insert_id();
  1173. if ($course_id) {
  1174. $sort = api_max_sort_value('0', api_get_user_id());
  1175. // Default true
  1176. $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true;
  1177. if ($addTeacher) {
  1178. $i_course_sort = CourseManager:: userCourseSort(
  1179. $user_id,
  1180. $code
  1181. );
  1182. if (!empty($user_id)) {
  1183. $sql = "INSERT INTO " . $TABLECOURSUSER . " SET
  1184. c_id = '" . $course_id . "',
  1185. user_id = '" . intval($user_id) . "',
  1186. status = '1',
  1187. is_tutor = '0',
  1188. sort = '" . ($i_course_sort) . "',
  1189. user_course_cat = '0'";
  1190. Database::query($sql);
  1191. }
  1192. }
  1193. if (!empty($teachers)) {
  1194. if (!is_array($teachers)) {
  1195. $teachers = array($teachers);
  1196. }
  1197. foreach ($teachers as $key) {
  1198. //just in case
  1199. if ($key == $user_id) {
  1200. continue;
  1201. }
  1202. if (empty($key)) {
  1203. continue;
  1204. }
  1205. $sql = "INSERT INTO " . $TABLECOURSUSER . " SET
  1206. c_id = '" . Database::escape_string($course_id) . "',
  1207. user_id = '" . Database::escape_string($key) . "',
  1208. status = '1',
  1209. is_tutor = '0',
  1210. sort = '" . ($sort + 1) . "',
  1211. user_course_cat = '0'";
  1212. Database::query($sql);
  1213. }
  1214. }
  1215. // Adding the course to an URL.
  1216. if (api_is_multiple_url_enabled()) {
  1217. $url_id = 1;
  1218. if (api_get_current_access_url_id() != -1) {
  1219. $url_id = api_get_current_access_url_id();
  1220. }
  1221. UrlManager::add_course_to_url($course_id, $url_id);
  1222. } else {
  1223. UrlManager::add_course_to_url($course_id, 1);
  1224. }
  1225. // Add event to the system log.
  1226. $user_id = api_get_user_id();
  1227. Event::addEvent(
  1228. LOG_COURSE_CREATE,
  1229. LOG_COURSE_CODE,
  1230. $code,
  1231. api_get_utc_datetime(),
  1232. $user_id,
  1233. $course_id
  1234. );
  1235. $send_mail_to_admin = api_get_setting(
  1236. 'send_email_to_admin_when_create_course'
  1237. );
  1238. // @todo Improve code to send to all current portal administrators.
  1239. if ($send_mail_to_admin == 'true') {
  1240. $siteName = api_get_setting('siteName');
  1241. $recipient_email = api_get_setting('emailAdministrator');
  1242. $recipient_name = api_get_person_name(
  1243. api_get_setting('administratorName'),
  1244. api_get_setting('administratorSurname')
  1245. );
  1246. $iname = api_get_setting('Institution');
  1247. $subject = get_lang(
  1248. 'NewCourseCreatedIn'
  1249. ) . ' ' . $siteName . ' - ' . $iname;
  1250. $message = get_lang(
  1251. 'Dear'
  1252. ) . ' ' . $recipient_name . ",\n\n" . get_lang(
  1253. 'MessageOfNewCourseToAdmin'
  1254. ) . ' ' . $siteName . ' - ' . $iname . "\n";
  1255. $message .= get_lang('CourseName') . ' ' . $title . "\n";
  1256. $message .= get_lang(
  1257. 'Category'
  1258. ) . ' ' . $category_code . "\n";
  1259. $message .= get_lang('Tutor') . ' ' . $tutor_name . "\n";
  1260. $message .= get_lang('Language') . ' ' . $course_language;
  1261. $userInfo = api_get_user_info($user_id);
  1262. $additionalParameters = array(
  1263. 'smsType' => SmsPlugin::NEW_COURSE_BEEN_CREATED,
  1264. 'userId' => $user_id,
  1265. 'courseName' => $title,
  1266. 'creatorUsername' => $userInfo['username']
  1267. );
  1268. api_mail_html(
  1269. $recipient_name,
  1270. $recipient_email,
  1271. $subject,
  1272. $message,
  1273. $siteName,
  1274. $recipient_email,
  1275. null,
  1276. null,
  1277. null,
  1278. $additionalParameters
  1279. );
  1280. }
  1281. }
  1282. }
  1283. return $course_id;
  1284. }
  1285. /**
  1286. * Extract properties of the files from a ZIP package, write them to disk and
  1287. * return them as an array.
  1288. * @todo this function seems not to be used
  1289. * @param string Absolute path to the ZIP file
  1290. * @param bool Whether the ZIP file is compressed (not implemented). Defaults to TRUE.
  1291. * @return array List of files properties from the ZIP package
  1292. * @deprecated seems not to be used
  1293. * @assert (null) === false
  1294. */
  1295. public static function readPropertiesInArchive($archive, $is_compressed = true)
  1296. {
  1297. include api_get_path(LIBRARY_PATH) . 'pclzip/pclzip.lib.php';
  1298. debug::printVar(dirname($archive), 'Zip : ');
  1299. $uid = api_get_user_id();
  1300. /*
  1301. string tempnam (string dir, string prefix)
  1302. tempnam() creates a unique temporary file in the dir directory. If the
  1303. directory doesn't existm tempnam() will generate a filename in the system's
  1304. temporary directory.
  1305. Before PHP 4.0.6, the behaviour of tempnam() depended of the underlying OS.
  1306. Under Windows, the "TMP" environment variable replaces the dir parameter;
  1307. under Linux, the "TMPDIR" environment variable has priority, while for the
  1308. OSes based on system V R4, the dir parameter will always be used if the
  1309. directory which it represents exists. Consult your documentation for more
  1310. details.
  1311. tempnam() returns the temporary filename, or the string NULL upon failure.
  1312. */
  1313. $zip_file = new PclZip($archive);
  1314. $tmp_dir_name = dirname($archive) . '/tmp' . $uid . uniqid($uid);
  1315. if (mkdir(
  1316. $tmp_dir_name,
  1317. api_get_permissions_for_new_directories(),
  1318. true
  1319. )) {
  1320. $unzipping_state = $zip_file->extract($tmp_dir_name);
  1321. } else {
  1322. die ('mkdir failed');
  1323. }
  1324. $path_to_archive_ini = dirname($tmp_dir_name) . '/archive.ini';
  1325. //echo $path_to_archive_ini;
  1326. $course_properties = parse_ini_file($path_to_archive_ini);
  1327. rmdir($tmp_dir_name);
  1328. return $course_properties;
  1329. }
  1330. /**
  1331. * Generate a new id for c_tool table
  1332. * @param int $courseId The course id
  1333. * @return int the new id
  1334. */
  1335. public static function generateToolId($courseId)
  1336. {
  1337. $newIdResultData = Database::select(
  1338. 'id + 1 AS new_id',
  1339. Database::get_course_table(TABLE_TOOL_LIST),
  1340. [
  1341. 'where' => ['c_id = ?' => intval($courseId)],
  1342. 'order' => 'id',
  1343. 'limit' => 1
  1344. ],
  1345. 'first'
  1346. );
  1347. if ($newIdResultData === false) {
  1348. return 1;
  1349. }
  1350. return $newIdResultData['new_id'] > 0 ? $newIdResultData['new_id'] : 1;
  1351. }
  1352. }