Beispiel #1
0
 function definition()
 {
     global $CFG, $DB;
     $mform =& $this->_form;
     $contexts = $this->_customdata['contexts'];
     $currentcat = $this->_customdata['currentcat'];
     //--------------------------------------------------------------------------------
     $mform->addElement('header', 'categoryheader', get_string('addcategory', 'quiz'));
     $questioncategoryel = $mform->addElement('questioncategory', 'parent', get_string('parent', 'quiz'), array('contexts' => $contexts, 'top' => true, 'currentcat' => $currentcat, 'nochildrenof' => $currentcat));
     $mform->setType('parent', PARAM_SEQUENCE);
     if (question_is_only_toplevel_category_in_context($currentcat)) {
         $mform->hardFreeze('parent');
     }
     $mform->setHelpButton('parent', array('categoryparent', get_string('parent', 'quiz'), 'question'));
     $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
     $mform->setDefault('name', '');
     $mform->addRule('name', get_string('categorynamecantbeblank', 'quiz'), 'required', null, 'client');
     $mform->setType('name', PARAM_MULTILANG);
     $mform->addElement('textarea', 'info', get_string('categoryinfo', 'quiz'), array('rows' => '10', 'cols' => '45'));
     $mform->setDefault('info', '');
     $mform->setType('info', PARAM_MULTILANG);
     //--------------------------------------------------------------------------------
     $this->add_action_buttons(false, get_string('addcategory', 'quiz'));
     //--------------------------------------------------------------------------------
     $mform->addElement('hidden', 'id', 0);
     $mform->setType('id', PARAM_INT);
 }
/**
 * Check whether this user is allowed to delete this category.
 *
 * @param integer $todelete a category id.
 */
function question_can_delete_cat($todelete)
{
    if (question_is_only_toplevel_category_in_context($todelete)) {
        error('You can\'t delete that category it is the default category for this context.');
    } else {
        $contextid = get_field('question_categories', 'contextid', 'id', $todelete);
        require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
    }
}
/**
 * Check whether this user is allowed to delete this category.
 *
 * @param int $todelete a category id.
 */
function question_can_delete_cat($todelete)
{
    global $DB;
    if (question_is_only_toplevel_category_in_context($todelete)) {
        print_error('cannotdeletecate', 'question');
    } else {
        $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
        require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
    }
}
Beispiel #4
0
    /**
     * Updates an existing category with given params
     */
    public function update_category($updateid, $newparent, $newname, $newinfo) {
        global $CFG, $DB;
        if (empty($newname)) {
            print_error('categorynamecantbeblank', 'question');
        }

        // Get the record we are updating.
        $oldcat = $DB->get_record('question_categories', array('id' => $updateid));
        $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid);

        if (!empty($newparent) && !$lastcategoryinthiscontext) {
            list($parentid, $tocontextid) = explode(',', $newparent);
        } else {
            $parentid = $oldcat->parent;
            $tocontextid = $oldcat->contextid;
        }

        // Check permissions.
        $fromcontext = context::instance_by_id($oldcat->contextid);
        require_capability('moodle/question:managecategory', $fromcontext);

        // If moving to another context, check permissions some more.
        if ($oldcat->contextid != $tocontextid) {
            $tocontext = context::instance_by_id($tocontextid);
            require_capability('moodle/question:managecategory', $tocontext);
        }

        // Update the category record.
        $cat = new stdClass();
        $cat->id = $updateid;
        $cat->name = $newname;
        $cat->info = $newinfo;
        $cat->parent = $parentid;
        $cat->contextid = $tocontextid;
        $DB->update_record('question_categories', $cat);

        // If the category name has changed, rename any random questions in that category.
        if ($oldcat->name != $cat->name) {
            $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";

            $randomqtype = question_bank::get_qtype('random');
            $randomqname = $randomqtype->question_name($cat, false);
            $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0'));

            $randomqname = $randomqtype->question_name($cat, true);
            $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1'));
        }

        if ($oldcat->contextid != $tocontextid) {
            // Moving to a new context. Must move files belonging to questions.
            question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid);
        }

        // Cat param depends on the context id, so update it.
        $this->pageurl->param('cat', $updateid . ',' . $tocontextid);
        redirect($this->pageurl);
    }
