Skip to content

Commit

Permalink
Merge branch 'wip-MDL-38147-master' of git://github.com/marinaglancy/…
Browse files Browse the repository at this point in the history
…moodle

Conflicts:
	lib/upgrade.txt
  • Loading branch information
Damyon Wiese committed Mar 26, 2013
2 parents 8af0dff + 4e53188 commit 0c241b9
Show file tree
Hide file tree
Showing 31 changed files with 3,322 additions and 1,006 deletions.
5 changes: 3 additions & 2 deletions blocks/course_list/block_course_list.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

include_once($CFG->dirroot . '/course/lib.php');
include_once($CFG->libdir . '/coursecatlib.php');

class block_course_list extends block_list {
function init() {
Expand Down Expand Up @@ -53,11 +54,11 @@ function get_content() {
}
}

$categories = get_categories("0"); // Parent = 0 ie top-level categories only
$categories = coursecat::get(0)->get_children(); // Parent = 0 ie top-level categories only
if ($categories) { //Check we have categories
if (count($categories) > 1 || (count($categories) == 1 && $DB->count_records('course') > 200)) { // Just print top level category links
foreach ($categories as $category) {
$categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id)));
$categoryname = $category->get_formatted_name();
$linkcss = $category->visible ? "" : " class=\"dimmed\" ";
$this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">".$icon . $categoryname . "</a>";
}
Expand Down
6 changes: 3 additions & 3 deletions cohort/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public function validation($data, $files) {
}

protected function get_category_options($currentcontextid) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
global $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$displaylist = coursecat::make_categories_list('moodle/cohort:manage');
$options = array();
$syscontext = context_system::instance();
if (has_capability('moodle/cohort:manage', $syscontext)) {
Expand Down
2 changes: 1 addition & 1 deletion cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function cohort_delete_cohort($cohort) {
* Somehow deal with cohorts when deleting course category,
* we can not just delete them because they might be used in enrol
* plugins or referenced in external systems.
* @param stdClass $category
* @param stdClass|coursecat $category
* @return void
*/
function cohort_delete_category($category) {
Expand Down
5 changes: 2 additions & 3 deletions course/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_once("../config.php");
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->libdir.'/textlib.class.php');
require_once($CFG->libdir. '/coursecatlib.php');

$id = required_param('id', PARAM_INT); // Category id
$page = optional_param('page', 0, PARAM_INT); // which page to show
Expand Down Expand Up @@ -69,9 +70,7 @@
echo $OUTPUT->header();

/// Print the category selector
$displaylist = array();
$notused = array();
make_categories_list($displaylist, $notused);
$displaylist = coursecat::make_categories_list();

echo '<div class="categorypicker">';
$select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
Expand Down
5 changes: 2 additions & 3 deletions course/completion_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ function definition() {
}

// Get category list
$list = array();
$parents = array();
make_categories_list($list, $parents);
require_once($CFG->libdir. '/coursecatlib.php');
$list = coursecat::make_categories_list();

// Get course list for select box
$selectbox = array();
Expand Down
139 changes: 46 additions & 93 deletions course/delete_category_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,83 @@

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->libdir. '/coursecatlib.php');

class delete_category_form extends moodleform {

var $_category;

function definition() {
global $CFG, $DB;

$mform =& $this->_form;
$category = $this->_customdata;
$categorycontext = context_coursecat::instance($category->id);
$this->_category = $category;

/// Check permissions, to see if it OK to give the option to delete
/// the contents, rather than move elsewhere.
/// Are there any subcategories of this one, can they be deleted?
$candeletecontent = true;
$tocheck = get_child_categories($category->id);
$containscategories = !empty($tocheck);
$categoryids = array($category->id);
while (!empty($tocheck)) {
$checkcat = array_pop($tocheck);
$childcategoryids[] = $checkcat->id;
$tocheck = $tocheck + get_child_categories($checkcat->id);
$chcontext = context_coursecat::instance($checkcat->id);
if ($candeletecontent && !has_capability('moodle/category:manage', $chcontext)) {
$candeletecontent = false;
}
}

/// Are there any courses in here, can they be deleted?
list($test, $params) = $DB->get_in_or_equal($categoryids);
$containedcourses = $DB->get_records_sql(
"SELECT id,1 FROM {course} c WHERE c.category $test", $params);
$containscourses = false;
if ($containedcourses) {
$containscourses = true;
foreach ($containedcourses as $courseid => $notused) {
if ($candeletecontent && !can_delete_course($courseid)) {
$candeletecontent = false;
break;
}
}
}
$mform = & $this->_form;
$this->_category = $this->_customdata;
$categorycontext = context_coursecat::instance($this->_category->id);

/// Are there any questions in the question bank here?
$containsquestions = question_context_has_any_questions($categorycontext);
// Check permissions, to see if it OK to give the option to delete
// the contents, rather than move elsewhere.
$candeletecontent = $this->_category->can_delete_full();

/// Get the list of categories we might be able to move to.
$testcaps = array();
if ($containscourses) {
$testcaps[] = 'moodle/course:create';
}
if ($containscategories || $containsquestions) {
$testcaps[] = 'moodle/category:manage';
}
$displaylist = array();
$notused = array();
if (!empty($testcaps)) {
make_categories_list($displaylist, $notused, $testcaps, $category->id);
}
// Get the list of categories we might be able to move to.
$displaylist = $this->_category->move_content_targets_list();

/// Now build the options.
// Now build the options.
$options = array();
if ($displaylist) {
$options[0] = get_string('movecontentstoanothercategory');
}
if ($candeletecontent) {
$options[1] = get_string('deleteallcannotundo');
}
if (empty($options)) {
print_error('youcannotdeletecategory', 'error', 'index.php', $this->_category->get_formatted_name());
}

/// Now build the form.
$mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name, true, array('context' => $categorycontext))));
// Now build the form.
$mform->addElement('header','general', get_string('categorycurrentcontents', '', $this->_category->get_formatted_name()));

