Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/category_class.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 // number of categories to display on page
00030 define('QUESTION_PAGE_LENGTH', 25);
00031 
00032 require_once($CFG->libdir . '/listlib.php');
00033 require_once($CFG->dirroot . '/question/category_form.php');
00034 require_once($CFG->dirroot . '/question/move_form.php');
00035 
00036 
00043 class question_category_list extends moodle_list {
00044     public $table = "question_categories";
00045     public $listitemclassname = 'question_category_list_item';
00049     public $nextlist = null;
00053     public $lastlist = null;
00054 
00055     public $context = null;
00056     public $sortby = 'parent, sortorder, name';
00057 
00058     public function __construct($type='ul', $attributes='', $editable = false, $pageurl=null, $page = 0, $pageparamname = 'page', $itemsperpage = 20, $context = null){
00059         parent::__construct('ul', '', $editable, $pageurl, $page, 'cpage', $itemsperpage);
00060         $this->context = $context;
00061     }
00062 
00063     public function get_records() {
00064         $this->records = get_categories_for_contexts($this->context->id, $this->sortby);
00065     }
00066 }
00067 
00068 
00075 class question_category_list_item extends list_item {
00076     public function set_icon_html($first, $last, &$lastitem){
00077         global $CFG;
00078         $category = $this->item;
00079         $url = new moodle_url('/question/category.php', ($this->parentlist->pageurl->params() + array('edit'=>$category->id)));
00080         $this->icons['edit']= $this->image_icon(get_string('editthiscategory', 'question'), $url, 'edit');
00081         parent::set_icon_html($first, $last, $lastitem);
00082         $toplevel = ($this->parentlist->parentitem === null);//this is a top level item
00083         if (($this->parentlist->nextlist !== null) && $last && $toplevel && (count($this->parentlist->items)>1)){
00084             $url = new moodle_url($this->parentlist->pageurl, array('movedowncontext'=>$this->id, 'tocontext'=>$this->parentlist->nextlist->context->id, 'sesskey'=>sesskey()));
00085             $this->icons['down'] = $this->image_icon(
00086                 get_string('shareincontext', 'question', print_context_name($this->parentlist->nextlist->context)), $url, 'down');
00087         }
00088         if (($this->parentlist->lastlist !== null) && $first && $toplevel && (count($this->parentlist->items)>1)){
00089             $url = new moodle_url($this->parentlist->pageurl, array('moveupcontext'=>$this->id, 'tocontext'=>$this->parentlist->lastlist->context->id, 'sesskey'=>sesskey()));
00090             $this->icons['up'] = $this->image_icon(
00091                 get_string('shareincontext', 'question', print_context_name($this->parentlist->lastlist->context)), $url, 'up');
00092         }
00093     }
00094 
00095     public function item_html($extraargs = array()){
00096         global $CFG, $OUTPUT;
00097         $str = $extraargs['str'];
00098         $category = $this->item;
00099 
00100         $editqestions = get_string('editquestions', 'question');
00101 
00103         $questionbankurl = new moodle_url("/question/edit.php", ($this->parentlist->pageurl->params() + array('category'=>"$category->id,$category->contextid")));
00104         $catediturl = $this->parentlist->pageurl->out(true, array('edit' => $this->id));
00105         $item = "<b><a title=\"{$str->edit}\" href=\"$catediturl\">".$category->name ."</a></b> <a title=\"$editqestions\" href=\"$questionbankurl\">".'('.$category->questioncount.')</a>';
00106 
00107         $item .= '&nbsp;'. $category->info;
00108 
00109         // don't allow delete if this is the last category in this context.
00110         if (count($this->parentlist->records) != 1) {
00111             $item .=  '<a title="' . $str->delete . '" href="'.$this->parentlist->pageurl->out(true, array('delete'=>$this->id, 'sesskey'=>sesskey())).'">
00112                     <img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' .$str->delete. '" /></a>';
00113         }
00114 
00115         return $item;
00116     }
00117 }
00118 
00119 
00126 class question_category_object {
00127 
00128     var $str;
00134     var $editlists = array();
00135     var $newtable;
00136     var $tab;
00137     var $tabsize = 3;
00138 
00142     var $pageurl;
00146     var $catform;
00147 
00153     public function question_category_object($page, $pageurl, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts) {
00154         global $CFG, $COURSE, $OUTPUT;
00155 
00156         $this->tab = str_repeat('&nbsp;', $this->tabsize);
00157 
00158         $this->str->course         = get_string('course');
00159         $this->str->category       = get_string('category', 'question');
00160         $this->str->categoryinfo   = get_string('categoryinfo', 'question');
00161         $this->str->questions      = get_string('questions', 'question');
00162         $this->str->add            = get_string('add');
00163         $this->str->delete         = get_string('delete');
00164         $this->str->moveup         = get_string('moveup');
00165         $this->str->movedown       = get_string('movedown');
00166         $this->str->edit           = get_string('editthiscategory', 'question');
00167         $this->str->hide           = get_string('hide');
00168         $this->str->order          = get_string('order');
00169         $this->str->parent         = get_string('parent', 'question');
00170         $this->str->add            = get_string('add');
00171         $this->str->action         = get_string('action');
00172         $this->str->top            = get_string('top');
00173         $this->str->addcategory    = get_string('addcategory', 'question');
00174         $this->str->editcategory   = get_string('editcategory', 'question');
00175         $this->str->cancel         = get_string('cancel');
00176         $this->str->editcategories = get_string('editcategories', 'question');
00177         $this->str->page           = get_string('page');
00178 
00179         $this->pageurl = $pageurl;
00180 
00181         $this->initialize($page, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts);
00182     }
00183 
00187     public function initialize($page, $contexts, $currentcat, $defaultcategory, $todelete, $addcontexts) {
00188         $lastlist = null;
00189         foreach ($contexts as $context){
00190             $this->editlists[$context->id] = new question_category_list('ul', '', true, $this->pageurl, $page, 'cpage', QUESTION_PAGE_LENGTH, $context);
00191             $this->editlists[$context->id]->lastlist =& $lastlist;
00192             if ($lastlist!== null){
00193                 $lastlist->nextlist =& $this->editlists[$context->id];
00194             }
00195             $lastlist =& $this->editlists[$context->id];
00196         }
00197 
00198         $count = 1;
00199         $paged = false;
00200         foreach ($this->editlists as $key => $list){
00201             list($paged, $count) = $this->editlists[$key]->list_from_records($paged, $count);
00202         }
00203         $this->catform = new question_category_edit_form($this->pageurl, compact('contexts', 'currentcat'));
00204         if (!$currentcat){
00205             $this->catform->set_data(array('parent'=>$defaultcategory));
00206         }
00207     }
00208 
00213     public function display_user_interface() {
00214 
00216         $this->output_edit_lists();
00217 
00218 
00219         echo '<br />';
00221         $this->output_new_table();
00222         echo '<br />';
00223 
00224     }
00225 
00229     public function output_new_table() {
00230         $this->catform->display();
00231     }
00232 
00239     public function output_edit_lists() {
00240         global $OUTPUT;
00241 
00242         echo $OUTPUT->heading_with_help(get_string('editcategories', 'question'), 'editcategories', 'question');
00243 
00244         foreach ($this->editlists as $context => $list){
00245             $listhtml = $list->to_html(0, array('str'=>$this->str));
00246             if ($listhtml){
00247                 echo $OUTPUT->box_start('boxwidthwide boxaligncenter generalbox questioncategories contextlevel' . $list->context->contextlevel);
00248                 echo $OUTPUT->heading(get_string('questioncatsfor', 'question', print_context_name(get_context_instance_by_id($context))), 3);
00249                 echo $listhtml;
00250                 echo $OUTPUT->box_end();
00251             }
00252         }
00253         echo $list->display_page_numbers();
00254      }
00255 
00262     public function get_course_ids($categories) {
00263         $courseids = array();
00264         foreach ($categories as $key=>$cat) {
00265             $courseids[$key] = $cat->course;
00266             if (!empty($cat->children)) {
00267                 $courseids = array_merge($courseids, $this->get_course_ids($cat->children));
00268             }
00269         }
00270         return $courseids;
00271     }
00272 
00273     public function edit_single_category($categoryid) {
00275         global $COURSE, $DB;
00277         if ($category = $DB->get_record("question_categories", array("id" => $categoryid))) {
00278 
00279             $category->parent = "$category->parent,$category->contextid";
00280             $category->submitbutton = get_string('savechanges');
00281             $category->categoryheader = $this->str->edit;
00282             $this->catform->set_data($category);
00283             $this->catform->display();
00284         } else {
00285             print_error('invalidcategory', '', '', $categoryid);
00286         }
00287     }
00288 
00298     public function set_viable_parents(&$parentstrings, $category) {
00299 
00300         unset($parentstrings[$category->id]);
00301         if (isset($category->children)) {
00302             foreach ($category->children as $child) {
00303                 $this->set_viable_parents($parentstrings, $child);
00304             }
00305         }
00306     }
00307 
00315     public function get_question_categories($parent=null, $sort="sortorder ASC") {
00316         global $COURSE, $DB;
00317         if (is_null($parent)) {
00318             $categories = $DB->get_records('question_categories', array('course' => $COURSE->id), $sort);
00319         } else {
00320             $select = "parent = ? AND course = ?";
00321             $categories = $DB->get_records_select('question_categories', $select, array($parent, $COURSE->id), $sort);
00322         }
00323         return $categories;
00324     }
00325 
00331     public function delete_category($categoryid) {
00332         global $CFG, $DB;
00333         question_can_delete_cat($categoryid);
00334         if (!$category = $DB->get_record("question_categories", array("id" => $categoryid))) {  // security
00335             print_error('unknowcategory');
00336         }
00338         $DB->set_field("question_categories", "parent", $category->parent, array("parent" => $category->id));
00339 
00341         $DB->delete_records("question_categories", array("id" => $category->id));
00342     }
00343 
00344     public function move_questions_and_delete_category($oldcat, $newcat){
00345         question_can_delete_cat($oldcat);
00346         $this->move_questions($oldcat, $newcat);
00347         $this->delete_category($oldcat);
00348     }
00349 
00350     public function display_move_form($questionsincategory, $category){
00351         global $OUTPUT;
00352         $vars = new stdClass();
00353         $vars->name = $category->name;
00354         $vars->count = $questionsincategory;
00355         echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
00356         $this->moveform->display();
00357     }
00358 
00359     public function move_questions($oldcat, $newcat){
00360         global $DB;
00361         $questionids = $DB->get_records_select_menu('question',
00362                 'category = ? AND (parent = 0 OR parent = id)', array($oldcat), '', 'id,1');
00363         question_move_questions_to_category(array_keys($questionids), $newcat);
00364     }
00365 
00369     public function add_category($newparent, $newcategory, $newinfo, $return = false) {
00370         global $DB;
00371         if (empty($newcategory)) {
00372             print_error('categorynamecantbeblank', 'question');
00373         }
00374         list($parentid, $contextid) = explode(',', $newparent);
00375         //moodle_form makes sure select element output is legal no need for further cleaning
00376         require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
00377 
00378         if ($parentid) {
00379             if(!($DB->get_field('question_categories', 'contextid', array('id' => $parentid)) == $contextid)) {
00380                 print_error('cannotinsertquestioncatecontext', 'question', '', array('cat'=>$newcategory, 'ctx'=>$contextid));
00381             }
00382         }
00383 
00384         $cat = new stdClass();
00385         $cat->parent = $parentid;
00386         $cat->contextid = $contextid;
00387         $cat->name = $newcategory;
00388         $cat->info = $newinfo;
00389         $cat->sortorder = 999;
00390         $cat->stamp = make_unique_id_code();
00391         $categoryid = $DB->insert_record("question_categories", $cat);
00392         if ($return) {
00393             return $categoryid;
00394         } else {
00395             redirect($this->pageurl);//always redirect after successful action
00396         }
00397     }
00398 
00402     public function update_category($updateid, $newparent, $newname, $newinfo) {
00403         global $CFG, $DB;
00404         if (empty($newname)) {
00405             print_error('categorynamecantbeblank', 'question');
00406         }
00407 
00408         // Get the record we are updating.
00409         $oldcat = $DB->get_record('question_categories', array('id' => $updateid));
00410         $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid);
00411 
00412         if (!empty($newparent) && !$lastcategoryinthiscontext) {
00413             list($parentid, $tocontextid) = explode(',', $newparent);
00414         } else {
00415             $parentid = $oldcat->parent;
00416             $tocontextid = $oldcat->contextid;
00417         }
00418 
00419         // Check permissions.
00420         $fromcontext = get_context_instance_by_id($oldcat->contextid);
00421         require_capability('moodle/question:managecategory', $fromcontext);
00422 
00423         // If moving to another context, check permissions some more.
00424         if ($oldcat->contextid != $tocontextid) {
00425             $tocontext = get_context_instance_by_id($tocontextid);
00426             require_capability('moodle/question:managecategory', $tocontext);
00427         }
00428 
00429         // Update the category record.
00430         $cat = null;
00431         $cat->id = $updateid;
00432         $cat->name = $newname;
00433         $cat->info = $newinfo;
00434         $cat->parent = $parentid;
00435         $cat->contextid = $tocontextid;
00436         $DB->update_record('question_categories', $cat);
00437 
00438         // If the category name has changed, rename any random questions in that category.
00439         if ($oldcat->name != $cat->name) {
00440             $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";
00441 
00442             $randomqtype = question_bank::get_qtype('random');
00443             $randomqname = $randomqtype->question_name($cat, false);
00444             $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0'));
00445 
00446             $randomqname = $randomqtype->question_name($cat, true);
00447             $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1'));
00448         }
00449 
00450         if ($oldcat->contextid != $tocontextid) {
00451             // Moving to a new context. Must move files belonging to questions.
00452             question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid);
00453         }
00454 
00455         redirect($this->pageurl);
00456     }
00457 }
 All Data Structures Namespaces Files Functions Variables Enumerations