/** * Tests the static ksort method * @return void */ public function test_ksort() { $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc'); $result = collatorlib::ksort($arr); $this->assertSame(array_keys($arr), array(0, 1, 'b')); $this->assertSame(array_values($arr), array('cc', 'aa', 'ab')); $this->assertTrue($result); $obj = new stdClass(); $arr = array('1.1.1' => array(), '1.2' => $obj, '1.20.2' => null); $result = collatorlib::ksort($arr, collatorlib::SORT_NATURAL); $this->assertSame(array_keys($arr), array('1.1.1', '1.2', '1.20.2')); $this->assertSame(array_values($arr), array(array(), $obj, null)); $this->assertTrue($result); $a = array(2 => 'b', 1 => 'c'); $c =& $a; $b =& $a; collatorlib::ksort($b); $this->assertSame($a, $b); $this->assertSame($c, $b); }
$reportsbyname = array(); foreach ($reports as $report => $reportdir) { $strreportname = get_string($report . 'report', 'quiz_'.$report); $reportsbyname[$strreportname] = $report; } collatorlib::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 = 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; } collatorlib::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'); // 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'),
/** * 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); } } collatorlib::ksort($folders, core_collator::SORT_NATURAL); collatorlib::ksort($files, core_collator::SORT_NATURAL); $ret['list'] = array_merge($folders, $files); $ret['list'] = array_filter($ret['list'], array($this, 'filter')); return $ret; }
/** * 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; } } } collatorlib::ksort($tophtmlfiles, collatorlib::SORT_NATURAL); collatorlib::ksort($subhtmlfiles, collatorlib::SORT_NATURAL); collatorlib::ksort($topdirs, collatorlib::SORT_NATURAL); $chapterfiles = array(); if ($type == 2) { $chapterfiles = $tophtmlfiles; } else if ($type == 1) { foreach ($topdirs as $dir => $htmlfiles) { if (empty($htmlfiles)) { continue; } collatorlib::ksort($htmlfiles, collatorlib::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; }
/** * 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') { collatorlib::ksort($value, collatorlib::SORT_NATURAL); foreach ($value as $subdirname => &$subtree) { $subtree = $this->sort_area_tree($subtree); } } else { if ($key == 'files') { collatorlib::ksort($value, collatorlib::SORT_NATURAL); } } } return $tree; }
/** * 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, // Do not use real thumbnails as they wouldn't work if the user disabled 3rd party // plugins in his browser, or if they're not logged in their Google account. ); // 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')); collatorlib::ksort($files, collatorlib::SORT_NATURAL); collatorlib::ksort($folders, collatorlib::SORT_NATURAL); return array_merge(array_values($folders), array_values($files)); }
/** * 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); } }