if ($containscourses || $containscategories || $containsquestions) {
if (empty($options)) {
print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name, true, array('context' => $categorycontext)));
}
// Describe the contents of this category.
$contents = '';
if ($this->_category->has_children()) {
$contents .= '<li>' . get_string('subcategories') . '</li>';
}
if ($this->_category->has_courses()) {
$contents .= '<li>' . get_string('courses') . '</li>';
}
if (question_context_has_any_questions($categorycontext)) {
$contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
}
if (!empty($contents)) {
$mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
} else {
$mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
}

/// Describe the contents of this category.
$contents = '<ul>';
if ($containscategories) {
$contents .= '<li>' . get_string('subcategories') . '</li>';
}
if ($containscourses) {
$contents .= '<li>' . get_string('courses') . '</li>';
}
if ($containsquestions) {
$contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
}
$contents .= '</ul>';
$mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents);

/// Give the options for what to do.
$mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
if (count($options) == 1) {
$optionkeys = array_keys($options);
$option = reset($optionkeys);
$mform->hardFreeze('fulldelete');
$mform->setConstant('fulldelete', $option);
}
// Give the options for what to do.
$mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
if (count($options) == 1) {
$optionkeys = array_keys($options);
$option = reset($optionkeys);
$mform->hardFreeze('fulldelete');
$mform->setConstant('fulldelete', $option);
}

if ($displaylist) {
$mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
if (in_array($category->parent, $displaylist)) {
$mform->setDefault('newparent', $category->parent);
}
$mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
if ($displaylist) {
$mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
if (in_array($this->_category->parent, $displaylist)) {
$mform->setDefault('newparent', $this->_category->parent);
}
} else {
$mform->addElement('hidden', 'fulldelete', 1);
$mform->setType('fulldelete', PARAM_INT);
$mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
$mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
}

$mform->addElement('hidden', 'deletecat');
$mform->setType('deletecat', PARAM_ALPHANUM);
$mform->addElement('hidden', 'sure');
$mform->setType('sure', PARAM_ALPHANUM);
$mform->setDefault('sure', md5(serialize($category)));
$mform->setDefault('sure', md5(serialize($this->_category)));

//--------------------------------------------------------------------------------
$this->add_action_buttons(true, get_string('delete'));

$this->set_data(array('deletecat' => $this->_category->id));
}

