plugin.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Plugin
  5. * Base class for plugins
  6. *
  7. * This class has to be extended by every plugin. It defines basic methods
  8. * to install/uninstall and get information about a plugin
  9. *
  10. * @author Julio Montoya <gugli100@gmail.com>
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. * @author Laurent Opprecht <laurent@opprecht.info>
  13. * @copyright 2012 University of Geneva
  14. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  15. *
  16. */
  17. class Plugin
  18. {
  19. protected $version = '';
  20. protected $author = '';
  21. protected $fields = array();
  22. private $settings = null;
  23. // Translation strings.
  24. private $strings = null;
  25. public $isCoursePlugin = false;
  26. /**
  27. * When creating a new course, these settings are added to the course, in
  28. * the course_info/infocours.php
  29. * To show the plugin course icons you need to add these icons:
  30. * main/img/icons/22/plugin_name.png
  31. * main/img/icons/64/plugin_name.png
  32. * main/img/icons/64/plugin_name_na.png
  33. * @example
  34. * $course_settings = array(
  35. array('name' => 'big_blue_button_welcome_message', 'type' => 'text'),
  36. array('name' => 'big_blue_button_record_and_store', 'type' => 'checkbox')
  37. );
  38. */
  39. public $course_settings = array();
  40. /**
  41. * This indicates whether changing the setting should execute the callback
  42. * function.
  43. */
  44. public $course_settings_callback = false;
  45. /**
  46. * Default constructor for the plugin class. By default, it only sets
  47. * a few attributes of the object
  48. * @param string $version of this plugin
  49. * @param string $author of this plugin
  50. * @param array $settings settings to be proposed to configure the plugin
  51. */
  52. protected function __construct($version, $author, $settings = array())
  53. {
  54. $this->version = $version;
  55. $this->author = $author;
  56. $this->fields = $settings;
  57. global $language_files;
  58. $language_files[] = 'plugin_' . $this->get_name();
  59. }
  60. /**
  61. * Gets an array of information about this plugin (name, version, ...)
  62. * @return array Array of information elements about this plugin
  63. */
  64. public function get_info()
  65. {
  66. $result = array();
  67. $result['title'] = $this->get_title();
  68. $result['comment'] = $this->get_comment();
  69. $result['version'] = $this->get_version();
  70. $result['author'] = $this->get_author();
  71. $result['plugin_class'] = get_class($this);
  72. $result['is_course_plugin'] = $this->isCoursePlugin;
  73. if ($form = $this->get_settings_form()) {
  74. $result['settings_form'] = $form;
  75. foreach ($this->fields as $name => $type) {
  76. $value = $this->get($name);
  77. $result[$name] = $value;
  78. }
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Returns the "system" name of the plugin in lowercase letters
  84. * @return string
  85. */
  86. public function get_name()
  87. {
  88. $result = get_class($this);
  89. $result = str_replace('Plugin', '', $result);
  90. $result = strtolower($result);
  91. return $result;
  92. }
  93. /**
  94. * Returns the title of the plugin
  95. * @return string
  96. */
  97. public function get_title()
  98. {
  99. return $this->get_lang('plugin_title');
  100. }
  101. /**
  102. * Returns the description of the plugin
  103. * @return string
  104. */
  105. public function get_comment()
  106. {
  107. return $this->get_lang('plugin_comment');
  108. }
  109. /**
  110. * Returns the version of the plugin
  111. * @return string
  112. */
  113. public function get_version()
  114. {
  115. return $this->version;
  116. }
  117. /**
  118. * Returns the author of the plugin
  119. * @return string
  120. */
  121. public function get_author()
  122. {
  123. return $this->author;
  124. }
  125. /**
  126. * Returns the contents of the CSS defined by the plugin
  127. * @return array
  128. */
  129. public function get_css()
  130. {
  131. $name = $this->get_name();
  132. $path = api_get_path(SYS_PLUGIN_PATH)."$name/resources/$name.css";
  133. if (!is_readable($path)) {
  134. return '';
  135. }
  136. $css = array();
  137. $css[] = file_get_contents($path);
  138. $result = implode($css);
  139. return $result;
  140. }
  141. /**
  142. * Returns an HTML form (generated by FormValidator) of the plugin settings
  143. * @return string FormValidator-generated form
  144. */
  145. public function get_settings_form()
  146. {
  147. $result = new FormValidator($this->get_name());
  148. $defaults = array();
  149. foreach ($this->fields as $name => $type) {
  150. $value = $this->get($name);
  151. $defaults[$name] = $value;
  152. $type = isset($type) ? $type : 'text';
  153. $help = null;
  154. if ($this->get_lang_plugin_exists($name.'_help')) {
  155. $help = $this->get_lang($name.'_help');
  156. }
  157. switch ($type) {
  158. case 'html':
  159. $result->addElement('html', $this->get_lang($name));
  160. break;
  161. case 'wysiwyg':
  162. $result->add_html_editor($name, $this->get_lang($name));
  163. break;
  164. case 'text':
  165. $result->addElement($type, $name, array($this->get_lang($name), $help));
  166. break;
  167. case 'boolean':
  168. $group = array();
  169. $group[] = $result->createElement('radio', $name, '', get_lang('Yes'), 'true');
  170. $group[] = $result->createElement('radio', $name, '', get_lang('No'), 'false');
  171. $result->addGroup($group, null, array($this->get_lang($name), $help));
  172. break;
  173. }
  174. }
  175. $result->setDefaults($defaults);
  176. $result->addElement('style_submit_button', 'submit_button', $this->get_lang('Save'));
  177. return $result;
  178. }
  179. /**
  180. * Returns the value of a given plugin global setting
  181. * @param string $name of the plugin
  182. *
  183. * @return string Value of the plugin
  184. */
  185. public function get($name)
  186. {
  187. $settings = $this->get_settings();
  188. foreach ($settings as $setting) {
  189. if ($setting['variable'] == ($this->get_name() . '_' . $name)) {
  190. return $setting['selected_value'];
  191. }
  192. }
  193. return false;
  194. }
  195. /**
  196. * Returns an array with the global settings for this plugin
  197. * @return array Plugin settings as an array
  198. */
  199. public function get_settings()
  200. {
  201. if (is_null($this->settings)) {
  202. $settings = api_get_settings_params(
  203. array(
  204. "subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting')
  205. )
  206. );
  207. $this->settings = $settings;
  208. }
  209. return $this->settings;
  210. }
  211. /**
  212. * Tells whether language variables are defined for this plugin or not
  213. * @param string $name System name of the plugin
  214. *
  215. * @return bool True if the plugin has language variables defined, false otherwise
  216. */
  217. public function get_lang_plugin_exists($name)
  218. {
  219. return isset($this->strings[$name]);
  220. }
  221. /**
  222. * Hook for the get_lang() function to check for plugin-defined language terms
  223. * @param string $name of the language variable we are looking for
  224. *
  225. * @return string The translated language term of the plugin
  226. */
  227. public function get_lang($name)
  228. {
  229. // Check whether the language strings for the plugin have already been
  230. // loaded. If so, no need to load them again.
  231. if (is_null($this->strings)) {
  232. global $language_interface;
  233. $root = api_get_path(SYS_PLUGIN_PATH);
  234. $plugin_name = $this->get_name();
  235. //1. Loading english if exists
  236. $english_path = $root.$plugin_name."/lang/english.php";
  237. if (is_readable($english_path)) {
  238. include $english_path;
  239. $this->strings = $strings;
  240. }
  241. $path = $root.$plugin_name."/lang/$language_interface.php";
  242. //2. Loading the system language
  243. if (is_readable($path)) {
  244. include $path;
  245. if (!empty($strings)) {
  246. foreach ($strings as $key => $string) {
  247. $this->strings[$key] = $string;
  248. }
  249. }
  250. }
  251. }
  252. if (isset($this->strings[$name])) {
  253. return $this->strings[$name];
  254. }
  255. return get_lang($name);
  256. }
  257. /**
  258. * Caller for the install_course_fields() function
  259. * @param int $courseId
  260. * @param boolean $addToolLink Whether to add a tool link on the course homepage
  261. *
  262. * @return void
  263. */
  264. public function course_install($courseId, $addToolLink = true)
  265. {
  266. $this->install_course_fields($courseId, $addToolLink);
  267. }
  268. /**
  269. * Add course settings and, if not asked otherwise, add a tool link on the course homepage
  270. * @param int $courseId Course integer ID
  271. * @param boolean $add_tool_link Whether to add a tool link or not
  272. * (some tools might just offer a configuration section and act on the backend)
  273. * @return boolean False on error, null otherwise
  274. */
  275. public function install_course_fields($courseId, $add_tool_link = true)
  276. {
  277. $plugin_name = $this->get_name();
  278. $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
  279. $courseId = intval($courseId);
  280. if (empty($courseId)) {
  281. return false;
  282. }
  283. // Adding course settings.
  284. if (!empty($this->course_settings)) {
  285. foreach ($this->course_settings as $setting) {
  286. $variable = Database::escape_string($setting['name']);
  287. $value ='';
  288. if (isset($setting['init_value'])) {
  289. $value = Database::escape_string($setting['init_value']);
  290. }
  291. $type = 'textfield';
  292. if (isset($setting['type'])) {
  293. $type = Database::escape_string($setting['type']);
  294. }
  295. if (isset($setting['group'])) {
  296. $group = Database::escape_string($setting['group']);
  297. $sql = "SELECT value FROM $t_course
  298. WHERE c_id = $courseId AND variable = '$group' AND subkey = '$variable' ";
  299. $result = Database::query($sql);
  300. if (!Database::num_rows($result)) {
  301. $sql = "INSERT INTO $t_course (c_id, variable, subkey, value, category, type) VALUES
  302. ($courseId, '$group', '$variable', '$value', 'plugins', '$type')";
  303. Database::query($sql);
  304. }
  305. } else {
  306. $sql = "SELECT value FROM $t_course
  307. WHERE c_id = $courseId AND variable = '$variable' ";
  308. $result = Database::query($sql);
  309. if (!Database::num_rows($result)) {
  310. $sql = "INSERT INTO $t_course (c_id, variable, value, category, subkey, type) VALUES
  311. ($courseId, '$variable','$value', 'plugins', '$plugin_name', '$type')";
  312. Database::query($sql);
  313. }
  314. }
  315. }
  316. }
  317. // Stop here if we don't want a tool link on the course homepage
  318. if (!$add_tool_link) {
  319. return true;
  320. }
  321. //Add an icon in the table tool list
  322. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  323. $sql = "SELECT name FROM $t_tool
  324. WHERE c_id = $courseId AND name = '$plugin_name' ";
  325. $result = Database::query($sql);
  326. if (!Database::num_rows($result)) {
  327. $tool_link = "$plugin_name/start.php";
  328. $visibility = string2binary(api_get_setting('course.course_create_active_tools', $plugin_name));
  329. $sql = "INSERT INTO $t_tool VALUES
  330. ($courseId, NULL, '$plugin_name', '$tool_link', '$plugin_name.png',' ".$visibility."','0', 'squaregrey.gif','NO','_self','plugin','0')";
  331. Database::query($sql);
  332. }
  333. }
  334. /**
  335. * Delete the fields added to the course settings page and the link to the
  336. * tool on the course's homepage
  337. * @param int $courseId
  338. * @return void
  339. */
  340. public function uninstall_course_fields($courseId)
  341. {
  342. $courseId = intval($courseId);
  343. if (empty($courseId)) {
  344. return false;
  345. }
  346. $plugin_name = $this->get_name();
  347. $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
  348. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  349. if (!empty($this->course_settings)) {
  350. foreach ($this->course_settings as $setting) {
  351. $variable = Database::escape_string($setting['name']);
  352. if (!empty($setting['group'])) {
  353. $variable = Database::escape_string($setting['group']);
  354. }
  355. if (empty($variable)) {
  356. continue;
  357. }
  358. $sql = "DELETE FROM $t_course
  359. WHERE c_id = $courseId AND variable = '$variable'";
  360. Database::query($sql);
  361. }
  362. }
  363. $plugin_name = Database::escape_string($plugin_name);
  364. $sql = "DELETE FROM $t_tool
  365. WHERE c_id = $courseId AND name = '$plugin_name'";
  366. Database::query($sql);
  367. }
  368. /**
  369. * Install the course fields and tool link of this plugin in all courses
  370. * @param boolean Whether we want to add a plugin link on the course homepage
  371. *
  372. * @return void
  373. */
  374. public function install_course_fields_in_all_courses($add_tool_link = true)
  375. {
  376. // Update existing courses to add conference settings
  377. $t_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  378. $sql = "SELECT id FROM $t_courses ORDER BY id";
  379. $res = Database::query($sql);
  380. while ($row = Database::fetch_assoc($res)) {
  381. $this->install_course_fields($row['id'], $add_tool_link);
  382. }
  383. }
  384. /**
  385. * Uninstall the plugin settings fields from all courses
  386. * @return void
  387. */
  388. public function uninstall_course_fields_in_all_courses()
  389. {
  390. // Update existing courses to add conference settings
  391. $t_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  392. $sql = "SELECT id FROM $t_courses ORDER BY id";
  393. $res = Database::query($sql);
  394. while ($row = Database::fetch_assoc($res)) {
  395. $this->uninstall_course_fields($row['id']);
  396. }
  397. }
  398. /**
  399. * @return array
  400. */
  401. public function getCourseSettings()
  402. {
  403. $settings = array();
  404. if (is_array($this->course_settings)) {
  405. foreach ($this->course_settings as $item) {
  406. if (isset($item['group'])) {
  407. if (!in_array($item['group'], $settings)) {
  408. $settings[] = $item['group'];
  409. }
  410. } else {
  411. $settings[] = $item['name'];
  412. }
  413. }
  414. }
  415. return $settings;
  416. }
  417. /**
  418. * Method to be extended when changing the setting in the course
  419. * configuration should trigger the use of a callback method
  420. * @param array $values sent back from the course configuration script
  421. *
  422. * @return void
  423. */
  424. public function course_settings_updated($values = array())
  425. {
  426. }
  427. /**
  428. * Add a tab to platform
  429. * @param string $tabName
  430. * @param string $url
  431. *
  432. * @return boolean
  433. */
  434. public function addTab($tabName, $url)
  435. {
  436. $sql = "SELECT * FROM settings_current
  437. WHERE
  438. variable = 'show_tabs' AND
  439. subkey like 'custom_tab_%'";
  440. $result = Database::query($sql);
  441. $customTabsNum = Database::num_rows($result);
  442. $tabNum = $customTabsNum + 1;
  443. //Avoid Tab Name Spaces
  444. $tabNameNoSpaces = preg_replace('/\s+/', '', $tabName);
  445. $subkeytext = "Tabs" . $tabNameNoSpaces;
  446. //Check if it is already added
  447. $checkCondition = array(
  448. 'where' =>
  449. array(
  450. "variable = 'show_tabs' AND subkeytext = ?" => array(
  451. $subkeytext
  452. )
  453. )
  454. );
  455. $checkDuplicate = Database::select('*', 'settings_current', $checkCondition);
  456. if (!empty($checkDuplicate)) {
  457. return false;
  458. }
  459. //End Check
  460. $subkey = 'custom_tab_' . $tabNum;
  461. $attributes = array(
  462. 'variable' => 'show_tabs',
  463. 'subkey' => $subkey,
  464. 'type' => 'checkbox',
  465. 'category' => 'Platform',
  466. 'selected_value' => 'true',
  467. 'title' => $tabName,
  468. 'comment' => $url,
  469. 'subkeytext' => $subkeytext,
  470. 'access_url' => 1,
  471. 'access_url_changeable' => 0,
  472. 'access_url_locked' => 0
  473. );
  474. $resp = Database::insert('settings_current', $attributes);
  475. //Save the id
  476. $settings = $this->get_settings();
  477. $setData = array (
  478. 'comment' => $subkey
  479. );
  480. $whereCondition = array(
  481. 'id = ?' => key($settings)
  482. );
  483. Database::update('settings_current', $setData, $whereCondition);
  484. return $resp;
  485. }
  486. /**
  487. * Delete a tab to chamilo's platform
  488. * @param string $key
  489. * @return boolean $resp Transaction response
  490. */
  491. public function deleteTab($key)
  492. {
  493. $sql = "SELECT *
  494. FROM settings_current
  495. WHERE variable = 'show_tabs'
  496. AND subkey <> '$key'
  497. AND subkey like 'custom_tab_%'
  498. ";
  499. $resp = $result = Database::query($sql);
  500. $customTabsNum = Database::num_rows($result);
  501. if (!empty($key)) {
  502. $whereCondition = array(
  503. 'variable = ? AND subkey = ?' => array('show_tabs', $key)
  504. );
  505. $resp = Database::delete('settings_current', $whereCondition);
  506. //if there is more than one tab
  507. //re enumerate them
  508. if (!empty($customTabsNum) && $customTabsNum > 0) {
  509. $tabs = Database::store_result($result, 'ASSOC');
  510. $i = 1;
  511. foreach ($tabs as $row) {
  512. $attributes = array(
  513. 'subkey' => 'custom_tab_' . $i
  514. );
  515. $this->updateTab($row['subkey'], $attributes);
  516. $i++;
  517. }
  518. }
  519. }
  520. return $resp;
  521. }
  522. /**
  523. * Update the tabs attributes
  524. * @param string $key
  525. * @param array $attributes
  526. *
  527. * @return boolean
  528. */
  529. public function updateTab($key, $attributes)
  530. {
  531. $whereCondition = array(
  532. 'variable = ? AND subkey = ?' => array('show_tabs', $key)
  533. );
  534. $resp = Database::update('settings_current', $attributes, $whereCondition);
  535. return $resp;
  536. }
  537. }