link.lib.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CLink;
  4. /**
  5. * Function library for the links tool.
  6. *
  7. * This is a complete remake of the original link tool.
  8. * New features:
  9. * - Organize links into categories;
  10. * - favorites/bookmarks interface;
  11. * - move links up/down within a category;
  12. * - move categories up/down;
  13. * - expand/collapse all categories;
  14. * - add link to 'root' category => category-less link is always visible.
  15. *
  16. * @author Patrick Cool, complete remake (December 2003 - January 2004)
  17. * @author René Haentjens, CSV file import (October 2004)
  18. * @package chamilo.link
  19. */
  20. class Link extends Model
  21. {
  22. public $table;
  23. public $is_course_model = true;
  24. public $columns = array(
  25. 'id',
  26. 'c_id',
  27. 'url',
  28. 'title',
  29. 'description',
  30. 'category_id',
  31. 'display_order',
  32. 'on_homepage',
  33. 'target',
  34. 'session_id'
  35. );
  36. public $required = array('url', 'title');
  37. private $course;
  38. /**
  39. *
  40. */
  41. public function __construct()
  42. {
  43. $this->table = Database::get_course_table(TABLE_LINK);
  44. }
  45. /**
  46. * @param array $course
  47. */
  48. public function setCourse($course)
  49. {
  50. $this->course = $course;
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function getCourse()
  56. {
  57. return !empty($this->course) ? $this->course : api_get_course_info();
  58. }
  59. /**
  60. * Organize the saving of a link, using the parent's save method and
  61. * updating the item_property table
  62. * @param array $params
  63. * @param boolean $show_query Whether to show the query in logs when
  64. * calling parent's save method
  65. *
  66. * @return bool True if link could be saved, false otherwise
  67. */
  68. public function save($params, $show_query = null)
  69. {
  70. $course_info = $this->getCourse();
  71. $courseId = $course_info['real_id'];
  72. $params['session_id'] = api_get_session_id();
  73. $params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
  74. $sql = "SELECT MAX(display_order)
  75. FROM ".$this->table."
  76. WHERE
  77. c_id = $courseId AND
  78. category_id = '".intval($params['category_id'])."'";
  79. $result = Database:: query($sql);
  80. list ($orderMax) = Database:: fetch_row($result);
  81. $order = $orderMax + 1;
  82. $params['display_order'] = $order;
  83. $id = parent::save($params, $show_query);
  84. if (!empty($id)) {
  85. // iid
  86. $sql = "UPDATE ".$this->table." SET id = iid WHERE iid = $id";
  87. Database:: query($sql);
  88. api_item_property_update(
  89. $course_info,
  90. TOOL_LINK,
  91. $id,
  92. 'LinkAdded',
  93. api_get_user_id()
  94. );
  95. api_set_default_visibility($id, TOOL_LINK);
  96. }
  97. return $id;
  98. }
  99. /**
  100. * Update a link in the database
  101. * @param int $linkId The ID of the link to update
  102. * @param string $linkUrl The new URL to be saved
  103. * @param int $courseId
  104. * @param int $sessionId
  105. * @return bool
  106. */
  107. public function updateLink(
  108. $linkId,
  109. $linkUrl,
  110. $courseId = null,
  111. $sessionId = null
  112. ) {
  113. $tblLink = Database::get_course_table(TABLE_LINK);
  114. $linkUrl = Database::escape_string($linkUrl);
  115. $linkId = intval($linkId);
  116. if (is_null($courseId)) {
  117. $courseId = api_get_course_int_id();
  118. }
  119. $courseId = intval($courseId);
  120. if (is_null($sessionId)) {
  121. $sessionId = api_get_session_id();
  122. }
  123. $sessionId = intval($sessionId);
  124. if ($linkUrl != '') {
  125. $sql = "UPDATE $tblLink SET
  126. url = '$linkUrl'
  127. WHERE id = $linkId AND c_id = $courseId AND session_id = $sessionId";
  128. $resLink = Database::query($sql);
  129. return $resLink;
  130. }
  131. return false;
  132. }
  133. /**
  134. * Used to add a link or a category
  135. * @param string $type , "link" or "category"
  136. * @todo replace strings by constants
  137. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  138. * @return bool True on success, false on failure
  139. */
  140. public static function addlinkcategory($type)
  141. {
  142. $ok = true;
  143. $_course = api_get_course_info();
  144. $course_id = $_course['real_id'];
  145. $session_id = api_get_session_id();
  146. if ($type == 'link') {
  147. $title = Security::remove_XSS(stripslashes($_POST['title']));
  148. $urllink = Security::remove_XSS($_POST['url']);
  149. $description = Security::remove_XSS($_POST['description']);
  150. $selectcategory = Security::remove_XSS($_POST['category_id']);
  151. if (!isset($_POST['on_homepage'])) {
  152. $onhomepage = 0;
  153. } else {
  154. $onhomepage = Security::remove_XSS($_POST['on_homepage']);
  155. }
  156. if (empty($_POST['target'])) {
  157. $target = '_self'; // Default target.
  158. } else {
  159. $target = Security::remove_XSS($_POST['target']);
  160. }
  161. $urllink = trim($urllink);
  162. $title = trim($title);
  163. $description = trim($description);
  164. // We ensure URL to be absolute.
  165. if (strpos($urllink, '://') === false) {
  166. $urllink = 'http://'.$urllink;
  167. }
  168. // If the title is empty, we use the URL as title.
  169. if ($title == '') {
  170. $title = $urllink;
  171. }
  172. // If the URL is invalid, an error occurs.
  173. if (!api_valid_url($urllink, true)) {
  174. // A check against an absolute URL
  175. Display::addFlash(Display::return_message(get_lang('GiveURL'), 'error'));
  176. return false;
  177. } else {
  178. // Looking for the largest order number for this category.
  179. $link = new Link();
  180. $params = [
  181. 'c_id' => $course_id,
  182. 'url' => $urllink,
  183. 'title' => $title,
  184. 'description' => $description ,
  185. 'category_id' => $selectcategory,
  186. 'on_homepage' => $onhomepage,
  187. 'target' => $target,
  188. 'session_id' => $session_id,
  189. ];
  190. $link_id = $link->save($params);
  191. if ((api_get_setting('search_enabled') == 'true') &&
  192. $link_id && extension_loaded('xapian')
  193. ) {
  194. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  195. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  196. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  197. $course_int_id = $_course['real_id'];
  198. $courseCode = $_course['code'];
  199. $specific_fields = get_specific_field_list();
  200. $ic_slide = new IndexableChunk();
  201. // Add all terms to db.
  202. $all_specific_terms = '';
  203. foreach ($specific_fields as $specific_field) {
  204. if (isset($_REQUEST[$specific_field['code']])) {
  205. $sterms = trim($_REQUEST[$specific_field['code']]);
  206. if (!empty($sterms)) {
  207. $all_specific_terms .= ' '.$sterms;
  208. $sterms = explode(',', $sterms);
  209. foreach ($sterms as $sterm) {
  210. $ic_slide->addTerm(
  211. trim($sterm),
  212. $specific_field['code']
  213. );
  214. add_specific_field_value(
  215. $specific_field['id'],
  216. $courseCode,
  217. TOOL_LINK,
  218. $link_id,
  219. $sterm
  220. );
  221. }
  222. }
  223. }
  224. }
  225. // Build the chunk to index.
  226. $ic_slide->addValue('title', $title);
  227. $ic_slide->addCourseId($courseCode);
  228. $ic_slide->addToolId(TOOL_LINK);
  229. $xapian_data = array(
  230. SE_COURSE_ID => $courseCode,
  231. SE_TOOL_ID => TOOL_LINK,
  232. SE_DATA => array(
  233. 'link_id' => (int) $link_id
  234. ),
  235. SE_USER => (int) api_get_user_id(),
  236. );
  237. $ic_slide->xapian_data = serialize($xapian_data);
  238. $description = $all_specific_terms.' '.$description;
  239. $ic_slide->addValue('content', $description);
  240. // Add category name if set.
  241. if (isset($selectcategory) && $selectcategory > 0) {
  242. $table_link_category = Database::get_course_table(
  243. TABLE_LINK_CATEGORY
  244. );
  245. $sql_cat = 'SELECT * FROM %s WHERE id=%d AND c_id = %d LIMIT 1';
  246. $sql_cat = sprintf(
  247. $sql_cat,
  248. $table_link_category,
  249. (int) $selectcategory,
  250. $course_int_id
  251. );
  252. $result = Database:: query($sql_cat);
  253. if (Database:: num_rows($result) == 1) {
  254. $row = Database:: fetch_array($result);
  255. $ic_slide->addValue(
  256. 'category',
  257. $row['category_title']
  258. );
  259. }
  260. }
  261. $di = new ChamiloIndexer();
  262. isset($_POST['language']) ? $lang = Database:: escape_string(
  263. $_POST['language']
  264. ) : $lang = 'english';
  265. $di->connectDb(null, null, $lang);
  266. $di->addChunk($ic_slide);
  267. // Index and return search engine document id.
  268. $did = $di->index();
  269. if ($did) {
  270. // Save it to db.
  271. $tbl_se_ref = Database::get_main_table(
  272. TABLE_MAIN_SEARCH_ENGINE_REF
  273. );
  274. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  275. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  276. $sql = sprintf(
  277. $sql,
  278. $tbl_se_ref,
  279. $course_int_id,
  280. $courseCode,
  281. TOOL_LINK,
  282. $link_id,
  283. $did
  284. );
  285. Database:: query($sql);
  286. }
  287. }
  288. Display::addFlash(Display::return_message(get_lang('LinkAdded')));
  289. }
  290. } elseif ($type == 'category') {
  291. $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
  292. $category_title = trim($_POST['category_title']);
  293. $description = trim($_POST['description']);
  294. if (empty($category_title)) {
  295. echo Display::return_message(get_lang('GiveCategoryName'), 'error');
  296. $ok = false;
  297. } else {
  298. // Looking for the largest order number for this category.
  299. $result = Database:: query(
  300. "SELECT MAX(display_order) FROM $tbl_categories
  301. WHERE c_id = $course_id "
  302. );
  303. list ($orderMax) = Database:: fetch_row($result);
  304. $order = $orderMax + 1;
  305. $order = intval($order);
  306. $session_id = api_get_session_id();
  307. $params = [
  308. 'c_id' => $course_id,
  309. 'category_title' => $category_title,
  310. 'description' => $description,
  311. 'display_order' => $order,
  312. 'session_id' => $session_id
  313. ];
  314. $linkId = Database::insert($tbl_categories, $params);
  315. if ($linkId) {
  316. // iid
  317. $sql = "UPDATE $tbl_categories SET id = iid WHERE iid = $linkId";
  318. Database:: query($sql);
  319. // add link_category visibility
  320. // course ID is taken from context in api_set_default_visibility
  321. //api_set_default_visibility($linkId, TOOL_LINK_CATEGORY);
  322. api_item_property_update(
  323. $_course,
  324. TOOL_LINK_CATEGORY,
  325. $linkId,
  326. 'LinkCategoryAdded',
  327. api_get_user_id()
  328. );
  329. api_set_default_visibility($linkId, TOOL_LINK_CATEGORY);
  330. }
  331. Display::addFlash(Display::return_message(get_lang('CategoryAdded')));
  332. }
  333. }
  334. return $ok;
  335. }
  336. /**
  337. * Used to delete a link or a category
  338. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  339. * @param int $id
  340. * @param string $type The type of item to delete
  341. * @return bool
  342. */
  343. public static function deletelinkcategory($id, $type)
  344. {
  345. $courseInfo = api_get_course_info();
  346. $tbl_link = Database::get_course_table(TABLE_LINK);
  347. $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
  348. $course_id = $courseInfo['real_id'];
  349. $id = intval($id);
  350. if (empty($id)) {
  351. return false;
  352. }
  353. $result = false;
  354. switch ($type) {
  355. case 'link':
  356. // -> Items are no longer physically deleted,
  357. // but the visibility is set to 2 (in item_property).
  358. // This will make a restore function possible for the platform administrator.
  359. $sql = "UPDATE $tbl_link SET on_homepage='0'
  360. WHERE c_id = $course_id AND id='".$id."'";
  361. Database:: query($sql);
  362. api_item_property_update(
  363. $courseInfo,
  364. TOOL_LINK,
  365. $id,
  366. 'delete',
  367. api_get_user_id()
  368. );
  369. self::delete_link_from_search_engine(api_get_course_id(), $id);
  370. Display::addFlash(Display::return_message(get_lang('LinkDeleted')));
  371. $result = true;
  372. break;
  373. case 'category':
  374. // First we delete the category itself and afterwards all the links of this category.
  375. $sql = "DELETE FROM ".$tbl_categories."
  376. WHERE c_id = $course_id AND id='".$id."'";
  377. Database:: query($sql);
  378. $sql = "DELETE FROM ".$tbl_link."
  379. WHERE c_id = $course_id AND category_id='".$id."'";
  380. Database:: query($sql);
  381. api_item_property_update(
  382. $courseInfo,
  383. TOOL_LINK_CATEGORY,
  384. $id,
  385. 'delete',
  386. api_get_user_id()
  387. );
  388. Display::addFlash(Display::return_message(get_lang('CategoryDeleted')));
  389. $result = true;
  390. break;
  391. }
  392. return $result;
  393. }
  394. /**
  395. * Removes a link from search engine database
  396. * @param string $course_id Course code
  397. * @param int $link_id Document id to delete
  398. * @return void
  399. */
  400. public static function delete_link_from_search_engine($course_id, $link_id)
  401. {
  402. // Remove from search engine if enabled.
  403. if (api_get_setting('search_enabled') === 'true') {
  404. $tbl_se_ref = Database::get_main_table(
  405. TABLE_MAIN_SEARCH_ENGINE_REF
  406. );
  407. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  408. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  409. $res = Database:: query($sql);
  410. if (Database:: num_rows($res) > 0) {
  411. $row = Database:: fetch_array($res);
  412. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  413. $di = new ChamiloIndexer();
  414. $di->remove_document((int) $row['search_did']);
  415. }
  416. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  417. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  418. Database:: query($sql);
  419. // Remove terms from db.
  420. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  421. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  422. }
  423. }
  424. /**
  425. *
  426. * Get link info
  427. * @param int link id
  428. * @param integer $id
  429. * @return array link info
  430. *
  431. **/
  432. public static function get_link_info($id)
  433. {
  434. $tbl_link = Database::get_course_table(TABLE_LINK);
  435. $course_id = api_get_course_int_id();
  436. if (empty($id) || empty($course_id)) {
  437. return [];
  438. }
  439. $sql = "SELECT * FROM $tbl_link
  440. WHERE c_id = $course_id AND id='".intval($id)."' ";
  441. $result = Database::query($sql);
  442. $data = array();
  443. if (Database::num_rows($result)) {
  444. $data = Database::fetch_array($result);
  445. }
  446. return $data;
  447. }
  448. /**
  449. * @param int $id
  450. * @param array $values
  451. */
  452. public static function editLink($id, $values = array())
  453. {
  454. $tbl_link = Database::get_course_table(TABLE_LINK);
  455. $_course = api_get_course_info();
  456. $course_id = $_course['real_id'];
  457. $id = intval($id);
  458. $values['url'] = trim($values['url']);
  459. $values['title'] = trim($values['title']);
  460. $values['description'] = trim($values['description']);
  461. $values['target'] = empty($values['target']) ? '_self' : $values['target'];
  462. $values['on_homepage'] = isset($values['on_homepage']) ? $values['on_homepage'] : '';
  463. $categoryId = intval($values['category_id']);
  464. // We ensure URL to be absolute.
  465. if (strpos($values['url'], '://') === false) {
  466. $values['url'] = 'http://'.$_POST['url'];
  467. }
  468. // If the title is empty, we use the URL as title.
  469. if ($values['title'] == '') {
  470. $values['title'] = $values['url'];
  471. }
  472. // If the URL is invalid, an error occurs.
  473. if (!api_valid_url($values['url'], true)) {
  474. Display::addFlash(
  475. Display::return_message(get_lang('GiveURL'), 'error')
  476. );
  477. return false;
  478. }
  479. if (empty($id) || empty($course_id)) {
  480. return false;
  481. }
  482. // Finding the old category_id.
  483. $sql = "SELECT * FROM $tbl_link
  484. WHERE c_id = $course_id AND id='".$id."'";
  485. $result = Database:: query($sql);
  486. $row = Database:: fetch_array($result);
  487. $category_id = $row['category_id'];
  488. if ($category_id != $values['category_id']) {
  489. $sql = "SELECT MAX(display_order)
  490. FROM $tbl_link
  491. WHERE
  492. c_id = $course_id AND
  493. category_id='".intval($values['category_id'])."'";
  494. $result = Database:: query($sql);
  495. list ($max_display_order) = Database:: fetch_row($result);
  496. $max_display_order++;
  497. } else {
  498. $max_display_order = $row['display_order'];
  499. }
  500. $params = [
  501. 'url' => $values['url'],
  502. 'title' => $values['title'],
  503. 'description' => $values['description'],
  504. 'category_id' => $values['category_id'],
  505. 'display_order' => $max_display_order,
  506. 'on_homepage' => $values['on_homepage'],
  507. 'target' => $values['target'],
  508. 'category_id' => $values['category_id']
  509. ];
  510. Database::update(
  511. $tbl_link,
  512. $params,
  513. ['c_id = ? AND id = ?' => [$course_id, $id]]
  514. );
  515. // Update search enchine and its values table if enabled.
  516. if (api_get_setting('search_enabled') == 'true') {
  517. $course_int_id = api_get_course_int_id();
  518. $course_id = api_get_course_id();
  519. $link_title = Database:: escape_string($values['title']);
  520. $link_description = Database:: escape_string($values['description']);
  521. // Actually, it consists on delete terms from db,
  522. // insert new ones, create a new search engine document, and remove the old one.
  523. // Get search_did.
  524. $tbl_se_ref = Database::get_main_table(
  525. TABLE_MAIN_SEARCH_ENGINE_REF
  526. );
  527. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  528. $sql = sprintf(
  529. $sql,
  530. $tbl_se_ref,
  531. $course_id,
  532. TOOL_LINK,
  533. $id
  534. );
  535. $res = Database:: query($sql);
  536. if (Database:: num_rows($res) > 0) {
  537. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  538. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  539. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  540. $se_ref = Database:: fetch_array($res);
  541. $specific_fields = get_specific_field_list();
  542. $ic_slide = new IndexableChunk();
  543. $all_specific_terms = '';
  544. foreach ($specific_fields as $specific_field) {
  545. delete_all_specific_field_value(
  546. $course_id,
  547. $specific_field['id'],
  548. TOOL_LINK,
  549. $id
  550. );
  551. if (isset($_REQUEST[$specific_field['code']])) {
  552. $sterms = trim(
  553. $_REQUEST[$specific_field['code']]
  554. );
  555. if (!empty($sterms)) {
  556. $all_specific_terms .= ' '.$sterms;
  557. $sterms = explode(',', $sterms);
  558. foreach ($sterms as $sterm) {
  559. $ic_slide->addTerm(
  560. trim($sterm),
  561. $specific_field['code']
  562. );
  563. add_specific_field_value(
  564. $specific_field['id'],
  565. $course_id,
  566. TOOL_LINK,
  567. $id,
  568. $sterm
  569. );
  570. }
  571. }
  572. }
  573. }
  574. // Build the chunk to index.
  575. $ic_slide->addValue("title", $link_title);
  576. $ic_slide->addCourseId($course_id);
  577. $ic_slide->addToolId(TOOL_LINK);
  578. $xapian_data = array(
  579. SE_COURSE_ID => $course_id,
  580. SE_TOOL_ID => TOOL_LINK,
  581. SE_DATA => array(
  582. 'link_id' => (int) $id
  583. ),
  584. SE_USER => (int) api_get_user_id(),
  585. );
  586. $ic_slide->xapian_data = serialize($xapian_data);
  587. $link_description = $all_specific_terms.' '.$link_description;
  588. $ic_slide->addValue('content', $link_description);
  589. // Add category name if set.
  590. if (isset($categoryId) && $categoryId > 0) {
  591. $table_link_category = Database::get_course_table(
  592. TABLE_LINK_CATEGORY
  593. );
  594. $sql_cat = 'SELECT * FROM %s WHERE id=%d and c_id = %d LIMIT 1';
  595. $sql_cat = sprintf(
  596. $sql_cat,
  597. $table_link_category,
  598. $categoryId,
  599. $course_int_id
  600. );
  601. $result = Database:: query($sql_cat);
  602. if (Database:: num_rows($result) == 1) {
  603. $row = Database:: fetch_array($result);
  604. $ic_slide->addValue(
  605. 'category',
  606. $row['category_title']
  607. );
  608. }
  609. }
  610. $di = new ChamiloIndexer();
  611. isset ($_POST['language']) ? $lang = Database:: escape_string($_POST['language']) : $lang = 'english';
  612. $di->connectDb(null, null, $lang);
  613. $di->remove_document((int) $se_ref['search_did']);
  614. $di->addChunk($ic_slide);
  615. // Index and return search engine document id.
  616. $did = $di->index();
  617. if ($did) {
  618. // Save it to db.
  619. $sql = 'DELETE FROM %s
  620. WHERE course_code=\'%s\'
  621. AND tool_id=\'%s\'
  622. AND ref_id_high_level=\'%s\'';
  623. $sql = sprintf(
  624. $sql,
  625. $tbl_se_ref,
  626. $course_id,
  627. TOOL_LINK,
  628. $id
  629. );
  630. Database:: query($sql);
  631. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  632. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  633. $sql = sprintf(
  634. $sql,
  635. $tbl_se_ref,
  636. $course_int_id,
  637. $course_id,
  638. TOOL_LINK,
  639. $id,
  640. $did
  641. );
  642. Database:: query($sql);
  643. }
  644. }
  645. }
  646. // "WHAT'S NEW" notification: update table last_toolEdit.
  647. api_item_property_update(
  648. $_course,
  649. TOOL_LINK,
  650. $id,
  651. 'LinkUpdated',
  652. api_get_user_id()
  653. );
  654. Display::addFlash(Display::return_message(get_lang('LinkModded')));
  655. }
  656. /**
  657. * @param int $id
  658. * @return bool
  659. */
  660. public static function editCategory($id, $values)
  661. {
  662. $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
  663. $course_id = api_get_course_int_id();
  664. $id = intval($id);
  665. // This is used to put the modified info of the category-form into the database.
  666. $params = [
  667. 'category_title' => $values['category_title'],
  668. 'description' => $values['description']
  669. ];
  670. Database::update(
  671. $tbl_categories,
  672. $params,
  673. ['c_id = ? AND id = ?' => [$course_id, $id]]
  674. );
  675. Display::addFlash(Display::return_message(get_lang('CategoryModded')));
  676. return true;
  677. }
  678. /**
  679. * Changes the visibility of a link
  680. * @todo add the changing of the visibility of a course
  681. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  682. */
  683. public static function change_visibility_link($id, $scope)
  684. {
  685. $_course = api_get_course_info();
  686. $_user = api_get_user_info();
  687. if ($scope == TOOL_LINK) {
  688. api_item_property_update(
  689. $_course,
  690. TOOL_LINK,
  691. $id,
  692. $_GET['action'],
  693. $_user['user_id']
  694. );
  695. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  696. } elseif ($scope == TOOL_LINK_CATEGORY) {
  697. api_item_property_update(
  698. $_course,
  699. TOOL_LINK_CATEGORY,
  700. $id,
  701. $_GET['action'],
  702. $_user['user_id']
  703. );
  704. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  705. }
  706. }
  707. /**
  708. * Generate SQL to select all the links categories in the current course and
  709. * session
  710. * @param int $courseId
  711. * @param int $sessionId
  712. * @param bool $withBaseContent
  713. *
  714. * @return array
  715. */
  716. public static function getLinkCategories($courseId, $sessionId, $withBaseContent = true)
  717. {
  718. $tblLinkCategory = Database::get_course_table(TABLE_LINK_CATEGORY);
  719. $tblItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  720. $courseId = intval($courseId);
  721. $courseInfo = api_get_course_info_by_id($courseId);
  722. // Condition for the session.
  723. $sessionCondition = api_get_session_condition(
  724. $sessionId,
  725. true,
  726. $withBaseContent,
  727. 'linkcat.session_id'
  728. );
  729. // Getting links
  730. $sql = "SELECT *, linkcat.id
  731. FROM $tblLinkCategory linkcat
  732. WHERE
  733. linkcat.c_id = $courseId
  734. $sessionCondition
  735. ORDER BY linkcat.display_order DESC";
  736. $result = Database::query($sql);
  737. $categories = Database::store_result($result);
  738. $sql = "SELECT *, linkcat.id
  739. FROM $tblLinkCategory linkcat
  740. INNER JOIN $tblItemProperty ip
  741. ON (linkcat.id = ip.ref AND linkcat.c_id = ip.c_id)
  742. WHERE
  743. ip.tool = '".TOOL_LINK_CATEGORY."' AND
  744. (ip.visibility = '0' OR ip.visibility = '1')
  745. $sessionCondition AND
  746. linkcat.c_id = ".$courseId."
  747. ORDER BY linkcat.display_order DESC";
  748. $result = Database::query($sql);
  749. $categoryInItemProperty = array();
  750. if (Database::num_rows($result)) {
  751. while ($row = Database::fetch_array($result, 'ASSOC')) {
  752. $categoryInItemProperty[$row['id']] = $row;
  753. }
  754. }
  755. foreach ($categories as & $category) {
  756. if (!isset($categoryInItemProperty[$category['id']])) {
  757. api_item_property_update(
  758. $courseInfo,
  759. TOOL_LINK_CATEGORY,
  760. $category['id'],
  761. 'LinkCategoryAdded',
  762. api_get_user_id()
  763. );
  764. //api_set_default_visibility($category['id'], TOOL_LINK_CATEGORY);
  765. }
  766. }
  767. $sql = "SELECT DISTINCT linkcat.*, visibility
  768. FROM $tblLinkCategory linkcat
  769. INNER JOIN $tblItemProperty ip
  770. ON (linkcat.id = ip.ref AND linkcat.c_id = ip.c_id)
  771. WHERE
  772. ip.tool = '".TOOL_LINK_CATEGORY."' AND
  773. (ip.visibility = '0' OR ip.visibility = '1')
  774. $sessionCondition AND
  775. linkcat.c_id = ".$courseId."
  776. ORDER BY linkcat.display_order DESC
  777. ";
  778. $result = Database::query($sql);
  779. return Database::store_result($result, 'ASSOC');
  780. }
  781. /**
  782. * @param integer $categoryId
  783. * @param $courseId
  784. * @param $sessionId
  785. * @param bool $withBaseContent
  786. *
  787. * @return array
  788. */
  789. public static function getLinksPerCategory(
  790. $categoryId,
  791. $courseId,
  792. $sessionId,
  793. $withBaseContent = true
  794. ) {
  795. $tbl_link = Database::get_course_table(TABLE_LINK);
  796. $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  797. $courseId = (int) $courseId;
  798. $sessionId = (int) $sessionId;
  799. $categoryId = (int) $categoryId;
  800. // Condition for the session.
  801. $condition_session = api_get_session_condition(
  802. $sessionId,
  803. true,
  804. false,
  805. 'ip.session_id'
  806. );
  807. if (!empty($sessionId)) {
  808. $conditionBaseSession = api_get_session_condition(
  809. 0,
  810. true,
  811. $withBaseContent,
  812. 'ip.session_id'
  813. );
  814. $condition = " AND
  815. (
  816. (ip.visibility = '1' $conditionBaseSession) OR
  817. (
  818. (ip.visibility = '0' OR ip.visibility = '1')
  819. $condition_session
  820. )
  821. )
  822. ";
  823. } else {
  824. $condition = api_get_session_condition(
  825. 0,
  826. true,
  827. false,
  828. 'ip.session_id'
  829. );
  830. $condition .= " AND (ip.visibility = '0' OR ip.visibility = '1') $condition ";
  831. }
  832. $sql = "SELECT
  833. link.id,
  834. ip.session_id,
  835. link.session_id link_session_id,
  836. url,
  837. category_id,
  838. visibility,
  839. description,
  840. title,
  841. target,
  842. on_homepage
  843. FROM $tbl_link link
  844. INNER JOIN $TABLE_ITEM_PROPERTY ip
  845. ON (link.id = ip.ref AND link.c_id = ip.c_id)
  846. WHERE
  847. ip.tool = '".TOOL_LINK."' AND
  848. link.category_id = '" . $categoryId."' AND
  849. link.c_id = $courseId AND
  850. ip.c_id = $courseId
  851. $condition
  852. ORDER BY link.display_order ASC, ip.session_id DESC";
  853. $result = Database:: query($sql);
  854. return Database::store_result($result);
  855. }
  856. /**
  857. * Displays all the links of a given category.
  858. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  859. * @author Julio Montoya
  860. *
  861. * @param $catid
  862. * @param integer $courseId
  863. * @param integer $session_id
  864. * @return string
  865. */
  866. public static function showLinksPerCategory($catid, $courseId, $session_id)
  867. {
  868. global $token;
  869. $_user = api_get_user_info();
  870. $catid = intval($catid);
  871. $links = self::getLinksPerCategory($catid, $courseId, $session_id);
  872. $content = '';
  873. $numberOfLinks = count($links);
  874. if (!empty($links)) {
  875. $content .= '<div class="link list-group">';
  876. $i = 1;
  877. $linksAdded = [];
  878. foreach ($links as $myrow) {
  879. $linkId = $myrow['id'];
  880. if (in_array($linkId, $linksAdded)) {
  881. continue;
  882. }
  883. $linksAdded[] = $linkId;
  884. $categoryId = $myrow['category_id'];
  885. // Validation when belongs to a session.
  886. $session_img = api_get_session_image(
  887. $myrow['link_session_id'],
  888. $_user['status']
  889. );
  890. $toolbar = '';
  891. $link_validator = '';
  892. if (api_is_allowed_to_edit(null, true)) {
  893. $toolbar .= Display::toolbarButton(
  894. '',
  895. 'javascript:void(0);',
  896. 'check-circle-o',
  897. 'default btn-sm',
  898. array(
  899. 'onclick' => "check_url('".$linkId."', '".addslashes($myrow['url'])."');",
  900. 'title' => get_lang('CheckURL')
  901. )
  902. );
  903. $link_validator .= Display::span(
  904. '',
  905. array(
  906. 'id' => 'url_id_'.$linkId,
  907. 'class' => 'check-link'
  908. )
  909. );
  910. if ($session_id == $myrow['link_session_id']) {
  911. $url = api_get_self().'?'.api_get_cidreq().'&action=editlink&id='.$linkId;
  912. $title = get_lang('Edit');
  913. $toolbar .= Display::toolbarButton(
  914. '',
  915. $url,
  916. 'pencil',
  917. 'default btn-sm',
  918. array(
  919. 'title' => $title
  920. )
  921. );
  922. }
  923. $urlVisibility = api_get_self().'?'.api_get_cidreq().
  924. '&sec_token='.$token.
  925. '&id='.$linkId.
  926. '&scope=link&category_id='.$categoryId;
  927. switch ($myrow['visibility']) {
  928. case '1':
  929. $urlVisibility .= '&action=invisible';
  930. $title = get_lang('MakeInvisible');
  931. $toolbar .= Display::toolbarButton(
  932. '',
  933. $urlVisibility,
  934. 'eye',
  935. 'default btn-sm',
  936. array(
  937. 'title' => $title
  938. )
  939. );
  940. break;
  941. case '0':
  942. $urlVisibility .= '&action=visible';
  943. $title = get_lang('MakeVisible');
  944. $toolbar .= Display::toolbarButton(
  945. '',
  946. $urlVisibility,
  947. 'eye-slash',
  948. 'primary btn-sm',
  949. array(
  950. 'title' => $title
  951. )
  952. );
  953. break;
  954. }
  955. if ($session_id == $myrow['link_session_id']) {
  956. $moveLinkParams = [
  957. 'id' => $linkId,
  958. 'scope' => 'category',
  959. 'category_id' => $categoryId,
  960. 'action' => 'move_link_up'
  961. ];
  962. $toolbar .= Display::toolbarButton(
  963. get_lang('MoveUp'),
  964. api_get_self().'?'.api_get_cidreq().'&'.http_build_query($moveLinkParams),
  965. 'level-up',
  966. 'default',
  967. ['class' => 'btn-sm '.($i === 1 ? 'disabled' : '')],
  968. false
  969. );
  970. $moveLinkParams['action'] = 'move_link_down';
  971. $toolbar .= Display::toolbarButton(
  972. get_lang('MoveDown'),
  973. api_get_self().'?'.api_get_cidreq().'&'.http_build_query($moveLinkParams),
  974. 'level-down',
  975. 'default',
  976. ['class' => 'btn-sm '.($i === $numberOfLinks ? 'disabled' : '')],
  977. false
  978. );
  979. $url .= api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=deletelink&id='.$linkId.'&category_id='.$categoryId;
  980. $event = "javascript: if(!confirm('".get_lang('LinkDelconfirm')."'))return false;";
  981. $title = get_lang('Delete');
  982. $toolbar .= Display::toolbarButton(
  983. '',
  984. $url,
  985. 'trash',
  986. 'default btn-sm',
  987. array(
  988. 'onclick' => $event,
  989. 'title' => $title
  990. )
  991. );
  992. }
  993. }
  994. $iconLink = Display::return_icon(
  995. 'url.png',
  996. get_lang('Link'),
  997. null,
  998. ICON_SIZE_SMALL
  999. );
  1000. if ($myrow['visibility'] == '1') {
  1001. $content .= '<div class="list-group-item">';
  1002. $content .= '<div class="pull-right"><div class="btn-group">'.$toolbar.'</div></div>';
  1003. $content .= '<h4 class="list-group-item-heading">';
  1004. $content .= $iconLink;
  1005. $url = api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$linkId.'&link_url='.urlencode($myrow['url']);
  1006. $content .= Display::tag(
  1007. 'a',
  1008. Security::remove_XSS($myrow['title']),
  1009. array(
  1010. 'href' => $url,
  1011. 'target' => $myrow['target']
  1012. )
  1013. );
  1014. $content .= $link_validator;
  1015. $content .= $session_img;
  1016. $content .= '</h4>';
  1017. $content .= '<p class="list-group-item-text">'.$myrow['description'].'</p>';
  1018. $content .= '</div>';
  1019. } else {
  1020. if (api_is_allowed_to_edit(null, true)) {
  1021. $content .= '<div class="list-group-item">';
  1022. $content .= '<div class="pull-right"><div class="btn-group">'.$toolbar.'</div></div>';
  1023. $content .= '<h4 class="list-group-item-heading">';
  1024. $content .= $iconLink;
  1025. $url = api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$linkId."&link_url=".urlencode($myrow['url']);
  1026. $content .= Display::tag(
  1027. 'a',
  1028. Security::remove_XSS($myrow['title']),
  1029. array(
  1030. 'href' => $url,
  1031. 'target' => '_blank',
  1032. 'class' => 'text-muted'
  1033. )
  1034. );
  1035. $content .= $link_validator;
  1036. $content .= $session_img;
  1037. $content .= '</h4>';
  1038. $content .= '<p class="list-group-item-text">'.$myrow['description'].'</p>';
  1039. $content .= '</div>';
  1040. }
  1041. }
  1042. $i++;
  1043. }
  1044. $content .= '</div>';
  1045. }
  1046. return $content;
  1047. }
  1048. /**
  1049. * Displays the edit, delete and move icons
  1050. * @param int Category ID
  1051. * @param integer $currentCategory
  1052. * @param integer $countCategories
  1053. * @return string
  1054. *
  1055. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1056. */
  1057. public static function showCategoryAdminTools($category, $currentCategory, $countCategories)
  1058. {
  1059. $categoryId = $category['id'];
  1060. $token = null;
  1061. $tools = '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=editcategory&id='.$categoryId.'&category_id='.$categoryId.'" title='.get_lang('Modify').'">'.
  1062. Display:: return_icon(
  1063. 'edit.png',
  1064. get_lang('Modify'),
  1065. array(),
  1066. ICON_SIZE_SMALL
  1067. ).'</a>';
  1068. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  1069. if ($currentCategory != 0) {
  1070. $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=up&up='.$categoryId.'&category_id='.$categoryId.'" title="'.get_lang('Up').'">'.
  1071. Display:: return_icon(
  1072. 'up.png',
  1073. get_lang('Up'),
  1074. array(),
  1075. ICON_SIZE_SMALL
  1076. ).'</a>';
  1077. } else {
  1078. $tools .= Display:: return_icon(
  1079. 'up_na.png',
  1080. get_lang('Up'),
  1081. array(),
  1082. ICON_SIZE_SMALL
  1083. ).'</a>';
  1084. }
  1085. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  1086. if ($currentCategory < $countCategories - 1) {
  1087. $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=down&down='.$categoryId.'&category_id='.$categoryId.'">'.
  1088. Display:: return_icon(
  1089. 'down.png',
  1090. get_lang('Down'),
  1091. array(),
  1092. ICON_SIZE_SMALL
  1093. ).'</a>';
  1094. } else {
  1095. $tools .= Display:: return_icon(
  1096. 'down_na.png',
  1097. get_lang('Down'),
  1098. array(),
  1099. ICON_SIZE_SMALL
  1100. ).'</a>';
  1101. }
  1102. $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=deletecategory&id='.$categoryId."&category_id=$categoryId\"
  1103. onclick=\"javascript: if(!confirm('".get_lang('CategoryDelconfirm')."')) return false;\">".
  1104. Display:: return_icon(
  1105. 'delete.png',
  1106. get_lang('Delete'),
  1107. array(),
  1108. ICON_SIZE_SMALL
  1109. ).'</a>';
  1110. return $tools;
  1111. }
  1112. /**
  1113. * move a link or a linkcategory up or down
  1114. * @param int Category ID
  1115. * @param int Course ID
  1116. * @param int Session ID
  1117. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1118. * @todo support sessions
  1119. */
  1120. public static function movecatlink($action, $catlinkid, $courseId = null, $sessionId = null)
  1121. {
  1122. $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
  1123. if (is_null($courseId)) {
  1124. $courseId = api_get_course_int_id();
  1125. }
  1126. $courseId = intval($courseId);
  1127. if (is_null($sessionId)) {
  1128. $sessionId = api_get_session_id();
  1129. }
  1130. $sessionId = intval($sessionId);
  1131. $thiscatlinkId = intval($catlinkid);
  1132. if ($action == 'down') {
  1133. $sortDirection = 'DESC';
  1134. }
  1135. if ($action == 'up') {
  1136. $sortDirection = 'ASC';
  1137. }
  1138. $movetable = $tbl_categories;
  1139. if (!empty($sortDirection)) {
  1140. if (!in_array(trim(strtoupper($sortDirection)), array('ASC', 'DESC'))) {
  1141. $sortDirection = 'ASC';
  1142. }
  1143. $sql = "SELECT id, display_order FROM $movetable
  1144. WHERE c_id = $courseId
  1145. ORDER BY display_order $sortDirection";
  1146. $linkresult = Database:: query($sql);
  1147. $thislinkOrder = 1;
  1148. while ($sortrow = Database:: fetch_array($linkresult)) {
  1149. // STEP 2 : FOUND THE NEXT LINK ID AND ORDER, COMMIT SWAP
  1150. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  1151. // and we know the next iteration of the while loop is the next one. These should be swapped.
  1152. if (isset($thislinkFound) && $thislinkFound) {
  1153. $nextlinkId = $sortrow['id'];
  1154. $nextlinkOrder = $sortrow['display_order'];
  1155. Database:: query(
  1156. "UPDATE ".$movetable."
  1157. SET display_order = '$nextlinkOrder'
  1158. WHERE c_id = $courseId AND id = '$thiscatlinkId'"
  1159. );
  1160. Database:: query(
  1161. "UPDATE ".$movetable."
  1162. SET display_order = '$thislinkOrder'
  1163. WHERE c_id = $courseId AND id = '$nextlinkId'"
  1164. );
  1165. break;
  1166. }
  1167. if ($sortrow['id'] == $thiscatlinkId) {
  1168. $thislinkOrder = $sortrow['display_order'];
  1169. $thislinkFound = true;
  1170. }
  1171. }
  1172. }
  1173. Display::addFlash(Display::return_message(get_lang('LinkMoved')));
  1174. }
  1175. /**
  1176. * CSV file import functions
  1177. * @author René Haentjens , Ghent University
  1178. * @param string $catname
  1179. */
  1180. public static function get_cat($catname)
  1181. {
  1182. // Get category id (existing or make new).
  1183. $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
  1184. $course_id = api_get_course_int_id();
  1185. $result = Database:: query(
  1186. "SELECT id FROM ".$tbl_categories."
  1187. WHERE c_id = $course_id AND category_title='".Database::escape_string($catname)."'"
  1188. );
  1189. if (Database:: num_rows($result) >= 1 && ($row = Database:: fetch_array($result))) {
  1190. return $row['id']; // Several categories with same name: take the first.
  1191. }
  1192. $result = Database:: query(
  1193. "SELECT MAX(display_order) FROM ".$tbl_categories." WHERE c_id = $course_id "
  1194. );
  1195. list ($max_order) = Database:: fetch_row($result);
  1196. $params = [
  1197. 'c_id' => $course_id,
  1198. 'category_title' => $catname,
  1199. 'description' => '',
  1200. 'display_order' => $max_order + 1
  1201. ];
  1202. $id = Database::insert($tbl_categories, $params);
  1203. return $id;
  1204. }
  1205. /**
  1206. * CSV file import functions
  1207. * @author René Haentjens , Ghent University
  1208. * @param string $url
  1209. * @param string $title
  1210. * @param string $description
  1211. * @param string $on_homepage
  1212. * @param string $hidden
  1213. */
  1214. public static function put_link($url, $cat, $title, $description, $on_homepage, $hidden)
  1215. {
  1216. $_course = api_get_course_info();
  1217. $_user = api_get_user_info();
  1218. $tbl_link = Database::get_course_table(TABLE_LINK);
  1219. $course_id = api_get_course_int_id();
  1220. $urleq = "url='".Database:: escape_string($url)."'";
  1221. $cateq = "category_id=".intval($cat);
  1222. $result = Database:: query("
  1223. SELECT id FROM $tbl_link
  1224. WHERE c_id = $course_id AND ".$urleq.' AND '.$cateq
  1225. );
  1226. if (Database:: num_rows($result) >= 1 && ($row = Database:: fetch_array($result))) {
  1227. $sql = "UPDATE $tbl_link SET
  1228. title = '".Database:: escape_string($title)."',
  1229. description = '" . Database:: escape_string($description)."'
  1230. WHERE c_id = $course_id AND id='".Database:: escape_string($row['id'])."'";
  1231. Database:: query($sql);
  1232. $ipu = 'LinkUpdated';
  1233. $rv = 1; // 1 = upd
  1234. } else {
  1235. // Add new link
  1236. $result = Database:: query(
  1237. "SELECT MAX(display_order) FROM $tbl_link
  1238. WHERE c_id = $course_id AND category_id='".intval($cat)."'"
  1239. );
  1240. list ($max_order) = Database:: fetch_row($result);
  1241. Database:: query(
  1242. "INSERT INTO $tbl_link (c_id, url, title, description, category_id, display_order, on_homepage)
  1243. VALUES (".api_get_course_int_id().",
  1244. '" . Database:: escape_string($url)."',
  1245. '" . Database:: escape_string($title)."',
  1246. '" . Database:: escape_string($description)."',
  1247. '" . intval($cat)."','".(intval($max_order) + 1)."',
  1248. '" . intval($on_homepage).
  1249. "')"
  1250. );
  1251. $id = Database:: insert_id();
  1252. $ipu = 'LinkAdded';
  1253. $rv = 2; // 2 = new
  1254. }
  1255. api_item_property_update(
  1256. $_course,
  1257. TOOL_LINK,
  1258. $id,
  1259. $ipu,
  1260. $_user['user_id']
  1261. );
  1262. if ($hidden && $ipu == 'LinkAdded') {
  1263. api_item_property_update(
  1264. $_course,
  1265. TOOL_LINK,
  1266. $id,
  1267. 'invisible',
  1268. $_user['user_id']
  1269. );
  1270. }
  1271. return $rv;
  1272. }
  1273. /**
  1274. * CSV file import functions
  1275. * @author René Haentjens , Ghent University
  1276. */
  1277. public static function import_link($linkdata)
  1278. {
  1279. // url, category_id, title, description, ...
  1280. // Field names used in the uploaded file
  1281. $known_fields = array(
  1282. 'url',
  1283. 'category',
  1284. 'title',
  1285. 'description',
  1286. 'on_homepage',
  1287. 'hidden'
  1288. );
  1289. $hide_fields = array(
  1290. 'kw',
  1291. 'kwd',
  1292. 'kwds',
  1293. 'keyword',
  1294. 'keywords'
  1295. );
  1296. // All other fields are added to description, as "name:value".
  1297. // Only one hide_field is assumed to be present, <> is removed from value.
  1298. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title']))) {
  1299. return 0; // 0 = fail
  1300. }
  1301. $cat = ($catname = trim($linkdata['category'])) ? self::get_cat($catname) : 0;
  1302. $regs = array(); // Will be passed to ereg()
  1303. $d = '';
  1304. foreach ($linkdata as $key => $value) {
  1305. if (!in_array($key, $known_fields)) {
  1306. if (in_array($key, $hide_fields) && ereg(
  1307. '^<?([^>]*)>?$',
  1308. $value,
  1309. $regs
  1310. )
  1311. ) { // possibly in <...>
  1312. if (($kwlist = trim($regs[1])) != '') {
  1313. $kw = '<i kw="'.htmlspecialchars($kwlist).'">';
  1314. } else {
  1315. $kw = '';
  1316. }
  1317. // i.e. assume only one of the $hide_fields will be present
  1318. // and if found, hide the value as expando property of an <i> tag
  1319. } elseif (trim($value)) {
  1320. $d .= ', '.$key.':'.$value;
  1321. }
  1322. }
  1323. }
  1324. if (!empty($d)) {
  1325. $d = substr($d, 2).' - ';
  1326. }
  1327. return self::put_link(
  1328. $url,
  1329. $cat,
  1330. $title,
  1331. $kw.ereg_replace(
  1332. '\[((/?(b|big|i|small|sub|sup|u))|br/)\]',
  1333. '<\\1>',
  1334. htmlspecialchars($d.$linkdata['description'])
  1335. ).($kw ? '</i>' : ''),
  1336. $linkdata['on_homepage'] ? '1' : '0',
  1337. $linkdata['hidden'] ? '1' : '0'
  1338. );
  1339. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  1340. }
  1341. /**
  1342. * This function checks if the url is a vimeo link
  1343. * @author Julio Montoya
  1344. * @version 1.0
  1345. */
  1346. public static function isVimeoLink($url)
  1347. {
  1348. $isLink = strrpos($url, "vimeo.com");
  1349. return $isLink;
  1350. }
  1351. /**
  1352. * Get vimeo id from URL
  1353. * @param string $url
  1354. * @return bool|mixed
  1355. */
  1356. public static function getVimeoLinkId($url)
  1357. {
  1358. $possibleUrls = array(
  1359. 'http://www.vimeo.com/',
  1360. 'http://vimeo.com/',
  1361. 'https://www.vimeo.com/',
  1362. 'https://vimeo.com/'
  1363. );
  1364. $url = str_replace($possibleUrls, '', $url);
  1365. if (is_numeric($url)) {
  1366. return $url;
  1367. }
  1368. return false;
  1369. }
  1370. /**
  1371. * This function checks if the url is a youtube link
  1372. * @author Jorge Frisancho
  1373. * @author Julio Montoya - Fixing code
  1374. * @version 1.0
  1375. */
  1376. public static function is_youtube_link($url)
  1377. {
  1378. $is_youtube_link = strrpos($url, "youtube") || strrpos(
  1379. $url,
  1380. "youtu.be"
  1381. );
  1382. return $is_youtube_link;
  1383. }
  1384. /**
  1385. * Get youtube id from an URL
  1386. * @param string $url
  1387. * @return string
  1388. */
  1389. public static function get_youtube_video_id($url)
  1390. {
  1391. // This is the length of YouTube's video IDs
  1392. $len = 11;
  1393. // The ID string starts after "v=", which is usually right after
  1394. // "youtube.com/watch?" in the URL
  1395. $pos = strpos($url, "v=");
  1396. $id = '';
  1397. //If false try other options
  1398. if ($pos === false) {
  1399. $url_parsed = parse_url($url);
  1400. //Youtube shortener
  1401. //http://youtu.be/ID
  1402. $pos = strpos($url, "youtu.be");
  1403. if ($pos == false) {
  1404. $id = '';
  1405. } else {
  1406. return substr($url_parsed['path'], 1);
  1407. }
  1408. //if empty try the youtube.com/embed/ID
  1409. if (empty($id)) {
  1410. $pos = strpos($url, "embed");
  1411. if ($pos === false) {
  1412. return '';
  1413. } else {
  1414. return substr($url_parsed['path'], 7);
  1415. }
  1416. }
  1417. } else {
  1418. // Offset the start location to match the beginning of the ID string
  1419. $pos += 2;
  1420. // Get the ID string and return it
  1421. $id = substr($url, $pos, $len);
  1422. return $id;
  1423. }
  1424. }
  1425. /**
  1426. * @param int $course_id
  1427. * @param int $session_id
  1428. * @param int $categoryId
  1429. * @param string $show
  1430. * @param null $token
  1431. */
  1432. public static function listLinksAndCategories(
  1433. $course_id,
  1434. $session_id,
  1435. $categoryId,
  1436. $show = 'none',
  1437. $token = null
  1438. ) {
  1439. $tbl_link = Database::get_course_table(TABLE_LINK);
  1440. $tblCIP = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1441. $categoryId = intval($categoryId);
  1442. /* Action Links */
  1443. echo '<div class="actions">';
  1444. if (api_is_allowed_to_edit(null, true)) {
  1445. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addlink&category_id='.$categoryId.'">'.
  1446. Display::return_icon('new_link.png', get_lang('LinkAdd'), '', ICON_SIZE_MEDIUM).'</a>';
  1447. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addcategory&category_id='.$categoryId.'">'.
  1448. Display::return_icon('new_folder.png', get_lang('CategoryAdd'), '', ICON_SIZE_MEDIUM).'</a>';
  1449. }
  1450. $categories = self::getLinkCategories($course_id, $session_id);
  1451. $countCategories = count($categories);
  1452. if (!empty($countCategories)) {
  1453. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=none">';
  1454. echo Display::return_icon('forum_listview.png', get_lang('FlatView'), '', ICON_SIZE_MEDIUM).' </a>';
  1455. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=all">';
  1456. echo Display::return_icon('forum_nestedview.png', get_lang('NestedView'), '', ICON_SIZE_MEDIUM).'</a>';
  1457. }
  1458. echo '</div>';
  1459. // Displaying the links which have no category (thus category = 0 or NULL),
  1460. // if none present this will not be displayed
  1461. $sql = "
  1462. SELECT COUNT(1) AS count FROM $tbl_link l
  1463. INNER JOIN $tblCIP c
  1464. ON (l.c_id = c.c_id AND l.iid = c.ref)
  1465. WHERE
  1466. c.tool = 'link' AND
  1467. c.visibility != 2 AND
  1468. l.c_id = $course_id AND
  1469. (l.category_id = 0 OR l.category_id IS NULL)
  1470. ";
  1471. $result = Database::query($sql);
  1472. $count = Database::result($result, 0, 'count');
  1473. $linksPerCategory = self::showLinksPerCategory(0, $course_id, $session_id);
  1474. if ($count && !$countCategories) {
  1475. echo $linksPerCategory;
  1476. } elseif ($count && $countCategories) {
  1477. echo Display::panel($linksPerCategory, get_lang('NoCategory'));
  1478. }
  1479. $counter = 0;
  1480. foreach ($categories as $myrow) {
  1481. // Student don't see invisible categories.
  1482. if (!api_is_allowed_to_edit(null, true)) {
  1483. if ($myrow['visibility'] == 0) {
  1484. continue;
  1485. }
  1486. }
  1487. // Validation when belongs to a session
  1488. $showChildren = $categoryId == $myrow['id'] || $show == 'all';
  1489. $myrow['description'] = $myrow['description'];
  1490. $strVisibility = '';
  1491. $visibilityClass = null;
  1492. if ($myrow['visibility'] == '1') {
  1493. $strVisibility = '<a href="link.php?'.api_get_cidreq().'&sec_token='.$token.'&action=invisible&id='.$myrow['id'].'&scope='.TOOL_LINK_CATEGORY.'" title="'.get_lang('Hide').'">'.
  1494. Display::return_icon('visible.png', get_lang('Hide'), array(), ICON_SIZE_SMALL).'</a>';
  1495. } elseif ($myrow['visibility'] == '0') {
  1496. $visibilityClass = 'text-muted';
  1497. $strVisibility = ' <a href="link.php?'.api_get_cidreq().'&sec_token='.$token.'&action=visible&id='.$myrow['id'].'&scope='.TOOL_LINK_CATEGORY.'" title="'.get_lang('Show').'">'.
  1498. Display::return_icon('invisible.png', get_lang('Show'), array(), ICON_SIZE_SMALL).'</a>';
  1499. }
  1500. $header = '';
  1501. if ($showChildren) {
  1502. $header .= '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id=">';
  1503. $header .= Display::return_icon('forum_nestedview.png');
  1504. } else {
  1505. $header .= '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id='.$myrow['id'].'">';
  1506. $header .= Display::return_icon('forum_listview.png');
  1507. }
  1508. $header .= Security::remove_XSS($myrow['category_title']).'</a>';
  1509. $header .= '<div class="pull-right">';
  1510. if (api_is_allowed_to_edit(null, true)) {
  1511. if ($session_id == $myrow['session_id']) {
  1512. $header .= $strVisibility;
  1513. $header .= self::showCategoryAdminTools($myrow, $counter, count($categories));
  1514. } else {
  1515. $header .= get_lang('EditionNotAvailableFromSession');
  1516. }
  1517. }
  1518. $childrenContent = '';
  1519. if ($showChildren) {
  1520. $childrenContent = self::showLinksPerCategory(
  1521. $myrow['id'],
  1522. api_get_course_int_id(),
  1523. api_get_session_id()
  1524. );
  1525. }
  1526. echo Display::panel($myrow['description'].$childrenContent, $header);
  1527. $counter++;
  1528. }
  1529. }
  1530. /**
  1531. * @param int $linkId
  1532. * @param $action
  1533. * @param null $token
  1534. *
  1535. * @return FormValidator
  1536. */
  1537. public static function getLinkForm($linkId, $action, $token = null)
  1538. {
  1539. $course_id = api_get_course_int_id();
  1540. $session_id = api_get_session_id();
  1541. $linkInfo = self::get_link_info($linkId);
  1542. $categoryId = isset($linkInfo['category_id']) ? $linkInfo['category_id'] : '';
  1543. $lpId = isset($_GET['lp_id']) ? Security::remove_XSS($_GET['lp_id']) : null;
  1544. $form = new FormValidator(
  1545. 'link',
  1546. 'post',
  1547. api_get_self().'?action='.$action.
  1548. '&category_id='.$categoryId.
  1549. '&'.api_get_cidreq().
  1550. '&id='.$linkId.
  1551. '&sec_token='.$token
  1552. );
  1553. if ($action == 'addlink') {
  1554. $form->addHeader(get_lang('LinkAdd'));
  1555. } else {
  1556. $form->addHeader(get_lang('LinkMod'));
  1557. }
  1558. $target_link = '_blank';
  1559. $title = '';
  1560. $category = '';
  1561. $onhomepage = '';
  1562. $description = '';
  1563. if (!empty($linkInfo)) {
  1564. $urllink = $linkInfo['url'];
  1565. $title = $linkInfo['title'];
  1566. $description = $linkInfo['description'];
  1567. $category = $linkInfo['category_id'];
  1568. if ($linkInfo['on_homepage'] != 0) {
  1569. $onhomepage = 1;
  1570. }
  1571. $target_link = $linkInfo['target'];
  1572. }
  1573. $form->addHidden('id', $linkId);
  1574. $form->addText('url', 'URL');
  1575. $form->addRule('url', get_lang('GiveURL'), 'url');
  1576. $form->addText('title', get_lang('LinkName'));
  1577. $form->addTextarea('description', get_lang('Description'));
  1578. $resultcategories = self::getLinkCategories($course_id, $session_id);
  1579. $options = ['0' => '--'];
  1580. if (!empty($resultcategories)) {
  1581. foreach ($resultcategories as $myrow) {
  1582. $options[$myrow['id']] = $myrow['category_title'];
  1583. }
  1584. }
  1585. $form->addSelect('category_id', get_lang('Category'), $options);
  1586. $form->addCheckBox('on_homepage', null, get_lang('OnHomepage'));
  1587. $targets = array(
  1588. '_self' => get_lang('LinkOpenSelf'),
  1589. '_blank' => get_lang('LinkOpenBlank'),
  1590. '_parent' => get_lang('LinkOpenParent'),
  1591. '_top' => get_lang('LinkOpenTop')
  1592. );
  1593. $form->addSelect(
  1594. 'target',
  1595. array(
  1596. get_lang('LinkTarget'),
  1597. get_lang('AddTargetOfLinkOnHomepage')
  1598. ),
  1599. $targets
  1600. );
  1601. $defaults = array(
  1602. 'url' => empty($urllink) ? 'http://' : Security::remove_XSS($urllink),
  1603. 'title' => Security::remove_XSS($title),
  1604. 'category_id' => $category,
  1605. 'on_homepage' => $onhomepage,
  1606. 'description' => $description,
  1607. 'target' => $target_link
  1608. );
  1609. if (api_get_setting('search_enabled') == 'true') {
  1610. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1611. $specific_fields = get_specific_field_list();
  1612. $form->addCheckBox('index_document', get_lang('SearchFeatureDoIndexLink'), get_lang('Yes'));
  1613. foreach ($specific_fields as $specific_field) {
  1614. $default_values = '';
  1615. if ($action == 'editlink') {
  1616. $filter = array(
  1617. 'field_id' => $specific_field['id'],
  1618. 'ref_id' => intval($_GET['id']),
  1619. 'tool_id' => '\''.TOOL_LINK.'\''
  1620. );
  1621. $values = get_specific_field_values_list($filter, array('value'));
  1622. if (!empty($values)) {
  1623. $arr_str_values = array();
  1624. foreach ($values as $value) {
  1625. $arr_str_values[] = $value['value'];
  1626. }
  1627. $default_values = implode(', ', $arr_str_values);
  1628. }
  1629. }
  1630. $form->addText($specific_field['name'], $specific_field['code']);
  1631. $defaults[$specific_field['name']] = $default_values;
  1632. }
  1633. }
  1634. $form->addHidden('lp_id', $lpId);
  1635. $form->addButtonSave(get_lang('SaveLink'), 'submitLink');
  1636. $form->setDefaults($defaults);
  1637. return $form;
  1638. }
  1639. /**
  1640. * @param int $id
  1641. * @param string $action
  1642. *
  1643. * @return FormValidator
  1644. */
  1645. public static function getCategoryForm($id, $action)
  1646. {
  1647. $form = new FormValidator(
  1648. 'category',
  1649. 'post',
  1650. api_get_self().'?action='.$action.'&'.api_get_cidreq()
  1651. );
  1652. $defaults = [];
  1653. if ($action == 'addcategory') {
  1654. $form->addHeader(get_lang('CategoryAdd'));
  1655. $my_cat_title = get_lang('CategoryAdd');
  1656. } else {
  1657. $form->addHeader(get_lang('CategoryMod'));
  1658. $my_cat_title = get_lang('CategoryMod');
  1659. $defaults = self::getCategory($id);
  1660. }
  1661. $form->addHidden('id', $id);
  1662. $form->addText('category_title', get_lang('CategoryName'));
  1663. $form->addTextarea('description', get_lang('Description'));
  1664. $form->addButtonSave($my_cat_title, 'submitCategory');
  1665. $form->setDefaults($defaults);
  1666. return $form;
  1667. }
  1668. /**
  1669. * @param int $id
  1670. * @return array
  1671. */
  1672. public static function getCategory($id)
  1673. {
  1674. $table = Database::get_course_table(TABLE_LINK_CATEGORY);
  1675. $id = intval($id);
  1676. $courseId = api_get_course_int_id();
  1677. if (empty($id) || empty($courseId)) {
  1678. return [];
  1679. }
  1680. $sql = "SELECT * FROM $table
  1681. WHERE id = $id AND c_id = $courseId";
  1682. $result = Database::query($sql);
  1683. $category = Database::fetch_array($result, 'ASSOC');
  1684. return $category;
  1685. }
  1686. /**
  1687. * Move a link inside its category (display_order field)
  1688. * @param int $id The link ID
  1689. * @param string $direction The direction to sort the links
  1690. * @return bool
  1691. */
  1692. private static function moveLinkDisplayOrder($id, $direction)
  1693. {
  1694. $em = Database::getManager();
  1695. /** @var CLink $link */
  1696. $link = $em->find('ChamiloCourseBundle:CLink', $id);
  1697. if (!$link) {
  1698. return false;
  1699. }
  1700. $compareLinks = $em
  1701. ->getRepository('ChamiloCourseBundle:CLink')
  1702. ->findBy(
  1703. [
  1704. 'cId' => $link->getCId(),
  1705. 'categoryId' => $link->getCategoryId()
  1706. ],
  1707. ['displayOrder' => $direction]
  1708. );
  1709. /** @var CLink $prevLink */
  1710. $prevLink = null;
  1711. /** @var CLink $compareLink */
  1712. foreach ($compareLinks as $compareLink) {
  1713. if ($compareLink->getId() !== $link->getId()) {
  1714. $prevLink = $compareLink;
  1715. continue;
  1716. }
  1717. if (!$prevLink) {
  1718. return false;
  1719. }
  1720. $newPrevLinkDisplayOrder = $link->getDisplayOrder();
  1721. $newLinkDisplayOrder = $prevLink->getDisplayOrder();
  1722. $link->setDisplayOrder($newLinkDisplayOrder);
  1723. $prevLink->setDisplayOrder($newPrevLinkDisplayOrder);
  1724. $em->merge($prevLink);
  1725. $em->merge($link);
  1726. break;
  1727. }
  1728. $em->flush();
  1729. return true;
  1730. }
  1731. /**
  1732. * Move a link up in its category
  1733. * @param int $id
  1734. * @return bool
  1735. */
  1736. public static function moveLinkUp($id)
  1737. {
  1738. return self::moveLinkDisplayOrder($id, 'ASC');
  1739. }
  1740. /**
  1741. * Move a link down in its category
  1742. * @param int $id
  1743. * @return bool
  1744. */
  1745. public static function moveLinkDown($id)
  1746. {
  1747. return self::moveLinkDisplayOrder($id, 'DESC');
  1748. }
  1749. /**
  1750. * @param string $url
  1751. * @return bool
  1752. */
  1753. public static function checkUrl($url)
  1754. {
  1755. // Check if curl is available.
  1756. if (!in_array('curl', get_loaded_extensions())) {
  1757. return false;
  1758. }
  1759. // set URL and other appropriate options
  1760. $defaults = array(
  1761. CURLOPT_URL => $url,
  1762. CURLOPT_FOLLOWLOCATION => true, // follow redirects accept youtube.com
  1763. CURLOPT_HEADER => 0,
  1764. CURLOPT_RETURNTRANSFER => true,
  1765. CURLOPT_TIMEOUT => 4
  1766. );
  1767. $proxySettings = api_get_configuration_value('proxy_settings');
  1768. if (!empty($proxySettings) &&
  1769. isset($proxySettings['curl_setopt_array'])
  1770. ) {
  1771. $defaults[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY'];
  1772. $defaults[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT'];
  1773. }
  1774. // Create a new cURL resource
  1775. $ch = curl_init();
  1776. curl_setopt_array($ch, $defaults);
  1777. // grab URL and pass it to the browser
  1778. ob_start();
  1779. $result = curl_exec($ch);
  1780. ob_get_clean();
  1781. // close cURL resource, and free up system resources
  1782. curl_close($ch);
  1783. return $result;
  1784. }
  1785. }