Auto-create encounter reason flag and some forgotten fixes. (#7681)
[openemr.git] / library / classes / Note.class.php
blob138508126705e103a515a48e24c99994051b2532
1 <?php
3 /**
4 * class Note
5 * This class offers functionality to store sequential comments/notes about an external object or anything with a unique id.
6 * It is not intended that once a note is save it can be editied or changed.
7 */
9 use OpenEMR\Common\ORDataObject\ORDataObject;
11 class Note extends ORDataObject
14 * Database unique identifier
15 * @var id
17 var $id;
20 * DB unique identifier reference to some other table, this is not unique in the notes table
21 * @var int
23 var $foreign_id;
26 * Narrative comments about whatever object is represented by the foreign id this note is associated with
27 * @var string upto 255 character string
29 var $note;
32 * Foreign key identifier of who initially persisited the note,
33 * potentially ownership could be changed but that would be up to an external non-document object process
34 * @var int
36 var $owner;
39 * Date the note was first persisted
40 * @var date
42 var $date;
45 * Timestamp of the last time the note was changed and persisted, auto maintained by DB, manually change at your own peril
46 * @var int
48 var $revision;
50 /**
51 * Constructor sets all Note attributes to their default value
52 * @param int $id optional existing id of a specific note, if omitted a "blank" note is created
54 function __construct($id = "")
56 //call the parent constructor so we have a _db to work with
57 parent::__construct();
59 //shore up the most basic ORDataObject bits
60 $this->id = $id;
61 $this->_table = "notes";
63 $this->note = "";
64 $this->date = date("Y-m-d H:i:s");
66 if ($id != "") {
67 $this->populate();
71 /**
72 * Convenience function to get an array of many document objects
73 * For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
74 * @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
76 public static function notes_factory($foreign_id = "")
78 $notes = array();
80 $sqlArray = array();
82 if (empty($foreign_id)) {
83 $foreign_id_sql = " like '%'";
84 } else {
85 $foreign_id_sql = " = ?";
86 $sqlArray[] = strval($foreign_id);
89 $d = new note();
90 $sql = "SELECT id FROM " . escape_table_name($d->_table) . " WHERE foreign_id " . $foreign_id_sql . " ORDER BY DATE DESC";
91 //echo $sql;
92 $result = $d->_db->Execute($sql, $sqlArray);
94 while ($result && !$result->EOF) {
95 $notes[] = new Note($result->fields['id']);
96 $result->MoveNext();
99 return $notes;
102 public function getOwnerName()
104 if (!empty($this->owner)) {
105 $user_info = sqlQuery("SELECT `fname`, `lname` FROM `users` where `id`=?", [$this->owner]);
106 if (!empty($user_info)) {
107 return ($user_info['fname'] . " " . $user_info['lname']);
113 * Convenience function to generate string debug data about the object
115 function toString($html = false)
117 $string .= "\n"
118 . "ID: " . $this->id . "\n"
119 . "FID: " . $this->foreign_id . "\n"
120 . "note: " . $this->note . "\n"
121 . "date: " . $this->date . "\n"
122 . "owner: " . $this->owner . "\n"
123 . "revision: " . $this->revision . "\n";
125 if ($html) {
126 return nl2br($string);
127 } else {
128 return $string;
132 /**#@+
133 * Getter/Setter methods used by reflection to affect object in persist/poulate operations
134 * @param mixed new value for given attribute
136 function set_id($id)
138 $this->id = $id;
140 function get_id()
142 return $this->id;
144 function set_foreign_id($fid)
146 $this->foreign_id = $fid;
148 function get_foreign_id()
150 return $this->foreign_id;
152 function set_note($note)
154 $this->note = $note;
156 function get_note()
158 return $this->note;
160 function set_date($date)
162 $this->date = $date;
164 function get_date()
166 return $this->date;
168 function set_owner($owner)
170 $this->owner = $owner;
172 function get_owner()
174 return $this->owner;
177 * No getter for revision because it is updated automatically by the DB.
179 function set_revision($revision)
181 $this->revision = $revision;
185 * Overridden function to store current object state in the db.
186 * This overide is to allow for a "just in time" foreign id, often this is needed
187 * when the object is never directly exposed and is handled as part of a larger
188 * object hierarchy.
189 * @param int $fid foreign id that should be used so that this note can be related (joined) on it later
192 function persist($fid = "")
194 if (!empty($fid)) {
195 $this->foreign_id = $fid;
198 parent::persist();
200 } // end of Note