Skip to content

Commit

Permalink
added tag class
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgenfinsveen committed Jul 18, 2023
1 parent 2e0947e commit 21d47f2
Showing 1 changed file with 144 additions and 1 deletion.
145 changes: 144 additions & 1 deletion classes/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit 21d47f2

Please sign in to comment.