Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/portfolio/caller.php
Go to the documentation of this file.
00001 <?php
00031 defined('MOODLE_INTERNAL') || die();
00032 
00039 abstract class portfolio_caller_base {
00040 
00045     protected $course;
00046 
00051     protected $exportconfig = array();
00052 
00057     protected $user;
00058 
00062     protected $exporter;
00063 
00067     protected $supportedformats;
00068 
00072     protected $singlefile;
00073 
00077     protected $multifiles;
00078 
00082     protected $intendedmimetype;
00083 
00084     public function __construct($callbackargs) {
00085         $expected = call_user_func(array(get_class($this), 'expected_callbackargs'));
00086         foreach ($expected as $key => $required) {
00087             if (!array_key_exists($key, $callbackargs)) {
00088                 if ($required) {
00089                     $a = (object)array('arg' => $key, 'class' => get_class($this));
00090                     throw new portfolio_caller_exception('missingcallbackarg', 'portfolio', null, $a);
00091                 }
00092                 continue;
00093             }
00094             $this->{$key} = $callbackargs[$key];
00095         }
00096     }
00097 
00106     public function export_config_form(&$mform, $instance) {}
00107 
00108 
00115     public function has_export_config() {
00116         return false;
00117     }
00118 
00127     public function export_config_validation($data) {}
00128 
00139     public abstract function expected_time();
00140 
00144     public function expected_time_file() {
00145         if ($this->multifiles) {
00146             return portfolio_expected_time_file($this->multifiles);
00147         }
00148         else if ($this->singlefile) {
00149             return portfolio_expected_time_file($this->singlefile);
00150         }
00151         return PORTFOLIO_TIME_LOW;
00152     }
00153 
00164     public abstract function get_navigation();
00165 
00169     public abstract function get_sha1();
00170 
00174     public function get_sha1_file() {
00175         if (empty($this->singlefile) && empty($this->multifiles)) {
00176             throw new portfolio_caller_exception('invalidsha1file', 'portfolio', $this->get_return_url());
00177         }
00178         if ($this->singlefile) {
00179             return $this->singlefile->get_contenthash();
00180         }
00181         $sha1s = array();
00182         foreach ($this->multifiles as $file) {
00183             $sha1s[] = $file->get_contenthash();
00184         }
00185         asort($sha1s);
00186         return sha1(implode('', $sha1s));
00187     }
00188 
00189     /*
00190     * generic getter for properties belonging to this instance
00191     * <b>outside</b> the subclasses
00192     * like name, visible etc.
00193     */
00194     public function get($field) {
00195         if (property_exists($this, $field)) {
00196             return $this->{$field};
00197         }
00198         $a = (object)array('property' => $field, 'class' => get_class($this));
00199         throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a);
00200     }
00201 
00208     public final function set($field, &$value) {
00209         if (property_exists($this, $field)) {
00210             $this->{$field} =& $value;
00211             $this->dirty = true;
00212             return true;
00213         }
00214         $a = (object)array('property' => $field, 'class' => get_class($this));
00215         throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a);
00216     }
00217 
00225     public final function set_export_config($config) {
00226         $allowed = array_merge(
00227             array('wait', 'hidewait', 'format', 'hideformat'),
00228             $this->get_allowed_export_config()
00229         );
00230         foreach ($config as $key => $value) {
00231             if (!in_array($key, $allowed)) {
00232                 $a = (object)array('property' => $key, 'class' => get_class($this));
00233                 throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
00234             }
00235             $this->exportconfig[$key] = $value;
00236         }
00237     }
00238 
00245     public final function get_export_config($key) {
00246         $allowed = array_merge(
00247             array('wait', 'hidewait', 'format', 'hideformat'),
00248             $this->get_allowed_export_config()
00249         );
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', $this->get_return_url(), $a);
00253         }
00254         if (!array_key_exists($key, $this->exportconfig)) {
00255             return null;
00256         }
00257         return $this->exportconfig[$key];
00258     }
00259 
00271     public function get_allowed_export_config() {
00272         return array();
00273     }
00274 
00286     public function get_export_summary() {
00287         return false;
00288     }
00289 
00296     public abstract function prepare_package();
00297 
00302     public function prepare_package_file() {
00303         if (empty($this->singlefile) && empty($this->multifiles)) {
00304             throw new portfolio_caller_exception('invalidpreparepackagefile', 'portfolio', $this->get_return_url());
00305         }
00306         if ($this->singlefile) {
00307             return $this->exporter->copy_existing_file($this->singlefile);
00308         }
00309         foreach ($this->multifiles as $file) {
00310             $this->exporter->copy_existing_file($file);
00311         }
00312     }
00313 
00324     public final function supported_formats() {
00325         $basic = $this->base_supported_formats();
00326         if (empty($this->supportedformats)) {
00327             $specific = array();
00328         } else if (!is_array($this->supportedformats)) {
00329             debugging(get_class($this) . ' has set a non array value of member variable supported formats - working around but should be fixed in code');
00330             $specific = array($this->supportedformats);
00331         } else {
00332             $specific = $this->supportedformats;
00333         }
00334         return portfolio_most_specific_formats($specific, $basic);
00335     }
00336 
00337     public static function base_supported_formats() {
00338         throw new coding_exception('base_supported_formats() method needs to be overridden in each subclass of portfolio_caller_base');
00339     }
00340 
00346     public abstract function get_return_url();
00347 
00352     public abstract function check_permissions();
00353 
00357     public static function display_name() {
00358         throw new coding_exception('display_name() method needs to be overridden in each subclass of portfolio_caller_base');
00359     }
00360 
00367     public function heading_summary() {
00368         return get_string('exportingcontentfrom', 'portfolio', $this->display_name());
00369     }
00370 
00371     public abstract function load_data();
00372 
00391     public function set_file_and_format_data($ids=null /* ..pass arguments to area files here. */) {
00392         $args = func_get_args();
00393         array_shift($args); // shift off $ids
00394         if (empty($ids) && count($args) == 0) {
00395             return;
00396         }
00397         $files = array();
00398         $fs = get_file_storage();
00399         if (!empty($ids)) {
00400             if (is_numeric($ids) || $ids instanceof stored_file) {
00401                 $ids = array($ids);
00402             }
00403             foreach ($ids as $id) {
00404                 if ($id instanceof stored_file) {
00405                     $files[] = $id;
00406                 } else {
00407                     $files[] = $fs->get_file_by_id($id);
00408                 }
00409             }
00410         } else if (count($args) != 0) {
00411             if (count($args) < 4) {
00412                 throw new portfolio_caller_exception('invalidfileareaargs', 'portfolio');
00413             }
00414             $files = array_values(call_user_func_array(array($fs, 'get_area_files'), $args));
00415         }
00416         switch (count($files)) {
00417             case 0: return;
00418             case 1: {
00419                 $this->singlefile = $files[0];
00420                 return;
00421             }
00422             default: {
00423                 $this->multifiles = $files;
00424             }
00425         }
00426     }
00427 
00434     public function set_formats_from_button($formats) {
00435         $base = $this->base_supported_formats();
00436         if (count($base) != count($formats)
00437                 || count($base) != count(array_intersect($base, $formats))) {
00438                 $this->supportedformats = portfolio_most_specific_formats($formats, $base);
00439                 return;
00440         }
00441         // in the case where the button hasn't actually set anything,
00442         // we need to run through again and resolve conflicts
00443         // TODO revisit this comment - it looks to me like it's lying
00444         $this->supportedformats = portfolio_most_specific_formats($formats, $formats);
00445     }
00446 
00456     protected function add_format($format) {
00457         if (in_array($format, $this->supportedformats)) {
00458             return;
00459         }
00460         $this->supportedformats = portfolio_most_specific_formats(array($format), $this->supportedformats);
00461     }
00462 
00463     public function get_mimetype() {
00464         if ($this->singlefile instanceof stored_file) {
00465             return $this->singlefile->get_mimetype();
00466         } else if (!empty($this->intendedmimetype)) {
00467             return $this->intendedmimetype;
00468         }
00469     }
00470 
00482     public static function expected_callbackargs() {
00483         throw new coding_exception('expected_callbackargs() method needs to be overridden in each subclass of portfolio_caller_base');
00484     }
00485 
00486 
00492     public abstract function set_context($PAGE);
00493 }
00494 
00504 abstract class portfolio_module_caller_base extends portfolio_caller_base {
00505 
00511     protected $cm;
00512 
00517     protected $id;
00518 
00522     protected $course;
00523 
00528     public function get_navigation() {
00529         $extranav = array('name' => $this->cm->name, 'link' => $this->get_return_url());
00530         return array($extranav, $this->cm);
00531     }
00532 
00538     public function get_return_url() {
00539         global $CFG;
00540         return $CFG->wwwroot . '/mod/' . $this->cm->modname . '/view.php?id=' . $this->cm->id;
00541     }
00542 
00548     public function get($key) {
00549         if ($key != 'course') {
00550             return parent::get($key);
00551         }
00552         global $DB;
00553         if (empty($this->course)) {
00554             $this->course = $DB->get_record('course', array('id' => $this->cm->course));
00555         }
00556         return $this->course;
00557     }
00558 
00564     public function heading_summary() {
00565         return get_string('exportingcontentfrom', 'portfolio', $this->display_name() . ': ' . $this->cm->name);
00566     }
00567 
00571     public function set_context($PAGE) {
00572         $PAGE->set_cm($this->cm);
00573     }
00574 }
 All Data Structures Namespaces Files Functions Variables Enumerations