forked from moodle/moodle
/
pluginlib.php
1691 lines (1440 loc) · 54.1 KB
/
pluginlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines classes used for plugins management
*
* This library provides a unified interface to various plugin types in
* Moodle. It is mainly used by the plugins management admin page and the
* plugins check page during the upgrade.
*
* @package core
* @subpackage admin
* @copyright 2011 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Singleton class providing general plugins management functionality
*/
class plugin_manager {
/** the plugin is shipped with standard Moodle distribution */
const PLUGIN_SOURCE_STANDARD = 'std';
/** the plugin is added extension */
const PLUGIN_SOURCE_EXTENSION = 'ext';
/** the plugin uses neither database nor capabilities, no versions */
const PLUGIN_STATUS_NODB = 'nodb';
/** the plugin is up-to-date */
const PLUGIN_STATUS_UPTODATE = 'uptodate';
/** the plugin is about to be installed */
const PLUGIN_STATUS_NEW = 'new';
/** the plugin is about to be upgraded */
const PLUGIN_STATUS_UPGRADE = 'upgrade';
/** the standard plugin is about to be deleted */
const PLUGIN_STATUS_DELETE = 'delete';
/** the version at the disk is lower than the one already installed */
const PLUGIN_STATUS_DOWNGRADE = 'downgrade';
/** the plugin is installed but missing from disk */
const PLUGIN_STATUS_MISSING = 'missing';
/** @var plugin_manager holds the singleton instance */
protected static $singletoninstance;
/** @var array of raw plugins information */
protected $pluginsinfo = null;
/** @var array of raw subplugins information */
protected $subpluginsinfo = null;
/**
* Direct initiation not allowed, use the factory method {@link self::instance()}
*
* @todo we might want to specify just a single plugin type to work with
*/
protected function __construct() {
$this->get_plugins(true);
}
/**
* Sorry, this is singleton
*/
protected function __clone() {
}
/**
* Factory method for this class
*
* @return plugin_manager the singleton instance
*/
public static function instance() {
global $CFG;
if (is_null(self::$singletoninstance)) {
self::$singletoninstance = new self();
}
return self::$singletoninstance;
}
/**
* Returns a tree of known plugins and information about them
*
* @param bool $disablecache force reload, cache can be used otherwise
* @return array 2D array. The first keys are plugin type names (e.g. qtype);
* the second keys are the plugin local name (e.g. multichoice); and
* the values are the corresponding {@link plugin_information} objects.
*/
public function get_plugins($disablecache=false) {
if ($disablecache or is_null($this->pluginsinfo)) {
$this->pluginsinfo = array();
$plugintypes = get_plugin_types();
foreach ($plugintypes as $plugintype => $plugintyperootdir) {
if (in_array($plugintype, array('base', 'general'))) {
throw new coding_exception('Illegal usage of reserved word for plugin type');
}
if (class_exists('plugintype_' . $plugintype)) {
$plugintypeclass = 'plugintype_' . $plugintype;
} else {
$plugintypeclass = 'plugintype_general';
}
if (!in_array('plugin_information', class_implements($plugintypeclass))) {
throw new coding_exception('Class ' . $plugintypeclass . ' must implement plugin_information');
}
$plugins = call_user_func(array($plugintypeclass, 'get_plugins'), $plugintype, $plugintyperootdir, $plugintypeclass);
$this->pluginsinfo[$plugintype] = $plugins;
}
}
return $this->pluginsinfo;
}
/**
* Returns list of plugins that define their subplugins and the information
* about them from the db/subplugins.php file.
*
* At the moment, only activity modules can define subplugins.
*
* @param bool $disablecache force reload, cache can be used otherwise
* @return array with keys like 'mod_quiz', and values the data from the
* corresponding db/subplugins.php file.
*/
public function get_subplugins($disablecache=false) {
if ($disablecache or is_null($this->subpluginsinfo)) {
$this->subpluginsinfo = array();
$mods = get_plugin_list('mod');
foreach ($mods as $mod => $moddir) {
$modsubplugins = array();
if (file_exists($moddir . '/db/subplugins.php')) {
include($moddir . '/db/subplugins.php');
foreach ($subplugins as $subplugintype => $subplugintyperootdir) {
$subplugin = new stdClass();
$subplugin->type = $subplugintype;
$subplugin->typerootdir = $subplugintyperootdir;
$modsubplugins[$subplugintype] = $subplugin;
}
$this->subpluginsinfo['mod_' . $mod] = $modsubplugins;
}
}
}
return $this->subpluginsinfo;
}
/**
* Returns the name of the plugin that defines the given subplugin type
*
* If the given subplugin type is not actually a subplugin, returns false.
*
* @param string $subplugintype the name of subplugin type, eg. workshopform or quiz
* @return false|string the name of the parent plugin, eg. mod_workshop
*/
public function get_parent_of_subplugin($subplugintype) {
$parent = false;
foreach ($this->get_subplugins() as $pluginname => $subplugintypes) {
if (isset($subplugintypes[$subplugintype])) {
$parent = $pluginname;
break;
}
}
return $parent;
}
/**
* Returns a localized name of a given plugin
*
* @param string $plugin name of the plugin, eg mod_workshop or auth_ldap
* @return string
*/
public function plugin_name($plugin) {
list($type, $name) = normalize_component($plugin);
return $this->pluginsinfo[$type][$name]->displayname;
}
/**
* Returns a localized name of a plugin type in plural form
*
* Most plugin types define their names in core_plugin lang file. In case of subplugins,
* we try to ask the parent plugin for the name. In the worst case, we will return
* the value of the passed $type parameter.
*
* @param string $type the type of the plugin, e.g. mod or workshopform
* @return string
*/
public function plugintype_name_plural($type) {
if (get_string_manager()->string_exists('type_' . $type . '_plural', 'core_plugin')) {
// for most plugin types, their names are defined in core_plugin lang file
return get_string('type_' . $type . '_plural', 'core_plugin');
} else if ($parent = $this->get_parent_of_subplugin($type)) {
// if this is a subplugin, try to ask the parent plugin for the name
if (get_string_manager()->string_exists('subplugintype_' . $type . '_plural', $parent)) {
return $this->plugin_name($parent) . ' / ' . get_string('subplugintype_' . $type . '_plural', $parent);
} else {
return $this->plugin_name($parent) . ' / ' . $type;
}
} else {
return $type;
}
}
/**
* @param string $component frankenstyle component name.
* @return plugin_information|null the corresponding plugin information.
*/
public function get_plugin_info($component) {
list($type, $name) = normalize_component($component);
$plugins = $this->get_plugins();
if (isset($plugins[$type][$name])) {
return $plugins[$type][$name];
} else {
return null;
}
}
/**
* Get a list of any other pluings that require this one.
* @param string $component frankenstyle component name.
* @return array of frankensyle component names that require this one.
*/
public function other_plugins_that_require($component) {
$others = array();
foreach ($this->get_plugins() as $type => $plugins) {
foreach ($plugins as $plugin) {
$required = $plugin->get_other_required_plugins();
if (isset($required[$component])) {
$others[] = $plugin->component;
}
}
}
return $others;
}
/**
* Check a dependencies list against the list of installed plugins.
* @param array $dependencies compenent name to required version or ANY_VERSION.
* @return bool true if all the dependencies are satisfied.
*/
public function are_dependencies_satisfied($dependencies) {
foreach ($dependencies as $component => $requiredversion) {
$otherplugin = $this->get_plugin_info($component);
if (is_null($otherplugin)) {
return false;
}
if ($requiredversion != ANY_VERSION and $otherplugin->versiondisk < $requiredversion) {
return false;
}
}
return true;
}
/**
* Checks all dependencies for all installed plugins. Used by install and upgrade.
* @param int $moodleversion the version from version.php.
* @return bool true if all the dependencies are satisfied for all plugins.
*/
public function all_plugins_ok($moodleversion) {
foreach ($this->get_plugins() as $type => $plugins) {
foreach ($plugins as $plugin) {
if (!empty($plugin->versionrequires) && $plugin->versionrequires > $moodleversion) {
return false;
}
if (!$this->are_dependencies_satisfied($plugin->get_other_required_plugins())) {
return false;
}
}
}
return true;
}
/**
* Defines a list of all plugins that were originally shipped in the standard Moodle distribution,
* but are not anymore and are deleted during upgrades.
*
* The main purpose of this list is to hide missing plugins during upgrade.
*
* @param string $type plugin type
* @param string $name plugin name
* @return bool
*/
public static function is_deleted_standard_plugin($type, $name) {
static $plugins = array(
'block' => array('admin', 'admin_tree', 'loancalc', 'search'),
'filter' => array('mod_data', 'mod_glossary'),
);
if (!isset($plugins[$type])) {
return false;
}
return in_array($name, $plugins[$type]);
}
/**
* Defines a white list of all plugins shipped in the standard Moodle distribution
*
* @param string $type
* @return false|array array of standard plugins or false if the type is unknown
*/
public static function standard_plugins_list($type) {
static $standard_plugins = array(
'assignment' => array(
'offline', 'online', 'upload', 'uploadsingle'
),
'auth' => array(
'cas', 'db', 'email', 'fc', 'imap', 'ldap', 'manual', 'mnet',
'nntp', 'nologin', 'none', 'pam', 'pop3', 'radius',
'shibboleth', 'webservice'
),
'block' => array(
'activity_modules', 'admin_bookmarks', 'blog_menu',
'blog_recent', 'blog_tags', 'calendar_month',
'calendar_upcoming', 'comments', 'community',
'completionstatus', 'course_list', 'course_overview',
'course_summary', 'feedback', 'glossary_random', 'html',
'login', 'mentees', 'messages', 'mnet_hosts', 'myprofile',
'navigation', 'news_items', 'online_users', 'participants',
'private_files', 'quiz_results', 'recent_activity',
'rss_client', 'search_forums', 'section_links',
'selfcompletion', 'settings', 'site_main_menu',
'social_activities', 'tag_flickr', 'tag_youtube', 'tags'
),
'coursereport' => array(
//deprecated!
),
'datafield' => array(
'checkbox', 'date', 'file', 'latlong', 'menu', 'multimenu',
'number', 'picture', 'radiobutton', 'text', 'textarea', 'url'
),
'datapreset' => array(
'imagegallery'
),
'editor' => array(
'textarea', 'tinymce'
),
'enrol' => array(
'authorize', 'category', 'cohort', 'database', 'flatfile',
'guest', 'imsenterprise', 'ldap', 'manual', 'meta', 'mnet',
'paypal', 'self'
),
'filter' => array(
'activitynames', 'algebra', 'censor', 'emailprotect',
'emoticon', 'mediaplugin', 'multilang', 'tex', 'tidy',
'urltolink', 'data', 'glossary'
),
'format' => array(
'scorm', 'social', 'topics', 'weeks'
),
'gradeexport' => array(
'ods', 'txt', 'xls', 'xml'
),
'gradeimport' => array(
'csv', 'xml'
),
'gradereport' => array(
'grader', 'outcomes', 'overview', 'user'
),
'gradingform' => array(
'rubric'
),
'local' => array(
),
'message' => array(
'email', 'jabber', 'popup'
),
'mnetservice' => array(
'enrol'
),
'mod' => array(
'assignment', 'chat', 'choice', 'data', 'feedback', 'folder',
'forum', 'glossary', 'imscp', 'label', 'lesson', 'lti', 'page',
'quiz', 'resource', 'scorm', 'survey', 'url', 'wiki', 'workshop'
),
'plagiarism' => array(
),
'portfolio' => array(
'boxnet', 'download', 'flickr', 'googledocs', 'mahara', 'picasa'
),
'profilefield' => array(
'checkbox', 'datetime', 'menu', 'text', 'textarea'
),
'qbehaviour' => array(
'adaptive', 'adaptivenopenalty', 'deferredcbm',
'deferredfeedback', 'immediatecbm', 'immediatefeedback',
'informationitem', 'interactive', 'interactivecountback',
'manualgraded', 'missing'
),
'qformat' => array(
'aiken', 'blackboard', 'blackboard_six', 'examview', 'gift',
'learnwise', 'missingword', 'multianswer', 'webct',
'xhtml', 'xml'
),
'qtype' => array(
'calculated', 'calculatedmulti', 'calculatedsimple',
'description', 'essay', 'match', 'missingtype', 'multianswer',
'multichoice', 'numerical', 'random', 'randomsamatch',
'shortanswer', 'truefalse'
),
'quiz' => array(
'grading', 'overview', 'responses', 'statistics'
),
'quizaccess' => array(
'delaybetweenattempts', 'ipaddress', 'numattempts', 'openclosedate',
'password', 'safebrowser', 'securewindow', 'timelimit'
),
'report' => array(
'backups', 'completion', 'configlog', 'courseoverview',
'log', 'loglive', 'outline', 'participation', 'progress', 'questioninstances', 'security', 'stats'
),
'repository' => array(
'alfresco', 'boxnet', 'coursefiles', 'dropbox', 'filesystem',
'flickr', 'flickr_public', 'googledocs', 'local', 'merlot',
'picasa', 'recent', 's3', 'upload', 'url', 'user', 'webdav',
'wikimedia', 'youtube'
),
'scormreport' => array(
'basic',
'interactions'
),
'theme' => array(
'afterburner', 'anomaly', 'arialist', 'base', 'binarius',
'boxxie', 'brick', 'canvas', 'formal_white', 'formfactor',
'fusion', 'leatherbound', 'magazine', 'mymobile', 'nimble',
'nonzero', 'overlay', 'serenity', 'sky_high', 'splash',
'standard', 'standardold'
),
'tool' => array(
'bloglevelupgrade', 'capability', 'customlang', 'dbtransfer', 'generator',
'health', 'innodb', 'langimport', 'multilangupgrade', 'profiling',
'qeupgradehelper', 'replace', 'spamcleaner', 'timezoneimport', 'unittest',
'uploaduser', 'unsuproles', 'xmldb'
),
'webservice' => array(
'amf', 'rest', 'soap', 'xmlrpc'
),
'workshopallocation' => array(
'manual', 'random'
),
'workshopeval' => array(
'best'
),
'workshopform' => array(
'accumulative', 'comments', 'numerrors', 'rubric'
)
);
if (isset($standard_plugins[$type])) {
return $standard_plugins[$type];
} else {
return false;
}
}
}
/**
* Interface for making information about a plugin available.
*
* Note that most of the useful information is made available in pubic fields,
* which cannot be documented in this interface. See the field definitions on
* {@link plugintype_base} to find out what information is available.
*
* @property-read string component the component name, type_name
*/
interface plugin_information {
/**
* Gathers and returns the information about all plugins of the given type
*
* Passing the parameter $typeclass allows us to reach the same effect as with the
* late binding in PHP 5.3. Once PHP 5.3 is required, we can refactor this to use
* {@example $plugin = new static();} instead of {@example $plugin = new $typeclass()}
*
* @param string $type the name of the plugintype, eg. mod, auth or workshopform
* @param string $typerootdir full path to the location of the plugin dir
* @param string $typeclass the name of the actually called class
* @return array of plugintype classes, indexed by the plugin name
*/
public static function get_plugins($type, $typerootdir, $typeclass);
/**
* Sets $displayname property to a localized name of the plugin
*
* @return void
*/
public function init_display_name();
/**
* Sets $versiondisk property to a numerical value representing the
* version of the plugin's source code.
*
* If the value is null after calling this method, either the plugin
* does not use versioning (typically does not have any database
* data) or is missing from disk.
*
* @return void
*/
public function load_disk_version();
/**
* Sets $versiondb property to a numerical value representing the
* currently installed version of the plugin.
*
* If the value is null after calling this method, either the plugin
* does not use versioning (typically does not have any database
* data) or has not been installed yet.
*
* @return void
*/
public function load_db_version();
/**
* Sets $versionrequires property to a numerical value representing
* the version of Moodle core that this plugin requires.
*
* @return void
*/
public function load_required_main_version();
/**
* Sets $source property to one of plugin_manager::PLUGIN_SOURCE_xxx
* constants.
*
* If the property's value is null after calling this method, then
* the type of the plugin has not been recognized and you should throw
* an exception.
*
* @return void
*/
public function init_is_standard();
/**
* Returns true if the plugin is shipped with the official distribution
* of the current Moodle version, false otherwise.
*
* @return bool
*/
public function is_standard();
/**
* Returns the status of the plugin
*
* @return string one of plugin_manager::PLUGIN_STATUS_xxx constants
*/
public function get_status();
/**
* Get the list of other plugins that this plugin requires ot be installed.
* @return array with keys the frankenstyle plugin name, and values either
* a version string (like '2011101700') or the constant ANY_VERSION.
*/
public function get_other_required_plugins();
/**
* Returns the information about plugin availability
*
* True means that the plugin is enabled. False means that the plugin is
* disabled. Null means that the information is not available, or the
* plugin does not support configurable availability or the availability
* can not be changed.
*
* @return null|bool
*/
public function is_enabled();
/**
* Returns the URL of the plugin settings screen
*
* Null value means that the plugin either does not have the settings screen
* or its location is not available via this library.
*
* @return null|moodle_url
*/
public function get_settings_url();
/**
* Returns the URL of the screen where this plugin can be uninstalled
*
* Visiting that URL must be safe, that is a manual confirmation is needed
* for actual uninstallation of the plugin. Null value means that the
* plugin either does not support uninstallation, or does not require any
* database cleanup or the location of the screen is not available via this
* library.
*
* @return null|moodle_url
*/
public function get_uninstall_url();
/**
* Returns relative directory of the plugin with heading '/'
*
* @example /mod/workshop
* @return string
*/
public function get_dir();
/**
* Return the full path name of a file within the plugin.
* No check is made to see if the file exists.
* @param string $relativepath e.g. 'version.php'.
* @return string e.g. $CFG->dirroot . '/mod/quiz/version.php'.
*/
public function full_path($relativepath);
}
/**
* Defines public properties that all plugintype classes must have
* and provides default implementation of required methods.
*
* @property-read string component the component name, type_name
*/
abstract class plugintype_base {
/** @var string the plugintype name, eg. mod, auth or workshopform */
public $type;
/** @var string full path to the location of all the plugins of this type */
public $typerootdir;
/** @var string the plugin name, eg. assignment, ldap */
public $name;
/** @var string the localized plugin name */
public $displayname;
/** @var string the plugin source, one of plugin_manager::PLUGIN_SOURCE_xxx constants */
public $source;
/** @var fullpath to the location of this plugin */
public $rootdir;
/** @var int|string the version of the plugin's source code */
public $versiondisk;
/** @var int|string the version of the installed plugin */
public $versiondb;
/** @var int|float|string required version of Moodle core */
public $versionrequires;
/** @var array other plugins that this one depends on.
* Lazy-loaded by {@link get_other_required_plugins()} */
public $dependencies = null;
/** @var int number of instances of the plugin - not supported yet */
public $instances;
/** @var int order of the plugin among other plugins of the same type - not supported yet */
public $sortorder;
/**
* @see plugin_information::get_plugins()
*/
public static function get_plugins($type, $typerootdir, $typeclass) {
// get the information about plugins at the disk
$plugins = get_plugin_list($type);
$ondisk = array();
foreach ($plugins as $pluginname => $pluginrootdir) {
$plugin = new $typeclass();
$plugin->type = $type;
$plugin->typerootdir = $typerootdir;
$plugin->name = $pluginname;
$plugin->rootdir = $pluginrootdir;
$plugin->init_display_name();
$plugin->load_disk_version();
$plugin->load_db_version();
$plugin->load_required_main_version();
$plugin->init_is_standard();
$ondisk[$pluginname] = $plugin;
}
return $ondisk;
}
/**
* @see plugin_information::init_display_name()
*/
public function init_display_name() {
if (!get_string_manager()->string_exists('pluginname', $this->component)) {
$this->displayname = '[pluginname,' . $this->component . ']';
} else {
$this->displayname = get_string('pluginname', $this->component);
}
}
/**
* Magic method getter, redirects to read only values.
* @param string $name
* @return mixed
*/
public function __get($name) {
switch ($name) {
case 'component': return $this->type . '_' . $this->name;
default:
debugging('Invalid plugin property accessed! '.$name);
return null;
}
}
/**
* @see plugin_information::full_path()
*/
public function full_path($relativepath) {
if (empty($this->rootdir)) {
return '';
}
return $this->rootdir . '/' . $relativepath;
}
/**
* Load the data from version.php.
* @return object the data object defined in version.php.
*/
protected function load_version_php() {
$versionfile = $this->full_path('version.php');
$plugin = new stdClass();
if (is_readable($versionfile)) {
include($versionfile);
}
return $plugin;
}
/**
* @see plugin_information::load_disk_version()
*/
public function load_disk_version() {
$plugin = $this->load_version_php();
if (isset($plugin->version)) {
$this->versiondisk = $plugin->version;
}
}
/**
* @see plugin_information::load_required_main_version()
*/
public function load_required_main_version() {
$plugin = $this->load_version_php();
if (isset($plugin->requires)) {
$this->versionrequires = $plugin->requires;
}
}
/**
* Initialise {@link $dependencies} to the list of other plugins (in any)
* that this one requires to be installed.
*/
protected function load_other_required_plugins() {
$plugin = $this->load_version_php();
if (!empty($plugin->dependencies)) {
$this->dependencies = $plugin->dependencies;
} else {
$this->dependencies = array(); // By default, no dependencies.
}
}
/**
* @see plugin_information::get_other_required_plugins()
*/
public function get_other_required_plugins() {
if (is_null($this->dependencies)) {
$this->load_other_required_plugins();
}
return $this->dependencies;
}
/**
* @see plugin_information::load_db_version()
*/
public function load_db_version() {
if ($ver = self::get_version_from_config_plugins($this->component)) {
$this->versiondb = $ver;
}
}
/**
* @see plugin_information::init_is_standard()
*/
public function init_is_standard() {
$standard = plugin_manager::standard_plugins_list($this->type);
if ($standard !== false) {
$standard = array_flip($standard);
if (isset($standard[$this->name])) {
$this->source = plugin_manager::PLUGIN_SOURCE_STANDARD;
} else if (!is_null($this->versiondb) and is_null($this->versiondisk)
and plugin_manager::is_deleted_standard_plugin($this->type, $this->name)) {
$this->source = plugin_manager::PLUGIN_SOURCE_STANDARD; // to be deleted
} else {
$this->source = plugin_manager::PLUGIN_SOURCE_EXTENSION;
}
}
}
/**
* @see plugin_information::is_standard()
*/
public function is_standard() {
return $this->source === plugin_manager::PLUGIN_SOURCE_STANDARD;
}
/**
* @see plugin_information::get_status()
*/
public function get_status() {
if (is_null($this->versiondb) and is_null($this->versiondisk)) {
return plugin_manager::PLUGIN_STATUS_NODB;
} else if (is_null($this->versiondb) and !is_null($this->versiondisk)) {
return plugin_manager::PLUGIN_STATUS_NEW;
} else if (!is_null($this->versiondb) and is_null($this->versiondisk)) {
if (plugin_manager::is_deleted_standard_plugin($this->type, $this->name)) {
return plugin_manager::PLUGIN_STATUS_DELETE;
} else {
return plugin_manager::PLUGIN_STATUS_MISSING;
}
} else if ((string)$this->versiondb === (string)$this->versiondisk) {
return plugin_manager::PLUGIN_STATUS_UPTODATE;
} else if ($this->versiondb < $this->versiondisk) {
return plugin_manager::PLUGIN_STATUS_UPGRADE;
} else if ($this->versiondb > $this->versiondisk) {
return plugin_manager::PLUGIN_STATUS_DOWNGRADE;
} else {
// $version = pi(); and similar funny jokes - hopefully Donald E. Knuth will never contribute to Moodle ;-)
throw new coding_exception('Unable to determine plugin state, check the plugin versions');
}
}
/**
* @see plugin_information::is_enabled()
*/
public function is_enabled() {
return null;
}
/**
* @see plugin_information::get_settings_url()
*/
public function get_settings_url() {
return null;
}
/**
* @see plugin_information::get_uninstall_url()
*/
public function get_uninstall_url() {
return null;
}
/**
* @see plugin_information::get_dir()
*/
public function get_dir() {
global $CFG;
return substr($this->rootdir, strlen($CFG->dirroot));
}
/**
* Provides access to plugin versions from {config_plugins}
*
* @param string $plugin plugin name
* @param double $disablecache optional, defaults to false
* @return int|false the stored value or false if not found
*/
protected function get_version_from_config_plugins($plugin, $disablecache=false) {
global $DB;
static $pluginversions = null;
if (is_null($pluginversions) or $disablecache) {
try {
$pluginversions = $DB->get_records_menu('config_plugins', array('name' => 'version'), 'plugin', 'plugin,value');
} catch (dml_exception $e) {
// before install
$pluginversions = array();
}
}
if (!array_key_exists($plugin, $pluginversions)) {
return false;
}
return $pluginversions[$plugin];
}
}
/**
* General class for all plugin types that do not have their own class
*/
class plugintype_general extends plugintype_base implements plugin_information {
}
/**
* Class for page side blocks
*/
class plugintype_block extends plugintype_base implements plugin_information {
/**
* @see plugin_information::get_plugins()
*/
public static function get_plugins($type, $typerootdir, $typeclass) {
// get the information about blocks at the disk
$blocks = parent::get_plugins($type, $typerootdir, $typeclass);
// add blocks missing from disk
$blocksinfo = self::get_blocks_info();
foreach ($blocksinfo as $blockname => $blockinfo) {
if (isset($blocks[$blockname])) {
continue;
}
$plugin = new $typeclass();
$plugin->type = $type;
$plugin->typerootdir = $typerootdir;
$plugin->name = $blockname;
$plugin->rootdir = null;
$plugin->displayname = $blockname;
$plugin->versiondb = $blockinfo->version;
$plugin->init_is_standard();
$blocks[$blockname] = $plugin;
}
return $blocks;
}
/**
* @see plugin_information::init_display_name()
*/
public function init_display_name() {
if (get_string_manager()->string_exists('pluginname', 'block_' . $this->name)) {
$this->displayname = get_string('pluginname', 'block_' . $this->name);
} else if (($block = block_instance($this->name)) !== false) {
$this->displayname = $block->get_title();
} else {
parent::init_display_name();
}
}