Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/filepicker.php
Go to the documentation of this file.
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 
00029 require_once('../config.php');
00030 require_once($CFG->libdir.'/filelib.php');
00031 require_once('lib.php');
00033 set_time_limit(0);
00034 
00035 require_sesskey();
00036 require_login();
00037 
00038 // disable blocks in this page
00039 $PAGE->set_pagelayout('embedded');
00040 
00041 // general parameters
00042 $action      = optional_param('action', '',        PARAM_ALPHA);
00043 $client_id   = optional_param('client_id', '', PARAM_RAW);    // client ID
00044 $itemid      = optional_param('itemid', '',        PARAM_INT);
00045 
00046 // parameters for repository
00047 $callback    = optional_param('callback', '',      PARAM_CLEANHTML);
00048 $contextid   = optional_param('ctx_id',    SYSCONTEXTID, PARAM_INT);    // context ID
00049 $courseid    = optional_param('course',    SITEID, PARAM_INT);    // course ID
00050 $env         = optional_param('env', 'filepicker', PARAM_ALPHA);  // opened in file picker, file manager or html editor
00051 $filename    = optional_param('filename', '',      PARAM_FILE);
00052 $fileurl     = optional_param('fileurl', '',       PARAM_RAW);
00053 $thumbnail   = optional_param('thumbnail', '',     PARAM_RAW);
00054 $targetpath  = optional_param('targetpath', '',    PARAM_PATH);
00055 $repo_id     = optional_param('repo_id', 0,        PARAM_INT);    // repository ID
00056 $req_path    = optional_param('p', '',             PARAM_RAW);    // the path in repository
00057 $curr_page   = optional_param('page', '',          PARAM_RAW);    // What page in repository?
00058 $search_text = optional_param('s', '',             PARAM_CLEANHTML);
00059 $maxfiles    = optional_param('maxfiles', -1,      PARAM_INT);    // maxfiles
00060 $maxbytes    = optional_param('maxbytes',  0,      PARAM_INT);    // maxbytes
00061 $subdirs     = optional_param('subdirs',  0,       PARAM_INT);    // maxbytes
00062 
00063 // the path to save files
00064 $savepath = optional_param('savepath', '/',    PARAM_PATH);
00065 // path in draft area
00066 $draftpath = optional_param('draftpath', '/',    PARAM_PATH);
00067 
00068 
00069 // user context
00070 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
00071 
00072 $PAGE->set_context($user_context);
00073 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
00074     print_error('invalidcourseid');
00075 }
00076 $PAGE->set_course($course);
00077 
00078 // init repository plugin
00079 $sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
00080        'WHERE i.id=? AND i.typeid=r.id';
00081 if ($repository = $DB->get_record_sql($sql, array($repo_id))) {
00082     $type = $repository->type;
00083     if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
00084         require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
00085         $classname = 'repository_' . $type;
00086         try {
00087             $repo = new $classname($repo_id, $contextid, array('ajax'=>false, 'name'=>$repository->name, 'type'=>$type));
00088         } catch (repository_exception $e){
00089             print_error('pluginerror', 'repository');
00090         }
00091     } else {
00092         print_error('invalidplugin', 'repository');
00093     }
00094 }
00095 
00096 $moodle_maxbytes = get_max_upload_file_size();
00097 // to prevent maxbytes greater than moodle maxbytes setting
00098 if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
00099     $maxbytes = $moodle_maxbytes;
00100 }
00101 
00102 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
00103 $params['action'] = 'browse';
00104 $params['draftpath'] = $draftpath;
00105 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
00106 
00107 $params['savepath'] = $savepath;
00108 $params['repo_id'] = $repo_id;
00109 $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
00110 $PAGE->set_url('/repository/filepicker.php', $params);
00111 
00112 switch ($action) {
00113 case 'upload':
00114     // The uploaded file has been processed in plugin construct function
00115     // redirect to default page
00116     try {
00117         $repo->upload('', $maxbytes);
00118         redirect($home_url, get_string('uploadsucc','repository'));
00119     } catch (moodle_exception $e) {
00120         // inject target URL
00121         $e->link = $PAGE->url->out();
00122         echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
00123         throw $e;
00124     }
00125     break;
00126 
00127 case 'search':
00128     echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
00129     try {
00130         $search_result = $repo->search($search_text);
00131         $search_result['issearchresult'] = true;
00132         $search_result['repo_id'] = $repo_id;
00133 
00134         // TODO: need a better solution
00135         $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
00136         $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
00137         echo $OUTPUT->render($pagingbar);
00138 
00139         echo '<table>';
00140         foreach ($search_result['list'] as $item) {
00141             echo '<tr>';
00142             echo '<td><img src="'.$item['thumbnail'].'" />';
00143             echo '</td><td>';
00144             if (!empty($item['url'])) {
00145                 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
00146             } else {
00147                 echo $item['title'];
00148             }
00149             echo '</td>';
00150             echo '<td>';
00151             echo '<form method="post">';
00152             echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
00153             echo '<input type="hidden" name="action" value="confirm"/>';
00154             echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
00155             echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
00156             echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
00157             echo '</form>';
00158             echo '</td>';
00159             echo '</tr>';
00160         }
00161         echo '</table>';
00162     } catch (repository_exception $e) {
00163     }
00164     break;
00165 
00166 case 'list':
00167 case 'sign':
00168     echo $OUTPUT->header();
00169 
00170     echo $OUTPUT->container_start();
00171     echo html_writer::link($url, get_string('back', 'repository'));
00172     echo $OUTPUT->container_end();
00173 
00174     if ($repo->check_login()) {
00175         $list = $repo->get_listing($req_path, $curr_page);
00176         $dynload = !empty($list['dynload'])?true:false;
00177         if (!empty($list['upload'])) {
00178             echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
00179             echo '<label>'.$list['upload']['label'].': </label>';
00180             echo '<input type="file" name="repo_upload_file" /><br />';
00181             echo '<input type="hidden" name="action" value="upload" /><br />';
00182             echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
00183             echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
00184             echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
00185             echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
00186             echo '</form>';
00187         } else {
00188             if (!empty($list['path'])) {
00189                 foreach ($list['path'] as $p) {
00190                     //echo '<form method="post" style="display:inline">';
00191                     //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
00192                     //echo '<input type="hidden" name="action" value="list"';
00193                     //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
00194                     //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
00195                     //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
00196                     //echo '</form>';
00197 
00198                     $pathurl = new moodle_url($url, array(
00199                         'p'=>$p['path'],
00200                         'action'=>'list',
00201                         'draftpath'=>$draftpath,
00202                         'savepath'=>$savepath
00203                         ));
00204                     echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>';
00205                     echo '<span> / </span>';
00206                 }
00207             }
00208             if (!empty($list['page'])) {
00209                 // TODO: need a better solution
00210                 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid");
00211                 echo $OUTPUT->paging_bar($list['total'], $list['page'] - 1, $list['perpage'], $pagingurl);
00212             }
00213             echo '<table>';
00214             foreach ($list['list'] as $item) {
00215                 echo '<tr>';
00216                 echo '<td><img src="'.$item['thumbnail'].'" />';
00217                 echo '</td><td>';
00218                 if (!empty($item['url'])) {
00219                     echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
00220                 } else {
00221                     echo $item['title'];
00222                 }
00223                 echo '</td>';
00224                 echo '<td>';
00225                 if (!isset($item['children'])) {
00226                     echo '<form method="post">';
00227                     echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
00228                     echo '<input type="hidden" name="action" value="confirm"/>';
00229                     echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
00230                     echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
00231                     echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
00232                     echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
00233                     echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
00234                     echo '</form>';
00235                 } else {
00236                     echo '<form method="post">';
00237                     echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
00238                     echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
00239                     echo '</form>';
00240                 }
00241                 echo '</td>';
00242                 echo '</tr>';
00243             }
00244             echo '</table>';
00245         }
00246     } else {
00247         echo '<form method="post">';
00248         echo '<input type="hidden" name="action" value="sign" />';
00249         echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
00250         $repo->print_login();
00251         echo '</form>';
00252     }
00253     echo $OUTPUT->footer();
00254     break;
00255 
00256 case 'download':
00257     $thefile = $repo->get_file($fileurl, $filename);
00258     $filesize = filesize($thefile['path']);
00259     if (($maxbytes!=-1) && ($filesize>$maxbytes)) {
00260         print_error('maxbytes');
00261     }
00262     if (!empty($thefile)) {
00263         $record = new stdClass();
00264         $record->filepath = $savepath;
00265         $record->filename = $filename;
00266         $record->component = 'user';
00267         $record->filearea = 'draft';
00268         $record->itemid   = $itemid;
00269         $record->license  = '';
00270         $record->author   = '';
00271         $record->source   = $thefile['url'];
00272         try {
00273             $info = repository::move_to_filepool($thefile['path'], $record);
00274             redirect($home_url, get_string('downloadsucc', 'repository'));
00275         } catch (moodle_exception $e) {
00276             // inject target URL
00277             $e->link = $PAGE->url->out();
00278             echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
00279             throw $e;
00280         }
00281     } else {
00282         print_error('cannotdownload', 'repository');
00283     }
00284 
00285     break;
00286 
00287 case 'confirm':
00288     echo $OUTPUT->header();
00289     echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
00290     echo '<img src="'.$thumbnail.'" />';
00291     echo '<form method="post">';
00292     echo '<table>';
00293     echo '  <tr>';
00294     echo '    <td><label>'.get_string('filename', 'repository').'</label></td>';
00295     echo '    <td><input type="text" name="filename" value="'.s($filename).'" /></td>';
00296     echo '    <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
00297     echo '    <td><input type="hidden" name="action" value="download" /></td>';
00298     echo '    <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
00299     echo '  </tr>';
00300     echo '</table>';
00301     echo '<div>';
00302     // the save path
00303     echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
00304     echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
00305     echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
00306     echo '</div>';
00307     echo '</form>';
00308     echo $OUTPUT->footer();
00309     break;
00310 
00311 default:
00312 case 'plugins':
00313     $params = array();
00314     $params['context'] = array($user_context, get_system_context());
00315     $params['currentcontext'] = $PAGE->context;
00316     $params['return_types'] = FILE_INTERNAL;
00317 
00318     $repos = repository::get_instances($params);
00319     echo $OUTPUT->header();
00320     echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
00321     echo '<div>';
00322     echo '<ul>';
00323     foreach($repos as $repo) {
00324         $info = $repo->get_meta();
00325 
00326         $aurl = clone($url);
00327         $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
00328 
00329         echo '<li>';
00330         echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> ';
00331         echo html_writer::link($aurl, $info->name);
00332         echo '</li>';
00333     }
00334     echo '</ul>';
00335     echo '</div>';
00336     echo $OUTPUT->footer();
00337     break;
00338 }
 All Data Structures Namespaces Files Functions Variables Enumerations