Example #1
0
 /**
  * Updates the information about the tag
  *
  * @param array|stdClass $data data to update, may contain: tagtype, description, descriptionformat, rawname
  * @return bool whether the tag was updated. False may be returned if: all new values match the existing,
  *         or an invalid tagtype was supplied, or it was attempted to rename the tag to the name that is already used.
  */
 public function update($data)
 {
     global $DB, $COURSE;
     $allowedfields = array('tagtype', 'description', 'descriptionformat', 'rawname');
     $data = (array) $data;
     if ($extrafields = array_diff(array_keys($data), $allowedfields)) {
         debugging('The field(s) ' . join(', ', $extrafields) . ' will be ignored when updating the tag', DEBUG_DEVELOPER);
     }
     $data = array_intersect_key($data, array_fill_keys($allowedfields, 1));
     $this->ensure_fields_exist(array_merge(array('tagcollid', 'userid', 'name', 'rawname'), array_keys($data)), 'update');
     // Validate the tag name.
     if (array_key_exists('rawname', $data)) {
         $data['rawname'] = clean_param($data['rawname'], PARAM_TAG);
         $name = core_text::strtolower($data['rawname']);
         if (!$name) {
             unset($data['rawname']);
         } else {
             if ($existing = static::get_by_name($this->tagcollid, $name, 'id')) {
                 // Prevent the rename if a tag with that name already exists.
                 if ($existing->id != $this->id) {
                     debugging('New tag name already exists, you should check it before calling core_tag_tag::update()', DEBUG_DEVELOPER);
                     unset($data['rawname']);
                 }
             }
         }
         if (isset($data['rawname'])) {
             $data['name'] = $name;
         }
     }
     // Validate the tag type.
     if (array_key_exists('tagtype', $data) && $data['tagtype'] !== 'official' && $data['tagtype'] !== 'default') {
         debugging('Unrecognised tag type "' . $data['tagtype'] . '" ignored when updating the tag', DEBUG_DEVELOPER);
         unset($data['tagtype']);
     }
     // Find only the attributes that need to be changed.
     $originalname = $this->name;
     foreach ($data as $key => $value) {
         if ($this->record->{$key} !== $value) {
             $this->record->{$key} = $value;
         } else {
             unset($data[$key]);
         }
     }
     if (empty($data)) {
         return false;
     }
     $data['id'] = $this->id;
     $data['timemodified'] = time();
     $DB->update_record('tag', $data);
     $event = \core\event\tag_updated::create(array('objectid' => $this->id, 'relateduserid' => $this->userid, 'context' => context_system::instance(), 'other' => array('name' => $this->name, 'rawname' => $this->rawname)));
     if (isset($data['rawname'])) {
         $event->set_legacy_logdata(array($COURSE->id, 'tag', 'update', 'index.php?id=' . $this->id, $originalname . '->' . $this->name));
     }
     $event->trigger();
     return true;
 }
Example #2
0
    /**
     * Update tags
     *
     * @param array $tags
     */
    public static function update_tags($tags) {
        global $CFG, $PAGE, $DB;
        require_once($CFG->dirroot.'/tag/lib.php');

        // Validate and normalize parameters.
        $tags = self::validate_parameters(self::update_tags_parameters(), array('tags' => $tags));

        $systemcontext = context_system::instance();
        $canmanage = has_capability('moodle/tag:manage', $systemcontext);
        $canedit = has_capability('moodle/tag:edit', $systemcontext);
        $warnings = array();

        if (empty($CFG->usetags)) {
            throw new moodle_exception('tagsaredisabled', 'tag');
        }

        $renderer = $PAGE->get_renderer('core');
        foreach ($tags['tags'] as $tag) {
            $tag = (array)$tag;
            if (array_key_exists('rawname', $tag)) {
                $tag['rawname'] = clean_param($tag['rawname'], PARAM_TAG);
                if (empty($tag['rawname'])) {
                    unset($tag['rawname']);
                } else {
                    $tag['name'] = core_text::strtolower($tag['rawname']);
                }
            }
            if (!$canmanage) {
                // User without manage capability can not change any fields except for descriptions.
                $tag = array_intersect_key($tag, array('id' => 1,
                    'description' => 1, 'descriptionformat' => 1));
            }
            if (!$canedit) {
                // User without edit capability can not change description.
                $tag = array_diff_key($tag,
                        array('description' => 1, 'descriptionformat' => 1));
            }
            if (count($tag) <= 1) {
                $warnings[] = array(
                    'item' => $tag['id'],
                    'warningcode' => 'nothingtoupdate',
                    'message' => get_string('nothingtoupdate', 'core_tag')
                );
                continue;
            }
            if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) {
                $warnings[] = array(
                    'item' => $tag['id'],
                    'warningcode' => 'tagnotfound',
                    'message' => get_string('tagnotfound', 'error')
                );
                continue;
            }
            // First check if new tag name is allowed.
            if (!empty($tag['name']) && ($existing = $DB->get_record('tag', array('name' => $tag['name']), 'id'))) {
                if ($existing->id != $tag['id']) {
                    $warnings[] = array(
                        'item' => $tag['id'],
                        'warningcode' => 'namesalreadybeeingused',
                        'message' => get_string('namesalreadybeeingused', 'core_tag')
                    );
                    continue;
                }
            }
            if (array_key_exists('official', $tag)) {
                $tag['tagtype'] = $tag['official'] ? 'official' : 'default';
                unset($tag['official']);
            }
            $tag['timemodified'] = time();
            $DB->update_record('tag', $tag);

            foreach ($tag as $key => $value) {
                $tagobject->$key = $value;
            }

            $event = \core\event\tag_updated::create(array(
                'objectid' => $tagobject->id,
                'relateduserid' => $tagobject->userid,
                'context' => context_system::instance(),
                'other' => array(
                    'name' => $tagobject->name,
                    'rawname' => $tagobject->rawname
                )
            ));
            $event->trigger();
        }
        return array('warnings' => $warnings);
    }
Example #3
0
/**
 * Change the "value" of a tag, and update the associated 'name'.
 *
 * @package  core_tag
 * @category tag
 * @access   public
 * @param    int      $tagid  the id of the tag to modify
 * @param    string   $newrawname the new rawname
 * @return   bool     true on success, false otherwise
 */
function tag_rename($tagid, $newrawname)
{
    global $COURSE, $DB;
    $norm = tag_normalize($newrawname, TAG_CASE_ORIGINAL);
    if (!($newrawname_clean = array_shift($norm))) {
        return false;
    }
    if (!($newname_clean = core_text::strtolower($newrawname_clean))) {
        return false;
    }
    // Prevent the rename if a tag with that name already exists
    if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
        if ($existing->id != $tagid) {
            // Another tag already exists with this name
            return false;
        }
    }
    if ($tag = tag_get('id', $tagid, 'id, userid, name, rawname')) {
        // Store the name before we change it.
        $oldname = $tag->name;
        $tag->rawname = $newrawname_clean;
        $tag->name = $newname_clean;
        $tag->timemodified = time();
        $DB->update_record('tag', $tag);
        $event = \core\event\tag_updated::create(array('objectid' => $tag->id, 'relateduserid' => $tag->userid, 'context' => context_system::instance(), 'other' => array('name' => $newname_clean, 'rawname' => $newrawname_clean)));
        $event->set_legacy_logdata(array($COURSE->id, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->' . $tag->name));
        $event->trigger();
        return true;
    }
    return false;
}