Implemented a test version of the gradebook category edit page, and developed the get_edit_tree method in grade_tree. Items can be successfully moved up and down, and a "move" button lets the user select a source element, then a destination box. All actions use POST.

This commit is contained in:
nicolasconnault 2007-05-30 08:48:11 +00:00
parent 81fb221d31
commit 5bfa0d793a
2 changed files with 144 additions and 22 deletions

View File

@ -0,0 +1,72 @@
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-2007 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
set_time_limit(0);
require_once '../../../config.php';
require_once $CFG->libdir . '/grade/grade_tree.php';
require_once $CFG->libdir . '/gradelib.php';
print_header('Edit categories');
$param = new stdClass();
$param->moveup = optional_param('moveup', 0, PARAM_INT);
$param->movedown = optional_param('movedown', 0, PARAM_INT);
$param->source = optional_param('source', 0, PARAM_INT);
$param->action = optional_param('action', 0, PARAM_ALPHA);
$param->move = optional_param('move', 0, PARAM_INT);
$tree = new grade_tree(641);
$select_source = false;
if (!empty($param->action)) {
if (empty($param->source)) {
$select_source = true;
} else {
print_heading("Select the destination for the selected element.");
}
} elseif (!empty($param->source)) {
if (!empty($param->moveup)) {
$tree->move_element($param->source, $param->moveup);
} elseif(!empty($param->movedown)) {
$tree->move_element($param->source, $param->movedown, 'after');
} elseif(!empty($param->move)) {
$tree->move_element($param->source, $param->move, 'after');
}
$tree->renumber();
$tree->update_db();
}
if ($select_source) {
print_heading("Select an element to move");
}
echo $tree->get_edit_tree($select_source, 1, null, $param->source, $param->action);
echo '<form id="move_button" action="category.php" method="post"><div>' . "\n";
echo '<input type="hidden" name="action" value="move" />' . "\n";
echo '<input type="submit" value="Move" />' . "\n";
echo '</div></form>';
print_footer();
?>

View File

