Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/portfolio/plugin.php
Go to the documentation of this file.
00001 <?php
00032 defined('MOODLE_INTERNAL') || die();
00033 
00039 abstract class portfolio_plugin_base {
00040 
00045     protected $dirty;
00046 
00051     protected $id;
00052 
00057     protected $name;
00058 
00063     protected $plugin;
00064 
00069     protected $visible;
00070 
00076     protected $config;
00077 
00085     protected $userconfig;
00086 
00092     protected $exportconfig;
00093 
00098     protected $user;
00099 
00103     protected $exporter;
00104 
00113     public function supported_formats() {
00114         return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICH);
00115     }
00116 
00123     public static function file_mime_check($mimetype) {
00124         return true;
00125     }
00126 
00127 
00139     public abstract function expected_time($callertime);
00140 
00148     public abstract function is_push();
00149 
00156     public static function get_name() {
00157         throw new coding_exception('get_name() method needs to be overridden in each subclass of portfolio_plugin_base');
00158     }
00159 
00167     public static function plugin_sanity_check() {
00168         return 0;
00169     }
00170 
00178     public function instance_sanity_check() {
00179         return 0;
00180     }
00181 
00188     public static function has_admin_config() {
00189         return false;
00190     }
00191 
00198     public function has_user_config() {
00199         return false;
00200     }
00201 
00208     public function has_export_config() {
00209         return false;
00210     }
00211 
00220     public function export_config_validation(array $data) {}
00221 
00230     public function user_config_validation(array $data) {}
00231 
00244     public function set_export_config($config) {
00245         $allowed = array_merge(
00246             array('wait', 'hidewait', 'format', 'hideformat'),
00247             $this->get_allowed_export_config()
00248         );
00249         foreach ($config as $key => $value) {
00250             if (!in_array($key, $allowed)) {
00251                 $a = (object)array('property' => $key, 'class' => get_class($this));
00252                 throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', null, $a);
00253             }
00254             $this->exportconfig[$key] = $value;
00255         }
00256     }
00257 
00267     public final function get_export_config($key) {
00268         $allowed = array_merge(
00269             array('hidewait', 'wait', 'format', 'hideformat'),
00270             $this->get_allowed_export_config()
00271         );
00272         if (!in_array($key, $allowed)) {
00273             $a = (object)array('property' => $key, 'class' => get_class($this));
00274             throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', null, $a);
00275         }
00276         if (!array_key_exists($key, $this->exportconfig)) {
00277             return null;
00278         }
00279         return $this->exportconfig[$key];
00280     }
00281 
00293     public function get_export_summary() {
00294         return false;
00295     }
00296 
00305     public abstract function prepare_package();
00306 
00314     public abstract function send_package();
00315 
00316 
00326     public function get_extra_finish_options() {
00327         return false;
00328     }
00329 
00336     public abstract function get_interactive_continue_url();
00337 
00343     public function get_static_continue_url() {
00344         return $this->get_interactive_continue_url();
00345     }
00346 
00352     public function resolve_static_continue_url($url) {
00353         return $url;
00354     }
00355 
00364     public function user_config_form(&$mform) {}
00365 
00378     public function admin_config_form(&$mform) {}
00379 
00388     public function admin_config_validation($data) {}
00397     public function export_config_form(&$mform) {}
00398 
00404     public static function allows_multiple_instances() {
00405         return true;
00406     }
00407 
00423     public function steal_control($stage) {
00424         return false;
00425     }
00426 
00441     public function post_control($stage, $params) { }
00442 
00456     public static function create_instance($plugin, $name, $config) {
00457         global $DB, $CFG;
00458         $new = (object)array(
00459             'plugin' => $plugin,
00460             'name'   => $name,
00461         );
00462         if (!portfolio_static_function($plugin, 'allows_multiple_instances')) {
00463             // check we don't have one already
00464             if ($DB->record_exists('portfolio_instance', array('plugin' => $plugin))) {
00465                 throw new portfolio_exception('multipleinstancesdisallowed', 'portfolio', '', $plugin);
00466             }
00467         }
00468         $newid = $DB->insert_record('portfolio_instance', $new);
00469         require_once($CFG->dirroot . '/portfolio/' . $plugin . '/lib.php');
00470         $classname = 'portfolio_plugin_'  . $plugin;
00471         $obj = new $classname($newid);
00472         $obj->set_config($config);
00473         $obj->save();
00474         return $obj;
00475     }
00476 
00487     public function __construct($instanceid, $record=null) {
00488         global $DB;
00489         if (!$record) {
00490             if (!$record = $DB->get_record('portfolio_instance', array('id' => $instanceid))) {
00491                 throw new portfolio_exception('invalidinstance', 'portfolio');
00492             }
00493         }
00494         foreach ((array)$record as $key =>$value) {
00495             if (property_exists($this, $key)) {
00496                 $this->{$key} = $value;
00497             }
00498         }
00499         $this->config = new StdClass;
00500         $this->userconfig = array();
00501         $this->exportconfig = array();
00502         foreach ($DB->get_records('portfolio_instance_config', array('instance' => $instanceid)) as $config) {
00503             $this->config->{$config->name} = $config->value;
00504         }
00505         $this->init();
00506         return $this;
00507     }
00508 
00513     protected function init() { }
00514 
00522     public static function get_allowed_config() {
00523         return array();
00524     }
00525 
00533     public function get_allowed_user_config() {
00534         return array();
00535     }
00536 
00544     public function get_allowed_export_config() {
00545         return array();
00546     }
00547 
00554     public final function set_config($config) {
00555         global $DB;
00556         foreach ($config as $key => $value) {
00557             // try set it in $this first
00558             try {
00559                 $this->set($key, $value);
00560                 continue;
00561             } catch (portfolio_exception $e) { }
00562             if (!in_array($key, $this->get_allowed_config())) {
00563                 $a = (object)array('property' => $key, 'class' => get_class($this));
00564                 throw new portfolio_export_exception($this->get('exporter'), 'invalidconfigproperty', 'portfolio', null, $a);
00565             }
00566             if (!isset($this->config->{$key})) {
00567                 $DB->insert_record('portfolio_instance_config', (object)array(
00568                     'instance' => $this->id,
00569                     'name' => $key,
00570                     'value' => $value,
00571                 ));
00572             } else if ($this->config->{$key} != $value) {
00573                 $DB->set_field('portfolio_instance_config', 'value', $value, array('name' => $key, 'instance' => $this->id));
00574             }
00575             $this->config->{$key} = $value;
00576         }
00577     }
00578 
00586     public final function get_config($key) {
00587         if (!in_array($key, $this->get_allowed_config())) {
00588             $a = (object)array('property' => $key, 'class' => get_class($this));
00589             throw new portfolio_export_exception($this->get('exporter'), 'invalidconfigproperty', 'portfolio', null, $a);
00590         }
00591         if (isset($this->config->{$key})) {
00592             return $this->config->{$key};
00593         }
00594         return null;
00595     }
00596 
00606     public final function get_user_config($key, $userid=0) {
00607         global $DB;
00608 
00609         if (empty($userid)) {
00610             $userid = $this->user->id;
00611         }
00612 
00613         if ($key != 'visible') { // handled by the parent class
00614             if (!in_array($key, $this->get_allowed_user_config())) {
00615                 $a = (object)array('property' => $key, 'class' => get_class($this));
00616                 throw new portfolio_export_exception($this->get('exporter'), 'invaliduserproperty', 'portfolio', null, $a);
00617             }
00618         }
00619         if (!array_key_exists($userid, $this->userconfig)) {
00620             $this->userconfig[$userid] = (object)array_fill_keys(array_merge(array('visible'), $this->get_allowed_user_config()), null);
00621             foreach ($DB->get_records('portfolio_instance_user', array('instance' => $this->id, 'userid' => $userid)) as $config) {
00622                 $this->userconfig[$userid]->{$config->name} = $config->value;
00623             }
00624         }
00625         if ($this->userconfig[$userid]->visible === null) {
00626             $this->set_user_config(array('visible' => 1), $userid);
00627         }
00628         return $this->userconfig[$userid]->{$key};
00629 
00630     }
00631 
00640     public final function set_user_config($config, $userid=0) {
00641         global $DB;
00642 
00643         if (empty($userid)) {
00644             $userid = $this->user->id;
00645         }
00646 
00647         foreach ($config as $key => $value) {
00648             if ($key != 'visible' && !in_array($key, $this->get_allowed_user_config())) {
00649                 $a = (object)array('property' => $key, 'class' => get_class($this));
00650                 throw new portfolio_export_exception($this->get('exporter'), 'invaliduserproperty', 'portfolio', null, $a);
00651             }
00652             if (!$existing = $DB->get_record('portfolio_instance_user', array('instance'=> $this->id, 'userid' => $userid, 'name' => $key))) {
00653                 $DB->insert_record('portfolio_instance_user', (object)array(
00654                     'instance' => $this->id,
00655                     'name' => $key,
00656                     'value' => $value,
00657                     'userid' => $userid,
00658                 ));
00659             } else if ($existing->value != $value) {
00660                 $DB->set_field('portfolio_instance_user', 'value', $value, array('name' => $key, 'instance' => $this->id, 'userid' => $userid));
00661             }
00662             $this->userconfig[$userid]->{$key} = $value;
00663         }
00664 
00665     }
00666 
00673     public final function get($field) {
00674         if (property_exists($this, $field)) {
00675             return $this->{$field};
00676         }
00677         $a = (object)array('property' => $field, 'class' => get_class($this));
00678         throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', null, $a);
00679     }
00680 
00687     public final function set($field, $value) {
00688         if (property_exists($this, $field)) {
00689             $this->{$field} =& $value;
00690             $this->dirty = true;
00691             return true;
00692         }
00693         $a = (object)array('property' => $field, 'class' => get_class($this));
00694         if ($this->get('exporter')) {
00695             throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', null, $a);
00696         }
00697         throw new portfolio_exception('invalidproperty', 'portfolio', null, $a); // this happens outside export (eg admin settings)
00698 
00699     }
00700 
00707     public function save() {
00708         global $DB;
00709         if (!$this->dirty) {
00710             return true;
00711         }
00712         $fordb = new StdClass();
00713         foreach (array('id', 'name', 'plugin', 'visible') as $field) {
00714             $fordb->{$field} = $this->{$field};
00715         }
00716         $DB->update_record('portfolio_instance', $fordb);
00717         $this->dirty = false;
00718         return true;
00719     }
00720 
00726     public function delete() {
00727         global $DB;
00728         $DB->delete_records('portfolio_instance_config', array('instance' => $this->get('id')));
00729         $DB->delete_records('portfolio_instance_user', array('instance' => $this->get('id')));
00730         $DB->delete_records('portfolio_tempdata', array('instance' => $this->get('id')));
00731         $DB->delete_records('portfolio_instance', array('id' => $this->get('id')));
00732         $this->dirty = false;
00733         return true;
00734     }
00735 
00739     public function cleanup() {
00740         return true;
00741     }
00742 
00753     public static function allows_multiple_exports() {
00754         return true;
00755     }
00756 
00763     public function heading_summary() {
00764         return get_string('exportingcontentto', 'portfolio', $this->name);
00765     }
00766 }
00767 
00772 abstract class portfolio_plugin_push_base extends portfolio_plugin_base {
00773 
00774     public function is_push() {
00775         return true;
00776     }
00777 }
00778 
00785 abstract class portfolio_plugin_pull_base extends portfolio_plugin_base {
00786 
00787     protected $file;
00788 
00789     public function is_push() {
00790         return false;
00791     }
00792 
00800     public function get_base_file_url() {
00801         global $CFG;
00802         return $CFG->wwwroot . '/portfolio/file.php?id=' . $this->exporter->get('id');
00803     }
00804 
00811     public abstract function verify_file_request_params($params);
00812 
00822     public function send_file() {
00823         $file = $this->get('file');
00824         if (!($file instanceof stored_file)) {
00825             throw new portfolio_export_exception($this->get('exporter'), 'filenotfound', 'portfolio');
00826         }
00827         // the last 'true' on the end of this means don't die(); afterwards, so we can clean up.
00828         send_stored_file($file, 0, 0, true, null, true);
00829         $this->get('exporter')->log_transfer();
00830     }
00831 
00832 }
 All Data Structures Namespaces Files Functions Variables Enumerations