Commit 90b43e3e authored by Son Nguyen's avatar Son Nguyen Committed by Gerrit Code Review

Merge "Asc/desc sort order option added to folder blocktype (bug 1233896)"

parents 11b56110 bae6a91a
......@@ -64,9 +64,16 @@ class PluginBlocktypeFolder extends PluginBlocktype {
$configdata = $instance->get('configdata');
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
$elements = array(
'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
'sortorder' => array(
'type' => 'select',
'labelhtml' => get_string('sortorder'),
'defaultvalue' => (isset($configdata['sortorder']) && $configdata['sortorder'] == 'desc') ? 'desc' : 'asc',
'options' => array('asc' => get_string('ascending'), 'desc' => get_string('descending')),
),
);
return $elements;
}
public static function artefactchooser_element($default=null) {
......
......@@ -32,6 +32,21 @@ else {
}
if ($viewid && $fileid) {
$file = artefact_instance_from_id($fileid);
$parent = $file->get('parent');
$artefactok = false;
if (artefact_in_view($file->get('id'), $viewid)) {
$artefactok = true;
}
// Check to see if the artefact has a parent that is allowed to be in this view
// for example subdirectory of a folder artefact on a view
while ($parent !== null) {
$parentobj = artefact_instance_from_id($parent);
$parent = $parentobj->get('parent');
if (artefact_in_view($parentobj->get('id'), $viewid)) {
$artefactok = true;
}
}
// The user may be trying to download a file that's not in the view, but which has
// been attached to public feedback on the view
......@@ -45,7 +60,7 @@ if ($viewid && $fileid) {
throw new AccessDeniedException('');
}
}
else if (!artefact_in_view($fileid, $viewid)) {
else if ($artefactok == false) {
throw new AccessDeniedException('');
}
......@@ -53,7 +68,6 @@ if ($viewid && $fileid) {
throw new AccessDeniedException('');
}
$file = artefact_instance_from_id($fileid);
if (!($file instanceof ArtefactTypeFile)) {
throw new NotFoundException();
}
......
......@@ -342,6 +342,11 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
(-2 * isset($b->isparent) + ($b->artefacttype != 'folder')) . 'a' . $b->title);
}
// Sort folders before files in descending order; then use nat sort order descending.
public static function my_files_cmp_desc($a, $b) {
return strnatcasecmp((+2 * isset($b->isparent) + ($b->artefacttype == 'folder')) . 'a' . $b->title,
(+2 * isset($a->isparent) + ($a->artefacttype == 'folder')) . 'a' . $a->title);
}
/**
* Gets a list of files in one folder
......@@ -1670,7 +1675,8 @@ class ArtefactTypeFolder extends ArtefactTypeFileBase {
if ($childrecords = $this->folder_contents()) {
$this->add_to_render_path($options);
usort($childrecords, array('ArtefactTypeFileBase', 'my_files_cmp'));
$sortorder = (isset($options['sortorder']) && $options['sortorder'] == 'desc') ? 'my_files_cmp_desc' : 'my_files_cmp';
usort($childrecords, array('ArtefactTypeFileBase', $sortorder));
$children = array();
foreach ($childrecords as &$child) {
$c = artefact_instance_from_id($child->id);
......
......@@ -1507,7 +1507,7 @@ function artefact_instance_from_id($id) {
*
* @return str the block instance title
*/
function artefact_title_for_view_and_block($artefactid, $viewid, $blockid) {
function artefact_title_for_view_and_block($artefact, $viewid, $blockid) {
$sql = "SELECT bi.title AS blocktitle,
a.title AS artefacttitle
FROM {artefact} a
......@@ -1515,8 +1515,10 @@ function artefact_title_for_view_and_block($artefactid, $viewid, $blockid) {
JOIN {block_instance} bi ON bi.id = va.block
WHERE va.artefact = ?
AND va.view = ? AND va.block = ?";
if (!$data = get_record_sql($sql, array($artefactid, $viewid, $blockid))) {
throw new ArtefactNotFoundException(get_string('artefactnotfound', 'mahara', $artefactid));
if (!$data = get_record_sql($sql, array($artefact->get('id'), $viewid, $blockid))) {
// if we are traversing folders where the subfolders/files are not directly connected
// to the blockinstance we just return their title
return $artefact->display_title();
}
$currenttitle = (!empty($data->blocktitle)) ? $data->blocktitle : $data->artefacttitle;
return $currenttitle;
......
......@@ -45,6 +45,9 @@ $string['at'] = 'at';
$string['From'] = 'From';
$string['To'] = 'To';
$string['All'] = 'All';
$string['ascending'] = 'Ascending';
$string['descending'] = 'Descending';
$string['sortorder'] = 'Sort order of files';
$string['enable'] = 'Enable';
$string['disable'] = 'Disable';
......
......@@ -29,7 +29,34 @@ if (!can_view_view($view)) {
throw new AccessDeniedException();
}
if (!artefact_in_view($artefactid, $viewid)) {
require_once(get_config('docroot') . 'artefact/lib.php');
$artefact = artefact_instance_from_id($artefactid);
// Build the path to the artefact, through its parents
$artefactpath = array();
$parent = $artefact->get('parent');
$artefactok = false;
if (artefact_in_view($artefact->get('id'), $viewid)) {
$artefactok = true;
$baseobject = $artefact;
}
while ($parent !== null) {
// This loop could get expensive when there are a lot of parents. But at least
// it works, unlike the old attempt
$parentobj = artefact_instance_from_id($parent);
if (artefact_in_view($parent, $viewid)) {
array_unshift($artefactpath, array(
'url' => get_config('wwwroot') . 'view/artefact.php?artefact=' . $parent . '&view=' . $viewid,
'title' => $parentobj->display_title(),
));
}
$parent = $parentobj->get('parent');
if (artefact_in_view($parentobj->get('id'), $viewid)) {
$artefactok = true;
$baseobject = $parentobj;
}
}
if ($artefactok == false) {
throw new AccessDeniedException(get_string('artefactnotinview', 'error', $artefactid, $viewid));
}
......@@ -38,11 +65,10 @@ $limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$showcomment = param_integer('showcomment', null);
require_once(get_config('docroot') . 'artefact/lib.php');
$artefact = artefact_instance_from_id($artefactid);
if ($artefactid && $viewid && $blockid) {
if ($artefact && $viewid && $blockid) {
// use the block instance title rather than the artefact title if it exists
$title = artefact_title_for_view_and_block($artefactid, $viewid, $blockid);
$title = artefact_title_for_view_and_block($artefact, $viewid, $blockid);
}
else {
$title = $artefact->display_title();
......@@ -70,6 +96,18 @@ $options = array(
if (param_integer('details', 0)) {
$options['metadata'] = 1;
}
if ($artefact->get('artefacttype') == 'folder') {
// get folder block sort order - returns the first instance of folder on view
// why you'd want more than one folder block on the same view is m̶a̶d̶n̶e̶s̶s̶ user preference.
if ($block = get_records_sql_array('SELECT block FROM {view_artefact} WHERE view = ? AND artefact = ?', array($viewid, $baseobject->get('id')),0,1)) {
require_once(get_config('docroot') . 'blocktype/lib.php');
$bi = new BlockInstance($block[0]->block);
$configdata = $bi->get('configdata');
if (!empty($configdata['sortorder'])) {
$options['sortorder'] = $configdata['sortorder'];
}
}
}
$rendered = $artefact->render_self($options);
$content = '';
if (!empty($rendered['javascript'])) {
......@@ -77,29 +115,11 @@ if (!empty($rendered['javascript'])) {
}
$content .= $rendered['html'];
// Build the path to the artefact, through its parents
$artefactpath = array();
$parent = $artefact->get('parent');
while ($parent !== null) {
// This loop could get expensive when there are a lot of parents. But at least
// it works, unlike the old attempt
$parentobj = artefact_instance_from_id($parent);
if (artefact_in_view($parent, $viewid)) {
array_unshift($artefactpath, array(
'url' => get_config('wwwroot') . 'view/artefact.php?artefact=' . $parent . '&view=' . $viewid,
'title' => $parentobj->display_title(),
));
}
$parent = $parentobj->get('parent');
}
$artefactpath[] = array(
'url' => '',
'title' => $title,
);
// Feedback
$feedback = ArtefactTypeComment::get_comments($limit, $offset, $showcomment, $view, $artefact);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment