示例#1
0
 /**
  * Sorts the result of {@link file_storage::get_area_tree()}.
  *
  * @param array $tree Array of results provided by {@link file_storage::get_area_tree()}
  * @return array of sorted results
  */
 protected function sort_area_tree($tree)
 {
     foreach ($tree as $key => &$value) {
         if ($key == 'subdirs') {
             core_collator::ksort($value, core_collator::SORT_NATURAL);
             foreach ($value as $subdirname => &$subtree) {
                 $subtree = $this->sort_area_tree($subtree);
             }
         } else {
             if ($key == 'files') {
                 core_collator::ksort($value, core_collator::SORT_NATURAL);
             }
         }
     }
     return $tree;
 }
示例#2
0
 /**
  * Query Google Drive for files and folders using a search query.
  *
  * Documentation about the query format can be found here:
  *   https://developers.google.com/drive/search-parameters
  *
  * This returns a list of files and folders with their details as they should be
  * formatted and returned by functions such as get_listing() or search().
  *
  * @param string $q search query as expected by the Google API.
  * @param string $path parent path of the current files, will not be used for the query.
  * @param int $page page.
  * @return array of files and folders.
  */
 protected function query($q, $path = null, $page = 0)
 {
     global $OUTPUT;
     $files = array();
     $folders = array();
     $fields = "items(id,title,mimeType,downloadUrl,fileExtension,exportLinks,modifiedDate,fileSize,thumbnailLink)";
     $params = array('q' => $q, 'fields' => $fields);
     try {
         // Retrieving files and folders.
         $response = $this->service->files->listFiles($params);
     } catch (Google_ServiceException $e) {
         if ($e->getCode() == 403 && strpos($e->getMessage(), 'Access Not Configured') !== false) {
             // This is raised when the service Drive API has not been enabled on Google APIs control panel.
             throw new repository_exception('servicenotenabled', 'repository_googledocs');
         } else {
             throw $e;
         }
     }
     $items = isset($response['items']) ? $response['items'] : array();
     foreach ($items as $item) {
         if ($item['mimeType'] == 'application/vnd.google-apps.folder') {
             // This is a folder.
             $folders[$item['title'] . $item['id']] = array('title' => $item['title'], 'path' => $this->build_node_path($item['id'], $item['title'], $path), 'date' => strtotime($item['modifiedDate']), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'children' => array());
         } else {
             // This is a file.
             if (isset($item['fileExtension'])) {
                 // The file has an extension, therefore there is a download link.
                 $title = $item['title'];
                 $source = $item['downloadUrl'];
             } else {
                 // The file is probably a Google Doc file, we get the corresponding export link.
                 // This should be improved by allowing the user to select the type of export they'd like.
                 $type = str_replace('application/vnd.google-apps.', '', $item['mimeType']);
                 $title = '';
                 $exportType = '';
                 switch ($type) {
                     case 'document':
                         $title = $item['title'] . '.rtf';
                         $exportType = 'application/rtf';
                         break;
                     case 'presentation':
                         $title = $item['title'] . '.pptx';
                         $exportType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                         break;
                     case 'spreadsheet':
                         $title = $item['title'] . '.xlsx';
                         $exportType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                         break;
                 }
                 // Skips invalid/unknown types.
                 if (empty($title) || !isset($item['exportLinks'][$exportType])) {
                     continue;
                 }
                 $source = $item['exportLinks'][$exportType];
             }
             // Adds the file to the file list. Using the itemId along with the title as key
             // of the array because Google Drive allows files with identical names.
             $files[$title . $item['id']] = array('title' => $title, 'source' => $source, 'date' => strtotime($item['modifiedDate']), 'size' => isset($item['fileSize']) ? $item['fileSize'] : null, 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64);
             // Sometimes the real thumbnails can't be displayed, for example if 3rd party cookies are disabled
             // or if the user is not logged in Google anymore. But this restriction does not seem to be applied
             // to a small subset of files.
             $extension = strtolower(pathinfo($title, PATHINFO_EXTENSION));
             if (isset($item['thumbnailLink']) && in_array($extension, array('jpg', 'png', 'txt', 'pdf'))) {
                 $files[$title . $item['id']]['realthumbnail'] = $item['thumbnailLink'];
             }
         }
     }
     // Filter and order the results.
     $files = array_filter($files, array($this, 'filter'));
     core_collator::ksort($files, core_collator::SORT_NATURAL);
     core_collator::ksort($folders, core_collator::SORT_NATURAL);
     return array_merge(array_values($folders), array_values($files));
 }