Beispiel #5
0
 /**
  * Updates an existing category with given params
  */
 function update_category($updateid, $newparent, $newname, $newinfo)
 {
     global $CFG, $QTYPES;
     if (empty($newname)) {
         print_error('categorynamecantbeblank', 'quiz');
     }
     // Get the record we are updating.
     $oldcat = get_record('question_categories', 'id', $updateid);
     $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid);
     if (!empty($newparent) && !$lastcategoryinthiscontext) {
         list($parentid, $tocontextid) = explode(',', $newparent);
     } else {
         $parentid = $oldcat->parent;
         $tocontextid = $oldcat->contextid;
     }
     // Check permissions.
     $fromcontext = get_context_instance_by_id($oldcat->contextid);
     require_capability('moodle/question:managecategory', $fromcontext);
     // If moving to another context, check permissions some more.
     if ($oldcat->contextid != $tocontextid) {
         $tocontext = get_context_instance_by_id($tocontextid);
         require_capability('moodle/question:managecategory', $tocontext);
     }
     // Update the category record.
     $cat = NULL;
     $cat->id = $updateid;
     $cat->name = $newname;
     $cat->info = $newinfo;
     $cat->parent = $parentid;
     // We don't change $cat->contextid here, if necessary we redirect to contextmove.php later.
     if (!update_record("question_categories", $cat)) {
         error("Could not update the category '{$newname}'", $this->pageurl->out());
     }
     // If the category name has changed, rename any random questions in that category.
     if (addslashes($oldcat->name) != $cat->name) {
         $randomqname = $QTYPES[RANDOM]->question_name($cat);
         set_field('question', 'name', addslashes($randomqname), 'category', $cat->id, 'qtype', RANDOM);
         // Ignore errors here. It is not a big deal if the questions are not renamed.
     }
     // Then redirect to an appropriate place.
     if ($oldcat->contextid == $tocontextid) {
         // not moving contexts
         redirect($this->pageurl->out());
     } else {
         redirect($CFG->wwwroot . '/question/contextmove.php?' . $this->pageurl->get_query_string(array('cattomove' => $updateid, 'toparent' => $newparent)));
     }
 }
 /**
  * Updates an existing category with given params
  */
 public function update_category($updateid, $newparent, $newname, $newinfo)
 {
     global $CFG, $QTYPES, $DB;
     if (empty($newname)) {
         print_error('categorynamecantbeblank', 'quiz');
     }
     // Get the record we are updating.
     $oldcat = $DB->get_record('question_categories', array('id' => $updateid));
     $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid);
     if (!empty($newparent) && !$lastcategoryinthiscontext) {
         list($parentid, $tocontextid) = explode(',', $newparent);
     } else {
         $parentid = $oldcat->parent;
         $tocontextid = $oldcat->contextid;
     }
     // Check permissions.
     $fromcontext = get_context_instance_by_id($oldcat->contextid);
     require_capability('moodle/question:managecategory', $fromcontext);
     // If moving to another context, check permissions some more.
     if ($oldcat->contextid != $tocontextid) {
         $tocontext = get_context_instance_by_id($tocontextid);
         require_capability('moodle/question:managecategory', $tocontext);
     }
     // Update the category record.
     $cat = NULL;
     $cat->id = $updateid;
     $cat->name = $newname;
     $cat->info = $newinfo;
     $cat->parent = $parentid;
     // We don't change $cat->contextid here, if necessary we redirect to contextmove.php later.
     if (!$DB->update_record('question_categories', $cat)) {
         print_error('cannotupdatecate', 'question', $this->pageurl->out(), $newname);
     }
     // If the category name has changed, rename any random questions in that category.
     if ($oldcat->name != $cat->name) {
         $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";
         $randomqname = $QTYPES[RANDOM]->question_name($cat, false);
         $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0'));
         $randomqname = $QTYPES[RANDOM]->question_name($cat, true);
         $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1'));
     }
     // Then redirect to an appropriate place.
     if ($oldcat->contextid == $tocontextid) {
         // not moving contexts
         redirect($this->pageurl->out());
     } else {
         redirect($CFG->wwwroot . '/question/contextmove.php?' . $this->pageurl->get_query_string(array('cattomove' => $updateid, 'toparent' => $newparent)));
     }
 }