|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00030 require_once(dirname(dirname(__FILE__)) . '/config.php'); 00031 require_once($CFG->libdir . '/filelib.php'); 00032 require_once($CFG->libdir . '/formslib.php'); 00033 00034 define('FILE_EXTERNAL', 1); 00035 define('FILE_INTERNAL', 2); 00036 define('RENAME_SUFFIX', '_2'); 00037 00059 class repository_type { 00060 00061 00067 private $_typename; 00068 00069 00077 private $_options; 00078 00079 00085 private $_visible; 00086 00087 00093 private $_sortorder; 00094 00102 public function get_contextvisibility($context) { 00103 global $USER; 00104 00105 if ($context->contextlevel == CONTEXT_COURSE) { 00106 return $this->_options['enablecourseinstances']; 00107 } 00108 00109 if ($context->contextlevel == CONTEXT_USER) { 00110 return $this->_options['enableuserinstances']; 00111 } 00112 00113 //the context is SITE 00114 return true; 00115 } 00116 00117 00118 00127 public function __construct($typename = '', $typeoptions = array(), $visible = true, $sortorder = 0) { 00128 global $CFG; 00129 00130 //set type attributs 00131 $this->_typename = $typename; 00132 $this->_visible = $visible; 00133 $this->_sortorder = $sortorder; 00134 00135 //set options attribut 00136 $this->_options = array(); 00137 $options = repository::static_function($typename, 'get_type_option_names'); 00138 //check that the type can be setup 00139 if (!empty($options)) { 00140 //set the type options 00141 foreach ($options as $config) { 00142 if (array_key_exists($config, $typeoptions)) { 00143 $this->_options[$config] = $typeoptions[$config]; 00144 } 00145 } 00146 } 00147 00148 //retrieve visibility from option 00149 if (array_key_exists('enablecourseinstances',$typeoptions)) { 00150 $this->_options['enablecourseinstances'] = $typeoptions['enablecourseinstances']; 00151 } else { 00152 $this->_options['enablecourseinstances'] = 0; 00153 } 00154 00155 if (array_key_exists('enableuserinstances',$typeoptions)) { 00156 $this->_options['enableuserinstances'] = $typeoptions['enableuserinstances']; 00157 } else { 00158 $this->_options['enableuserinstances'] = 0; 00159 } 00160 00161 } 00162 00168 public function get_typename() { 00169 return $this->_typename; 00170 } 00171 00176 public function get_readablename() { 00177 return get_string('pluginname','repository_'.$this->_typename); 00178 } 00179 00184 public function get_options() { 00185 return $this->_options; 00186 } 00187 00192 public function get_visible() { 00193 return $this->_visible; 00194 } 00195 00200 public function get_sortorder() { 00201 return $this->_sortorder; 00202 } 00203 00211 public function create($silent = false) { 00212 global $DB; 00213 00214 //check that $type has been set 00215 $timmedtype = trim($this->_typename); 00216 if (empty($timmedtype)) { 00217 throw new repository_exception('emptytype', 'repository'); 00218 } 00219 00220 //set sortorder as the last position in the list 00221 if (!isset($this->_sortorder) || $this->_sortorder == 0 ) { 00222 $sql = "SELECT MAX(sortorder) FROM {repository}"; 00223 $this->_sortorder = 1 + $DB->get_field_sql($sql); 00224 } 00225 00226 //only create a new type if it doesn't already exist 00227 $existingtype = $DB->get_record('repository', array('type'=>$this->_typename)); 00228 if (!$existingtype) { 00229 //create the type 00230 $newtype = new stdClass(); 00231 $newtype->type = $this->_typename; 00232 $newtype->visible = $this->_visible; 00233 $newtype->sortorder = $this->_sortorder; 00234 $plugin_id = $DB->insert_record('repository', $newtype); 00235 //save the options in DB 00236 $this->update_options(); 00237 00238 $instanceoptionnames = repository::static_function($this->_typename, 'get_instance_option_names'); 00239 00240 //if the plugin type has no multiple instance (e.g. has no instance option name) so it wont 00241 //be possible for the administrator to create a instance 00242 //in this case we need to create an instance 00243 if (empty($instanceoptionnames)) { 00244 $instanceoptions = array(); 00245 if (empty($this->_options['pluginname'])) { 00246 // when moodle trying to install some repo plugin automatically 00247 // this option will be empty, get it from language string when display 00248 $instanceoptions['name'] = ''; 00249 } else { 00250 // when admin trying to add a plugin manually, he will type a name 00251 // for it 00252 $instanceoptions['name'] = $this->_options['pluginname']; 00253 } 00254 repository::static_function($this->_typename, 'create', $this->_typename, 0, get_system_context(), $instanceoptions); 00255 } 00256 //run plugin_init function 00257 if (!repository::static_function($this->_typename, 'plugin_init')) { 00258 if (!$silent) { 00259 throw new repository_exception('cannotinitplugin', 'repository'); 00260 } 00261 } 00262 00263 if(!empty($plugin_id)) { 00264 // return plugin_id if create successfully 00265 return $plugin_id; 00266 } else { 00267 return false; 00268 } 00269 00270 } else { 00271 if (!$silent) { 00272 throw new repository_exception('existingrepository', 'repository'); 00273 } 00274 // If plugin existed, return false, tell caller no new plugins were created. 00275 return false; 00276 } 00277 } 00278 00279 00285 public function update_options($options = null) { 00286 global $DB; 00287 $classname = 'repository_' . $this->_typename; 00288 $instanceoptions = repository::static_function($this->_typename, 'get_instance_option_names'); 00289 if (empty($instanceoptions)) { 00290 // update repository instance name if this plugin type doesn't have muliti instances 00291 $params = array(); 00292 $params['type'] = $this->_typename; 00293 $instances = repository::get_instances($params); 00294 $instance = array_pop($instances); 00295 if ($instance) { 00296 $DB->set_field('repository_instances', 'name', $options['pluginname'], array('id'=>$instance->id)); 00297 } 00298 unset($options['pluginname']); 00299 } 00300 00301 if (!empty($options)) { 00302 $this->_options = $options; 00303 } 00304 00305 foreach ($this->_options as $name => $value) { 00306 set_config($name, $value, $this->_typename); 00307 } 00308 00309 return true; 00310 } 00311 00321 private function update_visible($visible = null) { 00322 global $DB; 00323 00324 if (!empty($visible)) { 00325 $this->_visible = $visible; 00326 } 00327 else if (!isset($this->_visible)) { 00328 throw new repository_exception('updateemptyvisible', 'repository'); 00329 } 00330 00331 return $DB->set_field('repository', 'visible', $this->_visible, array('type'=>$this->_typename)); 00332 } 00333 00343 private function update_sortorder($sortorder = null) { 00344 global $DB; 00345 00346 if (!empty($sortorder) && $sortorder!=0) { 00347 $this->_sortorder = $sortorder; 00348 } 00349 //if sortorder is not set, we set it as the ;ast position in the list 00350 else if (!isset($this->_sortorder) || $this->_sortorder == 0 ) { 00351 $sql = "SELECT MAX(sortorder) FROM {repository}"; 00352 $this->_sortorder = 1 + $DB->get_field_sql($sql); 00353 } 00354 00355 return $DB->set_field('repository', 'sortorder', $this->_sortorder, array('type'=>$this->_typename)); 00356 } 00357 00368 public function move_order($move) { 00369 global $DB; 00370 00371 $types = repository::get_types(); // retrieve all types 00372 00374 $i = 0; 00375 while (!isset($indice) && $i<count($types)) { 00376 if ($types[$i]->get_typename() == $this->_typename) { 00377 $indice = $i; 00378 } 00379 $i++; 00380 } 00381 00383 switch ($move) { 00384 case "up": 00385 $adjacentindice = $indice - 1; 00386 break; 00387 case "down": 00388 $adjacentindice = $indice + 1; 00389 break; 00390 default: 00391 throw new repository_exception('movenotdefined', 'repository'); 00392 } 00393 00394 //switch sortorder of this type and the adjacent type 00395 //TODO: we could reset sortorder for all types. This is not as good in performance term, but 00396 //that prevent from wrong behaviour on a screwed database. As performance are not important in this particular case 00397 //it worth to change the algo. 00398 if ($adjacentindice>=0 && !empty($types[$adjacentindice])) { 00399 $DB->set_field('repository', 'sortorder', $this->_sortorder, array('type'=>$types[$adjacentindice]->get_typename())); 00400 $this->update_sortorder($types[$adjacentindice]->get_sortorder()); 00401 } 00402 } 00403 00410 public function update_visibility($visible = null) { 00411 if (is_bool($visible)) { 00412 $this->_visible = $visible; 00413 } else { 00414 $this->_visible = !$this->_visible; 00415 } 00416 return $this->update_visible(); 00417 } 00418 00419 00426 public function delete() { 00427 global $DB; 00428 00429 //delete all instances of this type 00430 $params = array(); 00431 $params['context'] = array(); 00432 $params['onlyvisible'] = false; 00433 $params['type'] = $this->_typename; 00434 $instances = repository::get_instances($params); 00435 foreach ($instances as $instance) { 00436 $instance->delete(); 00437 } 00438 00439 //delete all general options 00440 foreach ($this->_options as $name => $value) { 00441 set_config($name, null, $this->_typename); 00442 } 00443 00444 return $DB->delete_records('repository', array('type' => $this->_typename)); 00445 } 00446 } 00447 00472 abstract class repository { 00473 // $disabled can be set to true to disable a plugin by force 00474 // example: self::$disabled = true 00475 public $disabled = false; 00476 public $id; 00478 public $context; 00479 public $options; 00480 public $readonly; 00481 public $returntypes; 00483 public $instance; 00492 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly = 0) { 00493 global $DB; 00494 $this->id = $repositoryid; 00495 if (is_object($context)) { 00496 $this->context = $context; 00497 } else { 00498 $this->context = get_context_instance_by_id($context); 00499 } 00500 $this->instance = $DB->get_record('repository_instances', array('id'=>$this->id)); 00501 $this->readonly = $readonly; 00502 $this->options = array(); 00503 00504 if (is_array($options)) { 00505 $options = array_merge($this->get_option(), $options); 00506 } else { 00507 $options = $this->get_option(); 00508 } 00509 foreach ($options as $n => $v) { 00510 $this->options[$n] = $v; 00511 } 00512 $this->name = $this->get_name(); 00513 $this->returntypes = $this->supported_returntypes(); 00514 $this->super_called = true; 00515 } 00516 00523 public static function get_type_by_typename($typename) { 00524 global $DB; 00525 00526 if (!$record = $DB->get_record('repository',array('type' => $typename))) { 00527 return false; 00528 } 00529 00530 return new repository_type($typename, (array)get_config($typename), $record->visible, $record->sortorder); 00531 } 00532 00539 public static function get_type_by_id($id) { 00540 global $DB; 00541 00542 if (!$record = $DB->get_record('repository',array('id' => $id))) { 00543 return false; 00544 } 00545 00546 return new repository_type($record->type, (array)get_config($record->type), $record->visible, $record->sortorder); 00547 } 00548 00557 public static function get_types($visible=null) { 00558 global $DB, $CFG; 00559 00560 $types = array(); 00561 $params = null; 00562 if (!empty($visible)) { 00563 $params = array('visible' => $visible); 00564 } 00565 if ($records = $DB->get_records('repository',$params,'sortorder')) { 00566 foreach($records as $type) { 00567 if (file_exists($CFG->dirroot . '/repository/'. $type->type .'/lib.php')) { 00568 $types[] = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder); 00569 } 00570 } 00571 } 00572 00573 return $types; 00574 } 00575 00582 public static function check_capability($contextid, $instance) { 00583 $context = get_context_instance_by_id($contextid); 00584 $capability = has_capability('repository/'.$instance->type.':view', $context); 00585 if (!$capability) { 00586 throw new repository_exception('nopermissiontoaccess', 'repository'); 00587 } 00588 } 00589 00598 public static function draftfile_exists($itemid, $filepath, $filename) { 00599 global $USER; 00600 $fs = get_file_storage(); 00601 $usercontext = get_context_instance(CONTEXT_USER, $USER->id); 00602 if ($fs->get_file($usercontext->id, 'user', 'draft', $itemid, $filepath, $filename)) { 00603 return true; 00604 } else { 00605 return false; 00606 } 00607 } 00608 00614 public function has_moodle_files() { 00615 return false; 00616 } 00628 public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) { 00629 global $USER, $DB; 00630 00631 if ($this->has_moodle_files() == false) { 00632 throw new coding_exception('Only repository used to browse moodle files can use copy_to_area'); 00633 } 00634 00635 $browser = get_file_browser(); 00636 $params = unserialize(base64_decode($encoded)); 00637 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 00638 00639 $contextid = clean_param($params['contextid'], PARAM_INT); 00640 $fileitemid = clean_param($params['itemid'], PARAM_INT); 00641 $filename = clean_param($params['filename'], PARAM_FILE); 00642 $filepath = clean_param($params['filepath'], PARAM_PATH);; 00643 $filearea = clean_param($params['filearea'], PARAM_AREA); 00644 $component = clean_param($params['component'], PARAM_COMPONENT); 00645 00646 $context = get_context_instance_by_id($contextid); 00647 // the file needs to copied to draft area 00648 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); 00649 00650 if (repository::draftfile_exists($draftitemid, $new_filepath, $new_filename)) { 00651 // create new file 00652 $unused_filename = repository::get_unused_filename($draftitemid, $new_filepath, $new_filename); 00653 $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $unused_filename); 00654 $event = array(); 00655 $event['event'] = 'fileexists'; 00656 $event['newfile'] = new stdClass; 00657 $event['newfile']->filepath = $new_filepath; 00658 $event['newfile']->filename = $unused_filename; 00659 $event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $unused_filename)->out(); 00660 $event['existingfile'] = new stdClass; 00661 $event['existingfile']->filepath = $new_filepath; 00662 $event['existingfile']->filename = $new_filename; 00663 $event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $filepath, $filename)->out();; 00664 return $event; 00665 } else { 00666 $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename); 00667 $info = array(); 00668 $info['itemid'] = $draftitemid; 00669 $info['title'] = $new_filename; 00670 $info['contextid'] = $user_context->id; 00671 $info['url'] = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();; 00672 $info['filesize'] = $file_info->get_filesize(); 00673 return $info; 00674 } 00675 } 00676 00685 public static function get_unused_filename($itemid, $filepath, $filename) { 00686 global $USER; 00687 $fs = get_file_storage(); 00688 while (repository::draftfile_exists($itemid, $filepath, $filename)) { 00689 $filename = repository::append_suffix($filename); 00690 } 00691 return $filename; 00692 } 00693 00700 function append_suffix($filename) { 00701 $pathinfo = pathinfo($filename); 00702 if (empty($pathinfo['extension'])) { 00703 return $filename . RENAME_SUFFIX; 00704 } else { 00705 return $pathinfo['filename'] . RENAME_SUFFIX . '.' . $pathinfo['extension']; 00706 } 00707 } 00708 00715 public static function get_editable_types($context = null) { 00716 00717 if (empty($context)) { 00718 $context = get_system_context(); 00719 } 00720 00721 $types= repository::get_types(true); 00722 $editabletypes = array(); 00723 foreach ($types as $type) { 00724 $instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names'); 00725 if (!empty($instanceoptionnames)) { 00726 if ($type->get_contextvisibility($context)) { 00727 $editabletypes[]=$type; 00728 } 00729 } 00730 } 00731 return $editabletypes; 00732 } 00733 00751 public static function get_instances($args = array()) { 00752 global $DB, $CFG, $USER; 00753 00754 if (isset($args['currentcontext'])) { 00755 $current_context = $args['currentcontext']; 00756 } else { 00757 $current_context = null; 00758 } 00759 00760 if (!empty($args['context'])) { 00761 $contexts = $args['context']; 00762 } else { 00763 $contexts = array(); 00764 } 00765 00766 $onlyvisible = isset($args['onlyvisible']) ? $args['onlyvisible'] : true; 00767 $returntypes = isset($args['return_types']) ? $args['return_types'] : 3; 00768 $type = isset($args['type']) ? $args['type'] : null; 00769 00770 $params = array(); 00771 $sql = "SELECT i.*, r.type AS repositorytype, r.sortorder, r.visible 00772 FROM {repository} r, {repository_instances} i 00773 WHERE i.typeid = r.id "; 00774 00775 if (!empty($args['disable_types']) && is_array($args['disable_types'])) { 00776 list($types, $p) = $DB->get_in_or_equal($args['disable_types'], SQL_PARAMS_QM, 'param', false); 00777 $sql .= " AND r.type $types"; 00778 $params = array_merge($params, $p); 00779 } 00780 00781 if (!empty($args['userid']) && is_numeric($args['userid'])) { 00782 $sql .= " AND (i.userid = 0 or i.userid = ?)"; 00783 $params[] = $args['userid']; 00784 } 00785 00786 foreach ($contexts as $context) { 00787 if (empty($firstcontext)) { 00788 $firstcontext = true; 00789 $sql .= " AND ((i.contextid = ?)"; 00790 } else { 00791 $sql .= " OR (i.contextid = ?)"; 00792 } 00793 $params[] = $context->id; 00794 } 00795 00796 if (!empty($firstcontext)) { 00797 $sql .=')'; 00798 } 00799 00800 if ($onlyvisible == true) { 00801 $sql .= " AND (r.visible = 1)"; 00802 } 00803 00804 if (isset($type)) { 00805 $sql .= " AND (r.type = ?)"; 00806 $params[] = $type; 00807 } 00808 $sql .= " ORDER BY r.sortorder, i.name"; 00809 00810 if (!$records = $DB->get_records_sql($sql, $params)) { 00811 $records = array(); 00812 } 00813 00814 $repositories = array(); 00815 $ft = new filetype_parser(); 00816 if (isset($args['accepted_types'])) { 00817 $accepted_types = $args['accepted_types']; 00818 } else { 00819 $accepted_types = '*'; 00820 } 00821 // Sortorder should be unique, which is not true if we use $record->sortorder 00822 // and there are multiple instances of any repository type 00823 $sortorder = 1; 00824 foreach ($records as $record) { 00825 if (!file_exists($CFG->dirroot . '/repository/'. $record->repositorytype.'/lib.php')) { 00826 continue; 00827 } 00828 require_once($CFG->dirroot . '/repository/'. $record->repositorytype.'/lib.php'); 00829 $options['visible'] = $record->visible; 00830 $options['type'] = $record->repositorytype; 00831 $options['typeid'] = $record->typeid; 00832 $options['sortorder'] = $sortorder++; 00833 // tell instance what file types will be accepted by file picker 00834 $classname = 'repository_' . $record->repositorytype; 00835 00836 $repository = new $classname($record->id, $record->contextid, $options, $record->readonly); 00837 00838 $is_supported = true; 00839 00840 if (empty($repository->super_called)) { 00841 // to make sure the super construct is called 00842 debugging('parent::__construct must be called by '.$record->repositorytype.' plugin.'); 00843 } else { 00844 // check mimetypes 00845 if ($accepted_types !== '*' and $repository->supported_filetypes() !== '*') { 00846 $accepted_types = $ft->get_extensions($accepted_types); 00847 $supported_filetypes = $ft->get_extensions($repository->supported_filetypes()); 00848 00849 $is_supported = false; 00850 foreach ($supported_filetypes as $type) { 00851 if (in_array($type, $accepted_types)) { 00852 $is_supported = true; 00853 } 00854 } 00855 00856 } 00857 // check return values 00858 if ($returntypes !== 3 and $repository->supported_returntypes() !== 3) { 00859 $type = $repository->supported_returntypes(); 00860 if ($type & $returntypes) { 00861 // 00862 } else { 00863 $is_supported = false; 00864 } 00865 } 00866 00867 if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) { 00868 // check capability in current context 00869 if (!empty($current_context)) { 00870 $capability = has_capability('repository/'.$record->repositorytype.':view', $current_context); 00871 } else { 00872 $capability = has_capability('repository/'.$record->repositorytype.':view', get_system_context()); 00873 } 00874 if ($record->repositorytype == 'coursefiles') { 00875 // coursefiles plugin needs managefiles permission 00876 if (!empty($current_context)) { 00877 $capability = $capability && has_capability('moodle/course:managefiles', $current_context); 00878 } else { 00879 $capability = $capability && has_capability('moodle/course:managefiles', get_system_context()); 00880 } 00881 } 00882 if ($is_supported && $capability) { 00883 $repositories[$repository->id] = $repository; 00884 } 00885 } 00886 } 00887 } 00888 return $repositories; 00889 } 00890 00898 public static function get_instance($id) { 00899 global $DB, $CFG; 00900 $sql = "SELECT i.*, r.type AS repositorytype, r.visible 00901 FROM {repository} r 00902 JOIN {repository_instances} i ON i.typeid = r.id 00903 WHERE i.id = ?"; 00904 00905 if (!$instance = $DB->get_record_sql($sql, array($id))) { 00906 return false; 00907 } 00908 require_once($CFG->dirroot . '/repository/'. $instance->repositorytype.'/lib.php'); 00909 $classname = 'repository_' . $instance->repositorytype; 00910 $options['typeid'] = $instance->typeid; 00911 $options['type'] = $instance->repositorytype; 00912 $options['name'] = $instance->name; 00913 $obj = new $classname($instance->id, $instance->contextid, $options, $instance->readonly); 00914 if (empty($obj->super_called)) { 00915 debugging('parent::__construct must be called by '.$classname.' plugin.'); 00916 } 00917 return $obj; 00918 } 00919 00927 public static function static_function($plugin, $function) { 00928 global $CFG; 00929 00930 //check that the plugin exists 00931 $typedirectory = $CFG->dirroot . '/repository/'. $plugin . '/lib.php'; 00932 if (!file_exists($typedirectory)) { 00933 //throw new repository_exception('invalidplugin', 'repository'); 00934 return false; 00935 } 00936 00937 $pname = null; 00938 if (is_object($plugin) || is_array($plugin)) { 00939 $plugin = (object)$plugin; 00940 $pname = $plugin->name; 00941 } else { 00942 $pname = $plugin; 00943 } 00944 00945 $args = func_get_args(); 00946 if (count($args) <= 2) { 00947 $args = array(); 00948 } else { 00949 array_shift($args); 00950 array_shift($args); 00951 } 00952 00953 require_once($typedirectory); 00954 return call_user_func_array(array('repository_' . $plugin, $function), $args); 00955 } 00956 00969 public static function antivir_scan_file($thefile, $filename, $deleteinfected) { 00970 global $CFG; 00971 00972 if (!is_readable($thefile)) { 00973 // this should not happen 00974 return; 00975 } 00976 00977 if (empty($CFG->runclamonupload) or empty($CFG->pathtoclam)) { 00978 // clam not enabled 00979 return; 00980 } 00981 00982 $CFG->pathtoclam = trim($CFG->pathtoclam); 00983 00984 if (!file_exists($CFG->pathtoclam) or !is_executable($CFG->pathtoclam)) { 00985 // misconfigured clam - use the old notification for now 00986 require("$CFG->libdir/uploadlib.php"); 00987 $notice = get_string('clamlost', 'moodle', $CFG->pathtoclam); 00988 clam_message_admins($notice); 00989 return; 00990 } 00991 00992 // do NOT mess with permissions here, the calling party is responsible for making 00993 // sure the scanner engine can access the files! 00994 00995 // execute test 00996 $cmd = escapeshellcmd($CFG->pathtoclam).' --stdout '.escapeshellarg($thefile); 00997 exec($cmd, $output, $return); 00998 00999 if ($return == 0) { 01000 // perfect, no problem found 01001 return; 01002 01003 } else if ($return == 1) { 01004 // infection found 01005 if ($deleteinfected) { 01006 unlink($thefile); 01007 } 01008 throw new moodle_exception('virusfounduser', 'moodle', '', array('filename'=>$filename)); 01009 01010 } else { 01011 //unknown problem 01012 require("$CFG->libdir/uploadlib.php"); 01013 $notice = get_string('clamfailed', 'moodle', get_clam_error_code($return)); 01014 $notice .= "\n\n". implode("\n", $output); 01015 clam_message_admins($notice); 01016 if ($CFG->clamfailureonupload === 'actlikevirus') { 01017 if ($deleteinfected) { 01018 unlink($thefile); 01019 } 01020 throw new moodle_exception('virusfounduser', 'moodle', '', array('filename'=>$filename)); 01021 } else { 01022 return; 01023 } 01024 } 01025 } 01026 01041 public static function move_to_filepool($thefile, $record) { 01042 global $DB, $CFG, $USER, $OUTPUT; 01043 01044 // scan for viruses if possible, throws exception if problem found 01045 self::antivir_scan_file($thefile, $record->filename, empty($CFG->repository_no_delete)); //TODO: MDL-28637 this repository_no_delete is a bloody hack! 01046 01047 if ($record->filepath !== '/') { 01048 $record->filepath = trim($record->filepath, '/'); 01049 $record->filepath = '/'.$record->filepath.'/'; 01050 } 01051 $context = get_context_instance(CONTEXT_USER, $USER->id); 01052 $now = time(); 01053 01054 $record->contextid = $context->id; 01055 $record->component = 'user'; 01056 $record->filearea = 'draft'; 01057 $record->timecreated = $now; 01058 $record->timemodified = $now; 01059 $record->userid = $USER->id; 01060 $record->mimetype = mimeinfo('type', $thefile); 01061 if(!is_numeric($record->itemid)) { 01062 $record->itemid = 0; 01063 } 01064 $fs = get_file_storage(); 01065 if ($existingfile = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) { 01066 $draftitemid = $record->itemid; 01067 $new_filename = repository::get_unused_filename($draftitemid, $record->filepath, $record->filename); 01068 $old_filename = $record->filename; 01069 // create a tmp file 01070 $record->filename = $new_filename; 01071 $newfile = $fs->create_file_from_pathname($record, $thefile); 01072 $event = array(); 01073 $event['event'] = 'fileexists'; 01074 $event['newfile'] = new stdClass; 01075 $event['newfile']->filepath = $record->filepath; 01076 $event['newfile']->filename = $new_filename; 01077 $event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $new_filename)->out(); 01078 01079 $event['existingfile'] = new stdClass; 01080 $event['existingfile']->filepath = $record->filepath; 01081 $event['existingfile']->filename = $old_filename; 01082 $event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $old_filename)->out();; 01083 return $event; 01084 } 01085 if ($file = $fs->create_file_from_pathname($record, $thefile)) { 01086 if (empty($CFG->repository_no_delete)) { 01087 $delete = unlink($thefile); 01088 unset($CFG->repository_no_delete); 01089 } 01090 return array( 01091 'url'=>moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename())->out(), 01092 'id'=>$file->get_itemid(), 01093 'file'=>$file->get_filename(), 01094 'icon' => $OUTPUT->pix_url(file_extension_icon($thefile, 32))->out(), 01095 ); 01096 } else { 01097 return null; 01098 } 01099 } 01100 01113 public static function build_tree($fileinfo, $search, $dynamicmode, &$list) { 01114 global $CFG, $OUTPUT; 01115 01116 $filecount = 0; 01117 $children = $fileinfo->get_children(); 01118 01119 foreach ($children as $child) { 01120 $filename = $child->get_visible_name(); 01121 $filesize = $child->get_filesize(); 01122 $filesize = $filesize ? display_size($filesize) : ''; 01123 $filedate = $child->get_timemodified(); 01124 $filedate = $filedate ? userdate($filedate) : ''; 01125 $filetype = $child->get_mimetype(); 01126 01127 if ($child->is_directory()) { 01128 $path = array(); 01129 $level = $child->get_parent(); 01130 while ($level) { 01131 $params = $level->get_params(); 01132 $path[] = array($params['filepath'], $level->get_visible_name()); 01133 $level = $level->get_parent(); 01134 } 01135 01136 $tmp = array( 01137 'title' => $child->get_visible_name(), 01138 'size' => 0, 01139 'date' => $filedate, 01140 'path' => array_reverse($path), 01141 'thumbnail' => $OUTPUT->pix_url('f/folder-32') 01142 ); 01143 01144 //if ($dynamicmode && $child->is_writable()) { 01145 // $tmp['children'] = array(); 01146 //} else { 01147 // if folder name matches search, we send back all files contained. 01148 $_search = $search; 01149 if ($search && stristr($tmp['title'], $search) !== false) { 01150 $_search = false; 01151 } 01152 $tmp['children'] = array(); 01153 $_filecount = repository::build_tree($child, $_search, $dynamicmode, $tmp['children']); 01154 if ($search && $_filecount) { 01155 $tmp['expanded'] = 1; 01156 } 01157 01158 //} 01159 01160 if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) { 01161 $filecount += $_filecount; 01162 $list[] = $tmp; 01163 } 01164 01165 } else { // not a directory 01166 // skip the file, if we're in search mode and it's not a match 01167 if ($search && (stristr($filename, $search) === false)) { 01168 continue; 01169 } 01170 $params = $child->get_params(); 01171 $source = serialize(array($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename'])); 01172 $list[] = array( 01173 'title' => $filename, 01174 'size' => $filesize, 01175 'date' => $filedate, 01176 //'source' => $child->get_url(), 01177 'source' => base64_encode($source), 01178 'thumbnail'=>$OUTPUT->pix_url(file_extension_icon($filename, 32)), 01179 ); 01180 $filecount++; 01181 } 01182 } 01183 01184 return $filecount; 01185 } 01186 01187 01196 public static function display_instances_list($context, $typename = null) { 01197 global $CFG, $USER, $OUTPUT; 01198 01199 $output = $OUTPUT->box_start('generalbox'); 01200 //if the context is SYSTEM, so we call it from administration page 01201 $admin = ($context->id == SYSCONTEXTID) ? true : false; 01202 if ($admin) { 01203 $baseurl = new moodle_url('/'.$CFG->admin.'/repositoryinstance.php', array('sesskey'=>sesskey())); 01204 $output .= $OUTPUT->heading(get_string('siteinstances', 'repository')); 01205 } else { 01206 $baseurl = new moodle_url('/repository/manage_instances.php', array('contextid'=>$context->id, 'sesskey'=>sesskey())); 01207 } 01208 $url = $baseurl; 01209 01210 $namestr = get_string('name'); 01211 $pluginstr = get_string('plugin', 'repository'); 01212 $settingsstr = get_string('settings'); 01213 $deletestr = get_string('delete'); 01214 //retrieve list of instances. In administration context we want to display all 01215 //instances of a type, even if this type is not visible. In course/user context we 01216 //want to display only visible instances, but for every type types. The repository::get_instances() 01217 //third parameter displays only visible type. 01218 $params = array(); 01219 $params['context'] = array($context, get_system_context()); 01220 $params['currentcontext'] = $context; 01221 $params['onlyvisible'] = !$admin; 01222 $params['type'] = $typename; 01223 $instances = repository::get_instances($params); 01224 $instancesnumber = count($instances); 01225 $alreadyplugins = array(); 01226 01227 $table = new html_table(); 01228 $table->head = array($namestr, $pluginstr, $settingsstr, $deletestr); 01229 $table->align = array('left', 'left', 'center','center'); 01230 $table->data = array(); 01231 01232 $updowncount = 1; 01233 01234 foreach ($instances as $i) { 01235 $settings = ''; 01236 $delete = ''; 01237 01238 $type = repository::get_type_by_id($i->options['typeid']); 01239 01240 if ($type->get_contextvisibility($context)) { 01241 if (!$i->readonly) { 01242 01243 $url->param('type', $i->options['type']); 01244 $url->param('edit', $i->id); 01245 $settings .= html_writer::link($url, $settingsstr); 01246 01247 $url->remove_params('edit'); 01248 $url->param('delete', $i->id); 01249 $delete .= html_writer::link($url, $deletestr); 01250 01251 $url->remove_params('type'); 01252 } 01253 } 01254 01255 $type = repository::get_type_by_id($i->options['typeid']); 01256 $table->data[] = array($i->name, $type->get_readablename(), $settings, $delete); 01257 01258 //display a grey row if the type is defined as not visible 01259 if (isset($type) && !$type->get_visible()) { 01260 $table->rowclasses[] = 'dimmed_text'; 01261 } else { 01262 $table->rowclasses[] = ''; 01263 } 01264 01265 if (!in_array($i->name, $alreadyplugins)) { 01266 $alreadyplugins[] = $i->name; 01267 } 01268 } 01269 $output .= html_writer::table($table); 01270 $instancehtml = '<div>'; 01271 $addable = 0; 01272 01273 //if no type is set, we can create all type of instance 01274 if (!$typename) { 01275 $instancehtml .= '<h3>'; 01276 $instancehtml .= get_string('createrepository', 'repository'); 01277 $instancehtml .= '</h3><ul>'; 01278 $types = repository::get_editable_types($context); 01279 foreach ($types as $type) { 01280 if (!empty($type) && $type->get_visible()) { 01281 $instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names'); 01282 if (!empty($instanceoptionnames)) { 01283 $baseurl->param('new', $type->get_typename()); 01284 $instancehtml .= '<li><a href="'.$baseurl->out().'">'.get_string('createxxinstance', 'repository', get_string('pluginname', 'repository_'.$type->get_typename())). '</a></li>'; 01285 $baseurl->remove_params('new'); 01286 $addable++; 01287 } 01288 } 01289 } 01290 $instancehtml .= '</ul>'; 01291 01292 } else { 01293 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names'); 01294 if (!empty($instanceoptionnames)) { //create a unique type of instance 01295 $addable = 1; 01296 $baseurl->param('new', $typename); 01297 $instancehtml .= "<form action='".$baseurl->out()."' method='post'> 01298 <p><input type='submit' value='".get_string('createinstance', 'repository')."'/></p> 01299 </form>"; 01300 $baseurl->remove_params('new'); 01301 } 01302 } 01303 01304 if ($addable) { 01305 $instancehtml .= '</div>'; 01306 $output .= $instancehtml; 01307 } 01308 01309 $output .= $OUTPUT->box_end(); 01310 01311 //print the list + creation links 01312 print($output); 01313 } 01314 01319 public function prepare_file($filename) { 01320 global $CFG; 01321 if (!file_exists($CFG->tempdir.'/download')) { 01322 mkdir($CFG->tempdir.'/download/', $CFG->directorypermissions, true); 01323 } 01324 if (is_dir($CFG->tempdir.'/download')) { 01325 $dir = $CFG->tempdir.'/download/'; 01326 } 01327 if (empty($filename)) { 01328 $filename = uniqid('repo', true).'_'.time().'.tmp'; 01329 } 01330 if (file_exists($dir.$filename)) { 01331 $filename = uniqid('m').$filename; 01332 } 01333 return $dir.$filename; 01334 } 01335 01344 public function get_link($url) { 01345 return $url; 01346 } 01347 01358 public function get_file($url, $filename = '') { 01359 global $CFG; 01360 $path = $this->prepare_file($filename); 01361 $fp = fopen($path, 'w'); 01362 $c = new curl; 01363 $c->download(array(array('url'=>$url, 'file'=>$fp))); 01364 return array('path'=>$path, 'url'=>$url); 01365 } 01366 01373 public function get_file_size($source) { 01374 $browser = get_file_browser(); 01375 $params = unserialize(base64_decode($source)); 01376 $contextid = clean_param($params['contextid'], PARAM_INT); 01377 $fileitemid = clean_param($params['itemid'], PARAM_INT); 01378 $filename = clean_param($params['filename'], PARAM_FILE); 01379 $filepath = clean_param($params['filepath'], PARAM_PATH); 01380 $filearea = clean_param($params['filearea'], PARAM_AREA); 01381 $component = clean_param($params['component'], PARAM_COMPONENT); 01382 $context = get_context_instance_by_id($contextid); 01383 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); 01384 if (!empty($file_info)) { 01385 $filesize = $file_info->get_filesize(); 01386 } else { 01387 $filesize = null; 01388 } 01389 return $filesize; 01390 } 01391 01397 public function is_visible() { 01398 $type = repository::get_type_by_id($this->options['typeid']); 01399 $instanceoptions = repository::static_function($type->get_typename(), 'get_instance_option_names'); 01400 01401 if ($type->get_visible()) { 01402 //if the instance is unique so it's visible, otherwise check if the instance has a enabled context 01403 if (empty($instanceoptions) || $type->get_contextvisibility($this->context)) { 01404 return true; 01405 } 01406 } 01407 01408 return false; 01409 } 01410 01416 public function get_name() { 01417 global $DB; 01418 if ( $name = $this->instance->name ) { 01419 return $name; 01420 } else { 01421 return get_string('pluginname', 'repository_' . $this->options['type']); 01422 } 01423 } 01424 01430 public function supported_filetypes() { 01431 // return array('text/plain', 'image/gif'); 01432 return '*'; 01433 } 01434 01439 public function supported_returntypes() { 01440 return (FILE_INTERNAL | FILE_EXTERNAL); 01441 } 01442 01448 final public function get_meta() { 01449 global $CFG, $OUTPUT; 01450 $ft = new filetype_parser; 01451 $meta = new stdClass(); 01452 $meta->id = $this->id; 01453 $meta->name = $this->get_name(); 01454 $meta->type = $this->options['type']; 01455 $meta->icon = $OUTPUT->pix_url('icon', 'repository_'.$meta->type)->out(false); 01456 $meta->supported_types = $ft->get_extensions($this->supported_filetypes()); 01457 $meta->return_types = $this->supported_returntypes(); 01458 $meta->sortorder = $this->options['sortorder']; 01459 return $meta; 01460 } 01461 01473 public static function create($type, $userid, $context, $params, $readonly=0) { 01474 global $CFG, $DB; 01475 $params = (array)$params; 01476 require_once($CFG->dirroot . '/repository/'. $type . '/lib.php'); 01477 $classname = 'repository_' . $type; 01478 if ($repo = $DB->get_record('repository', array('type'=>$type))) { 01479 $record = new stdClass(); 01480 $record->name = $params['name']; 01481 $record->typeid = $repo->id; 01482 $record->timecreated = time(); 01483 $record->timemodified = time(); 01484 $record->contextid = $context->id; 01485 $record->readonly = $readonly; 01486 $record->userid = $userid; 01487 $id = $DB->insert_record('repository_instances', $record); 01488 $options = array(); 01489 $configs = call_user_func($classname . '::get_instance_option_names'); 01490 if (!empty($configs)) { 01491 foreach ($configs as $config) { 01492 if (isset($params[$config])) { 01493 $options[$config] = $params[$config]; 01494 } else { 01495 $options[$config] = null; 01496 } 01497 } 01498 } 01499 01500 if (!empty($id)) { 01501 unset($options['name']); 01502 $instance = repository::get_instance($id); 01503 $instance->set_option($options); 01504 return $id; 01505 } else { 01506 return null; 01507 } 01508 } else { 01509 return null; 01510 } 01511 } 01512 01518 final public function delete() { 01519 global $DB; 01520 $DB->delete_records('repository_instances', array('id'=>$this->id)); 01521 $DB->delete_records('repository_instance_config', array('instanceid'=>$this->id)); 01522 return true; 01523 } 01524 01531 final public function hide($hide = 'toggle') { 01532 global $DB; 01533 if ($entry = $DB->get_record('repository', array('id'=>$this->id))) { 01534 if ($hide === 'toggle' ) { 01535 if (!empty($entry->visible)) { 01536 $entry->visible = 0; 01537 } else { 01538 $entry->visible = 1; 01539 } 01540 } else { 01541 if (!empty($hide)) { 01542 $entry->visible = 0; 01543 } else { 01544 $entry->visible = 1; 01545 } 01546 } 01547 return $DB->update_record('repository', $entry); 01548 } 01549 return false; 01550 } 01551 01559 public function set_option($options = array()) { 01560 global $DB; 01561 01562 if (!empty($options['name'])) { 01563 $r = new stdClass(); 01564 $r->id = $this->id; 01565 $r->name = $options['name']; 01566 $DB->update_record('repository_instances', $r); 01567 unset($options['name']); 01568 } 01569 foreach ($options as $name=>$value) { 01570 if ($id = $DB->get_field('repository_instance_config', 'id', array('name'=>$name, 'instanceid'=>$this->id))) { 01571 $DB->set_field('repository_instance_config', 'value', $value, array('id'=>$id)); 01572 } else { 01573 $config = new stdClass(); 01574 $config->instanceid = $this->id; 01575 $config->name = $name; 01576 $config->value = $value; 01577 $DB->insert_record('repository_instance_config', $config); 01578 } 01579 } 01580 return true; 01581 } 01582 01589 public function get_option($config = '') { 01590 global $DB; 01591 $entries = $DB->get_records('repository_instance_config', array('instanceid'=>$this->id)); 01592 $ret = array(); 01593 if (empty($entries)) { 01594 return $ret; 01595 } 01596 foreach($entries as $entry) { 01597 $ret[$entry->name] = $entry->value; 01598 } 01599 if (!empty($config)) { 01600 if (isset($ret[$config])) { 01601 return $ret[$config]; 01602 } else { 01603 return null; 01604 } 01605 } else { 01606 return $ret; 01607 } 01608 } 01609 01610 public function filter(&$value) { 01611 $pass = false; 01612 $accepted_types = optional_param_array('accepted_types', '', PARAM_RAW); 01613 $ft = new filetype_parser; 01614 //$ext = $ft->get_extensions($this->supported_filetypes()); 01615 if (isset($value['children'])) { 01616 $pass = true; 01617 if (!empty($value['children'])) { 01618 $value['children'] = array_filter($value['children'], array($this, 'filter')); 01619 } 01620 } else { 01621 if ($accepted_types == '*' or empty($accepted_types) 01622 or (is_array($accepted_types) and in_array('*', $accepted_types))) { 01623 $pass = true; 01624 } elseif (is_array($accepted_types)) { 01625 foreach ($accepted_types as $type) { 01626 $extensions = $ft->get_extensions($type); 01627 if (!is_array($extensions)) { 01628 $pass = true; 01629 } else { 01630 foreach ($extensions as $ext) { 01631 if (preg_match('#'.$ext.'$#i', $value['title'])) { 01632 $pass = true; 01633 } 01634 } 01635 } 01636 } 01637 } 01638 } 01639 return $pass; 01640 } 01641 01665 public function get_listing($path = '', $page = '') { 01666 } 01667 01675 public function search($search_text) { 01676 $list = array(); 01677 $list['list'] = array(); 01678 return false; 01679 } 01680 01687 public function logout(){ 01688 return $this->print_login(); 01689 } 01690 01696 public function check_login(){ 01697 return true; 01698 } 01699 01700 01704 public function print_login(){ 01705 return $this->get_listing(); 01706 } 01707 01712 public function print_search() { 01713 $str = ''; 01714 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />'; 01715 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />'; 01716 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />'; 01717 $str .= '<label>'.get_string('keyword', 'repository').': </label><br/><input name="s" value="" /><br/>'; 01718 return $str; 01719 } 01720 01725 public function callback() { 01726 } 01727 01732 public function global_search() { 01733 return false; 01734 } 01735 01740 public function cron() { 01741 return true; 01742 } 01743 01748 public static function plugin_init() { 01749 return true; 01750 } 01751 01757 public function type_config_form($mform, $classname = 'repository') { 01758 $instnaceoptions = call_user_func(array($classname, 'get_instance_option_names'), $mform, $classname); 01759 if (empty($instnaceoptions)) { 01760 // this plugin has only one instance 01761 // so we need to give it a name 01762 // it can be empty, then moodle will look for instance name from language string 01763 $mform->addElement('text', 'pluginname', get_string('pluginname', 'repository'), array('size' => '40')); 01764 $mform->addElement('static', 'pluginnamehelp', '', get_string('pluginnamehelp', 'repository')); 01765 } 01766 } 01767 01775 public static function type_form_validation($mform, $data, $errors) { 01776 return $errors; 01777 } 01778 01779 01784 public function instance_config_form($mform) { 01785 } 01786 01792 public static function get_type_option_names() { 01793 return array('pluginname'); 01794 } 01795 01801 public static function get_instance_option_names() { 01802 return array(); 01803 } 01804 01805 public static function instance_form_validation($mform, $data, $errors) { 01806 return $errors; 01807 } 01808 01809 public function get_short_filename($str, $maxlength) { 01810 if (textlib::strlen($str) >= $maxlength) { 01811 return trim(textlib::substr($str, 0, $maxlength)).'...'; 01812 } else { 01813 return $str; 01814 } 01815 } 01816 01827 function overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename) { 01828 global $USER; 01829 $fs = get_file_storage(); 01830 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 01831 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $filepath, $filename)) { 01832 if ($tempfile = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $newfilepath, $newfilename)) { 01833 // delete existing file to release filename 01834 $file->delete(); 01835 // create new file 01836 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$filepath, 'filename'=>$filename), $tempfile); 01837 // remove temp file 01838 $tempfile->delete(); 01839 return true; 01840 } 01841 } 01842 return false; 01843 } 01844 01853 function delete_tempfile_from_draft($draftitemid, $filepath, $filename) { 01854 global $USER; 01855 $fs = get_file_storage(); 01856 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 01857 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $filepath, $filename)) { 01858 $file->delete(); 01859 return true; 01860 } else { 01861 return false; 01862 } 01863 } 01864 } 01865 01875 class repository_exception extends moodle_exception { 01876 } 01877 01887 final class repository_instance_form extends moodleform { 01888 protected $instance; 01889 protected $plugin; 01890 protected function add_defaults() { 01891 $mform =& $this->_form; 01892 $strrequired = get_string('required'); 01893 01894 $mform->addElement('hidden', 'edit', ($this->instance) ? $this->instance->id : 0); 01895 $mform->setType('edit', PARAM_INT); 01896 $mform->addElement('hidden', 'new', $this->plugin); 01897 $mform->setType('new', PARAM_FORMAT); 01898 $mform->addElement('hidden', 'plugin', $this->plugin); 01899 $mform->setType('plugin', PARAM_PLUGIN); 01900 $mform->addElement('hidden', 'typeid', $this->typeid); 01901 $mform->setType('typeid', PARAM_INT); 01902 $mform->addElement('hidden', 'contextid', $this->contextid); 01903 $mform->setType('contextid', PARAM_INT); 01904 01905 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"'); 01906 $mform->addRule('name', $strrequired, 'required', null, 'client'); 01907 } 01908 01909 public function definition() { 01910 global $CFG; 01911 // type of plugin, string 01912 $this->plugin = $this->_customdata['plugin']; 01913 $this->typeid = $this->_customdata['typeid']; 01914 $this->contextid = $this->_customdata['contextid']; 01915 $this->instance = (isset($this->_customdata['instance']) 01916 && is_subclass_of($this->_customdata['instance'], 'repository')) 01917 ? $this->_customdata['instance'] : null; 01918 01919 $mform =& $this->_form; 01920 01921 $this->add_defaults(); 01922 //add fields 01923 if (!$this->instance) { 01924 $result = repository::static_function($this->plugin, 'instance_config_form', $mform); 01925 if ($result === false) { 01926 $mform->removeElement('name'); 01927 } 01928 } else { 01929 $data = array(); 01930 $data['name'] = $this->instance->name; 01931 if (!$this->instance->readonly) { 01932 $result = $this->instance->instance_config_form($mform); 01933 if ($result === false) { 01934 $mform->removeElement('name'); 01935 } 01936 // and set the data if we have some. 01937 foreach ($this->instance->get_instance_option_names() as $config) { 01938 if (!empty($this->instance->options[$config])) { 01939 $data[$config] = $this->instance->options[$config]; 01940 } else { 01941 $data[$config] = ''; 01942 } 01943 } 01944 } 01945 $this->set_data($data); 01946 } 01947 01948 if ($result === false) { 01949 $mform->addElement('cancel'); 01950 } else { 01951 $this->add_action_buttons(true, get_string('save','repository')); 01952 } 01953 } 01954 01955 public function validation($data) { 01956 global $DB; 01957 $errors = array(); 01958 $plugin = $this->_customdata['plugin']; 01959 $instance = (isset($this->_customdata['instance']) 01960 && is_subclass_of($this->_customdata['instance'], 'repository')) 01961 ? $this->_customdata['instance'] : null; 01962 if (!$instance) { 01963 $errors = repository::static_function($plugin, 'instance_form_validation', $this, $data, $errors); 01964 } else { 01965 $errors = $instance->instance_form_validation($this, $data, $errors); 01966 } 01967 01968 $sql = "SELECT count('x') 01969 FROM {repository_instances} i, {repository} r 01970 WHERE r.type=:plugin AND r.id=i.typeid AND i.name=:name"; 01971 if ($DB->count_records_sql($sql, array('name' => $data['name'], 'plugin' => $data['plugin'])) > 1) { 01972 $errors['name'] = get_string('erroruniquename', 'repository'); 01973 } 01974 01975 return $errors; 01976 } 01977 } 01978 01988 final class repository_type_form extends moodleform { 01989 protected $instance; 01990 protected $plugin; 01991 protected $action; 01992 01997 public function definition() { 01998 global $CFG; 01999 // type of plugin, string 02000 $this->plugin = $this->_customdata['plugin']; 02001 $this->instance = (isset($this->_customdata['instance']) 02002 && is_a($this->_customdata['instance'], 'repository_type')) 02003 ? $this->_customdata['instance'] : null; 02004 02005 $this->action = $this->_customdata['action']; 02006 $this->pluginname = $this->_customdata['pluginname']; 02007 $mform =& $this->_form; 02008 $strrequired = get_string('required'); 02009 02010 $mform->addElement('hidden', 'action', $this->action); 02011 $mform->setType('action', PARAM_TEXT); 02012 $mform->addElement('hidden', 'repos', $this->plugin); 02013 $mform->setType('repos', PARAM_PLUGIN); 02014 02015 // let the plugin add its specific fields 02016 $classname = 'repository_' . $this->plugin; 02017 require_once($CFG->dirroot . '/repository/' . $this->plugin . '/lib.php'); 02018 //add "enable course/user instances" checkboxes if multiple instances are allowed 02019 $instanceoptionnames = repository::static_function($this->plugin, 'get_instance_option_names'); 02020 02021 $result = call_user_func(array($classname, 'type_config_form'), $mform, $classname); 02022 02023 if (!empty($instanceoptionnames)) { 02024 $sm = get_string_manager(); 02025 $component = 'repository'; 02026 if ($sm->string_exists('enablecourseinstances', 'repository_' . $this->plugin)) { 02027 $component .= ('_' . $this->plugin); 02028 } 02029 $mform->addElement('checkbox', 'enablecourseinstances', get_string('enablecourseinstances', $component)); 02030 02031 $component = 'repository'; 02032 if ($sm->string_exists('enableuserinstances', 'repository_' . $this->plugin)) { 02033 $component .= ('_' . $this->plugin); 02034 } 02035 $mform->addElement('checkbox', 'enableuserinstances', get_string('enableuserinstances', $component)); 02036 } 02037 02038 // set the data if we have some. 02039 if ($this->instance) { 02040 $data = array(); 02041 $option_names = call_user_func(array($classname,'get_type_option_names')); 02042 if (!empty($instanceoptionnames)){ 02043 $option_names[] = 'enablecourseinstances'; 02044 $option_names[] = 'enableuserinstances'; 02045 } 02046 02047 $instanceoptions = $this->instance->get_options(); 02048 foreach ($option_names as $config) { 02049 if (!empty($instanceoptions[$config])) { 02050 $data[$config] = $instanceoptions[$config]; 02051 } else { 02052 $data[$config] = ''; 02053 } 02054 } 02055 // XXX: set plugin name for plugins which doesn't have muliti instances 02056 if (empty($instanceoptionnames)){ 02057 $data['pluginname'] = $this->pluginname; 02058 } 02059 $this->set_data($data); 02060 } 02061 02062 $this->add_action_buttons(true, get_string('save','repository')); 02063 } 02064 02065 public function validation($data) { 02066 $errors = array(); 02067 $plugin = $this->_customdata['plugin']; 02068 $instance = (isset($this->_customdata['instance']) 02069 && is_subclass_of($this->_customdata['instance'], 'repository')) 02070 ? $this->_customdata['instance'] : null; 02071 if (!$instance) { 02072 $errors = repository::static_function($plugin, 'type_form_validation', $this, $data, $errors); 02073 } else { 02074 $errors = $instance->type_form_validation($this, $data, $errors); 02075 } 02076 02077 return $errors; 02078 } 02079 } 02080 02094 function initialise_filepicker($args) { 02095 global $CFG, $USER, $PAGE, $OUTPUT; 02096 require_once($CFG->libdir . '/licenselib.php'); 02097 02098 $return = new stdClass(); 02099 $licenses = array(); 02100 if (!empty($CFG->licenses)) { 02101 $array = explode(',', $CFG->licenses); 02102 foreach ($array as $license) { 02103 $l = new stdClass(); 02104 $l->shortname = $license; 02105 $l->fullname = get_string($license, 'license'); 02106 $licenses[] = $l; 02107 } 02108 } 02109 if (!empty($CFG->sitedefaultlicense)) { 02110 $return->defaultlicense = $CFG->sitedefaultlicense; 02111 } 02112 02113 $return->licenses = $licenses; 02114 02115 $return->author = fullname($USER); 02116 02117 $ft = new filetype_parser(); 02118 if (empty($args->context)) { 02119 $context = $PAGE->context; 02120 } else { 02121 $context = $args->context; 02122 } 02123 $disable_types = array(); 02124 if (!empty($args->disable_types)) { 02125 $disable_types = $args->disable_types; 02126 } 02127 02128 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 02129 02130 list($context, $course, $cm) = get_context_info_array($context->id); 02131 $contexts = array($user_context, get_system_context()); 02132 if (!empty($course)) { 02133 // adding course context 02134 $contexts[] = get_context_instance(CONTEXT_COURSE, $course->id); 02135 } 02136 $externallink = (int)get_config(null, 'repositoryallowexternallinks'); 02137 $repositories = repository::get_instances(array( 02138 'context'=>$contexts, 02139 'currentcontext'=> $context, 02140 'accepted_types'=>$args->accepted_types, 02141 'return_types'=>$args->return_types, 02142 'disable_types'=>$disable_types 02143 )); 02144 02145 $return->repositories = array(); 02146 02147 if (empty($externallink)) { 02148 $return->externallink = false; 02149 } else { 02150 $return->externallink = true; 02151 } 02152 02153 // provided by form element 02154 $return->accepted_types = $ft->get_extensions($args->accepted_types); 02155 $return->return_types = $args->return_types; 02156 foreach ($repositories as $repository) { 02157 $meta = $repository->get_meta(); 02158 // Please note that the array keys for repositories are used within 02159 // JavaScript a lot, the key NEEDS to be the repository id. 02160 $return->repositories[$repository->id] = $meta; 02161 } 02162 return $return; 02163 } 02170 function repository_attach_id(&$value, $key, $id){ 02171 $value['repo_id'] = $id; 02172 }