示例#3
0
 /**
  * Tests the static ksort method.
  */
 public function test_ksort()
 {
     $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
     $result = core_collator::ksort($arr);
     $this->assertSame(array(0, 1, 'b'), array_keys($arr));
     $this->assertSame(array('cc', 'aa', 'ab'), array_values($arr));
     $this->assertTrue($result);
     $obj = new stdClass();
     $arr = array('1.1.1' => array(), '1.2' => $obj, '1.20.2' => null);
     $result = core_collator::ksort($arr, core_collator::SORT_NATURAL);
     $this->assertSame(array('1.1.1', '1.2', '1.20.2'), array_keys($arr));
     $this->assertSame(array(array(), $obj, null), array_values($arr));
     $this->assertTrue($result);
     $a = array(2 => 'b', 1 => 'c');
     $c =& $a;
     $b =& $a;
     core_collator::ksort($b);
     $this->assertSame($a, $b);
     $this->assertSame($c, $b);
 }
示例#4
0
文件: lib.php 项目: evltuma/moodle
 /**
  * Get file listing
  *
  * @param string $path
  * @param string $page
  * @return mixed
  */
 public function get_listing($fullpath = '', $page = '')
 {
     global $OUTPUT;
     $ret = array();
     $ret['list'] = array();
     $ret['manage'] = self::MANAGE_URL;
     $ret['dynload'] = true;
     $crumbs = explode('/', $fullpath);
     $path = array_pop($crumbs);
     if (empty($path)) {
         $type = 'folder';
         $pathid = 0;
         $pathname = get_string('pluginname', 'repository_boxnet');
     } else {
         list($type, $pathid, $pathname) = $this->split_part($path);
     }
     $ret['path'] = $this->build_breadcrumb($fullpath);
     $folders = array();
     $files = array();
     if ($type == 'search') {
         $result = $this->boxnetclient->search($pathname);
     } else {
         $result = $this->boxnetclient->get_folder_items($pathid);
     }
     foreach ($result->entries as $item) {
         if ($item->type == 'folder') {
             $folders[$item->name . ':' . $item->id] = array('title' => $item->name, 'path' => $fullpath . '/' . $this->build_part('folder', $item->id, $item->name), 'date' => strtotime($item->modified_at), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'children' => array(), 'size' => $item->size);
         } else {
             $files[$item->name . ':' . $item->id] = array('title' => $item->name, 'source' => $this->build_part('file', $item->id, $item->name), 'size' => $item->size, 'date' => strtotime($item->modified_at), 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($item->name, 64))->out(false), 'thumbnail_height' => 64, 'thumbnail_width' => 64, 'author' => $item->owned_by->name);
         }
     }
     core_collator::ksort($folders, core_collator::SORT_NATURAL);
     core_collator::ksort($files, core_collator::SORT_NATURAL);
     $ret['list'] = array_merge($folders, $files);
     $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
     return $ret;
 }
示例#5
0
/**
 * Returns all the html files (chapters) from a file package
 *
 * @param stored_file $package file to be processed
 * @param string $type type of the package ('typezipdirs' or 'typezipfiles')
 *
 * @return array the html files found in the package
 */
