-<?PHP // $Id$
+<?PHP // $Id$
// error.php - created with Moodle 1.7 beta + (2006101003)
$string['invalidrequest'] = 'Invalid request';
$string['invalidrole'] = 'Invalid role';
$string['invalidxmlfile'] = '\"$a\" is not a valid XML file';
+$string['listnopeers'] = 'No peers of item found.';
+$string['listnoitem'] = 'Item not found.';
+$string['listnochildren'] = 'No children of item found.';
+$string['listupdatefail'] = 'DB operation failed when editing list hierarchy.';
+$string['listcantmoveup'] = 'Failed to move the item up, it is the first of it\'s peers.';
+$string['listcantmovedown'] = 'Failed to move item down, it is the last of it\'s peers.';
+$string['listcantmoveleft'] = 'Failed to move item left, it has no parent.';
+$string['listcantmoveright'] = 'Failed to move item right, their is no peer to make it a child of. Move it below another peer and then you can move it right.';
$string['loginasonecourse'] = 'You can not enter this course.<br /> You have to terminate the \"Login as\" session before entering any other course.';
$string['loginasnoenrol'] = 'You can not use enrol or unenrol when in course \"Login as\" session.';
$string['missingfield'] = 'Field \"$a\" is missing';
$string['questiondoesnotexist'] = 'This question does not exist';
$string['unknownquestiontype'] = 'Unknown question type: $a.';
$string['fractionsnomax'] = 'One of the answers should have a score of 100%% so it is possible to get full marks for this question.';
-
+$string['makechildof'] = "Make Child of '\$a'";
+$string['maketoplevelitem'] = 'Move to top level';
?>
--- /dev/null
+<?php // $Id$
+
+///////////////////////////////////////////////////////////////////////////
+// //
+// NOTICE OF COPYRIGHT //
+// //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment //
+// http://moodle.com //
+// //
+// Copyright (C) 2007 Jamie Pratt http://jamiep.org //
+// //
+// 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 //
+// //
+///////////////////////////////////////////////////////////////////////////
+
+/**
+ * Classes for displaying and editing a nested list of items.
+ *
+ * Handles functionality for :
+ *
+ * Construction of nested list from db records with some key pointing to a parent id.
+ * Display of list with or without editing icons with optional pagination.
+ * Reordering of items works across pages.
+ * Processing of editing actions on list.
+ *
+ * @author Jamie Pratt
+ * @version $Id$
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package moodlecore
+ */
+
+
+class moodle_list{
+ var $attributes;
+ var $listitemclassname = 'list_item';
+ /**
+ * An array of $listitemclassname objects.
+ *
+ * @var array
+ */
+ var $items = array();
+ /**
+ * ol / ul
+ *
+ * @var string
+ */
+ var $type;
+ /**
+ *
+ * @var list_item or derived class
+ */
+ var $parentitem;
+ var $table;
+ var $fieldnamesparent = 'parent';
+ var $sortby = 'parent, sortorder, name';
+ /**
+ * Records from db, only used in top level list.
+ *
+ * @var array
+ */
+ var $records = array();
+
+ var $editable;
+
+ /**
+ * Key is child id, value is parent.
+ *
+ * @var array
+ */
+ var $childparent;
+
+//------------------------------------------------------
+//vars used for pagination.
+ var $page = 0;// 0 means no pagination
+ var $firstitem = 1;
+ var $lastitem = 999999;
+ var $topcount;
+ var $pagecount;
+//------------------------------------------------------
+ var $pageurl;
+ var $pageparams = array();
+
+ var $str;
+ /**
+ * Constructor function
+ *
+ * @param string $type
+ * @param string $attributes
+ * @param boolean $editable
+ * @param integer $page if 0 no pagination.
+ * @return moodle_list
+ */
+ function moodle_list($type='ul', $attributes='', $editable = false, $page = 0){
+ $this->editable = $editable;
+ $this->attributes = $attributes;
+ $this->type = $type;
+ $this->page = $page;
+ $this->pageurl = strip_querystring(qualified_me());//default
+ if (!empty($this->page)){
+ $this->add_page_params(array('page' => $this->page));
+ }
+ }
+ /**
+ * Add an array of params to the params for this page.
+ *
+ * @param unknown_type $params
+ */
+ function add_page_params($params){
+ $this->pageparams = $params + $this->pageparams;
+ }
+
+ /**
+ * Get url and query string for an action on this page (get_url() + sesskey)
+ *
+ * @param array $overrideparams an array of params which override $this->pageparams
+ * @return string
+ */
+ function get_action_url($overrideparams = array()){
+ global $USER;
+
+ $arr = array();
+ $paramarray = $overrideparams + $this->pageparams + array('sesskey'=>$USER->sesskey);
+ foreach ($paramarray as $key => $val){
+ $arr[] = urlencode($key)."=".urlencode($val);
+ }
+ $params = implode($arr, "&");
+
+ return $this->pageurl.'?'.$params;
+ }
+
+ /**
+ * Get url and query string for this page
+ *
+ * @param array $overrideparams an array of params which override $this->pageparams
+ * @return string
+ */
+ function get_url($overrideparams = array()){
+
+ $arr = array();
+ $paramarray = $overrideparams + $this->pageparams;
+ foreach ($paramarray as $key => $val){
+ $arr[] = urlencode($key)."=".urlencode($val);
+ }
+ $params = implode($arr, "&");
+
+ return $this->pageurl.'?'.$params;
+ }
+
+ /**
+ * Returns html string.
+ *
+ * @param integer $indent depth of indentation.
+ */
+ function to_html($indent=0, $extraargs=array()){
+ if (count($this->items)){
+ $tabs = str_repeat("\t", $indent);
+ $html = $tabs.'<'.$this->type.((!empty($this->attributes))?(' '.$this->attributes):'').">\n";
+ $first = true;
+ $itemiter = 1;
+ $lastitem = '';
+
+ foreach ($this->items as $item){
+ $last = (count($this->items) == $itemiter);
+ if ($itemiter >= $this->firstitem && $itemiter <= $this->lastitem ){
+ if ($this->editable){
+ $item->set_icon_html($first, $last, $lastitem);
+ }
+ $html .= "$tabs\t<li".((!empty($item->attributes))?(' '.$item->attributes):'').">";
+ $html .= $item->to_html($indent+1, $extraargs);
+ $html .= "</li>\n";
+ }
+ $first = false;
+ $lastitem = $item;
+ $itemiter++;
+ }
+ $html .= $tabs."</".$this->type.">\n";
+ } else {
+ $html = '';
+ }
+ return $html;
+ }
+
+ /**
+ * Recurse down the tree and find an item by it's id.
+ *
+ * @param integer $id
+ * @return list_item *copy* or null if item is not found
+ */
+ function find_item($id, $suppresserror = false){
+ if (isset($this->items)){
+ foreach ($this->items as $key => $child){
+ if ($child->id == $id){
+ return $this->items[$key];
+ }
+ }
+ foreach (array_keys($this->items) as $key){
+ $thischild =& $this->items[$key];
+ $ref = $thischild->children->find_item($id, true);//error always reported at top level
+ if ($ref !== null){
+ return $ref;
+ }
+ }
+ }
+
+ if (!$suppresserror){
+ print_error('listnoitem');
+ }
+ return null;
+ }
+
+
+
+ function add_item(&$item){
+ $this->items[] =& $item;
+ }
+
+ function set_parent(&$parent){
+ $this->parentitem =& $parent;
+ }
+
+
+ /**
+ * Produces a hierarchical tree of list items from a flat array of records.
+ * 'parent' field is expected to point to a parent record.
+ * records are already sorted.
+ * If the parent field doesn't point to another record in the array then this is
+ * a top level list
+ *
+ * @param array $records
+ * @param string $listitemclassname
+ * @param integer $itemsperpage no of top level items.
+ */
+ function list_from_records($itemsperpage = 25){
+ $this->get_records();
+ $records = $this->records;
+ $page = $this->page;
+ if (!empty($page)) {
+ $this->firstitem = ($page-1) * $itemsperpage + 1;
+ $this->lastitem = $this->firstitem + $itemsperpage - 1;
+ }
+ $itemiter = 1;
+ //make a simple array which is easier to search
+ $this->childparent = array();
+ foreach ($records as $record){
+ $this->childparent[$record->id] = $record->parent;
+ }
+ //create top level list items and they're responsible for creating their children
+ foreach ($records as $record){
+ if (!array_key_exists($record->parent, $this->childparent)){
+ //if this record is not a child of another record then
+
+ //make list item for top level for all items
+ //we need the info about the top level items for reordering peers.
+ $newlistitem =& new $this->listitemclassname($record, $this);
+ if ($itemiter >= $this->firstitem && $itemiter <= $this->lastitem ){
+ //but don't recurse down the tree for items that are not on this page
+ $newlistitem->create_children($records, $this->childparent, $record->id);
+ }
+ $itemiter++;
+ }
+ }
+ $this->topcount = $itemiter - 1;
+ $this->pagecount = (integer) ceil( $this->topcount / QUESTION_PAGE_LENGTH );
+ }
+
+ /**
+ * Should be overriden to return an array of records of list items.
+ *
+ */
+ function get_records() {
+ }
+
+ /**
+ * display list of page numbers for navigation
+ */
+ function display_page_numbers() {
+ if (!empty($this->page) && ($this->pagecount>1)){
+ echo "<div class=\"paging\">".get_string('page').":\n";
+ foreach (range(1,$this->pagecount) as $currentpage) {
+ if ($this->page == $currentpage) {
+ echo " $currentpage \n";
+ }
+ else {
+ echo "<a href=\"".$this->get_url(array('page'=>$currentpage))."\">";
+ echo " $currentpage </a>\n";
+ }
+ }
+ echo "</div>";
+ }
+ }
+
+ /**
+ * Returns an array of ids of peers of an item.
+ *
+ * @param int itemid - if given, restrict records to those with this parent id.
+ * @return array peer ids
+ */
+ function get_items_peers($itemid) {
+ $itemref = $this->find_item($itemid);
+ $peerids = $itemref->parentlist->get_child_ids();
+ return $peerids;
+ }
+
+ /**
+ * Returns an array of ids of child items.
+ *
+ * @return array peer ids
+ */
+ function get_child_ids() {
+ $childids = array();
+ foreach ($this->items as $child){
+ $childids[] = $child->id;
+ }
+ return $childids;
+ }
+
+ /**
+ * Move a record up or down
+ *
+ * @param string $direction up / down
+ * @param integer $id
+ */
+ function move_item_up_down($direction, $id) {
+ $peers = $this->get_items_peers($id);
+ $itemkey = array_search($id, $peers);
+ switch ($direction) {
+ case 'down' :
+ if (isset($peers[$itemkey+1])){
+ $olditem = $peers[$itemkey+1];
+ $peers[$itemkey+1] = $id;
+ $peers[$itemkey] = $olditem;
+ } else {
+ print_error('listcantmoveup');
+
+ }
+ break;
+
+ case 'up' :
+ if (isset($peers[$itemkey-1])){
+ $olditem = $peers[$itemkey-1];
+ $peers[$itemkey-1] = $id;
+ $peers[$itemkey] = $olditem;
+ } else {
+ print_error('listcantmovedown');
+ }
+ break;
+ }
+ $this->reorder_peers($peers);
+ }
+ function reorder_peers($peers){
+ foreach ($peers as $key => $peer) {
+ if (! set_field("{$this->table}", "sortorder", $key, "id", $peer)) {
+ print_error('listupdatefail');
+ }
+ }
+ }
+ function move_item_left($id) {
+ $item = $this->find_item($id);
+ if (!isset($item->parentlist->parentitem->parentlist)){
+ print_error('listcantmoveleft');
+ } else {
+ $newpeers = $this->get_items_peers($item->parentlist->parentitem->id);
+ if (isset($item->parentlist->parentitem->parentlist->parentitem)){
+ $newparent = $item->parentlist->parentitem->parentlist->parentitem->id;
+ } else {
+ $newparent = 0; // top level item
+ }
+ if (!set_field("{$this->table}", "parent", $newparent, "id", $item->id)) {
+ print_error('listupdatefail');
+ } else {
+ $oldparentkey = array_search($item->parentlist->parentitem->id, $newpeers);
+ $neworder = array_merge(array_slice($newpeers, 0, $oldparentkey+1), array($item->id), array_slice($newpeers, $oldparentkey+1));
+ $this->reorder_peers($neworder);
+ }
+ }
+ }
+ /**
+ * Make item with id $id the child of the peer that is just above it in the sort order.
+ *
+ * @param integer $id
+ */
+ function move_item_right($id) {
+ $peers = $this->get_items_peers($id);
+ $itemkey = array_search($id, $peers);
+ if (!isset($peers[$itemkey-1])){
+ print_error('listcantmoveright');
+ } else {
+ if (!set_field("{$this->table}", "parent", $peers[$itemkey-1], "id", $peers[$itemkey])) {
+ print_error('listupdatefail');
+ } else {
+ $newparent = $this->find_item($peers[$itemkey-1]);
+ if (isset($newparent->children)){
+ $newpeers = $newparent->children->get_child_ids();
+ }
+ if ($newpeers){
+ $newpeers[] = $peers[$itemkey];
+ $this->reorder_peers($newpeers);
+ }
+ }
+ }
+ }
+
+ /**
+ * process any actions.
+ *
+ * @param integer $left id of item to move left
+ * @param integer $right id of item to move right
+ * @param integer $moveup id of item to move up
+ * @param integer $movedown id of item to move down
+ * @return unknown
+ */
+ function process_actions($left, $right, $moveup, $movedown){
+ if (!empty($left)) {
+ $this->move_item_left($left);
+ } else if (!empty($right)) {
+ $this->move_item_right($right);
+ } else if (!empty($moveup)) {
+ $this->move_item_up_down('up', $moveup);
+ if ($moveup == $this->items[$this->firstitem -1]->id){//redirect to page that item has been moved to.
+ $this->page --;
+ $this->add_page_params(array('page'=>$this->page));
+ }
+ } else if (!empty($movedown)) {
+ $this->move_item_up_down('down', $movedown);
+ if ($movedown == $this->items[$this->lastitem -1]->id){//redirect to page that item has been moved to.
+ $this->page ++;
+ $this->add_page_params(array('page'=>$this->page));
+ }
+ } else {
+ return false;
+ }
+
+ redirect($this->get_url());
+ }
+}
+
+class list_item{
+ /**
+ * id of record, used if list is editable
+ *
+ * @var integer
+ */
+ var $id;
+ /**
+ * name of this item, used if list is editable
+ *
+ * @var string
+ */
+ var $name;
+ /**
+ * The object or string representing this item.
+ *
+ * @var mixed
+ */
+ var $item;
+ var $fieldnamesname = 'name';
+ var $attributes;
+ var $iconhtml = '';
+ /**
+ *
+ * @var moodle_list
+ */
+ var $parentlist;
+ /**
+ * Set if there are any children of this listitem.
+ *
+ * @var moodle_list
+ */
+ var $children;
+ /**
+ * Constructor
+ *
+ * @param mixed $item fragment of html for list item or record
+ * @param string $attributes attributes for li tag
+ * @return list_item
+ */
+ function list_item($item, &$parent, $attributes=''){
+ $this->item = $item;
+ if (is_object($this->item)) {
+ $this->id = $this->item->id;
+ $this->name = $this->item->{$this->fieldnamesname};
+ }
+ $this->set_parent($parent);
+ $this->attributes = $attributes;
+ $parentlistclass = get_class($parent);
+ $this->children =& new $parentlistclass($parent->type, $parent->attributes, $parent->editable, $parent->page);
+ $this->children->add_page_params($parent->pageparams);
+ $this->children->pageurl = $parent->pageurl;
+ $this->children->set_parent($this);
+ }
+ /**
+ * Output the html just for this item. Called by to_html which adds html for children.
+ *
+ */
+ function item_html($extraargs = array()){
+ if (is_string($this->item)){
+ $html = $this->item;
+ } elseif (is_object($this->item)) {
+ //for debug purposes only. You should create a sub class to
+ //properly handle the record
+ $html = join(', ', (array)$this->item);
+ }
+ return $html;
+ }
+ /**
+ * Returns html
+ *
+ * @param integer $indent
+ * @param array $extraargs any extra data that is needed to print the list item
+ * may be used by sub class.
+ * @return string html
+ */
+ function to_html($indent=0, $extraargs = array()){
+ $tabs = str_repeat("\t", $indent);
+
+ if (isset($this->children)){
+ $childrenhtml = $this->children->to_html($indent+1, $extraargs);
+ } else {
+ $childrenhtml = '';
+ }
+ return $this->item_html($extraargs).$this->iconhtml.(($childrenhtml !='')?("\n".$childrenhtml):'');
+ }
+
+ function set_icon_html($first, $last, &$lastitem){
+ global $CFG;
+ $strmoveup = get_string('moveup');
+ $strmovedown = get_string('movedown');
+ $pixpath = $CFG->pixpath;
+ $icons = ' ';
+ if (!empty($this->parentlist->page)) {
+ $pagelink="&page={$this->parentlist->page}";
+ } else {
+ $pagelink="";
+ }
+ if (isset($this->parentlist->parentitem)) {
+ $parentitem =& $this->parentlist->parentitem;
+ if (isset($parentitem->parentlist->parentitem)){
+ $action = get_string('makechildof', 'question', $parentitem->parentlist->parentitem->name);
+ } else {
+ $action = get_string('maketoplevelitem', 'question');
+ }
+ $icons .= '<a title="' . $action .'" href="'.$this->parentlist->get_action_url().'&left=' . $this->id .'">
+ <img src="' . $pixpath . '/t/left.gif" class="iconsmall" alt="' . $action. '" /></a> ';
+ } else {
+ $icons .= '<img src="' . $pixpath . '/spacer.gif" class="iconsmall" alt="" />';
+ }
+
+ if (!$first) {
+ $icons .= '<a title="' . $strmoveup .'" href="'.$this->parentlist->get_action_url().'&moveup=' . $this->id .'">
+ <img src="' . $pixpath . '/t/up.gif" class="iconsmall" alt="' . $strmoveup. '" /></a> ';
+ } else {
+ $icons .= '<img src="' . $pixpath . '/spacer.gif" class="iconsmall" alt="" />';
+ }
+
+ if (!$last) {
+ $icons .= '<a title="' . $strmovedown .'" href="'.$this->parentlist->get_action_url().'&movedown=' . $this->id .'">
+ <img src="' . $pixpath . '/t/down.gif" class="iconsmall" alt="' .$strmovedown. '" /></a> ';
+ } else {
+ $icons .= '<img src="' . $pixpath . '/spacer.gif" class="iconsmall" alt="" />';
+ }
+
+ if (!empty($lastitem)) {
+ $makechildof = get_string('makechildof', 'question', $lastitem->name);
+ $icons .= '<a title="' . $makechildof .'" href="'.$this->parentlist->get_action_url().'&right=' . $this->id .'">
+ <img src="' . $pixpath . '/t/right.gif" class="iconsmall" alt="' . $makechildof. '" /></a> ';
+ } else {
+ $icons .= '<img src="' . $pixpath . '/spacer.gif" class="iconsmall" alt="" />';
+ }
+
+ $this->iconhtml = $icons;
+ }
+ /**
+ * Recurse down tree creating list_items, called from moodle_list::list_from_records
+ *
+ * @param array $records
+ * @param array $children
+ * @param integer $thisrecordid
+ */
+ function create_children(&$records, &$children, $thisrecordid){
+ //keys where value is $thisrecordid
+ $thischildren = array_keys($children, $thisrecordid);
+ if (count($thischildren)){
+ foreach ($thischildren as $child){
+ $thisclass = get_class($this);
+ $newlistitem =& new $thisclass($records[$child], $this->children, $this->attributes);
+ $newlistitem->create_children($records, $children, $records[$child]->id);
+ }
+ }
+ }
+ function set_parent(&$parent){
+ $this->parentlist =& $parent;
+ $parent->add_item($this);
+ }
+
+}
+?>
\ No newline at end of file
// get values from form
$param = new stdClass();
- $id = required_param('id',PARAM_INT); // course id
- $param->moveup = optional_param('moveup',0,PARAM_INT);
- $param->movedown = optional_param('movedown',0,PARAM_INT);
- $param->hide = optional_param('hide',0,PARAM_INT);
- $param->delete = optional_param('delete',0,PARAM_INT);
- $param->confirm = optional_param('confirm',0,PARAM_INT);
- $param->cancel = optional_param('cancel','',PARAM_ALPHA);
- $param->move = optional_param('move',0,PARAM_INT);
- $param->moveto = optional_param('moveto',0,PARAM_INT);
- $param->publish = optional_param('publish',0,PARAM_INT);
- $param->addcategory = optional_param('addcategory','',PARAM_NOTAGS);
- $param->edit = optional_param('edit',0,PARAM_INT);
- $param->updateid = optional_param('updateid',0,PARAM_INT);
- $param->page = optional_param('page',1,PARAM_INT);
+
+ $id = required_param('id', PARAM_INT); // course id
+ $param->page = optional_param('page', 1, PARAM_INT);
+
+ $param->moveup = optional_param('moveup', 0, PARAM_INT);
+ $param->movedown = optional_param('movedown', 0, PARAM_INT);
+ $param->left = optional_param('left', 0, PARAM_INT);
+ $param->right = optional_param('right', 0, PARAM_INT);
+ $param->hide = optional_param('hide', 0, PARAM_INT);
+ $param->delete = optional_param('delete', 0, PARAM_INT);
+ $param->confirm = optional_param('confirm', 0, PARAM_INT);
+ $param->cancel = optional_param('cancel', '', PARAM_ALPHA);
+ $param->move = optional_param('move', 0, PARAM_INT);
+ $param->moveto = optional_param('moveto', 0, PARAM_INT);
+ $param->publish = optional_param('publish', 0, PARAM_INT);
+ $param->addcategory = optional_param('addcategory', '', PARAM_NOTAGS);
+ $param->edit = optional_param('edit', 0, PARAM_INT);
+ $param->updateid = optional_param('updateid', 0, PARAM_INT);
if (! $course = get_record("course", "id", $id)) {
error("Course ID is incorrect");
}
-
+
$context = get_context_instance(CONTEXT_COURSE, $id);
-
+
require_login($course->id, false);
require_capability('moodle/question:managecategory', $context);
- $qcobject = new question_category_object();
- $qcobject->set_course($course);
+ $qcobject = new question_category_object($param->page);
- // Page header
- // TODO: generalise this to any activity
- if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
+ if ($qcobject->editlist->process_actions($param->left, $param->right, $param->moveup, $param->movedown)) {
+ //processing of these actions is handled in the method and page redirects.
+ } else if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
+ // Page header
+ // TODO: generalise this to any activity
$strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))
? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
: "";
} else {
$qcobject->delete_category($param->delete);
}
- } else if (!empty($param->moveup)) {
- $qcobject->move_category_up_down('up', $param->moveup);
- } else if (!empty($param->movedown)) {
- $qcobject->move_category_up_down('down', $param->movedown);
} else if (!empty($param->hide)) {
$qcobject->publish_category(false, $param->hide);
} else if (!empty($param->move)) {
}
// display the user interface
- $qcobject->display_user_interface($param->page);
+ $qcobject->display_user_interface();
print_footer($course);
?>
*/
// number of categories to display on page
-define( "PAGE_LENGTH",25 );
+define("QUESTION_PAGE_LENGTH", 25);
+
+require_once("$CFG->libdir/listlib.php");
+
+class question_category_list extends moodle_list {
+ var $table = "question_categories";
+ var $listitemclassname = 'question_category_list_item';
+ function question_category_list($type='ul', $attributes='', $editable = false, $page = 0){
+ parent::moodle_list($type, $attributes, $editable, $page);
+ }
+ function get_records() {
+ global $COURSE, $CFG;
+ $categories = get_records($this->table, 'course', "{$COURSE->id}", $this->sortby);
+
+ $catids = array_keys($categories);
+ $select = "WHERE category IN ('".join("', '", $catids)."') AND hidden='0' AND parent='0'";
+ $questioncounts = get_records_sql_menu('SELECT category, COUNT(*) FROM '. $CFG->prefix . 'question' .' '. $select.' GROUP BY category');
+ foreach ($categories as $categoryid => $category){
+ if (isset($questioncounts[$categoryid])){
+ $categories[$categoryid]->questioncount = $questioncounts[$categoryid];
+ } else {
+ $categories[$categoryid]->questioncount = 0;
+ }
+ }
+ $this->records = $categories;
+ }
+}
+
+class question_category_list_item extends list_item {
+
+
+ function item_html($extraargs = array()){
+ global $CFG;
+ $pixpath = $CFG->pixpath;
+ $str = $extraargs['str'];
+ $category = $this->item;
+
+ $linkcss = $category->publish ? ' class="published" ' : ' class="unpublished" ';
+
+ if (!empty($parent->page)) {
+ $pagelink="&page=".$parent->page;
+ } else {
+ $pagelink="";
+ }
+
+ /// Each section adds html to be displayed as part of this list item
+
+ $item = '<a ' . $linkcss . ' title="' . $str->edit. '" href="'.$this->parentlist->get_action_url().'&edit=' . $this->id .'">
+ <img src="' . $pixpath . '/t/edit.gif" class="iconsmall"
+ alt="' .$str->edit. '" /> ' . $category->name . '('.$category->questioncount.')'. '</a>';
+
+ $item .= ' '. $category->info;
+
+
+ if (!empty($category->publish)) {
+ $item .= '<a title="' . $str->hide . '" href="'.$this->parentlist->get_action_url().'&hide=' . $this->id .'">
+ <img src="' . $pixpath . '/t/hide.gif" class="iconsmall" alt="' .$str->hide. '" /></a> ';
+ } else {
+ $item .= '<a title="' . $str->publish . '" href="'.$this->parentlist->get_action_url().'&publish=' . $this->id .'">
+ <img src="' . $pixpath . '/t/show.gif" class="iconsmall" alt="' .$str->publish. '" /></a> ';
+ }
+
+ if ($category->id != $extraargs['defaultcategory']->id) {
+ $item .= '<a title="' . $str->delete . '"href="'.$this->parentlist->get_action_url().'&delete=' . $this->id .'">
+ <img src="' . $pixpath . '/t/delete.gif" class="iconsmall" alt="' .$str->delete. '" /></a> ';
+ }
+
+ return $item;
+
+
+ }
+
+}
+
/**
* Class representing question categories
- *
+ *
* @package questionbank
*/
class question_category_object {
var $str;
var $pixpath;
- var $edittable;
+ /**
+ * Nested list to display categories.
+ *
+ * @var question_category_list
+ */
+ var $editlist;
var $newtable;
var $tab;
var $tabsize = 3;
var $categories;
var $categorystrings;
var $defaultcategory;
- var $course;
- var $topcount;
/**
* Constructor
*
* Gets necessary strings and sets relevant path information
*/
- function question_category_object() {
- global $CFG;
+ function question_category_object($page) {
+ global $CFG, $COURSE;
$this->tab = str_repeat(' ', $this->tabsize);
$this->str->page = get_string('page');
$this->pixpath = $CFG->pixpath;
- }
+ $this->editlist = new question_category_list('ul', '', true, $page);
+ $this->editlist->add_page_params(array('id'=>$COURSE->id));
+ $this->initialize();
- /**
- * Sets the course for this object
- *
- * @param object course
- */
- function set_course($course) {
- $this->course = $course;
}
+
+
/**
* Displays the user interface
*
- * @param object modform
- * @param int $page page number to display (0=don't paginate)
*/
- function display_user_interface($page=0) {
- $this->initialize();
+ function display_user_interface() {
+
+ /// Interface for editing existing categories
+ print_heading_with_help($this->str->editcategories, 'categories', 'quiz');
+ $this->output_edit_list();
+
+ echo '<br />';
/// Interface for adding a new category:
print_heading_with_help($this->str->addcategory, 'categories_edit', 'quiz');
$this->output_new_table();
echo '<br />';
- /// Interface for editing existing categories
- print_heading_with_help($this->str->editcategories, 'categories', 'quiz');
- $this->output_edit_table($page);
- if ($this->topcount>PAGE_LENGTH) {
- $this->display_page_numbers($page);
- }
- echo '<br />';
-
}
* Initializes this classes general category-related variables
*/
function initialize() {
+ global $COURSE, $CFG;
/// Get the existing categories
- if (!$this->defaultcategory = get_default_question_category($this->course->id)) {
+ if (!$this->defaultcategory = get_default_question_category($COURSE->id)) {
error("Error: Could not find or make a category!");
}
- $this->categories = $this->get_question_categories(null, "parent, sortorder, name ASC");
+ $this->editlist->list_from_records(QUESTION_PAGE_LENGTH);
- $this->categories = $this->arrange_categories($this->categories);
+ $this->categories = $this->editlist->records;
// create the array of id=>full_name strings
$this->categorystrings = $this->expanded_category_strings($this->categories);
- // for pagination calculate number of 'top' categories and hence number of pages
- // (pagination only based on top categories)
- $count = 0;
- foreach( $this->categories as $category ) {
- if ($category->parent==0) {
- ++$count;
- }
- }
- $this->topcount = $count;
- $this->pagecount = (integer) ceil( $count / PAGE_LENGTH );
- }
-
- /**
- * display list of page numbers for navigation
- */
- function display_page_numbers( $page=0 ) {
- global $USER;
- echo "<div class=\"paging\">{$this->str->page}:\n";
- foreach (range(1,$this->pagecount) as $currentpage) {
- if ($page == $currentpage) {
- echo " $currentpage \n";
- }
- else {
- echo "<a href=\"category.php?id={$this->course->id}&page=$currentpage&sesskey={$USER->sesskey}\">";
- echo " $currentpage </a>\n";
- }
- }
- echo "</div>";
}
+
/**
* Outputs a table to allow entry of a new category
*/
function output_new_table() {
- global $USER;
+ global $USER, $COURSE;
$publishoptions[0] = get_string("no");
$publishoptions[1] = get_string("yes");
$this->newtable->head = array ($this->str->parent, $this->str->category, $this->str->categoryinfo, $this->str->publish, $this->str->action);
- $this->newtable->width = 200;
+ $this->newtable->width = '200';
$this->newtable->data[] = array();
$this->newtable->tablealign = 'center';
echo '<form action="category.php" method="post">';
echo '<fieldset class="invisiblefieldset" style="display: block">';
echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
- echo '<input type="hidden" name="id" value="'. $this->course->id . '" />';
+ echo '<input type="hidden" name="id" value="'. $COURSE->id . '" />';
echo '<input type="hidden" name="addcategory" value="true" />';
print_table($this->newtable);
echo '</fieldset>';
echo '</form>';
}
+
/**
- * Outputs a table to allow editing/rearranging of existing categories
+ * Outputs a list to allow editing/rearranging of existing categories
*
* $this->initialize() must have already been called
*
- * @param object course
* @param int $page page to display (0=do not paginate)
*/
- function output_edit_table($page=0) {
- $this->edittable->head = array ($this->str->category, $this->str->categoryinfo, $this->str->questions, $this->str->publish,
- $this->str->delete, $this->str->order, $this->str->parent);
- $this->edittable->width = 200;
- $this->edittable->tablealign = 'center';
-
- $courses = $this->course->shortname;
-
- // if pagination required work out range
- if (!empty($page)) {
- $firstcat = ($page-1) * PAGE_LENGTH + 1;
- $lastcat = $firstcat + PAGE_LENGTH - 1;
- }
- else {
- $firstcat = 1;
- $lastcat = $this->topcount;
- }
-//echo "$firstcat $lastcat $page"; die;
- $this->build_edit_table_body($this->categories, $page, $firstcat, $lastcat);
- print_table($this->edittable);
- }
-
- /**
- * Recursively builds up the edit-categories table body
- *
- * @param array categories contains category objects in a tree representation
- * @param mixed courses String with shortname of course | array containing courseid=>shortname
- * @param int depth controls the indenting
- */
- function build_edit_table_body($categories, $page = 0, $firstcat = 1, $lastcat = 99999, $depth = 0) {
- $countcats = count($categories);
- $count = 0;
- $first = true;
- $last = false;
- $topcount = 0;
-
- foreach ($categories as $category) {
- $count++;
- if ($count == $countcats) {
- $last = true;
- }
- // check if this category is on the display page
- if ($depth==0) {
- $topcount++;
- if (($topcount<$firstcat) or ($topcount>$lastcat)) {
- continue;
- }
- }
- $up = $first ? false : true;
- $down = $last ? false : true;
- $first = false;
- $this->edit_question_category_row($category, $depth, $up, $down, $page);
- if (isset($category->children)) {
- $this->build_edit_table_body($category->children, $page, $firstcat, $lastcat, $depth + 1);
- }
- }
+ function output_edit_list() {
+ print_box_start('boxwidthwide boxaligncenter generalbox');
+ echo $this->editlist->to_html(0, array('str'=>$this->str,
+ 'defaultcategory' => $this->defaultcategory));
+ print_box_end();
+ echo $this->editlist->display_page_numbers();
+
}
+
+
/**
* gets all the courseids for the given categories
*
return $courseids;
}
- /**
- * Constructs each row of the edit-categories table
- *
- * @param object category
- * @param int depth controls the indenting
- * @param string shortname short name of the course
- * @param boolean up can it be moved up?
- * @param boolean down can it be moved down?
- * @param int page page number
- */
- function edit_question_category_row($category, $depth, $up = false, $down = false, $page = 0) {
- global $USER;
- $fill = str_repeat($this->tab, $depth);
-
- $linkcss = $category->publish ? ' class="published" ' : ' class="unpublished" ';
-
- if (!empty($page)) {
- $pagelink="&page=$page";
- }
- else {
- $pagelink="";
- }
-
- /// Each section below adds a data cell to this table row
-
- $this->edittable->align["$category->id.name"] = "left";
- $this->edittable->wrap["$category->id.name"] = "nowrap";
- $row["$category->id.name"] = '<a ' . $linkcss . ' title="' . $this->str->edit. '" href="category.php?id=' . $this->course->id .
- '&edit=' . $category->id . '&sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/edit.gif" class="iconsmall"
- alt="' .$this->str->edit. '" /> ' . $fill . $category->name . '</a>';
-
- $this->edittable->align["$category->id.info"] = "left";
- $this->edittable->wrap["$category->id.info"] = "nowrap";
- $row["$category->id.info"] = '<a ' . $linkcss . ' title="' . $this->str->edit .'" href="category.php?id=' . $this->course->id .
- '&edit=' . $category->id . '&sesskey='.$USER->sesskey.$pagelink.'">' . $category->info . '</a>';
-
- $this->edittable->align["$category->id.qcount"] = "center";
- $row["$category->id.qcount"] = $category->questioncount;
-
- $this->edittable->align["$category->id.publish"] = "center";
- $this->edittable->wrap["$category->id.publish"] = "nowrap";
- if (!empty($category->publish)) {
- $row["$category->id.publish"] = '<a title="' . $this->str->hide . '" href="category.php?id=' . $this->course->id . '&hide=' . $category->id .
- '&sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/hide.gif" class="iconsmall" alt="' .$this->str->hide. '" /></a> ';
- } else {
- $row["$category->id.publish"] = '<a title="' . $this->str->publish . '" href="category.php?id=' . $this->course->id . '&publish=' . $category->id .
- '&sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/show.gif" class="iconsmall" alt="' .$this->str->publish. '" /></a> ';
- }
-
- if ($category->id != $this->defaultcategory->id) {
- $this->edittable->align["$category->id.delete"] = "center";
- $this->edittable->wrap["$category->id.delete"] = "nowrap";
- $row["$category->id.delete"] = '<a title="' . $this->str->delete . '" href="category.php?id=' . $this->course->id .
- '&delete=' . $category->id . '&sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/delete.gif" class="iconsmall" alt="' .$this->str->delete. '" /></a> ';
- } else {
- $row["$category->id.delete"] = '';
- }
-
- $this->edittable->align["$category->id.order"] = "left";
- $this->edittable->wrap["$category->id.order"] = "nowrap";
- $icons = '';
- if ($up) {
- $icons .= '<a title="' . $this->str->moveup .'" href="category.php?id=' . $this->course->id . '&moveup=' . $category->id . '&sesskey='.$USER->sesskey.$pagelink.'">
- <img src="' . $this->pixpath . '/t/up.gif" class="iconsmall" alt="' . $this->str->moveup. '" /></a> ';
- }
- if ($down) {
- $icons .= '<a title="' . $this->str->movedown .'" href="category.php?id=' . $this->course->id . '&movedown=' . $category->id . '&sesskey='.$USER->sesskey.$pagelink.'">
- <img src="' . $this->pixpath . '/t/down.gif" class="iconsmall" alt="' .$this->str->movedown. '" /></a> ';
- }
- $row["$category->id.order"]= $icons;
- $this->edittable->align["$category->id.moveto"] = "left";
- $this->edittable->wrap["$category->id.moveto"] = "nowrap";
- if ($category->id != $this->defaultcategory->id) {
- $viableparents = $this->categorystrings;
- $this->set_viable_parents($viableparents, $category);
- $viableparents = array(0=>$this->str->top) + $viableparents;
- $row["$category->id.moveto"] = popup_form ("category.php?id={$this->course->id}&move={$category->id}&sesskey=$USER->sesskey$pagelink&moveto=",
- $viableparents, "moveform{$category->id}", "$category->parent", "", "", "", true);
- } else {
- $row["$category->id.moveto"]='---';
- }
-
-
- $this->edittable->data[$category->id] = $row;
- }
-
-
- function edit_single_category($categoryid,$page=1) {
+ function edit_single_category($categoryid, $page=1) {
/// Interface for adding a new category
- global $USER;
- $this->initialize();
+ global $USER, $COURSE;
/// Interface for editing existing categories
if ($category = get_record("question_categories", "id", $categoryid)) {
helpbutton("categories_edit", $this->str->editcategory, "quiz");
echo '</h2>';
echo '<table width="100%"><tr><td>';
- $this->output_edit_single_table($category,$page);
+ $this->output_edit_single_table($category, $page);
echo '</td></tr></table>';
echo '<p><div align="center"><form action="category.php" method="get">
<div>
<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />
- <input type="hidden" name="id" value="' . $this->course->id . '" />
+ <input type="hidden" name="id" value="' . $COURSE->id . '" />
<input type="submit" value="' . $this->str->cancel . '" />
</div>
</form>
</div></p>';
- print_footer($this->course);
+ print_footer($COURSE);
exit;
} else {
- error("Category $categoryid not found", "category.php?id={$this->course->id}");
+ error("Category $categoryid not found", "category.php?id={$COURSE->id}");
}
}
* @param int page current page
*/
function output_edit_single_table($category, $page=1) {
- global $USER;
+ global $USER, $COURSE;
$publishoptions[0] = get_string("no");
$publishoptions[1] = get_string("yes");
$strupdate = get_string('update');
echo '<p><form action="category.php" method="post">';
echo '<fieldset class="invisiblefieldset">';
echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
- echo '<input type="hidden" name="id" value="'. $this->course->id . '" />';
+ echo '<input type="hidden" name="id" value="'. $COURSE->id . '" />';
echo '<input type="hidden" name="updateid" value="' . $category->id . '" />';
echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />";
print_table($edittable);
return $categorystrings;
}
- /**
- * Arranges the categories into a hierarchical tree
- *
- * If a category has children, it's "children" property holds an array of children
- * The questioncount for each category is also calculated
- *
- * @param array records a flat list of the categories
- * @return array categorytree a hierarchical list of the categories
- */
- function arrange_categories($records) {
- //TODO: get the question count for all records with one sql statement: select category, count(*) from question group by category
- $levels = array();
-
- // build a levels array, which places each record according to it's depth from the top level
- $parents = array(0);
- while (!empty($parents)) {
- $children = array();
- foreach ($records as $record) {
- if (in_array($record->parent, $parents)) {
- $children[] = $record->id;
- }
- }
- if (!empty($children)) {
- $levels[] = $children;
- }
- $parents = $children;
- }
- // if there is no hierarchy (e.g., if all records have parent == 0), set level[0] to these keys
- if (empty($levels)) {
- $levels[0] = array_keys($records);
- }
-
- // build a hierarchical array that depicts the parent-child relationships of the categories
- $categorytree = array();
- for ($index = count($levels) - 1; $index >= 0; $index--) {
- foreach($levels[$index] as $key) {
- $parentkey = $records[$key]->parent;
- if (!($records[$key]->questioncount = count_records('question', 'category', $records[$key]->id, 'hidden', 0, 'parent', '0'))) {
- $records[$key]->questioncount = 0;
- }
- if ($parentkey == 0) {
- $categorytree[$key] = $records[$key];
- } else {
- $records[$parentkey]->children[$key] = $records[$key];
- }
- }
- }
- return $categorytree;
- }
/**
* Sets the viable parents
* @return array categories
*/
function get_question_categories($parent=null, $sort="sortorder ASC") {
-
+ global $COURSE;
if (is_null($parent)) {
- $categories = get_records('question_categories', 'course', "{$this->course->id}", $sort);
+ $categories = get_records('question_categories', 'course', "{$COURSE->id}", $sort);
} else {
- $select = "parent = '$parent' AND course = '{$this->course->id}'";
+ $select = "parent = '$parent' AND course = '{$COURSE->id}'";
$categories = get_records_select('question_categories', $select, $sort);
}
return $categories;
* @param int destcategoryid id of category which will inherit the orphans of deletecat
*/
function delete_category($deletecat, $destcategoryid = null) {
- global $USER;
+ global $USER, $COURSE;
if (!$category = get_record("question_categories", "id", $deletecat)) { // security
- error("No such category $deletecat!", "category.php?id={$this->course->id}");
+ error("No such category $deletecat!", "category.php?id={$COURSE->id}");
}
if (!is_null($destcategoryid)) { // Need to move some questions before deleting the category
if (!$category2 = get_record("question_categories", "id", $destcategoryid)) { // security
- error("No such category $destcategoryid!", "category.php?id={$this->course->id}");
+ error("No such category $destcategoryid!", "category.php?id={$COURSE->id}");
}
if (! set_field('question', 'category', $destcategoryid, 'category', $deletecat)) {
- error("Error while moving questions from category '" . format_string($category->name) . "' to '$category2->name'", "category.php?id={$this->course->id}");
+ error("Error while moving questions from category '" . format_string($category->name) . "' to '$category2->name'", "category.php?id={$COURSE->id}");
}
} else {
echo "<p><div align=\"center\"><form action=\"category.php\" method=\"get\">";
echo '<fieldset class="invisiblefieldset">';
echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
- echo "<input type=\"hidden\" name=\"id\" value=\"{$this->course->id}\" />";
+ echo "<input type=\"hidden\" name=\"id\" value=\"{$COURSE->id}\" />";
echo "<input type=\"hidden\" name=\"delete\" value=\"$category->id\" />";
choose_from_menu($categorystrings, "confirm", "", "");
echo "<input type=\"submit\" value=\"". get_string("categorymoveto", "quiz") . "\" />";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->str->cancel}\" />";
echo '</fieldset>';
echo "</form></div></p>";
- print_footer($this->course);
+ print_footer($COURSE);
exit;
}
}
if ($childcats = get_records("question_categories", "parent", $category->id)) {
foreach ($childcats as $childcat) {
if (! set_field("question_categories", "parent", $category->parent, "id", $childcat->id)) {
- error("Could not update a child category!", "category.php?id={$this->course->id}");
+ error("Could not update a child category!", "category.php?id={$COURSE->id}");
}
}
}