@ -404,7 +404,7 @@ class grade_tree {
$this->first_sortorder = $sortorder; $this->first_sortorder = $sortorder;
foreach ($elements as $key => $element) { foreach ($elements as $key => $element) {
$this->first_sortorder++; $this->first_sortorder++;
$new_sortorder = $this->first_sortorder; $new_sortorder = $this->first_sortorder;
$old_sortorder = $element['object']->sortorder; $old_sortorder = $element['object']->sortorder;
@ -439,7 +439,7 @@ class grade_tree {
return $newtree; return $newtree;
} }
} }
/** /**
* Because the $element referred to in this class is rather loosely defined, it * Because the $element referred to in this class is rather loosely defined, it
* may come in different flavours and forms. However, it will almost always contain * may come in different flavours and forms. However, it will almost always contain
@ -912,9 +912,14 @@ class grade_tree {
/** /**
* Returns a HTML list with sorting arrows and insert boxes. This is a recursive method. * Returns a HTML list with sorting arrows and insert boxes. This is a recursive method.
* @param boolean $select_source Whether or not to display each element as a link to be selected as a source for an action
* @param int $level The level of recursion
* @param array $elements The elements to display in a list. Defaults to this->tree_array
* @param int $source_sortorder A source sortorder, given when an element needs to be moved or inserted.
* @param string $action 'move' or 'insert'
* @return string HTML code * @return string HTML code
*/ */
function get_edit_tree($level=1, $elements=null) { function get_edit_tree($select_source=false, $level=1, $elements=NULL, $source_sortorder=NULL, $action=NULL) {
if (empty($this->tree_array)) { if (empty($this->tree_array)) {
return null; return null;
} else { } else {
@ -923,17 +928,21 @@ class grade_tree {
$strmoveup = get_string("moveup"); $strmoveup = get_string("moveup");
$strmovedown = get_string("movedown"); $strmovedown = get_string("movedown");
$sesskey_input = '<input type="hidden" name="sesskey" value="' . $USER->sesskey . '" />';
$courseid_input = '<input type="hidden" name="courseid" value="' . $this->courseid . '" />';
if (empty($elements)) { if (empty($elements)) {
$list = '<ul id="grade_edit_tree">'; $list = '<ul id="grade_edit_tree">' . "\n";
$elements = $this->tree_array; $elements = $this->tree_array;
} else { } else {
$list = '<ul class="level' . $level . 'children">'; $list = '<ul class="level' . $level . 'children">' . "\n";
} }
$first = true; $first = true;
$count = 1; $count = 1;
$last = false; $last = false;
$last_sortorder = null;
if (count($elements) == 1) { if (count($elements) == 1) {
$last = true; $last = true;
@ -943,43 +952,84 @@ class grade_tree {
if (empty($element->next_sortorder)) { if (empty($element->next_sortorder)) {
$element->next_sortorder = null; $element->next_sortorder = null;
} }
$object_name = $element['object']->get_name();
if ($select_source) {
$formname = "select_source_$sortorder";
$object_name = '<form id="' . $formname . '" class="movearrow" action="category.php" method="post"><div>
<input type="hidden" name="source" value="' . $sortorder . '" />
<input type="hidden" name="action" value="' . $action . '" />
<a href="#" onclick="document.getElementById(\'' . $formname . '\').submit();" title="Select this element">'
. $object_name . '</a></div></form>';
}
$list .= '<li class="level' . $level . 'element sortorder' $list .= '<li class="level' . $level . 'element sortorder'
. $element['object']->get_sortorder() . '">' . $element['object']->get_sortorder() . '">' . "\n"
. $element['object']->get_name(); . $object_name;
if (!$first) { if (!$first) {
$list .= '<a title="'.$strmoveup.'" href="category.php?courseid='.$this->courseid $formname = "moveup_$sortorder";
. '&amp;source=' . $sortorder . '&amp;moveup=' . $element['object']->previous_sortorder $list .= '<form method="post" action="category.php" class="movearrow" id="' . $formname . '"><div>
. '&amp;sesskey='.$USER->sesskey.'">' <input type="hidden" name="source" value="' . $sortorder . '" />
. '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> '; <input type="hidden" name="moveup" value="' . $element['object']->previous_sortorder . '" />' . "\n";
$list .= $sesskey_input . "\n" . $courseid_input;
$list .= '<div class="moveuparrow">'
. '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" '
. 'alt="'.$strmoveup.'" title="'.$strmoveup.'"'
. 'onclick="document.getElementById(\'' . $formname . '\').submit();" /> </div>'. "\n";
$list .= '</div></form>';
} else { } else {
$list .= '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> '; $list .= '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> '. "\n";
} }
if (!$last) { if (!$last) {
$list .= '<a title="'.$strmovedown.'" href="category.php?courseid='.$this->courseid $formname = "movedown_$sortorder";
. '&amp;source=' . $sortorder . '&amp;movedown=' . $element['object']->next_sortorder $list .= '<form method="post" action="category.php" class="movearrow" id="' . $formname . '"><div>
. '&amp;sesskey='.$USER->sesskey.'">' <input type="hidden" name="source" value="' . $sortorder . '" />
. '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> '; <input type="hidden" name="movedown" value="' . $element['object']->next_sortorder . '" />' . "\n";
$list .= $sesskey_input . "\n" . $courseid_input;
$list .= '<div class="movedownarrow">'
. '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" '
. 'alt="'.$strmovedown.'" title="'.$strmovedown.'"'
. 'onclick="document.getElementById(\'' . $formname . '\').submit();" /></div>'. "\n";
$list .= '</div></form>';
} else { } else {
$list .= '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> '; $list .= '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ' . "\n";
} }
if (!empty($element['children'])) { if (!empty($element['children'])) {
$list .= $this->get_edit_tree($level + 1, $element['children']); $list .= $this->get_edit_tree($select_source, $level + 1, $element['children'], $source_sortorder, $action);
} }
$list .= '</li>'; $list .= '</li>' . "\n";
$first = false; $first = false;
$count++; $count++;
if ($count == count($elements)) { if ($count == count($elements)) {
$last = true; $last = true;
} }
}
$list .= '</ul>';
$last_sortorder = $sortorder;
}
// Add an insertion box if source_sortorder is given
if ($source_sortorder && !empty($action)) {
$list .= '<li class="insertion">
<form method="post" action="category.php" id="insertion_' . $last_sortorder . '">
<div>
<input type="hidden" name="source" value="' . $source_sortorder . '" />
<input type="hidden" name="' . $action . '" value="' . $last_sortorder . '" />
<div class="insertion_box" onclick="document.getElementById(\'insertion_' . $last_sortorder . '\').submit();"></div>
</div>
</form>
</li>';
}
$list .= '</ul>' . "\n";
return $list; return $list;
} }