add_course.lib.inc.php 66 KB

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