/// perform some extra moodle validation
Expand Down
11 changes: 4 additions & 7 deletions course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir. '/coursecatlib.php');

class course_edit_form extends moodleform {
protected $course;
Expand Down Expand Up @@ -48,9 +49,7 @@ function definition() {
// verify permissions to change course category or keep current
if (empty($course->id)) {
if (has_capability('moodle/course:create', $categorycontext)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
$displaylist = coursecat::make_categories_list('moodle/course:create');
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category');
$mform->setDefault('category', $category->id);
Expand All @@ -61,12 +60,10 @@ function definition() {
}
} else {
if (has_capability('moodle/course:changecategory', $coursecontext)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
$displaylist = coursecat::make_categories_list('moodle/course:create');
if (!isset($displaylist[$course->category])) {
//always keep current
$displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
$displaylist[$course->category] = coursecat::get($course->category)->get_formatted_name();
}
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category');
Expand Down
41 changes: 8 additions & 33 deletions course/editcategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
*/

require_once('../config.php');
require_once('lib.php');
require_once('editcategory_form.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/course/editcategory_form.php');
require_once($CFG->libdir.'/coursecatlib.php');

require_login();

Expand Down Expand Up @@ -92,42 +93,16 @@
redirect($CFG->wwwroot .'/course/manage.php');
}
} else if ($data = $mform->get_data()) {
$newcategory = new stdClass();
$newcategory->name = $data->name;
$newcategory->idnumber = $data->idnumber;
$newcategory->description_editor = $data->description_editor;
$newcategory->parent = $data->parent; // if $data->parent = 0, the new category will be a top-level category

if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
$newcategory->theme = $data->theme;
}

$logaction = 'update';
if ($id) {
// Update an existing category.
$newcategory->id = $category->id;
if ($newcategory->parent != $category->parent) {
// check category manage capability if parent changed
require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent));
$parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
move_category($newcategory, $parent_cat);
$newcategory = coursecat::get($id);
if ($data->parent != $category->parent && !$newcategory->can_change_parent($data->parent)) {
print_error('cannotmovecategory');
}
$newcategory->update($data, $editoroptions);
} else {
// Create a new category.
$newcategory->description = $data->description_editor['text'];

// Don't overwrite the $newcategory object as it'll be processed by file_postupdate_standard_editor in a moment
$category = create_course_category($newcategory);
$newcategory->id = $category->id;
$categorycontext = $category->context;
$logaction = 'add';
$newcategory = coursecat::create($data, $editoroptions);
}

$newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
$DB->update_record('course_categories', $newcategory);
add_to_log(SITEID, "category", $logaction, "editcategory.php?id=$newcategory->id", $newcategory->id);
fix_course_sortorder();

redirect('manage.php?id='.$newcategory->id);
}

Expand Down
6 changes: 3 additions & 3 deletions course/editcategory_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
}

require_once ($CFG->dirroot.'/course/moodleform_mod.php');
require_once ($CFG->libdir.'/coursecatlib.php');
class editcategory_form extends moodleform {

// form definition
Expand All @@ -18,17 +19,16 @@ function definition() {
if (has_capability('moodle/category:manage', get_system_context()) || $category->parent == 0) {
$options[0] = get_string('top');
}
$parents = array();
if ($category->id) {
// Editing an existing category.
make_categories_list($options, $parents, 'moodle/category:manage', $category->id);
$options += coursecat::make_categories_list('moodle/category:manage', $category->id);
if (empty($options[$category->parent])) {
$options[$category->parent] = $DB->get_field('course_categories', 'name', array('id'=>$category->parent));
}
$strsubmit = get_string('savechanges');
} else {
// Making a new category
make_categories_list($options, $parents, 'moodle/category:manage');
$options += coursecat::make_categories_list('moodle/category:manage');
$strsubmit = get_string('createcategory');
}

Expand Down
Loading

0 comments on commit 0c241b9

Please sign in to comment.