diff --git a/classes/tag.php b/classes/tag.php index f58c4bd..d61219d 100644 --- a/classes/tag.php +++ b/classes/tag.php @@ -32,7 +32,150 @@ */ class tag extends referable { + /** + * @var \stdClass + */ + protected $tag = null; + + + /** + * Constructor. + * + * @param int|\stdClass $tag + * @return void + */ + public function __construct($tag) { + global $DB; + if (is_scalar($tag)) { + $tag = $DB->get_record('local_qtracker_tag', array('id' => $tag), '*', MUST_EXIST); + if (!$tag) { + throw new \moodle_exception('errorunexistingmodel', 'analytics', '', $tag); + } + } + $this->tag = $tag; + } + + + /** + * Returns the tag id. + * + * @return int + */ public function get_id() { - return null; + return $this->tag->id; + } + + + /** + * Returns the tag name. + * + * @return string + */ + public function get_name() { + return $this->tag->name; + } + + + /** + * Returns the tag color. + * + * @return string + */ + public function get_color() { + return $this->tag->color; + } + + + /** + * Returns a plain \stdClass with the tag data. + * + * @return \stdClass + */ + public function get_tag_obj() { + return $this->tag; + } + + + /** + * Loads and returns tag with id $tagid + * + * @param int $tagid + * @return tag|null + */ + public static function load(int $tagid) { + global $DB; + $tagobj = $DB->get_record('local_qtracker_tag', ['id' => $tagid]); + if ($tagobj === false) { + return null; + } + return new tag($tagobj); + } + + + /** + * Creates a new tag. + * + * @param string $name + * @param string $color + * + * @return tag + */ + public static function create($name, $color) { + global $DB; + + $tagobj = new \stdClass(); + $tagobj->name = $name; + $tagobj->color = $color; + + $id = $DB->insert_record('local_qtracker_tag', $tagobj); + + $tagobj->id = $id; + + return new tag($tagobj); + } + + + /** + * Sets the name of the tag. + * + * @param string $name + */ + public function set_name($name) { + global $DB; + $this->tag->name = $name; + $DB->update_record('local_qtracker_tag', $this->tag); + } + + + /** + * Sets the color of the tag. + * + * @param string $color + */ + public function set_color($color) { + global $DB; + $this->tag->color = $color; + $DB->update_record('local_qtracker_tag', $this->tag); + } + + + /** + * Deletes the tag and all its references. + * + * @return void + */ + public function delete() { + global $DB; + + $outrefs = $this->get_outgoing_references(); + foreach ($outrefs as $outref) { + $outref->delete(); + } + $inrefs = $this->get_incoming_references(); + foreach ($inrefs as $inref) { + $inref->delete(); + } + + return $DB->delete_records('local_qtracker_tag', array('id' => $this->get_id())); } }