plugin.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CTool;
  4. /**
  5. * Class Plugin
  6. * Base class for plugins
  7. *
  8. * This class has to be extended by every plugin. It defines basic methods
  9. * to install/uninstall and get information about a plugin
  10. *
  11. * @author Julio Montoya <gugli100@gmail.com>
  12. * @author Yannick Warnier <ywarnier@beeznest.org>
  13. * @author Laurent Opprecht <laurent@opprecht.info>
  14. * @copyright 2012 University of Geneva
  15. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  16. *
  17. */
  18. class Plugin
  19. {
  20. const TAB_FILTER_NO_STUDENT = '::no-student';
  21. const TAB_FILTER_ONLY_STUDENT = '::only-student';
  22. protected $version = '';
  23. protected $author = '';
  24. protected $fields = [];
  25. private $settings = [];
  26. // Translation strings.
  27. private $strings = null;
  28. public $isCoursePlugin = false;
  29. public $isAdminPlugin = false;
  30. public $isMailPlugin = false;
  31. // Adds icon in the course home
  32. public $addCourseTool = true;
  33. /**
  34. * When creating a new course, these settings are added to the course, in
  35. * the course_info/infocours.php
  36. * To show the plugin course icons you need to add these icons:
  37. * main/img/icons/22/plugin_name.png
  38. * main/img/icons/64/plugin_name.png
  39. * main/img/icons/64/plugin_name_na.png
  40. * @example
  41. * $course_settings = array(
  42. array('name' => 'big_blue_button_welcome_message', 'type' => 'text'),
  43. array('name' => 'big_blue_button_record_and_store', 'type' => 'checkbox')
  44. );
  45. */
  46. public $course_settings = array();
  47. /**
  48. * This indicates whether changing the setting should execute the callback
  49. * function.
  50. */
  51. public $course_settings_callback = false;
  52. /**
  53. * Default constructor for the plugin class. By default, it only sets
  54. * a few attributes of the object
  55. * @param string $version of this plugin
  56. * @param string $author of this plugin
  57. * @param array $settings settings to be proposed to configure the plugin
  58. */
  59. protected function __construct($version, $author, $settings = array())
  60. {
  61. $this->version = $version;
  62. $this->author = $author;
  63. $this->fields = $settings;
  64. global $language_files;
  65. $language_files[] = 'plugin_'.$this->get_name();
  66. }
  67. /**
  68. * Gets an array of information about this plugin (name, version, ...)
  69. * @return array Array of information elements about this plugin
  70. */
  71. public function get_info()
  72. {
  73. $result = array();
  74. $result['obj'] = $this;
  75. $result['title'] = $this->get_title();
  76. $result['comment'] = $this->get_comment();
  77. $result['version'] = $this->get_version();
  78. $result['author'] = $this->get_author();
  79. $result['plugin_class'] = get_class($this);
  80. $result['is_course_plugin'] = $this->isCoursePlugin;
  81. $result['is_admin_plugin'] = $this->isAdminPlugin;
  82. $result['is_mail_plugin'] = $this->isMailPlugin;
  83. if ($form = $this->get_settings_form()) {
  84. $result['settings_form'] = $form;
  85. foreach ($this->fields as $name => $type) {
  86. $value = $this->get($name);
  87. if (is_array($type)) {
  88. $value = $type['options'];
  89. }
  90. $result[$name] = $value;
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * Returns the "system" name of the plugin in lowercase letters
  97. * @return string
  98. */
  99. public function get_name()
  100. {
  101. $result = get_class($this);
  102. $result = str_replace('Plugin', '', $result);
  103. $result = strtolower($result);
  104. return $result;
  105. }
  106. /**
  107. * @return string
  108. */
  109. public function getCamelCaseName()
  110. {
  111. $result = get_class($this);
  112. return str_replace('Plugin', '', $result);
  113. }
  114. /**
  115. * Returns the title of the plugin
  116. * @return string
  117. */
  118. public function get_title()
  119. {
  120. return $this->get_lang('plugin_title');
  121. }
  122. /**
  123. * Returns the description of the plugin
  124. * @return string
  125. */
  126. public function get_comment()
  127. {
  128. return $this->get_lang('plugin_comment');
  129. }
  130. /**
  131. * Returns the version of the plugin
  132. * @return string
  133. */
  134. public function get_version()
  135. {
  136. return $this->version;
  137. }
  138. /**
  139. * Returns the author of the plugin
  140. * @return string
  141. */
  142. public function get_author()
  143. {
  144. return $this->author;
  145. }
  146. /**
  147. * Returns the contents of the CSS defined by the plugin
  148. * @return string
  149. */
  150. public function get_css()
  151. {
  152. $name = $this->get_name();
  153. $path = api_get_path(SYS_PLUGIN_PATH)."$name/resources/$name.css";
  154. if (!is_readable($path)) {
  155. return '';
  156. }
  157. $css = array();
  158. $css[] = file_get_contents($path);
  159. $result = implode($css);
  160. return $result;
  161. }
  162. /**
  163. * Returns an HTML form (generated by FormValidator) of the plugin settings
  164. * @return FormValidator FormValidator-generated form
  165. */
  166. public function get_settings_form()
  167. {
  168. $result = new FormValidator($this->get_name());
  169. $defaults = array();
  170. $checkboxGroup = array();
  171. $checkboxCollection = array();
  172. if ($checkboxNames = array_keys($this->fields, 'checkbox')) {
  173. $pluginInfoCollection = api_get_settings('Plugins');
  174. foreach ($pluginInfoCollection as $pluginInfo) {
  175. if (array_search($pluginInfo['title'], $checkboxNames) !== false) {
  176. $checkboxCollection[$pluginInfo['title']] = $pluginInfo;
  177. }
  178. }
  179. }
  180. foreach ($this->fields as $name => $type) {
  181. $options = null;
  182. if (is_array($type) && isset($type['type']) && $type['type'] === 'select') {
  183. $attributes = isset($type['attributes']) ? $type['attributes'] : [];
  184. $options = $type['options'];
  185. $type = $type['type'];
  186. }
  187. $value = $this->get($name);
  188. $defaults[$name] = $value;
  189. $type = isset($type) ? $type : 'text';
  190. $help = null;
  191. if ($this->get_lang_plugin_exists($name.'_help')) {
  192. $help = $this->get_lang($name.'_help');
  193. if ($name === "show_main_menu_tab") {
  194. $pluginName = strtolower(str_replace('Plugin', '', get_class($this)));
  195. $pluginUrl = api_get_path(WEB_PATH)."plugin/$pluginName/index.php";
  196. $pluginUrl = "<a href=$pluginUrl>$pluginUrl</a>";
  197. $help = sprintf($help, $pluginUrl);
  198. }
  199. }
  200. switch ($type) {
  201. case 'html':
  202. $result->addElement('html', $this->get_lang($name));
  203. break;
  204. case 'wysiwyg':
  205. $result->addHtmlEditor($name, $this->get_lang($name), false);
  206. break;
  207. case 'text':
  208. $result->addElement($type, $name, array($this->get_lang($name), $help));
  209. break;
  210. case 'boolean':
  211. $group = array();
  212. $group[] = $result->createElement(
  213. 'radio',
  214. $name,
  215. '',
  216. get_lang('Yes'),
  217. 'true'
  218. );
  219. $group[] = $result->createElement(
  220. 'radio',
  221. $name,
  222. '',
  223. get_lang('No'),
  224. 'false'
  225. );
  226. $result->addGroup($group, null, array($this->get_lang($name), $help));
  227. break;
  228. case 'checkbox':
  229. $selectedValue = null;
  230. if (isset($checkboxCollection[$name])) {
  231. if ($checkboxCollection[$name]['selected_value'] === 'true') {
  232. $selectedValue = 'checked';
  233. }
  234. }
  235. $element = $result->createElement(
  236. $type,
  237. $name,
  238. '',
  239. $this->get_lang($name),
  240. $selectedValue
  241. );
  242. $element->_attributes['value'] = 'true';
  243. $checkboxGroup[] = $element;
  244. break;
  245. case 'select':
  246. $result->addElement(
  247. $type,
  248. $name,
  249. array($this->get_lang($name), $help),
  250. $options,
  251. $attributes
  252. );
  253. break;
  254. }
  255. }
  256. if (!empty($checkboxGroup)) {
  257. $result->addGroup(
  258. $checkboxGroup,
  259. null,
  260. array($this->get_lang('sms_types'), $help)
  261. );
  262. }
  263. $result->setDefaults($defaults);
  264. $result->addButtonSave($this->get_lang('Save'), 'submit_button');
  265. return $result;
  266. }
  267. /**
  268. * Returns the value of a given plugin global setting
  269. * @param string $name of the plugin
  270. *
  271. * @return string Value of the plugin
  272. */
  273. public function get($name)
  274. {
  275. $settings = $this->get_settings();
  276. foreach ($settings as $setting) {
  277. if ($setting['variable'] == $this->get_name().'_'.$name) {
  278. if (!empty($setting['selected_value']) &&
  279. @unserialize($setting['selected_value']) !== false
  280. ) {
  281. $setting['selected_value'] = unserialize($setting['selected_value']);
  282. }
  283. return $setting['selected_value'];
  284. }
  285. }
  286. return false;
  287. }
  288. /**
  289. * Returns an array with the global settings for this plugin
  290. * @param bool $forceFromDB Optional. Force get settings from the database
  291. * @return array Plugin settings as an array
  292. */
  293. public function get_settings($forceFromDB = false)
  294. {
  295. if (empty($this->settings) || $forceFromDB) {
  296. $settings = api_get_settings_params(
  297. array(
  298. "subkey = ? AND category = ? AND type = ? AND access_url = ?" => array(
  299. $this->get_name(),
  300. 'Plugins',
  301. 'setting',
  302. api_get_current_access_url_id()
  303. )
  304. )
  305. );
  306. $this->settings = $settings;
  307. }
  308. return $this->settings;
  309. }
  310. /**
  311. * Tells whether language variables are defined for this plugin or not
  312. * @param string $name System name of the plugin
  313. *
  314. * @return bool True if the plugin has language variables defined, false otherwise
  315. */
  316. public function get_lang_plugin_exists($name)
  317. {
  318. return isset($this->strings[$name]);
  319. }
  320. /**
  321. * Hook for the get_lang() function to check for plugin-defined language terms
  322. * @param string $name of the language variable we are looking for
  323. *
  324. * @return string The translated language term of the plugin
  325. */
  326. public function get_lang($name)
  327. {
  328. // Check whether the language strings for the plugin have already been
  329. // loaded. If so, no need to load them again.
  330. if (is_null($this->strings)) {
  331. $root = api_get_path(SYS_PLUGIN_PATH);
  332. $plugin_name = $this->get_name();
  333. $language_interface = api_get_language_isocode();
  334. //1. Loading english if exists
  335. $english_path = $root.$plugin_name."/lang/en.php";
  336. if (is_readable($english_path)) {
  337. $strings = array();
  338. include $english_path;
  339. $this->strings = $strings;
  340. }
  341. $path = $root.$plugin_name."/lang/$language_interface.php";
  342. // 2. Loading the system language
  343. if (is_readable($path)) {
  344. include $path;
  345. if (!empty($strings)) {
  346. foreach ($strings as $key => $string) {
  347. $this->strings[$key] = $string;
  348. }
  349. }
  350. } elseif ($languageParentId > 0) {
  351. $languageParentInfo = api_get_language_info($languageParentId);
  352. $languageParentFolder = $languageParentInfo['dokeos_folder'];
  353. $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php";
  354. if (is_readable($parentPath)) {
  355. include $parentPath;
  356. if (!empty($strings)) {
  357. foreach ($strings as $key => $string) {
  358. $this->strings[$key] = $string;
  359. }
  360. }
  361. }
  362. }
  363. }
  364. if (isset($this->strings[$name])) {
  365. return $this->strings[$name];
  366. }
  367. return get_lang($name);
  368. }
  369. /**
  370. * Caller for the install_course_fields() function
  371. * @param int $courseId
  372. *
  373. * @param boolean $addToolLink Whether to add a tool link on the course homepage
  374. *
  375. * @return void
  376. */
  377. public function course_install($courseId, $addToolLink = true)
  378. {
  379. $this->install_course_fields($courseId, $addToolLink);
  380. }
  381. /**
  382. * Add course settings and, if not asked otherwise, add a tool link on the course homepage
  383. * @param int $courseId Course integer ID
  384. * @param boolean $add_tool_link Whether to add a tool link or not
  385. * (some tools might just offer a configuration section and act on the backend)
  386. *
  387. * @return boolean|null False on error, null otherwise
  388. */
  389. public function install_course_fields($courseId, $add_tool_link = true)
  390. {
  391. $plugin_name = $this->get_name();
  392. $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
  393. $courseId = (int) $courseId;
  394. if (empty($courseId)) {
  395. return false;
  396. }
  397. // Adding course settings.
  398. if (!empty($this->course_settings)) {
  399. foreach ($this->course_settings as $setting) {
  400. $variable = $setting['name'];
  401. $value = '';
  402. if (isset($setting['init_value'])) {
  403. $value = $setting['init_value'];
  404. }
  405. $type = 'textfield';
  406. if (isset($setting['type'])) {
  407. $type = $setting['type'];
  408. }
  409. if (isset($setting['group'])) {
  410. $group = $setting['group'];
  411. $sql = "SELECT value
  412. FROM $t_course
  413. WHERE
  414. c_id = $courseId AND
  415. variable = '".Database::escape_string($group)."' AND
  416. subkey = '".Database::escape_string($variable)."'
  417. ";
  418. $result = Database::query($sql);
  419. if (!Database::num_rows($result)) {
  420. $params = [
  421. 'c_id' => $courseId,
  422. 'variable' => $group,
  423. 'subkey' => $variable,
  424. 'value' => $value,
  425. 'category' => 'plugins',
  426. 'type' => $type,
  427. 'title' => ''
  428. ];
  429. Database::insert($t_course, $params);
  430. }
  431. } else {
  432. $sql = "SELECT value FROM $t_course
  433. WHERE c_id = $courseId AND variable = '$variable' ";
  434. $result = Database::query($sql);
  435. if (!Database::num_rows($result)) {
  436. $params = [
  437. 'c_id' => $courseId,
  438. 'variable' => $variable,
  439. 'subkey' => $plugin_name,
  440. 'value' => $value,
  441. 'category' => 'plugins',
  442. 'type' => $type,
  443. 'title' => ''
  444. ];
  445. Database::insert($t_course, $params);
  446. }
  447. }
  448. }
  449. }
  450. // Stop here if we don't want a tool link on the course homepage
  451. if (!$add_tool_link || $this->addCourseTool == false) {
  452. return true;
  453. }
  454. //Add an icon in the table tool list
  455. $this->createLinkToCourseTool($plugin_name, $courseId);
  456. }
  457. /**
  458. * Delete the fields added to the course settings page and the link to the
  459. * tool on the course's homepage
  460. * @param int $courseId
  461. *
  462. * @return false|null
  463. */
  464. public function uninstall_course_fields($courseId)
  465. {
  466. $courseId = intval($courseId);
  467. if (empty($courseId)) {
  468. return false;
  469. }
  470. $plugin_name = $this->get_name();
  471. $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
  472. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  473. if (!empty($this->course_settings)) {
  474. foreach ($this->course_settings as $setting) {
  475. $variable = Database::escape_string($setting['name']);
  476. if (!empty($setting['group'])) {
  477. $variable = Database::escape_string($setting['group']);
  478. }
  479. if (empty($variable)) {
  480. continue;
  481. }
  482. $sql = "DELETE FROM $t_course
  483. WHERE c_id = $courseId AND variable = '$variable'";
  484. Database::query($sql);
  485. }
  486. }
  487. $plugin_name = Database::escape_string($plugin_name);
  488. $sql = "DELETE FROM $t_tool
  489. WHERE c_id = $courseId AND name = '$plugin_name'";
  490. Database::query($sql);
  491. }
  492. /**
  493. * Add an link for a course tool
  494. * @param string $name The tool name
  495. * @param int $courseId The course ID
  496. * @param string $iconName Optional. Icon file name
  497. * @param string $link Optional. Link URL
  498. * @return CTool|null
  499. */
  500. protected function createLinkToCourseTool(
  501. $name,
  502. $courseId,
  503. $iconName = null,
  504. $link = null
  505. ) {
  506. if (!$this->addCourseTool) {
  507. return null;
  508. }
  509. $em = Database::getManager();
  510. /** @var CTool $tool */
  511. $tool = $em
  512. ->getRepository('ChamiloCourseBundle:CTool')
  513. ->findOneBy([
  514. 'name' => $name,
  515. 'cId' => $courseId
  516. ]);
  517. if (!$tool) {
  518. $cToolId = AddCourse::generateToolId($courseId);
  519. $pluginName = $this->get_name();
  520. $tool = new CTool();
  521. $tool
  522. ->setId($cToolId)
  523. ->setCId($courseId)
  524. ->setName($name)
  525. ->setLink($link ?: "$pluginName/start.php")
  526. ->setImage($iconName ?: "$pluginName.png")
  527. ->setVisibility(true)
  528. ->setAdmin(0)
  529. ->setAddress('squaregrey.gif')
  530. ->setAddedTool(false)
  531. ->setTarget('_self')
  532. ->setCategory('plugin')
  533. ->setSessionId(0);
  534. $em->persist($tool);
  535. $em->flush();
  536. }
  537. return $tool;
  538. }
  539. /**
  540. * Install the course fields and tool link of this plugin in all courses
  541. * @param boolean $add_tool_link Whether we want to add a plugin link on the course homepage
  542. *
  543. * @return void
  544. */
  545. public function install_course_fields_in_all_courses($add_tool_link = true)
  546. {
  547. // Update existing courses to add plugin settings
  548. $t_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  549. $sql = "SELECT id FROM $t_courses ORDER BY id";
  550. $res = Database::query($sql);
  551. while ($row = Database::fetch_assoc($res)) {
  552. $this->install_course_fields($row['id'], $add_tool_link);
  553. }
  554. }
  555. /**
  556. * Uninstall the plugin settings fields from all courses
  557. * @return void
  558. */
  559. public function uninstall_course_fields_in_all_courses()
  560. {
  561. // Update existing courses to add conference settings
  562. $t_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  563. $sql = "SELECT id FROM $t_courses
  564. ORDER BY id";
  565. $res = Database::query($sql);
  566. while ($row = Database::fetch_assoc($res)) {
  567. $this->uninstall_course_fields($row['id']);
  568. }
  569. }
  570. /**
  571. * @return array
  572. */
  573. public function getCourseSettings()
  574. {
  575. $settings = array();
  576. if (is_array($this->course_settings)) {
  577. foreach ($this->course_settings as $item) {
  578. if (isset($item['group'])) {
  579. if (!in_array($item['group'], $settings)) {
  580. $settings[] = $item['group'];
  581. }
  582. } else {
  583. $settings[] = $item['name'];
  584. }
  585. }
  586. }
  587. return $settings;
  588. }
  589. /**
  590. * Method to be extended when changing the setting in the course
  591. * configuration should trigger the use of a callback method
  592. * @param array $values sent back from the course configuration script
  593. *
  594. * @return void
  595. */
  596. public function course_settings_updated($values = array())
  597. {
  598. }
  599. /**
  600. * Add a tab to platform
  601. * @param string $tabName
  602. * @param string $url
  603. * @param string $userFilter Optional. Filter tab type
  604. * @return false|string
  605. */
  606. public function addTab($tabName, $url, $userFilter = null)
  607. {
  608. $sql = "SELECT * FROM settings_current
  609. WHERE
  610. variable = 'show_tabs' AND
  611. subkey LIKE 'custom_tab_%'";
  612. $result = Database::query($sql);
  613. $customTabsNum = Database::num_rows($result);
  614. $tabNum = $customTabsNum + 1;
  615. // Avoid Tab Name Spaces
  616. $tabNameNoSpaces = preg_replace('/\s+/', '', $tabName);
  617. $subkeytext = "Tabs".$tabNameNoSpaces;
  618. // Check if it is already added
  619. $checkCondition = array(
  620. 'where' =>
  621. array(
  622. "variable = 'show_tabs' AND subkeytext = ?" => array(
  623. $subkeytext
  624. )
  625. )
  626. );
  627. $checkDuplicate = Database::select('*', 'settings_current', $checkCondition);
  628. if (!empty($checkDuplicate)) {
  629. return false;
  630. }
  631. // End Check
  632. $subkey = 'custom_tab_'.$tabNum;
  633. if (!empty($userFilter)) {
  634. switch ($userFilter) {
  635. case self::TAB_FILTER_NO_STUDENT:
  636. //no break
  637. case self::TAB_FILTER_ONLY_STUDENT:
  638. $subkey .= $userFilter;
  639. break;
  640. }
  641. }
  642. $attributes = array(
  643. 'variable' => 'show_tabs',
  644. 'subkey' => $subkey,
  645. 'type' => 'checkbox',
  646. 'category' => 'Platform',
  647. 'selected_value' => 'true',
  648. 'title' => $tabName,
  649. 'comment' => $url,
  650. 'subkeytext' => $subkeytext,
  651. 'access_url' => 1,
  652. 'access_url_changeable' => 0,
  653. 'access_url_locked' => 0
  654. );
  655. $resp = Database::insert('settings_current', $attributes);
  656. // Save the id
  657. $settings = $this->get_settings();
  658. $setData = array(
  659. 'comment' => $subkey
  660. );
  661. $whereCondition = array(
  662. 'id = ?' => key($settings)
  663. );
  664. Database::update('settings_current', $setData, $whereCondition);
  665. return $resp;
  666. }
  667. /**
  668. * Delete a tab to chamilo's platform
  669. * @param string $key
  670. * @return boolean $resp Transaction response
  671. */
  672. public function deleteTab($key)
  673. {
  674. $t = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  675. $sql = "SELECT *
  676. FROM $t
  677. WHERE variable = 'show_tabs'
  678. AND subkey <> '$key'
  679. AND subkey like 'custom_tab_%'
  680. ";
  681. $resp = $result = Database::query($sql);
  682. $customTabsNum = Database::num_rows($result);
  683. if (!empty($key)) {
  684. $whereCondition = array(
  685. 'variable = ? AND subkey = ?' => array('show_tabs', $key)
  686. );
  687. $resp = Database::delete('settings_current', $whereCondition);
  688. //if there is more than one tab
  689. //re enumerate them
  690. if (!empty($customTabsNum) && $customTabsNum > 0) {
  691. $tabs = Database::store_result($result, 'ASSOC');
  692. $i = 1;
  693. foreach ($tabs as $row) {
  694. $newSubKey = "custom_tab_$i";
  695. if (strpos($row['subkey'], self::TAB_FILTER_NO_STUDENT) !== false) {
  696. $newSubKey .= self::TAB_FILTER_NO_STUDENT;
  697. } elseif (strpos($row['subkey'], self::TAB_FILTER_ONLY_STUDENT) !== false) {
  698. $newSubKey .= self::TAB_FILTER_ONLY_STUDENT;
  699. }
  700. $attributes = ['subkey' => $newSubKey];
  701. $this->updateTab($row['subkey'], $attributes);
  702. $i++;
  703. }
  704. }
  705. }
  706. return $resp;
  707. }
  708. /**
  709. * Update the tabs attributes
  710. * @param string $key
  711. * @param array $attributes
  712. *
  713. * @return boolean
  714. */
  715. public function updateTab($key, $attributes)
  716. {
  717. $whereCondition = array(
  718. 'variable = ? AND subkey = ?' => array('show_tabs', $key)
  719. );
  720. $resp = Database::update('settings_current', $attributes, $whereCondition);
  721. return $resp;
  722. }
  723. /**
  724. * This method shows or hides plugin's tab
  725. * @param boolean $showTab Shows or hides the main menu plugin tab
  726. * @param string $filePath Plugin starter file path
  727. */
  728. public function manageTab($showTab, $filePath = 'index.php')
  729. {
  730. $langString = str_replace('Plugin', '', get_class($this));
  731. $pluginName = strtolower($langString);
  732. $pluginUrl = 'plugin/'.$pluginName.'/'.$filePath;
  733. if ($showTab === 'true') {
  734. $tabAdded = $this->addTab($langString, $pluginUrl);
  735. if ($tabAdded) {
  736. // The page must be refreshed to show the recently created tab
  737. echo "<script>location.href = '".Security::remove_XSS($_SERVER['REQUEST_URI'])."';</script>";
  738. }
  739. } else {
  740. $settingsCurrentTable = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  741. $conditions = array(
  742. 'where' => array(
  743. "variable = 'show_tabs' AND title = ? AND comment = ? " => array(
  744. $langString,
  745. $pluginUrl
  746. )
  747. )
  748. );
  749. $result = Database::select('subkey', $settingsCurrentTable, $conditions);
  750. if (!empty($result)) {
  751. $this->deleteTab($result[0]['subkey']);
  752. }
  753. }
  754. }
  755. /**
  756. * @param string $variable
  757. * @return bool
  758. */
  759. public function validateCourseSetting($variable)
  760. {
  761. return true;
  762. }
  763. /**
  764. * @param string $region
  765. * @return string
  766. */
  767. public function renderRegion($region)
  768. {
  769. return '';
  770. }
  771. /**
  772. * Returns true if the plugin is installed, false otherwise
  773. * @return bool True if plugin is installed/enabled, false otherwise
  774. */
  775. public function isEnabled()
  776. {
  777. $settings = api_get_settings_params_simple(
  778. array(
  779. "subkey = ? AND category = ? AND type = ? AND variable = 'status' " => array($this->get_name(), 'Plugins', 'setting')
  780. )
  781. );
  782. if (is_array($settings) && isset($settings['selected_value']) && $settings['selected_value'] == 'installed') {
  783. return true;
  784. }
  785. return false;
  786. }
  787. /**
  788. * Allow make some actions after configure the plugin parameters
  789. * This function is called from main/admin/configure_plugin.php page
  790. * when saving the plugin parameters
  791. * @return \Plugin
  792. */
  793. public function performActionsAfterConfigure()
  794. {
  795. return $this;
  796. }
  797. }