add_course.lib.inc.php 66 KB

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