PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/main/inc/lib/plugin.class.php

https://bitbucket.org/hanutimes/hanutimes
PHP | 397 lines | 230 code | 35 blank | 132 comment | 27 complexity | 967e85eb1b36d94d18ae2caf26300a2c MD5 | raw file
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Base class for plugins
  5. *
  6. * This class has to be extended by every plugin. It defines basic methods
  7. * to install/uninstall and get information about a plugin
  8. *
  9. * @copyright (c) 2012 University of Geneva
  10. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  11. * @author Laurent Opprecht <laurent@opprecht.info>
  12. * @author Julio Montoya <gugli100@gmail.com> added course settings support + lang variable fixes
  13. * @author Yannick Warnier <ywarnier@beeznest.org> added documentation
  14. *
  15. */
  16. class Plugin {
  17. protected $version = '';
  18. protected $author = '';
  19. protected $fields = array();
  20. private $settings = null;
  21. private $strings = null; //translation strings
  22. public $is_course_plugin = false;
  23. /**
  24. * When creating a new course, these settings are added to the course, in
  25. * the course_info/infocours.php
  26. * To show the plugin course icons you need to add these icons:
  27. * main/img/icons/22/plugin_name.png
  28. * main/img/icons/64/plugin_name.png
  29. * main/img/icons/64/plugin_name_na.png
  30. * @example
  31. * $course_settings = array(
  32. array('name' => 'big_blue_button_welcome_message', 'type' => 'text'),
  33. array('name' => 'big_blue_button_record_and_store', 'type' => 'checkbox')
  34. );
  35. */
  36. public $course_settings = array();
  37. /**
  38. * This indicates whether changing the setting should execute the callback
  39. * function.
  40. */
  41. public $course_settings_callback = false;
  42. /**
  43. * Default constructor for the plugin class. By default, it only sets
  44. * a few attributes of the object
  45. * @param string Version of this plugin
  46. * @param string Author of this plugin
  47. * @param array Array of global settings to be proposed to configure the plugin
  48. * @return void
  49. */
  50. protected function __construct($version, $author, $settings = array()) {
  51. $this->version = $version;
  52. $this->author = $author;
  53. $this->fields = $settings;
  54. global $language_files;
  55. $language_files[] = 'plugin_' . $this->get_name();
  56. }
  57. /**
  58. * Gets an array of information about this plugin (name, version, ...)
  59. * @return array Array of information elements about this plugin
  60. */
  61. function get_info() {
  62. $result = array();
  63. $result['title'] = $this->get_title();
  64. $result['comment'] = $this->get_comment();
  65. $result['version'] = $this->get_version();
  66. $result['author'] = $this->get_author();
  67. $result['plugin_class'] = get_class($this);
  68. $result['is_course_plugin'] = $this->is_course_plugin;
  69. if ($form = $this->get_settings_form()) {
  70. $result['settings_form'] = $form;
  71. foreach ($this->fields as $name => $type) {
  72. $value = $this->get($name);
  73. $result[$name] = $value;
  74. }
  75. }
  76. return $result;
  77. }
  78. /**
  79. * Returns the "system" name of the plugin in lowercase letters
  80. * @param string Name of the plugin
  81. */
  82. function get_name() {
  83. $result = get_class($this);
  84. $result = str_replace('Plugin', '', $result);
  85. $result = strtolower($result);
  86. return $result;
  87. }
  88. /**
  89. * Returns the title of the plugin
  90. * @param string Title of the plugin
  91. */
  92. function get_title() {
  93. return $this->get_lang('plugin_title');
  94. }
  95. /**
  96. * Returns the description of the plugin
  97. * @param string Description of the plugin
  98. */
  99. function get_comment() {
  100. return $this->get_lang('plugin_comment');
  101. }
  102. /**
  103. * Returns the version of the plugin
  104. * @param string Version of the plugin
  105. */
  106. function get_version() {
  107. return $this->version;
  108. }
  109. /**
  110. * Returns the author of the plugin
  111. * @param string Author(s) of the plugin
  112. */
  113. function get_author() {
  114. return $this->author;
  115. }
  116. /**
  117. * Returns the contents of the CSS defined by the plugin
  118. * @param string The CSS string
  119. */
  120. function get_css() {
  121. $name = $this->get_name();
  122. $path = api_get_path(SYS_PLUGIN_PATH)."$name/resources/$name.css";
  123. if (!is_readable($path)) {
  124. return '';
  125. }
  126. $css = array();
  127. $css[] = file_get_contents($path);
  128. $result = implode($css);
  129. return $result;
  130. }
  131. /**
  132. * Returns an HTML form (generated by FormValidator) of the plugin settings
  133. * @return string FormValidator-generated form
  134. */
  135. function get_settings_form() {
  136. $result = new FormValidator($this->get_name());
  137. $defaults = array();
  138. foreach ($this->fields as $name => $type) {
  139. $value = $this->get($name);
  140. $defaults[$name] = $value;
  141. $type = isset($type) ? $type : 'text';
  142. $help = null;
  143. if ($this->get_lang_plugin_exists($name.'_help')) {
  144. $help = $this->get_lang($name.'_help');
  145. }
  146. switch ($type) {
  147. case 'html':
  148. $result->addElement('html', $this->get_lang($name));
  149. break;
  150. case 'wysiwyg':
  151. $result->add_html_editor($name, $this->get_lang($name));
  152. break;
  153. case 'text':
  154. $result->addElement($type, $name, array($this->get_lang($name), $help));
  155. break;
  156. case 'boolean':
  157. $group = array();
  158. $group[] = $result->createElement('radio', $name, '', get_lang('Yes'), 'true');
  159. $group[] = $result->createElement('radio', $name, '', get_lang('No'), 'false');
  160. $result->addGroup($group, null, array($this->get_lang($name), $help));
  161. break;
  162. }
  163. }
  164. $result->setDefaults($defaults);
  165. $result->addElement('style_submit_button', 'submit_button', $this->get_lang('Save'));
  166. return $result;
  167. }
  168. /**
  169. * Returns the value of a given plugin global setting
  170. * @param string Name of the plugin
  171. * @return string Value of the plugin
  172. */
  173. function get($name) {
  174. $settings = $this->get_settings();
  175. foreach ($settings as $setting) {
  176. if ($setting['variable'] == ($this->get_name() . '_' . $name)) {
  177. return $setting['selected_value'];
  178. }
  179. }
  180. return false;
  181. }
  182. /**
  183. * Returns an array with the global settings for this plugin
  184. * @return array Plugin settings as an array
  185. */
  186. public function get_settings() {
  187. if (is_null($this->settings)) {
  188. $settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting')));
  189. $this->settings = $settings;
  190. }
  191. return $this->settings;
  192. }
  193. /**
  194. * Tells whether language variables are defined for this plugin or not
  195. * @param string System name of the plugin
  196. * @return boolean True if the plugin has languag variables defined, false otherwise
  197. */
  198. public function get_lang_plugin_exists($name) {
  199. return isset($this->strings[$name]);
  200. }
  201. /**
  202. * Hook for the get_lang() function to check for plugin-defined language terms
  203. * @param string Name of the language variable we are looking for
  204. * @return string The translated language term of the plugin
  205. */
  206. public function get_lang($name) {
  207. // Check whether the language strings for the plugin have already been
  208. // loaded. If so, no need to load them again.
  209. if (is_null($this->strings)) {
  210. global $language_interface;
  211. $root = api_get_path(SYS_PLUGIN_PATH);
  212. $plugin_name = $this->get_name();
  213. //1. Loading english if exists
  214. $english_path = $root.$plugin_name."/lang/english.php";
  215. if (is_readable($english_path)) {
  216. include $english_path;
  217. $this->strings = $strings;
  218. }
  219. $path = $root.$plugin_name."/lang/$language_interface.php";
  220. //2. Loading the system language
  221. if (is_readable($path)) {
  222. include $path;
  223. if (!empty($strings)) {
  224. foreach ($strings as $key => $string) {
  225. $this->strings[$key] = $string;
  226. }
  227. }
  228. }
  229. }
  230. if (isset($this->strings[$name])) {
  231. return $this->strings[$name];
  232. }
  233. return get_lang($name);
  234. }
  235. /**
  236. * Caller for the install_course_fields() function
  237. * @param int The course's integer ID
  238. * @param boolean Whether to add a tool link on the course homepage
  239. * @return void
  240. */
  241. function course_install($course_id, $add_tool_link = true) {
  242. $this->install_course_fields($course_id, $add_tool_link);
  243. }
  244. /**
  245. * Add course settings and, if not asked otherwise, add a tool link on the course homepage
  246. * @param int Course integer ID
  247. * @param boolean Whether to add a tool link or not (some tools might just offer a configuration section and act on the backend)
  248. * @return boolean False on error, null otherwise
  249. */
  250. public function install_course_fields($course_id, $add_tool_link = true) {
  251. $plugin_name = $this->get_name();
  252. $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
  253. $course_id = intval($course_id);
  254. if (empty($course_id)) {
  255. return false;
  256. }
  257. //Ads course settings
  258. if (!empty($this->course_settings)) {
  259. foreach ($this->course_settings as $setting) {
  260. $variable = Database::escape_string($setting['name']);
  261. $value ='';
  262. if (isset($setting['init_value'])) {
  263. $value = Database::escape_string($setting['init_value']);
  264. }
  265. $type = 'textfield';
  266. if (isset($setting['type'])) {
  267. $type = Database::escape_string($setting['type']);
  268. }
  269. if (isset($setting['group'])) {
  270. $group = Database::escape_string($setting['group']);
  271. $sql = "SELECT value FROM $t_course WHERE c_id = $course_id AND variable = '$group' AND subkey = '$variable' ";
  272. $result = Database::query($sql);
  273. if (!Database::num_rows($result)) {
  274. $sql_course = "INSERT INTO $t_course (c_id, variable, subkey, value, category, type) VALUES ($course_id, '$group', '$variable', '$value', 'plugins', '$type')";
  275. $r = Database::query($sql_course);
  276. }
  277. } else {
  278. $sql = "SELECT value FROM $t_course WHERE c_id = $course_id AND variable = '$variable' ";
  279. $result = Database::query($sql);
  280. if (!Database::num_rows($result)) {
  281. $sql_course = "INSERT INTO $t_course (c_id, variable, value, category, subkey, type) VALUES ($course_id, '$variable','$value', 'plugins', '$plugin_name', '$type')";
  282. $r = Database::query($sql_course);
  283. }
  284. }
  285. }
  286. }
  287. // Stop here if we don't want a tool link on the course homepage
  288. if (!$add_tool_link) { return true; }
  289. //Add an icon in the table tool list
  290. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  291. $sql = "SELECT name FROM $t_tool WHERE c_id = $course_id AND name = '$plugin_name' ";
  292. $result = Database::query($sql);
  293. if (!Database::num_rows($result)) {
  294. $tool_link = "$plugin_name/start.php";
  295. $visibility = Text::string2binary(api_get_setting('course_create_active_tools', $plugin_name));
  296. $sql_course = "INSERT INTO $t_tool VALUES ($course_id, NULL, '$plugin_name', '$tool_link', '$plugin_name.png',' ".$visibility."','0', 'squaregrey.gif','NO','_self','plugin','0')";
  297. $r = Database::query($sql_course);
  298. }
  299. }
  300. /**
  301. * Delete the fields added to the course settings page and the link to the
  302. * tool on the course's homepage
  303. * @param int The integer course ID
  304. * @return void
  305. */
  306. public function uninstall_course_fields($course_id) {
  307. $course_id = intval($course_id);
  308. if (empty($course_id)) {
  309. return false;
  310. }
  311. $plugin_name = $this->get_name();
  312. $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
  313. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  314. if (!empty($this->course_settings)) {
  315. foreach ($this->course_settings as $setting) {
  316. $variable = Database::escape_string($setting['name']);
  317. if (!empty($setting['group'])) {
  318. $variable = Database::escape_string($setting['group']);
  319. }
  320. $sql_course = "DELETE FROM $t_course WHERE c_id = $course_id AND variable = '$variable'";
  321. Database::query($sql_course);
  322. }
  323. }
  324. $sql_course = "DELETE FROM $t_tool WHERE c_id = $course_id AND name = '$plugin_name'";
  325. Database::query($sql_course);
  326. }
  327. /**
  328. * Install the course fields and tool link of this plugin in all courses
  329. * @param boolean Whether we want to add a plugin link on the course homepage
  330. * @return void
  331. */
  332. function install_course_fields_in_all_courses($add_tool_link = true) {
  333. // Update existing courses to add conference settings
  334. $t_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  335. $sql = "SELECT id, code FROM $t_courses ORDER BY id";
  336. $res = Database::query($sql);
  337. while ($row = Database::fetch_assoc($res)) {
  338. $this->install_course_fields($row['id'], $add_tool_link);
  339. }
  340. }
  341. /**
  342. * Uninstall the plugin settings fields from all courses
  343. * @return void
  344. */
  345. function uninstall_course_fields_in_all_courses() {
  346. // Update existing courses to add conference settings
  347. $t_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  348. $sql = "SELECT id, code FROM $t_courses ORDER BY id";
  349. $res = Database::query($sql);
  350. while ($row = Database::fetch_assoc($res)) {
  351. $this->uninstall_course_fields($row['id']);
  352. }
  353. }
  354. /**
  355. * Method to be extended when changing the setting in the course
  356. * configuration should trigger the use of a callback method
  357. * @param array Values sent back from the course configuration script
  358. * @return void
  359. */
  360. public function course_settings_updated($values = array()) {
  361. }
  362. }