add_course.lib.inc.php 62 KB

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