function toolbook_importhtml_get_chapter_files($package, $type)
{
    $packer = get_file_packer('application/zip');
    $files = $package->list_files($packer);
    $tophtmlfiles = array();
    $subhtmlfiles = array();
    $topdirs = array();
    foreach ($files as $file) {
        if (empty($file->pathname)) {
            continue;
        }
        if (substr($file->pathname, -1) === '/') {
            if (substr_count($file->pathname, '/') !== 1) {
                // skip subdirs
                continue;
            }
            if (!isset($topdirs[$file->pathname])) {
                $topdirs[$file->pathname] = array();
            }
        } else {
            $mime = mimeinfo('icon', $file->pathname);
            if ($mime !== 'html') {
                continue;
            }
            $level = substr_count($file->pathname, '/');
            if ($level === 0) {
                $tophtmlfiles[$file->pathname] = $file;
            } else {
                if ($level === 1) {
                    $subhtmlfiles[$file->pathname] = $file;
                    $dir = preg_replace('|/.*$|', '', $file->pathname);
                    $topdirs[$dir][$file->pathname] = $file;
                } else {
                    // lower levels are not interesting
                    continue;
                }
            }
        }
    }
    core_collator::ksort($tophtmlfiles, core_collator::SORT_NATURAL);
    core_collator::ksort($subhtmlfiles, core_collator::SORT_NATURAL);
    core_collator::ksort($topdirs, core_collator::SORT_NATURAL);
    $chapterfiles = array();
    if ($type == 2) {
        $chapterfiles = $tophtmlfiles;
    } else {
        if ($type == 1) {
            foreach ($topdirs as $dir => $htmlfiles) {
                if (empty($htmlfiles)) {
                    continue;
                }
                core_collator::ksort($htmlfiles, core_collator::SORT_NATURAL);
                if (isset($htmlfiles[$dir . '/index.html'])) {
                    $htmlfile = $htmlfiles[$dir . '/index.html'];
                } else {
                    if (isset($htmlfiles[$dir . '/index.htm'])) {
                        $htmlfile = $htmlfiles[$dir . '/index.htm'];
                    } else {
                        if (isset($htmlfiles[$dir . '/Default.htm'])) {
                            $htmlfile = $htmlfiles[$dir . '/Default.htm'];
                        } else {
                            $htmlfile = reset($htmlfiles);
                        }
                    }
                }
                $chapterfiles[$htmlfile->pathname] = $htmlfile;
            }
        } else {
            if ($type == 0) {
                if ($tophtmlfiles) {
                    if (isset($tophtmlfiles['index.html'])) {
                        $htmlfile = $tophtmlfiles['index.html'];
                    } else {
                        if (isset($tophtmlfiles['index.htm'])) {
                            $htmlfile = $tophtmlfiles['index.htm'];
                        } else {
                            if (isset($tophtmlfiles['Default.htm'])) {
                                $htmlfile = $tophtmlfiles['Default.htm'];
                            } else {
                                $htmlfile = reset($tophtmlfiles);
                            }
                        }
                    }
                } else {
                    $htmlfile = reset($subhtmlfiles);
                }
                $chapterfiles[$htmlfile->pathname] = $htmlfile;
            }
        }
    }
    return $chapterfiles;
}
示例#6
0
$reports = core_component::get_plugin_list_with_file('quiz', 'settings.php', false);
$reportsbyname = array();
foreach ($reports as $report => $reportdir) {
    $strreportname = get_string($report . 'report', 'quiz_' . $report);
    $reportsbyname[$strreportname] = $report;
}
core_collator::ksort($reportsbyname);
// First get a list of quiz reports with there own settings pages. If there none,
// we use a simpler overall menu structure.
$rules = core_component::get_plugin_list_with_file('quizaccess', 'settings.php', false);
$rulesbyname = array();
foreach ($rules as $rule => $ruledir) {
    $strrulename = get_string('pluginname', 'quizaccess_' . $rule);
    $rulesbyname[$strrulename] = $rule;
}
core_collator::ksort($rulesbyname);
// Create the quiz settings page.
if (empty($reportsbyname) && empty($rulesbyname)) {
    $pagetitle = get_string('modulename', 'quiz');
} else {
    $pagetitle = get_string('generalsettings', 'admin');
}
$quizsettings = new admin_settingpage('modsettingquiz', $pagetitle, 'moodle/site:config');
if ($ADMIN->fulltree) {
    // Introductory explanation that all the settings are defaults for the add quiz form.
    $quizsettings->add(new admin_setting_heading('quizintro', '', get_string('configintro', 'quiz')));
    // Time limit.
    $quizsettings->add(new admin_setting_configtext_with_advanced('quiz/timelimit', get_string('timelimitsec', 'quiz'), get_string('configtimelimitsec', 'quiz'), array('value' => '0', 'adv' => false), PARAM_INT));
    // What to do with overdue attempts.
    $quizsettings->add(new mod_quiz_admin_setting_overduehandling('quiz/overduehandling', get_string('overduehandling', 'quiz'), get_string('overduehandling_desc', 'quiz'), array('value' => 'autoabandon', 'adv' => false), null));
    // Grace period time.
示例#7
0
 function sortguielems()
 {
     // sort top gui elements
     if (is_array($this->guielems_top)) {
         core_collator::ksort($this->guielems_top, core_collator::SORT_NATURAL);
     }
     // sort middle gui elements
     if (is_array($this->guielems_middle)) {
         $final = array();
         foreach (array_keys($this->guielems_middle) as $category) {
             foreach (array_keys($this->guielems_middle[$category]) as $section) {
                 core_collator::ksort($this->guielems_middle[$category][$section], core_collator::SORT_NATURAL);
                 for ($placement = 0; $placement < 10; $placement++) {
                     if ($this->guielems_middle[$category][$section]['placement'] == $placement) {
                         $final[$category][$placement][$section] = $this->guielems_middle[$category][$section];
                     }
                 }
             }
             ksort($final[$category]);
         }
         $this->guielems_middle = $final;
         core_collator::ksort($this->guielems_middle, core_collator::SORT_NATURAL);
         uksort($this->guielems_middle, function ($a, $b) {
             $a = strtolower($a);
             $b = strtolower($b);
             $categories = array("general" => 1, "voicemail" => 2, "findmefollow" => 3, "advanced" => 4, "other" => 999);
             $aOrder = isset($categories[$a]) ? $categories[$a] : 5;
             $bOrder = isset($categories[$b]) ? $categories[$b] : 5;
             return $aOrder < $bOrder ? -1 : 1;
         });
     }
     // sort bottom gui elements
     if (is_array($this->guielems_bottom)) {
         core_collator::ksort($this->guielems_bottom, core_collator::SORT_NATURAL);
     }
     $this->sorted_guielems = true;
 }
 /**
  * Locale-aware version of PHP's ksort function.
  * @param array $array The array to sort. Sorted in place.
  */
 public static function sort_array_by_key(&$array)
 {
     if (class_exists('core_collator')) {
         core_collator::ksort($array);
     } else {
         collatorlib::ksort($array);
     }
 }